added stub for ID*[','] if we want that syntax

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9455]
This commit is contained in:
parrt 2011-11-25 12:13:29 -08:00
parent 3b543098aa
commit 06b27b8244
1 changed files with 37 additions and 10 deletions

View File

@ -51,28 +51,55 @@ public class GrammarTransformPipeline {
}
public void process() {
GrammarRootAST ast = g.ast;
if ( ast==null ) return;
tool.log("grammar", "before: "+ast.toStringTree());
GrammarRootAST root = g.ast;
if ( root==null ) return;
tool.log("grammar", "before: "+root.toStringTree());
integrateImportedGrammars(g);
if ( ast.grammarType==ANTLRParser.PARSER || ast.grammarType==ANTLRParser.COMBINED ) {
translateLeftRecursiveRules(ast);
if ( root.grammarType==ANTLRParser.PARSER || root.grammarType==ANTLRParser.COMBINED ) {
translateLeftRecursiveRules(root);
}
reduceBlocksToSets(ast);
reduceBlocksToSets(root);
expandParameterizedLoops(root);
tool.log("grammar", "after: "+ast.toStringTree());
tool.log("grammar", "after: "+root.toStringTree());
}
public void reduceBlocksToSets(GrammarRootAST ast) {
public void reduceBlocksToSets(GrammarRootAST root) {
org.antlr.runtime.tree.CommonTreeNodeStream nodes =
new org.antlr.runtime.tree.CommonTreeNodeStream(ast);
new org.antlr.runtime.tree.CommonTreeNodeStream(root);
GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
BlockSetTransformer transformer = new BlockSetTransformer(nodes, g);
transformer.setTreeAdaptor(adaptor);
transformer.downup(ast);
transformer.downup(root);
}
/** Find and replace
* ID*[','] with ID (',' ID)*
* ID+[','] with ID (',' ID)+
* (x {action} y)+[','] with x {action} y (',' x {action} y)+
*
* Parameter must be a token.
* todo: do we want?
*/
public void expandParameterizedLoops(GrammarRootAST root) {
TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor());
v.visit(root, new TreeVisitorAction() {
public Object pre(Object t) {
if ( ((GrammarAST)t).getType() == 3 ) {
return expandParameterizedLoop((GrammarAST)t);
}
return t;
}
public Object post(Object t) { return t; }
});
}
public GrammarAST expandParameterizedLoop(GrammarAST t) {
// todo: update grammar, alter AST
return t;
}
public void translateLeftRecursiveRules(GrammarRootAST ast) {
String language = Grammar.getLanguageOption(ast);
for (GrammarAST r : ast.getNodesWithType(ANTLRParser.RULE)) {