congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
GrammarAST
Code IndexAdd Tabnine to your IDE (free)

How to use
GrammarAST
in
org.antlr.v4.tool.ast

Best Java code snippets using org.antlr.v4.tool.ast.GrammarAST (Showing top 20 results out of 315)

origin: uk.co.nichesolutions/antlr4

public GrammarAST stripLeftRecursion(GrammarAST altAST) {
  GrammarAST lrlabel=null;
  GrammarAST first = (GrammarAST)altAST.getChild(0);
  int leftRecurRuleIndex = 0;
  if ( first.getType() == ELEMENT_OPTIONS ) {
    first = (GrammarAST)altAST.getChild(1);
    leftRecurRuleIndex = 1;
  }
  Tree rref = first.getChild(1); // if label=rule
  if ( (first.getType()==RULE_REF && first.getText().equals(ruleName)) ||
     (rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName)) )
  {
    if ( first.getType()==ASSIGN || first.getType()==PLUS_ASSIGN ) lrlabel = (GrammarAST)first.getChild(0);
    // remove rule ref (first child unless options present)
    altAST.deleteChild(leftRecurRuleIndex);
    // reset index so it prints properly (sets token range of
    // ALT to start to right of left recur rule we deleted)
    GrammarAST newFirstChild = (GrammarAST)altAST.getChild(leftRecurRuleIndex);
    altAST.setTokenStartIndex(newFirstChild.getTokenStartIndex());
  }
  return lrlabel;
}
origin: com.tunnelvisionlabs/antlr4

public GrammarAST getNodeWithTokenIndex(int index) {
  if ( this.getToken()!=null && this.getToken().getTokenIndex()==index ) {
    return this;
  }
  // walk all children of root.
  for (int i= 0; i < getChildCount(); i++) {
    GrammarAST child = (GrammarAST)getChild(i);
    GrammarAST result = child.getNodeWithTokenIndex(index);
    if ( result!=null ) {
      return result;
    }
  }
  return null;
}
origin: org.antlr/antlr4

@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);
}
origin: org.antlr/antlr4

  @Override
  public String toString() {
    return label.getText()+" "+type+" "+element.toString();
  }
}
origin: org.antlr/antlr4

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);
  }
}
origin: org.antlr/antlr4

GrammarAST id = (GrammarAST) root.getChild(0);
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(id.token.getInputStream());
GrammarAST channelsRoot = (GrammarAST)root.getFirstChildWithType(ANTLRParser.CHANNELS);
 GrammarAST tokensRoot = (GrammarAST)root.getFirstChildWithType(ANTLRParser.TOKENS_SPEC);
