added test for modes in non lexers

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6817]
This commit is contained in:
parrt 2010-04-19 15:30:01 -08:00
parent 849060e55a
commit 91f92d9b6c
6 changed files with 388 additions and 292 deletions

View File

@ -94,14 +94,11 @@ NO_VIABLE_DFA_ALT(arg,arg2) ::=
// GRAMMAR ERRORS
SYNTAX_ERROR(arg) ::= "<arg>"
RULE_REDEFINITION(arg) ::=
"rule <arg> redefinition"
SCOPE_REDEFINITION(arg) ::=
"scope <arg> redefinition"
LEXER_RULES_NOT_ALLOWED(arg) ::=
"lexer rule <arg> not allowed in parser"
PARSER_RULES_NOT_ALLOWED(arg) ::=
"parser rule <arg> not allowed in lexer"
RULE_REDEFINITION(arg) ::= "rule <arg> redefinition"
SCOPE_REDEFINITION(arg) ::= "scope <arg> redefinition"
LEXER_RULES_NOT_ALLOWED(arg) ::= "lexer rule <arg> not allowed in parser"
PARSER_RULES_NOT_ALLOWED(arg) ::= "parser rule <arg> not allowed in lexer"
MODE_NOT_IN_LEXER(arg,arg2) ::= "lexical modes are only allowed in lexer grammars"
TOKEN_NAMES_MUST_START_UPPER(arg) ::=
"token names must start with an uppercase letter: <arg>"
CANNOT_FIND_ATTRIBUTE_NAME_IN_DECL(arg) ::=

View File

@ -131,8 +131,7 @@ public class BasicSemanticChecks {
}
}
void checkNumRules(GrammarAST rulesNode)
{
void checkNumRules(GrammarAST rulesNode) {
if ( rulesNode.getChildCount()==0 ) {
GrammarAST root = (GrammarAST)rulesNode.getParent();
GrammarAST IDNode = (GrammarAST)root.getChild(0);
@ -141,6 +140,13 @@ public class BasicSemanticChecks {
}
}
void checkMode(Token modeNameToken) {
if ( g.getType()!=ANTLRParser.LEXER ) {
g.tool.errMgr.grammarError(ErrorType.MODE_NOT_IN_LEXER, g.fileName,
modeNameToken, modeNameToken.getText(), g);
}
}
void checkNumPrequels(List<GrammarAST> options,
List<GrammarAST> imports,
List<GrammarAST> tokens)

View File

@ -86,6 +86,7 @@ public BasicSemanticTriggers(TreeNodeStream input, Grammar g) {
topdown // do these on way down so options and such are set first
: grammarSpec
| rules
| mode
| option
| rule
| tokenAlias
@ -143,6 +144,8 @@ delegateGrammar
rules : RULES {checker.checkNumRules($RULES);} ;
mode : ^(MODE ID .*) {checker.checkMode($ID.token);} ;
option // TODO: put in grammar, or rule, or block
: {inContext("OPTIONS")}? ^(ASSIGN o=ID optionValue)
{

File diff suppressed because it is too large Load Diff

View File

@ -142,6 +142,8 @@ public enum ErrorType {
LEFT_RECURSION_CYCLES(ErrorSeverity.ERROR, true, true),
ANALYSIS_TIMEOUT(ErrorSeverity.ERROR, true, true),
MODE_NOT_IN_LEXER(ErrorSeverity.ERROR, true, true),
/** Documentation comment is unterminated */
//UNTERMINATED_DOC_COMMENT(ErrorSeverity.ERROR, true, true),

View File

@ -128,4 +128,16 @@ public class TestSyntaxErrors extends BaseTest {
super.testErrors(pair, true);
}
@Test public void testModeInParser() {
String[] pair = new String[] {
"grammar A;\n" +
"a : A ;\n" +
"mode foo;\n" +
"b : B ;",
"error(87): A.g:3:5: lexical modes are only allowed in lexer grammars\n"
};
super.testErrors(pair, true);
}
}