got some symbols collected; renamed.

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6667]
This commit is contained in:
parrt 2010-02-05 14:21:02 -08:00
parent ffd60dc26b
commit 6f7e52ec9e
7 changed files with 106 additions and 534 deletions

View File

@ -1,4 +1,4 @@
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 BasicSemanticTriggers.g 2010-02-04 17:59:03
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 BasicSemanticTriggers.g 2010-02-05 14:20:12
/*
[The "BSD license"]

View File

@ -27,7 +27,7 @@
/** Triggers for defining rules, tokens, scopes, and actions.
* Side-effects: ...
*/
tree grammar DefineSymbolTriggers;
tree grammar CollectSymbols;
options {
language = Java;
tokenVocab = ANTLRParser;
@ -68,31 +68,79 @@ import org.antlr.v4.tool.*;
}
@members {
Rule currentRule = null;
public List<Rule> rules = new ArrayList<Rule>();
public List<GrammarAST> terminals = new ArrayList<GrammarAST>();
public List<GrammarAST> aliases = new ArrayList<GrammarAST>();
public List<GrammarAST> scopes = new ArrayList<GrammarAST>();
public List<GrammarAST> actions = new ArrayList<GrammarAST>();
Grammar g; // which grammar are we checking
public DefineSymbolTriggers(TreeNodeStream input, Grammar g) {
public CollectSymbols(TreeNodeStream input, Grammar g) {
this(input);
this.g = g;
}
}
topdown
: tokenAlias
: globalScope
| action
| tokenAlias
| rule
| ruleArg
| ruleReturns
| terminal
;
tokenAlias
: {inContext("TOKENS")}? ^(ASSIGN ID STRING_LITERAL)
{System.out.println("token alias "+$ID.text+"="+$STRING_LITERAL.token);}
bottomup
: finishRule
;
rule: ^( RULE r=ID .*) {System.out.println("rule "+$r.token);} //{DefineSymbols.checkInvalidRuleDef(g.getType(), $r.token);}
globalScope
: {inContext("GRAMMAR")}? ^(SCOPE ID ACTION) {scopes.add($ID);}
;
action
: {inContext("GRAMMAR")}? ^(AT sc=ID? ID ACTION)
{actions.add($AT);}
;
tokenAlias
: {inContext("TOKENS")}?
( ^(ASSIGN t=ID STRING_LITERAL) {terminals.add($t); aliases.add($ASSIGN);}
| t=ID {terminals.add($t);}
)
;
rule: ^( RULE name=ID .+)
{
Rule r = new Rule($name.text, (GrammarASTWithOptions)$RULE);
rules.add(r);
currentRule = r;
}
;
finishRule
: RULE {currentRule = null;}
;
ruleArg
: {inContext("RULE")}? ARG_ACTION {currentRule.arg = $ARG_ACTION;}
;
ruleReturns
: ^(RETURNS ARG_ACTION) {currentRule.ret = $ARG_ACTION;}
;
ruleScopeSpec
: {inContext("RULE")}?
( ^(SCOPE ACTION)
| ^(SCOPE ID+)
)
;
terminal
: {!inContext("TOKENS ASSIGN")}? STRING_LITERAL
{System.out.println("terminal "+$STRING_LITERAL.token);}
| TOKEN_REF {System.out.println("terminal "+$TOKEN_REF.token);}
: {!inContext("TOKENS ASSIGN")}? STRING_LITERAL {terminals.add($start);}
| TOKEN_REF {terminals.add($start);}
;
/*

View File

@ -1,422 +0,0 @@
// $ANTLR 3.2.1-SNAPSHOT Jan 26, 2010 15:12:28 DefineSymbolTriggers.g 2010-02-04 17:59:03
/*
[The "BSD license"]
Copyright (c) 2010 Terence Parr
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.semantics;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.TreeNodeStream;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.GrammarAST;
/** Triggers for defining rules, tokens, scopes, and actions.
* Side-effects: ...
*/
public class DefineSymbolTriggers extends org.antlr.v4.runtime.tree.TreeFilter {
public static final String[] tokenNames = new String[] {
"<invalid>", "<EOR>", "<DOWN>", "<UP>", "SEMPRED", "FORCED_ACTION", "DOC_COMMENT", "SRC", "NLCHARS", "COMMENT", "DOUBLE_QUOTE_STRING_LITERAL", "DOUBLE_ANGLE_STRING_LITERAL", "ACTION_STRING_LITERAL", "ACTION_CHAR_LITERAL", "ARG_ACTION", "NESTED_ACTION", "ACTION", "ACTION_ESC", "WSNLCHARS", "OPTIONS", "TOKENS", "SCOPE", "IMPORT", "FRAGMENT", "LEXER", "PARSER", "TREE", "GRAMMAR", "PROTECTED", "PUBLIC", "PRIVATE", "RETURNS", "THROWS", "CATCH", "FINALLY", "TEMPLATE", "COLON", "COLONCOLON", "COMMA", "SEMI", "LPAREN", "RPAREN", "IMPLIES", "LT", "GT", "ASSIGN", "QUESTION", "BANG", "STAR", "PLUS", "PLUS_ASSIGN", "OR", "ROOT", "DOLLAR", "DOT", "RANGE", "ETC", "RARROW", "TREE_BEGIN", "AT", "NOT", "RBRACE", "TOKEN_REF", "RULE_REF", "INT", "WSCHARS", "ESC_SEQ", "STRING_LITERAL", "HEX_DIGIT", "UNICODE_ESC", "WS", "ERRCHAR", "RULE", "RULES", "RULEMODIFIERS", "RULEACTIONS", "BLOCK", "REWRITE_BLOCK", "OPTIONAL", "CLOSURE", "POSITIVE_CLOSURE", "SYNPRED", "CHAR_RANGE", "EPSILON", "ALT", "ALTLIST", "RESULT", "ID", "ARG", "ARGLIST", "RET", "INITACTION", "LABEL", "GATED_SEMPRED", "SYN_SEMPRED", "BACKTRACK_SEMPRED", "WILDCARD", "LIST", "ELEMENT_OPTIONS", "ST_RESULT", "ALT_REWRITE"
};
public static final int LT=43;
public static final int STAR=48;
public static final int BACKTRACK_SEMPRED=95;
public static final int DOUBLE_ANGLE_STRING_LITERAL=11;
public static final int FORCED_ACTION=5;
public static final int ARGLIST=89;
public static final int ALTLIST=85;
public static final int NOT=60;
public static final int EOF=-1;
public static final int SEMPRED=4;
public static final int ACTION=16;
public static final int TOKEN_REF=62;
public static final int RULEMODIFIERS=74;
public static final int ST_RESULT=99;
public static final int RPAREN=41;
public static final int RET=90;
public static final int IMPORT=22;
public static final int STRING_LITERAL=67;
public static final int ARG=88;
public static final int ARG_ACTION=14;
public static final int DOUBLE_QUOTE_STRING_LITERAL=10;
public static final int COMMENT=9;
public static final int ACTION_CHAR_LITERAL=13;
public static final int GRAMMAR=27;
public static final int RULEACTIONS=75;
public static final int WSCHARS=65;
public static final int INITACTION=91;
public static final int ALT_REWRITE=100;
public static final int IMPLIES=42;
public static final int RULE=72;
public static final int RBRACE=61;
public static final int ACTION_ESC=17;
public static final int PRIVATE=30;
public static final int SRC=7;
public static final int THROWS=32;
public static final int CHAR_RANGE=82;
public static final int INT=64;
public static final int EPSILON=83;
public static final int LIST=97;
public static final int COLONCOLON=37;
public static final int WSNLCHARS=18;
public static final int WS=70;
public static final int LEXER=24;
public static final int OR=51;
public static final int GT=44;
public static final int CATCH=33;
public static final int CLOSURE=79;
public static final int PARSER=25;
public static final int DOLLAR=53;
public static final int PROTECTED=28;
public static final int ELEMENT_OPTIONS=98;
public static final int NESTED_ACTION=15;
public static final int FRAGMENT=23;
public static final int ID=87;
public static final int TREE_BEGIN=58;
public static final int LPAREN=40;
public static final int AT=59;
public static final int ESC_SEQ=66;
public static final int ALT=84;
public static final int TREE=26;
public static final int SCOPE=21;
public static final int ETC=56;
public static final int COMMA=38;
public static final int WILDCARD=96;
public static final int DOC_COMMENT=6;
public static final int PLUS=49;
public static final int REWRITE_BLOCK=77;
public static final int DOT=54;
public static final int RETURNS=31;
public static final int RULES=73;
public static final int RARROW=57;
public static final int UNICODE_ESC=69;
public static final int HEX_DIGIT=68;
public static final int RANGE=55;
public static final int TOKENS=20;
public static final int GATED_SEMPRED=93;
public static final int RESULT=86;
public static final int BANG=47;
public static final int ACTION_STRING_LITERAL=12;
public static final int ROOT=52;
public static final int SEMI=39;
public static final int RULE_REF=63;
public static final int NLCHARS=8;
public static final int OPTIONAL=78;
public static final int SYNPRED=81;
public static final int COLON=36;
public static final int QUESTION=46;
public static final int FINALLY=34;
public static final int TEMPLATE=35;
public static final int LABEL=92;
public static final int SYN_SEMPRED=94;
public static final int ERRCHAR=71;
public static final int BLOCK=76;
public static final int ASSIGN=45;
public static final int PLUS_ASSIGN=50;
public static final int PUBLIC=29;
public static final int POSITIVE_CLOSURE=80;
public static final int OPTIONS=19;
// delegates
// delegators
public DefineSymbolTriggers(TreeNodeStream input) {
this(input, new RecognizerSharedState());
}
public DefineSymbolTriggers(TreeNodeStream input, RecognizerSharedState state) {
super(input, state);
}
public String[] getTokenNames() { return DefineSymbolTriggers.tokenNames; }
public String getGrammarFileName() { return "DefineSymbolTriggers.g"; }
Grammar g; // which grammar are we checking
public DefineSymbolTriggers(TreeNodeStream input, Grammar g) {
this(input);
this.g = g;
}
// $ANTLR start "topdown"
// DefineSymbolTriggers.g:78:1: topdown : ( tokenAlias | rule | terminal );
public final void topdown() throws RecognitionException {
try {
// DefineSymbolTriggers.g:79:5: ( tokenAlias | rule | terminal )
int alt1=3;
switch ( input.LA(1) ) {
case ASSIGN:
{
alt1=1;
}
break;
case RULE:
{
alt1=2;
}
break;
case TOKEN_REF:
case STRING_LITERAL:
{
alt1=3;
}
break;
default:
if (state.backtracking>0) {state.failed=true; return ;}
NoViableAltException nvae =
new NoViableAltException("", 1, 0, input);
throw nvae;
}
switch (alt1) {
case 1 :
// DefineSymbolTriggers.g:79:7: tokenAlias
{
pushFollow(FOLLOW_tokenAlias_in_topdown96);
tokenAlias();
state._fsp--;
if (state.failed) return ;
}
break;
case 2 :
// DefineSymbolTriggers.g:80:7: rule
{
pushFollow(FOLLOW_rule_in_topdown104);
rule();
state._fsp--;
if (state.failed) return ;
}
break;
case 3 :
// DefineSymbolTriggers.g:81:7: terminal
{
pushFollow(FOLLOW_terminal_in_topdown112);
terminal();
state._fsp--;
if (state.failed) return ;
}
break;
}
}
catch (RecognitionException re) {
reportError(re);
recover(input,re);
}
finally {
}
return ;
}
// $ANTLR end "topdown"
// $ANTLR start "tokenAlias"
// DefineSymbolTriggers.g:84:1: tokenAlias : {...}? ^( ASSIGN ID STRING_LITERAL ) ;
public final void tokenAlias() throws RecognitionException {
GrammarAST ID1=null;
GrammarAST STRING_LITERAL2=null;
try {
// DefineSymbolTriggers.g:85:2: ({...}? ^( ASSIGN ID STRING_LITERAL ) )
// DefineSymbolTriggers.g:85:4: {...}? ^( ASSIGN ID STRING_LITERAL )
{
if ( !((inContext("TOKENS"))) ) {
if (state.backtracking>0) {state.failed=true; return ;}
throw new FailedPredicateException(input, "tokenAlias", "inContext(\"TOKENS\")");
}
match(input,ASSIGN,FOLLOW_ASSIGN_in_tokenAlias126); if (state.failed) return ;
match(input, Token.DOWN, null); if (state.failed) return ;
ID1=(GrammarAST)match(input,ID,FOLLOW_ID_in_tokenAlias128); if (state.failed) return ;
STRING_LITERAL2=(GrammarAST)match(input,STRING_LITERAL,FOLLOW_STRING_LITERAL_in_tokenAlias130); if (state.failed) return ;
match(input, Token.UP, null); if (state.failed) return ;
if ( state.backtracking==1 ) {
System.out.println("token alias "+(ID1!=null?ID1.getText():null)+"="+STRING_LITERAL2.token);
}
}
}
catch (RecognitionException re) {
reportError(re);
recover(input,re);
}
finally {
}
return ;
}
// $ANTLR end "tokenAlias"
// $ANTLR start "rule"
// DefineSymbolTriggers.g:89:1: rule : ^( RULE r= ID ( . )* ) ;
public final void rule() throws RecognitionException {
GrammarAST r=null;
try {
// DefineSymbolTriggers.g:89:5: ( ^( RULE r= ID ( . )* ) )
// DefineSymbolTriggers.g:89:9: ^( RULE r= ID ( . )* )
{
match(input,RULE,FOLLOW_RULE_in_rule148); if (state.failed) return ;
match(input, Token.DOWN, null); if (state.failed) return ;
r=(GrammarAST)match(input,ID,FOLLOW_ID_in_rule152); if (state.failed) return ;
// DefineSymbolTriggers.g:89:22: ( . )*
loop2:
do {
int alt2=2;
int LA2_0 = input.LA(1);
if ( ((LA2_0>=SEMPRED && LA2_0<=ALT_REWRITE)) ) {
alt2=1;
}
else if ( (LA2_0==UP) ) {
alt2=2;
}
switch (alt2) {
case 1 :
// DefineSymbolTriggers.g:89:22: .
{
matchAny(input); if (state.failed) return ;
}
break;
default :
break loop2;
}
} while (true);
match(input, Token.UP, null); if (state.failed) return ;
if ( state.backtracking==1 ) {
System.out.println("rule "+r.token);
}
}
}
catch (RecognitionException re) {
reportError(re);
recover(input,re);
}
finally {
}
return ;
}
// $ANTLR end "rule"
// $ANTLR start "terminal"
// DefineSymbolTriggers.g:92:1: terminal : ({...}? STRING_LITERAL | TOKEN_REF );
public final void terminal() throws RecognitionException {
GrammarAST STRING_LITERAL3=null;
GrammarAST TOKEN_REF4=null;
try {
// DefineSymbolTriggers.g:93:5: ({...}? STRING_LITERAL | TOKEN_REF )
int alt3=2;
int LA3_0 = input.LA(1);
if ( (LA3_0==STRING_LITERAL) ) {
alt3=1;
}
else if ( (LA3_0==TOKEN_REF) ) {
alt3=2;
}
else {
if (state.backtracking>0) {state.failed=true; return ;}
NoViableAltException nvae =
new NoViableAltException("", 3, 0, input);
throw nvae;
}
switch (alt3) {
case 1 :
// DefineSymbolTriggers.g:93:7: {...}? STRING_LITERAL
{
if ( !((!inContext("TOKENS ASSIGN"))) ) {
if (state.backtracking>0) {state.failed=true; return ;}
throw new FailedPredicateException(input, "terminal", "!inContext(\"TOKENS ASSIGN\")");
}
STRING_LITERAL3=(GrammarAST)match(input,STRING_LITERAL,FOLLOW_STRING_LITERAL_in_terminal178); if (state.failed) return ;
if ( state.backtracking==1 ) {
System.out.println("terminal "+STRING_LITERAL3.token);
}
}
break;
case 2 :
// DefineSymbolTriggers.g:95:7: TOKEN_REF
{
TOKEN_REF4=(GrammarAST)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_terminal193); if (state.failed) return ;
if ( state.backtracking==1 ) {
System.out.println("terminal "+TOKEN_REF4.token);
}
}
break;
}
}
catch (RecognitionException re) {
reportError(re);
recover(input,re);
}
finally {
}
return ;
}
// $ANTLR end "terminal"
// Delegated rules
public static final BitSet FOLLOW_tokenAlias_in_topdown96 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_rule_in_topdown104 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_terminal_in_topdown112 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_ASSIGN_in_tokenAlias126 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ID_in_tokenAlias128 = new BitSet(new long[]{0x0000000000000000L,0x0000000000000008L});
public static final BitSet FOLLOW_STRING_LITERAL_in_tokenAlias130 = new BitSet(new long[]{0x0000000000000008L});
public static final BitSet FOLLOW_RULE_in_rule148 = new BitSet(new long[]{0x0000000000000004L});
public static final BitSet FOLLOW_ID_in_rule152 = new BitSet(new long[]{0xFFFFFFFFFFFFFFF8L,0x0000001FFFFFFFFFL});
public static final BitSet FOLLOW_STRING_LITERAL_in_terminal178 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_TOKEN_REF_in_terminal193 = new BitSet(new long[]{0x0000000000000002L});
}

