Examples of GrammarAST


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

  public void checkActionRedefinitions(List<GrammarAST> actions) {
    if ( actions==null ) return;
    String scope = g.getDefaultActionScope();
    String name;
    GrammarAST nameNode;
    for (GrammarAST ampersandAST : actions) {
      nameNode = (GrammarAST)ampersandAST.getChild(0);
      if ( ampersandAST.getChildCount()==2 ) {
        name = nameNode.getText();
      }
      else {
        scope = nameNode.getText();
                name = ampersandAST.getChild(1).getText();
            }
            Set<String> scopeActions = actionScopeToActionNames.get(scope);
            if ( scopeActions==null ) { // init scope
                scopeActions = new HashSet<String>();
View Full Code Here

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

  public void checkRuleArgs(Grammar g, List<GrammarAST> rulerefs) {
    if ( rulerefs==null ) return;
    for (GrammarAST ref : rulerefs) {
      String ruleName = ref.getText();
      Rule r = g.getRule(ruleName);
      GrammarAST arg = (GrammarAST)ref.getFirstChildWithType(ANTLRParser.ARG_ACTION);
      if ( arg!=null && (r==null || r.args==null) ) {
        errMgr.grammarError(ErrorType.RULE_HAS_NO_ARGS,
                      g.fileName, ref.token, ruleName);

      }
View Full Code Here

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

    }
  }

  public void checkForQualifiedRuleIssues(Grammar g, List<GrammarAST> qualifiedRuleRefs) {
    for (GrammarAST dot : qualifiedRuleRefs) {
      GrammarAST grammar = (GrammarAST)dot.getChild(0);
      GrammarAST rule = (GrammarAST)dot.getChild(1);
            g.tool.log("semantics", grammar.getText()+"."+rule.getText());
      Grammar delegate = g.getImportedGrammar(grammar.getText());
      if ( delegate==null ) {
        errMgr.grammarError(ErrorType.NO_SUCH_GRAMMAR_SCOPE,
                      g.fileName, grammar.token, grammar.getText(),
                      rule.getText());
      }
      else {
        if ( g.getRule(grammar.getText(), rule.getText())==null ) {
          errMgr.grammarError(ErrorType.NO_SUCH_RULE_IN_SCOPE,
                        g.fileName, rule.token, grammar.getText(),
                        rule.getText());
        }
      }
    }
  }
View Full Code Here

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

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

  }

  @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

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

  @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

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

    }
  }

  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

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

  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

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

        }
        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

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

   * 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
TOP
Copyright © 2018 www.massapi.com. 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.