Exemplo n.º 1
0
 private boolean parseGlobalDirective() {
   boolean eol = false;
   do {
     IToken t = nextToken();
     if (t == null) {
       break;
     }
     switch (t.getType()) {
       case IToken.tIDENTIFIER:
         registerGlobalLabel(t.getImage());
         break;
       case Lexer.tNEWLINE:
         eol = true;
         break;
       default:
         if (fLineSeparatorChar != 0) {
           if (t.getLength() == 1 && t.getCharImage()[0] == fLineSeparatorChar) {
             eol = true;
           }
         }
     }
   } while (!eol);
   return eol;
 }
Exemplo n.º 2
0
  /**
   * Build the model.
   *
   * @param source
   * @throws CModelException
   */
  private void buildModel(final char[] source) throws CModelException {
    fGlobals = new HashMap<String, Counter>();
    fLabels = new HashMap<String, Counter>();
    fLastLabel = null;
    fLastGlobalLabel = null;
    fLastLabelEndOffset = 0;

    final LexerOptions lexerOptions = new LexerOptions();
    fLexer = new Lexer(source, lexerOptions, new LexerLog(), null);

    // if true the next token is the first on a (logical) line
    boolean firstTokenOnLine = true;
    // next token can be an instruction or a label
    boolean expectInstruction = true;
    // inside instruction
    boolean inInstruction = false;

    IToken token = nextToken();
    while (token != null) {
      switch (token.getType()) {
        case IToken.tPOUND:
          if (fLexer.currentTokenIsFirstOnLine()) {
            parsePPDirective(fTranslationUnit);
          }
          break;
        case IToken.tDOT:
          // assembly directive?
          firstTokenOnLine = false;
          if (expectInstruction) {
            expectInstruction = false;
            token = nextToken();
            if (token != null && token.getType() == IToken.tIDENTIFIER) {
              String text = token.getImage();
              if (isGlobalDirective(text)) {
                firstTokenOnLine = parseGlobalDirective();
              } else if (isDataDirective(text)) {
                inInstruction = true;
              }
            }
          }
          break;
        case IToken.tIDENTIFIER:
          // identifier may be a label or part of an instruction
          if (firstTokenOnLine) {
            // peek next char
            char nextChar = source[token.getEndOffset()];
            if (nextChar == ':') {
              createLabel(fTranslationUnit, token);
              expectInstruction = true;
            } else {
              expectInstruction = false;
              inInstruction = true;
            }
            firstTokenOnLine = false;
          } else if (expectInstruction) {
            expectInstruction = false;
            inInstruction = true;
          }
          break;
        case Lexer.tNEWLINE:
          if (!firstTokenOnLine) {
            firstTokenOnLine = true;
            if (inInstruction) {
              fLastLabelEndOffset = fLexer.currentToken().getEndOffset();
            }
          }
          break;
        default:
          expectInstruction = false;
          firstTokenOnLine = false;
          if (fLineSeparatorChar != 0) {
            if (token.getLength() == 1 && token.getCharImage()[0] == fLineSeparatorChar) {
              firstTokenOnLine = true;
            }
          }
      }
      if (firstTokenOnLine) {
        expectInstruction = true;
        inInstruction = false;
      }
      token = nextToken();
    }
    if (!firstTokenOnLine && inInstruction) {
      fLastLabelEndOffset = fLexer.currentToken().getEndOffset();
    }
    if (fLastLabel != null) {
      fixupLastLabel();
    }
    if (fLastGlobalLabel != null) {
      fixupLastGlobalLabel();
    }
  }