Merge pull request #770 from antlr/add-$parser-support

Add $parser support
This commit is contained in:
Terence Parr 2014-12-02 10:37:58 -08:00
commit 9275a0ecad
13 changed files with 151 additions and 10 deletions

View File

@ -711,11 +711,13 @@ RulePropertyRef_start(r) ::= "(<ctx(r)>.<r.label>!=null?(<ctx(r)>.<r.label>.star
RulePropertyRef_stop(r) ::= "(<ctx(r)>.<r.label>!=null?(<ctx(r)>.<r.label>.stop):null)"
RulePropertyRef_text(r) ::= "(<ctx(r)>.<r.label>!=null?_input.getText(<ctx(r)>.<r.label>.start,<ctx(r)>.<r.label>.stop):null)"
RulePropertyRef_ctx(r) ::= "<ctx(r)>.<r.label>"
RulePropertyRef_parser(r) ::= "this"
ThisRulePropertyRef_start(r) ::= "_localctx.start"
ThisRulePropertyRef_stop(r) ::= "_localctx.stop"
ThisRulePropertyRef_text(r) ::= "_input.getText(_localctx.start, _input.LT(-1))"
ThisRulePropertyRef_ctx(r) ::= "_localctx"
ThisRulePropertyRef_parser(r) ::= "this"
NonLocalAttrRef(s) ::= "((<s.ruleName; format=\"cap\">Context)getInvokingContext(<s.ruleIndex>)).<s.name>"
SetNonLocalAttr(s, rhsChunks) ::=

View File

