@@ -23,12 +23,12 @@ public class TestActionTemplates {
2323 String actionTemplates = tempDir + FileSeparator + "Java.st" ;
2424
2525 String grammar =
26- "lexer grammar L;" +
26+ "lexer grammar L;" +
2727 "WS : (' '|'\\ n') -> skip ;" ;
2828
2929 State state = execLexer (grammar , "34 34" , tempDir , actionTemplates );
3030
31- assertInstanceOf (GeneratedState .class , state );
31+ assertInstanceOf (GeneratedState .class , state , state . getErrorMessage () );
3232
3333 GeneratedState generated = (GeneratedState ) state ;
3434
@@ -45,12 +45,12 @@ public class TestActionTemplates {
4545 String actionTemplates = tempDir + FileSeparator + "Java.stg" ;
4646
4747 String grammar =
48- "lexer grammar L;" +
48+ "lexer grammar L;" +
4949 "WS : (' '|'\\ n') -> skip ;" ;
5050
5151 State state = execLexer (grammar , "34 34" , tempDir , actionTemplates );
5252
53- assertInstanceOf (GeneratedState .class , state );
53+ assertInstanceOf (GeneratedState .class , state , state . getErrorMessage () );
5454
5555 GeneratedState generated = (GeneratedState ) state ;
5656
@@ -69,13 +69,13 @@ public class TestActionTemplates {
6969 String grammarFile = tempDir + FileSeparator + "L.g4" ;
7070
7171 String grammar =
72- "lexer grammar L;\n " +
73- "I : '0'..'9'+ {<¢>} ;\n " +
72+ "lexer grammar L;\n " +
73+ "I : '0'..'9'+ {<¢>} ;\n " +
7474 "WS : (' '|'\\ n') -> skip ;" ;
7575
7676 State state = execLexer (grammar , "34 34" , tempDir , actionTemplates );
7777
78- assertInstanceOf (GeneratedState .class , state );
78+ assertInstanceOf (GeneratedState .class , state , state . getErrorMessage () );
7979
8080 GeneratedState generated = (GeneratedState ) state ;
8181
@@ -95,15 +95,15 @@ public class TestActionTemplates {
9595 String grammarFile = tempDir + FileSeparator + "L.g4" ;
9696
9797 String grammar =
98- "lexer grammar L;\n " +
98+ "lexer grammar L;\n " +
9999 "I : '0'..'9'+ {\n " +
100100 " <¢>\n " +
101- "};\n " +
101+ "};\n " +
102102 "WS : (' '|'\\ n') -> skip ;" ;
103103
104104 State state = execLexer (grammar , "34 34" , tempDir , actionTemplates );
105105
106- assertInstanceOf (GeneratedState .class , state );
106+ assertInstanceOf (GeneratedState .class , state , state . getErrorMessage () );
107107
108108 GeneratedState generated = (GeneratedState ) state ;
109109
@@ -123,8 +123,8 @@ public class TestActionTemplates {
123123 String grammarFile = tempDir + FileSeparator + "L.g4" ;
124124
125125 String grammar =
126- "lexer grammar L;\n " +
127- "I : '0'..'9'+ {<writeln(\" I\" )>} ;\n " +
126+ "lexer grammar L;\n " +
127+ "I : '0'..'9'+ {<writeln(\" I\" )>} ;\n " +
128128 "WS : (' '|'\\ n') -> skip ;" ;
129129
130130 State state = execLexer (grammar , "34 34" , tempDir , actionTemplates );
@@ -147,8 +147,8 @@ public class TestActionTemplates {
147147 String grammarFile = tempDir + FileSeparator + "L.g4" ;
148148
149149 String grammar =
150- "lexer grammar L;\n " +
151- "I : '0'..'9'+ {<writeln(\" I\" )} ;\n " +
150+ "lexer grammar L;\n " +
151+ "I : '0'..'9'+ {<writeln(\" I\" )} ;\n " +
152152 "WS : (' '|'\\ n') -> skip ;" ;
153153
154154 State state = execLexer (grammar , "34 34" , tempDir , actionTemplates );
@@ -163,17 +163,18 @@ public class TestActionTemplates {
163163 generated .getErrorMessage ());
164164 }
165165
166- @ Test void testInvalidMultilineActionTemplate (@ TempDir Path tempDir ) {
166+ @ Test
167+ void testInvalidMultilineActionTemplate (@ TempDir Path tempDir ) {
167168 writeActionTemplatesFile (tempDir , "writeln(s) ::= <<outStream.println(\" <s>\" );>>" );
168169
169170 String actionTemplates = tempDir + FileSeparator + "Java.stg" ;
170171 String grammarFile = tempDir + FileSeparator + "L.g4" ;
171172
172173 String grammar =
173- "lexer grammar L;\n " +
174- "I : '0'..'9'+ {\n " +
175- " <writeln(\" I\" )\n " +
176- "};\n " +
174+ "lexer grammar L;\n " +
175+ "I : '0'..'9'+ {\n " +
176+ " <writeln(\" I\" )\n " +
177+ "};\n " +
177178 "WS : (' '|'\\ n') -> skip ;" ;
178179
179180 State state = execLexer (grammar , "34 34" , tempDir , actionTemplates );
@@ -188,14 +189,15 @@ public class TestActionTemplates {
188189 generated .getErrorMessage ());
189190 }
190191
191- @ Test void testValidActionTemplate (@ TempDir Path tempDir ) {
192+ @ Test
193+ void testValidActionTemplate (@ TempDir Path tempDir ) {
192194 writeActionTemplatesFile (tempDir , "writeln(s) ::= <<outStream.println(\" <s>\" );>>" );
193195
194196 String actionTemplates = tempDir + FileSeparator + "Java.stg" ;
195197
196198 String grammar =
197- "lexer grammar L;\n " +
198- "I : '0'..'9'+ {<writeln(\" I\" )>} ;\n " +
199+ "lexer grammar L;\n " +
200+ "I : '0'..'9'+ {<writeln(\" I\" )>} ;\n " +
199201 "WS : (' '|'\\ n') -> skip ;" ;
200202
201203 State state = execLexer (grammar , "34 34" , tempDir , actionTemplates );
@@ -208,21 +210,22 @@ public class TestActionTemplates {
208210 "[@1,3:4='34',<1>,1:3]\n " +
209211 "[@2,5:4='<EOF>',<-1>,1:5]\n " ;
210212
211- assertInstanceOf (ExecutedState .class , state );
213+ assertInstanceOf (ExecutedState .class , state , state . getErrorMessage () );
212214
213215 assertEquals (expecting , ((ExecutedState ) state ).output );
214216 }
215217
216- @ Test void testValidMultilineActionTemplate (@ TempDir Path tempDir ) {
218+ @ Test
219+ void testValidMultilineActionTemplate (@ TempDir Path tempDir ) {
217220 writeActionTemplatesFile (tempDir , "writeln(s) ::= <<outStream.println(\" <s>\" );>>" );
218221
219222 String actionTemplates = tempDir + FileSeparator + "Java.stg" ;
220223
221224 String grammar =
222- "lexer grammar L;\n " +
223- "I : '0'..'9'+ {\n " +
224- " <writeln(\" I\" )>\n " +
225- "};\n " +
225+ "lexer grammar L;\n " +
226+ "I : '0'..'9'+ {\n " +
227+ " <writeln(\" I\" )>\n " +
228+ "};\n " +
226229 "WS : (' '|'\\ n') -> skip ;" ;
227230
228231 State state = execLexer (grammar , "34 34" , tempDir , actionTemplates );
@@ -235,12 +238,13 @@ public class TestActionTemplates {
235238 "[@1,3:4='34',<1>,1:3]\n " +
236239 "[@2,5:4='<EOF>',<-1>,1:5]\n " ;
237240
238- assertInstanceOf (ExecutedState .class , state );
241+ assertInstanceOf (ExecutedState .class , state , state . getErrorMessage () );
239242
240243 assertEquals (expecting , ((ExecutedState ) state ).output );
241244 }
242245
243- @ Test void testActionTemplateHeader (@ TempDir Path tempDir ) {
246+ @ Test
247+ void testActionTemplateHeader (@ TempDir Path tempDir ) {
244248 String actionTemplates =
245249 "normalizerImports() ::= <<\n " +
246250 "import java.text.Normalizer;\n " +
@@ -255,54 +259,55 @@ public class TestActionTemplates {
255259 String actionTemplatesFile = tempDir + FileSeparator + "Java.stg" ;
256260
257261 String grammar =
258- "lexer grammar L;\n " +
259- "@lexer::header {\n " +
260- "<normalizerImports()>\n " +
261- "}\n " +
262- "ID : (ID_START ID_CONTINUE* | '_' ID_CONTINUE+) { <setText(normalize(getText()))> } ;\n " +
263- "ID_START : [\\ p{XID_Start}] ;\n " +
264- "ID_CONTINUE: [\\ p{XID_Continue}] ;\n " +
262+ "lexer grammar L;\n " +
263+ "@lexer::header {\n " +
264+ "<normalizerImports()>\n " +
265+ "}\n " +
266+ "ID : (ID_START ID_CONTINUE* | '_' ID_CONTINUE+) { <setText(normalize(getText()))> } ;\n " +
267+ "ID_START : [\\ p{XID_Start}] ;\n " +
268+ "ID_CONTINUE: [\\ p{XID_Continue}] ;\n " +
265269 "WS : (' '|'\\ n') -> skip ;" ;
266270
267271 State state = execLexer (grammar , "This _is \ufb01 ne" , tempDir , actionTemplatesFile );
268272
269273 String expecting =
270- "[@0,0:3='This',<1>,1:0]\n " +
271- "[@1,5:7='_is',<1>,1:5]\n " +
272- "[@2,9:11='fine',<1>,1:9]\n " +
274+ "[@0,0:3='This',<1>,1:0]\n " +
275+ "[@1,5:7='_is',<1>,1:5]\n " +
276+ "[@2,9:11='fine',<1>,1:9]\n " +
273277 "[@3,12:11='<EOF>',<-1>,1:12]\n " ;
274278
275- assertInstanceOf (ExecutedState .class , state );
279+ assertInstanceOf (ExecutedState .class , state , state . getErrorMessage () );
276280
277281 assertEquals (expecting , ((ExecutedState ) state ).output );
278282 }
279283
280- @ Test void testActionTemplateSemanticPredicate (@ TempDir Path tempDir ) {
284+ @ Test
285+ void testActionTemplateSemanticPredicate (@ TempDir Path tempDir ) {
281286 String actionTemplates = "pred() ::= <<true>>" ;
282287
283288 writeActionTemplatesFile (tempDir , actionTemplates );
284289
285290 String actionTemplatesFile = tempDir + FileSeparator + "Java.stg" ;
286291
287292 String grammar =
288- "grammar P;\n " +
289- "file : atom EOF ;\n " +
290- "atom : scientific | { <pred()> }? variable ;\n " +
291- "variable: VARIABLE ;\n " +
292- "scientific: SCIENTIFIC_NUMBER ;\n " +
293+ "grammar P;\n " +
294+ "file : atom EOF ;\n " +
295+ "atom : scientific | { <pred()> }? variable ;\n " +
296+ "variable: VARIABLE ;\n " +
297+ "scientific: SCIENTIFIC_NUMBER ;\n " +
293298 "VARIABLE : VALID_ID_START VALID_ID_CHAR* ;\n " +
294- "SCIENTIFIC_NUMBER : NUMBER (E SIGN? UNSIGNED_INTEGER)? ;\n " +
295- "fragment VALID_ID_START : ('a' .. 'z') | ('A' .. 'Z') | '_' ;\n " +
296- "fragment VALID_ID_CHAR : VALID_ID_START | ('0' .. '9') ;\n " +
297- "fragment NUMBER : ('0' .. '9') + ('.' ('0' .. '9') +)? ;\n " +
298- "fragment UNSIGNED_INTEGER : ('0' .. '9')+ ;\n " +
299- "fragment E : 'E' | 'e' ;\n " +
300- "fragment SIGN : ('+' | '-') ;\n " +
299+ "SCIENTIFIC_NUMBER : NUMBER (E SIGN? UNSIGNED_INTEGER)? ;\n " +
300+ "fragment VALID_ID_START : ('a' .. 'z') | ('A' .. 'Z') | '_' ;\n " +
301+ "fragment VALID_ID_CHAR : VALID_ID_START | ('0' .. '9') ;\n " +
302+ "fragment NUMBER : ('0' .. '9') + ('.' ('0' .. '9') +)? ;\n " +
303+ "fragment UNSIGNED_INTEGER : ('0' .. '9')+ ;\n " +
304+ "fragment E : 'E' | 'e' ;\n " +
305+ "fragment SIGN : ('+' | '-') ;\n " +
301306 "WS : (' '|'\\ n') -> skip ;" ;
302307
303308 State state = execParser (grammar , "Bla" , tempDir , "file" , actionTemplatesFile );
304309
305- assertInstanceOf (ExecutedState .class , state );
310+ assertInstanceOf (ExecutedState .class , state , state . getErrorMessage () );
306311
307312 ExecutedState executedState = (ExecutedState ) state ;
308313
@@ -311,14 +316,40 @@ public class TestActionTemplates {
311316 assertEquals ("" , executedState .errors );
312317 }
313318
319+ @ Test
320+ void testValidActionTemplateInLibDir (@ TempDir Path tempDir , @ TempDir Path libDir ) {
321+ writeActionTemplatesFile (libDir , "writeln(s) ::= <<outStream.println(\" <s>\" );>>" );
322+
323+ String actionTemplates = "Java.stg" ;
324+
325+ String grammar =
326+ "lexer grammar L;\n " +
327+ "I : '0'..'9'+ {<writeln(\" I\" )>} ;\n " +
328+ "WS : (' '|'\\ n') -> skip ;" ;
329+
330+ State state = execLexer (grammar , "34 34" , tempDir , libDir , actionTemplates );
331+
332+ // Should have identical output to TestLexerActions.testActionExecutedInDFA
333+ String expecting =
334+ "I\n " +
335+ "I\n " +
336+ "[@0,0:1='34',<1>,1:0]\n " +
337+ "[@1,3:4='34',<1>,1:3]\n " +
338+ "[@2,5:4='<EOF>',<-1>,1:5]\n " ;
339+
340+ assertInstanceOf (ExecutedState .class , state , state .getErrorMessage ());
341+
342+ assertEquals (expecting , ((ExecutedState ) state ).output );
343+ }
344+
314345 void writeActionTemplatesFile (Path tempDir , String template ) {
315346 writeFile (tempDir .toString (), "Java.stg" , template );
316347 }
317348
318349 State execParser (String grammarStr , String input , Path tempDir , String startRule , String actionTemplates ) {
319350 RunOptions runOptions = createOptionsForJavaToolTests ("P.g4" , grammarStr , "PParser" , "PLexer" ,
320351 false , true , startRule , input ,
321- false , false , Stage .Execute , actionTemplates );
352+ false , false , Stage .Execute , null , actionTemplates );
322353 try (JavaRunner runner = new JavaRunner (tempDir , false )) {
323354 return runner .run (runOptions );
324355 }
@@ -327,7 +358,16 @@ State execParser(String grammarStr, String input, Path tempDir, String startRule
327358 State execLexer (String grammarStr , String input , Path tempDir , String actionTemplates ) {
328359 RunOptions runOptions = createOptionsForJavaToolTests ("L.g4" , grammarStr , null , "L" ,
329360 false , true , null , input ,
330- false , false , Stage .Execute , actionTemplates );
361+ false , false , Stage .Execute , null , actionTemplates );
362+ try (JavaRunner runner = new JavaRunner (tempDir , false )) {
363+ return runner .run (runOptions );
364+ }
365+ }
366+
367+ State execLexer (String grammarStr , String input , Path tempDir , Path libDir , String actionTemplates ) {
368+ RunOptions runOptions = createOptionsForJavaToolTests ("L.g4" , grammarStr , null , "L" ,
369+ false , true , null , input ,
370+ false , false , Stage .Execute , libDir .toString (), actionTemplates );
331371 try (JavaRunner runner = new JavaRunner (tempDir , false )) {
332372 return runner .run (runOptions );
333373 }
0 commit comments