Merge pull request #566 from parrt/args-as-templates

Args as templates
This commit is contained in:
Sam Harwell 2014-05-24 12:36:30 -05:00
commit 4ae0161f1a
5 changed files with 195 additions and 23 deletions

View File

@ -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>"

View File

@ -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());

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -0,0 +1,162 @@
/*
* [The "BSD license"]
* Copyright (c) 2014 Terence Parr
* Copyright (c) 2014 Sam Harwell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.test;
import org.antlr.runtime.RecognitionException;
import org.antlr.v4.automata.ATNFactory;
import org.antlr.v4.automata.LexerATNFactory;
import org.antlr.v4.automata.ParserATNFactory;
import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.semantics.SemanticPipeline;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.LexerGrammar;
import org.junit.Test;
import org.stringtemplate.v4.AutoIndentWriter;
import org.stringtemplate.v4.InstanceScope;
import org.stringtemplate.v4.Interpreter;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STWriter;
import org.stringtemplate.v4.misc.ErrorManager;
import org.stringtemplate.v4.misc.ErrorType;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertFalse;
public class TestCodeGeneration extends BaseTest {
@Test public void testArgDecl() throws Exception { // should use template not string
ErrorQueue equeue = new ErrorQueue();
String g =
"grammar T;\n" +
"a[int xyz] : 'a' ;\n";
List<String> evals = getEvalInfoForString(g, "int xyz");
System.out.println(evals);
for (int i = 0; i < evals.size(); i++) {
String eval = evals.get(i);
assertFalse("eval should not be POJO: "+eval, eval.startsWith("<pojo:"));
}
}
/** Add tags around each attribute/template/value write */
public static class DebugInterpreter extends Interpreter {
List<String> evals = new ArrayList<String>();
ErrorManager myErrMgrCopy;
int tab = 0;
public DebugInterpreter(STGroup group, ErrorManager errMgr, boolean debug) {
super(group, errMgr, debug);
myErrMgrCopy = errMgr;
}
@Override
protected int writeObject(STWriter out, InstanceScope scope, Object o, String[] options) {
if ( o instanceof ST ) {
String name = ((ST)o).getName();
name = name.substring(1);
if ( !name.startsWith("_sub") ) {
try {
out.write("<ST:" + name + ">");
evals.add("<ST:" + name + ">");
int r = super.writeObject(out, scope, o, options);
out.write("</ST:" + name + ">");
evals.add("</ST:" + name + ">");
return r;
} catch (IOException ioe) {
myErrMgrCopy.IOError(scope.st, ErrorType.WRITE_IO_ERROR, ioe);
}
}
}
return super.writeObject(out, scope, o, options);
}
@Override
protected int writePOJO(STWriter out, InstanceScope scope, Object o, String[] options) throws IOException {
Class<?> type = o.getClass();
String name = type.getSimpleName();
out.write("<pojo:"+name+">"+o.toString()+"</pojo:"+name+">");
evals.add("<pojo:" + name + ">" + o.toString() + "</pojo:" + name + ">");
return super.writePOJO(out, scope, o, options);
}
public void indent(STWriter out) throws IOException {
for (int i=1; i<=tab; i++) {
out.write("\t");
}
}
}
public List<String> getEvalInfoForString(String grammarString, String pattern) throws RecognitionException {
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(grammarString);
List<String> evals = new ArrayList<String>();
if ( g.ast!=null && !g.ast.hasErrors ) {
SemanticPipeline sem = new SemanticPipeline(g);
sem.process();
ATNFactory factory = new ParserATNFactory(g);
if (g.isLexer()) factory = new LexerATNFactory((LexerGrammar) g);
g.atn = factory.createATN();
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,
outputFileST.impl.nativeGroup.errMgr,
debug);
InstanceScope scope = new InstanceScope(null, outputFileST);
StringWriter sw = new StringWriter();
AutoIndentWriter out = new AutoIndentWriter(sw);
interp.exec(out, scope);
for (String e : interp.evals) {
if (e.contains(pattern)) {
evals.add(e);
}
}
}
if ( equeue.size()>0 ) {
System.err.println(equeue.toString());
}
return evals;
}
}