View File

@ -1,97 +0,0 @@
LT=43
STAR=48
BACKTRACK_SEMPRED=95
DOUBLE_ANGLE_STRING_LITERAL=11
FORCED_ACTION=5
ARGLIST=89
ALTLIST=85
NOT=60
SEMPRED=4
ACTION=16
TOKEN_REF=62
RULEMODIFIERS=74
ST_RESULT=99
RPAREN=41
RET=90
IMPORT=22
STRING_LITERAL=67
ARG=88
ARG_ACTION=14
DOUBLE_QUOTE_STRING_LITERAL=10
COMMENT=9
ACTION_CHAR_LITERAL=13
GRAMMAR=27
RULEACTIONS=75
WSCHARS=65
INITACTION=91
ALT_REWRITE=100
IMPLIES=42
RULE=72
RBRACE=61
ACTION_ESC=17
PRIVATE=30
SRC=7
THROWS=32
CHAR_RANGE=82
INT=64
EPSILON=83
LIST=97
COLONCOLON=37
WSNLCHARS=18
WS=70
LEXER=24
OR=51
GT=44
CATCH=33
CLOSURE=79
PARSER=25
DOLLAR=53
PROTECTED=28
ELEMENT_OPTIONS=98
NESTED_ACTION=15
FRAGMENT=23
ID=87
TREE_BEGIN=58
LPAREN=40
AT=59
ESC_SEQ=66
ALT=84
TREE=26
SCOPE=21
ETC=56
COMMA=38
WILDCARD=96
DOC_COMMENT=6
PLUS=49
REWRITE_BLOCK=77
DOT=54
RETURNS=31
RULES=73
RARROW=57
UNICODE_ESC=69
HEX_DIGIT=68
RANGE=55
TOKENS=20
GATED_SEMPRED=93
RESULT=86
BANG=47
ACTION_STRING_LITERAL=12
ROOT=52
SEMI=39
RULE_REF=63
NLCHARS=8
OPTIONAL=78
SYNPRED=81
COLON=36
QUESTION=46
FINALLY=34
TEMPLATE=35
LABEL=92
SYN_SEMPRED=94
ERRCHAR=71
BLOCK=76
ASSIGN=45
PLUS_ASSIGN=50
PUBLIC=29
POSITIVE_CLOSURE=80
OPTIONS=19

View File

@ -28,6 +28,9 @@ public class SemanticsPipeline {
BasicSemanticTriggers basics = new BasicSemanticTriggers(nodes,g);
basics.downup(g.ast);
// don't continue if we get errors in this basic check
if ( false ) return;
// NOW DO BASIC / EASY SEMANTIC CHECKS FOR DELEGATES (IF ANY)
if ( g.getImportedGrammars()!=null ) {
for (Grammar d : g.getImportedGrammars()) {
@ -35,10 +38,15 @@ public class SemanticsPipeline {
}
}
// CHECK FOR SYMBOL COLLISIONS
// DEFINE SYMBOLS
nodes.reset();
DefineSymbolTriggers sym = new DefineSymbolTriggers(nodes,g);
CollectSymbols sym = new CollectSymbols(nodes,g);
sym.downup(g.ast);
System.out.println("rules="+sym.rules);
System.out.println("terminals="+sym.terminals);
System.out.println("aliases="+sym.aliases);
System.out.println("aliases="+sym.actions);
// ASSIGN TOKEN TYPES

View File

@ -0,0 +1,21 @@
package org.antlr.v4.semantics;
/** check for the following errors:
*
* RULE_REDEFINITION
RULE_HAS_NO_ARGS
UNDEFINED_RULE_REF
MISSING_RULE_ARGS
SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE
LABEL_CONFLICTS_WITH_RULE
LABEL_CONFLICTS_WITH_TOKEN
LABEL_TYPE_CONFLICT
ACTION_REDEFINITION
NO_SUCH_RULE_IN_SCOPE
TOKEN_ALIAS_CONFLICT
TOKEN_ALIAS_REASSIGNMENT
The
*/
public class SymbolCollisionChecks {
}

View File

@ -1,7 +1,21 @@
package org.antlr.v4.tool;
import org.antlr.runtime.tree.CommonTree;
public class Rule {
public CommonTree tree;
public String name;
public GrammarASTWithOptions ast;
public GrammarAST arg;
public GrammarAST ret;
public Rule(String name, GrammarASTWithOptions ast) {
this.name = name;
this.ast = ast;
}
@Override
public String toString() {
return "Rule{" +
"name='" + name + '\'' +
", arg=" + arg +
", ret=" + ret +
'}';
}
}