-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_lua.py
More file actions
1054 lines (895 loc) · 50.3 KB
/
test_lua.py
File metadata and controls
1054 lines (895 loc) · 50.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2020 Kevin Stevens
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import pathlib
import pytest
import pda_lua
DEBUG_LEVEL = 0
COUNTER = 0
def reset_counter():
pass
global COUNTER
COUNTER = 0
def end():
pass
raise Exception('Intentional test failure')
def parse_lua(s, *args, **kwargs):
"""
Helper function that parses something with the appropriate debug_level set
"""
global COUNTER
COUNTER += 1
print(COUNTER)
if isinstance(s, str):
s = s.encode('cp1252')
pda_lua.Lua_2PDA().parse(s, *args, **kwargs, debug_level=DEBUG_LEVEL)
def iter_whitespace():
"""
Helper function to iterate over various whitespace.
Yields: ws, ws_sep
Where ws is whitespace (possibly empty) and ws_sep is the same as
ws, unless ws is empty, in which case it will be one space.
"""
def make_both(ws):
return ws, (ws if ws else ' ')
yield make_both('')
yield make_both(' ')
yield make_both('--\n')
yield make_both(' --\n ')
yield make_both('--[[comment]]')
yield make_both(' --[[comment]] ')
# Add tests corresponding to files in the Lua test suite.
# You can have these be skipped by running `pytest -m "not luasuite"`
def make_test_suite_test(path):
"""
Factory function for creating a test case that parses a Lua file at
the given path (pathlib.Path) and runs it through the 2PDA
"""
@pytest.mark.luasuite
def test():
parse_lua(path.read_bytes())
return test
for p in pathlib.Path('lua-5.3/testes/').glob('*.lua'):
globals()[f'test_lua_test_suite_{p.stem}'] = make_test_suite_test(p)
del make_test_suite_test
def helper_test_multiline_comment_or_long_string(prefix):
"""
Helper function to test parsing multiline comments or long strings.
prefix should be a string that will be prefixed onto all test cases.
"""
def check_closed(equals):
"""
Perform a check to see if an --[equals[ multiline comment or
[equals[ long-form string has been closed as it should have been
We do this by adding a comment of the form
--[other[ ]equals] ]other]
(where "other" is some other amount of equals)
If --[equals[ had been properly closed, this is a perfectly
valid comment. If not, it closes the original comment or string
and leaves some unparseable "]other]" garbage afterwards,
causing a syntax error
"""
other = '==' if equals == '====' else '===='
return f' --[{other}[ ]{equals}] ]{other}]'
parse_lua(prefix + '[[multiline\rcomment\n]not yet]]' + check_closed(''))
parse_lua(prefix + '[====[multiline\rcomment\n]=not yet]====]' + check_closed('===='))
parse_lua(prefix + '[====[multiline\rcomment\n]]====]' + check_closed('===='))
parse_lua(prefix + '[[]]' + check_closed(''))
parse_lua(prefix + '[=[]=]' + check_closed('='))
# 10 opening '='s, 11 ending ones -- should be counted as equal with
# the current PDA implementation
parse_lua(prefix + '[==========[multiline\rcomment\n]]===========]' + check_closed('=========='))
# Non-ASCII characters
parse_lua((prefix + '[=[ ツ ]]=]' + check_closed('=')).encode('utf-8'))
# Some things that should *not* parse
with pytest.raises(Exception):
parse_lua(prefix + '[[multiline\rcomment\n]=]' + check_closed(''))
with pytest.raises(Exception):
parse_lua(prefix + '[=[multiline\rcomment\n]]' + check_closed('='))
with pytest.raises(Exception):
parse_lua(prefix + '[=[multiline\rcomment\n]==]' + check_closed('='))
with pytest.raises(Exception):
parse_lua(prefix + '[=[multiline\rcomment\n]]]' + check_closed('='))
with pytest.raises(Exception):
parse_lua(prefix + '[=[multiline\rcomment\n]]==]' + check_closed('='))
def helper_test_function_bodies(prefix):
"""
Helper function to test parsing function statements or function
expressions.
prefix should be a string that will be prefixed onto all test cases.
"""
for ws, ws_sep in iter_whitespace():
parse_lua(prefix + '( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(prefix + '( a ) ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(prefix + '( ... ) do ; ::x::end; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(prefix + '( a , b , ... ) end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(prefix + '( a , bb , ccc , ... ) end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(prefix + '( .. )_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(prefix + '( a , )_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(prefix + '( , a )_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(prefix + '( a_b ) end_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(prefix + '( a ... ) end_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(prefix + '( a , ... , c )_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(prefix + '( ) end_end_'.replace(' ', ws).replace('_', ws_sep))
def test_bang():
"""
Test that "The first line in the file is ignored if it starts with a #."
"""
reset_counter()
# Single-line
parse_lua('#shebang line\ndo --\n end')
with pytest.raises(Exception):
parse_lua('#shebang line\noh no invalid syntax')
end()
def test_comment():
"""
Test parsing comments
"""
reset_counter()
# Single-line
parse_lua(' --this is a comment\ndo end')
parse_lua('do --\n end')
with pytest.raises(Exception):
parse_lua(' --this is a comment\noops the comment ended')
helper_test_multiline_comment_or_long_string('--')
# A parsing error when trying to read the start of a multiline
# comment should cause it to read the rest of the line as a
# single-line comment instead
parse_lua('do --[\n end')
parse_lua('do --[=\n end')
parse_lua('do --[==\n end')
end()
def test_name():
"""
Test name parsing
"""
reset_counter()
parse_lua('a = 1;')
parse_lua('abc123 = 1;')
parse_lua('_abc_123_ = 1;')
with pytest.raises(Exception):
parse_lua('123abc = 1;')
end()
def test_semicolon():
"""
Test the semicolon statement
"""
reset_counter()
parse_lua(';')
parse_lua('; ;;; ; ;; ; ;;;;;;; ; ;; ')
end()
def test_assignment_statement():
"""
Test assignment statements
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
for exp in ['nil', '1', '-23', 'd', 'a.b(c)[d]']: # important to check that negative numbers work
parse_lua(f'a = {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'a . b = {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'a , bb . cc = {exp} , {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'a , bb , ccc . ddd = {exp} , {exp} , {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(f'a {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(f'a , b {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(f'a , if = {exp} , {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a , b = if ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a , b , c ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a , b , c , ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a , b , c = ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_function_call_statement():
"""
Test parsing function-call statements
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
# ======== Var ========
parse_lua('a = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # (none)
parse_lua('a [ -23 ] = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # [exp]
parse_lua('a . b = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # .name
parse_lua('( a ) . c = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # starting with (
with pytest.raises(Exception):
parse_lua('a : b = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name, with no args
with pytest.raises(Exception):
parse_lua('a : b ( 1 , 2 ) = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name (args)
with pytest.raises(Exception):
parse_lua("a : b 'arg' = 1 ;_".replace(' ', ws).replace('_', ws_sep)) # :name 'string argument'
with pytest.raises(Exception):
parse_lua('a : b "arg" = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name "string argument"
with pytest.raises(Exception):
parse_lua('a : b [[arg]] = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name [[string argument]]
with pytest.raises(Exception):
parse_lua('a : b [==[arg]==] = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name [==[string argument]==]
with pytest.raises(Exception):
parse_lua('a : b { 1 , 2 ; 3 } = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name {table constructor argument}
with pytest.raises(Exception):
parse_lua('a ( 1 , 2 ) = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # (args)
with pytest.raises(Exception):
parse_lua("a 'arg' = 1 ;_".replace(' ', ws).replace('_', ws_sep)) # 'string argument'
with pytest.raises(Exception):
parse_lua('a "arg" = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # "string argument"
with pytest.raises(Exception):
parse_lua('a [[ arg ]] = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # [[string argument]]
with pytest.raises(Exception):
parse_lua('a [==[arg]==] = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # [==[string argument]==]
with pytest.raises(Exception):
parse_lua('a { 1 , 2 ; 3 } = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # {table constructor argument}
with pytest.raises(Exception):
parse_lua('( a ) = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # starting with (, with nothing following
# Test chaining multiple parts
parse_lua('a ( b ) . c [ e ] "f" [[g]] [ h . i ] = 1 ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a ( b ) . c [ e ] "f" [[g]] [ h . i ] ( ) = 1 ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a [=[b]=] [ c ] \'d\' { e } . f . g : h ( i ) . j = 1 ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a [=[b]=] [ c ] \'d\' { e } . f . g : h ( i ) = 1 ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a [=[b]=] [ c ] \'d\' { e } . f . g : h ( i ) "j" = 1 ;_'.replace(' ', ws).replace('_', ws_sep))
# ======== Prefixexp ========
for exp in ['nil', '1', '-23']: # important to check that negative numbers work
parse_lua( 'a = a ;_'.replace(' ', ws).replace('_', ws_sep)) # (none)
parse_lua(f'a = a [ {exp} ] ;_'.replace(' ', ws).replace('_', ws_sep)) # [exp]
parse_lua( 'a = a . b ;_'.replace(' ', ws).replace('_', ws_sep)) # .name
parse_lua(f'a = a : b ( {exp} , {exp} ) ;_'.replace(' ', ws).replace('_', ws_sep)) # :name (args)
parse_lua( "a = a : b 'arg' ;_".replace(' ', ws).replace('_', ws_sep)) # :name 'string argument'
parse_lua( 'a = a : b "arg" ;_'.replace(' ', ws).replace('_', ws_sep)) # :name "string argument"
parse_lua( 'a = a : b [[arg]] ;_'.replace(' ', ws).replace('_', ws_sep)) # :name [[string argument]]
parse_lua( 'a = a : b [==[arg]==] ;_'.replace(' ', ws).replace('_', ws_sep)) # :name [==[string argument]==]
parse_lua(f'a = a : b {{ {exp} , {exp} ; {exp} }} ;_'.replace(' ', ws).replace('_', ws_sep)) # :name {table constructor argument}
parse_lua(f'a = a ( {exp} , {exp} ) ;_'.replace(' ', ws).replace('_', ws_sep)) # (args)
parse_lua( "a = a 'arg' ;_".replace(' ', ws).replace('_', ws_sep)) # 'string argument'
parse_lua( 'a = a "arg" ;_'.replace(' ', ws).replace('_', ws_sep)) # "string argument"
parse_lua( 'a = a [[arg]] ;_'.replace(' ', ws).replace('_', ws_sep)) # [[string argument]]
parse_lua( 'a = a [==[arg]==] ;_'.replace(' ', ws).replace('_', ws_sep)) # [==[string argument]==]
parse_lua(f'a = a {{ {exp} , {exp} ; {exp} }} ;_'.replace(' ', ws).replace('_', ws_sep)) # {table constructor argument}
parse_lua( 'a = ( a ) ;_'.replace(' ', ws).replace('_', ws_sep)) # starting with (
parse_lua( 'a = ( a ) . b ;_'.replace(' ', ws).replace('_', ws_sep)) # starting with (, with something else following
with pytest.raises(Exception):
parse_lua( 'a = a : b ;_'.replace(' ', ws).replace('_', ws_sep)) # :name, with no args
with pytest.raises(Exception):
parse_lua( 'a = ( a , b ) ;_'.replace(' ', ws).replace('_', ws_sep)) # this should not be read as a function call
# Test chaining multiple parts
parse_lua('a = a ( b ) . c [ e ] "f" [[g]] [ h . i ] ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = a ( b ) . c [ e ] "f" [[g]] [ h . i ] : j ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = a [=[b]=] [ c ] \'d\' { e } . f . g : h ( i ) . j ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = a [=[b]=] [ c ] \'d\' { e } . f . g : h ( i ) : j ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_label_statement():
"""
Test the label statement
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
parse_lua(':: x :: ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(';; :: abcdefg :: ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('do ;; :: abcdefg :: ;; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(':: :: ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(':: 33 :: ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(':: if :: ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(':: x : ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(': x :: ;_'.replace(' ', ws).replace('_', ws_sep))
# Test labels following all kinds of expressions where it could
# be mistaken for the start of a "a:b()" function call, including...
# ...assignment statement:
parse_lua('a = b :: label :: ;_'.replace(' ', ws).replace('_', ws_sep))
# ...local assignment statement:
parse_lua('local a = b :: label :: ;_'.replace(' ', ws).replace('_', ws_sep))
# ...function call statement:
parse_lua('b ( ) :: label :: ;_'.replace(' ', ws).replace('_', ws_sep))
# ...repeat-until loop statement:
parse_lua('repeat ; until_a :: label :: ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_break_statement():
"""
Test parsing the break statement
"""
reset_counter()
parse_lua('break')
for ws, ws_sep in iter_whitespace():
parse_lua('do break end'.replace(' ', ws).replace('_', ws_sep))
end()
def test_goto_statement():
"""
Test parsing goto statements
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
parse_lua('goto_x ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('goto_xyz ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('goto_5 ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('goto ( x ) ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_do_statement():
"""
Test parsing do-end blocks
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
parse_lua('do end'.replace(' ', ws_sep))
parse_lua('do do end end'.replace(' ', ws_sep))
parse_lua('do end do end'.replace(' ', ws_sep))
parse_lua('do ;; ;; do ;; end ; end ;;;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('do_end_end ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_while_statement():
"""
Test parsing while statements
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
parse_lua('while_nil_do_end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('while_true_do ; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('while_-23_do ;;; do_end_end_do_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('while_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('while_do_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('while ; do ; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('while_true_then_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('while_..._do_else_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('while_nil_do_end_end ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_repeat_statement():
"""
Test parsing repeat statements
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
parse_lua('repeat_until_-23 ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('repeat ;;; do_end_until_..._do_end ;_'.replace(' ', ws).replace('_', ws_sep))
# Test that it handles the "expression parser tries to read 'and' but fails" case
parse_lua('repeat_until_5_andz ( nil , nil ) do_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('repeat_nil ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('repeat_until_do_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('repeat ; until ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('repeat_then ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('repeat_else_until ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('repeat ; until_false_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('repeat ; until_nil_until_nil ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_if_statement():
"""
Test parsing if statements
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
parse_lua('if_true_then_end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('if_..._then_elseif_true_then_else_end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('if_-23_then ; elseif_true_then ; else_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('if_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('if_then_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('if_nil_then_elseif_then_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('if_nil ; then_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('if_nil_then_elseif_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('else ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('elseif ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('if_nil_then_end_end ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_numerical_for_loop_statement():
"""
Test parsing "numerical" for-loop statements, i.e.
stat ::= for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
parse_lua('for_a = b , c_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('for_a = b , c , d_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('for_a = -1 , -2 , -3_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('for_a = b do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('for_a = b , c , d , e_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('for_true = b_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('for_a, b = b_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('for_a = b_do ;;; end_end ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_generic_for_loop_statement():
"""
Test parsing "generic" for-loop statements, i.e.
stat ::= for namelist in explist do block end
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
parse_lua('for_a_in_b_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('for_a , b , c_in_d , e , f_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('for_a_in_nil , -23 , true , b . c [ e ] { f } "g" ( h ) do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('for_a_in_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('for_in_b_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('for_true_in_b_do ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('for_a_in_b_do ;;; end_end ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_function_statement():
"""
Test parsing function statements
"""
reset_counter()
helper_test_function_bodies('function x.y.z:a')
for ws, ws_sep in iter_whitespace():
# Test various combinations of .'s and :'s
parse_lua('function_x ( ) ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('function_x . y ( ) ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('function_x : y ( ) ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('function_x . y . z ( ) ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('function_x . y : z ( ) ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('function_x . y . z : a ( ) ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
# Test functions with multi-character names
parse_lua('function xyz123abc(a) ;;; end ')
# Test functions with invalid names
with pytest.raises(Exception):
parse_lua('function () end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('function_nil ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('function_end ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('function_3 ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('function_a : b : c ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_local_function_statement():
"""
Test parsing local-function statements
"""
reset_counter()
helper_test_function_bodies('local function x')
for ws, ws_sep in iter_whitespace():
# Basic test
parse_lua('local_function_xyz123abc ( a ) ;;; end ;_'.replace(' ', ws).replace('_', ws_sep))
# Test local functions with invalid names
with pytest.raises(Exception):
parse_lua('local_function ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('local_function_nil ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('local_function_end ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('local_function_3 ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('local_function_a . b ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('local_function_a : b ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('local_function_a_b ( ) end ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_local_assignment_statement():
"""
Test parsing local-assignment statements
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
for exp in ['nil', '1', '-23', 'a.b(c)[d]']: # important to check that negative numbers work
parse_lua(f'local_a ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'local_a ; local_b ; local_c ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'local_a_local_b_local_c ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'local_a = {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'local_a , bb = {exp} , {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'local_a , bb , ccc = {exp} , {exp} , {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(f'local_a_{exp} ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(f'local_a , b_{exp} ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(f'local_a , {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(f'local_a , b , {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(f'local_a , if = {exp} , {exp} ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('local_a , b = if ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('local_a , b , c , ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('local_a , b , c = ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('local_a_b = ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_return_statement():
"""
Test parsing return statements
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
for semicolon in ['_', ';']:
for explist in ['_', '_nil_', '_nil , nil_', '_nil , nil , nil_', '_-23_']:
parse_lua(f'return {explist} {semicolon}'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'function_x ( ) return {explist} {semicolon} end_do_end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'do_return {explist} {semicolon} end_do_end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'if_nil_then_return {explist} {semicolon} else_do_end_end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'if_nil_then_return {explist} {semicolon} elseif_nil_then_do_end_end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'repeat_return {explist} {semicolon} until_nil ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('function_x ( ) return_nil , end_do_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('function_x ( ) return_nil , ; end_do_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('function_x ( ) return , ; end_do_end ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('return , ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('function_x ( ) return_nil_do_end_end_do_end;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_var_and_prefixexp():
"""
Test parsing "var"s and "prefixexp"s (from the Lua grammar)
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
# ======== Var ========
parse_lua('a = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # (none)
parse_lua('a [ -23 ] = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # [exp]
parse_lua('a . b = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # .name
with pytest.raises(Exception):
parse_lua('a : b = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name, with no args
with pytest.raises(Exception):
parse_lua('a : b ( 1 , 2 ) = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name (args)
with pytest.raises(Exception):
parse_lua("a : b 'arg' = 1 ;_".replace(' ', ws).replace('_', ws_sep)) # :name 'string argument'
with pytest.raises(Exception):
parse_lua('a : b "arg" = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name "string argument"
with pytest.raises(Exception):
parse_lua('a : b [[arg]] = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name [[string argument]]
with pytest.raises(Exception):
parse_lua('a : b [==[arg]==] = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name [==[string argument]==]
with pytest.raises(Exception):
parse_lua('a : b { 1 , 2 ; 3 } = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # :name {table constructor argument}
with pytest.raises(Exception):
parse_lua('a ( 1 , 2 ) = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # (args)
with pytest.raises(Exception):
parse_lua('a ( ) = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # ()
with pytest.raises(Exception):
parse_lua("a 'arg' = 1 ;_".replace(' ', ws).replace('_', ws_sep)) # 'string argument'
with pytest.raises(Exception):
parse_lua('a "arg" = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # "string argument"
with pytest.raises(Exception):
parse_lua('a [[ arg ]] = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # [[string argument]]
with pytest.raises(Exception):
parse_lua('a [==[arg]==] = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # [==[string argument]==]
with pytest.raises(Exception):
parse_lua('a { 1 , 2 ; 3 } = 1 ;_'.replace(' ', ws).replace('_', ws_sep)) # {table constructor argument}
# Test chaining multiple parts
parse_lua('a ( b ) . c [ e ] "f" [[g]] [ h . i ] = 1 ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a ( b ) . c [ e ] "f" [[g]] [ h . i ] ( ) = 1 ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a [=[b]=] [ c ] \'d\' { e } . f . g : h ( i ) . j = 1 ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a [=[b]=] [ c ] \'d\' { e } . f . g : h ( i ) = 1 ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a [=[b]=] [ c ] \'d\' { e } . f . g : h ( i ) "j" = 1 ;_'.replace(' ', ws).replace('_', ws_sep))
# ======== Prefixexp ========
for exp in ['nil', '1', '-23']: # important to check that negative numbers work
parse_lua( 'a = a ;_'.replace(' ', ws).replace('_', ws_sep)) # (none)
parse_lua(f'a = a [ {exp} ] ;_'.replace(' ', ws).replace('_', ws_sep)) # [exp]
parse_lua( 'a = a . b ;_'.replace(' ', ws).replace('_', ws_sep)) # .name
parse_lua(f'a = a : b ( {exp} , {exp} ) ;_'.replace(' ', ws).replace('_', ws_sep)) # :name (args)
parse_lua( "a = a : b 'arg' ;_".replace(' ', ws).replace('_', ws_sep)) # :name 'string argument'
parse_lua( 'a = a : b "arg" ;_'.replace(' ', ws).replace('_', ws_sep)) # :name "string argument"
parse_lua( 'a = a : b [[arg]] ;_'.replace(' ', ws).replace('_', ws_sep)) # :name [[string argument]]
parse_lua( 'a = a : b [==[arg]==] ;_'.replace(' ', ws).replace('_', ws_sep)) # :name [==[string argument]==]
parse_lua(f'a = a : b {{ {exp} , {exp} ; {exp} }} ;_'.replace(' ', ws).replace('_', ws_sep)) # :name {table constructor argument}
parse_lua(f'a = a ( {exp} , {exp} ) ;_'.replace(' ', ws).replace('_', ws_sep)) # (args)
parse_lua( "a = a 'arg' ;_".replace(' ', ws).replace('_', ws_sep)) # 'string argument'
parse_lua( 'a = a "arg" ;_'.replace(' ', ws).replace('_', ws_sep)) # "string argument"
parse_lua( 'a = a [[arg]] ;_'.replace(' ', ws).replace('_', ws_sep)) # [[string argument]]
parse_lua( 'a = a [==[arg]==] ;_'.replace(' ', ws).replace('_', ws_sep)) # [==[string argument]==]
parse_lua(f'a = a {{ {exp} , {exp} ; {exp} }} ;_'.replace(' ', ws).replace('_', ws_sep)) # {table constructor argument}
with pytest.raises(Exception):
parse_lua( 'a = a : b ;_'.replace(' ', ws).replace('_', ws_sep)) # :name, with no args
# Test chaining multiple parts
parse_lua('a = a ( b ) . c [ e ] "f" [[g]] [ h . i ] ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = a ( b ) . c [ e ] "f" [[g]] [ h . i ] : j ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = a [=[b]=] [ c ] \'d\' { e } . f . g : h ( i ) . j ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = a [=[b]=] [ c ] \'d\' { e } . f . g : h ( i ) : j ;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_singleton_expression():
"""
Test parsing expressions "nil", "false", "true", and "..."
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
for semicolon in ['_', ';']:
for exp in ['_nil', '_false', '_true', '...']:
parse_lua(f'do_return {exp}{semicolon}end_do_end ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(f'a = {exp} , {exp} {semicolon}'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = ._'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = .._'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = .;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = ..;_'.replace(' ', ws).replace('_', ws_sep))
end()
def test_numeral_expression():
"""
Test parsing numeral expressions
"""
reset_counter()
# There are a lot of possible numbers, and it's easy to miss some
# cases, so we'll generate a representative case for pretty much
# every situation.
# Make some lists of digit sequences.
# "0" is handled specially in the parser for the purposes of detecting
# "0x", so it's important to test sequences beginning with that.
# Also, be sure these are lowercase, since uppercase versions are tested automatically.
# Also, ".0" and "0." are both valid ways of writing "0.0" but you
# *can't* write just "0e", so only include the empty string in the former
# two situations
decDigitSequences = ['0', '01', '012', '1', '123']
hexDigitSequences = decDigitSequences + ['e', 'f', '1e2', '0a1b2c']
# Test both hex and decimal...
for isHex in [False, True]:
digitSequences = (hexDigitSequences if isHex else decDigitSequences)
# First few digits (which can be empty)
for leadingDigits in digitSequences + ['']:
# Optional fractional part (which can be just a "." with no following digits,
# but only if the leading digits are nonempty)
fractionalParts = ['.' + s for s in digitSequences]
if leadingDigits: fractionalParts += ['', '.']
for fractionalPart in fractionalParts:
# Sign for the optional exponent part
for expSign in ['', '-', '+']:
# Optional exponential part (which is in decimal no matter what)
expSep = 'p' if isHex else 'e'
for exponentPart in [''] + [expSep + expSign + s for s in decDigitSequences]:
# Construct the number, in both lowercase and uppercase
expL = ('0x' if isHex else '') + leadingDigits + fractionalPart + exponentPart
expU = expL.upper()
# Try to parse both types
parse_lua(f'a = {expL} ; ')
parse_lua(f'a = {expU} ; ')
with pytest.raises(Exception):
parse_lua('a = 0D2 ; ')
with pytest.raises(Exception):
parse_lua('a = 5D2 ; ')
with pytest.raises(Exception):
parse_lua('a = 0x ; ')
with pytest.raises(Exception):
parse_lua('a = 12e ; ')
with pytest.raises(Exception):
parse_lua('a = 0x34p ; ')
with pytest.raises(Exception):
parse_lua('a = 0x34pFE ; ')
with pytest.raises(Exception):
parse_lua('a = . ; ')
with pytest.raises(Exception):
parse_lua('a = .e5 ; ')
# Note: llex.c mentions "3-4" and "0xe+1" as test cases that should be
# parsed as expressions rather than single numbers. There's no way to
# test for this, though, since, in every context where a number can be
# used, an expression would be also accepted instead.
# Additional tests from Lua test suite (literals.lua):
with pytest.raises(Exception):
parse_lua('a = 0xe- ; ')
with pytest.raises(Exception):
parse_lua('a = 0xep-p ; ')
end()
def test_string_expression():
"""
Test parsing string expressions
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
if '\n' in ws: continue # causes too many problems
# Test short-form strings
for quote, opposite_quote in [('"' "'"), ("'" '"')]:
parse_lua(f'a = {quote} hello {opposite_quote} world {quote} ;_'.replace(' ', ws).replace('_', ws_sep))
# Test simple escapes
for esc in ('abfnrtv' '\\' '"' "'" '\n'):
parse_lua(f'a = "hello\\{esc}world" ;_'.replace(' ', ws).replace('_', ws_sep))
# \z: skips following whitespace, including newlines
if '--' not in ws:
parse_lua(f'a = "hello \\z \t \r \n world" ;_'.replace(' ', ws).replace('_', ws_sep))
# \xXX hex escapes
parse_lua(r'a = "hello \x00 \xfF \x78 world" ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(r'a = "hello \x2 world" ;_'.replace(' ', ws).replace('_', ws_sep))
# \d, \dd, \ddd decimal escapes
parse_lua(r'a = "hello \1 \12 \123 \1234 world" ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(r'a = "hello \240 \250 \255 \25599 \255F world" ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(r'a = "hello \2a \25a \255a world" ;_'.replace(' ', ws).replace('_', ws_sep))
for v in ['256', '260', '300', '999']:
with pytest.raises(Exception):
parse_lua(f'a = "hello \\{v} world" ;_'.replace(' ', ws).replace('_', ws_sep))
# \u{XXX} unicode literals
parse_lua(r'a = "hello \u{0} \u{1234CDEF} \u{0001234CDEF} world" ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua(r'a = "hello \u{7FFFFFFF} \u{0007FFFFFFF} world" ;_'.replace(' ', ws).replace('_', ws_sep))
for v in ['700000000', '7FFFFFFF0', '80000000', '8FFFFFFF', 'FFFFFFFF']:
with pytest.raises(Exception):
parse_lua(f'a = "hello \\u{v} world" ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua(r'a = "hello \u{} world" ;_'.replace(' ', ws).replace('_', ws_sep))
# Test illegal linebreaks
with pytest.raises(Exception):
parse_lua(f'a = "hello\nworld" ;_'.replace(' ', ws).replace('_', ws_sep))
# Test some random invalid escapes
for esc in 'cdyq!#*':
with pytest.raises(Exception):
parse_lua(f'a = "hello\\{esc}world" ;_'.replace(' ', ws).replace('_', ws_sep))
# Test non-ASCII characters
parse_lua(f'a = " ツ " ; '.encode('utf-8'))
# Test long-form strings
# These ignore escape sequences so we don't have to test that
helper_test_multiline_comment_or_long_string('a =')
end()
def test_function_expression():
"""
Test parsing function expressions
"""
reset_counter()
helper_test_function_bodies('a = function')
end()
def test_table_constructor_expression():
"""
Test parsing table constructor expressions
"""
reset_counter()
for ws, ws_sep in iter_whitespace():
# Empty table constructors, with varying whitespace
parse_lua('a = { } ;_'.replace(' ', ws).replace('_', ws_sep))
# Field format: [exp] = exp
parse_lua('a = { [ 5 ] = 5 } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { [ b . c : e ( f ) ] = g ( h ) . i [ nil ] } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { [ b . c : e ( f ) ] = g ( h ) . i [ -23 ] ; } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { [ b . c : e ( f ) ] = g ( h ) . i [ ... ] , } ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = { [ 5 ] } ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = { [ 5 ] = } ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = { [ for ] = nil } ;_'.replace(' ', ws).replace('_', ws_sep))
# Field format: name = exp
parse_lua('a = { five = 5 } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { a = g ( h ) . i [ nil ] } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { a = g ( h ) . i [ -23 ] ; } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { a = g ( h ) . i [ ... ] , } ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = { five = } ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = { 7 = 5 } ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = { nil = 5 } ;_'.replace(' ', ws).replace('_', ws_sep))
# Field format: exp
parse_lua('a = { 5 } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { b . c : e ( f ) } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { g ( h ) . i [ nil ] } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { g ( h ) . i [ -23 ] ; } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { g ( h ) . i [ ... ] , } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { nil } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { five } ;_'.replace(' ', ws).replace('_', ws_sep))
if '[[' not in ws:
parse_lua('a = { [[ [long string starting with single square bracket ]] } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { [[ [long string starting with single square bracket ]] , } ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = { 5 = } ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = { [[ [this should be a syntax error]] ] } ;_'.replace(' ', ws).replace('_', ws_sep))
# Multiple fields
parse_lua('a = { 5 , 6 , 7 } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { 5 , fish ; hello = "world" } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { 5 ; fish , hello = "world" } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { hello = "world" ; [ a . b ] = 33 } ;_'.replace(' ', ws).replace('_', ws_sep))
parse_lua('a = { -23 } ;_'.replace(' ', ws).replace('_', ws_sep))
# An example from the Lua documentation
parse_lua('a = { [ f ( 1 ) ] = g ; "x" , "y" ; x = 1 , f ( x ) , [ 30 ] = 23 ; 45 } ;_'.replace(' ', ws).replace('_', ws_sep))
# Table constructors with only a field separator (illegal)
with pytest.raises(Exception):
parse_lua('a = { , } ;_'.replace(' ', ws).replace('_', ws_sep))
with pytest.raises(Exception):
parse_lua('a = { ; } ;_'.replace(' ', ws).replace('_', ws_sep))
end()