aboutsummaryrefslogtreecommitdiff
path: root/testerrors.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testerrors.lua')
-rw-r--r--testerrors.lua701
1 files changed, 701 insertions, 0 deletions
diff --git a/testerrors.lua b/testerrors.lua
new file mode 100644
index 0000000..1c5bb9f
--- /dev/null
+++ b/testerrors.lua
@@ -0,0 +1,701 @@
1local re = require 'relabel'
2
3function testerror(repatt, msg)
4 msg = msg:match("^%s*(.-)%s*$") -- trim
5 local ok, err = pcall(function () re.compile(repatt) end)
6 assert(not ok)
7 if msg:match("^[^\n]*\n(.-)$") then
8 -- expecting a syntax error
9 err = err:match("^[^\n]*\n(.-)$") -- remove first line (filename)
10 err = err:gsub("[ \t]*\n", "\n") -- remove trailing spaces
11 -- if err ~= msg then
12 -- print(#err, #msg)
13 -- print('--')
14 -- print(err)
15 -- print('--')
16 -- print(msg)
17 -- print('--')
18 -- end
19 assert(err == msg)
20 else
21 -- expecting a non-syntax error
22 assert(err:match(msg))
23 end
24end
25
26-- testing NoPatt
27
28testerror([[~]], [[
29L1:C1: no pattern found
30~
31^
32]])
33
34testerror([[???]], [[
35L1:C1: no pattern found
36???
37^
38]])
39
40-- testing ExtraChars
41
42testerror([['p'~]], [[
43L1:C4: unexpected characters after the pattern
44'p'~
45 ^
46]])
47
48testerror([['p'?$?]], [[
49L1:C5: unexpected characters after the pattern
50'p'?$?
51 ^
52]])
53
54-- testing ExpPatt1
55
56testerror([['p' /{1}]], [[
57L1:C9: expected a pattern after '/' or the label(s)
58'p' /{1}
59 ^
60]])
61
62testerror([['p' /{1} /{2} 'q']], [[
63L1:C9: expected a pattern after '/' or the label(s)
64'p' /{1} /{2} 'q'
65 ^
66]])
67
68testerror([['p' /]], [[
69L1:C6: expected a pattern after '/' or the label(s)
70'p' /
71 ^
72]])
73
74testerror([['p' / / 'q']], [[
75L1:C6: expected a pattern after '/' or the label(s)
76'p' / / 'q'
77 ^
78]])
79
80-- testing ExpPatt2
81
82testerror([[&]], [[
83L1:C2: expected a pattern after '&'
84&
85 ^
86]])
87
88testerror([[& / 'p']], [[
89L1:C2: expected a pattern after '&'
90& / 'p'
91 ^
92]])
93
94testerror([['p' &]], [[
95L1:C6: expected a pattern after '&'
96'p' &
97 ^
98]])
99
100testerror([['p' / & / 'q']], [[
101L1:C8: expected a pattern after '&'
102'p' / & / 'q'
103 ^
104]])
105
106testerror([[&&]], [[
107L1:C3: expected a pattern after '&'
108&&
109 ^
110]])
111
112testerror([[!&]], [[
113L1:C3: expected a pattern after '&'
114!&
115 ^
116]])
117
118-- testing ExpPatt3
119
120testerror([[!]], [[
121L1:C2: expected a pattern after '!'
122!
123 ^
124]])
125
126testerror([[! / 'p']], [[
127L1:C2: expected a pattern after '!'
128! / 'p'
129 ^
130]])
131
132testerror([['p' !]], [[
133L1:C6: expected a pattern after '!'
134'p' !
135 ^
136]])
137
138testerror([['p' / ! / 'q']], [[
139L1:C8: expected a pattern after '!'
140'p' / ! / 'q'
141 ^
142]])
143
144testerror([[!!]], [[
145L1:C3: expected a pattern after '!'
146!!
147 ^
148]])
149
150testerror([[&!]], [[
151L1:C3: expected a pattern after '!'
152&!
153 ^
154]])
155
156-- testing ExpPatt4
157
158testerror([[()]], [[
159L1:C2: expected a pattern after '('
160()
161 ^
162]])
163
164testerror([[($$$)]], [[
165L1:C2: expected a pattern after '('
166($$$)
167 ^
168]])
169
170-- testing ExpPatt5
171
172testerror([[{: *** :}]], [[
173L1:C3: expected a pattern after ':'
174{: *** :}
175 ^
176]])
177
178testerror([[{:group: *** :}]], [[
179L1:C9: expected a pattern after ':'
180{:group: *** :}
181 ^
182]])
183
184testerror([[x <- {:x:}]], [[
185L1:C10: expected a pattern after ':'
186x <- {:x:}
187 ^
188]])
189
190-- testing ExpPatt6
191
192testerror([[{~~}]], [[
193L1:C3: expected a pattern after '{~'
194{~~}
195 ^
196]])
197
198testerror([[{ {~ } ~}]], [[
199L1:C5: expected a pattern after '{~'
200{ {~ } ~}
201 ^
202L1:C10: missing closing '}'
203{ {~ } ~}
204 ^
205]])
206
207testerror([[{~ ^_^ ~}]], [[
208L1:C3: expected a pattern after '{~'
209{~ ^_^ ~}
210 ^
211]])
212
213-- testing ExpPatt7
214
215testerror([[{||}]], [[
216L1:C3: expected a pattern after '{|'
217{||}
218 ^
219]])
220
221testerror([[{|@|}]], [[
222L1:C3: expected a pattern after '{|'
223{|@|}
224 ^
225]])
226
227-- testing ExpPatt8
228
229testerror([[S <-]], [[
230L1:C5: expected a pattern after '<-'
231S <-
232 ^
233]])
234
235testerror([[S <- 'p' T <-]], [[
236L1:C14: expected a pattern after '<-'
237S <- 'p' T <-
238 ^
239]])
240
241-- testing ExpPattOrClose
242
243testerror([[{0}]], [[
244L1:C2: expected a pattern or closing '}' after '{'
245{0}
246 ^
247]])
248
249testerror([[{ :'p': }]], [[
250L1:C2: expected a pattern or closing '}' after '{'
251{ :'p': }
252 ^
253]])
254
255-- testing ExpNum
256
257testerror([['p' ^ n]], [[
258L1:C6: expected a number after '^', '+' or '-' (no space)
259'p' ^ n
260 ^
261]])
262
263testerror([['p'^+(+1)]], [[
264L1:C5: expected a number after '^', '+' or '-' (no space)
265'p'^+(+1)
266 ^
267]])
268
269testerror([['p'^-/'q']], [[
270L1:C5: expected a number after '^', '+' or '-' (no space)
271'p'^-/'q'
272 ^
273]])
274
275-- testing ExpCap
276
277testerror([['p' -> {]], [[
278L1:C7: expected a string, number, '{}' or name after '->'
279'p' -> {
280 ^
281]])
282
283testerror([['p' -> {'q'}]], [[
284L1:C7: expected a string, number, '{}' or name after '->'
285'p' -> {'q'}
286 ^
287]])
288
289testerror([['p' -> / 'q']], [[
290L1:C7: expected a string, number, '{}' or name after '->'
291'p' -> / 'q'
292 ^
293]])
294
295testerror([['p' -> [0-9] ]], [[
296L1:C7: expected a string, number, '{}' or name after '->'
297'p' -> [0-9]
298 ^
299]])
300
301-- testing ExpName1
302
303testerror([['p' =>]], [[
304L1:C7: expected the name of a rule after '=>'
305'p' =>
306 ^
307]])
308
309testerror([['p' => 'q']], [[
310L1:C7: expected the name of a rule after '=>'
311'p' => 'q'
312 ^
313]])
314
315-- testing ExpName2
316
317testerror([['<' {:tag: [a-z]+ :} '>' '<' = '>']], [[
318L1:C31: expected the name of a rule after '=' (no space)
319'<' {:tag: [a-z]+ :} '>' '<' = '>'
320 ^
321]])
322
323testerror([['<' {:tag: [a-z]+ :} '>' '<' = tag '>']], [[
324L1:C31: expected the name of a rule after '=' (no space)
325'<' {:tag: [a-z]+ :} '>' '<' = tag '>'
326 ^
327]])
328
329-- testing ExpName3
330
331testerror([[<>]], [[
332L1:C2: expected the name of a rule after '<' (no space)
333<>
334 ^
335]])
336
337testerror([[<123>]], [[
338L1:C2: expected the name of a rule after '<' (no space)
339<123>
340 ^
341]])
342
343testerror([[< hello >]], [[
344L1:C2: expected the name of a rule after '<' (no space)
345< hello >
346 ^
347]])
348
349testerror([[<<S>>]], [[
350L1:C2: expected the name of a rule after '<' (no space)
351<<S>>
352 ^
353]])
354
355-- testing ExpLab1
356
357testerror([['p' /{} 'q']], [[
358L1:C7: expected at least one label after '{'
359'p' /{} 'q'
360 ^
361]])
362
363testerror([[%{ 'label' }]], [[
364L1:C3: expected at least one label after '{'
365%{ 'label' }
366 ^
367]])
368
369-- testing ExpLab2
370
371testerror([['p' /{1,2,3,} 'q']], [[
372L1:C13: expected a label after the comma
373'p' /{1,2,3,} 'q'
374 ^
375]])
376
377testerror([[%{ a,,b,,c }]], [[
378L1:C6: expected a label after the comma
379%{ a,,b,,c }
380 ^
381]])
382
383-- testing ExpNameOrLab
384
385testerror([[% s]], [[
386L1:C2: expected a name or label after '%' (no space)
387% s
388 ^
389]])
390
391testerror([[% {1}]], [[
392L1:C2: expected a name or label after '%' (no space)
393% {1}
394 ^
395]])
396
397-- testing ExpItem
398
399testerror([[
400"p" [
401abc
402] "q"
403]], [[
404L1:C6: expected at least one item after '[' or '^'
405"p" [
406 ^
407]])
408
409-- testing MisClose1
410
411testerror([[('p' ('q' / 'r')]], [[
412L1:C17: missing closing ')'
413('p' ('q' / 'r')
414 ^
415]])
416
417-- testing MisClose2
418
419-- two errors are reported due to the ignore strategy
420testerror([[{: group: 'p' :}]], [[
421L1:C9: missing closing ':}'
422{: group: 'p' :}
423 ^
424L1:C9: unexpected characters after the pattern
425{: group: 'p' :}
426 ^
427]])
428
429testerror([[S <- {: 'p' T <- 'q']], [[
430L1:C12: missing closing ':}'
431S <- {: 'p' T <- 'q'
432 ^
433]])
434
435-- testing MisClose3
436
437testerror([['p' {~ ('q' 'r') / 's']], [[
438L1:C23: missing closing '~}'
439'p' {~ ('q' 'r') / 's'
440 ^
441]])
442
443-- testing MisClose4
444
445-- two errors are reported due to the ignore strategy
446testerror([['p' {| 'q' / 'r' }]], [[
447L1:C17: missing closing '|}'
448'p' {| 'q' / 'r' }
449 ^
450L1:C18: unexpected characters after the pattern
451'p' {| 'q' / 'r' }
452 ^
453]])
454
455-- testing MisClose5
456
457testerror([[{ 'p' ]], [[
458L1:C6: missing closing '}'
459{ 'p'
460 ^
461]])
462
463-- testing MisClose6
464
465testerror([[<patt]], [[
466L1:C6: missing closing '>'
467<patt
468 ^
469]])
470
471testerror([[<insert your name here>]], [[
472L1:C8: missing closing '>'
473<insert your name here>
474 ^
475]])
476
477-- testing MisClose7
478
479testerror([['{' %{ a, b '}']], [[
480L1:C12: missing closing '}'
481'{' %{ a, b '}'
482 ^
483]])
484
485-- testing MisClose8
486
487testerror([[[]], [[
488L1:C1: missing closing ']'
489[
490^
491]])
492
493testerror([[[^]], [[
494L1:C1: missing closing ']'
495[^
496^
497]])
498
499testerror([[[] ]], [[
500L1:C1: missing closing ']'
501[]
502^
503]])
504
505testerror([[[^] ]], [[
506L1:C1: missing closing ']'
507[^]
508^
509]])
510
511testerror([[[_-___-_|]], [[
512L1:C1: missing closing ']'
513[_-___-_|
514^
515]])
516
517-- testing MisTerm1
518
519testerror([['That is the question...]], [[
520L1:C1: missing terminating single quote
521'That is the question...
522^
523]])
524
525-- testing MisTerm2
526
527testerror([[Q <- "To be or not to be...]], [[
528L1:C6: missing terminating double quote
529Q <- "To be or not to be...
530 ^
531]])
532
533-- testing error recovery, more complex grammars (multiline),
534-- and pointer positions in error recovery
535
536testerror([[&'p'/&/!/'p'^'q']], [[
537L1:C7: expected a pattern after '&'
538&'p'/&/!/'p'^'q'
539 ^
540L1:C9: expected a pattern after '!'
541&'p'/&/!/'p'^'q'
542 ^
543L1:C14: expected a number after '^', '+' or '-' (no space)
544&'p'/&/!/'p'^'q'
545 ^
546]])
547
548testerror([[
549 A <- 'a' (B 'b'
550 B <- 'x' / !
551 C <- 'c'
552]], [[
553L1:C18: missing closing ')'
554 A <- 'a' (B 'b'
555 ^
556L2:C15: expected a pattern after '!'
557 B <- 'x' / !
558 ^
559]])
560
561testerror([['a' / &@ ('c' / 'd')]], [[
562L1:C8: expected a pattern after '&'
563'a' / &@ ('c' / 'd')
564 ^
565]])
566
567testerror([['x' / & / 'y']], [[
568L1:C8: expected a pattern after '&'
569'x' / & / 'y'
570 ^
571]])
572
573testerror([[&/'p'/!/'q']], [[
574L1:C2: expected a pattern after '&'
575&/'p'/!/'q'
576 ^
577L1:C8: expected a pattern after '!'
578&/'p'/!/'q'
579 ^
580]])
581
582testerror([['p'//'q']], [[
583L1:C5: expected a pattern after '/' or the label(s)
584'p'//'q'
585 ^
586]])
587
588testerror([[
589 S <- 'forgot to close / T
590 T <- 'T' & / 't'
591]], [[
592L1:C8: missing terminating single quote
593 S <- 'forgot to close / T
594 ^
595L2:C13: expected a pattern after '&'
596 T <- 'T' & / 't'
597 ^
598]])
599
600testerror([[
601 S <- [a-z / T
602 T <- 'x' / & / 'y'
603]], [[
604L1:C8: missing closing ']'
605 S <- [a-z / T
606 ^
607L2:C15: expected a pattern after '&'
608 T <- 'x' / & / 'y'
609 ^
610]])
611
612testerror([[
613 S <- ('p' -- comment
614]], [[
615L1:C12: missing closing ')'
616 S <- ('p' -- comment
617 ^
618]])
619
620-- an unfortunate second error exists because we don't know
621-- what's part of the quotation
622testerror([[
623 X <- ('p / Q (R
624 / S))
625 Q <- 'q'
626 R <- 'r'
627 S <- 's'
628]], [[
629L1:C9: missing terminating single quote
630 X <- ('p / Q (R
631 ^
632L2:C9: unexpected characters after the pattern
633 / S))
634 ^
635]])
636
637testerror([[
638 A <- 'A' /{'lab'} B / !
639
640 B <- %{1, 2 3} 'b' / '6' & / 'B'
641
642 C <- A^B
643]], [[
644L1:C14: expected at least one label after '{'
645 A <- 'A' /{'lab'} B / !
646 ^
647L1:C26: expected a pattern after '!'
648 A <- 'A' /{'lab'} B / !
649 ^
650L3:C15: missing closing '}'
651 B <- %{1, 2 3} 'b' / '6' & / 'B'
652 ^
653L3:C29: expected a pattern after '&'
654 B <- %{1, 2 3} 'b' / '6' & / 'B'
655 ^
656L5:C10: expected a number after '^', '+' or '-' (no space)
657 C <- A^B
658 ^
659]])
660
661testerror([['p'/{1/'q'/&]], [[
662L1:C7: missing closing '}'
663'p'/{1/'q'/&
664 ^
665L1:C13: expected a pattern after '&'
666'p'/{1/'q'/&
667 ^
668]])
669
670-- testing non-syntax errors
671
672testerror([[
673 A <- %nosuch %def
674 A <- 'A again'
675 A <- 'and again'
676]], [[
677name 'nosuch' undefined
678]])
679
680testerror([[names not in grammar]], [[
681rule 'names' used outside a grammar
682]])
683
684testerror([[
685 A <- %nosuch %def
686 A <- 'A again'
687 A <- 'and again'
688]], [[
689name 'nosuch' undefined
690]])
691
692-- the non-syntax error should not be reported
693-- since there is a syntax error
694testerror([[ A <- %nosuch ('error' ]], [[
695L1:C23: missing closing ')'
696 A <- %nosuch ('error'
697 ^
698]])
699
700
701print 'OK'