From 97ab2c42da56405c55265698f0b14f6315eb3336 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Mon, 13 Feb 2012 11:45:10 -0800 Subject: [PATCH] mode wasn't working as lexer command --- tool/src/org/antlr/v4/parse/ANTLRParser.g | 9 ++++-- .../test/org/antlr/v4/test/TestLexerExec.java | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/tool/src/org/antlr/v4/parse/ANTLRParser.g b/tool/src/org/antlr/v4/parse/ANTLRParser.g index 32d31db70..540bfbe47 100644 --- a/tool/src/org/antlr/v4/parse/ANTLRParser.g +++ b/tool/src/org/antlr/v4/parse/ANTLRParser.g @@ -642,8 +642,8 @@ lexerCommands ; lexerCommand - : id LPAREN lexerCommandExpr RPAREN -> ^(LEXER_ACTION_CALL id lexerCommandExpr) - | id + : lexerCommandName LPAREN lexerCommandExpr RPAREN -> ^(LEXER_ACTION_CALL lexerCommandName lexerCommandExpr) + | lexerCommandName ; lexerCommandExpr @@ -651,6 +651,11 @@ lexerCommandExpr | INT ; +lexerCommandName + : id + | MODE ->ID[$MODE] + ; + altList : alternative (OR alternative)* -> alternative+ ; diff --git a/tool/test/org/antlr/v4/test/TestLexerExec.java b/tool/test/org/antlr/v4/test/TestLexerExec.java index c4b659f4b..a97bbe99b 100644 --- a/tool/test/org/antlr/v4/test/TestLexerExec.java +++ b/tool/test/org/antlr/v4/test/TestLexerExec.java @@ -119,6 +119,38 @@ public class TestLexerExec extends BaseTest { assertEquals(expecting, found); } + @Test public void testLexerPushPopModeAction() throws Exception { + String grammar = + "lexer grammar L;\n" + + "STRING_START : '\"' -> pushMode(STRING_MODE), more ;\n" + + "WS : (' '|'\n') -> skip ;\n"+ + "mode STRING_MODE;\n"+ + "STRING : '\"' -> popMode ;\n"+ + "ANY : . -> more ;\n"; + String found = execLexer("L.g", grammar, "L", "\"abc\" \"ab\""); + String expecting = + "[@0,0:4='\"abc\"',<5>,1:0]\n" + + "[@1,6:9='\"ab\"',<5>,1:6]\n" + + "[@2,10:9='',<-1>,1:10]\n"; + assertEquals(expecting, found); + } + + @Test public void testLexerModeAction() throws Exception { + String grammar = + "lexer grammar L;\n" + + "STRING_START : '\"' -> mode(STRING_MODE), more ;\n" + + "WS : (' '|'\n') -> skip ;\n"+ + "mode STRING_MODE;\n"+ + "STRING : '\"' -> mode(DEFAULT_MODE) ;\n"+ + "ANY : . -> more ;\n"; + String found = execLexer("L.g", grammar, "L", "\"abc\" \"ab\""); + String expecting = + "[@0,0:4='\"abc\"',<5>,1:0]\n" + + "[@1,6:9='\"ab\"',<5>,1:6]\n" + + "[@2,10:9='',<-1>,1:10]\n"; + assertEquals(expecting, found); + } + @Test public void testKeywordID() throws Exception { String grammar = "lexer grammar L;\n"+