fixed wildcard; ast ops all work now per tests :)

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8866]
This commit is contained in:
parrt 2011-07-16 12:24:43 -08:00
parent 8de1408365
commit 9fb93fd090
11 changed files with 35 additions and 33 deletions

View File

@ -79,7 +79,7 @@ public abstract class BlankOutputModelFactory implements OutputModelFactory {
public List<SrcOp> rootRule(List<SrcOp> ops) { return ops; }
public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST) { return null; }
public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST, GrammarAST astOp) { return null; }
// AST REWRITES

View File

@ -98,6 +98,10 @@ public class CodeGeneratorExtension {
public List<SrcOp> leafSet(List<SrcOp> ops) { return ops; }
public List<SrcOp> rootWildcard(List<SrcOp> ops) { return ops; }
public List<SrcOp> leafWildcard(List<SrcOp> ops) { return ops; }
// BLOCKS
public Choice getChoiceBlock(Choice c) { return c; }

View File

@ -255,21 +255,17 @@ public class OutputModelController {
return ops;
}
public List<SrcOp> rootToken(List<SrcOp> ops) {
ops = delegate.rootToken(ops);
for (CodeGeneratorExtension ext : extensions) ops = ext.rootToken(ops);
return ops;
}
public List<SrcOp> rootRule(List<SrcOp> ops) {
ops = delegate.rootRule(ops);
for (CodeGeneratorExtension ext : extensions) ops = ext.rootRule(ops);
return ops;
}
public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST) {
List<SrcOp> ops = delegate.wildcard(ast, labelAST);
for (CodeGeneratorExtension ext : extensions) ops = ext.wildcard(ops);
public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST, GrammarAST astOp) {
List<SrcOp> ops = delegate.wildcard(ast, labelAST, astOp);
for (CodeGeneratorExtension ext : extensions) {
ops = ext.set(ops);
if ( astOp!=null && astOp.getType()==ANTLRParser.ROOT ) {
ops = ext.rootWildcard(ops);
}
else if ( astOp==null ) {
ops = ext.leafWildcard(ops);
}
}
return ops;
}

View File

@ -82,7 +82,7 @@ public interface OutputModelFactory {
List<SrcOp> rootRule(List<SrcOp> ops);
List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST);
List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST, GrammarAST astOp);
Choice getChoiceBlock(BlockAST blkAST, List<CodeBlockForAlt> alts, GrammarAST label);

View File

@ -208,6 +208,12 @@ public class ParserASTExtension extends CodeGeneratorExtension {
}
}
@Override
public List<SrcOp> rootWildcard(List<SrcOp> ops) { return rootToken(ops); }
@Override
public List<SrcOp> leafWildcard(List<SrcOp> ops) { return leafToken(ops); }
public void trackExplicitLabel(List<SrcOp> ops, Decl label, SrcOp opWithLabel) {
CodeBlock blk = factory.getCurrentOuterMostAlternativeBlock();
// declare _track_label

View File

@ -126,7 +126,7 @@ public class ParserFactory extends DefaultOutputModelFactory {
}
@Override
public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST) {
public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST, GrammarAST astOp) {
Wildcard wild = new Wildcard(this, ast);
// TODO: dup with tokenRef
if ( labelAST!=null ) {
@ -139,6 +139,7 @@ public class ParserFactory extends DefaultOutputModelFactory {
getCurrentRuleFunction().addContextDecl(l);
}
}
if ( controller.needsImplicitLabel(ast, wild) ) defineImplicitLabel(ast, wild);
AddToLabelList listLabelOp = getListLabelIfPresent(wild, labelAST);
return list(wild, listLabelOp);
}
@ -369,7 +370,7 @@ public class ParserFactory extends DefaultOutputModelFactory {
public void defineImplicitLabel(GrammarAST ast, LabeledOp op) {
Decl d;
Rule r = g.getRule(ast.getText());
if ( ast.getType()==ANTLRParser.SET ) {
if ( ast.getType()==ANTLRParser.SET || ast.getType()==ANTLRParser.WILDCARD ) {
String implLabel =
gen.target.getImplicitSetLabel(String.valueOf(ast.token.getTokenIndex()));
d = new TokenDecl(this, implLabel);

View File

@ -159,8 +159,8 @@ atom[GrammarAST label, GrammarAST astOp, boolean invert] returns [List<SrcOp> om
| range[label] {$omos = $range.omos;}
| ^(DOT ID terminal[$label, null])
| ^(DOT ID ruleref[$label, null])
| ^(WILDCARD .) {$omos = controller.wildcard($WILDCARD, $label);}
| WILDCARD {$omos = controller.wildcard($WILDCARD, $label);}
| ^(WILDCARD .) {$omos = controller.wildcard($WILDCARD, $label, $astOp);}
| WILDCARD {$omos = controller.wildcard($WILDCARD, $label, $astOp);}
| terminal[label, $astOp] {$omos = $terminal.omos;}
| ruleref[label, $astOp] {$omos = $ruleref.omos;}
| blockSet[$label, $astOp, invert] {$omos = $blockSet.omos;}

View File

@ -29,19 +29,11 @@
package org.antlr.v4.codegen;
import org.antlr.v4.codegen.model.*;
import org.antlr.v4.codegen.model.decl.Decl;
import org.antlr.v4.codegen.model.MatchToken;
import org.antlr.v4.tool.GrammarAST;
import java.util.*;
public class Wildcard extends RuleElement implements LabeledOp {
public List<Decl> labels = new ArrayList<Decl>();
public class Wildcard extends MatchToken {
public Wildcard(OutputModelFactory factory, GrammarAST ast) {
super(factory, ast);
}
public List<Decl> getLabels() { return labels; }
}

View File

@ -708,7 +708,9 @@ atom: // Qualified reference delegate.rule. This must be
// Because the terminal rule is allowed to be the node
// specification for the start of a tree rule, we must
// later check that wildcard was not used for that.
DOT elementOptions? -> ^(WILDCARD<TerminalAST>[$DOT] elementOptions?)
DOT elementOptions? (astop=ROOT|astop=BANG)?
-> {astop!=null}? ^($astop ^(WILDCARD<TerminalAST>[$DOT] elementOptions?))
-> ^(WILDCARD<TerminalAST>[$DOT] elementOptions?)
;
catch [RecognitionException re] { throw re; } // pass upwards to element

View File

@ -50,6 +50,7 @@ public class GrammarAST extends CommonTree {
token.setInputStream(t.getInputStream());
token.setLine(t.getLine());
token.setCharPositionInLine(t.getCharPositionInLine());
token.setTokenIndex(t.getTokenIndex());
}
public GrammarAST(int type, Token t, String text) {
this(new CommonToken(type, text));

View File

@ -182,7 +182,7 @@ public class TestASTOps extends BaseTest {
String grammar =
"grammar T;\n" +
"options {output=AST;}\n" +
"a : v='void' x=.^ ';' ;\n" +
"a : v='void' x+=.^ ';' ;\n" +
"ID : 'a'..'z'+ ;\n" +
"INT : '0'..'9'+;\n" +
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";