Package org.antlr.v4.tool.ast

Examples of org.antlr.v4.tool.ast.GrammarAST


                    org.antlr.runtime.tree.TreeWizard wiz,
                    List<Pair<GrammarAST,GrammarAST>> lexerRuleToStringLiteral)
  {
    HashMap<String, Object> nodes = new HashMap<String, Object>();
    if ( wiz.parse(r, pattern, nodes) ) {
      GrammarAST litNode = (GrammarAST)nodes.get("lit");
      GrammarAST nameNode = (GrammarAST)nodes.get("name");
      Pair<GrammarAST, GrammarAST> pair =
        new Pair<GrammarAST, GrammarAST>(nameNode, litNode);
      lexerRuleToStringLiteral.add(pair);
      return true;
    }
View Full Code Here


  }

  @Override
  public void finishPrequels(GrammarAST firstPrequel) {
    if ( firstPrequel==null ) return;
    GrammarAST parent = (GrammarAST)firstPrequel.parent;
    List<GrammarAST> options = parent.getAllChildrenWithType(OPTIONS);
    List<GrammarAST> imports = parent.getAllChildrenWithType(IMPORT);
    List<GrammarAST> tokens = parent.getAllChildrenWithType(TOKENS_SPEC);
    checkNumPrequels(options, imports, tokens);
  }
View Full Code Here

  @Override
  public void finishRule(RuleAST rule, GrammarAST ID, GrammarAST block) {
    if ( rule.isLexerRule() ) return;
    BlockAST blk = (BlockAST)rule.getFirstChildWithType(BLOCK);
    int nalts = blk.getChildCount();
    GrammarAST idAST = (GrammarAST)rule.getChild(0);
    for (int i=0; i< nalts; i++) {
      AltAST altAST = (AltAST)blk.getChild(i);
      if ( altAST.altLabel!=null ) {
        String altLabel = altAST.altLabel.getText();
        // first check that label doesn't conflict with a rule
View Full Code Here

    }
  }

  void checkNumRules(GrammarAST rulesNode) {
    if ( rulesNode.getChildCount()==0 ) {
      GrammarAST root = (GrammarAST)rulesNode.getParent();
      GrammarAST IDNode = (GrammarAST)root.getChild(0);
      g.tool.errMgr.grammarError(ErrorType.NO_RULES, g.fileName,
                     null, IDNode.getText(), g);
    }
  }
View Full Code Here

  public boolean translateLeftRecursiveRule(GrammarRootAST ast,
                        LeftRecursiveRule r,
                        String language)
  {
    //tool.log("grammar", ruleAST.toStringTree());
    GrammarAST prevRuleAST = r.ast;
    String ruleName = prevRuleAST.getChild(0).getText();
    LeftRecursiveRuleAnalyzer leftRecursiveRuleWalker =
      new LeftRecursiveRuleAnalyzer(prevRuleAST, tool, ruleName, language);
    boolean isLeftRec;
    try {
//      System.out.println("TESTING ---------------\n"+
//                 leftRecursiveRuleWalker.text(ruleAST));
      isLeftRec = leftRecursiveRuleWalker.rec_rule();
    }
    catch (RecognitionException re) {
      isLeftRec = false; // didn't match; oh well
    }
    if ( !isLeftRec ) return false;

    // replace old rule's AST
    GrammarAST RULES = (GrammarAST)ast.getFirstChildWithType(ANTLRParser.RULES);
    String newRuleText = leftRecursiveRuleWalker.getArtificialOpPrecRule();
//    System.out.println("created: "+newRuleText);
    RuleAST t = parseArtificialRule(g, newRuleText);

    // reuse the name token from the original AST since it refers to the proper source location in the original grammar
    ((GrammarAST)t.getChild(0)).token = ((GrammarAST)prevRuleAST.getChild(0)).getToken();

    // update grammar AST and set rule's AST.
    RULES.setChild(prevRuleAST.getChildIndex(), t);
    r.ast = t;

    // Reduce sets in newly created rule tree
    GrammarTransformPipeline transform = new GrammarTransformPipeline(g, g.tool);
    transform.reduceBlocksToSets(r.ast);
    transform.expandParameterizedLoops(r.ast);

    // Rerun semantic checks on the new rule
    RuleCollector ruleCollector = new RuleCollector(g);
    ruleCollector.visit(t, "rule");
    BasicSemanticChecks basics = new BasicSemanticChecks(g, ruleCollector);
    // disable the assoc element option checks because they are already
    // handled for the pre-transformed rule.
    basics.checkAssocElementOption = false;
    basics.visit(t, "rule");

    // track recursive alt info for codegen
    r.recPrimaryAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
    r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAlts);
    r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.otherAlts);
    if (r.recPrimaryAlts.isEmpty()) {
      tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.getChild(0)).getToken(), r.name);
    }

    r.recOpAlts = new OrderedHashMap<Integer, LeftRecursiveRuleAltInfo>();
    r.recOpAlts.putAll(leftRecursiveRuleWalker.binaryAlts);
    r.recOpAlts.putAll(leftRecursiveRuleWalker.ternaryAlts);
    r.recOpAlts.putAll(leftRecursiveRuleWalker.suffixAlts);

    // walk alt info records and set their altAST to point to appropriate ALT subtree
    // from freshly created AST
    setAltASTPointers(r, t);

    // update Rule to just one alt and add prec alt
    ActionAST arg = (ActionAST)r.ast.getFirstChildWithType(ANTLRParser.ARG_ACTION);
    if ( arg!=null ) {
      r.args = ScopeParser.parseTypedArgList(arg, arg.getText(), g);
      r.args.type = AttributeDict.DictType.ARG;
      r.args.ast = arg;
      arg.resolver = r.alt[1]; // todo: isn't this Rule or something?
    }

    // define labels on recursive rule refs we delete; they don't point to nodes of course
    // these are so $label in action translation works
    for (Pair<GrammarAST,String> pair : leftRecursiveRuleWalker.leftRecursiveRuleRefLabels) {
      GrammarAST labelNode = pair.a;
      GrammarAST labelOpNode = (GrammarAST)labelNode.getParent();
      GrammarAST elementNode = (GrammarAST)labelOpNode.getChild(1);
      LabelElementPair lp = new LabelElementPair(g, labelNode, elementNode, labelOpNode.getType());
      r.alt[1].labelDefs.map(labelNode.getText(), lp);
    }
    // copy to rule from walker
    r.leftRecursiveRuleRefLabels = leftRecursiveRuleWalker.leftRecursiveRuleRefLabels;
