Пример #1
0
  void CPPLexer::declarationOrDefinition() { // A scope declaration or definition
                                             // of a function, class (or some macro-ed stuff e.g. CALLME();) or local variables

                                             // Skip whitespaces
    while (str->at(pos) == ' ') {
      pos++;
    }

    // Handle any keyword or identifier until a terminator character
    bool foundSegment = false;
    size_t startSegment = pos;
    while ((str->at(pos) >= '0' && str->at(pos) <= '9')
      || (str->at(pos) >= 'A' && str->at(pos) <= 'Z')
      || (str->at(pos) >= 'a' && str->at(pos) <= 'z')
      || str->at(pos) == '_') {
      pos++;
    }
    if (pos > startSegment) { // We found something
      Style s = Normal;

      // It might be a reserved keyword
      std::string segment = str->substr(startSegment, pos - startSegment);
      if (m_reservedKeywords.find(segment) != m_reservedKeywords.end()) {
        // For purely aesthetic reasons, style the keywords which aren't private/protected/public
        // in an inner scope with a different style
        if (m_scopesStack.size() > 0 && segment.compare("protected") != 0
          && segment.compare("private") != 0 && segment.compare("public") != 0)
          s = KeywordInnerScope;
        else
          s = Keyword;
        if ((segment.compare("class") == 0 || segment.compare("struct") == 0) &&
          m_classKeywordActiveOnScope == -2 /* No inner class support for now */)
          m_classKeywordActiveOnScope = -1; // We keep track of this since a class scope is *not* a local
                                            // scope, but rather should be treated as the global scope. If we encounter a ';' before any '{', this
                                            // value gets back to -2, i.e. 'no class keyword active'. If we join a scope, this gets set to the
                                            // scope number and from that point forward whenever we're in that scope, no function call can be
                                            // used (only declarations). If we pop out of that function scope, it returns to -2.
      }
      else {

        // Or perhaps a literal (e.g. 11)
        std::regex lit("\\d+[uUlL]?[ull]?[ULL]?[UL]?[ul]?[ll]?[LL]?");
        std::regex lit2("0[xbX][\\da-fA-F]+");
        if (std::regex_match(segment, lit) || std::regex_match(segment, lit2))
          s = Literal;

      }

      // Assign a Keyword or Normal style and later, if we find (, make it a function declaration
      addSegment(startSegment, pos - startSegment, s);
      foundSegment = true;
    }
    // Skip whitespaces and stuff that we're not interested in
    while (str->at(pos) == ' ' || str->at(pos) == '\n') {
      pos++;
    }

    if (str->at(pos) == '(') {

      // Check for the scopes stack and, if we're not in a global scope, mark this as function call.
      // Notice that class member functions aren't marked as function calls but rather as identifiers.
      if (foundSegment && !m_scopesStack.empty() && m_classKeywordActiveOnScope != m_scopesStack.top() &&
        styleDb->styleSegment[styleDb->styleSegment.size() - 1].style != Keyword) {

        styleDb->styleSegment[styleDb->styleSegment.size() - 1].style = FunctionCall;

        // Also set the same style for all the linked previous segments
        for (auto i : m_adaptPreviousSegments)
          styleDb->styleSegment[i].style = FunctionCall;
        m_adaptPreviousSegments.clear();

      }
      else if (foundSegment && (m_scopesStack.empty() || m_classKeywordActiveOnScope == m_scopesStack.top()) &&
        styleDb->styleSegment[styleDb->styleSegment.size() - 1].style != Keyword) {

        styleDb->styleSegment[styleDb->styleSegment.size() - 1].style = Identifier;

        // Also set the same style for all the linked previous segments
        for (auto i : m_adaptPreviousSegments)
          styleDb->styleSegment[i].style = Identifier;
        m_adaptPreviousSegments.clear();
      }

      pos++; // Eat the '('
    }

    if (str->at(pos) == ':' && str->at(pos + 1) == ':') { // :: makes the previous segment part of the new one
      if (styleDb->styleSegment.size() > 0)
        m_adaptPreviousSegments.push_back(static_cast<int>(styleDb->styleSegment.size()) - 1);
    }

    if (foundSegment == false) { // We couldn't find a normal identifier
      if (str->at(pos) == '{') { // Handle entering/exiting scopes
        pos++;
        m_scopesStack.push(static_cast<int>(m_scopesStack.size()));

        if (m_classKeywordActiveOnScope == -1)
          m_classKeywordActiveOnScope = m_scopesStack.top(); // Joined a class scope

      }
      else if (str->at(pos) == '}') {
        pos++;

        if (m_classKeywordActiveOnScope == m_scopesStack.top())
          m_classKeywordActiveOnScope = -2; // Exited a class scope

        m_scopesStack.pop();
      }
      else if (str->at(pos) == '"' || str->at(pos) == '\'') {

        // A quoted string
        char startCharacter = str->at(pos);
        startSegment = pos++;
        while (str->at(pos) != startCharacter)
          ++pos;
        pos++; // Include the terminal character

        addSegment(startSegment, pos - startSegment, QuotedString);
      }
      else {

        // We really can't identify this token, just skip it and assign a regular style

        if (str->at(pos) == ';' && m_classKeywordActiveOnScope == -1)
          m_classKeywordActiveOnScope = -2; // Deactivate the class scope override

        pos++;
      }
    }







    //    // Handle any other keyword or identifier (this could be a function name)
    //    bool foundSegment = false;
    //    size_t startSegment = pos;
    //    while (str->at(pos) != ' ' && str->at(pos) != '(' && str->at(pos) != ';' && str->at(pos) != '{') {
    //      pos++;
    //    }
    //    if (pos > startSegment) { // We found something else
    //      // This might be a declaration if we find another pair of parenthesis or a variable name.
    //      // Assign a Normal style and later, if we find (, make it a function declaration
    //      addSegment(startSegment, pos - startSegment, Normal);
    //      foundSegment = true;
    //    }
    //    // Skip whitespaces and stuff that we're not interested in
    //    while (str->at(pos) == ' ' || str->at(pos) == '\n') {
    //      pos++;
    //    }

    //    if(str->at(pos) == ';') {
    //      pos++;
    //      break; // Hit a terminator
    //    }




    //  // First check if this is a class forward declaration or definition
    //  if (str->substr(pos, 5).compare("class") == 0) { // class
    //    classDeclarationOrDefinition();
    //    return;
    //  }



    //  do { // Continuously parse identifiers/arguments until we hit a terminator

    //    // Handle any other keyword or identifier (this could be a function name)
    //    bool foundSegment = false;
    //    size_t startSegment = pos;
    //    while (str->at(pos) != ' ' && str->at(pos) != '(' && str->at(pos) != ';' && str->at(pos) != '{') {
    //      pos++;
    //    }
    //    if (pos > startSegment) { // We found something else
    //      // This might be a declaration if we find another pair of parenthesis or a variable name.
    //      // Assign a Normal style and later, if we find (, make it a function declaration
    //      addSegment(startSegment, pos - startSegment, Normal);
    //      foundSegment = true;
    //    }
    //    // Skip whitespaces and stuff that we're not interested in
    //    while (str->at(pos) == ' ' || str->at(pos) == '\n') {
    //      pos++;
    //    }

    //    if(str->at(pos) == ';') {
    //      pos++;
    //      break; // Hit a terminator
    //    }

    //    const char *ptr = str->c_str() + pos; // debug

    //    // Handle any (..) section
    //    if (str->at(pos) == '(') {
    //      // Global scope function call

    //      // This also means the previous segment was a declaration or a function call (if we're inside a function)
    //      if (foundSegment && m_scopesStack.empty())
    //        styleDb->styleSegment[styleDb->styleSegment.size()-1].style = Identifier;
    //      else if (foundSegment) // We're in an inner scope
    //        styleDb->styleSegment[styleDb->styleSegment.size()-1].style = FunctionCall;

    //      while (str->at(pos) != ')' && str->at(pos) != ';') {

    //        if (str->at(pos) == '{') {
    //          pos++;
    //          m_scopesStack.push(m_scopesStack.size());
    //        }

    //        if (str->at(pos) == '}') {
    //          pos++;
    //          m_scopesStack.pop();
    //          if (m_scopesStack.empty())
    //            break;
    //        }

    //        pos++;
    //      }
    //      pos++; // Eat the )
    //    }
    //    // Skip whitespaces and stuff that we're not interested in
    //    while (str->at(pos) == ' ' || str->at(pos) == '\n') {
    //      pos++;
    //    }

    //    // There might be a function body at this point, if there is: handle it
    //    if (str->at(pos) == '{') {
    //      pos++;
    //      m_scopesStack.push(m_scopesStack.size());
    //    }

    //    if (str->at(pos) == '}') {
    //      pos++;
    //      m_scopesStack.pop();
    //      if (m_scopesStack.empty())
    //        break;
    //    }

    //  } while (true);

  }
