forked from jasder/antlr
Fixes #554
This commit is contained in:
parent
4aec5aeac2
commit
813fefdbb7
|
@ -320,12 +320,12 @@ case <index>: return <actions.(index)>;}; separator="\n">
|
|||
}
|
||||
>>
|
||||
|
||||
RuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= <<
|
||||
RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= <<
|
||||
|
||||
<ruleCtx>
|
||||
<altLabelCtxs:{l | <altLabelCtxs.(l)>}; separator="\n">
|
||||
|
||||
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else>public final <endif><currentRule.ctxType> <currentRule.name>(<currentRule.args; separator=",">) throws RecognitionException {
|
||||
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else>public final <endif><currentRule.ctxType> <currentRule.name>(<args; separator=",">) throws RecognitionException {
|
||||
<currentRule.ctxType> _localctx = new <currentRule.ctxType>(_ctx, getState()<currentRule.args:{a | , <a.name>}>);
|
||||
enterRule(_localctx, <currentRule.startState>, RULE_<currentRule.name>);
|
||||
<namedActions.init>
|
||||
|
@ -355,18 +355,18 @@ RuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAc
|
|||
}
|
||||
>>
|
||||
|
||||
LeftRecursiveRuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,
|
||||
LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,
|
||||
namedActions,finallyAction,postamble) ::=
|
||||
<<
|
||||
|
||||
<ruleCtx>
|
||||
<altLabelCtxs:{l | <altLabelCtxs.(l)>}; separator="\n">
|
||||
|
||||
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else>public final <endif><currentRule.ctxType> <currentRule.name>(<currentRule.args; separator=", ">) throws RecognitionException {
|
||||
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else>public final <endif><currentRule.ctxType> <currentRule.name>(<args; separator=", ">) throws RecognitionException {
|
||||
return <currentRule.name>(0<currentRule.args:{a | , <a.name>}>);
|
||||
}
|
||||
|
||||
private <currentRule.ctxType> <currentRule.name>(int _p<currentRule.args:{a | , <a>}>) throws RecognitionException {
|
||||
private <currentRule.ctxType> <currentRule.name>(int _p<args:{a | , <a>}>) throws RecognitionException {
|
||||
ParserRuleContext _parentctx = _ctx;
|
||||
int _parentState = getState();
|
||||
<currentRule.ctxType> _localctx = new <currentRule.ctxType>(_ctx, _parentState<currentRule.args:{a | , <a.name>}>);
|
||||
|
@ -697,11 +697,11 @@ SetNonLocalAttr(s, rhsChunks) ::=
|
|||
|
||||
AddToLabelList(a) ::= "<ctx(a.label)>.<a.listName>.add(<labelref(a.label)>);"
|
||||
|
||||
TokenDecl(t) ::= "public <TokenLabelType()> <t.name>;"
|
||||
TokenDecl(t) ::= "<TokenLabelType()> <t.name>"
|
||||
TokenTypeDecl(t) ::= "int <t.name>;"
|
||||
TokenListDecl(t) ::= "public List\<Token> <t.name> = new ArrayList\<Token>();"
|
||||
RuleContextDecl(r) ::= "public <r.ctxName> <r.name>;"
|
||||
RuleContextListDecl(rdecl) ::= "public List\<<rdecl.ctxName>> <rdecl.name> = new ArrayList\<<rdecl.ctxName>>();"
|
||||
TokenListDecl(t) ::= "List\<Token> <t.name> = new ArrayList\<Token>()"
|
||||
RuleContextDecl(r) ::= "<r.ctxName> <r.name>"
|
||||
RuleContextListDecl(rdecl) ::= "List\<<rdecl.ctxName>> <rdecl.name> = new ArrayList\<<rdecl.ctxName>>()"
|
||||
|
||||
ContextTokenGetterDecl(t) ::=
|
||||
"public TerminalNode <t.name>() { return getToken(<parser.name>.<t.name>, 0); }"
|
||||
|
@ -743,13 +743,13 @@ ListLabelName(label) ::= "<label>"
|
|||
CaptureNextToken(d) ::= "<d.varName> = _input.LT(1);"
|
||||
CaptureNextTokenType(d) ::= "<d.varName> = _input.LA(1);"
|
||||
|
||||
StructDecl(struct,attrs,getters,dispatchMethods,interfaces,extensionMembers,
|
||||
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,
|
||||
superClass={ParserRuleContext}) ::= <<
|
||||
public static class <struct.name> extends <superClass><if(interfaces)> implements <interfaces; separator=", "><endif> {
|
||||
<attrs:{a | <a>}; separator="\n">
|
||||
<attrs:{a | public <a>;}; separator="\n">
|
||||
<getters:{g | <g>}; separator="\n">
|
||||
<if(struct.ctorAttrs)>public <struct.name>(ParserRuleContext parent, int invokingState) { super(parent, invokingState); }<endif>
|
||||
public <struct.name>(ParserRuleContext parent, int invokingState<struct.ctorAttrs:{a | , <a>}>) {
|
||||
<if(ctorAttrs)>public <struct.name>(ParserRuleContext parent, int invokingState) { super(parent, invokingState); }<endif>
|
||||
public <struct.name>(ParserRuleContext parent, int invokingState<ctorAttrs:{a | , <a>}>) {
|
||||
super(parent, invokingState);
|
||||
<struct.ctorAttrs:{a | this.<a.name> = <a.name>;}; separator="\n">
|
||||
}
|
||||
|
@ -768,7 +768,7 @@ public static class <struct.name> extends <superClass><if(interfaces)> implement
|
|||
|
||||
AltLabelStructDecl(struct,attrs,getters,dispatchMethods) ::= <<
|
||||
public static class <struct.name> extends <currentRule.name; format="cap">Context {
|
||||
<attrs:{a | <a>}; separator="\n">
|
||||
<attrs:{a | public <a>;}; separator="\n">
|
||||
<getters:{g | <g>}; separator="\n">
|
||||
public <struct.name>(<currentRule.name; format="cap">Context ctx) { copyFrom(ctx); }
|
||||
<dispatchMethods; separator="\n">
|
||||
|
@ -790,7 +790,7 @@ public \<T> T accept(ParseTreeVisitor\<? extends T> visitor) {
|
|||
}
|
||||
>>
|
||||
|
||||
AttributeDecl(d) ::= "public <d.decl>;"
|
||||
AttributeDecl(d) ::= "<d.type> <d.name>"
|
||||
|
||||
/** If we don't know location of label def x, use this template */
|
||||
labelref(x) ::= "<if(!x.isLocal)>((<x.ctx.name>)_localctx).<endif><x.name>"
|
||||
|
|
|
@ -35,6 +35,7 @@ import org.antlr.runtime.tree.CommonTreeNodeStream;
|
|||
import org.antlr.runtime.tree.TreeNodeStream;
|
||||
import org.antlr.v4.codegen.OutputModelFactory;
|
||||
import org.antlr.v4.codegen.model.decl.AltLabelStructDecl;
|
||||
import org.antlr.v4.codegen.model.decl.AttributeDecl;
|
||||
import org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl;
|
||||
import org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl;
|
||||
import org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl;
|
||||
|
@ -82,13 +83,13 @@ public class RuleFunction extends OutputModelObject {
|
|||
public Collection<String> tokenLabels;
|
||||
public ATNState startState;
|
||||
public int index;
|
||||
public Collection<Attribute> args = null;
|
||||
public Rule rule;
|
||||
public AltLabelStructDecl[] altToContext;
|
||||
public boolean hasLookaheadBlock;
|
||||
|
||||
@ModelElement public List<SrcOp> code;
|
||||
@ModelElement public OrderedHashSet<Decl> locals; // TODO: move into ctx?
|
||||
@ModelElement public Collection<AttributeDecl> args = null;
|
||||
@ModelElement public StructDecl ruleCtx;
|
||||
@ModelElement public Map<String,AltLabelStructDecl> altLabelCtxs;
|
||||
@ModelElement public Map<String,Action> namedActions;
|
||||
|
@ -113,9 +114,15 @@ public class RuleFunction extends OutputModelObject {
|
|||
addContextGetters(factory, r);
|
||||
|
||||
if ( r.args!=null ) {
|
||||
ruleCtx.addDecls(r.args.attributes.values());
|
||||
args = r.args.attributes.values();
|
||||
ruleCtx.ctorAttrs = args;
|
||||
Collection<Attribute> decls = r.args.attributes.values();
|
||||
if ( decls.size()>0 ) {
|
||||
args = new ArrayList<AttributeDecl>();
|
||||
ruleCtx.addDecls(decls);
|
||||
for (Attribute a : decls) {
|
||||
args.add(new AttributeDecl(factory, a));
|
||||
}
|
||||
ruleCtx.ctorAttrs = args;
|
||||
}
|
||||
}
|
||||
if ( r.retvals!=null ) {
|
||||
ruleCtx.addDecls(r.retvals.attributes.values());
|
||||
|
|
|
@ -31,10 +31,13 @@
|
|||
package org.antlr.v4.codegen.model.decl;
|
||||
|
||||
import org.antlr.v4.codegen.OutputModelFactory;
|
||||
import org.antlr.v4.tool.Attribute;
|
||||
|
||||
/** */
|
||||
public class AttributeDecl extends Decl {
|
||||
public AttributeDecl(OutputModelFactory factory, String name, String decl) {
|
||||
super(factory, name, decl);
|
||||
public String type;
|
||||
public AttributeDecl(OutputModelFactory factory, Attribute a) {
|
||||
super(factory, a.name, a.decl);
|
||||
this.type = a.type;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ public class StructDecl extends Decl {
|
|||
public boolean provideCopyFrom;
|
||||
@ModelElement public OrderedHashSet<Decl> attrs = new OrderedHashSet<Decl>();
|
||||
@ModelElement public OrderedHashSet<Decl> getters = new OrderedHashSet<Decl>();
|
||||
@ModelElement public Collection<Attribute> ctorAttrs;
|
||||
@ModelElement public Collection<AttributeDecl> ctorAttrs;
|
||||
@ModelElement public List<? super DispatchMethod> dispatchMethods;
|
||||
@ModelElement public List<OutputModelObject> interfaces;
|
||||
@ModelElement public List<OutputModelObject> extensionMembers;
|
||||
|
@ -85,7 +85,7 @@ public class StructDecl extends Decl {
|
|||
}
|
||||
|
||||
public void addDecl(Attribute a) {
|
||||
addDecl(new AttributeDecl(factory, a.name, a.decl));
|
||||
addDecl(new AttributeDecl(factory, a));
|
||||
}
|
||||
|
||||
public void addDecls(Collection<Attribute> attrList) {
|
||||
|
|
|
@ -130,6 +130,14 @@ public class TestCodeGeneration extends BaseTest {
|
|||
CodeGenerator gen = new CodeGenerator(g);
|
||||
ST outputFileST = gen.generateParser();
|
||||
|
||||
// STViz viz = outputFileST.inspect();
|
||||
// try {
|
||||
// viz.waitForClose();
|
||||
// }
|
||||
// catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
|
||||
boolean debug = false;
|
||||
DebugInterpreter interp =
|
||||
new DebugInterpreter(outputFileST.groupThatCreatedThisInstance,
|
||||
|
|
Loading…
Reference in New Issue