handled scope/arg errors; added tests

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6681]
This commit is contained in:
parrt 2010-02-10 11:40:22 -08:00
parent 56a0940360
commit 25be3b0172
12 changed files with 345 additions and 193 deletions

View File

@ -143,7 +143,7 @@ LABEL_CONFLICTS_WITH_RULE_ARG_RETVAL(arg,arg2) ::=
ATTRIBUTE_CONFLICTS_WITH_RULE(arg,arg2) ::=
"rule <arg2>'s dynamically-scoped attribute <arg> conflicts with the rule name"
ATTRIBUTE_CONFLICTS_WITH_RULE_ARG_RETVAL(arg,arg2) ::=
"rule <arg2>'s dynamically-scoped attribute <arg> conflicts with<arg2>'s return value or parameter with same name"
"rule <arg2>'s dynamically-scoped attribute <arg> conflicts with <arg2>'s return value or parameter with same name"
LABEL_TYPE_CONFLICT(arg,arg2) ::=
"label <arg> type mismatch with previous definition: <arg2>"
ARG_RETVAL_CONFLICT(arg,arg2) ::=

View File

@ -31,7 +31,15 @@ public class ScopeParser {
*/
public static AttributeScope parseTypeList(String s) { return parse(s, ','); }
public static AttributeScope parseDynamicScope(String s) { return parse(s, ';'); }
public static AttributeScope parseDynamicScope(String s) {
// ignore outer {..} if present
s = s.trim();
if ( s.startsWith("{") ) {
int lastCurly = s.lastIndexOf('}');
s = s.substring(1, lastCurly);
}
return parse(s, ';');
}
public static AttributeScope parse(String s, char separator) {
int i = 0;
@ -53,9 +61,12 @@ public class ScopeParser {
i++;
}
i++; // skip separator
// System.out.println("def="+buf.toString());
Attribute a = parseAttributeDef(buf.toString());
scope.attributes.put(a.name, a);
String def = buf.toString();
//System.out.println("def="+ def);
if ( def.trim().length()>0 ) {
Attribute a = parseAttributeDef(def);
scope.attributes.put(a.name, a);
}
}
return scope;
}

View File

@ -0,0 +1,5 @@
package org.antlr.v4.semantics;
/** Check attribute refs in actions */
public class AttributeChecks {
}

View File

@ -1,4 +1,4 @@
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 BasicSemanticTriggers.g 2010-02-08 17:03:10
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 BasicSemanticTriggers.g 2010-02-10 11:36:38
/*
[The "BSD license"]

View File

@ -65,6 +65,7 @@ options {
*/
package org.antlr.v4.semantics;
import org.antlr.v4.tool.*;
import org.antlr.v4.parse.*;
import java.util.Set;
import java.util.HashSet;
import org.stringtemplate.v4.misc.MultiMap;
@ -148,17 +149,29 @@ finishRule
;
ruleArg
: {inContext("RULE")}? ARG_ACTION {currentRule.arg = $ARG_ACTION;}
: {inContext("RULE")}? ARG_ACTION
{
currentRule.args = ScopeParser.parseTypeList($ARG_ACTION.text);
currentRule.args.ast = $ARG_ACTION;
}
;
ruleReturns
: ^(RETURNS ARG_ACTION) {currentRule.ret = $ARG_ACTION;}
: ^(RETURNS ARG_ACTION)
{
currentRule.retvals = ScopeParser.parseTypeList($ARG_ACTION.text);
currentRule.retvals.ast = $ARG_ACTION;
}
;
ruleScopeSpec
: {inContext("RULE")}?
( ^(SCOPE ACTION)
| ^(SCOPE ID+)
{
currentRule.scope = ScopeParser.parseDynamicScope($ACTION.text);
currentRule.scope.ast = $ACTION;
}
| ^(SCOPE ids+=ID+) {currentRule.useScopes = $ids;}
)
;

View File

