예제 #1
0
  /**
   * Create block comment from token.
   *
   * @param token Token object.
   * @return DetailAST with BLOCK_COMMENT type.
   */
  private static DetailAST createBlockCommentNode(Token token) {
    final DetailAST blockComment = new DetailAST();
    blockComment.initialize(TokenTypes.BLOCK_COMMENT_BEGIN, "/*");

    // column counting begins from 0
    blockComment.setColumnNo(token.getColumn() - 1);
    blockComment.setLineNo(token.getLine());

    final DetailAST blockCommentContent = new DetailAST();
    blockCommentContent.initialize(token);
    blockCommentContent.setType(TokenTypes.COMMENT_CONTENT);

    // column counting begins from 0
    // plus length of '/*'
    blockCommentContent.setColumnNo(token.getColumn() - 1 + 2);
    blockCommentContent.setLineNo(token.getLine());
    blockCommentContent.setText(token.getText());

    final DetailAST blockCommentClose = new DetailAST();
    blockCommentClose.initialize(TokenTypes.BLOCK_COMMENT_END, "*/");

    final Entry<Integer, Integer> linesColumns =
        countLinesColumns(token.getText(), token.getLine(), token.getColumn());
    blockCommentClose.setLineNo(linesColumns.getKey());
    blockCommentClose.setColumnNo(linesColumns.getValue());

    blockComment.addChild(blockCommentContent);
    blockComment.addChild(blockCommentClose);
    return blockComment;
  }
예제 #2
0
  /**
   * Create single-line comment from token.
   *
   * @param token Token object.
   * @return DetailAST with SINGLE_LINE_COMMENT type.
   */
  private static DetailAST createSlCommentNode(Token token) {
    final DetailAST slComment = new DetailAST();
    slComment.setType(TokenTypes.SINGLE_LINE_COMMENT);
    slComment.setText("//");

    // column counting begins from 0
    slComment.setColumnNo(token.getColumn() - 1);
    slComment.setLineNo(token.getLine());

    final DetailAST slCommentContent = new DetailAST();
    slCommentContent.initialize(token);
    slCommentContent.setType(TokenTypes.COMMENT_CONTENT);

    // column counting begins from 0
    // plus length of '//'
    slCommentContent.setColumnNo(token.getColumn() - 1 + 2);
    slCommentContent.setLineNo(token.getLine());
    slCommentContent.setText(token.getText());

    slComment.addChild(slCommentContent);
    return slComment;
  }
예제 #3
0
파일: Main.java 프로젝트: yurius-r/decaf
  public static void main(String[] args) {
    try {
      CLI.parse(args, new String[0]);

      InputStream inputStream =
          args.length == 0 ? System.in : new java.io.FileInputStream(CLI.infile);

      if (CLI.target == CLI.SCAN) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        Token token;
        boolean done = false;
        while (!done) {
          try {
            for (token = lexer.nextToken();
                token.getType() != DecafParserTokenTypes.EOF;
                token = lexer.nextToken()) {
              String type = "";
              String text = token.getText();

              switch (token.getType()) {
                case DecafScannerTokenTypes.ID:
                  type = " IDENTIFIER";
                  break;
              }
              System.out.println(token.getLine() + type + " " + text);
            }
            done = true;
          } catch (Exception e) {
            // print the error:
            System.out.println(CLI.infile + " " + e);
            lexer.consume();
          }
        }
      } else if (CLI.target == CLI.PARSE || CLI.target == CLI.DEFAULT) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer);
        parser.program();
      }

    } catch (Exception e) {
      // print the error:
      System.out.println(CLI.infile + " " + e);
    }
  }
예제 #4
0
 @Override
 public void initialize(Token tok) {
   super.initialize(tok);
   lineNo = tok.getLine();
   columnNo = tok.getColumn() - 1; // expect columns to start @ 0
 }
예제 #5
0
  public static void main(String[] args) {
    try {
      CLI.parse(args, new String[0]);

      InputStream inputStream =
          args.length == 0 ? System.in : new java.io.FileInputStream(CLI.infile);

      if (CLI.target == CLI.SCAN) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        Token token;
        boolean done = false;
        while (!done) {
          try {
            for (token = lexer.nextToken();
                token.getType() != DecafParserTokenTypes.EOF;
                token = lexer.nextToken()) {
              String type = "";
              String text = token.getText();

              switch (token.getType()) {
                case DecafScannerTokenTypes.ID:
                  type = " IDENTIFIER";
                  break;
                case DecafScannerTokenTypes.CHAR:
                  type = " CHARLITERAL";
                  break;
                case DecafScannerTokenTypes.TRUE:
                case DecafScannerTokenTypes.FALSE:
                  type = " BOOLEANLITERAL";
                  break;
                case DecafScannerTokenTypes.HEX:
                case DecafScannerTokenTypes.DECIMAL:
                  type = " INTLITERAL";
                  break;
                case DecafScannerTokenTypes.STRING:
                  type = " STRINGLITERAL";
                  break;
              }
              System.out.println(token.getLine() + type + " " + text);
            }
            done = true;
          } catch (Exception e) {
            // print the error:
            System.out.println(CLI.infile + " " + e);
            lexer.consume();
          }
        }
      } else if (CLI.target == CLI.PARSE || CLI.target == CLI.DEFAULT) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer, CLI.debug);
        // DecafParser parser = new DecafParser (lexer);
        parser.program();
      } else if (CLI.target == CLI.INTER) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer, CLI.debug);
        parser.program();
        IrNode irRoot = parser.getIrTree();
        SemanticChecker checker = new SemanticChecker(CLI.infile, CLI.debug);
        if (CLI.debug) System.out.println("--- checking -----");
        checker.checkProgram((IrClassDecl) irRoot);
      } else if (CLI.target == CLI.LOWIR) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer, CLI.debug);
        parser.program();
        IrNode irRoot = parser.getIrTree();
        SemanticChecker checker = new SemanticChecker(CLI.infile, CLI.debug);
        if (CLI.debug) System.out.println("--- checking -----");
        checker.checkProgram((IrClassDecl) irRoot);
        CodeGen codegen = new CodeGen(irRoot, CLI.debug);
        codegen.genLowIr();
        codegen.printLowIr();
      }
    } catch (Exception e) {
      // print the error:
      System.out.println(CLI.infile + " " + e);
    }
  }