[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6863]
This commit is contained in:
parrt 2010-05-12 16:50:23 -08:00
parent adc231ddae
commit d1b8c3229d
11 changed files with 387 additions and 372 deletions

View File

@ -74,7 +74,6 @@ LL1StarBlockSingleAlt(choice, expr, alts, preamble) ::= <<
<preamble>
while ( <expr> ) {
<alts; separator="\n">
<choice.loop
}
>>
@ -100,7 +99,7 @@ LL1PlusBlockSingleAlt(choice, expr, alts, preamble) ::= <<
do {
<alts; separator="\n">
// TODO: only if !set
<choice.expr.nextToken.varName> = input.LA(1);
choice.expr.nextToken.varName> = input.LA(1);
} while ( <expr> );
>>

View File

@ -10,13 +10,16 @@ import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.GrammarAST;
import java.util.List;
import java.util.Stack;
/** */
/** Create output objects wthin rule functions */
public abstract class OutputModelFactory {
public Grammar g;
public CodeGenerator gen;
public OutputModelObject root;
// Context ptrs
public OutputModelObject file; // root
public Stack<RuleFunction> currentRule = new Stack<RuleFunction>();
protected OutputModelFactory(CodeGenerator gen) {
this.gen = gen;
@ -25,17 +28,20 @@ public abstract class OutputModelFactory {
public abstract OutputModelObject buildOutputModel();
public abstract ParserFile outputFile(String fileName);
public abstract Parser parser(ParserFile pf);
public CodeBlock epsilon() { return new CodeBlock(this); }
public CodeBlock alternative(List<SrcOp> elems) { return new CodeBlock(this, elems); }
public SrcOp action(GrammarAST ast) { return new Action(this, ast); }
public SrcOp sempred(GrammarAST ast) { return new SemPred(this, ast); }
public abstract List<SrcOp> ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args);
public abstract List<SrcOp> tokenRef(GrammarAST ID, GrammarAST label, GrammarAST args);
public abstract List<SrcOp> stringRef(GrammarAST ID, GrammarAST label);
public Choice getChoiceBlock(BlockAST blkAST, GrammarAST ebnfRoot, List<CodeBlock> alts) {
// TODO: assumes LL1
int ebnf = 0;

View File

@ -1,8 +1,11 @@
package org.antlr.v4.codegen;
import org.antlr.v4.codegen.src.*;
import org.antlr.v4.misc.Utils;
import org.antlr.v4.tool.GrammarAST;
import org.antlr.v4.tool.TerminalAST;
import java.util.Stack;
import java.util.List;
/** */
public class ParserFactory extends OutputModelFactory {
@ -16,30 +19,29 @@ public class ParserFactory extends OutputModelFactory {
// put(MatchToken.class, "matchToken");
// }};
// Context ptrs
ParserFile file;
Parser parser;
Stack<RuleFunction> currentRule;
public ParserFactory(CodeGenerator gen) {
super(gen);
}
public OutputModelObject buildOutputModel() {
root = file = new ParserFile(this, gen.getRecognizerFileName());
file.parser = new Parser(this, file);
// side-effect: fills pf dfa and bitset defs
return file;
return new ParserFile(this, gen.getRecognizerFileName());
}
public ParserFile outputFile(String fileName) {
return new ParserFile(this, fileName);
@Override
public List<SrcOp> ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args) {
InvokeRule r = new InvokeRule(this, ID, label);
return Utils.list(r);
}
public Parser parser(ParserFile pf) {
return new Parser(this, pf);
@Override
public List<SrcOp> tokenRef(GrammarAST ID, GrammarAST label, GrammarAST args) {
return Utils.list(new MatchToken(this, (TerminalAST)ID, label));
}
public void defineBitSet(BitSetDef b) { file.defineBitSet(b); }
@Override
public List<SrcOp> stringRef(GrammarAST ID, GrammarAST label) {
return tokenRef(ID, label, null);
}
public void defineBitSet(BitSetDef b) { ((ParserFile)file).defineBitSet(b); }
}

View File

@ -7,6 +7,7 @@ options {
@header {
package org.antlr.v4.codegen;
import org.antlr.v4.misc.Utils;
import org.antlr.v4.codegen.src.*;
import org.antlr.v4.tool.*;
import java.util.Collections;
@ -38,24 +39,24 @@ alternative returns [CodeBlock omo]
@init {List<SrcOp> elems = new ArrayList<SrcOp>();}
: ^(ALT_REWRITE a=alternative .)
| ^(ALT EPSILON) {$omo = factory.epsilon();}
| ^( ALT ( element {elems.add($element.omo);} )+ ) {$omo = factory.alternative(elems);}
| ^( ALT ( element {elems.addAll($element.omos);} )+ ) {$omo = factory.alternative(elems);}
;
element returns [SrcOp omo]
: labeledElement {$omo = $labeledElement.omo;}
| atom[null] {$omo = $atom.omo;}
| ebnf {$omo = $ebnf.omo;}
| ACTION {$omo = factory.action($ACTION);}
| SEMPRED {$omo = factory.sempred($SEMPRED);}
element returns [List<SrcOp> omos]
: labeledElement {$omos = $labeledElement.omos;}
| atom[null] {$omos = $atom.omos;}
| ebnf {$omos = Utils.list($ebnf.omo);}
| ACTION {$omos = Utils.list(factory.action($ACTION));}
| SEMPRED {$omos = Utils.list(factory.sempred($SEMPRED));}
| GATED_SEMPRED
| treeSpec
;
labeledElement returns [SrcOp omo]
: ^(ASSIGN ID atom[$ID] ) {$omo = $atom.omo;}
| ^(ASSIGN ID block[$ID,null]) {$omo = $block.omo;}
| ^(PLUS_ASSIGN ID atom[$ID]) {$omo = $atom.omo;}
| ^(PLUS_ASSIGN ID block[$ID,null]) {$omo = $block.omo;}
labeledElement returns [List<SrcOp> omos]
: ^(ASSIGN ID atom[$ID] ) {$omos = $atom.omos;}
| ^(ASSIGN ID block[$ID,null]) {$omos = Utils.list($block.omo);}
| ^(PLUS_ASSIGN ID atom[$ID]) {$omos = $atom.omos;}
| ^(PLUS_ASSIGN ID block[$ID,null]) {$omos = Utils.list($block.omo);}
;
treeSpec returns [SrcOp omo]
@ -79,42 +80,42 @@ astBlockSuffix
// TODO: combine ROOT/BANG into one then just make new op ref'ing return value of atom/terminal...
// TODO: same for NOT
atom[GrammarAST label] returns [SrcOp omo]
atom[GrammarAST label] returns [List<SrcOp> omos]
: ^(ROOT range[label])
| ^(BANG range[label]) {$omo = $range.omo;}
| ^(BANG range[label]) {$omos = $range.omos;}
| ^(ROOT notSet[label])
| ^(BANG notSet[label]) {$omo = $notSet.omo;}
| ^(BANG notSet[label]) {$omos = $notSet.omos;}
| notSet[label]
| range[label] {$omo = $range.omo;}
| range[label] {$omos = $range.omos;}
| ^(DOT ID terminal[label])
| ^(DOT ID ruleref[label])
| ^(WILDCARD .)
| WILDCARD
| terminal[label] {$omo = $terminal.omo;}
| ruleref[label] {$omo = $ruleref.omo;}
| terminal[label] {$omos = $terminal.omos;}
| ruleref[label] {$omos = $ruleref.omos;}
;
notSet[GrammarAST label] returns [SrcOp omo]
notSet[GrammarAST label] returns [List<SrcOp> omos]
: ^(NOT terminal[label])
| ^(NOT block[label,null])
;
ruleref[GrammarAST label] returns [SrcOp omo]
ruleref[GrammarAST label] returns [List<SrcOp> omos]
: ^(ROOT ^(RULE_REF ARG_ACTION?))
| ^(BANG ^(RULE_REF ARG_ACTION?)) {$omo = new InvokeRule(factory, $RULE_REF, $label);}
| ^(RULE_REF ARG_ACTION?) {$omo = new InvokeRule(factory, $RULE_REF, $label);}
| ^(BANG ^(RULE_REF ARG_ACTION?)) {$omos = factory.ruleRef($RULE_REF, $label, $ARG_ACTION);}
| ^(RULE_REF ARG_ACTION?) {$omos = factory.ruleRef($RULE_REF, $label, $ARG_ACTION);}
;
range[GrammarAST label] returns [SrcOp omo]
range[GrammarAST label] returns [List<SrcOp> omos]
: ^(RANGE a=STRING_LITERAL b=STRING_LITERAL)
;
terminal[GrammarAST label] returns [MatchToken omo]
: ^(STRING_LITERAL .) {$omo = new MatchToken(factory, (TerminalAST)$STRING_LITERAL, $label);}
| STRING_LITERAL {$omo = new MatchToken(factory, (TerminalAST)$STRING_LITERAL, $label);}
| ^(TOKEN_REF ARG_ACTION .) {$omo = new MatchToken(factory, (TerminalAST)$TOKEN_REF, $label);}
| ^(TOKEN_REF .) {$omo = new MatchToken(factory, (TerminalAST)$TOKEN_REF, $label);}
| TOKEN_REF {$omo = new MatchToken(factory, (TerminalAST)$TOKEN_REF, $label);}
terminal[GrammarAST label] returns [List<SrcOp> omos]
: ^(STRING_LITERAL .) {$omos = factory.stringRef($STRING_LITERAL, $label);}
| STRING_LITERAL {$omos = factory.stringRef($STRING_LITERAL, $label);}
| ^(TOKEN_REF ARG_ACTION .) {$omos = factory.tokenRef($TOKEN_REF, $label, $ARG_ACTION);}
| ^(TOKEN_REF .) {$omos = factory.tokenRef($TOKEN_REF, $label, null);}
| TOKEN_REF {$omos = factory.tokenRef($TOKEN_REF, $label, null);}
| ^(ROOT terminal[label])
| ^(BANG terminal[label])
;

File diff suppressed because it is too large Load Diff

View File

@ -14,9 +14,9 @@ public class LL1PlusBlockSingleAlt extends LL1Choice {
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, blkAST);
expr = factory.getLL1Test(loopBackLook, ast);
if ( expr instanceof TestSetInline ) {
CaptureNextToken nextToken = new CaptureNextToken("la"+blkAST.token.getTokenIndex());
CaptureNextToken nextToken = new CaptureNextToken("la"+ast.token.getTokenIndex());
addPreambleOp(nextToken);
loopIteration.add(nextToken);
}

View File

@ -10,6 +10,15 @@ public abstract class OutputModelObject {
public OutputModelFactory factory;
public GrammarAST ast;
public OutputModelObject() {;}
public OutputModelObject(OutputModelFactory factory) { this.factory = factory; }
public OutputModelObject(OutputModelFactory factory, GrammarAST ast) {
this.factory = factory;
this.ast = ast;
}
/** If the output model object encloses some other model objects,
* we need to be able to walk them. Rather than make each class
* properly walk any nested objects, I'm going to use a generic
@ -21,13 +30,4 @@ public abstract class OutputModelObject {
public List<String> getChildren() {
return null;
}
public OutputModelObject() {;}
public OutputModelObject(OutputModelFactory factory) { this.factory = factory; }
public OutputModelObject(OutputModelFactory factory, GrammarAST ast) {
this.factory = factory;
this.ast = ast;
}
}

View File

@ -17,12 +17,6 @@ public class Parser extends OutputModelObject {
this.file = file; // who contains us?
name = factory.g.getRecognizerName();
for (Rule r : factory.g.rules.values()) funcs.add( new RuleFunction(factory, r) );
// We create dfa and bitsets during rule function construction.
// They get stored in code gen for convenience as we walk rule block tree
// for (DFA dfa : gen.g.decisionDFAs.values()) {
// file.dfaDefs.add( new DFADef("DFA"+dfa.decision, dfa) );
// }
}
@Override

View File

@ -15,6 +15,8 @@ public class ParserFile extends OutputModelObject {
public ParserFile(OutputModelFactory factory, String fileName) {
super(factory);
this.fileName = fileName;
factory.file = this;
parser = new Parser(factory, this);
}
public void defineBitSet(BitSetDef b) {

View File

@ -47,6 +47,7 @@ public class RuleFunction extends OutputModelObject {
exceptions = Utils.nodesToStrings(r.exceptionActions);
if ( r.finallyAction!=null ) finallyAction = r.finallyAction.getText();
factory.currentRule.push(this);
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream());
GrammarAST blk = (GrammarAST)r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk);
@ -57,6 +58,7 @@ public class RuleFunction extends OutputModelObject {
catch (Exception e){
e.printStackTrace(System.err);
}
factory.currentRule.pop();
}
@Override

View File

@ -102,9 +102,17 @@ public class Utils {
return a;
}
public static <T> List<T> list(T... values) {
List<T> x = new ArrayList<T>(values.length);
for (T v : values) {
// public static <T> List<T> list(T... values) {
// List<T> x = new ArrayList<T>(values.length);
// for (T v : values) {
// if ( v!=null ) x.add(v);
// }
// return x;
// }
public static List list(Object... values) {
List x = new ArrayList(values.length);
for (Object v : values) {
if ( v!=null ) x.add(v);
}
return x;