root.fileName = root.getGrammarName(); String grammarName = root.getChild(0).getText(); for (String grammarName : sortedGrammarNames) { for (GrammarRootAST root : roots) { if (root.getGrammarName().equals(grammarName)) { LOGGER.debug("add to ast buffer {}", grammarName); ast.put(grammarName, root);
public void process() { GrammarRootAST root = g.ast; if ( root==null ) return; tool.log("grammar", "before: "+root.toStringTree()); integrateImportedGrammars(g); reduceBlocksToSets(root); expandParameterizedLoops(root); tool.log("grammar", "after: "+root.toStringTree()); }
@Override public GrammarRootAST dupNode() { return new GrammarRootAST(this); } }
GrammarAST[] elements = combinedAST.getChildren().toArray(new GrammarAST[0]); String lexerName = combinedAST.getChild(0).getText()+"Lexer"; GrammarRootAST lexerAST = new GrammarRootAST(new CommonToken(ANTLRParser.GRAMMAR, "LEXER_GRAMMAR"), combinedGrammar.ast.tokenStream); lexerAST.grammarType = ANTLRParser.LEXER; lexerAST.token.setInputStream(combinedAST.token.getInputStream()); lexerAST.addChild((GrammarAST)adaptor.create(ANTLRParser.ID, lexerName)); (GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.OPTIONS); 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) { lexerAST.setOption(optionName, (GrammarAST)optionTree.getChild(1)); for (GrammarAST e : elements) { if ( e.getType()==ANTLRParser.AT ) { lexerAST.addChild((Tree)adaptor.dupTree(e)); if ( e.getChild(0).getText().equals("lexer") ) { actionsWeMoved.add(e); combinedAST.deleteChild( r ); (GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.RULES); if ( combinedRulesRoot==null ) return lexerAST;
GrammarAST imp_tokensRoot = (GrammarAST)imp.ast.getFirstChildWithType(ANTLRParser.TOKENS_SPEC); if ( imp_tokensRoot!=null ) { rootGrammar.tool.log("grammar", "imported tokens: "+imp_tokensRoot.getChildren()); List<GrammarAST> imp_actionRoots = imp.ast.getAllChildrenWithType(ANTLRParser.AT); if ( actionRoots!=null ) all_actionRoots.addAll(actionRoots); all_actionRoots.addAll(imp_actionRoots); List<GrammarAST> rules = imp.ast.getNodesWithType(ANTLRParser.RULE); if ( rules!=null ) { for (GrammarAST r : rules) { GrammarAST optionsRoot = (GrammarAST)imp.ast.getFirstChildWithType(ANTLRParser.OPTIONS); if ( optionsRoot!=null ) { for (Map.Entry<String, GrammarAST> option : imp.ast.getOptions().entrySet()) { String importOption = imp.ast.getOptionString(option.getKey()); if (importOption == null) { continue; String rootOption = rootGrammar.ast.getOptionString(option.getKey()); if (!importOption.equals(rootOption)) { hasNewOption = true; rootGrammar.tool.log("grammar", "Grammar: "+rootGrammar.ast.toStringTree());
new ArrayList<Pair<GrammarAST,GrammarAST>>(); List<GrammarAST> ruleNodes = ast.getNodesWithType(ANTLRParser.RULE); if ( ruleNodes==null || ruleNodes.isEmpty() ) return null;
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));
/** 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; }
public String getGrammarName() { Tree t = getChild(0); if ( t!=null ) return t.getText(); return null; }
/** * create code generation pipeline from grammar ast * * @param ast grammar ast * @return string code generation pipeline */ public StringCodeGenPipeline createPipeline(GrammarRootAST ast) { if (pip.containsKey(ast.getGrammarName())) pip.get(ast.getGrammarName()); LOGGER.debug("create grammar {}", ast.getGrammarName()); final Grammar g = createGrammar(ast); g.fileName = g.name; g.loadImportedGrammars(); StringCodeGenPipeline spip = new StringCodeGenPipeline(g); LOGGER.debug("put grammar {}", g.name); pip.put(g.name, spip); return spip; }
return; for (GrammarAST importDecl : grammar.getAllChildrenWithType(ANTLRParser.IMPORT)) { for (Tree id: importDecl.getAllChildrenWithType(ANTLRParser.ID)) { for (GrammarAST options : grammar.getAllChildrenWithType(ANTLRParser.OPTIONS)) { for (int i = 0, count = options.getChildCount(); i < count; i++) { Tree option = options.getChild(i);
GrammarAST[] elements = combinedAST.getChildren().toArray(new GrammarAST[0]); String lexerName = combinedAST.getChild(0).getText()+"Lexer"; GrammarRootAST lexerAST = new GrammarRootAST(new CommonToken(ANTLRParser.GRAMMAR, "LEXER_GRAMMAR"), combinedGrammar.ast.tokenStream); lexerAST.grammarType = ANTLRParser.LEXER; lexerAST.token.setInputStream(combinedAST.token.getInputStream()); lexerAST.addChild((GrammarAST)adaptor.create(ANTLRParser.ID, lexerName)); (GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.OPTIONS); 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) { lexerAST.setOption(optionName, (GrammarAST)optionTree.getChild(1)); for (GrammarAST e : elements) { if ( e.getType()==ANTLRParser.AT ) { lexerAST.addChild((Tree)adaptor.dupTree(e)); if ( e.getChild(0).getText().equals("lexer") ) { actionsWeMoved.add(e); combinedAST.deleteChild( r ); (GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.RULES); if ( combinedRulesRoot==null ) return lexerAST;
GrammarAST imp_tokensRoot = (GrammarAST)imp.ast.getFirstChildWithType(ANTLRParser.TOKENS_SPEC); if ( imp_tokensRoot!=null ) { rootGrammar.tool.log("grammar", "imported tokens: "+imp_tokensRoot.getChildren()); List<GrammarAST> imp_actionRoots = imp.ast.getAllChildrenWithType(ANTLRParser.AT); if ( actionRoots!=null ) all_actionRoots.addAll(actionRoots); all_actionRoots.addAll(imp_actionRoots); List<GrammarAST> rules = imp.ast.getNodesWithType(ANTLRParser.RULE); if ( rules!=null ) { for (GrammarAST r : rules) { GrammarAST optionsRoot = (GrammarAST)imp.ast.getFirstChildWithType(ANTLRParser.OPTIONS); if ( optionsRoot!=null ) { for (Map.Entry<String, GrammarAST> option : imp.ast.getOptions().entrySet()) { String importOption = imp.ast.getOptionString(option.getKey()); if (importOption == null) { continue; String rootOption = rootGrammar.ast.getOptionString(option.getKey()); if (!importOption.equals(rootOption)) { hasNewOption = true; rootGrammar.tool.log("grammar", "Grammar: "+rootGrammar.ast.toStringTree());
new ArrayList<Pair<GrammarAST,GrammarAST>>(); List<GrammarAST> ruleNodes = ast.getNodesWithType(ANTLRParser.RULE); if ( ruleNodes==null || ruleNodes.isEmpty() ) return null;
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));
/** 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; }
public String getGrammarName() { Tree t = getChild(0); if ( t!=null ) return t.getText(); return null; }
@Override public Grammar createGrammar(GrammarRootAST ast) { final Grammar g; LOGGER.debug("ast " + ast.getGrammarName()); if (ast.grammarType == ANTLRParser.LEXER) { g = new InmemantlrLexerGrammar(this, ast); } else { g = new InmemantlrGrammar(this, ast); } // ensure each node has pointer to surrounding grammar GrammarTransformPipeline.setGrammarPtr(g, ast); return g; }
GrammarAST[] elements = combinedAST.getChildren().toArray(new GrammarAST[0]); String lexerName = combinedAST.getChild(0).getText()+"Lexer"; GrammarRootAST lexerAST = new GrammarRootAST(new CommonToken(ANTLRParser.GRAMMAR, "LEXER_GRAMMAR"), combinedGrammar.ast.tokenStream); lexerAST.grammarType = ANTLRParser.LEXER; lexerAST.token.setInputStream(combinedAST.token.getInputStream()); lexerAST.addChild((GrammarAST)adaptor.create(ANTLRParser.ID, lexerName)); (GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.OPTIONS); 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) { lexerAST.setOption(optionName, (GrammarAST)optionTree.getChild(1)); for (GrammarAST e : elements) { if ( e.getType()==ANTLRParser.AT ) { lexerAST.addChild((Tree)adaptor.dupTree(e)); if ( e.getChild(0).getText().equals("lexer") ) { actionsWeMoved.add(e); combinedAST.deleteChild( r ); (GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.RULES); if ( combinedRulesRoot==null ) return lexerAST;
GrammarAST imp_channelRoot = (GrammarAST)imp.ast.getFirstChildWithType(ANTLRParser.CHANNELS); if ( imp_channelRoot != null) { rootGrammar.tool.log("grammar", "imported channels: "+imp_channelRoot.getChildren()); GrammarAST imp_tokensRoot = (GrammarAST)imp.ast.getFirstChildWithType(ANTLRParser.TOKENS_SPEC); if ( imp_tokensRoot!=null ) { rootGrammar.tool.log("grammar", "imported tokens: "+imp_tokensRoot.getChildren()); List<GrammarAST> imp_actionRoots = imp.ast.getAllChildrenWithType(ANTLRParser.AT); if ( actionRoots!=null ) all_actionRoots.addAll(actionRoots); all_actionRoots.addAll(imp_actionRoots); List<GrammarAST> modes = imp.ast.getNodesWithType(ANTLRParser.MODE); if (modes != null) { for (GrammarAST m : modes) { rootGrammar.ast.addChild(destinationAST); rootModeNames.add(name); rootModes.add(destinationAST); List<GrammarAST> rules = imp.ast.getNodesWithType(ANTLRParser.RULE); if ( rules!=null ) { for (GrammarAST r : rules) { GrammarAST optionsRoot = (GrammarAST)imp.ast.getFirstChildWithType(ANTLRParser.OPTIONS); if ( optionsRoot!=null ) { for (Map.Entry<String, GrammarAST> option : imp.ast.getOptions().entrySet()) { String importOption = imp.ast.getOptionString(option.getKey());