forked from jasder/antlr
got lookahead _la decl done
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6864]
This commit is contained in:
parent
d1b8c3229d
commit
82b93e717d
|
@ -1,3 +1,15 @@
|
||||||
|
javaTypeInitMap ::= [
|
||||||
|
"int":"0",
|
||||||
|
"long":"0",
|
||||||
|
"float":"0.0f",
|
||||||
|
"double":"0.0",
|
||||||
|
"boolean":"false",
|
||||||
|
"byte":"0",
|
||||||
|
"short":"0",
|
||||||
|
"char":"0",
|
||||||
|
default:"null" // anything other than an atomic type
|
||||||
|
]
|
||||||
|
|
||||||
// args must be <object-model-object>, <fields-resulting-in-STs>
|
// args must be <object-model-object>, <fields-resulting-in-STs>
|
||||||
ParserFile(f, parser, dfaDefs, bitSetDefs) ::= <<
|
ParserFile(f, parser, dfaDefs, bitSetDefs) ::= <<
|
||||||
// $ANTLR ANTLRVersion> <f.fileName> generatedTimestamp>
|
// $ANTLR ANTLRVersion> <f.fileName> generatedTimestamp>
|
||||||
|
@ -22,8 +34,9 @@ BitSetDef(b) ::= <<
|
||||||
// define <b.name>
|
// define <b.name>
|
||||||
>>
|
>>
|
||||||
|
|
||||||
RuleFunction(f,code) ::= <<
|
RuleFunction(f,code,decls) ::= <<
|
||||||
<f.modifiers:{f | <f> }>void <f.name>(<f.args>) {
|
<f.modifiers:{f | <f> }>void <f.name>(<f.args>) {
|
||||||
|
<decls>
|
||||||
<code>
|
<code>
|
||||||
}
|
}
|
||||||
>>
|
>>
|
||||||
|
@ -46,7 +59,7 @@ switch ( input.LA(1) ) {
|
||||||
LL1OptionalBlock ::= LL1Choice
|
LL1OptionalBlock ::= LL1Choice
|
||||||
|
|
||||||
LL1OptionalBlockSingleAlt(choice, expr, alts, preamble) ::= <<
|
LL1OptionalBlockSingleAlt(choice, expr, alts, preamble) ::= <<
|
||||||
<preamble>
|
<preamble; separator="\n">
|
||||||
if ( <expr> ) {
|
if ( <expr> ) {
|
||||||
<alts; separator="\n">
|
<alts; separator="\n">
|
||||||
}
|
}
|
||||||
|
@ -70,10 +83,11 @@ while (true) {
|
||||||
}
|
}
|
||||||
>>
|
>>
|
||||||
|
|
||||||
LL1StarBlockSingleAlt(choice, expr, alts, preamble) ::= <<
|
LL1StarBlockSingleAlt(choice, expr, alts, preamble, iteration) ::= <<
|
||||||
<preamble>
|
<preamble; separator="\n">
|
||||||
while ( <expr> ) {
|
while ( <expr> ) {
|
||||||
<alts; separator="\n">
|
<alts; separator="\n">
|
||||||
|
<iteration>
|
||||||
}
|
}
|
||||||
>>
|
>>
|
||||||
|
|
||||||
|
@ -94,12 +108,11 @@ while (true) {
|
||||||
}
|
}
|
||||||
>>
|
>>
|
||||||
|
|
||||||
LL1PlusBlockSingleAlt(choice, expr, alts, preamble) ::= <<
|
LL1PlusBlockSingleAlt(choice, expr, alts, preamble, iteration) ::= <<
|
||||||
<preamble>
|
<preamble; separator="\n">
|
||||||
do {
|
do {
|
||||||
<alts; separator="\n">
|
<alts; separator="\n">
|
||||||
// TODO: only if !set
|
<iteration>
|
||||||
choice.expr.nextToken.varName> = input.LA(1);
|
|
||||||
} while ( <expr> );
|
} while ( <expr> );
|
||||||
>>
|
>>
|
||||||
|
|
||||||
|
@ -130,7 +143,18 @@ SemPred(p) ::= <<
|
||||||
if (!(<p.ast.text>)) throw new FailedPredicateException(input, "<ruleName>", "<description>");
|
if (!(<p.ast.text>)) throw new FailedPredicateException(input, "<ruleName>", "<description>");
|
||||||
>>
|
>>
|
||||||
|
|
||||||
CaptureNextToken(d) ::= "Token <d.varName> = input.LA(1);"
|
//Decl(d) ::= "<d.type> <d.varName> = <d.type:initValue()>;"
|
||||||
|
|
||||||
|
TokenDecl(t) ::= "Token <t.varName>;"
|
||||||
|
|
||||||
|
CaptureNextToken(d) ::= "<d.varName> = input.LA(1);"
|
||||||
|
|
||||||
|
/** Using a type to init value map, try to init a type; if not in table
|
||||||
|
* must be an object, default value is "null".
|
||||||
|
*/
|
||||||
|
initValue(typeName) ::= <<
|
||||||
|
<javaTypeInitMap.(typeName)>
|
||||||
|
>>
|
||||||
|
|
||||||
codeFileExtension() ::= ".java"
|
codeFileExtension() ::= ".java"
|
||||||
|
|
||||||
|
|
|
@ -3,5 +3,16 @@ package org.antlr.v4.codegen.src;
|
||||||
/** */
|
/** */
|
||||||
public class Decl extends SrcOp {
|
public class Decl extends SrcOp {
|
||||||
public String varName;
|
public String varName;
|
||||||
public Decl(String varName) { this.varName = varName; }
|
public String type;
|
||||||
|
public Decl(String varName, String type) { this.varName = varName; this.type = type; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return varName.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return varName.equals(((Decl)obj).varName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
|
import org.antlr.v4.codegen.OutputModelFactory;
|
||||||
|
import org.antlr.v4.misc.IntervalSet;
|
||||||
|
import org.antlr.v4.tool.GrammarAST;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/** */
|
||||||
|
public abstract class LL1Loop extends LL1Choice {
|
||||||
|
public Object expr;
|
||||||
|
public List<SrcOp> iteration;
|
||||||
|
|
||||||
|
public LL1Loop(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> alts) {
|
||||||
|
super(factory, blkAST, alts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addIterationOp(SrcOp op) {
|
||||||
|
if ( iteration==null ) iteration = new ArrayList<SrcOp>();
|
||||||
|
iteration.add(op);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addLookaheadTempVar(IntervalSet look) {
|
||||||
|
expr = factory.getLL1Test(look, ast);
|
||||||
|
if ( expr instanceof TestSetInline ) {
|
||||||
|
TestSetInline e = (TestSetInline) expr;
|
||||||
|
Decl d = new TokenDecl(e.varName);
|
||||||
|
factory.currentRule.peek().addDecl(d);
|
||||||
|
CaptureNextToken nextToken = new CaptureNextToken(e.varName);
|
||||||
|
addPreambleOp(nextToken);
|
||||||
|
addIterationOp(nextToken);
|
||||||
|
iteration = new ArrayList<SrcOp>();
|
||||||
|
iteration.add(nextToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getChildren() {
|
||||||
|
final List<String> sup = super.getChildren();
|
||||||
|
return new ArrayList<String>() {{
|
||||||
|
if ( sup!=null ) addAll(sup); add("expr"); add("iteration");
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -9,7 +9,7 @@ import org.antlr.v4.tool.GrammarAST;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class LL1PlusBlock extends LL1Choice {
|
public class LL1PlusBlock extends LL1Loop {
|
||||||
public String loopLabel;
|
public String loopLabel;
|
||||||
public String loopCounterVar;
|
public String loopCounterVar;
|
||||||
public String[] exitLook;
|
public String[] exitLook;
|
||||||
|
|
|
@ -4,28 +4,14 @@ import org.antlr.v4.codegen.OutputModelFactory;
|
||||||
import org.antlr.v4.misc.IntervalSet;
|
import org.antlr.v4.misc.IntervalSet;
|
||||||
import org.antlr.v4.tool.GrammarAST;
|
import org.antlr.v4.tool.GrammarAST;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class LL1PlusBlockSingleAlt extends LL1Choice {
|
public class LL1PlusBlockSingleAlt extends LL1Loop {
|
||||||
public Object expr;
|
|
||||||
public List<SrcOp> loopIteration = new ArrayList<SrcOp>();
|
|
||||||
public LL1PlusBlockSingleAlt(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> alts) {
|
public LL1PlusBlockSingleAlt(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> alts) {
|
||||||
super(factory, blkAST, alts);
|
super(factory, blkAST, alts);
|
||||||
IntervalSet loopBackLook = altLookSets[2]; // loop exit is alt 1
|
IntervalSet loopBackLook = altLookSets[2]; // loop exit is alt 1
|
||||||
expr = factory.getLL1Test(loopBackLook, ast);
|
addLookaheadTempVar(loopBackLook);
|
||||||
if ( expr instanceof TestSetInline ) {
|
|
||||||
CaptureNextToken nextToken = new CaptureNextToken("la"+ast.token.getTokenIndex());
|
|
||||||
addPreambleOp(nextToken);
|
|
||||||
loopIteration.add(nextToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> getChildren() {
|
|
||||||
final List<String> sup = super.getChildren();
|
|
||||||
return new ArrayList<String>() {{ if ( sup!=null ) addAll(sup); add("expr"); }};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import org.antlr.v4.tool.GrammarAST;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class LL1StarBlock extends LL1Choice {
|
public class LL1StarBlock extends LL1Loop {
|
||||||
public String loopLabel;
|
public String loopLabel;
|
||||||
public String[] exitLook;
|
public String[] exitLook;
|
||||||
public LL1StarBlock(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> alts) {
|
public LL1StarBlock(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> alts) {
|
||||||
|
|
|
@ -4,28 +4,13 @@ import org.antlr.v4.codegen.OutputModelFactory;
|
||||||
import org.antlr.v4.misc.IntervalSet;
|
import org.antlr.v4.misc.IntervalSet;
|
||||||
import org.antlr.v4.tool.GrammarAST;
|
import org.antlr.v4.tool.GrammarAST;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
public class LL1StarBlockSingleAlt extends LL1Choice {
|
public class LL1StarBlockSingleAlt extends LL1Loop {
|
||||||
public Object expr;
|
|
||||||
public List<SrcOp> loopIteration = new ArrayList<SrcOp>();
|
|
||||||
public LL1StarBlockSingleAlt(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> alts) {
|
public LL1StarBlockSingleAlt(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> alts) {
|
||||||
super(factory, blkAST, alts);
|
super(factory, blkAST, alts);
|
||||||
IntervalSet look = altLookSets[1];
|
IntervalSet look = altLookSets[1];
|
||||||
expr = factory.getLL1Test(look, blkAST);
|
addLookaheadTempVar(look);
|
||||||
if ( expr instanceof TestSetInline ) {
|
|
||||||
TestSetInline e = (TestSetInline)expr;
|
|
||||||
CaptureNextToken nextToken = new CaptureNextToken(e.varName);
|
|
||||||
addPreambleOp(nextToken);
|
|
||||||
loopIteration.add(nextToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> getChildren() {
|
|
||||||
final List<String> sup = super.getChildren();
|
|
||||||
return new ArrayList<String>() {{ if ( sup!=null ) addAll(sup); add("expr"); }};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.antlr.v4.codegen.src;
|
||||||
import org.antlr.runtime.tree.CommonTreeNodeStream;
|
import org.antlr.runtime.tree.CommonTreeNodeStream;
|
||||||
import org.antlr.v4.codegen.OutputModelFactory;
|
import org.antlr.v4.codegen.OutputModelFactory;
|
||||||
import org.antlr.v4.codegen.SourceGenTriggers;
|
import org.antlr.v4.codegen.SourceGenTriggers;
|
||||||
|
import org.antlr.v4.misc.OrderedHashSet;
|
||||||
import org.antlr.v4.misc.Utils;
|
import org.antlr.v4.misc.Utils;
|
||||||
import org.antlr.v4.parse.ANTLRParser;
|
import org.antlr.v4.parse.ANTLRParser;
|
||||||
import org.antlr.v4.parse.GrammarASTAdaptor;
|
import org.antlr.v4.parse.GrammarASTAdaptor;
|
||||||
|
@ -28,6 +29,7 @@ public class RuleFunction extends OutputModelObject {
|
||||||
public List<String> exceptions;
|
public List<String> exceptions;
|
||||||
public String finallyAction;
|
public String finallyAction;
|
||||||
|
|
||||||
|
public OrderedHashSet<SrcOp> decls;
|
||||||
public SrcOp code;
|
public SrcOp code;
|
||||||
|
|
||||||
public RuleFunction(OutputModelFactory factory, Rule r) {
|
public RuleFunction(OutputModelFactory factory, Rule r) {
|
||||||
|
@ -61,9 +63,16 @@ public class RuleFunction extends OutputModelObject {
|
||||||
factory.currentRule.pop();
|
factory.currentRule.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addDecl(Decl d) {
|
||||||
|
if ( decls==null ) decls = new OrderedHashSet<SrcOp>();
|
||||||
|
decls.add(d);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getChildren() {
|
public List<String> getChildren() {
|
||||||
final List<String> sup = super.getChildren();
|
final List<String> sup = super.getChildren();
|
||||||
return new ArrayList<String>() {{ if ( sup!=null ) addAll(sup); add("code"); }};
|
return new ArrayList<String>() {{
|
||||||
|
if ( sup!=null ) addAll(sup); add("decls"); add("code");
|
||||||
|
}};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ public class TestSetInline extends OutputModelObject {
|
||||||
public TestSetInline(OutputModelFactory factory, GrammarAST blkAST, IntervalSet set) {
|
public TestSetInline(OutputModelFactory factory, GrammarAST blkAST, IntervalSet set) {
|
||||||
super(factory, blkAST);
|
super(factory, blkAST);
|
||||||
this.ttypes = factory.gen.target.getTokenTypesAsTargetLabels(factory.g, set.toArray());
|
this.ttypes = factory.gen.target.getTokenTypesAsTargetLabels(factory.g, set.toArray());
|
||||||
this.varName = "la"+blkAST.token.getTokenIndex();
|
this.varName = "_la";
|
||||||
// this.choice = choice;
|
// this.choice = choice;
|
||||||
// nextToken = new CaptureNextToken();
|
// nextToken = new CaptureNextToken();
|
||||||
// choice.addPreambleOp(nextToken);
|
// choice.addPreambleOp(nextToken);
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
package org.antlr.v4.codegen.src;
|
||||||
|
|
||||||
|
/** */
|
||||||
|
public class TokenDecl extends Decl {
|
||||||
|
public TokenDecl(String varName) {
|
||||||
|
super(varName,null);
|
||||||
|
}
|
||||||
|
}
|
|
@ -40,7 +40,7 @@ public class OrderedHashSet<T> extends HashSet<T> {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean remove(Object o) {
|
public boolean remove(Object o) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,16 +56,6 @@ public class OrderedHashSet<T> extends HashSet<T> {
|
||||||
return elements;
|
return elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
|
||||||
/*
|
|
||||||
if ( elements.size()!=super.size() ) {
|
|
||||||
ErrorManager.internalError("OrderedHashSet: elements and set size differs; "+
|
|
||||||
elements.size()+"!="+super.size());
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return elements.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return elements.toString();
|
return elements.toString();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue