Merge branch 'fix-several-bugs' of git://github.com/sharwell/antlr4

This commit is contained in:
Terence Parr 2012-11-18 16:19:30 -08:00
commit 80f7c35a7c
4 changed files with 28 additions and 17 deletions

View File

@ -433,10 +433,16 @@ public class Tool {
public boolean checkForRuleIssues(final Grammar g) { public boolean checkForRuleIssues(final Grammar g) {
// check for redefined rules // check for redefined rules
GrammarAST RULES = (GrammarAST)g.ast.getFirstChildWithType(ANTLRParser.RULES); GrammarAST RULES = (GrammarAST)g.ast.getFirstChildWithType(ANTLRParser.RULES);
List<RuleAST> rules = (List<RuleAST>)RULES.getChildren();
List<?> rules = (List<?>)RULES.getChildren();
if (rules == null) {
rules = Collections.emptyList();
}
final Map<String, RuleAST> ruleToAST = new HashMap<String, RuleAST>(); final Map<String, RuleAST> ruleToAST = new HashMap<String, RuleAST>();
for (RuleAST r : rules) { for (Object r : rules) {
GrammarAST ID = (GrammarAST)r.getChild(0); RuleAST ruleAST = (RuleAST)r;
GrammarAST ID = (GrammarAST)ruleAST.getChild(0);
String ruleName = ID.getText(); String ruleName = ID.getText();
RuleAST prev = ruleToAST.get(ruleName); RuleAST prev = ruleToAST.get(ruleName);
if ( prev !=null ) { if ( prev !=null ) {
@ -446,10 +452,10 @@ public class Tool {
ID.getToken(), ID.getToken(),
ruleName, ruleName,
prevChild.getToken().getLine()); prevChild.getToken().getLine());
r.dead = true; ruleAST.dead = true;
continue; continue;
} }
ruleToAST.put(ruleName, r); ruleToAST.put(ruleName, ruleAST);
} }
// check for undefined rules // check for undefined rules
@ -457,6 +463,11 @@ public class Tool {
public boolean undefined = false; public boolean undefined = false;
@Override @Override
public void tokenRef(TerminalAST ref) { public void tokenRef(TerminalAST ref) {
if ("EOF".equals(ref.getText())) {
// this is a special predefined reference
return;
}
if ( g.isLexer() ) ruleRef(ref, null); if ( g.isLexer() ) ruleRef(ref, null);
} }

View File

@ -298,8 +298,8 @@ ARG_ACTION
// //
ACTION ACTION
: NESTED_ACTION : NESTED_ACTION
('?' {$type = SEMPRED;} )? ( '?' {$type = SEMPRED;}
( '=>' // v3 gated sempred ( (WSNLCHARS* '=>') => WSNLCHARS* '=>' // v3 gated sempred
{ {
Token t = new CommonToken(input, state.type, state.channel, state.tokenStartCharIndex, getCharIndex()-1); Token t = new CommonToken(input, state.type, state.channel, state.tokenStartCharIndex, getCharIndex()-1);
t.setLine(state.tokenStartLine); t.setLine(state.tokenStartLine);
@ -308,6 +308,7 @@ ACTION
grammarError(ErrorType.V3_GATED_SEMPRED, t); grammarError(ErrorType.V3_GATED_SEMPRED, t);
} }
)? )?
)?
; ;
// ---------------- // ----------------

View File

@ -347,8 +347,7 @@ public class BasicSemanticChecks extends GrammarTreeVisitor {
boolean outerMostAlt = blk.parent.getType() == RULE; boolean outerMostAlt = blk.parent.getType() == RULE;
Tree rule = tree.getAncestor(RULE); Tree rule = tree.getAncestor(RULE);
String fileName = tree.getToken().getInputStream().getSourceName(); String fileName = tree.getToken().getInputStream().getSourceName();
if ( !outerMostAlt || tree.getChildIndex() != alt.getChildCount()-1 || if ( !outerMostAlt || blk.getChildCount()>1 )
blk.getChildCount()>1 )
{ {
ErrorType e = ErrorType.LEXER_COMMAND_PLACEMENT_ISSUE; ErrorType e = ErrorType.LEXER_COMMAND_PLACEMENT_ISSUE;
if ( tree.getType() == ACTION ) e = ErrorType.LEXER_ACTION_PLACEMENT_ISSUE; if ( tree.getType() == ACTION ) e = ErrorType.LEXER_ACTION_PLACEMENT_ISSUE;

View File

@ -151,7 +151,7 @@ public class SemanticPipeline {
void assignLexerTokenTypes(Grammar g, List<GrammarAST> tokensDefs) { void assignLexerTokenTypes(Grammar g, List<GrammarAST> tokensDefs) {
Grammar G = g.getOutermostGrammar(); // put in root, even if imported Grammar G = g.getOutermostGrammar(); // put in root, even if imported
for (GrammarAST def : tokensDefs) { for (GrammarAST def : tokensDefs) {
if ( def.getType()== ANTLRParser.ID ) G.defineTokenName(def.getText()); if ( def.getType()== ANTLRParser.TOKEN_REF ) G.defineTokenName(def.getText());
} }
/* Define token types for nonfragment rules which do not include a 'type(...)' /* Define token types for nonfragment rules which do not include a 'type(...)'