@ -30,6 +30,12 @@
package org.antlr.v4.codegen;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.Token;
import org.antlr.v4.codegen.model.RuleFunction;
@ -44,12 +50,14 @@ import org.antlr.v4.codegen.model.chunk.QRetValueRef;
import org.antlr.v4.codegen.model.chunk.RetValueRef;
import org.antlr.v4.codegen.model.chunk.RulePropertyRef;
import org.antlr.v4.codegen.model.chunk.RulePropertyRef_ctx;
import org.antlr.v4.codegen.model.chunk.RulePropertyRef_parser;
import org.antlr.v4.codegen.model.chunk.RulePropertyRef_start;
import org.antlr.v4.codegen.model.chunk.RulePropertyRef_stop;
import org.antlr.v4.codegen.model.chunk.RulePropertyRef_text;
import org.antlr.v4.codegen.model.chunk.SetAttr;
import org.antlr.v4.codegen.model.chunk.SetNonLocalAttr;
import org.antlr.v4.codegen.model.chunk.ThisRulePropertyRef_ctx;
import org.antlr.v4.codegen.model.chunk.ThisRulePropertyRef_parser;
import org.antlr.v4.codegen.model.chunk.ThisRulePropertyRef_start;
import org.antlr.v4.codegen.model.chunk.ThisRulePropertyRef_stop;
import org.antlr.v4.codegen.model.chunk.ThisRulePropertyRef_text;
@ -71,12 +79,6 @@ import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.ast.ActionAST;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** */
public class ActionTranslator implements ActionSplitterListener {
public static final Map<String, Class<? extends RulePropertyRef>> thisRulePropToModelMap =
@ -86,6 +88,7 @@ public class ActionTranslator implements ActionSplitterListener {
thisRulePropToModelMap.put("stop", ThisRulePropertyRef_stop.class);
thisRulePropToModelMap.put("text", ThisRulePropertyRef_text.class);
thisRulePropToModelMap.put("ctx", ThisRulePropertyRef_ctx.class);
thisRulePropToModelMap.put("parser", ThisRulePropertyRef_parser.class);
}
public static final Map<String, Class<? extends RulePropertyRef>> rulePropToModelMap =
@ -95,6 +98,7 @@ public class ActionTranslator implements ActionSplitterListener {
rulePropToModelMap.put("stop", RulePropertyRef_stop.class);
rulePropToModelMap.put("text", RulePropertyRef_text.class);
rulePropToModelMap.put("ctx", RulePropertyRef_ctx.class);
rulePropToModelMap.put("parser", RulePropertyRef_parser.class);
}
public static final Map<String, Class<? extends TokenPropertyRef>> tokenPropToModelMap =

View File

@ -0,0 +1,40 @@
/*
* [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.chunk;
import org.antlr.v4.codegen.model.decl.StructDecl;
/** */
public class RulePropertyRef_parser extends RulePropertyRef {
public RulePropertyRef_parser(StructDecl ctx, String label) {
super(ctx, label);
}
}

View File

@ -0,0 +1,40 @@
/*
* [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.chunk;
import org.antlr.v4.codegen.model.decl.StructDecl;
/** */
public class ThisRulePropertyRef_parser extends RulePropertyRef {
public ThisRulePropertyRef_parser(StructDecl ctx, String label) {
super(ctx, label);
}
}

View File

@ -55,6 +55,7 @@ public class Rule implements AttributeResolver {
public static final AttributeDict predefinedRulePropertiesDict =
new AttributeDict(AttributeDict.DictType.PREDEFINED_RULE);
static {
predefinedRulePropertiesDict.add(new Attribute("parser"));
predefinedRulePropertiesDict.add(new Attribute("text"));
predefinedRulePropertiesDict.add(new Attribute("start"));
predefinedRulePropertiesDict.add(new Attribute("stop"));

View File

@ -1417,6 +1417,7 @@ public class Generator {
file.addParserTests(input, "ReferenceToATN", "T", "a",
"", "\n",
"a 34 c", "a34c\n");
file.addParserTest(input, "ParserProperty", "T", "a", "abc", "valid\n", null);
/*CompositeParserTestMethod tm = file.addCompositeParserTest(input, "AlternateQuotes", "ModeTagsParser", "file_", "", "", null, "ModeTagsLexer");
tm.slaveIsLexer = true;*/
return file;

View File

@ -0,0 +1,6 @@
grammar <grammarName>;
<ParserPropertyMember()>
a : {$parser.Property()}? ID {<writeln("\"valid\"")>}
;
ID : 'a'..'z'+ ;
WS : (' '|'\n') -> skip ;

View File

@ -1,7 +1,7 @@
grammar <grammarName>;
s : stmt EOF ;
stmt : ifStmt | ID;
ifStmt : 'if' ID stmt ('else' stmt | { <LANotEquals("1", "ELSE")> }?);
ifStmt : 'if' ID stmt ('else' stmt | { <LANotEquals("1", "TParser.ELSE")> }?);
ELSE : 'else';
ID : [a-zA-Z]+;
WS : [ \\n\\t]+ -> skip;

View File

@ -3,7 +3,7 @@ file_
@after {<ToStringTree("$ctx"):writeln()>}
: para para EOF ;
para: paraContent NL NL ;
paraContent : ('s'|'x'|{<LANotEquals("2","NL")>}? NL)+ ;
paraContent : ('s'|'x'|{<LANotEquals("2","TParser.NL")>}? NL)+ ;
NL : '\n' ;
s : 's' ;
X : 'x' ;

View File

@ -179,6 +179,14 @@ RuleInvocationStack() ::= "getRuleInvocationStack()"
LL_EXACT_AMBIG_DETECTION() ::= <<_interp.setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);>>
ParserPropertyMember() ::= <<
@members {
boolean Property() {
return true;
}
}
>>
PositionAdjustingLexer() ::= <<
@Override

View File

@ -332,7 +332,7 @@ public class TestParserExec extends BaseTest {
String grammar = "grammar T;\n" +
"s : stmt EOF ;\n" +
"stmt : ifStmt | ID;\n" +
"ifStmt : 'if' ID stmt ('else' stmt | { this._input.LA(1)!=ELSE }?);\n" +
"ifStmt : 'if' ID stmt ('else' stmt | { this._input.LA(1)!=TParser.ELSE }?);\n" +
"ELSE : 'else';\n" +
"ID : [a-zA-Z]+;\n" +
"WS : [ \\n\\t]+ -> skip;";
@ -453,5 +453,23 @@ public class TestParserExec extends BaseTest {
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testParserProperty() throws Exception {
String grammar = "grammar T;\n" +
"@members {\n" +
"boolean Property() {\n" +
" return true;\n" +
"}\n" +
"}\n" +
"a : {$parser.Property()}? ID {System.out.println(\"valid\");}\n" +
" ;\n" +
"ID : 'a'..'z'+ ;\n" +
"WS : (' '|'\\n') -> skip ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abc", false);
assertEquals("valid\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -427,7 +427,7 @@ public class TestSemPredEvalParser extends BaseTest {
"@after {System.out.println($ctx.toStringTree(this));}\n" +
" : para para EOF ;\n" +
"para: paraContent NL NL ;\n" +
"paraContent : ('s'|'x'|{this._input.LA(2)!=NL}? NL)+ ;\n" +
"paraContent : ('s'|'x'|{this._input.LA(2)!=TParser.NL}? NL)+ ;\n" +
"NL : '\\n' ;\n" +
"s : 's' ;\n" +
"X : 'x' ;";

View File

@ -0,0 +1,21 @@
package org.antlr.v4.test.tool;
import static org.junit.Assert.*;
import org.antlr.v4.test.rt.java.BaseTest;
import org.junit.Test;
public class TestDollarParser extends BaseTest {
@Test
public void testSimpleCall() throws Exception {
String grammar = "grammar T;\n" +
"a : ID { System.out.println( $parser.getSourceName() ); }\n" +
" ;\n" +
"ID : 'a'..'z'+ ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", true);
assertTrue(found.indexOf(this.getClass().getSimpleName())>=0);
assertNull(this.stderrDuringParse);
}
}