Skip to content

Commit

Permalink
moved from $self to $parser
Browse files Browse the repository at this point in the history
- also adjusted generated tests for the above
- also adjusted generated tests for recent changes in Python/JavaScript
targets (token types are nos class members like Java/C#)
  • Loading branch information
ericvergnaud committed Dec 2, 2014
1 parent a47ee4d commit 38c4e2b
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -711,13 +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_self(r) ::= "this"
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_self(r) ::= "this"
ThisRulePropertyRef_parser(r) ::= "this"

NonLocalAttrRef(s) ::= "((<s.ruleName; format=\"cap\">Context)getInvokingContext(<s.ruleIndex>)).<s.name>"
SetNonLocalAttr(s, rhsChunks) ::=
Expand Down
8 changes: 4 additions & 4 deletions tool/src/org/antlr/v4/codegen/ActionTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
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_self;
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_self;
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;
Expand Down Expand Up @@ -88,7 +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("self", ThisRulePropertyRef_self.class);
thisRulePropToModelMap.put("parser", ThisRulePropertyRef_parser.class);
}

public static final Map<String, Class<? extends RulePropertyRef>> rulePropToModelMap =
Expand All @@ -98,7 +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("self", RulePropertyRef_self.class);
rulePropToModelMap.put("parser", RulePropertyRef_parser.class);
}

public static final Map<String, Class<? extends TokenPropertyRef>> tokenPropToModelMap =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import org.antlr.v4.codegen.model.decl.StructDecl;

/** */
public class RulePropertyRef_self extends RulePropertyRef {
public RulePropertyRef_self(StructDecl ctx, String label) {
public class RulePropertyRef_parser extends RulePropertyRef {
public RulePropertyRef_parser(StructDecl ctx, String label) {
super(ctx, label);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import org.antlr.v4.codegen.model.decl.StructDecl;

/** */
public class ThisRulePropertyRef_self extends RulePropertyRef {
public ThisRulePropertyRef_self(StructDecl ctx, String label) {
public class ThisRulePropertyRef_parser extends RulePropertyRef {
public ThisRulePropertyRef_parser(StructDecl ctx, String label) {
super(ctx, label);
}
}
2 changes: 1 addition & 1 deletion tool/src/org/antlr/v4/tool/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class Rule implements AttributeResolver {
public static final AttributeDict predefinedRulePropertiesDict =
new AttributeDict(AttributeDict.DictType.PREDEFINED_RULE);
static {
predefinedRulePropertiesDict.add(new Attribute("self"));
predefinedRulePropertiesDict.add(new Attribute("parser"));
predefinedRulePropertiesDict.add(new Attribute("text"));
predefinedRulePropertiesDict.add(new Attribute("start"));
predefinedRulePropertiesDict.add(new Attribute("stop"));
Expand Down
1 change: 1 addition & 0 deletions tool/test/org/antlr/v4/test/rt/gen/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,7 @@ private JUnitTestFile buildParserExec() throws Exception {
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
grammar <grammarName>;
<ParserPropertyMember()>
a : {$parser.Property()}? ID {<writeln("\"valid\"")>}
;
ID : 'a'..'z'+ ;
WS : (' '|'\n') -> skip ;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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' ;
8 changes: 8 additions & 0 deletions tool/test/org/antlr/v4/test/rt/java/Java.test.stg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion tool/test/org/antlr/v4/test/rt/java/TestParserExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public void testPredicatedIfIfElse() throws Exception {
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;";
Expand Down Expand Up @@ -453,5 +453,23 @@ public void testReferenceToATN_2() throws Exception {
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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ String testPredFromAltTestedInLoopBack(String input) throws Exception {
"@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' ;";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import org.antlr.v4.test.rt.java.BaseTest;
import org.junit.Test;

public class TestDollarSelf extends BaseTest {
public class TestDollarParser extends BaseTest {

@Test
public void testSimpleCall() throws Exception {
String grammar = "grammar T;\n" +
"a : ID { System.out.println( $self.getSourceName() ); }\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);
Expand Down

0 comments on commit 38c4e2b

Please sign in to comment.