forked from jasder/antlr
got core ^ and ! ops in
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8791]
This commit is contained in:
parent
15fbaccb6d
commit
2d5c4fd686
|
@ -1,6 +1,6 @@
|
|||
parser grammar T;
|
||||
options {output=AST;}
|
||||
a : A^ b ;
|
||||
a : x+=A^ y+=b B! b!;
|
||||
|
||||
b : B ;
|
||||
/*
|
||||
|
|
|
@ -104,17 +104,17 @@ public QStack\<<currentRule.ctxType>\> <currentRule.name>_stk = new QStack\<<cur
|
|||
try {
|
||||
<code>
|
||||
<postamble; separator="\n">
|
||||
_localctx.stop = input.LT(-1);
|
||||
<namedActions.after>
|
||||
}
|
||||
catch (RecognitionException re) {
|
||||
reportError(re);
|
||||
recover();
|
||||
}
|
||||
finally {
|
||||
_localctx.stop = input.LT(-1);
|
||||
<namedActions.after>
|
||||
<currentRule.name>_stk.pop();
|
||||
<finallyAction>
|
||||
_ctx = (ParserRuleContext)_ctx.parent;
|
||||
<finallyAction>
|
||||
//System.out.println("exit "+ruleNames[<currentRule.index>]);
|
||||
}
|
||||
return _localctx;
|
||||
|
@ -266,12 +266,12 @@ cases(ttypes) ::= <<
|
|||
>>
|
||||
|
||||
InvokeRule(r) ::= <<
|
||||
_ctx.s = <r.stateNumber>;
|
||||
_localctx.s = <r.stateNumber>;
|
||||
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif><r.name>(<r.argExprs:{e| <e>}; separator=",">);
|
||||
>>
|
||||
|
||||
MatchToken(m) ::= <<
|
||||
_ctx.s = <m.stateNumber>;
|
||||
_localctx.s = <m.stateNumber>;
|
||||
<if(m.labels)><m.labels:{l | <labelref(l)> = }>(Token)<endif>match(<m.name>);
|
||||
>>
|
||||
|
||||
|
@ -323,7 +323,7 @@ SetDynScopeAttr_negIndex(s, indexChunks, rhsChunks) ::=
|
|||
SetDynScopeAttr_index(s, indexChunks, rhsChunks) ::=
|
||||
"<s.scope>.get(<indexChunks>).<s.attr> =<rhsChunks>;"
|
||||
|
||||
AddToLabelList(a) ::= "_localctx.<a.listName>.add(_localctx.<first(a.opWithResultToAdd.labels)>);"
|
||||
AddToLabelList(a) ::= "_localctx.<a.listName>.add(<labelref(first(a.opWithResultToAdd.labels))>);"
|
||||
|
||||
TokenDecl(t) ::= "Token <t.name>;"
|
||||
TokenTypeDecl(t) ::= "int <t.name>;"
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package org.antlr.v4.codegen;
|
||||
|
||||
import org.antlr.runtime.tree.Tree;
|
||||
import org.antlr.v4.codegen.model.*;
|
||||
import org.antlr.v4.codegen.model.ast.*;
|
||||
import org.antlr.v4.misc.Utils;
|
||||
import org.antlr.v4.parse.ANTLRParser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -18,15 +20,33 @@ public class ParserASTExtension extends CodeGeneratorExtension {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<SrcOp> ruleRef(List<SrcOp> ops) {
|
||||
public List<SrcOp> rootRule(List<SrcOp> ops) {
|
||||
InvokeRule invokeOp = (InvokeRule)Utils.find(ops, InvokeRule.class);
|
||||
SrcOp treeOp = new BecomeRoot(factory, invokeOp.ast, invokeOp);
|
||||
return DefaultOutputModelFactory.list(ops, treeOp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SrcOp> rootToken(List<SrcOp> ops) {
|
||||
MatchToken matchOp = (MatchToken)Utils.find(ops, MatchToken.class);
|
||||
SrcOp treeOp = new BecomeRoot(factory, matchOp.ast, matchOp);
|
||||
return DefaultOutputModelFactory.list(ops, treeOp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SrcOp> ruleRef(List<SrcOp> ops) {
|
||||
InvokeRule invokeOp = (InvokeRule)Utils.find(ops, InvokeRule.class);
|
||||
Tree parent = invokeOp.ast.getParent();
|
||||
if ( parent!=null && parent.getType()==ANTLRParser.BANG ) return ops;
|
||||
SrcOp treeOp = new AddLeaf(factory, invokeOp.ast, invokeOp);
|
||||
return DefaultOutputModelFactory.list(ops, treeOp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SrcOp> tokenRef(List<SrcOp> ops) {
|
||||
MatchToken matchOp = (MatchToken)Utils.find(ops, MatchToken.class);
|
||||
Tree parent = matchOp.ast.getParent();
|
||||
if ( parent!=null && parent.getType()==ANTLRParser.BANG ) return ops;
|
||||
SrcOp treeOp = new AddLeaf(factory, matchOp.ast, matchOp);
|
||||
return DefaultOutputModelFactory.list(ops, treeOp);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue