got _root0 defined; each SrcOp knows its block now; setting current block

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8823]
This commit is contained in:
parrt 2011-07-03 11:23:02 -08:00
parent 411e1bb210
commit 3cfbe39b4f
19 changed files with 186 additions and 81 deletions

View File

@ -122,7 +122,9 @@ public QStack\<<currentRule.ctxType>\> <currentRule.name>_stk = new QStack\<<cur
}
>>
CodeBlockForAlt(c, ops) ::= <<
CodeBlockForAlt(c, locals, preamble, ops) ::= <<
<locals; separator="\n">
<preamble; separator="\n">
<ops; separator="\n">
>>
@ -379,16 +381,14 @@ TrackTokenElement(e) ::= "<e.name>.add(_adaptor.create(<labelref(e.label)>));"
TreeRewrite(tr, locals, preamble, ops) ::= <<
// rewrite: ...
<locals; separator="\n">
<preamble; separator="\n">
<ops; separator="\n">
<CodeBlockForAlt(tr, locals, preamble, ops)>
>>
RewriteIteratorDecl(d) ::= "Iterator <d.name>;"
RewriteIteratorInit(i) ::= "<i.decl.name> = <i.decl.listName>.iterator();"
RewriteIteratorName(elemName,level) ::= "it<level>_<elemName>"
RewriteTokenRef(t) ::= "_adaptor.addChild(<t.decl.name>.next());"
RewriteTokenRef(t) ::= "_adaptor.addChild(<t.enclosingBlock.rootDecl.name>, <t.decl.name>.next());"
RewriteRuleRef(r) ::= "/* ruleref */"
/*

View File

@ -51,7 +51,7 @@ public abstract class BlankOutputModelFactory implements OutputModelFactory {
// ALTERNATIVES / ELEMENTS
public CodeBlockForAlt alternative(List<SrcOp> elems) { return null; }
public CodeBlockForAlt alternative(Alternative alt) { return null; }
public CodeBlockForAlt epsilon() { return null; }
@ -77,7 +77,7 @@ public abstract class BlankOutputModelFactory implements OutputModelFactory {
// AST REWRITES
public TreeRewrite treeRewrite(GrammarAST ast) { return null; }
public TreeRewrite treeRewrite(GrammarAST ast, int rewriteLevel) { return null; }
public List<SrcOp> rewrite_ruleRef(GrammarAST ID) { return null; }

View File

@ -30,6 +30,7 @@
package org.antlr.v4.codegen;
import org.antlr.v4.codegen.model.*;
import org.antlr.v4.codegen.model.decl.CodeBlock;
import org.antlr.v4.tool.*;
import java.util.*;
@ -49,7 +50,7 @@ public abstract class DefaultOutputModelFactory extends BlankOutputModelFactory
public OutputModelObject root; // normally ParserFile, LexerFile, ...
public Stack<RuleFunction> currentRule = new Stack<RuleFunction>();
public Alternative currentAlt;
public Rewrite currentRewrite;
public CodeBlock currentBlock;
protected DefaultOutputModelFactory(CodeGenerator gen) {
this.gen = gen;
@ -84,12 +85,12 @@ public abstract class DefaultOutputModelFactory extends BlankOutputModelFactory
this.controller = controller;
}
public void setCurrentRewriteBlock(Rewrite rew) {
currentRewrite = rew;
public void setCurrentBlock(CodeBlock blk) {
currentBlock = blk;
}
public Rewrite getCurrentRewriteBlock() {
return currentRewrite;
public CodeBlock getCurrentBlock() {
return currentBlock;
}
// MISC

View File

@ -32,6 +32,7 @@ package org.antlr.v4.codegen;
import org.antlr.runtime.tree.*;
import org.antlr.v4.codegen.model.*;
import org.antlr.v4.codegen.model.ast.TreeRewrite;
import org.antlr.v4.codegen.model.decl.CodeBlock;
import org.antlr.v4.parse.*;
import org.antlr.v4.runtime.misc.IntervalSet;
import org.antlr.v4.tool.*;
@ -139,8 +140,8 @@ public class OutputModelController implements OutputModelFactory {
public CodeGenerator getGenerator() { return delegate.getGenerator(); }
public CodeBlockForAlt alternative(List<SrcOp> elems) {
CodeBlockForAlt code = delegate.alternative(elems);
public CodeBlockForAlt alternative(Alternative alt) {
CodeBlockForAlt code = delegate.alternative(alt);
for (CodeGeneratorExtension ext : extensions) code = ext.alternative(code);
return code;
}
@ -271,8 +272,8 @@ public class OutputModelController implements OutputModelFactory {
// REWRITES
public TreeRewrite treeRewrite(GrammarAST ast) {
TreeRewrite r = delegate.treeRewrite(ast);
public TreeRewrite treeRewrite(GrammarAST ast, int rewriteLevel) {
TreeRewrite r = delegate.treeRewrite(ast, rewriteLevel);
for (CodeGeneratorExtension ext : extensions) r = ext.treeRewrite(r);
return r;
}
@ -307,7 +308,7 @@ public class OutputModelController implements OutputModelFactory {
public void setController(OutputModelController controller) { } // nop; we are controller
public void setCurrentRewriteBlock(Rewrite rew) { delegate.setCurrentRewriteBlock(rew); }
public void setCurrentBlock(CodeBlock blk) { delegate.setCurrentBlock(blk); }
public Rewrite getCurrentRewriteBlock() { return delegate.getCurrentRewriteBlock(); }
public CodeBlock getCurrentBlock() { return delegate.getCurrentBlock(); }
}

View File

@ -31,6 +31,7 @@ package org.antlr.v4.codegen;
import org.antlr.v4.codegen.model.*;
import org.antlr.v4.codegen.model.ast.TreeRewrite;
import org.antlr.v4.codegen.model.decl.CodeBlock;
import org.antlr.v4.runtime.misc.IntervalSet;
import org.antlr.v4.tool.*;
@ -55,7 +56,7 @@ public interface OutputModelFactory {
// ELEMENT TRIGGERS
CodeBlockForAlt alternative(List<SrcOp> elems);
CodeBlockForAlt alternative(Alternative alt);
CodeBlockForAlt epsilon();
@ -95,7 +96,7 @@ public interface OutputModelFactory {
// Though dealing with ASTs, we must deal with here since these are
// triggered from elements in ANTLR's internal GrammarAST
TreeRewrite treeRewrite(GrammarAST ast);
TreeRewrite treeRewrite(GrammarAST ast, int rewriteLevel);
List<SrcOp> rewrite_ruleRef(GrammarAST ID);
@ -121,7 +122,7 @@ public interface OutputModelFactory {
void setController(OutputModelController controller);
void setCurrentRewriteBlock(Rewrite rew);
void setCurrentBlock(CodeBlock blk);
Rewrite getCurrentRewriteBlock();
CodeBlock getCurrentBlock();
}

View File

@ -43,9 +43,10 @@ public class ParserASTExtension extends CodeGeneratorExtension {
}
@Override
public RuleFunction rule(RuleFunction rf) {
rf.addLocalDecl(new RootDecl(factory, 0));
return rf;
public CodeBlockForAlt alternative(CodeBlockForAlt blk) {
Alternative alt = factory.getCurrentAlt();
if ( !alt.hasRewrite() ) blk.addLocalDecl( new RootDecl(factory, 0) );
return blk;
}
@Override

View File

@ -58,7 +58,7 @@ public class ParserFactory extends DefaultOutputModelFactory {
public CodeBlockForAlt epsilon() { return new CodeBlockForAlt(this); }
public CodeBlockForAlt alternative(List<SrcOp> elems) { return new CodeBlockForAlt(this, elems); }
public CodeBlockForAlt alternative(Alternative alt) { return new CodeBlockForAlt(this); }
public List<SrcOp> action(GrammarAST ast) { return list(new Action(this, ast)); }
@ -172,21 +172,23 @@ public class ParserFactory extends DefaultOutputModelFactory {
@Override
public TreeRewrite treeRewrite(GrammarAST ast) {
return new TreeRewrite(this);
public TreeRewrite treeRewrite(GrammarAST ast, int rewriteLevel) {
return new TreeRewrite(this, rewriteLevel);
}
public List<SrcOp> rewrite_ruleRef(GrammarAST ID) {
Decl d = new RewriteIteratorDecl(this, ID, 0);
getCurrentRewriteBlock().addLocalDecl(d);
RewriteIteratorDecl d = new RewriteIteratorDecl(this, ID, 0);
getCurrentBlock().addLocalDecl(d);
RewriteIteratorInit init = new RewriteIteratorInit(this, d);
getCurrentBlock().addPreambleOp(init);
return list(new RewriteRuleRef(this, ID));
}
public List<SrcOp> rewrite_tokenRef(GrammarAST ID) {
RewriteIteratorDecl d = new RewriteIteratorDecl(this, ID, 0);
getCurrentRewriteBlock().addLocalDecl(d);
getCurrentBlock().addLocalDecl(d);
RewriteIteratorInit init = new RewriteIteratorInit(this, d);
getCurrentRewriteBlock().addPreambleOp(init);
getCurrentBlock().addPreambleOp(init);
return list(new RewriteTokenRef(this, ID, d));
}

View File

@ -9,6 +9,7 @@ options {
package org.antlr.v4.codegen;
import org.antlr.v4.misc.Utils;
import org.antlr.v4.codegen.model.*;
import org.antlr.v4.codegen.model.decl.*;
import org.antlr.v4.tool.*;
import java.util.Collections;
import java.util.Map;
@ -16,6 +17,7 @@ import java.util.HashMap;
}
@members {
public int rewriteLevel = 0;
public OutputModelFactory factory;
public SourceGenTriggers(TreeNodeStream input, OutputModelFactory factory) {
this(input);
@ -54,9 +56,15 @@ alternative returns [CodeBlockForAlt altCodeBlock]
)
{$altCodeBlock=$a.altCodeBlock;}
)
| ^(ALT EPSILON) {$altCodeBlock = factory.epsilon();}
| ^( ALT ( element {if ($element.omos!=null) elems.addAll($element.omos);} )+ )
{$altCodeBlock = factory.alternative(elems);}
| {
$altCodeBlock = factory.alternative(factory.getCurrentAlt());
factory.setCurrentBlock($altCodeBlock);
}
^( ALT ( element {if ($element.omos!=null) elems.addAll($element.omos);} )+ )
{$altCodeBlock.ops = elems;}
;
element returns [List<? extends SrcOp> omos]
@ -150,11 +158,15 @@ elementOption
rewrite returns [Rewrite code]
: {
$code = factory.treeRewrite($start);
factory.setCurrentRewriteBlock($code);
$code = factory.treeRewrite($start, rewriteLevel++);
CodeBlock save = factory.getCurrentBlock();
factory.setCurrentBlock($code);
}
predicatedRewrite* nakedRewrite
{$code.ops = $nakedRewrite.omos;}
{
$code.ops = $nakedRewrite.omos;
factory.setCurrentBlock(save);
}
;
predicatedRewrite returns [List<SrcOp> omos]

View File

@ -237,7 +237,6 @@ public class Target {
public String getElementListName(GrammarAST elem) {
ST st = gen.templates.getInstanceOf("ElementListName");
String text = elem.getText();
st.add("elemName", getElementName(elem));
return st.render();
}

View File

@ -30,21 +30,16 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.decl.CodeBlock;
import java.util.*;
import java.util.List;
/** */
public class CodeBlockForAlt extends SrcOp {
@ModelElement public List ops; // has to be unchecked so we can add different subclasses of SrcOp :(
public CodeBlockForAlt(OutputModelFactory factory) { this.factory = factory; }
/** Contains Rewrite block (usually as last op) */
public class CodeBlockForAlt extends CodeBlock {
public CodeBlockForAlt(OutputModelFactory factory) { super(factory); }
public CodeBlockForAlt(OutputModelFactory factory, List<SrcOp> ops) {
super(factory);
this.ops = ops;
}
public CodeBlockForAlt(OutputModelFactory factory, final SrcOp elem) {
this(factory, new ArrayList<SrcOp>() {{ add(elem); }});
}
}

View File

@ -30,29 +30,10 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.decl.Decl;
import org.antlr.v4.runtime.misc.OrderedHashSet;
import java.util.*;
public class Rewrite extends SrcOp {
@ModelElement public OrderedHashSet<Decl> locals;
@ModelElement public List<SrcOp> preamble;
@ModelElement public List<? extends SrcOp> ops;
import org.antlr.v4.codegen.model.decl.CodeBlock;
public class Rewrite extends CodeBlock {
public Rewrite(OutputModelFactory factory) {
super(factory);
}
/** Add local var decl */
public void addLocalDecl(Decl d) {
if ( locals==null ) locals = new OrderedHashSet<Decl>();
locals.add(d);
d.isLocal = true;
}
public void addPreambleOp(SrcOp op) {
if ( preamble==null ) preamble = new ArrayList<SrcOp>();
preamble.add(op);
}
}

View File

@ -35,12 +35,22 @@ import org.antlr.v4.tool.GrammarAST;
/** */
public abstract class SrcOp extends OutputModelObject {
/** Used to create unique var names etc... */
public int uniqueID;
// public int uniqueID;
/** All operations know in which block they live:
*
* CodeBlockForAlt, TreeRewrite, STRewrite
*
* Templates might need to know block nesting level or find
* a specific declaration, etc...
*/
public SrcOp enclosingBlock;
public SrcOp() {;}
public SrcOp(OutputModelFactory factory) { super(factory); }
public SrcOp(OutputModelFactory factory, GrammarAST ast) {
super(factory,ast);
uniqueID = ast.token.getTokenIndex();
//uniqueID = ast.token.getTokenIndex();
enclosingBlock = factory.getCurrentBlock();
}
}

View File

@ -31,9 +31,16 @@ package org.antlr.v4.codegen.model.ast;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.Rewrite;
import org.antlr.v4.codegen.model.decl.RootDecl;
public class TreeRewrite extends Rewrite {
public TreeRewrite(OutputModelFactory factory) {
public int level;
public RootDecl rootDecl;
public TreeRewrite(OutputModelFactory factory, int level) {
super(factory);
this.level = level;
rootDecl = new RootDecl(factory, level); // track here too for easy access in template
addLocalDecl(rootDecl);
}
}

View File

@ -0,0 +1,58 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
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.codegen.model.decl;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.*;
import org.antlr.v4.runtime.misc.OrderedHashSet;
import java.util.*;
public class CodeBlock extends SrcOp {
@ModelElement public OrderedHashSet<Decl> locals;
@ModelElement public List<SrcOp> preamble;
@ModelElement public List ops; // has to be unchecked so we can add different subclasses of SrcOp :(
public CodeBlock(OutputModelFactory factory) {
super(factory);
}
/** Add local var decl */
public void addLocalDecl(Decl d) {
if ( locals==null ) locals = new OrderedHashSet<Decl>();
locals.add(d);
d.isLocal = true;
}
public void addPreambleOp(SrcOp op) {
if ( preamble==null ) preamble = new ArrayList<SrcOp>();
preamble.add(op);
}
}

View File

@ -35,7 +35,7 @@ import org.antlr.v4.codegen.model.SrcOp;
/** */
public class Decl extends SrcOp {
public String name;
public String decl; // whole thing if copied from action
public String decl; // whole thing if copied from action
public boolean isLocal; // if local var (not in RuleContext struct)
public Decl(OutputModelFactory factory, String name, String decl) {

View File

@ -33,7 +33,6 @@ import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.tool.GrammarAST;
public class ElementListDecl extends Decl {
public int level;
public ElementListDecl(OutputModelFactory factory, GrammarAST elem) {
super(factory, factory.getGenerator().target.getElementListName(elem));
}

View File

@ -0,0 +1,41 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
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.codegen.model.decl;
import org.antlr.v4.codegen.OutputModelFactory;
public class NestedDecl extends Decl {
public int level;
public NestedDecl(OutputModelFactory factory, String name, int level) {
super(factory, name);
this.level = level;
}
}

View File

@ -32,12 +32,10 @@ package org.antlr.v4.codegen.model.decl;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.tool.GrammarAST;
public class RewriteIteratorDecl extends Decl {
public int level;
public class RewriteIteratorDecl extends NestedDecl {
public String listName;
public RewriteIteratorDecl(OutputModelFactory factory, GrammarAST elem, int level) {
super(factory, factory.getGenerator().target.getRewriteIteratorName(elem, level));
super(factory, factory.getGenerator().target.getRewriteIteratorName(elem, level), level);
listName = factory.getGenerator().target.getElementListName(elem);
this.level = level;
}
}

View File

@ -31,9 +31,8 @@ package org.antlr.v4.codegen.model.decl;
import org.antlr.v4.codegen.OutputModelFactory;
public class RootDecl extends Decl {
public int level;
public class RootDecl extends NestedDecl {
public RootDecl(OutputModelFactory factory, int level) {
super(factory, factory.getGenerator().target.getRootName(level));
super(factory, factory.getGenerator().target.getRootName(level), level);
}
}