@ -1,4 +1,4 @@
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 CollectSymbols.g 2010-02-08 17:57:03
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 CollectSymbols.g 2010-02-10 11:36:37
/*
[The "BSD license"]
@ -30,6 +30,7 @@ package org.antlr.v4.semantics;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.TreeNodeStream;
import org.antlr.runtime.tree.TreeRuleReturnScope;
import org.antlr.v4.parse.ScopeParser;
import org.antlr.v4.tool.*;
import java.util.ArrayList;
@ -177,15 +178,15 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "topdown"
// CollectSymbols.g:91:1: topdown : ( globalScope | action | tokensSection | rule | ruleArg | ruleReturns | ruleScopeSpec | ruleref | rewriteElement | terminal | labeledElement | setAlt );
// CollectSymbols.g:92:1: topdown : ( globalScope | action | tokensSection | rule | ruleArg | ruleReturns | ruleScopeSpec | ruleref | rewriteElement | terminal | labeledElement | setAlt );
public final void topdown() throws RecognitionException {
try {
// CollectSymbols.g:93:5: ( globalScope | action | tokensSection | rule | ruleArg | ruleReturns | ruleScopeSpec | ruleref | rewriteElement | terminal | labeledElement | setAlt )
// CollectSymbols.g:94:5: ( globalScope | action | tokensSection | rule | ruleArg | ruleReturns | ruleScopeSpec | ruleref | rewriteElement | terminal | labeledElement | setAlt )
int alt1=12;
alt1 = dfa1.predict(input);
switch (alt1) {
case 1 :
// CollectSymbols.g:93:7: globalScope
// CollectSymbols.g:94:7: globalScope
{
pushFollow(FOLLOW_globalScope_in_topdown97);
globalScope();
@ -196,7 +197,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 2 :
// CollectSymbols.g:94:7: action
// CollectSymbols.g:95:7: action
{
pushFollow(FOLLOW_action_in_topdown105);
action();
@ -207,7 +208,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 3 :
// CollectSymbols.g:95:7: tokensSection
// CollectSymbols.g:96:7: tokensSection
{
pushFollow(FOLLOW_tokensSection_in_topdown113);
tokensSection();
@ -218,7 +219,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 4 :
// CollectSymbols.g:96:7: rule
// CollectSymbols.g:97:7: rule
{
pushFollow(FOLLOW_rule_in_topdown121);
rule();
@ -229,7 +230,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 5 :
// CollectSymbols.g:97:7: ruleArg
// CollectSymbols.g:98:7: ruleArg
{
pushFollow(FOLLOW_ruleArg_in_topdown129);
ruleArg();
@ -240,7 +241,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 6 :
// CollectSymbols.g:98:7: ruleReturns
// CollectSymbols.g:99:7: ruleReturns
{
pushFollow(FOLLOW_ruleReturns_in_topdown137);
ruleReturns();
@ -251,7 +252,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 7 :
// CollectSymbols.g:99:7: ruleScopeSpec
// CollectSymbols.g:100:7: ruleScopeSpec
{
pushFollow(FOLLOW_ruleScopeSpec_in_topdown145);
ruleScopeSpec();
@ -262,7 +263,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 8 :
// CollectSymbols.g:100:7: ruleref
// CollectSymbols.g:101:7: ruleref
{
pushFollow(FOLLOW_ruleref_in_topdown153);
ruleref();
@ -273,7 +274,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 9 :
// CollectSymbols.g:101:7: rewriteElement
// CollectSymbols.g:102:7: rewriteElement
{
pushFollow(FOLLOW_rewriteElement_in_topdown161);
rewriteElement();
@ -284,7 +285,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 10 :
// CollectSymbols.g:103:7: terminal
// CollectSymbols.g:104:7: terminal
{
pushFollow(FOLLOW_terminal_in_topdown182);
terminal();
@ -295,7 +296,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 11 :
// CollectSymbols.g:104:7: labeledElement
// CollectSymbols.g:105:7: labeledElement
{
pushFollow(FOLLOW_labeledElement_in_topdown190);
labeledElement();
@ -306,7 +307,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 12 :
// CollectSymbols.g:105:7: setAlt
// CollectSymbols.g:106:7: setAlt
{
pushFollow(FOLLOW_setAlt_in_topdown198);
setAlt();
@ -331,11 +332,11 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "bottomup"
// CollectSymbols.g:108:1: bottomup : finishRule ;
// CollectSymbols.g:109:1: bottomup : finishRule ;
public final void bottomup() throws RecognitionException {
try {
// CollectSymbols.g:109:2: ( finishRule )
// CollectSymbols.g:109:4: finishRule
// CollectSymbols.g:110:2: ( finishRule )
// CollectSymbols.g:110:4: finishRule
{
pushFollow(FOLLOW_finishRule_in_bottomup209);
finishRule();
@ -358,13 +359,13 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "globalScope"
// CollectSymbols.g:112:1: globalScope : {...}? ^( SCOPE ID ACTION ) ;
// CollectSymbols.g:113:1: globalScope : {...}? ^( SCOPE ID ACTION ) ;
public final void globalScope() throws RecognitionException {
GrammarAST SCOPE1=null;
try {
// CollectSymbols.g:113:2: ({...}? ^( SCOPE ID ACTION ) )
// CollectSymbols.g:113:4: {...}? ^( SCOPE ID ACTION )
// CollectSymbols.g:114:2: ({...}? ^( SCOPE ID ACTION ) )
// CollectSymbols.g:114:4: {...}? ^( SCOPE ID ACTION )
{
if ( !((inContext("GRAMMAR"))) ) {
if (state.backtracking>0) {state.failed=true; return ;}
@ -396,13 +397,13 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "action"
// CollectSymbols.g:116:1: action : {...}? ^( AT ( ID )? ID ACTION ) ;
// CollectSymbols.g:117:1: action : {...}? ^( AT ( ID )? ID ACTION ) ;
public final void action() throws RecognitionException {
GrammarAST AT2=null;
try {
// CollectSymbols.g:117:2: ({...}? ^( AT ( ID )? ID ACTION ) )
// CollectSymbols.g:117:4: {...}? ^( AT ( ID )? ID ACTION )
// CollectSymbols.g:118:2: ({...}? ^( AT ( ID )? ID ACTION ) )
// CollectSymbols.g:118:4: {...}? ^( AT ( ID )? ID ACTION )
{
if ( !((inContext("GRAMMAR"))) ) {
if (state.backtracking>0) {state.failed=true; return ;}
@ -411,7 +412,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
AT2=(GrammarAST)match(input,AT,FOLLOW_AT_in_action244); if (state.failed) return ;
match(input, Token.DOWN, null); if (state.failed) return ;
// CollectSymbols.g:117:33: ( ID )?
// CollectSymbols.g:118:33: ( ID )?
int alt2=2;
int LA2_0 = input.LA(1);
@ -424,7 +425,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
switch (alt2) {
case 1 :
// CollectSymbols.g:117:33: ID
// CollectSymbols.g:118:33: ID
{
match(input,ID,FOLLOW_ID_in_action246); if (state.failed) return ;
@ -456,21 +457,21 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "tokensSection"
// CollectSymbols.g:121:1: tokensSection : {...}? ( ^( ASSIGN t= ID STRING_LITERAL ) | t= ID ) ;
// CollectSymbols.g:122:1: tokensSection : {...}? ( ^( ASSIGN t= ID STRING_LITERAL ) | t= ID ) ;
public final void tokensSection() throws RecognitionException {
GrammarAST t=null;
GrammarAST ASSIGN3=null;
GrammarAST STRING_LITERAL4=null;
try {
// CollectSymbols.g:122:2: ({...}? ( ^( ASSIGN t= ID STRING_LITERAL ) | t= ID ) )
// CollectSymbols.g:122:4: {...}? ( ^( ASSIGN t= ID STRING_LITERAL ) | t= ID )
// CollectSymbols.g:123:2: ({...}? ( ^( ASSIGN t= ID STRING_LITERAL ) | t= ID ) )
// CollectSymbols.g:123:4: {...}? ( ^( ASSIGN t= ID STRING_LITERAL ) | t= ID )
{
if ( !((inContext("TOKENS"))) ) {
if (state.backtracking>0) {state.failed=true; return ;}
throw new FailedPredicateException(input, "tokensSection", "inContext(\"TOKENS\")");
}
// CollectSymbols.g:123:3: ( ^( ASSIGN t= ID STRING_LITERAL ) | t= ID )
// CollectSymbols.g:124:3: ( ^( ASSIGN t= ID STRING_LITERAL ) | t= ID )
int alt3=2;
int LA3_0 = input.LA(1);
@ -489,7 +490,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
switch (alt3) {
case 1 :
// CollectSymbols.g:123:5: ^( ASSIGN t= ID STRING_LITERAL )
// CollectSymbols.g:124:5: ^( ASSIGN t= ID STRING_LITERAL )
{
ASSIGN3=(GrammarAST)match(input,ASSIGN,FOLLOW_ASSIGN_in_tokensSection274); if (state.failed) return ;
@ -506,7 +507,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 2 :
// CollectSymbols.g:126:5: t= ID
// CollectSymbols.g:127:5: t= ID
{
t=(GrammarAST)match(input,ID,FOLLOW_ID_in_tokensSection294); if (state.failed) return ;
if ( state.backtracking==1 ) {
@ -534,20 +535,20 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "rule"
// CollectSymbols.g:131:1: rule : ^( RULE name= ID ( . )+ ) ;
// CollectSymbols.g:132:1: rule : ^( RULE name= ID ( . )+ ) ;
public final void rule() throws RecognitionException {
GrammarAST name=null;
GrammarAST RULE5=null;
try {
// CollectSymbols.g:131:5: ( ^( RULE name= ID ( . )+ ) )
// CollectSymbols.g:131:9: ^( RULE name= ID ( . )+ )
// CollectSymbols.g:132:5: ( ^( RULE name= ID ( . )+ ) )
// CollectSymbols.g:132:9: ^( RULE name= ID ( . )+ )
{
RULE5=(GrammarAST)match(input,RULE,FOLLOW_RULE_in_rule316); if (state.failed) return ;
match(input, Token.DOWN, null); if (state.failed) return ;
name=(GrammarAST)match(input,ID,FOLLOW_ID_in_rule320); if (state.failed) return ;
// CollectSymbols.g:131:25: ( . )+
// CollectSymbols.g:132:25: ( . )+
int cnt4=0;
loop4:
do {
@ -564,7 +565,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
switch (alt4) {
case 1 :
// CollectSymbols.g:131:25: .
// CollectSymbols.g:132:25: .
{
matchAny(input); if (state.failed) return ;
@ -610,14 +611,14 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
};
// $ANTLR start "setAlt"
// CollectSymbols.g:141:1: setAlt : {...}? ( ALT | ALT_REWRITE ) ;
// CollectSymbols.g:142:1: setAlt : {...}? ( ALT | ALT_REWRITE ) ;
public final CollectSymbols.setAlt_return setAlt() throws RecognitionException {
CollectSymbols.setAlt_return retval = new CollectSymbols.setAlt_return();
retval.start = input.LT(1);
try {
// CollectSymbols.g:142:2: ({...}? ( ALT | ALT_REWRITE ) )
// CollectSymbols.g:142:4: {...}? ( ALT | ALT_REWRITE )
// CollectSymbols.g:143:2: ({...}? ( ALT | ALT_REWRITE ) )
// CollectSymbols.g:143:4: {...}? ( ALT | ALT_REWRITE )
{
if ( !((inContext("RULE BLOCK"))) ) {
if (state.backtracking>0) {state.failed=true; return retval;}
@ -652,11 +653,11 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "finishRule"
// CollectSymbols.g:146:1: finishRule : RULE ;
// CollectSymbols.g:147:1: finishRule : RULE ;
public final void finishRule() throws RecognitionException {
try {
// CollectSymbols.g:147:2: ( RULE )
// CollectSymbols.g:147:4: RULE
// CollectSymbols.g:148:2: ( RULE )
// CollectSymbols.g:148:4: RULE
{
match(input,RULE,FOLLOW_RULE_in_finishRule368); if (state.failed) return ;
if ( state.backtracking==1 ) {
@ -678,13 +679,13 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "ruleArg"
// CollectSymbols.g:150:1: ruleArg : {...}? ARG_ACTION ;
// CollectSymbols.g:151:1: ruleArg : {...}? ARG_ACTION ;
public final void ruleArg() throws RecognitionException {
GrammarAST ARG_ACTION6=null;
try {
// CollectSymbols.g:151:2: ({...}? ARG_ACTION )
// CollectSymbols.g:151:4: {...}? ARG_ACTION
// CollectSymbols.g:152:2: ({...}? ARG_ACTION )
// CollectSymbols.g:152:4: {...}? ARG_ACTION
{
if ( !((inContext("RULE"))) ) {
if (state.backtracking>0) {state.failed=true; return ;}
@ -692,7 +693,10 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
ARG_ACTION6=(GrammarAST)match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_ruleArg383); if (state.failed) return ;
if ( state.backtracking==1 ) {
currentRule.arg = ARG_ACTION6;
currentRule.args = ScopeParser.parseTypeList((ARG_ACTION6!=null?ARG_ACTION6.getText():null));
currentRule.args.ast = ARG_ACTION6;
}
}
@ -710,22 +714,25 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "ruleReturns"
// CollectSymbols.g:154:1: ruleReturns : ^( RETURNS ARG_ACTION ) ;
// CollectSymbols.g:159:1: ruleReturns : ^( RETURNS ARG_ACTION ) ;
public final void ruleReturns() throws RecognitionException {
GrammarAST ARG_ACTION7=null;
try {
// CollectSymbols.g:155:2: ( ^( RETURNS ARG_ACTION ) )
// CollectSymbols.g:155:4: ^( RETURNS ARG_ACTION )
// CollectSymbols.g:160:2: ( ^( RETURNS ARG_ACTION ) )
// CollectSymbols.g:160:4: ^( RETURNS ARG_ACTION )
{
match(input,RETURNS,FOLLOW_RETURNS_in_ruleReturns398); if (state.failed) return ;
match(input,RETURNS,FOLLOW_RETURNS_in_ruleReturns400); if (state.failed) return ;
match(input, Token.DOWN, null); if (state.failed) return ;
ARG_ACTION7=(GrammarAST)match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_ruleReturns400); if (state.failed) return ;
ARG_ACTION7=(GrammarAST)match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_ruleReturns402); if (state.failed) return ;
match(input, Token.UP, null); if (state.failed) return ;
if ( state.backtracking==1 ) {
currentRule.ret = ARG_ACTION7;
currentRule.retvals = ScopeParser.parseTypeList((ARG_ACTION7!=null?ARG_ACTION7.getText():null));
currentRule.retvals.ast = ARG_ACTION7;
}
}
@ -743,17 +750,21 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "ruleScopeSpec"
// CollectSymbols.g:158:1: ruleScopeSpec : {...}? ( ^( SCOPE ACTION ) | ^( SCOPE ( ID )+ ) ) ;
// CollectSymbols.g:167:1: ruleScopeSpec : {...}? ( ^( SCOPE ACTION ) | ^( SCOPE (ids+= ID )+ ) ) ;
public final void ruleScopeSpec() throws RecognitionException {
GrammarAST ACTION8=null;
GrammarAST ids=null;
List list_ids=null;
try {
// CollectSymbols.g:159:2: ({...}? ( ^( SCOPE ACTION ) | ^( SCOPE ( ID )+ ) ) )
// CollectSymbols.g:159:4: {...}? ( ^( SCOPE ACTION ) | ^( SCOPE ( ID )+ ) )
// CollectSymbols.g:168:2: ({...}? ( ^( SCOPE ACTION ) | ^( SCOPE (ids+= ID )+ ) ) )
// CollectSymbols.g:168:4: {...}? ( ^( SCOPE ACTION ) | ^( SCOPE (ids+= ID )+ ) )
{
if ( !((inContext("RULE"))) ) {
if (state.backtracking>0) {state.failed=true; return ;}
throw new FailedPredicateException(input, "ruleScopeSpec", "inContext(\"RULE\")");
}
// CollectSymbols.g:160:3: ( ^( SCOPE ACTION ) | ^( SCOPE ( ID )+ ) )
// CollectSymbols.g:169:3: ( ^( SCOPE ACTION ) | ^( SCOPE (ids+= ID )+ ) )
int alt6=2;
int LA6_0 = input.LA(1);
@ -794,24 +805,30 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
switch (alt6) {
case 1 :
// CollectSymbols.g:160:5: ^( SCOPE ACTION )
// CollectSymbols.g:169:5: ^( SCOPE ACTION )
{
match(input,SCOPE,FOLLOW_SCOPE_in_ruleScopeSpec421); if (state.failed) return ;
match(input,SCOPE,FOLLOW_SCOPE_in_ruleScopeSpec425); if (state.failed) return ;
match(input, Token.DOWN, null); if (state.failed) return ;
match(input,ACTION,FOLLOW_ACTION_in_ruleScopeSpec423); if (state.failed) return ;
ACTION8=(GrammarAST)match(input,ACTION,FOLLOW_ACTION_in_ruleScopeSpec427); if (state.failed) return ;
match(input, Token.UP, null); if (state.failed) return ;
if ( state.backtracking==1 ) {
currentRule.scope = ScopeParser.parseDynamicScope((ACTION8!=null?ACTION8.getText():null));
currentRule.scope.ast = ACTION8;
}
}
break;
case 2 :
// CollectSymbols.g:161:5: ^( SCOPE ( ID )+ )
// CollectSymbols.g:174:5: ^( SCOPE (ids+= ID )+ )
{
match(input,SCOPE,FOLLOW_SCOPE_in_ruleScopeSpec431); if (state.failed) return ;
match(input,SCOPE,FOLLOW_SCOPE_in_ruleScopeSpec440); if (state.failed) return ;
match(input, Token.DOWN, null); if (state.failed) return ;
// CollectSymbols.g:161:13: ( ID )+
// CollectSymbols.g:174:16: (ids+= ID )+
int cnt5=0;
loop5:
do {
@ -825,9 +842,12 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
switch (alt5) {
case 1 :
// CollectSymbols.g:161:13: ID
// CollectSymbols.g:174:16: ids+= ID
{
match(input,ID,FOLLOW_ID_in_ruleScopeSpec433); if (state.failed) return ;
ids=(GrammarAST)match(input,ID,FOLLOW_ID_in_ruleScopeSpec444); if (state.failed) return ;
if (list_ids==null) list_ids=new ArrayList();
list_ids.add(ids);
}
break;
@ -844,6 +864,9 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
match(input, Token.UP, null); if (state.failed) return ;
if ( state.backtracking==1 ) {
currentRule.useScopes = list_ids;
}
}
break;
@ -868,14 +891,14 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
};
// $ANTLR start "rewriteElement"
// CollectSymbols.g:165:1: rewriteElement : {...}? ( TOKEN_REF | RULE_REF | STRING_LITERAL | LABEL ) ;
// CollectSymbols.g:178:1: rewriteElement : {...}? ( TOKEN_REF | RULE_REF | STRING_LITERAL | LABEL ) ;
public final CollectSymbols.rewriteElement_return rewriteElement() throws RecognitionException {
CollectSymbols.rewriteElement_return retval = new CollectSymbols.rewriteElement_return();
retval.start = input.LT(1);
try {
// CollectSymbols.g:167:2: ({...}? ( TOKEN_REF | RULE_REF | STRING_LITERAL | LABEL ) )
// CollectSymbols.g:168:6: {...}? ( TOKEN_REF | RULE_REF | STRING_LITERAL | LABEL )
// CollectSymbols.g:180:2: ({...}? ( TOKEN_REF | RULE_REF | STRING_LITERAL | LABEL ) )
// CollectSymbols.g:181:6: {...}? ( TOKEN_REF | RULE_REF | STRING_LITERAL | LABEL )
{
if ( !((inContext("RESULT ..."))) ) {
if (state.backtracking>0) {state.failed=true; return retval;}
@ -912,7 +935,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
};
// $ANTLR start "labeledElement"
// CollectSymbols.g:172:1: labeledElement : {...}? ( ^( ASSIGN id= ID e= . ) | ^( PLUS_ASSIGN id= ID e= . ) ) ;
// CollectSymbols.g:185:1: labeledElement : {...}? ( ^( ASSIGN id= ID e= . ) | ^( PLUS_ASSIGN id= ID e= . ) ) ;
public final CollectSymbols.labeledElement_return labeledElement() throws RecognitionException {
CollectSymbols.labeledElement_return retval = new CollectSymbols.labeledElement_return();
retval.start = input.LT(1);
@ -921,14 +944,14 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
GrammarAST e=null;
try {
// CollectSymbols.g:178:2: ({...}? ( ^( ASSIGN id= ID e= . ) | ^( PLUS_ASSIGN id= ID e= . ) ) )
// CollectSymbols.g:178:4: {...}? ( ^( ASSIGN id= ID e= . ) | ^( PLUS_ASSIGN id= ID e= . ) )
// CollectSymbols.g:191:2: ({...}? ( ^( ASSIGN id= ID e= . ) | ^( PLUS_ASSIGN id= ID e= . ) ) )
// CollectSymbols.g:191:4: {...}? ( ^( ASSIGN id= ID e= . ) | ^( PLUS_ASSIGN id= ID e= . ) )
{
if ( !((inContext("RULE ..."))) ) {
if (state.backtracking>0) {state.failed=true; return retval;}
throw new FailedPredicateException(input, "labeledElement", "inContext(\"RULE ...\")");
}
// CollectSymbols.g:179:3: ( ^( ASSIGN id= ID e= . ) | ^( PLUS_ASSIGN id= ID e= . ) )
// CollectSymbols.g:192:3: ( ^( ASSIGN id= ID e= . ) | ^( PLUS_ASSIGN id= ID e= . ) )
int alt7=2;
int LA7_0 = input.LA(1);
@ -947,12 +970,12 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
switch (alt7) {
case 1 :
// CollectSymbols.g:179:5: ^( ASSIGN id= ID e= . )
// CollectSymbols.g:192:5: ^( ASSIGN id= ID e= . )
{
match(input,ASSIGN,FOLLOW_ASSIGN_in_labeledElement495); if (state.failed) return retval;
match(input,ASSIGN,FOLLOW_ASSIGN_in_labeledElement508); if (state.failed) return retval;
match(input, Token.DOWN, null); if (state.failed) return retval;
id=(GrammarAST)match(input,ID,FOLLOW_ID_in_labeledElement499); if (state.failed) return retval;
id=(GrammarAST)match(input,ID,FOLLOW_ID_in_labeledElement512); if (state.failed) return retval;
e=(GrammarAST)input.LT(1);
matchAny(input); if (state.failed) return retval;
@ -961,12 +984,12 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 2 :
// CollectSymbols.g:180:5: ^( PLUS_ASSIGN id= ID e= . )
// CollectSymbols.g:193:5: ^( PLUS_ASSIGN id= ID e= . )
{
match(input,PLUS_ASSIGN,FOLLOW_PLUS_ASSIGN_in_labeledElement511); if (state.failed) return retval;
match(input,PLUS_ASSIGN,FOLLOW_PLUS_ASSIGN_in_labeledElement524); if (state.failed) return retval;
match(input, Token.DOWN, null); if (state.failed) return retval;
id=(GrammarAST)match(input,ID,FOLLOW_ID_in_labeledElement515); if (state.failed) return retval;
id=(GrammarAST)match(input,ID,FOLLOW_ID_in_labeledElement528); if (state.failed) return retval;
e=(GrammarAST)input.LT(1);
matchAny(input); if (state.failed) return retval;
@ -1002,16 +1025,16 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
};
// $ANTLR start "terminal"
// CollectSymbols.g:184:1: terminal : ({...}? STRING_LITERAL | TOKEN_REF );
// CollectSymbols.g:197:1: terminal : ({...}? STRING_LITERAL | TOKEN_REF );
public final CollectSymbols.terminal_return terminal() throws RecognitionException {
CollectSymbols.terminal_return retval = new CollectSymbols.terminal_return();
retval.start = input.LT(1);
GrammarAST STRING_LITERAL8=null;
GrammarAST TOKEN_REF9=null;
GrammarAST STRING_LITERAL9=null;
GrammarAST TOKEN_REF10=null;
try {
// CollectSymbols.g:185:5: ({...}? STRING_LITERAL | TOKEN_REF )
// CollectSymbols.g:198:5: ({...}? STRING_LITERAL | TOKEN_REF )
int alt8=2;
int LA8_0 = input.LA(1);
@ -1030,19 +1053,19 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
switch (alt8) {
case 1 :
// CollectSymbols.g:185:7: {...}? STRING_LITERAL
// CollectSymbols.g:198:7: {...}? STRING_LITERAL
{
if ( !((!inContext("TOKENS ASSIGN"))) ) {
if (state.backtracking>0) {state.failed=true; return retval;}
throw new FailedPredicateException(input, "terminal", "!inContext(\"TOKENS ASSIGN\")");
}
STRING_LITERAL8=(GrammarAST)match(input,STRING_LITERAL,FOLLOW_STRING_LITERAL_in_terminal541); if (state.failed) return retval;
STRING_LITERAL9=(GrammarAST)match(input,STRING_LITERAL,FOLLOW_STRING_LITERAL_in_terminal554); if (state.failed) return retval;
if ( state.backtracking==1 ) {
terminals.add(((GrammarAST)retval.start));
strings.add(STRING_LITERAL8);
strings.add(STRING_LITERAL9);
if ( currentRule!=null ) {
currentRule.alt[currentAlt].tokenRefs.map((STRING_LITERAL8!=null?STRING_LITERAL8.getText():null), STRING_LITERAL8);
currentRule.alt[currentAlt].tokenRefs.map((STRING_LITERAL9!=null?STRING_LITERAL9.getText():null), STRING_LITERAL9);
}
}
@ -1050,15 +1073,15 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
}
break;
case 2 :
// CollectSymbols.g:193:7: TOKEN_REF
// CollectSymbols.g:206:7: TOKEN_REF
{
TOKEN_REF9=(GrammarAST)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_terminal556); if (state.failed) return retval;
TOKEN_REF10=(GrammarAST)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_terminal569); if (state.failed) return retval;
if ( state.backtracking==1 ) {
terminals.add(TOKEN_REF9);
tokenIDRefs.add(TOKEN_REF9);
terminals.add(TOKEN_REF10);
tokenIDRefs.add(TOKEN_REF10);
if ( currentRule!=null ) {
currentRule.alt[currentAlt].tokenRefs.map((TOKEN_REF9!=null?TOKEN_REF9.getText():null), TOKEN_REF9);
currentRule.alt[currentAlt].tokenRefs.map((TOKEN_REF10!=null?TOKEN_REF10.getText():null), TOKEN_REF10);
}
}
@ -1080,20 +1103,20 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
// $ANTLR start "ruleref"
// CollectSymbols.g:203:1: ruleref : RULE_REF ;
// CollectSymbols.g:216:1: ruleref : RULE_REF ;
public final void ruleref() throws RecognitionException {
GrammarAST RULE_REF10=null;
GrammarAST RULE_REF11=null;
try {
// CollectSymbols.g:205:5: ( RULE_REF )
// CollectSymbols.g:205:7: RULE_REF
// CollectSymbols.g:218:5: ( RULE_REF )
// CollectSymbols.g:218:7: RULE_REF
{
RULE_REF10=(GrammarAST)match(input,RULE_REF,FOLLOW_RULE_REF_in_ruleref581); if (state.failed) return ;
RULE_REF11=(GrammarAST)match(input,RULE_REF,FOLLOW_RULE_REF_in_ruleref594); if (state.failed) return ;
if ( state.backtracking==1 ) {
rulerefs.add(RULE_REF10);
rulerefs.add(RULE_REF11);
if ( currentRule!=null ) {
currentRule.alt[currentAlt].ruleRefs.map((RULE_REF10!=null?RULE_REF10.getText():null), RULE_REF10);
currentRule.alt[currentAlt].ruleRefs.map((RULE_REF11!=null?RULE_REF11.getText():null), RULE_REF11);
}
}
@ -1129,7 +1152,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
"\2\uffff\1\2\1\uffff\1\3\1\4\1\5\1\6\3\uffff\1\11\1\13\1\14\2\uffff"+
"\1\10\1\12\1\uffff\1\7\1\uffff\1\1\2\uffff";
static final String DFA1_specialS =
"\10\uffff\1\2\1\1\1\0\14\uffff\1\3}>";
"\10\uffff\1\0\1\1\1\3\14\uffff\1\2}>";
static final String[] DFA1_transitionS = {
"\1\6\6\uffff\1\1\11\uffff\1\7\15\uffff\1\3\4\uffff\1\14\10\uffff"+
"\1\2\2\uffff\1\12\1\10\3\uffff\1\11\4\uffff\1\5\13\uffff\1\15"+
@ -1189,25 +1212,25 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
this.transition = DFA1_transition;
}
public String getDescription() {
return "91:1: topdown : ( globalScope | action | tokensSection | rule | ruleArg | ruleReturns | ruleScopeSpec | ruleref | rewriteElement | terminal | labeledElement | setAlt );";
return "92:1: topdown : ( globalScope | action | tokensSection | rule | ruleArg | ruleReturns | ruleScopeSpec | ruleref | rewriteElement | terminal | labeledElement | setAlt );";
}
public int specialStateTransition(int s, IntStream _input) throws NoViableAltException {
TreeNodeStream input = (TreeNodeStream)_input;
int _s = s;
switch ( s ) {
case 0 :
int LA1_10 = input.LA(1);
int LA1_8 = input.LA(1);
int index1_10 = input.index();
int index1_8 = input.index();
input.rewind();
s = -1;
if ( ((inContext("RESULT ..."))) ) {s = 11;}
if ( (!(((inContext("RESULT ..."))))) ) {s = 16;}
else if ( (true) ) {s = 17;}
else if ( ((inContext("RESULT ..."))) ) {s = 11;}
input.seek(index1_10);
input.seek(index1_8);
if ( s>=0 ) return s;
break;
case 1 :
@ -1226,21 +1249,6 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
if ( s>=0 ) return s;
break;
case 2 :
int LA1_8 = input.LA(1);
int index1_8 = input.index();
input.rewind();
s = -1;
if ( (!(((inContext("RESULT ..."))))) ) {s = 16;}
else if ( ((inContext("RESULT ..."))) ) {s = 11;}
input.seek(index1_8);
if ( s>=0 ) return s;
break;
case 3 :
int LA1_23 = input.LA(1);
@ -1255,6 +1263,21 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
input.seek(index1_23);
if ( s>=0 ) return s;
break;
case 3 :
int LA1_10 = input.LA(1);
int index1_10 = input.index();
input.rewind();
s = -1;
if ( ((inContext("RESULT ..."))) ) {s = 11;}
else if ( (true) ) {s = 17;}
input.seek(index1_10);
if ( s>=0 ) return s;
break;
}
if (state.backtracking>0) {state.failed=true; return -1;}
NoViableAltException nvae =
@ -1294,19 +1317,19 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter {
public static final BitSet FOLLOW_set_in_setAlt344 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_RULE_in_finishRule368 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_ARG_ACTION_in_ruleArg383 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_RETURNS_in_ruleReturns398 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ARG_ACTION_in_ruleReturns400 = new BitSet(new long[]{0x0000000000000008L});
public static final BitSet FOLLOW_SCOPE_in_ruleScopeSpec421 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ACTION_in_ruleScopeSpec423 = new BitSet(new long[]{0x0000000000000008L});
public static final BitSet FOLLOW_SCOPE_in_ruleScopeSpec431 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ID_in_ruleScopeSpec433 = new BitSet(new long[]{0x0000000000000008L,0x0000000000800000L});
public static final BitSet FOLLOW_set_in_rewriteElement459 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_ASSIGN_in_labeledElement495 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ID_in_labeledElement499 = new BitSet(new long[]{0xFFFFFFFFFFFFFFF0L,0x0000003FFFFFFFFFL});
public static final BitSet FOLLOW_PLUS_ASSIGN_in_labeledElement511 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ID_in_labeledElement515 = new BitSet(new long[]{0xFFFFFFFFFFFFFFF0L,0x0000003FFFFFFFFFL});
public static final BitSet FOLLOW_STRING_LITERAL_in_terminal541 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_TOKEN_REF_in_terminal556 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_RULE_REF_in_ruleref581 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_RETURNS_in_ruleReturns400 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ARG_ACTION_in_ruleReturns402 = new BitSet(new long[]{0x0000000000000008L});
public static final BitSet FOLLOW_SCOPE_in_ruleScopeSpec425 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ACTION_in_ruleScopeSpec427 = new BitSet(new long[]{0x0000000000000008L});
public static final BitSet FOLLOW_SCOPE_in_ruleScopeSpec440 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ID_in_ruleScopeSpec444 = new BitSet(new long[]{0x0000000000000008L,0x0000000000800000L});
public static final BitSet FOLLOW_set_in_rewriteElement472 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_ASSIGN_in_labeledElement508 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ID_in_labeledElement512 = new BitSet(new long[]{0xFFFFFFFFFFFFFFF0L,0x0000003FFFFFFFFFL});
public static final BitSet FOLLOW_PLUS_ASSIGN_in_labeledElement524 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ID_in_labeledElement528 = new BitSet(new long[]{0xFFFFFFFFFFFFFFF0L,0x0000003FFFFFFFFFL});
public static final BitSet FOLLOW_STRING_LITERAL_in_terminal554 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_TOKEN_REF_in_terminal569 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_RULE_REF_in_ruleref594 = new BitSet(new long[]{0x0000000000000002L});
}

View File

@ -125,12 +125,12 @@ public class SymbolChecks {
g.fileName, ref.token, ruleName);
}
GrammarAST arg = (GrammarAST)ref.getChild(0);
if ( arg!=null && r.arg==null ) {
if ( arg!=null && r.args==null ) {
ErrorManager.grammarError(ErrorType.RULE_HAS_NO_ARGS,
g.fileName, ref.token, ruleName);
}
else if ( arg==null && (r!=null&&r.arg!=null) ) {
else if ( arg==null && (r!=null&&r.args!=null) ) {
ErrorManager.grammarError(ErrorType.MISSING_RULE_ARGS,
g.fileName, ref.token, ruleName);
}
@ -174,6 +174,8 @@ public class SymbolChecks {
*/
public void checkForLabelConflicts(List<Rule> rules) {
for (Rule r : rules) {
checkForRuleArgumentAndReturnValueConflicts(r);
checkForRuleScopeAttributeConflict(r);
Map<String, LabelElementPair> labelNameSpace =
new HashMap<String, LabelElementPair>();
for (List<LabelElementPair> pairs : r.labelDefs.values() ) {
@ -216,22 +218,65 @@ public class SymbolChecks {
else if ( tokenIDs.contains(name) ) {
etype = ErrorType.LABEL_CONFLICTS_WITH_TOKEN;
}
// else if ( r.ruleScope!=null && r.ruleScope.getAttribute(label.getText())!=null ) {
// etype = ErrorType.LABEL_CONFLICTS_WITH_RULE_SCOPE_ATTRIBUTE;
// arg2 = r.name;
// }
// else if ( (r.returnScope!=null&&r.returnScope.getAttribute(label.getText())!=null) ||
// (r.parameterScope!=null&&r.parameterScope.getAttribute(label.getText())!=null) )
// {
// etype = ErrorType.LABEL_CONFLICTS_WITH_RULE_ARG_RETVAL;
// arg2 = r.name;
// }
else if ( r.scope!=null && r.scope.get(name)!=null ) {
etype = ErrorType.LABEL_CONFLICTS_WITH_RULE_SCOPE_ATTRIBUTE;
arg2 = r.name;
}
else if ( (r.retvals!=null&&r.retvals.get(name)!=null) ||
(r.args!=null&&r.args.get(name)!=null) )
{
etype = ErrorType.LABEL_CONFLICTS_WITH_RULE_ARG_RETVAL;
arg2 = r.name;
}
if ( etype!=ErrorType.INVALID ) {
ErrorManager.grammarError(etype,g.fileName,labelID.token,name,arg2);
}
}
public void checkForRuleArgumentAndReturnValueConflicts(Rule r) {
if ( r.retvals!=null ) {
Set conflictingKeys = r.retvals.intersection(r.args);
if (conflictingKeys!=null) {
for (Iterator it = conflictingKeys.iterator(); it.hasNext();) {
String key = (String) it.next();
ErrorManager.grammarError(
ErrorType.ARG_RETVAL_CONFLICT,
g.fileName,
((GrammarAST)r.ast.getChild(0)).token,
key,
r.name);
}
}
}
}
/** Check for collision of a rule-scope dynamic attribute with:
* arg, return value, rule name itself. Labels are checked elsewhere.
*/
public void checkForRuleScopeAttributeConflict(Rule r) {
if ( r.scope==null ) return;
for (Attribute a : r.scope.attributes.values()) {
ErrorType msgID = ErrorType.INVALID;
Object arg2 = null;
String attrName = a.name;
if ( r.name.equals(attrName) ) {
msgID = ErrorType.ATTRIBUTE_CONFLICTS_WITH_RULE;
arg2 = r.name;
}
else if ( (r.retvals!=null&&r.retvals.get(attrName)!=null) ||
(r.args!=null&&r.args.get(attrName)!=null) )
{
msgID = ErrorType.ATTRIBUTE_CONFLICTS_WITH_RULE_ARG_RETVAL;
arg2 = r.name;
}
if ( msgID!=ErrorType.INVALID ) {
ErrorManager.grammarError(msgID,g.fileName,
r.scope.ast.token,
attrName,arg2);
}
}
}
public void checkRewriteElementsPresentOnLeftSide(List<Rule> rules) {
for (Rule r : rules) {
for (int a=1; a<=r.numberOfAlts; a++) {

View File

@ -1,6 +1,9 @@
package org.antlr.v4.tool;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
/** Track the attributes within a scope. A named scoped has just its list
* of attributes. Each rule has potentially 3 scopes: return values,
@ -10,7 +13,8 @@ import java.util.LinkedHashMap;
*/
public class AttributeScope {
/** The scope name */
private String name;
protected String name;
public GrammarAST ast;
public static enum Type {
ARG, RET, TOKEN, PREDEFINED_RULE, PREDEFINED_LEXER_RULE,
@ -23,6 +27,8 @@ public class AttributeScope {
public LinkedHashMap<String, Attribute> attributes =
new LinkedHashMap<String, Attribute>();
public Attribute get(String name) { return attributes.get(name); }
public String getName() {
// if ( isParameterScope ) {
// return name+"_parameter";
@ -32,6 +38,29 @@ public class AttributeScope {
// }
return name;
}
public int size() { return attributes==null?0:attributes.size(); }
/** Return the set of keys that collide from
* this and other.
*/
public Set intersection(AttributeScope other) {
if ( other==null || other.size()==0 || size()==0 ) {
return null;
}
Set inter = new HashSet();
Set thisKeys = attributes.keySet();
for (Iterator it = thisKeys.iterator(); it.hasNext();) {
String key = (String) it.next();
if ( other.attributes.get(key)!=null ) {
inter.add(key);
}
}
if ( inter.size()==0 ) {
return null;
}
return inter;
}
public String toString() {
return getName()+":"+attributes;

View File

@ -69,7 +69,6 @@ public enum ErrorType {
LEXER_RULES_NOT_ALLOWED(ErrorSeverity.ERROR, true, true),
PARSER_RULES_NOT_ALLOWED(ErrorSeverity.ERROR, true, true),
REPEATED_PREQUEL(ErrorSeverity.ERROR, true, true),
CANNOT_FIND_ATTRIBUTE_NAME_IN_DECL(ErrorSeverity.ERROR, true, true),
NO_TOKEN_DEFINITION(ErrorSeverity.ERROR, true, true),
UNDEFINED_RULE_REF(ErrorSeverity.ERROR, true, true),
LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE(ErrorSeverity.ERROR, true, true),

View File

@ -1,12 +1,19 @@
package org.antlr.v4.tool;
import org.antlr.runtime.Token;
import org.stringtemplate.v4.misc.MultiMap;
import java.util.List;
public class Rule {
public String name;
public GrammarASTWithOptions ast;
public GrammarAST arg;
public GrammarAST ret;
public AttributeScope args;
public AttributeScope retvals;
public AttributeScope scope;
/** A list of scope names used by this rule */
public List<Token> useScopes;
public int numberOfAlts;
/** Labels are visible to all alts in a rule. Record all defs here.
@ -30,9 +37,10 @@ public class Rule {
public String toString() {
return "Rule{" +
"name='" + name + '\'' +
", arg=" + arg +
", ret=" + ret +
", args=" + args +
", retvals=" + retvals +
", labels=" + labelDefs +
", scope=" + scope +
'}';
}
}

View File

@ -5,6 +5,8 @@ import org.junit.Test;
public class TestScopeParsing extends BaseTest {
String[] argPairs = {
"", "{}",
" ", "{}",
"int i", "{i=int i}",
"int[] i, int j[]", "{i=int[] i, j=int [] j}",
"Map<A\\,B>[] i, int j[]", "{i=Map<A,B>[] i, j=int [] j}",
@ -25,22 +27,15 @@ public class TestScopeParsing extends BaseTest {
"int i = 34+a[3]; int j[] = new int[34];",
"{i=int i= 34+a[3], j=int [] j= new int[34]}",
"char *foo32[] = {1,2,3};", "{foo32=char *[] foo32= {1,2,3}}",
" int i; int c; int k; ", "{i=int i, c=int c, k=int k}",
" { int i; int c; int k; }", "{i=int i, c=int c, k=int k}",
// python/ruby style
"i", "{i=null i}",
"i; j;", "{i=null i, j=null j}",
" i ; j ;", "{i=null i, j=null j}",
"i; j; k;", "{i=null i, j=null j, k=null k}",
};
public void testInputOutputPairs(String[] pairs) {
for (int i = 0; i < pairs.length; i+=2) {
String input = pairs[i];
String expected = pairs[i+1];
String actual = ScopeParser.parseTypeList(input).attributes.toString();
assertEquals(expected, actual);
}
}
@Test public void testArgs() {
for (int i = 0; i < argPairs.length; i+=2) {
String input = argPairs[i];

View File

@ -24,17 +24,17 @@ public class TestSymbolIssues extends BaseTest {
"\n" +
"ID : 'a'..'z'+ ID ;",
// YIELDS
"error(49): A.g:2:10: illegal option opt\n" +
"error(59): A.g:11:6: scope Blort redefinition\n" +
"error(48): A.g:2:10: illegal option opt\n" +
"error(58): A.g:11:6: scope Blort redefinition\n" +
"error(18): A.g:15:0: rule a redefinition\n" +
"error(58): A.g:7:1: redefinition of members action\n" +
"error(58): A.g:9:1: redefinition of header action\n" +
"error(72): A.g:3:19: cannot alias X; token name already defined\n" +
"error(72): A.g:3:26: cannot alias Y; token name already assigned to 'y'\n" +
"error(72): A.g:3:36: cannot alias Z; token name already defined\n" +
"error(46): A.g:13:37: rule b has no defined parameters\n" +
"error(24): A.g:13:43: reference to undefined rule: q\n" +
"error(45): A.g:14:31: missing parameter(s) on rule reference: a"
"error(57): A.g:7:1: redefinition of members action\n" +
"error(57): A.g:9:1: redefinition of header action\n" +
"error(71): A.g:3:19: cannot alias X; token name already defined\n" +
"error(71): A.g:3:26: cannot alias Y; token name already assigned to 'y'\n" +
"error(71): A.g:3:36: cannot alias Z; token name already defined\n" +
"error(45): A.g:13:37: rule b has no defined parameters\n" +
"error(23): A.g:13:43: reference to undefined rule: q\n" +
"error(44): A.g:14:31: missing parameter(s) on rule reference: a"
};
static String[] B = {
@ -49,11 +49,11 @@ public class TestSymbolIssues extends BaseTest {
"\n" +
"s : FOO ;",
// YIELDS
"error(34): B.g:9:0: symbol s conflicts with global dynamic scope with same name\n" +
"error(35): B.g:5:9: label b conflicts with rule with same name\n" +
"error(34): B.g:5:4: symbol s conflicts with global dynamic scope with same name\n" +
"error(36): B.g:5:15: label X conflicts with token with same name\n" +
"error(41): B.g:7:9: label x type mismatch with previous definition: TOKEN_LIST_LABEL!=TOKEN_LABEL"
"error(33): B.g:9:0: symbol s conflicts with global dynamic scope with same name\n" +
"error(34): B.g:5:9: label b conflicts with rule with same name\n" +
"error(33): B.g:5:4: symbol s conflicts with global dynamic scope with same name\n" +
"error(35): B.g:5:15: label X conflicts with token with same name\n" +
"error(40): B.g:7:9: label x type mismatch with previous definition: TOKEN_LIST_LABEL!=TOKEN_LABEL"
};
static String[] C = {
@ -66,14 +66,38 @@ public class TestSymbolIssues extends BaseTest {
"b : B ;\n"+
"A : 'a';",
// YIELDS
"error(51): C.g:3:23: reference to rewrite element ID not found to left of ->\n" +
"error(51): C.g:3:28: reference to rewrite element r not found to left of ->\n" +
"error(51): C.g:3:30: reference to rewrite element foo not found to left of ->\n" +
"error(51): C.g:3:49: reference to rewrite element 'eh?' not found to left of ->\n" +
"error(51): C.g:4:10: reference to rewrite element x not found to left of ->\n" +
"error(51): C.g:4:13: reference to rewrite element A not found to left of ->"
"error(50): C.g:3:23: reference to rewrite element ID not found to left of ->\n" +
"error(50): C.g:3:28: reference to rewrite element r not found to left of ->\n" +
"error(50): C.g:3:30: reference to rewrite element foo not found to left of ->\n" +
"error(50): C.g:3:49: reference to rewrite element 'eh?' not found to left of ->\n" +
"error(50): C.g:4:10: reference to rewrite element x not found to left of ->\n" +
"error(50): C.g:4:13: reference to rewrite element A not found to left of ->"
};
static String[] D = {
// INPUT
"parser grammar D;\n" +
"a[int j] \n" +
"scope { int i; }\n" +
" : i=ID j=ID ;\n" +
"\n" +
"b[int i] returns [int i] : ID ;\n" +
"\n" +
"c[int i] returns [String k]\n" +
"scope { int i; int c; int k; }\n" +
" : ID ;",
// YIELDS
"error(37): D.g:4:21: label j conflicts with rule a's return value or parameter with same name\n" +
"error(36): D.g:4:16: label i conflicts with rule a's dynamically-scoped attribute with same name\n" +
"error(41): D.g:6:0: rule b's argument i conflicts a return value with same name\n" +
"error(39): D.g:9:6: rule c's dynamically-scoped attribute i conflicts with c's return value or parameter with same name\n" +
"error(38): D.g:9:6: rule c's dynamically-scoped attribute c conflicts with the rule name\n" +
"error(39): D.g:9:6: rule c's dynamically-scoped attribute k conflicts with c's return value or parameter with same name"
};
@Test public void testA() { super.testErrors(A); }
@Test public void testB() { super.testErrors(B); }
@Test public void testC() { super.testErrors(C); }
@Test public void testD() { super.testErrors(D); }
}