got lookahead _la decl done

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6864]
This commit is contained in:
parrt 2010-05-13 11:59:04 -08:00
parent d1b8c3229d
commit 82b93e717d
11 changed files with 117 additions and 58 deletions

View File

@ -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>
ParserFile(f, parser, dfaDefs, bitSetDefs) ::= <<
// $ANTLR ANTLRVersion> <f.fileName> generatedTimestamp>
@ -22,8 +34,9 @@ BitSetDef(b) ::= <<
// define <b.name>
>>
RuleFunction(f,code) ::= <<
RuleFunction(f,code,decls) ::= <<
<f.modifiers:{f | <f> }>void <f.name>(<f.args>) {
<decls>
<code>
}
>>
@ -46,7 +59,7 @@ switch ( input.LA(1) ) {
LL1OptionalBlock ::= LL1Choice
LL1OptionalBlockSingleAlt(choice, expr, alts, preamble) ::= <<
<preamble>
<preamble; separator="\n">
if ( <expr> ) {
<alts; separator="\n">
}
@ -70,10 +83,11 @@ while (true) {
}
>>
LL1StarBlockSingleAlt(choice, expr, alts, preamble) ::= <<
<preamble>
LL1StarBlockSingleAlt(choice, expr, alts, preamble, iteration) ::= <<
<preamble; separator="\n">
while ( <expr> ) {
<alts; separator="\n">
<iteration>
}
>>
@ -94,12 +108,11 @@ while (true) {
}
>>
LL1PlusBlockSingleAlt(choice, expr, alts, preamble) ::= <<
<preamble>
LL1PlusBlockSingleAlt(choice, expr, alts, preamble, iteration) ::= <<
<preamble; separator="\n">
do {
<alts; separator="\n">
// TODO: only if !set
choice.expr.nextToken.varName> = input.LA(1);
<iteration>
} while ( <expr> );
>>
@ -130,7 +143,18 @@ SemPred(p) ::= <<
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"

View File

@ -3,5 +3,16 @@ package org.antlr.v4.codegen.src;
/** */
public class Decl extends SrcOp {
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);
}
}

View File

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

View File

@ -9,7 +9,7 @@ import org.antlr.v4.tool.GrammarAST;
import java.util.List;
/** */
public class LL1PlusBlock extends LL1Choice {
public class LL1PlusBlock extends LL1Loop {
public String loopLabel;
public String loopCounterVar;
public String[] exitLook;

View File

@ -4,28 +4,14 @@ 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 class LL1PlusBlockSingleAlt extends LL1Choice {
public Object expr;
public List<SrcOp> loopIteration = new ArrayList<SrcOp>();
public class LL1PlusBlockSingleAlt extends LL1Loop {
public LL1PlusBlockSingleAlt(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> alts) {
super(factory, blkAST, alts);
IntervalSet loopBackLook = altLookSets[2]; // loop exit is alt 1
expr = factory.getLL1Test(loopBackLook, ast);
if ( expr instanceof TestSetInline ) {
CaptureNextToken nextToken = new CaptureNextToken("la"+ast.token.getTokenIndex());
addPreambleOp(nextToken);
loopIteration.add(nextToken);
}
addLookaheadTempVar(loopBackLook);
}
@Override
public List<String> getChildren() {
final List<String> sup = super.getChildren();
return new ArrayList<String>() {{ if ( sup!=null ) addAll(sup); add("expr"); }};
}
}

View File

@ -9,7 +9,7 @@ import org.antlr.v4.tool.GrammarAST;
import java.util.List;
/** */
public class LL1StarBlock extends LL1Choice {
public class LL1StarBlock extends LL1Loop {
public String loopLabel;
public String[] exitLook;
public LL1StarBlock(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> alts) {

View File

@ -4,28 +4,13 @@ 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 class LL1StarBlockSingleAlt extends LL1Choice {
public Object expr;
public List<SrcOp> loopIteration = new ArrayList<SrcOp>();
public class LL1StarBlockSingleAlt extends LL1Loop {
public LL1StarBlockSingleAlt(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> alts) {
super(factory, blkAST, alts);
IntervalSet look = altLookSets[1];
expr = factory.getLL1Test(look, blkAST);
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"); }};
addLookaheadTempVar(look);
}
}

View File

@ -3,6 +3,7 @@ package org.antlr.v4.codegen.src;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.SourceGenTriggers;
import org.antlr.v4.misc.OrderedHashSet;
import org.antlr.v4.misc.Utils;
import org.antlr.v4.parse.ANTLRParser;
import org.antlr.v4.parse.GrammarASTAdaptor;
@ -28,6 +29,7 @@ public class RuleFunction extends OutputModelObject {
public List<String> exceptions;
public String finallyAction;
public OrderedHashSet<SrcOp> decls;
public SrcOp code;
public RuleFunction(OutputModelFactory factory, Rule r) {
@ -61,9 +63,16 @@ public class RuleFunction extends OutputModelObject {
factory.currentRule.pop();
}
public void addDecl(Decl d) {
if ( decls==null ) decls = new OrderedHashSet<SrcOp>();
decls.add(d);
}
@Override
public List<String> 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");
}};
}
}

View File

@ -13,7 +13,7 @@ public class TestSetInline extends OutputModelObject {
public TestSetInline(OutputModelFactory factory, GrammarAST blkAST, IntervalSet set) {
super(factory, blkAST);
this.ttypes = factory.gen.target.getTokenTypesAsTargetLabels(factory.g, set.toArray());
this.varName = "la"+blkAST.token.getTokenIndex();
this.varName = "_la";
// this.choice = choice;
// nextToken = new CaptureNextToken();
// choice.addPreambleOp(nextToken);

View File

@ -0,0 +1,8 @@
package org.antlr.v4.codegen.src;
/** */
public class TokenDecl extends Decl {
public TokenDecl(String varName) {
super(varName,null);
}
}

View File

@ -40,7 +40,7 @@ public class OrderedHashSet<T> extends HashSet<T> {
return result;
}
public boolean remove(Object o) {
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@ -56,16 +56,6 @@ public class OrderedHashSet<T> extends HashSet<T> {
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() {
return elements.toString();
}