List<GrammarAST> actionRoots = root.getNodesWithType(ANTLRParser.AT);
GrammarAST RULES = (GrammarAST)root.getFirstChildWithType(ANTLRParser.RULES);
Set<String> rootRuleNames = new HashSet<String>();
List<GrammarAST> rootRules = RULES.getNodesWithType(ANTLRParser.RULE);
for (GrammarAST r : rootRules) rootRuleNames.add(r.getChild(0).getText());
List<GrammarAST> rootModes = root.getNodesWithType(ANTLRParser.MODE);
Set<String> rootModeNames = new HashSet<String>();
for (GrammarAST m : rootModes) rootModeNames.add(m.getChild(0).getText());
List<GrammarAST> addedModes = new ArrayList<GrammarAST>();
    rootGrammar.tool.log("grammar", "imported channels: "+imp_channelRoot.getChildren());
    if (channelsRoot==null) {
      channelsRoot = imp_channelRoot.dupTree();
      channelsRoot.g = rootGrammar;
      root.insertChild(1, channelsRoot); // ^(GRAMMAR ID TOKENS...)
    } else {
      for (int c = 0; c < imp_channelRoot.getChildCount(); ++c) {
        String channel = imp_channelRoot.getChild(c).getText();
        boolean channelIsInRootGrammar = false;
        for (int rc = 0; rc < channelsRoot.getChildCount(); ++rc) {
origin: uk.co.nichesolutions/antlr4

if ( optionsRoot!=null && optionsRoot.getChildCount()!=0 ) {
  GrammarAST lexerOptionsRoot = (GrammarAST)adaptor.dupNode(optionsRoot);
  lexerAST.addChild(lexerOptionsRoot);
  GrammarAST[] options = optionsRoot.getChildren().toArray(new GrammarAST[0]);
  for (GrammarAST o : options) {
    String optionName = o.getChild(0).getText();
    if ( Grammar.lexerOptions.contains(optionName) &&
       !Grammar.doNotCopyOptionsToLexer.contains(optionName) )
      lexerOptionsRoot.addChild(optionTree);
      lexerAST.setOption(optionName, (GrammarAST)optionTree.getChild(1));
  if ( e.getType()==ANTLRParser.AT ) {
    lexerAST.addChild((Tree)adaptor.dupTree(e));
    if ( e.getChild(0).getText().equals("lexer") ) {
      actionsWeMoved.add(e);
List<GrammarAST> rulesWeMoved = new ArrayList<GrammarAST>();
GrammarASTWithOptions[] rules;
if (combinedRulesRoot.getChildCount() > 0) {
  rules = combinedRulesRoot.getChildren().toArray(new GrammarASTWithOptions[0]);
  String ruleName = r.getChild(0).getText();
  if (Grammar.isTokenName(ruleName)) {
    lexerRulesRoot.addChild((Tree)adaptor.dupTree(r));
    rulesWeMoved.add(r);
  combinedRulesRoot.deleteChild( r );
origin: uk.co.nichesolutions/antlr4

GrammarAST id = (GrammarAST) root.getChild(0);
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(id.token.getInputStream());
 GrammarAST tokensRoot = (GrammarAST)root.getFirstChildWithType(ANTLRParser.TOKENS_SPEC);
List<GrammarAST> actionRoots = root.getNodesWithType(ANTLRParser.AT);
GrammarAST RULES = (GrammarAST)root.getFirstChildWithType(ANTLRParser.RULES);
Set<String> rootRuleNames = new HashSet<String>();
List<GrammarAST> rootRules = RULES.getNodesWithType(ANTLRParser.RULE);
for (GrammarAST r : rootRules) rootRuleNames.add(r.getChild(0).getText());
    rootGrammar.tool.log("grammar", "imported tokens: "+imp_tokensRoot.getChildren());
    if ( tokensRoot==null ) {
      tokensRoot = (GrammarAST)adaptor.create(ANTLRParser.TOKENS_SPEC, "TOKENS");
      tokensRoot.g = rootGrammar;
      root.insertChild(1, tokensRoot); // ^(GRAMMAR ID TOKENS...)
    tokensRoot.addChildren(Arrays.asList(imp_tokensRoot.getChildren().toArray(new Tree[0])));
      String scopeName = rootGrammar.getDefaultActionScope();
      GrammarAST scope, name, action;
      if ( at.getChildCount()>2 ) { // must have a scope
        scope = (GrammarAST)at.getChild(0);
        scopeName = scope.getText();
        name = (GrammarAST)at.getChild(1);
        action = (GrammarAST)at.getChild(2);
origin: com.tunnelvisionlabs/antlr4

switch (element.getType()) {
case ANTLRParser.ASSIGN:
case ANTLRParser.PLUS_ASSIGN:
  GrammarAST translatedChildElement = translateLeftFactoredElement((GrammarAST)element.getChild(1), factoredRule, variant, mode, includeFactoredElement);
  if (translatedChildElement == null) {
    return null;
  RuleAST ruleAST = (RuleAST)element.getAncestor(ANTLRParser.RULE);
  LOGGER.log(Level.WARNING, "Could not left factor ''{0}'' out of decision in rule ''{1}'': labeled rule references are not yet supported.",
    new Object[] { factoredRule, ruleAST.getChild(0).getText() });
  if (factoredRule.equals(element.getToken().getText())) {
    if (!mode.includeFactoredAlts()) {
      return null;
    root.addChild(adaptor.create(Token.EPSILON, "EPSILON"));
    root.deleteChild(0);
    return root;
  Rule targetRule = _rules.get(element.getToken().getText());
  if (targetRule == null) {
    return null;
  element.setText(element.getText() + marker + factoredRule);
  GrammarAST cloned = element.dupTree();
  if (!translateLeftFactoredDecision(cloned, factoredRule, variant, mode, includeFactoredElement)) {
    return null;
origin: com.impetus.fabric/fabric-jdbc-driver-shaded

public void getNodesWithTypePreorderDFS_(List<GrammarAST> nodes, IntervalSet types) {
  if ( types.contains(this.getType()) ) nodes.add(this);
  // walk all children of root.
  for (int i= 0; i < getChildCount(); i++) {
    GrammarAST child = (GrammarAST)getChild(i);
    child.getNodesWithTypePreorderDFS_(nodes, types);
  }
}
origin: com.tunnelvisionlabs/antlr4

List<GrammarAST> alternatives = block.getAllChildrenWithType(ANTLRParser.ALT);
GrammarAST[] factoredAlternatives = new GrammarAST[alternatives.size()];
GrammarAST[] unfactoredAlternatives = new GrammarAST[alternatives.size()];
  GrammarAST alternative = alternatives.get(i);
  if (mode.includeUnfactoredAlts()) {
    GrammarAST unfactoredAlt = translateLeftFactoredAlternative(alternative.dupTree(), factoredRule, variant, DecisionFactorMode.PARTIAL_UNFACTORED, false);
    unfactoredAlternatives[i] = unfactoredAlt;
    if (unfactoredAlt != null) {
    factoredAlternatives[i] = factoredAlt;
    if (factoredAlt != null) {
      factoredIntervals.add(alternative.getChildIndex());
  for (int i = 0; i < factoredAlternatives.length; i++) {
    GrammarAST translatedAlt = factoredAlternatives[i];
    if (translatedAlt.getChildCount() == 0) {
      adaptor.addChild(translatedAlt, adaptor.create(ANTLRParser.EPSILON, "EPSILON"));
  for (int i = 0; i < unfactoredAlternatives.length; i++) {
    GrammarAST translatedAlt = unfactoredAlternatives[i];
    if (translatedAlt.getChildCount() == 0) {
      adaptor.addChild(translatedAlt, adaptor.create(ANTLRParser.EPSILON, "EPSILON"));
    if (combineWithPrevious) {
      GrammarAST translatedAlt = factoredAlternatives[i];
      if (translatedAlt.getChildCount() == 0) {
        adaptor.addChild(translatedAlt, adaptor.create(ANTLRParser.EPSILON, "EPSILON"));
origin: org.antlr/antlr4

List<GrammarAST> rules = new ArrayList<GrammarAST>(RULES.getAllChildrenWithType(ANTLRParser.RULE));
for (GrammarAST mode : g.ast.getAllChildrenWithType(ANTLRParser.MODE)) {
  rules.addAll(mode.getAllChildrenWithType(ANTLRParser.RULE));
  RuleAST ruleAST = (RuleAST)r;
  GrammarAST ID = (GrammarAST)ruleAST.getChild(0);
  String ruleName = ID.getText();
  RuleAST prev = ruleToAST.get(ruleName);
  if ( prev !=null ) {
    g.tool.errMgr.grammarError(ErrorType.RULE_REDEFINITION,
                  g.fileName,
                  ID.getToken(),
                  ruleName,
                  prevChild.getToken().getLine());
    redefinition = true;
    continue;
origin: com.tunnelvisionlabs/antlr4

protected boolean expandOptionalQuantifiersForBlock(GrammarAST block, boolean variant) {
  List<GrammarAST> children = new ArrayList<GrammarAST>();
  for (int i = 0; i < block.getChildCount(); i++) {
    GrammarAST child = (GrammarAST)block.getChild(i);
    if (child.getType() != ANTLRParser.ALT) {
      children.add(child);
      continue;
  newChildren.addChildren(children);
  block.replaceChildren(0, block.getChildCount() - 1, newChildren);
  block.freshenParentAndChildIndexesDeeply();
  if (!variant && block.getParent() instanceof RuleAST) {
    RuleAST ruleAST = (RuleAST)block.getParent();
    String ruleName = ruleAST.getChild(0).getText();
    Rule r = _rules.get(ruleName);
    List<GrammarAST> blockAlts = block.getAllChildrenWithType(ANTLRParser.ALT);
    r.numberOfAlts = blockAlts.size();
    r.alt = new Alternative[blockAlts.size() + 1];
origin: com.tunnelvisionlabs/antlr4

protected GrammarAST expandOptionalQuantifiersForAlt(GrammarAST alt) {
  if (alt.getChildCount() == 0) {
    return null;
  if (alt.getChild(0).getType() == ANTLRParser.OPTIONAL) {
    GrammarAST root = adaptor.nil();
    GrammarAST alt2 = alt.dupTree();
    alt2.deleteChild(0);
    if (alt2.getChildCount() == 0) {
      adaptor.addChild(alt2, adaptor.create(ANTLRParser.EPSILON, "EPSILON"));
    alt.setChild(0, alt.getChild(0).getChild(0));
    if (alt.getChild(0).getType() == ANTLRParser.BLOCK && alt.getChild(0).getChildCount() == 1 && alt.getChild(0).getChild(0).getType() == ANTLRParser.ALT) {
      GrammarAST list = adaptor.nil();
      for (Object tree : ((GrammarAST)alt.getChild(0).getChild(0)).getChildren()) {
        adaptor.addChild(list, tree);
  else if (alt.getChild(0).getType() == ANTLRParser.CLOSURE) {
    GrammarAST root = adaptor.nil();
    GrammarAST alt2 = alt.dupTree();
    alt2.deleteChild(0);
    if (alt2.getChildCount() == 0) {
      adaptor.addChild(alt2, adaptor.create(ANTLRParser.EPSILON, "EPSILON"));
    for (int i = 0; i < alt.getChild(0).getChildCount(); i++) {
      plusBlockAST.addChild(alt.getChild(0).getChild(i));
    alt.setChild(0, plusBlockAST);
origin: org.antlr/antlr4

@Override
protected void enterLabeledLexerElement(GrammarAST tree) {
  Token label = ((GrammarAST)tree.getChild(0)).getToken();
  g.tool.errMgr.grammarError(ErrorType.V3_LEXER_LABEL,
                g.fileName,
                label,
                label.getText());
}
origin: io.virtdata/virtdata-lib-realer

  @Override
  public void discoverLexerRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers,
                 GrammarAST block)
  {
    int numAlts = block.getChildCount();
    Rule r = new Rule(g, ID.getText(), rule, numAlts);
    r.mode = currentModeName;
    if ( !modifiers.isEmpty() ) r.modifiers = modifiers;
    rules.put(r.name, r);
  }
}
origin: com.impetus.fabric/fabric-jdbc-driver-shaded

  @Override
  public void suffixAlt(AltAST originalAltTree, int alt) {
    AltAST altTree = (AltAST)originalAltTree.dupTree();
    String altLabel = altTree.altLabel!=null ? altTree.altLabel.getText() : null;

    String label = null;
    boolean isListLabel = false;
    GrammarAST lrlabel = stripLeftRecursion(altTree);
    if ( lrlabel!=null ) {
      label = lrlabel.getText();
      isListLabel = lrlabel.getParent().getType() == PLUS_ASSIGN;
      leftRecursiveRuleRefLabels.add(new Pair<GrammarAST,String>(lrlabel,altLabel));
    }
    stripAltLabel(altTree);
    String altText = text(altTree);
    altText = altText.trim();
    LeftRecursiveRuleAltInfo a =
      new LeftRecursiveRuleAltInfo(alt, altText, label, altLabel, isListLabel, originalAltTree);
    suffixAlts.put(alt, a);
//        System.out.println("suffixAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
  }

origin: org.antlr/antlr4

/** Not valid for non-lexers. */
@Override
public Handle range(GrammarAST a, GrammarAST b) {
  g.tool.errMgr.grammarError(ErrorType.TOKEN_RANGE_IN_PARSER, g.fileName,
                a.getToken(),
                a.getToken().getText(),
                b.getToken().getText());
  // From a..b, yield ATN for just a.
  return tokenRef((TerminalAST)a);
}
origin: org.antlr/antlr4

@Override
public GrammarAST dupNode() {
  return new GrammarAST(this);
}
origin: org.antlr/antlr4-maven-plugin

for (Tree id: importDecl.getAllChildrenWithType(ANTLRParser.ID)) {
for (int i = 0, count = options.getChildCount(); i < count; i++) {
  Tree option = options.getChild(i);
org.antlr.v4.tool.astGrammarAST

Most used methods

  • getText
  • getToken
  • getAllChildrenWithType
  • getChild
  • getChildCount
  • getParent
  • <init>
  • addChild
  • addChildren
  • deleteChild
  • dupNode
  • freshenParentAndChildIndexes
  • dupNode,
  • freshenParentAndChildIndexes,
  • getAltLabel,
  • getAncestor,
  • getAncestors,
  • getCharPositionInLine,
  • getChildIndex,
  • getChildren,
  • getChildrenAsArray,
  • getFirstChildWithType

Popular in Java

  • Making http post requests using okhttp
  • getResourceAsStream (ClassLoader)
  • getSystemService (Context)
  • scheduleAtFixedRate (ScheduledExecutorService)
  • KeyStore (java.security)
    KeyStore is responsible for maintaining cryptographic keys and their owners. The type of the syste
  • Calendar (java.util)
    Calendar is an abstract base class for converting between a Date object and a set of integer fields
  • NoSuchElementException (java.util)
    Thrown when trying to retrieve an element past the end of an Enumeration or Iterator.
  • SortedSet (java.util)
    SortedSet is a Set which iterates over its elements in a sorted order. The order is determined eithe
  • Semaphore (java.util.concurrent)
    A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each #acquire blocks if
  • ThreadPoolExecutor (java.util.concurrent)
    An ExecutorService that executes each submitted task using one of possibly several pooled threads, n
  • Best plugins for Eclipse
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now