Java Code Examples for org.antlr.v4.tool.ast.GrammarAST#getText()
The following examples show how to use
org.antlr.v4.tool.ast.GrammarAST#getText() .
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: LeftRecursiveRuleAnalyzer.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@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); }
Example 2
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 3
Source File: ParserFactory.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@Override public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST) { Wildcard wild = new Wildcard(this, ast); // TODO: dup with tokenRef if ( labelAST!=null ) { String label = labelAST.getText(); Decl d = getTokenLabelDecl(label); wild.labels.add(d); getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), d); if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) { TokenListDecl l = getTokenListLabelDecl(label); getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), l); } } if ( controller.needsImplicitLabel(ast, wild) ) defineImplicitLabel(ast, wild); AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(wild, labelAST); return list(wild, listLabelOp); }
Example 4
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 5
Source File: SymbolChecks.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
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); } else if ( arg==null && (r!=null&&r.args!=null) ) { errMgr.grammarError(ErrorType.MISSING_RULE_ARGS, g.fileName, ref.token, ruleName); } } }
Example 6
Source File: Tool.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** * Try current dir then dir of g then lib dir * @param g * @param nameNode The node associated with the imported grammar name. */ public Grammar loadImportedGrammar(Grammar g, GrammarAST nameNode) throws IOException { String name = nameNode.getText(); Grammar imported = importedGrammars.get(name); if (imported == null) { g.tool.log("grammar", "load " + name + " from " + g.fileName); File importedFile = null; for (String extension : ALL_GRAMMAR_EXTENSIONS) { importedFile = getImportedGrammarFile(g, name + extension); if (importedFile != null) { break; } } if ( importedFile==null ) { errMgr.grammarError(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, g.fileName, nameNode.getToken(), name); return null; } String absolutePath = importedFile.getAbsolutePath(); ANTLRFileStream in = new ANTLRFileStream(absolutePath, grammarEncoding); GrammarRootAST root = parse(g.fileName, in); if (root == null) { return null; } imported = createGrammar(root); imported.fileName = absolutePath; importedGrammars.put(root.getGrammarName(), imported); } return imported; }
Example 7
Source File: LexerATNFactory.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public IntervalSet getSetFromCharSetLiteral(GrammarAST charSetAST) { String chars = charSetAST.getText(); chars = chars.substring(1, chars.length()-1); String cset = '"'+ chars +'"'; IntervalSet set = new IntervalSet(); // unescape all valid escape char like \n, leaving escaped dashes as '\-' // so we can avoid seeing them as '-' range ops. chars = CharSupport.getStringFromGrammarStringLiteral(cset); // now make x-y become set of char int n = chars.length(); for (int i=0; i< n; i++) { int c = chars.charAt(i); if ( c=='\\' && (i+1)<n && chars.charAt(i+1)=='-' ) { // \- set.add('-'); i++; } else if ( (i+2)<n && chars.charAt(i+1)=='-' ) { // range x-y int x = c; int y = chars.charAt(i+2); if ( x<=y ) set.add(x,y); i+=2; } else { set.add(c); } } return set; }
Example 8
Source File: LeftRecursiveRuleAnalyzer.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void binaryAlt(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); // rewrite e to be e_[rec_arg] int nextPrec = nextPrecedence(alt); altTree = addPrecedenceArgToRules(altTree, nextPrec); stripAltLabel(altTree); String altText = text(altTree); altText = altText.trim(); LeftRecursiveRuleAltInfo a = new LeftRecursiveRuleAltInfo(alt, altText, label, altLabel, isListLabel, originalAltTree); a.nextPrec = nextPrec; binaryAlts.put(alt, a); //System.out.println("binaryAlt " + alt + ": " + altText + ", rewrite=" + rewriteText); }
Example 9
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 10
Source File: ParserFactory.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override public List<SrcOp> tokenRef(GrammarAST ID, GrammarAST labelAST, GrammarAST args) { MatchToken matchOp = new MatchToken(this, (TerminalAST) ID); if ( labelAST!=null ) { String label = labelAST.getText(); RuleFunction rf = getCurrentRuleFunction(); if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) { // add Token _X and List<Token> X decls defineImplicitLabel(ID, matchOp); // adds _X TokenListDecl l = getTokenListLabelDecl(label); rf.addContextDecl(ID.getAltLabel(), l); } else { Decl d = getTokenLabelDecl(label); matchOp.labels.add(d); rf.addContextDecl(ID.getAltLabel(), d); } // Decl d = getTokenLabelDecl(label); // ((MatchToken)matchOp).labels.add(d); // getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), d); // if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) { // TokenListDecl l = getTokenListLabelDecl(label); // getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), l); // } } if ( controller.needsImplicitLabel(ID, matchOp) ) defineImplicitLabel(ID, matchOp); AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST); return list(matchOp, listLabelOp); }
Example 11
Source File: RuleCollector.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@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); }
Example 12
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 13
Source File: SemanticPipeline.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
void identifyStartRules(SymbolCollector collector) { for (GrammarAST ref : collector.rulerefs) { String ruleName = ref.getText(); Rule r = g.getRule(ruleName); if ( r!=null ) r.isStartRule = false; } }
Example 14
Source File: SemanticPipeline.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** * Assign constant values to custom channels defined in a grammar. * * @param g The grammar. * @param channelDefs A collection of AST nodes defining individual channels * within a {@code channels{}} block in the grammar. */ void assignChannelTypes(Grammar g, List<GrammarAST> channelDefs) { Grammar outermost = g.getOutermostGrammar(); for (GrammarAST channel : channelDefs) { String channelName = channel.getText(); // Channel names can't alias tokens or modes, because constant // values are also assigned to them and the ->channel(NAME) lexer // command does not distinguish between the various ways a constant // can be declared. This method does not verify that channels do not // alias rules, because rule names are not associated with constant // values in ANTLR grammar semantics. if (g.getTokenType(channelName) != Token.INVALID_TYPE) { g.tool.errMgr.grammarError(ErrorType.CHANNEL_CONFLICTS_WITH_TOKEN, g.fileName, channel.token, channelName); } if (LexerATNFactory.COMMON_CONSTANTS.containsKey(channelName)) { g.tool.errMgr.grammarError(ErrorType.CHANNEL_CONFLICTS_WITH_COMMON_CONSTANTS, g.fileName, channel.token, channelName); } if (outermost instanceof LexerGrammar) { LexerGrammar lexerGrammar = (LexerGrammar)outermost; if (lexerGrammar.modes.containsKey(channelName)) { g.tool.errMgr.grammarError(ErrorType.CHANNEL_CONFLICTS_WITH_MODE, g.fileName, channel.token, channelName); } } outermost.defineChannelName(channel.getText()); } }
Example 15
Source File: RuleCollector.java From codebuff with BSD 2-Clause "Simplified" License | 4 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) { int numAlts = block.getChildCount(); Rule r; if ( LeftRecursiveRuleAnalyzer.hasImmediateRecursiveRuleRefs(rule, ID.getText()) ) { r = new LeftRecursiveRule(g, ID.getText(), rule); } else { r = new Rule(g, ID.getText(), rule, numAlts); } rules.put(r.name, r); 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[currentOuterAltNumber]; } if ( returns!=null ) { r.retvals = ScopeParser.parseTypedArgList(returns, returns.getText(), g); r.retvals.type = AttributeDict.DictType.RET; r.retvals.ast = returns; } if ( locals!=null ) { r.locals = ScopeParser.parseTypedArgList(locals, locals.getText(), g); r.locals.type = AttributeDict.DictType.LOCAL; r.locals.ast = locals; } for (GrammarAST a : actions) { // a = ^(AT ID ACTION) ActionAST action = (ActionAST) a.getChild(1); r.namedActions.put(a.getChild(0).getText(), action); action.resolver = r; } }