Package org.antlr.grammar.v3

Examples of org.antlr.grammar.v3.CodeGenTreeWalker


    }
  }

  public void parseAndBuildAST(Reader r) {
    // BUILD AST FROM GRAMMAR
    ANTLRLexer lexer;
    try {
      lexer = new ANTLRLexer(new ANTLRReaderStream(r));
    } catch (IOException e) {
      ErrorManager.internalError("unexpected stream error from parsing "+fileName, e);
      return;
    }

    lexer.setFileName(this.getFileName());
    tokenBuffer = new CommonTokenStream(lexer);
    ANTLRParser parser = ANTLRParser.createParser(tokenBuffer);
    parser.setFileName(this.getFileName());
    ANTLRParser.grammar__return result = null;
    try {
      result = parser.grammar_(this);
    }
    catch (RecognitionException re) {
      ErrorManager.internalError("unexpected parser recognition error from "+fileName, re);
    }

        dealWithTreeFilterMode(); // tree grammar and filter=true?

        if ( lexer.hasASTOperator && !buildAST() ) {
      Object value = getOption("output");
      if ( value == null ) {
        ErrorManager.grammarWarning(ErrorManager.MSG_REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION,
                        this, null);
        setOption("output", "AST", null);
      }
      else {
        ErrorManager.grammarError(ErrorManager.MSG_AST_OP_WITH_NON_AST_OUTPUT_OPTION,
                      this, null, value);
      }
    }

    setGrammarTree(result.getTree());

    //if ( grammarTree!=null ) System.out.println("grammar tree: "+grammarTree.toStringTree());

    grammarTree.setUnknownTokenBoundaries();

    setFileName(lexer.getFileName()); // the lexer #src might change name
    if ( grammarTree.findFirstType(ANTLRParser.RULE)==null ) {
      ErrorManager.error(ErrorManager.MSG_NO_RULES, getFileName());
    }
  }
View Full Code Here


    //return (GrammarAST)parser.getAST();
    return r;
  }

  public GrammarAST parseArtificialRule(String ruleText) {
    ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText));
    ANTLRParser parser = ANTLRParser.createParser(new CommonTokenStream(lexer));
    parser.setGrammar(this);
    parser.setGrammarType(this.type);
    try {
      ANTLRParser.rule_return result = parser.rule();
View Full Code Here

                             Token actionToken,
                             String templateActionText)
  {
    // first, parse with antlr.g
    //System.out.println("translate template: "+templateActionText);
    ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(templateActionText));
    lexer.setFileName(grammar.getFileName());
    ANTLRParser parser = ANTLRParser.createParser(new CommonTokenStream(lexer));
    parser.setFileName(grammar.getFileName());
    ANTLRParser.rewrite_template_return parseResult = null;
    try {
      parseResult = parser.rewrite_template();
View Full Code Here

      return;
    }

    lexer.setFileName(this.getFileName());
    tokenBuffer = new CommonTokenStream(lexer);
    ANTLRParser parser = ANTLRParser.createParser(tokenBuffer);
    parser.setFileName(this.getFileName());
    ANTLRParser.grammar__return result = null;
    try {
      result = parser.grammar_(this);
    }
    catch (RecognitionException re) {
      ErrorManager.internalError("unexpected parser recognition error from "+fileName, re);
    }
View Full Code Here

    return r;
  }

  public GrammarAST parseArtificialRule(String ruleText) {
    ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText));
    ANTLRParser parser = ANTLRParser.createParser(new CommonTokenStream(lexer));
    parser.setGrammar(this);
    parser.setGrammarType(this.type);
    try {
      ANTLRParser.rule_return result = parser.rule();
      return result.getTree();
    }
    catch (Exception e) {
      ErrorManager.error(ErrorManager.MSG_ERROR_CREATING_ARTIFICIAL_RULE,
                 e);
View Full Code Here

  {
    // first, parse with antlr.g
    //System.out.println("translate template: "+templateActionText);
    ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(templateActionText));
    lexer.setFileName(grammar.getFileName());
    ANTLRParser parser = ANTLRParser.createParser(new CommonTokenStream(lexer));
    parser.setFileName(grammar.getFileName());
    ANTLRParser.rewrite_template_return parseResult = null;
    try {
      parseResult = parser.rewrite_template();
    }
    catch (RecognitionException re) {
      ErrorManager.grammarError(ErrorManager.MSG_INVALID_TEMPLATE_ACTION,
                      grammar,
                      actionToken,
View Full Code Here

  public String grammarTreeToString(GrammarAST t, boolean showActions) {
    String s;
    try {
      s = t.getLine()+":"+(t.getCharPositionInLine()+1)+": ";
      s += new ANTLRTreePrinter(new CommonTreeNodeStream(t)).toString(this, showActions);
    }
    catch (Exception e) {
      s = "<invalid or missing tree structure>";
    }
    return s;
View Full Code Here

    }
    return s;
  }

  public void printGrammar(PrintStream output) {
    ANTLRTreePrinter printer = new ANTLRTreePrinter(new CommonTreeNodeStream(getGrammarTree()));
    try {
      String g = printer.toString(this, false);
      output.println(g);
    }
    catch (RecognitionException re) {
      ErrorManager.error(ErrorManager.MSG_SYNTAX_ERROR,re);
    }
View Full Code Here

        processArgs(args);
        CharStream input;
        if ( filename!=null ) input = new ANTLRFileStream(filename);
        else input = new ANTLRInputStream(System.in);
        // BUILD AST
        ANTLRv3Lexer lex = new ANTLRv3Lexer(input);
        tokens = new TokenRewriteStream(lex);
        ANTLRv3Parser g = new ANTLRv3Parser(tokens);
        ANTLRv3Parser.grammarDef_return r = g.grammarDef();
        CommonTree t = r.getTree();
        if (tree_option) System.out.println(t.toStringTree());
View Full Code Here

        if ( filename!=null ) input = new ANTLRFileStream(filename);
        else input = new ANTLRInputStream(System.in);
        // BUILD AST
        ANTLRv3Lexer lex = new ANTLRv3Lexer(input);
        tokens = new TokenRewriteStream(lex);
        ANTLRv3Parser g = new ANTLRv3Parser(tokens);
        ANTLRv3Parser.grammarDef_return r = g.grammarDef();
        CommonTree t = r.getTree();
        if (tree_option) System.out.println(t.toStringTree());
        rewrite(g.getTreeAdaptor(),t,g.getTokenNames());
    }
View Full Code Here

TOP

Related Classes of org.antlr.grammar.v3.CodeGenTreeWalker

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.