Пример #2
0
void
EBCoarToFineRedist::
define(const DisjointBoxLayout& a_dblFine,
       const DisjointBoxLayout& a_dblCoar,
       const EBISLayout& a_ebislFine,
       const EBISLayout& a_ebislCoar,
       const Box& a_domainCoar,
       const int& a_nref,
       const int& a_nvar,
       int a_redistRad,
       const EBIndexSpace* ebisPtr)
{
  CH_TIME("EBCoarToFineRedist::define");
  m_isDefined = true;
  m_nComp = a_nvar;
  m_refRat = a_nref;
  m_domainCoar = a_domainCoar;
  m_gridsFine = a_dblFine;
  m_gridsCoar = a_dblCoar;
  m_ebislFine = a_ebislFine;
  m_ebislCoar = a_ebislCoar;
  m_redistRad = a_redistRad;

  //created the coarsened fine layout
  m_gridsCedFine = DisjointBoxLayout();
  coarsen(m_gridsCedFine, m_gridsFine, m_refRat);

  CH_assert(ebisPtr->isDefined());
  int nghost = 3*m_redistRad;
  ebisPtr->fillEBISLayout(m_ebislCedFine, m_gridsCedFine,
                          m_domainCoar, nghost);
  m_ebislCedFine.setMaxRefinementRatio(m_refRat, ebisPtr);

  //define the intvectsets over which the objects live
  m_setsCoar.define(m_gridsCoar);
  m_setsCedFine.define(m_gridsCedFine);
  //the buffers for the fine level are really simple.
  //grow the coarsened fine grid by the redist radius
  //and subtract off the coarsened fine grids.
  //intersect with the irregular cells.
  //this way get irregular cells range of this grid on coarse
  //grid.
  {
    CH_TIME("coarsened_fine_sets");
    for (DataIterator dit = m_gridsCedFine.dataIterator();
        dit.ok(); ++dit)
      {
        Box grownBox = grow(m_gridsCedFine.get(dit()), m_redistRad);
        grownBox &= m_domainCoar;
        IntVectSet localIVS = m_ebislCedFine[dit()].getIrregIVS(grownBox);
        //subtract off all boxes on fine level so we get stuff at the
        //coarse-fine interface that will redistribute to this grid
        for (LayoutIterator lit = m_gridsCedFine.layoutIterator();
            lit.ok(); ++lit)
          {
            localIVS -= m_gridsCedFine.get(lit());
          }
        m_setsCedFine[dit()] = localIVS;
      }
  }
  //the coarse buffers are more complicated.
  //Need to intersect irregular cells on this grid
  // with entire coarse/fine interface of width redist radius
  {
    CH_TIME("coarse_sets");
    for (DataIterator dit = m_gridsCoar.dataIterator();
        dit.ok(); ++dit)
      {
        Box coarBox = m_gridsCoar.get(dit());
        //make the complement set the whole
        IntVectSet cfIntComp(coarBox);
        //subtract the CF Interface from each coarsened fine box
        //from the complement
        for (LayoutIterator lit1 = m_gridsCedFine.layoutIterator();
            lit1.ok(); ++lit1)
          {
            Box grownBox = grow(m_gridsCedFine.get(lit1()), m_redistRad);
            grownBox &= m_domainCoar;
            IntVectSet cedCFIVS(grownBox);
            //subtract off all boxes on fine level so we get stuff at the
            //coarse-fine interface that will redistribute to this grid
            for (LayoutIterator lit2 = m_gridsCedFine.layoutIterator();
                lit2.ok(); ++lit2)
              {
                cedCFIVS -= m_gridsCedFine.get(lit2());
              }

            cfIntComp -=cedCFIVS;
          }
        //coarse fine interface is whole box - complement
        IntVectSet cfInterface(coarBox);
        cfInterface -= cfIntComp;

        IntVectSet localIVS = m_ebislCoar[dit()].getIrregIVS(coarBox);
        //intersect with fat coarse-fine interface
        localIVS &= cfInterface;
        m_setsCoar[dit()] = localIVS;
      }
  }

  //define the registers and all that
  defineDataHolders();

  //initialize the buffers to zero
  setToZero();
}