View Full Code Here

        }
        tokenDef = br.readLine();
      }
    }
    catch (FileNotFoundException fnfe) {
      GrammarAST inTree = g.ast.getOptionAST("tokenVocab");
      String inTreeValue = inTree.getToken().getText();
      if ( vocabName.equals(inTreeValue) ) {
        tool.errMgr.grammarError(ErrorType.CANNOT_FIND_TOKENS_FILE_REFD_IN_GRAMMAR,
                     g.fileName,
                     inTree.getToken(),
                     fullFile);
      }
      else { // must be from -D option on cmd-line not token in tree
        tool.errMgr.toolError(ErrorType.CANNOT_FIND_TOKENS_FILE_GIVEN_ON_CMDLINE,
                    fullFile,
View Full Code Here

   * definitions of the same rule or a reference to an undefined rule or
   * parser rule ref in lexer rule.
   */
  public boolean checkForRuleIssues(final Grammar g) {
    // check for redefined rules
    GrammarAST RULES = (GrammarAST)g.ast.getFirstChildWithType(ANTLRParser.RULES);
    List<GrammarAST> rules = new ArrayList<GrammarAST>(RULES.getAllChildrenWithType(ANTLRParser.RULE));
    for (GrammarAST mode : g.ast.getAllChildrenWithType(ANTLRParser.MODE)) {
      rules.addAll(mode.getAllChildrenWithType(ANTLRParser.RULE));
    }

    boolean redefinition = false;
    final Map<String, RuleAST> ruleToAST = new HashMap<String, RuleAST>();
    for (GrammarAST r : rules) {
      RuleAST ruleAST = (RuleAST)r;
      GrammarAST ID = (GrammarAST)ruleAST.getChild(0);
      String ruleName = ID.getText();
      RuleAST prev = ruleToAST.get(ruleName);
      if ( prev !=null ) {
        GrammarAST prevChild = (GrammarAST)prev.getChild(0);
        g.tool.errMgr.grammarError(ErrorType.RULE_REDEFINITION,
                       g.fileName,
                       ID.getToken(),
                       ruleName,
                       prevChild.getToken().getLine());
        redefinition = true;
        continue;
      }
      ruleToAST.put(ruleName, ruleAST);
    }
View Full Code Here

  public List<GrammarRootAST> sortGrammarByTokenVocab(List<String> fileNames) {
//    System.out.println(fileNames);
    Graph<String> g = new Graph<String>();
    List<GrammarRootAST> roots = new ArrayList<GrammarRootAST>();
    for (String fileName : fileNames) {
      GrammarAST t = parseGrammar(fileName);
      if ( t==null || t instanceof GrammarASTErrorNode) continue; // came back as error node
      if ( ((GrammarRootAST)t).hasErrors ) continue;
      GrammarRootAST root = (GrammarRootAST)t;
      roots.add(root);
      root.fileName = fileName;
      String grammarName = root.getChild(0).getText();

      GrammarAST tokenVocabNode = findOptionValueAST(root, "tokenVocab");
      // Make grammars depend on any tokenVocab options
      if ( tokenVocabNode!=null ) {
        String vocabName = tokenVocabNode.getText();
        g.addEdge(grammarName, vocabName);
      }
      // add cycle to graph so we always process a grammar if no error
      // even if no dependency
      g.addEdge(grammarName, grammarName);
View Full Code Here

    return sortedRoots;
  }

  /** Manually get option node from tree; return null if no defined. */
  public static GrammarAST findOptionValueAST(GrammarRootAST root, String option) {
    GrammarAST options = (GrammarAST)root.getFirstChildWithType(ANTLRParser.OPTIONS);
    if ( options!=null && options.getChildCount() > 0 ) {
      for (Object o : options.getChildren()) {
        GrammarAST c = (GrammarAST)o;
        if ( c.getType() == ANTLRParser.ASSIGN &&
           c.getChild(0).getText().equals(option) )
        {
          return (GrammarAST)c.getChild(1);
        }
      }
    }
    return null;
  }
View Full Code Here

      lexer.tokens = tokens;
      ToolANTLRParser p = new ToolANTLRParser(tokens, this);
      p.setTreeAdaptor(adaptor);
      try {
        ParserRuleReturnScope r = p.grammarSpec();
        GrammarAST root = (GrammarAST)r.getTree();
        if ( root instanceof GrammarRootAST) {
          ((GrammarRootAST)root).hasErrors = lexer.getNumberOfSyntaxErrors()>0 || p.getNumberOfSyntaxErrors()>0;
          assert ((GrammarRootAST)root).tokenStream == tokens;
          if ( grammarOptions!=null ) {
            ((GrammarRootAST)root).cmdLineOptions = grammarOptions;
View Full Code Here

TOP

Related Classes of org.antlr.v4.tool.ast.GrammarAST

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.