forked from jasder/antlr
Merge pull request #570 from parrt/codegen-tests
Templates LL1PlusBlock and LL1StarBlock were not used. removed. added te...
This commit is contained in:
commit
f37347277f
|
@ -448,23 +448,6 @@ if (<expr>) {
|
||||||
<!else if ( !(<followExpr>) ) <error>!>
|
<!else if ( !(<followExpr>) ) <error>!>
|
||||||
>>
|
>>
|
||||||
|
|
||||||
LL1StarBlock(choice, alts) ::= <<
|
|
||||||
setState(<choice.stateNumber>);
|
|
||||||
_errHandler.sync(this);
|
|
||||||
<choice.loopLabel>:
|
|
||||||
while (true) {
|
|
||||||
switch (_input.LA(1)) {
|
|
||||||
<choice.altLook,alts:{look,alt| <cases(look)>
|
|
||||||
<alt>
|
|
||||||
break;}; separator="\n">
|
|
||||||
<cases(choice.exitLook)>
|
|
||||||
break <choice.loopLabel>;
|
|
||||||
}
|
|
||||||
setState(<choice.loopBackStateNumber>);
|
|
||||||
_errHandler.sync(this);
|
|
||||||
}
|
|
||||||
>>
|
|
||||||
|
|
||||||
LL1StarBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= <<
|
LL1StarBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= <<
|
||||||
setState(<choice.stateNumber>);
|
setState(<choice.stateNumber>);
|
||||||
_errHandler.sync(this);
|
_errHandler.sync(this);
|
||||||
|
@ -477,23 +460,6 @@ while (<loopExpr>) {
|
||||||
}
|
}
|
||||||
>>
|
>>
|
||||||
|
|
||||||
LL1PlusBlock(choice, alts, iteration, loopExpr, error) ::= <<
|
|
||||||
setState(<choice.blockStartStateNumber>); <! alt block decision !>
|
|
||||||
_errHandler.sync(this);
|
|
||||||
do {
|
|
||||||
switch (_input.LA(1)) {
|
|
||||||
<choice.altLook,alts:{look,alt| <cases(look)>
|
|
||||||
<alt>
|
|
||||||
break;}; separator="\n">
|
|
||||||
default:
|
|
||||||
<error>
|
|
||||||
}
|
|
||||||
setState(<choice.stateNumber>);
|
|
||||||
_errHandler.sync(this);
|
|
||||||
<iteration>
|
|
||||||
} while ( <loopExpr> );
|
|
||||||
>>
|
|
||||||
|
|
||||||
LL1PlusBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= <<
|
LL1PlusBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= <<
|
||||||
setState(<choice.blockStartStateNumber>); <! alt block decision !>
|
setState(<choice.blockStartStateNumber>); <! alt block decision !>
|
||||||
_errHandler.sync(this);
|
_errHandler.sync(this);
|
||||||
|
|
|
@ -41,9 +41,7 @@ import org.antlr.v4.codegen.model.InvokeRule;
|
||||||
import org.antlr.v4.codegen.model.LL1AltBlock;
|
import org.antlr.v4.codegen.model.LL1AltBlock;
|
||||||
import org.antlr.v4.codegen.model.LL1OptionalBlock;
|
import org.antlr.v4.codegen.model.LL1OptionalBlock;
|
||||||
import org.antlr.v4.codegen.model.LL1OptionalBlockSingleAlt;
|
import org.antlr.v4.codegen.model.LL1OptionalBlockSingleAlt;
|
||||||
import org.antlr.v4.codegen.model.LL1PlusBlock;
|
|
||||||
import org.antlr.v4.codegen.model.LL1PlusBlockSingleAlt;
|
import org.antlr.v4.codegen.model.LL1PlusBlockSingleAlt;
|
||||||
import org.antlr.v4.codegen.model.LL1StarBlock;
|
|
||||||
import org.antlr.v4.codegen.model.LL1StarBlockSingleAlt;
|
import org.antlr.v4.codegen.model.LL1StarBlockSingleAlt;
|
||||||
import org.antlr.v4.codegen.model.LabeledOp;
|
import org.antlr.v4.codegen.model.LabeledOp;
|
||||||
import org.antlr.v4.codegen.model.LeftRecursiveRuleFunction;
|
import org.antlr.v4.codegen.model.LeftRecursiveRuleFunction;
|
||||||
|
@ -287,11 +285,11 @@ public class ParserFactory extends DefaultOutputModelFactory {
|
||||||
break;
|
break;
|
||||||
case ANTLRParser.CLOSURE :
|
case ANTLRParser.CLOSURE :
|
||||||
if ( alts.size()==1 ) c = new LL1StarBlockSingleAlt(this, ebnfRoot, alts);
|
if ( alts.size()==1 ) c = new LL1StarBlockSingleAlt(this, ebnfRoot, alts);
|
||||||
else c = new LL1StarBlock(this, ebnfRoot, alts);
|
else c = getComplexEBNFBlock(ebnfRoot, alts);
|
||||||
break;
|
break;
|
||||||
case ANTLRParser.POSITIVE_CLOSURE :
|
case ANTLRParser.POSITIVE_CLOSURE :
|
||||||
if ( alts.size()==1 ) c = new LL1PlusBlockSingleAlt(this, ebnfRoot, alts);
|
if ( alts.size()==1 ) c = new LL1PlusBlockSingleAlt(this, ebnfRoot, alts);
|
||||||
else c = new LL1PlusBlock(this, ebnfRoot, alts);
|
else c = getComplexEBNFBlock(ebnfRoot, alts);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return c;
|
return c;
|
||||||
|
|
|
@ -1,80 +0,0 @@
|
||||||
/*
|
|
||||||
* [The "BSD license"]
|
|
||||||
* Copyright (c) 2012 Terence Parr
|
|
||||||
* Copyright (c) 2012 Sam Harwell
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
import org.antlr.v4.codegen.CodeGenerator;
|
|
||||||
import org.antlr.v4.codegen.OutputModelFactory;
|
|
||||||
import org.antlr.v4.runtime.atn.PlusBlockStartState;
|
|
||||||
import org.antlr.v4.runtime.misc.IntervalSet;
|
|
||||||
import org.antlr.v4.tool.Grammar;
|
|
||||||
import org.antlr.v4.tool.ast.GrammarAST;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/** */
|
|
||||||
public class LL1PlusBlock extends LL1Loop {
|
|
||||||
/** Token names for each alt 0..n-1 */
|
|
||||||
public List<String[]> altLook;
|
|
||||||
|
|
||||||
public String loopLabel;
|
|
||||||
public String loopCounterVar;
|
|
||||||
public String[] exitLook;
|
|
||||||
|
|
||||||
@ModelElement public ThrowNoViableAlt error;
|
|
||||||
|
|
||||||
public LL1PlusBlock(OutputModelFactory factory, GrammarAST plusRoot, List<CodeBlockForAlt> alts) {
|
|
||||||
super(factory, plusRoot, alts);
|
|
||||||
|
|
||||||
PlusBlockStartState blkStart = (PlusBlockStartState)plusRoot.atnState;
|
|
||||||
|
|
||||||
stateNumber = blkStart.loopBackState.stateNumber;
|
|
||||||
blockStartStateNumber = blkStart.stateNumber;
|
|
||||||
|
|
||||||
this.decision = blkStart.decision;
|
|
||||||
Grammar g = factory.getGrammar();
|
|
||||||
CodeGenerator gen = factory.getGenerator();
|
|
||||||
/** Lookahead for each alt 1..n */
|
|
||||||
IntervalSet[] altLookSets = g.decisionLOOK.get(decision);
|
|
||||||
altLook = getAltLookaheadAsStringLists(altLookSets);
|
|
||||||
IntervalSet all = IntervalSet.or(altLookSets);
|
|
||||||
|
|
||||||
this.error = getThrowNoViableAlt(factory, plusRoot, all);
|
|
||||||
|
|
||||||
loopExpr = addCodeForLoopLookaheadTempVar(all);
|
|
||||||
|
|
||||||
loopLabel = gen.getTarget().getLoopLabel(plusRoot);
|
|
||||||
loopCounterVar = gen.getTarget().getLoopCounter(plusRoot);
|
|
||||||
|
|
||||||
IntervalSet exitLookSet = altLookSets[altLookSets.length-1];
|
|
||||||
this.exitLook = gen.getTarget().getTokenTypesAsTargetLabels(g,
|
|
||||||
exitLookSet.toArray());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,70 +0,0 @@
|
||||||
/*
|
|
||||||
* [The "BSD license"]
|
|
||||||
* Copyright (c) 2012 Terence Parr
|
|
||||||
* Copyright (c) 2012 Sam Harwell
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
import org.antlr.v4.codegen.OutputModelFactory;
|
|
||||||
import org.antlr.v4.runtime.atn.StarLoopEntryState;
|
|
||||||
import org.antlr.v4.runtime.misc.IntervalSet;
|
|
||||||
import org.antlr.v4.tool.ast.GrammarAST;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/** */
|
|
||||||
public class LL1StarBlock extends LL1Loop {
|
|
||||||
/** Token names for each alt 0..n-1 */
|
|
||||||
public List<String[]> altLook;
|
|
||||||
public String loopLabel;
|
|
||||||
public String[] exitLook;
|
|
||||||
|
|
||||||
public LL1StarBlock(OutputModelFactory factory, GrammarAST starRootAST, List<CodeBlockForAlt> alts) {
|
|
||||||
super(factory, starRootAST, alts);
|
|
||||||
|
|
||||||
StarLoopEntryState star = (StarLoopEntryState)starRootAST.atnState;
|
|
||||||
blockStartStateNumber =
|
|
||||||
starRootAST.atnState.transition(0).target.stateNumber;
|
|
||||||
|
|
||||||
loopBackStateNumber = star.loopBackState.stateNumber;
|
|
||||||
|
|
||||||
this.decision = star.decision;
|
|
||||||
|
|
||||||
/** Lookahead for each alt 1..n */
|
|
||||||
IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);
|
|
||||||
IntervalSet lastLook = altLookSets[altLookSets.length-1];
|
|
||||||
// remove last (exit) alt
|
|
||||||
altLookSets = Arrays.copyOf(altLookSets, altLookSets.length - 1);
|
|
||||||
altLook = getAltLookaheadAsStringLists(altLookSets);
|
|
||||||
loopLabel = factory.getGenerator().getTarget().getLoopLabel(starRootAST);
|
|
||||||
|
|
||||||
this.exitLook =
|
|
||||||
factory.getGenerator().getTarget().getTokenTypesAsTargetLabels(factory.getGrammar(), lastLook.toArray());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -33,7 +33,8 @@ package org.antlr.v4.test;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
/** Test parser execution.
|
/** Test parser execution.
|
||||||
*
|
*
|
||||||
|
@ -205,6 +206,22 @@ public class TestParserExec extends BaseTest {
|
||||||
assertEquals("abc\n", found);
|
assertEquals("abc\n", found);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void testLL1OptionalBlock() throws Exception {
|
||||||
|
String grammar =
|
||||||
|
"grammar T;\n" +
|
||||||
|
"a : (ID|{}INT)? {System.out.println($text);} ;\n" +
|
||||||
|
"ID : 'a'..'z'+ ;\n" +
|
||||||
|
"INT : '0'..'9'+ ;\n" +
|
||||||
|
"WS : (' '|'\\n') -> skip ;\n";
|
||||||
|
|
||||||
|
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a",
|
||||||
|
"", false);
|
||||||
|
assertEquals("\n", found);
|
||||||
|
found = execParser("T.g4", grammar, "TParser", "TLexer", "a",
|
||||||
|
"a", false);
|
||||||
|
assertEquals("a\n", found);
|
||||||
|
}
|
||||||
|
|
||||||
// force complex decision
|
// force complex decision
|
||||||
@Test public void testAorAStar() throws Exception {
|
@Test public void testAorAStar() throws Exception {
|
||||||
String grammar =
|
String grammar =
|
||||||
|
|
Loading…
Reference in New Issue