forked from jasder/antlr
fixed wildcard; ast ops all work now per tests :)
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8866]
This commit is contained in:
parent
8de1408365
commit
9fb93fd090
|
@ -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
|
||||
|
||||
|
|
|
@ -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; }
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;}
|
||||
|
|
|
@ -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; }
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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";
|
||||
|
|
Loading…
Reference in New Issue