org.antlr.v4.tool.ast.GrammarAST Java Examples
The following examples show how to use
org.antlr.v4.tool.ast.GrammarAST.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParserFactory.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@Override public Choice getLL1EBNFBlock(GrammarAST ebnfRoot, List<CodeBlockForAlt> alts) { int ebnf = 0; if ( ebnfRoot!=null ) ebnf = ebnfRoot.getType(); Choice c = null; switch ( ebnf ) { case ANTLRParser.OPTIONAL : if ( alts.size()==1 ) c = new LL1OptionalBlockSingleAlt(this, ebnfRoot, alts); else c = new LL1OptionalBlock(this, ebnfRoot, alts); break; case ANTLRParser.CLOSURE : if ( alts.size()==1 ) c = new LL1StarBlockSingleAlt(this, ebnfRoot, alts); else c = getComplexEBNFBlock(ebnfRoot, alts); break; case ANTLRParser.POSITIVE_CLOSURE : if ( alts.size()==1 ) c = new LL1PlusBlockSingleAlt(this, ebnfRoot, alts); else c = getComplexEBNFBlock(ebnfRoot, alts); break; } return c; }
Example #2
Source File: GrammarIssuesCollector.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static Map<String,GrammarAST> getUnusedParserRules(Grammar g) { if ( g.ast==null || g.isLexer() ) return null; List<GrammarAST> ruleNodes = g.ast.getNodesWithTypePreorderDFS(IntervalSet.of(ANTLRParser.RULE_REF)); // in case of errors, we walk AST ourselves // ANTLR's Grammar object might have bailed on rule defs etc... Set<String> ruleRefs = new HashSet<String>(); Map<String,GrammarAST> ruleDefs = new HashMap<String,GrammarAST>(); for (GrammarAST x : ruleNodes) { if ( x.getParent().getType()==ANTLRParser.RULE ) { // System.out.println("def "+x); ruleDefs.put(x.getText(), x); } else if ( x instanceof RuleRefAST) { RuleRefAST r = (RuleRefAST) x; // System.out.println("ref "+r); ruleRefs.add(r.getText()); } } ruleDefs.keySet().removeAll(ruleRefs); return ruleDefs; }
Example #3
Source File: BasicSemanticChecks.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@Override public void discoverLexerRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers, GrammarAST block) { checkInvalidRuleDef(ID.token); if (modifiers != null) { for (GrammarAST tree : modifiers) { if (tree.getType() == ANTLRParser.FRAGMENT) { inFragmentRule = true; } } } if (!inFragmentRule) { nonFragmentRuleCount++; } }
Example #4
Source File: InputPanel.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void setCursorToGrammarRule(Project project, PreviewState previewState, int offset) { Token tokenUnderCursor = ParsingUtils.getTokenUnderCursor(previewState, offset); if ( tokenUnderCursor==null ) { return; } ParseTree tree = previewState.parsingResult.tree; TerminalNode nodeWithToken = (TerminalNode) ParsingUtils.getParseTreeNodeWithToken(tree, tokenUnderCursor); if ( nodeWithToken==null ) { // hidden token return; } ParserRuleContext parent = (ParserRuleContext) nodeWithToken.getParent(); int ruleIndex = parent.getRuleIndex(); Rule rule = previewState.g.getRule(ruleIndex); GrammarAST ruleNameNode = (GrammarAST) rule.ast.getChild(0); int start = ((CommonToken) ruleNameNode.getToken()).getStartIndex(); jumpToGrammarPosition(project, start); }
Example #5
Source File: LexerATNFactory.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@Override public Handle lexerCommand(GrammarAST ID) { LexerAction lexerAction = createLexerAction(ID, null); if (lexerAction != null) { return action(ID, lexerAction); } // fall back to standard action generation for the command ST cmdST = codegenTemplates.getInstanceOf("Lexer" + CharSupport.capitalize(ID.getText()) + "Command"); if (cmdST == null) { g.tool.errMgr.grammarError(ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.token, ID.getText()); return epsilon(ID); } if (cmdST.impl.formalArguments != null && cmdST.impl.formalArguments.containsKey("arg")) { g.tool.errMgr.grammarError(ErrorType.MISSING_LEXER_COMMAND_ARGUMENT, g.fileName, ID.token, ID.getText()); return epsilon(ID); } return action(cmdST.render()); }
Example #6
Source File: LexerATNFactory.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@Override public Handle lexerCallCommand(GrammarAST ID, GrammarAST arg) { LexerAction lexerAction = createLexerAction(ID, arg); if (lexerAction != null) { return action(ID, lexerAction); } // fall back to standard action generation for the command ST cmdST = codegenTemplates.getInstanceOf("Lexer" + CharSupport.capitalize(ID.getText())+ "Command"); if (cmdST == null) { g.tool.errMgr.grammarError(ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.token, ID.getText()); return epsilon(ID); } if (cmdST.impl.formalArguments == null || !cmdST.impl.formalArguments.containsKey("arg")) { g.tool.errMgr.grammarError(ErrorType.UNWANTED_LEXER_COMMAND_ARGUMENT, g.fileName, ID.token, ID.getText()); return epsilon(ID); } cmdST.add("arg", arg.getText()); return action(cmdST.render()); }
Example #7
Source File: LeftRecursiveRuleAnalyzer.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*))) * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*))) */ public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) { if ( t==null ) return false; GrammarAST blk = (GrammarAST)t.getFirstChildWithType(BLOCK); if ( blk==null ) return false; int n = blk.getChildren().size(); for (int i = 0; i < n; i++) { GrammarAST alt = (GrammarAST)blk.getChildren().get(i); Tree first = alt.getChild(0); if ( first==null ) continue; if (first.getType() == ELEMENT_OPTIONS) { first = alt.getChild(1); if (first == null) { continue; } } if ( first.getType()==RULE_REF && first.getText().equals(ruleName) ) return true; Tree rref = first.getChild(1); if ( rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName) ) return true; } return false; }
Example #8
Source File: ParserFactory.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@Override public List<SrcOp> set(GrammarAST setAST, GrammarAST labelAST, boolean invert) { MatchSet matchOp; if ( invert ) matchOp = new MatchNotSet(this, setAST); else matchOp = new MatchSet(this, setAST); if ( labelAST!=null ) { String label = labelAST.getText(); RuleFunction rf = getCurrentRuleFunction(); if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) { defineImplicitLabel(setAST, matchOp); TokenListDecl l = getTokenListLabelDecl(label); rf.addContextDecl(setAST.getAltLabel(), l); } else { Decl d = getTokenLabelDecl(label); matchOp.labels.add(d); rf.addContextDecl(setAST.getAltLabel(), d); } } if ( controller.needsImplicitLabel(setAST, matchOp) ) defineImplicitLabel(setAST, matchOp); AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST); return list(matchOp, listLabelOp); }
Example #9
Source File: SymbolChecks.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
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()); } } } }
Example #10
Source File: SymbolChecks.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
public SymbolChecks(Grammar g, SymbolCollector collector) { this.g = g; this.collector = collector; this.errMgr = g.tool.errMgr; for (GrammarAST tokenId : collector.tokenIDRefs) { tokenIDs.add(tokenId.getText()); } /* System.out.println("rules="+collector.rules); System.out.println("rulerefs="+collector.rulerefs); System.out.println("tokenIDRefs="+collector.tokenIDRefs); System.out.println("terminals="+collector.terminals); System.out.println("strings="+collector.strings); System.out.println("tokensDef="+collector.tokensDefs); System.out.println("actions="+collector.actions); System.out.println("scopes="+collector.scopes); */ }
Example #11
Source File: ParserFactory.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@Override public Choice getChoiceBlock(BlockAST blkAST, List<CodeBlockForAlt> alts, GrammarAST labelAST) { int decision = ((DecisionState)blkAST.atnState).decision; Choice c; if ( !g.tool.force_atn && AnalysisPipeline.disjoint(g.decisionLOOK.get(decision)) ) { c = getLL1ChoiceBlock(blkAST, alts); } else { c = getComplexChoiceBlock(blkAST, alts); } if ( labelAST!=null ) { // for x=(...), define x or x_list String label = labelAST.getText(); Decl d = getTokenLabelDecl(label); c.label = d; getCurrentRuleFunction().addContextDecl(labelAST.getAltLabel(), d); if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) { String listLabel = gen.getTarget().getListLabel(label); TokenListDecl l = new TokenListDecl(this, listLabel); getCurrentRuleFunction().addContextDecl(labelAST.getAltLabel(), l); } } return c; }
Example #12
Source File: MatchSet.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public MatchSet(OutputModelFactory factory, GrammarAST ast) { super(factory, ast); SetTransition st = (SetTransition)ast.atnState.transition(0); int wordSize = factory.getGenerator().getTarget().getInlineTestSetWordSize(); expr = new TestSetInline(factory, null, st.set, wordSize); Decl d = new TokenTypeDecl(factory, expr.varName); factory.getCurrentRuleFunction().addLocalDecl(d); capture = new CaptureNextTokenType(factory,expr.varName); }
Example #13
Source File: RuleFunction.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public List<Decl> getDeclForAltElement(GrammarAST t, String refLabelName, boolean needList) { List<Decl> decls = new ArrayList<Decl>(); if ( t.getType()==RULE_REF ) { Rule rref = factory.getGrammar().getRule(t.getText()); String ctxName = factory.getGenerator().getTarget() .getRuleFunctionContextStructName(rref); if ( needList) { if(factory.getGenerator().getTarget().supportsOverloadedMethods()) decls.add( new ContextRuleListGetterDecl(factory, refLabelName, ctxName) ); decls.add( new ContextRuleListIndexedGetterDecl(factory, refLabelName, ctxName) ); } else { decls.add( new ContextRuleGetterDecl(factory, refLabelName, ctxName) ); } } else { if ( needList ) { if(factory.getGenerator().getTarget().supportsOverloadedMethods()) decls.add( new ContextTokenListGetterDecl(factory, refLabelName) ); decls.add( new ContextTokenListIndexedGetterDecl(factory, refLabelName) ); } else { decls.add( new ContextTokenGetterDecl(factory, refLabelName) ); } } return decls; }
Example #14
Source File: ParserATNFactory.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** From an empty alternative build {@code o-e->o}. */ @Override public Handle epsilon(GrammarAST node) { ATNState left = newState(node); ATNState right = newState(node); epsilon(left, right); node.atnState = left; return new Handle(left, right); }
Example #15
Source File: Choice.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public Choice(OutputModelFactory factory, GrammarAST blkOrEbnfRootAST, List<CodeBlockForAlt> alts) { super(factory, blkOrEbnfRootAST); this.alts = alts; }
Example #16
Source File: PlusBlock.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public PlusBlock(OutputModelFactory factory, GrammarAST plusRoot, List<CodeBlockForAlt> alts) { super(factory, plusRoot, alts); BlockAST blkAST = (BlockAST)plusRoot.getChild(0); PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState; PlusLoopbackState loop = blkStart.loopBackState; stateNumber = blkStart.loopBackState.stateNumber; blockStartStateNumber = blkStart.stateNumber; loopBackStateNumber = loop.stateNumber; this.error = getThrowNoViableAlt(factory, plusRoot, null); decision = loop.decision; }
Example #17
Source File: BasicSemanticChecks.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
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); } }
Example #18
Source File: LeftRecursiveRuleFunction.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public LeftRecursiveRuleFunction(OutputModelFactory factory, LeftRecursiveRule r) { super(factory, r); CodeGenerator gen = factory.getGenerator(); // Since we delete x=lr, we have to manually add decls for all labels // on left-recur refs to proper structs for (Pair<GrammarAST,String> pair : r.leftRecursiveRuleRefLabels) { GrammarAST idAST = pair.a; String altLabel = pair.b; String label = idAST.getText(); GrammarAST rrefAST = (GrammarAST)idAST.getParent().getChild(1); if ( rrefAST.getType() == ANTLRParser.RULE_REF ) { Rule targetRule = factory.getGrammar().getRule(rrefAST.getText()); String ctxName = gen.getTarget().getRuleFunctionContextStructName(targetRule); RuleContextDecl d; if (idAST.getParent().getType() == ANTLRParser.ASSIGN) { d = new RuleContextDecl(factory, label, ctxName); } else { d = new RuleContextListDecl(factory, label, ctxName); } StructDecl struct = ruleCtx; if ( altLabelCtxs!=null ) { StructDecl s = altLabelCtxs.get(altLabel); if ( s!=null ) struct = s; // if alt label, use subctx } struct.addDecl(d); // stick in overall rule's ctx } } }
Example #19
Source File: SymbolChecks.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public void checkForTokenConflicts(List<GrammarAST> tokenIDRefs) { // for (GrammarAST a : tokenIDRefs) { // Token t = a.token; // String ID = t.getText(); // tokenIDs.add(ID); // } }
Example #20
Source File: BasicSemanticChecks.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void discoverRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers, ActionAST arg, ActionAST returns, GrammarAST thrws, GrammarAST options, ActionAST locals, List<GrammarAST> actions, GrammarAST block) { // TODO: chk that all or no alts have "# label" checkInvalidRuleDef(ID.token); }
Example #21
Source File: LeftRecursiveRuleAnalyzer.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public AltAST addPrecedenceArgToRules(AltAST t, int prec) { if ( t==null ) return null; // get all top-level rule refs from ALT List<GrammarAST> outerAltRuleRefs = t.getNodesWithTypePreorderDFS(IntervalSet.of(RULE_REF)); for (GrammarAST x : outerAltRuleRefs) { RuleRefAST rref = (RuleRefAST)x; boolean recursive = rref.getText().equals(ruleName); boolean rightmost = rref == outerAltRuleRefs.get(outerAltRuleRefs.size()-1); if ( recursive && rightmost ) { GrammarAST dummyValueNode = new GrammarAST(new CommonToken(ANTLRParser.INT, ""+prec)); rref.setOption(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME, dummyValueNode); } } return t; }
Example #22
Source File: ParserFactory.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public AddToLabelList getAddToListOpIfListLabelPresent(LabeledOp op, GrammarAST label) { AddToLabelList labelOp = null; if ( label!=null && label.parent.getType()==ANTLRParser.PLUS_ASSIGN ) { String listLabel = gen.getTarget().getListLabel(label.getText()); labelOp = new AddToLabelList(this, listLabel, op.getLabels().get(0)); } return labelOp; }
Example #23
Source File: AnalysisPipeline.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
protected void processLexer() { // make sure all non-fragment lexer rules must match at least one symbol for (Rule rule : g.rules.values()) { if (rule.isFragment()) { continue; } LL1Analyzer analyzer = new LL1Analyzer(g.atn); IntervalSet look = analyzer.LOOK(g.atn.ruleToStartState[rule.index], null); if (look.contains(Token.EPSILON)) { g.tool.errMgr.grammarError(ErrorType.EPSILON_TOKEN, g.fileName, ((GrammarAST)rule.ast.getChild(0)).getToken(), rule.name); } } }
Example #24
Source File: ElementFrequenciesVisitor.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override protected void exitSubrule(GrammarAST tree) { if (tree.getType() == CLOSURE || tree.getType() == POSITIVE_CLOSURE) { for (Map.Entry<String, MutableInt> entry : frequencies.peek().entrySet()) { entry.getValue().v = 2; } } }
Example #25
Source File: ParserATNFactory.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** * {@code (BLOCK (ALT .))} or {@code (BLOCK (ALT 'a') (ALT .))}. */ public static boolean blockHasWildcardAlt(GrammarAST block) { for (Object alt : block.getChildren()) { if ( !(alt instanceof AltAST) ) continue; AltAST altAST = (AltAST)alt; if ( altAST.getChildCount()==1 || (altAST.getChildCount() == 2 && altAST.getChild(0).getType() == ANTLRParser.ELEMENT_OPTIONS) ) { Tree e = altAST.getChild(altAST.getChildCount() - 1); if ( e.getType()==ANTLRParser.WILDCARD ) { return true; } } } return false; }
Example #26
Source File: LL1AltBlock.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public LL1AltBlock(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlockForAlt> alts) { super(factory, blkAST, alts); this.decision = ((DecisionState)blkAST.atnState).decision; /** Lookahead for each alt 1..n */ IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision); altLook = getAltLookaheadAsStringLists(altLookSets); IntervalSet expecting = IntervalSet.or(altLookSets); // combine alt sets this.error = getThrowNoViableAlt(factory, blkAST, expecting); }
Example #27
Source File: ParserATNFactory.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** * From {@code (A)?} build either: * * <pre> * o--A->o * | ^ * o---->| * </pre> * * or, if {@code A} is a block, just add an empty alt to the end of the * block */ @Override public Handle optional(GrammarAST optAST, Handle blk) { BlockStartState blkStart = (BlockStartState)blk.left; ATNState blkEnd = blk.right; preventEpsilonOptionalBlocks.add(new Triple<Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd)); boolean greedy = ((QuantifierAST)optAST).isGreedy(); blkStart.nonGreedy = !greedy; epsilon(blkStart, blk.right, !greedy); optAST.atnState = blk.left; return blk; }
Example #28
Source File: Tool.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** 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; }
Example #29
Source File: BasicSemanticChecks.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override protected void enterTerminal(GrammarAST tree) { String text = tree.getText(); if (text.equals("''")) { g.tool.errMgr.grammarError(ErrorType.EMPTY_STRINGS_NOT_ALLOWED, g.fileName, tree.token); } }
Example #30
Source File: BasicSemanticChecks.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override protected void enterChannelsSpec(GrammarAST tree) { if (g.isParser()) { g.tool.errMgr.grammarError(ErrorType.CHANNELS_BLOCK_IN_PARSER_GRAMMAR, g.fileName, tree.token); } else if (g.isCombined()) { g.tool.errMgr.grammarError(ErrorType.CHANNELS_BLOCK_IN_COMBINED_GRAMMAR, g.fileName, tree.token); } }