Merge commit '51c38c1'

This commit is contained in:
Terence Parr 2012-03-06 13:27:46 -08:00
commit ce142a2a4c
4 changed files with 21 additions and 11 deletions

View File

@ -225,6 +225,9 @@ RuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAc
<namedActions.init> <namedActions.init>
<locals; separator="\n"> <locals; separator="\n">
try { try {
<if(currentRule.hasLookaheadBlock)>
int _alt;
<endif>
<code> <code>
_localctx.stop = _input.LT(-1); _localctx.stop = _input.LT(-1);
<postamble; separator="\n"> <postamble; separator="\n">
@ -258,6 +261,9 @@ LeftRecursiveRuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,
<namedActions.init> <namedActions.init>
<locals; separator="\n"> <locals; separator="\n">
try { try {
<if(currentRule.hasLookaheadBlock)>
int _alt;
<endif>
<code> <code>
_localctx.stop = _input.LT(-1); _localctx.stop = _input.LT(-1);
<postamble; separator="\n"> <postamble; separator="\n">
@ -409,29 +415,27 @@ case <i>:
} }
>> >>
// TODO: we we need uniqueID? a single _alt might work
StarBlock(choice, alts, sync, iteration) ::= << StarBlock(choice, alts, sync, iteration) ::= <<
setState(<choice.stateNumber>); setState(<choice.stateNumber>);
_errHandler.sync(this); _errHandler.sync(this);
int _alt<choice.uniqueID> = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx); _alt = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx);
while ( _alt<choice.uniqueID>!=<choice.exitAlt> && _alt<choice.uniqueID>!=-1 ) { while ( _alt!=<choice.exitAlt> && _alt!=-1 ) {
if ( _alt<choice.uniqueID>==1 ) { if ( _alt==1 ) {
<iteration> <iteration>
<alts> <! should only be one !> <alts> <! should only be one !>
} }
setState(<choice.loopBackStateNumber>); setState(<choice.loopBackStateNumber>);
_errHandler.sync(this); _errHandler.sync(this);
_alt<choice.uniqueID> = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx); _alt = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx);
} }
>> >>
PlusBlock(choice, alts, error) ::= << PlusBlock(choice, alts, error) ::= <<
setState(<choice.blockStartStateNumber>); <! alt block decision !> setState(<choice.blockStartStateNumber>); <! alt block decision !>
_errHandler.sync(this); _errHandler.sync(this);
int _alt<choice.uniqueID> = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx); _alt = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx);
do { do {
switch ( _alt<choice.uniqueID> ) { switch ( _alt ) {
<alts:{alt| <alts:{alt|
case <i>: case <i>:
<alt> <alt>
@ -441,8 +445,8 @@ case <i>:
} }
setState(<choice.loopBackStateNumber>); <! loopback/exit decision !> setState(<choice.loopBackStateNumber>); <! loopback/exit decision !>
_errHandler.sync(this); _errHandler.sync(this);
_alt<choice.uniqueID> = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx); _alt = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx);
} while ( _alt<choice.uniqueID>!=<choice.exitAlt> && _alt<choice.uniqueID>!=-1 ); } while ( _alt!=<choice.exitAlt> && _alt!=-1 );
>> >>
Sync(s) ::= "sync(<s.expecting.name>);" Sync(s) ::= "sync(<s.expecting.name>);"

View File

@ -274,6 +274,7 @@ public class OutputModelController {
try { try {
// walk AST of rule alts/elements // walk AST of rule alts/elements
function.code = DefaultOutputModelFactory.list(walker.block(null, null)); function.code = DefaultOutputModelFactory.list(walker.block(null, null));
function.hasLookaheadBlock = walker.hasLookaheadBlock;
} }
catch (org.antlr.runtime.RecognitionException e){ catch (org.antlr.runtime.RecognitionException e){
e.printStackTrace(System.err); e.printStackTrace(System.err);

View File

@ -47,6 +47,7 @@ import java.util.HashMap;
@members { @members {
public OutputModelController controller; public OutputModelController controller;
public boolean hasLookaheadBlock;
public SourceGenTriggers(TreeNodeStream input, OutputModelController controller) { public SourceGenTriggers(TreeNodeStream input, OutputModelController controller) {
this(input); this(input);
this.controller = controller; this.controller = controller;
@ -66,7 +67,9 @@ block[GrammarAST label, GrammarAST ebnfRoot] returns [List<? extends SrcOp> omos
$omos = DefaultOutputModelFactory.list(controller.getChoiceBlock((BlockAST)$blk, alts, $label)); $omos = DefaultOutputModelFactory.list(controller.getChoiceBlock((BlockAST)$blk, alts, $label));
} }
else { else {
$omos = DefaultOutputModelFactory.list(controller.getEBNFBlock($ebnfRoot, alts)); Choice choice = controller.getEBNFBlock($ebnfRoot, alts);
hasLookaheadBlock |= choice instanceof PlusBlock || choice instanceof StarBlock;
$omos = DefaultOutputModelFactory.list(choice);
} }
} }
; ;
@ -132,6 +135,7 @@ subrule returns [List<? extends SrcOp> omos]
alt.addOp(blk); alt.addOp(blk);
alts.add(alt); alts.add(alt);
SrcOp loop = controller.getEBNFBlock($op, alts); // "star it" SrcOp loop = controller.getEBNFBlock($op, alts); // "star it"
hasLookaheadBlock |= loop instanceof PlusBlock || loop instanceof StarBlock;
$omos = DefaultOutputModelFactory.list(loop); $omos = DefaultOutputModelFactory.list(loop);
} }
| block[null, null] {$omos = $block.omos;} | block[null, null] {$omos = $block.omos;}

View File

@ -76,6 +76,7 @@ public class RuleFunction extends OutputModelObject {
public Collection<Attribute> args = null; public Collection<Attribute> args = null;
public Rule rule; public Rule rule;
public AltLabelStructDecl[] altToContext; public AltLabelStructDecl[] altToContext;
public boolean hasLookaheadBlock;
@ModelElement public List<SrcOp> code; @ModelElement public List<SrcOp> code;
@ModelElement public OrderedHashSet<Decl> locals; // TODO: move into ctx? @ModelElement public OrderedHashSet<Decl> locals; // TODO: move into ctx?