From b433c2ae345dab266cdf1c9aee700c6e042a97a7 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 3 Apr 2012 16:11:26 -0500 Subject: [PATCH] Add warning 125: implicit definition of token in parser --- .../src/org/antlr/v4/semantics/SemanticPipeline.java | 12 +++++++++++- tool/src/org/antlr/v4/tool/ErrorType.java | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tool/src/org/antlr/v4/semantics/SemanticPipeline.java b/tool/src/org/antlr/v4/semantics/SemanticPipeline.java index 127733b5d..a75fd4e74 100644 --- a/tool/src/org/antlr/v4/semantics/SemanticPipeline.java +++ b/tool/src/org/antlr/v4/semantics/SemanticPipeline.java @@ -31,6 +31,7 @@ package org.antlr.v4.semantics; import org.antlr.v4.analysis.LeftRecursiveRuleTransformer; import org.antlr.v4.parse.ANTLRParser; +import org.antlr.v4.runtime.Token; import org.antlr.v4.tool.*; import org.antlr.v4.tool.ast.GrammarAST; @@ -174,6 +175,9 @@ public class SemanticPipeline { String lit = alias.getChild(1).getText(); g.defineTokenAlias(name, lit); } + else { + g.defineTokenName(alias.getText()); + } } // DEFINE TOKEN TYPES FOR X : 'x' ; RULES @@ -187,7 +191,13 @@ public class SemanticPipeline { */ // DEFINE TOKEN TYPES FOR TOKEN REFS LIKE ID, INT - for (GrammarAST idAST : tokenIDs) { g.defineTokenName(idAST.getText()); } + for (GrammarAST idAST : tokenIDs) { + if (g.getTokenType(idAST.getText()) == Token.INVALID_TYPE) { + g.tool.errMgr.grammarError(ErrorType.IMPLICIT_TOKEN_DEFINITION, g.fileName, idAST.token, idAST.getText()); + } + + g.defineTokenName(idAST.getText()); + } // DEFINE TOKEN TYPES FOR STRING LITERAL REFS LIKE 'while', ';' for (String s : strings) { g.defineStringLiteral(s); } diff --git a/tool/src/org/antlr/v4/tool/ErrorType.java b/tool/src/org/antlr/v4/tool/ErrorType.java index 3d406d9dd..07ca65eec 100644 --- a/tool/src/org/antlr/v4/tool/ErrorType.java +++ b/tool/src/org/antlr/v4/tool/ErrorType.java @@ -144,6 +144,8 @@ public enum ErrorType { RULE_WITH_TOO_FEW_ALT_LABELS(122, "rule : must label all alternatives or none", ErrorSeverity.ERROR), ALT_LABEL_REDEF(123, "rule alt label redefined in rule , originally in ", ErrorSeverity.ERROR), ALT_LABEL_CONFLICTS_WITH_RULE(124, "rule alt label conflicts with rule ", ErrorSeverity.ERROR), + IMPLICIT_TOKEN_DEFINITION(125, "implicit definition of token in parser", ErrorSeverity.WARNING), + /** Documentation comment is unterminated */ //UNTERMINATED_DOC_COMMENT(, "", ErrorSeverity.ERROR),