-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua-globals.lua
More file actions
executable file
·1546 lines (1476 loc) · 71.1 KB
/
lua-globals.lua
File metadata and controls
executable file
·1546 lines (1476 loc) · 71.1 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
#!/usr/bin/env lua
---------------------------------------------------------------------------------------------------------------------------------------
-- Display list of globals used by your Lua script
---------------------------------------------------------------------------------------------------------------------------------------
-- Version: 2021-10-25
-- License: MIT (see LICENSE file)
-- By: Egor Skriptunoff
-- Updated by: im-mortal
--
-- Reads your Lua script from STDIN
-- Writes list of globals to STDOUT (if the script is syntactically correct)
-- Writes parsing error to STDERR (if the script is not syntactically correct)
--
-- Usage examples (both Windows and Linux):
--
-- How to display all the globals access (both read and write) from two files
-- show_globals.lua --mode RW --lua-version 51 your_script.lua src/another_script.lua
--
-- How to display only write access to globals from stdin
-- show_globals.lua -m W -i < your_script.lua
--
-- How to set 5.3 as Lua version for interpreting "your_script.lua"
-- show_globals.lua -l 53 your_script.lua
local argparse = require "argparse"
local argparser = argparse()
:name "lua-globals"
:description "Writes list of globals used by a supplied Lua script to STDOUT or parsing error to STDERR."
:epilog "For more information, see:\nhttps://github.com/Warhammer-Mods/lua-globals"
argparser:mutex(
argparser:argument "input"
:description "Input file. If no provided, reads from STDIN"
:args "*",
argparser:flag "-i --read-from-stdin"
:description "Read from STDIN."
)
argparser:option "-l --lua-version"
:description "Script Lua version target"
:choices { "5.1", "5.2", "5.3", "5.4" }
:default "5.4"
argparser:option "-m --mode"
:description "Globals reporting mode, R is for read access, W is for write access, RW if for all globals access."
:choices {"R", "W", "RW"}
:default "RW"
argparser:flag "-d --debug"
:description "Enables debug output."
local args = argparser:parse()
if args.debug == true then
local inspect = require "inspect"
print("args = " .. inspect(args) .. "\n")
end
local read_write_access = { R = args.mode:find"R", W = args.mode:find"W" }
local lua_version = args.lua_version:gsub("%.", "")
local all_supported_versions = { ["51"] = true, ["52"] = true, ["53"] = true, ["54"] = true }
local lua_parser
do
local
byte, sub, find, char, rep, match, gsub, upper, reverse, pairs, ipairs, tonumber, tostring, type, unpack, table_concat, math_min, huge
=
string.byte, string.sub, string.find, string.char, string.rep, string.match, string.gsub, string.upper, string.reverse,
pairs, ipairs, tonumber, tostring, type, table.unpack or unpack, table.concat, math.min, math.huge
local scanner
do
local keyword_types = {
["nil"] = "literal value",
["false"] = "literal value",
["true"] = "literal value",
["not"] = "operator",
["and"] = "operator",
["or"] = "operator",
["repeat"] = "operator",
["until"] = "operator",
["while"] = "operator",
["do"] = "operator",
["end"] = "operator",
["if"] = "operator",
["then"] = "operator",
["elseif"] = "operator",
["else"] = "operator",
["for"] = "operator",
["in"] = "operator",
["local"] = "operator",
["break"] = "operator",
["return"] = "operator",
["function"] = "operator",
}
local keyword_values = { ["false"] = false, ["true"] = true } -- ["nil"] = nil
local escapes = { a = "\a", b = "\b", f = "\f", n = "\n", r = "\r", t = "\t", v = "\v", ["\\"] = "\\", ['"'] = '"', ["'"] = "'", ["\n"] = "\n" }
local type_of_characters = { -- some characters are omitted intentionally
[" "] = "lexem delimiter",
["\n"] = "lexem delimiter",
["\t"] = "lexem delimiter",
["\f"] = "lexem delimiter",
["\v"] = "lexem delimiter",
["#"] = "operator",
["("] = "operator", [")"] = "operator", ["{"] = "operator", ["}"] = "operator", ["]"] = "operator",
["+"] = "operator", ["*"] = "operator", ["%"] = "operator", ["^"] = "operator",
[","] = "operator", [";"] = "operator",
["&"] = "bitwise operator",
["|"] = "bitwise operator",
["'"] = "quote", ['"'] = "quote",
["<"] = "comparison", [">"] = "comparison", ["="] = "comparison", ["~"] = "comparison",
_ = "alpha",
}
for j = 1, 26 do
type_of_characters[char(64 + j)] = "alpha" -- A-Z
type_of_characters[char(96 + j)] = "alpha" -- a-z
end
for j = 0, 9 do
type_of_characters[char(48 + j)] = "digit" -- 0-9
end
local all_scanners = {}
function scanner(version)
version = gsub(version, "%D", "")
local scanner_for_version = all_scanners[version]
if not scanner_for_version then
if not all_supported_versions[version] then
error("There is no scanner for this version available", 2)
end
local version_53_plus = version == "53" or version == "54"
local version_52_plus = version_53_plus or version == "52"
local feature_floating_point_hex_numbers = version_52_plus
local feature_goto_is_keyword = version_52_plus
local feature_prohibit_unknown_escapes = version_52_plus
local feature_backslash_z = version_52_plus
local feature_backslash_x = version_52_plus
local feature_backslash_u = version_53_plus
local feature_int64 = version_53_plus
function scanner_for_version(lua_code)
local pos = 1 -- position of first unread character
local current_line_no = 1
local current_line_start_pos = 1
local function get_current_line_col()
return
current_line_no,
pos - current_line_start_pos + 1,
pos
end
local function read_character()
-- returns next_character (any variant of newline is converted to "\n")
-- returns nil (when EOF)
local character = sub(lua_code, pos, pos)
if pos == 1 and character == "#" then
repeat -- skip shebang
pos = pos + 1
character = sub(lua_code, pos, pos)
until character == "\n" or character == "\r" or character == ""
end
if character ~= "" then
pos = pos + 1
if character == "\n" or character == "\r" then
local newline = character
character = sub(lua_code, pos, pos)
if (character == "\n" or character == "\r") and newline ~= character then
pos = pos + 1
end
current_line_no = current_line_no + 1
current_line_start_pos = pos
character = "\n"
end
return character
end
end
local function read_string_literal(line_no, col_no, quote, is_comment)
-- current position is just after opening quote
-- returns true
-- returns nil, error_message (in case of error)
if quote == '"' or quote == "'" then
local unfinished
while true do
local character = read_character()
if not character or character == "\n" then
unfinished = true
break
end
if character == quote then
break
end
if character == "\\" then
character = read_character()
local line_no, col_no = get_current_line_col()
col_no = col_no - 2
if not character then
unfinished = true
break
end
local code = byte(character)
if code >= 48 and code <= 57 then
character = code - 48
for _ = 1, 2 do
code = byte(lua_code, pos)
if not code or code < 48 or code > 57 then
break
end
read_character()
character = character * 10 + code - 48
end
if character > 255 then
return nil, "Syntax error: decimal escape too large at line "..line_no.." col "..col_no
end
else
local unescaped = escapes[character]
if character == "z" and feature_backslash_z then
while find(lua_code, "^%s", pos) do
read_character()
end
unescaped = true
elseif character == "x" and feature_backslash_x then
local character = match(lua_code, "^%x%x", pos)
if not character then
return nil, "Syntax error: two hexadecimal digits expected after '\\x' at line "..line_no.." col "..col_no
end
unescaped = true
read_character()
read_character()
elseif character == "u" and feature_backslash_u then
if read_character() ~= "{" then
return nil, "Syntax error: '{' is expected after '\\u' at line "..line_no.." col "..col_no
end
local digit_code
unescaped = 0
repeat
character = read_character()
if character ~= "}" then
digit_code = character and byte(upper(character)) or 0
if digit_code < 48 or digit_code > 90 or digit_code > 57 and digit_code < 65 then
return nil, "Syntax error: '{XXX}' is expected after '\\u' at line "..line_no.." col "..col_no
end
unescaped = unescaped * 16 + tonumber(character, 16)
if unescaped > 0x10FFFF then
return nil, "Syntax error: UTF-8 value too large at line "..line_no.." col "..col_no
end
end
until character == "}"
if not digit_code then
return nil, "Syntax error: hexadecimal digit is expected after '\\u{' at line "..line_no.." col "..col_no
end
end
if feature_prohibit_unknown_escapes and not unescaped then
return nil, "Syntax error: invalid escape sequence at line "..line_no.." col "..col_no
end
end
end
end
if unfinished then
return nil, "Syntax error: unfinished string literal at line "..line_no.." col "..col_no
end
else
quote = "]"..rep("=", #quote - 2).."]"
local next_character = sub(lua_code, pos, pos)
if next_character == "\n" or next_character == "\r" then
read_character()
end
local quote_start_pos, quote_end_pos = find(lua_code, quote, pos, true)
if not quote_start_pos then
return nil, "Syntax error: unfinished long "..(is_comment and "comment" or "string literal").." at line "..line_no.." col "..col_no
end
while pos < quote_start_pos do
read_character()
end
pos = quote_end_pos + 1
end
return true
end
local function scan_next_lexem()
-- returns {lexem.type == "EOF"} (in case of EOF)
-- returns nil, error_message (in case of error)
-- returns lexem record consisting of the following fields:
-- type subtype additional fields
-- ---------------------------- --------------------------------- -----------------
-- "identifier" line col pos
-- "literal value" "boolean"/"nil"/"string"/"number" line col pos
-- "comment" "short"/"long" line col pos
-- (all operators and keywords) line col pos
-- "EOF" line col pos
local line_no, col_no, lexem_start_pos, character, character_type
repeat
line_no, col_no, lexem_start_pos = get_current_line_col()
character = read_character()
character_type = type_of_characters[character]
until character_type ~= "lexem delimiter"
if character then
local next_character = sub(lua_code, pos, pos)
local next_next_character = sub(lua_code, pos + 1, pos + 1)
local code = byte(character)
if character_type == "alpha" then -- A-Za-z_
character, pos = match(lua_code, "^([A-Za-z_%d]+)()", pos - 1)
local keyword_type = keyword_types[character]
if keyword_type == "literal value" then
-- constants of types "boolean"/"nil"
local value = keyword_values[character]
return { type = keyword_type, subtype = type(value), line = line_no, col = col_no, pos = lexem_start_pos }
elseif keyword_type or character == "goto" and feature_goto_is_keyword then
-- keywords
return { type = character, line = line_no, col = col_no, pos = lexem_start_pos }
else
-- identifiers
return { type = "identifier", value = character, line = line_no, col = col_no, pos = lexem_start_pos }
end
elseif character_type == "operator" or character_type == "bitwise operator" and feature_int64 then
-- punctuation
return { type = character, line = line_no, col = col_no, pos = lexem_start_pos }
elseif character_type == "quote" then
local ok, error_message = read_string_literal(line_no, col_no, character)
if ok then
return { type = "literal value", subtype = "string", line = line_no, col = col_no, pos = lexem_start_pos }
else
return nil, error_message
end
elseif character_type == "comparison" then
if next_character == "=" or character == next_character and character ~= "~" and feature_int64 then
character = character..next_character
pos = pos + 1
end
if character == "~" and not feature_int64 then
return nil, "Syntax error at line "..line_no.." col "..col_no
end
return { type = character, line = line_no, col = col_no, pos = lexem_start_pos }
elseif character == "0" and (next_character == "x" or next_character == "X") then
local hex_pos = pos + 1
if feature_floating_point_hex_numbers then
character, pos = match(lua_code, "^([%.%x]*)()", hex_pos)
local ok = find(character, "^%x*%.?%x*$") and character ~= "." and character ~= ""
if upper(sub(lua_code, pos, pos)) == "P" then
pos = match(lua_code, "^[%+%-]?%d+()", pos + 1)
end
if not (ok and pos) then
return nil, "Syntax error: malformed hexadecimal number at line "..line_no.." col "..col_no
end
return { type = "literal value", subtype = "number", line = line_no, col = col_no, pos = lexem_start_pos }
else
pos = match(lua_code, "^%x+()", hex_pos)
if not pos then
return nil, "Syntax error: malformed hexadecimal number at line "..line_no.." col "..col_no
end
return { type = "literal value", subtype = "number", line = line_no, col = col_no, pos = lexem_start_pos }
end
elseif character == "." and next_character == "." then
if next_next_character == "." then
pos = pos + 2
return { type = "...", line = line_no, col = col_no, pos = lexem_start_pos }
else
pos = pos + 1
return { type = "..", line = line_no, col = col_no, pos = lexem_start_pos }
end
elseif character == "." or character_type == "digit" then
local starting_pos = pos - 1
character, pos = match(lua_code, "^([%.%d]+)()", starting_pos)
if character == "." then
return { type = character, line = line_no, col = col_no, pos = lexem_start_pos }
else
local ok = match(character, "^%d*%.?%d*$")
if upper(sub(lua_code, pos, pos)) == "E" then
pos = match(lua_code, "^[%+%-]?%d+()", pos + 1)
end
if not (ok and pos) then
return nil, "Syntax error: malformed number at line "..line_no.." col "..col_no
end
return { type = "literal value", subtype = "number", line = line_no, col = col_no, pos = lexem_start_pos }
end
elseif character == "[" then
if next_character == "[" or next_character == "=" then
local equal_ctr = -1
repeat
equal_ctr = equal_ctr + 1
character = read_character()
if character ~= "[" and character ~= "=" then
return nil, "Syntax error at line "..line_no.." col "..col_no
end
until character == "["
local ok, error_message = read_string_literal(line_no, col_no, "["..rep("=", equal_ctr).."[")
if ok then
return { type = "literal value", subtype = "string", line = line_no, col = col_no, pos = lexem_start_pos }
else
return nil, error_message
end
else
return { type = character, line = line_no, col = col_no, pos = lexem_start_pos }
end
elseif character == "-" then
if next_character == "-" then
pos = pos + 1
local equal_ctr
if next_next_character == "[" then
equal_ctr = -1
repeat
equal_ctr = equal_ctr + 1
local next_pos = pos + 1 + equal_ctr
character = sub(lua_code, next_pos, next_pos)
if character ~= "[" and character ~= "=" then
equal_ctr = nil
end
until not equal_ctr or character == "["
end
if equal_ctr then
pos = pos + 2 + equal_ctr
local ok, error_message = read_string_literal(line_no, col_no, rep("=", equal_ctr + 2), true)
if ok then
return { type = "comment", subtype = "long", line = line_no, col = col_no, pos = lexem_start_pos }
else
return nil, error_message
end
else
pos = pos + #match(lua_code, "[^\r\n]*", pos)
return { type = "comment", subtype = "short", line = line_no, col = col_no, pos = lexem_start_pos }
end
end
return { type = character, line = line_no, col = col_no, pos = lexem_start_pos }
elseif character == "/" then
if next_character == "/" and feature_int64 then
pos = pos + 1
character = "//"
end
return { type = character, line = line_no, col = col_no, pos = lexem_start_pos }
elseif character == ":" then
if next_character == ":" and feature_goto_is_keyword then
pos = pos + 1
character = "::"
end
return { type = character, line = line_no, col = col_no, pos = lexem_start_pos }
else
return nil, "Syntax error at line "..line_no.." col "..col_no
end
else
return { type = "EOF", line = line_no, col = col_no, pos = lexem_start_pos }
end
end
return scan_next_lexem
end
all_scanners[version] = scanner_for_version
end
return scanner_for_version
end
end
local function find_def(vars, name)
-- returns subtype("local"/"upvalue"/"global"), def(only for "local"/"upvalue")
-- vars[1] = parent_vars/nil, vars[2] = is_function_body, vars[name] = def
local subtype = "local"
repeat
local def = vars[name]
if def then
return subtype, def
end
if vars[2] then
subtype = "upvalue"
end
vars = vars[1]
until not vars
return "global"
end
local binary_operator_predecessors = {
-- binary operators are preceded in Lua grammar only by the following lexems (ignoring comments):
["identifier"] = true,
["literal value"] = true,
["..."] = true,
["end"] = true,
[")"] = true,
["}"] = true,
["]"] = true,
}
-- Priorities are positive numbers
-- The same priority level must not be shared between unary and binary operators
-- The same priority level must not be shared between left-associative and right-associative binary operators
local binary_operators = {
-- priorities of binary operators
["or"] = 1,
["and"] = 2,
["<"] = 3, [">"] = 3, ["<="] = 3, [">="] = 3, ["~="] = 3, ["=="] = 3,
["|"] = 4,
["~"] = 5,
["&"] = 6,
["<<"] = 7, [">>"] = 7,
[".."] = 8,
["+"] = 9, ["-"] = 9,
["*"] = 10, ["/"] = 10, ["//"] = 10, ["%"] = 10,
["^"] = 12
}
local unary_operators = {
-- priorities of unary operators
["not"] = 11, ["#"] = 11, ["unary -"] = 11, ["unary ~"] = 11
}
local right_associative_priorities = {
-- precedences of right associative binary operators:
[8] = true, -- .. concatenation
[12] = true, -- ^ exponentiation
}
local all_parsers = {}
function lua_parser(version)
version = gsub(version, "%D", "")
local parser_for_version = all_parsers[version]
if not parser_for_version then
if not all_supported_versions[version] then
error("There is no parser for this version available", 2)
end
local version_52_plus = version == "52" or version == "53" or version == "54"
local feature_break_is_last_statement = version == "51"
local feature_empty_statement = version_52_plus
local feature_ENV = version_52_plus
local lua_scanner = scanner(version)
function parser_for_version(lua_code, global_catcher)
-- returns true
-- returns nil, error_message (in case of parsing error)
local get_next_lexem_from_scanner = lua_scanner(lua_code)
local error_message
local next_lexem, next_next_lexem
local last_lexem, before_last_lexem
local prev_non_comment_lexem_type
local function read_lexem()
-- returns nil in case of error (error_message is set)
-- lexem.type == "EOF" (when EOF)
local lexem
if next_lexem then
lexem, next_lexem, next_next_lexem = next_lexem, next_next_lexem
else
repeat
lexem, error_message = get_next_lexem_from_scanner()
if not lexem then
return
end
local lexem_type = lexem.type
local is_comment = lexem_type == "comment"
if not is_comment then
if (lexem_type == "-" or lexem_type == "~") and not binary_operator_predecessors[prev_non_comment_lexem_type] then
lexem_type = "unary "..lexem_type
lexem.type = lexem_type
end
prev_non_comment_lexem_type = lexem_type
end
until not is_comment
end
before_last_lexem, last_lexem = last_lexem, lexem
return lexem
end
local function unread_last_lexem()
last_lexem, next_lexem, next_next_lexem = before_last_lexem, last_lexem, next_lexem
end
local function get_lexem_coordinates_as_text(lexem)
if lexem.type == "EOF" then
return "EOF"
else
return "line "..lexem.line.." col "..lexem.col
end
end
local read_chunk, read_exp, read_explist
--------------------------------------------------------------------------------------------------------------------------------------------
local function read_table_constructor(vars)
--------------------------------------------------------------------------------------------------------------------------------------------
-- opening curly brace is already read
unread_last_lexem()
local result = read_lexem() -- this is the opening curly brace
result.type = "table constructor"
local array_of_table_items = {}
result.items = array_of_table_items
local lexem = read_lexem()
if not lexem then
return
end
local lexem_type = lexem.type
while lexem_type ~= "}" do
local index_exp, field_exp, value_exp
if lexem_type == "[" then
index_exp = read_exp(vars)
if not index_exp then
return
end
lexem = read_lexem()
if not lexem then
return
end
if lexem.type ~= "]" then
error_message = "Syntax error: ']' is expected at "..get_lexem_coordinates_as_text(lexem)
return
end
lexem = read_lexem()
if not lexem then
return
end
if lexem.type ~= "=" then
error_message = "Syntax error: '=' is expected at "..get_lexem_coordinates_as_text(lexem)
return
end
value_exp = read_exp(vars)
if not value_exp then
return
end
else
if lexem_type == "identifier" then
local next_lexem = read_lexem()
if not next_lexem then
return
end
if next_lexem.type == "=" then
field_exp = lexem
field_exp.subtype = "field"
else
unread_last_lexem()
end
end
if not field_exp then
unread_last_lexem()
end
value_exp = read_exp(vars)
if not value_exp then
return
end
end
array_of_table_items[#array_of_table_items + 1] = { index = index_exp, field = field_exp, value = value_exp }
lexem = read_lexem()
if not lexem then
return
end
lexem_type = lexem.type
if lexem_type == "," or lexem_type == ";" then
lexem = read_lexem()
if not lexem then
return
end
lexem_type = lexem.type
elseif lexem_type ~= "}" then
error_message = "Syntax error: ',' or ';' or '}' is expected at "..get_lexem_coordinates_as_text(lexem)
return
end
end
return result
end
--------------------------------------------------------------------------------------------------------------------------------------------
local function read_funcbody(vars, invisible_self_argument)
--------------------------------------------------------------------------------------------------------------------------------------------
local result = read_lexem()
if not result then
return
end
if result.type ~= "(" then
error_message = "Syntax error: '(' is expected at "..get_lexem_coordinates_as_text(result)
return
end
result.type = "function definition"
local arguments = {}
if invisible_self_argument then
arguments[1] = { type = "identifier", subtype = "definition", value = "self", line = result.line, col = result.col, pos = result.pos }
end
result.arguments = arguments
local is_vararg = false
local after_separator = true
local visible_argument
while true do
local lexem = read_lexem()
if not lexem then
return
end
local lexem_type = lexem.type
if lexem_type == ")" and not (after_separator and visible_argument) then
local body = read_chunk(vars, false, arguments, true)
if not body then
return
end
result.body = body
local closing_lexem = read_lexem()
if not closing_lexem then
return
end
if closing_lexem.type ~= "end" then
error_message = "Syntax error: 'end' is expected at "..get_lexem_coordinates_as_text(closing_lexem)
return
end
result.is_vararg = is_vararg
return result
elseif lexem_type == "," and not after_separator and not is_vararg then
after_separator = true
elseif (lexem_type == "identifier" or lexem_type == "...") and after_separator then
arguments[#arguments + 1] = lexem
visible_argument = true
after_separator = false
is_vararg = lexem_type == "..."
else
error_message = "Syntax error in function argument list at "..get_lexem_coordinates_as_text(lexem)
return
end
end
end
--------------------------------------------------------------------------------------------------------------------------------------------
local function read_prefixexp(vars, pure_global_LHS)
--------------------------------------------------------------------------------------------------------------------------------------------
-- returns prefixexp_subtree, exp_type, first_lexem_of_prefixexp
-- exp_type: 1 = rvalue (neither lvalue nor funccall)
-- 3 = funccall
-- 5 = lvalue
local exp_type, write_access
local first_lexem = read_lexem()
if not first_lexem then
return
end
local lexem_type = first_lexem.type
if lexem_type == "(" then
exp_type = 1
first_lexem.type = "(expression)"
local expression = read_exp(vars)
if not expression then
return
end
first_lexem.expression = expression
local lexem = read_lexem()
if not lexem then
return
end
if lexem.type ~= ")" then
error_message = "Syntax error: ')' is expected at "..get_lexem_coordinates_as_text(lexem)
return
end
elseif lexem_type == "identifier" then
exp_type = 5
first_lexem.subtype, first_lexem.def = find_def(vars, first_lexem.value)
if global_catcher and first_lexem.subtype == "global" then
global_catcher(first_lexem)
if pure_global_LHS then
write_access = true
end
end
else
error_message = "Syntax error at "..get_lexem_coordinates_as_text(first_lexem)
return
end
local result = first_lexem
while true do
local lexem = read_lexem()
if not lexem then
return
end
lexem_type = lexem.type
if lexem_type == "[" then
write_access = false
-- [index]
exp_type = 5
lexem.type = "table[index]"
lexem.table = result
local exp = read_exp(vars)
if not exp then
return
end
lexem.index = exp
local next_lexem = read_lexem()
if not next_lexem then
return
end
if next_lexem.type ~= "]" then
error_message = "Syntax error: ']' is expected at "..get_lexem_coordinates_as_text(next_lexem)
end
result = lexem
elseif lexem_type == "." then
write_access = false
-- .field
exp_type = 5
lexem.type = "object.field"
lexem.object = result
local next_lexem = read_lexem()
if not next_lexem then
return
end
if next_lexem.type ~= "identifier" then
error_message = "Syntax error: an identifier is expected at"..get_lexem_coordinates_as_text(next_lexem)
return
end
next_lexem.subtype = "field"
lexem.field = next_lexem
result = lexem
elseif lexem_type == ":" or lexem_type == "(" or lexem_type == "{" or lexem_type == "literal value" and lexem.subtype == "string" then
write_access = false
-- function call
exp_type = 3
if lexem_type == ":" then
lexem.object = result
result = lexem
lexem = read_lexem()
if not lexem then
return
end
if lexem.type ~= "identifier" then
error_message = "Syntax error: an identifier is expected after ':' at "..get_lexem_coordinates_as_text(lexem)
return
end
lexem.subtype = "field"
result.method = lexem
lexem = read_lexem()
if not lexem then
return
end
lexem_type = lexem.type
if not (lexem_type == "(" or lexem_type == "{" or lexem_type == "literal value" and lexem.subtype == "string") then
error_message = "Syntax error: '(' is expected at "..get_lexem_coordinates_as_text(lexem)
return
end
else
if lexem_type == "(" then
lexem.object = result
result = lexem
else
result = { object = result, line = lexem.line, col = lexem.col }
end
end
result.type = "funccall"
if lexem_type == "(" then
lexem = read_lexem()
if not lexem then
return
end
if lexem.type == ")" then
result.arguments = {}
else
unread_last_lexem()
local arguments = read_explist(vars)
if not arguments then
return
end
result.arguments = arguments
lexem = read_lexem()
if not lexem then
return
end
if lexem.type ~= ")" then
error_message = "Syntax error: ')' is expected at "..get_lexem_coordinates_as_text(lexem)
return
end
end
elseif lexem_type == "{" then
-- read table constructor
local table_constructor_exp = read_table_constructor(vars)
if not table_constructor_exp then
return
end
result.arguments = { table_constructor_exp }
else -- literal string as single argument
result.arguments = { lexem }
end
else
unread_last_lexem()
if write_access then
global_catcher() -- mark write access
end
return result, exp_type, first_lexem
end
end
end
--------------------------------------------------------------------------------------------------------------------------------------------
local function read_term(vars)
--------------------------------------------------------------------------------------------------------------------------------------------
-- term is an expression having root operator (last operator to be executed) different from "exp ::= exp binop exp"
local lexem = read_lexem()
if not lexem then
return
end
local lexem_type = lexem.type
if lexem_type == "literal value" then
return lexem
elseif lexem_type == "..." then
lexem.subtype, lexem.def = find_def(vars, lexem_type)
return lexem
elseif lexem_type == "{" then
return read_table_constructor(vars)
elseif lexem_type == "function" then
return read_funcbody(vars)
else
local unary_oper_priority = unary_operators[lexem_type]
if unary_oper_priority then
local argument, ends_with_prefixexp = read_exp(vars, unary_oper_priority)
if not argument then
return
end
lexem.argument = argument
return lexem, ends_with_prefixexp
else
unread_last_lexem()
return read_prefixexp(vars) -- second returned value is always truthy
end
end
end
--------------------------------------------------------------------------------------------------------------------------------------------
local function read_chain_of_binary_operators_with_specified_priority(vars, first_term, first_binary_operator, chain_priority)
--------------------------------------------------------------------------------------------------------------------------------------------
-- parse expression consisting of binary operators with specified priority
first_binary_operator.left_argument = first_term
local is_right_associative = right_associative_priorities[chain_priority]
local term, ends_with_prefixexp = read_term(vars)
if not term then
return
end
first_binary_operator.right_argument = term
local last_binary_operator = first_binary_operator
while true do
local binary_operator = read_lexem()
if not binary_operator then
return
end
local priority = binary_operators[binary_operator.type]
if not priority or priority < chain_priority then
unread_last_lexem()
return is_right_associative and first_binary_operator or last_binary_operator, ends_with_prefixexp
elseif priority > chain_priority then
term, ends_with_prefixexp = read_chain_of_binary_operators_with_specified_priority(vars, last_binary_operator.right_argument, binary_operator, priority)
if not term then
return
end
last_binary_operator.right_argument = term
else
term, ends_with_prefixexp = read_term(vars)
if not term then
return
end
binary_operator.right_argument = term
if is_right_associative then
binary_operator.left_argument = last_binary_operator.right_argument
last_binary_operator.right_argument = binary_operator
else
binary_operator.left_argument = last_binary_operator
end
last_binary_operator = binary_operator
end
end
end
--------------------------------------------------------------------------------------------------------------------------------------------
function read_exp(vars, outer_priority)
--------------------------------------------------------------------------------------------------------------------------------------------
local fake_operator, ends_with_prefixexp = read_chain_of_binary_operators_with_specified_priority(vars, nil, {}, outer_priority or 0)
if not fake_operator then
return
end
return fake_operator.right_argument, ends_with_prefixexp
end
--------------------------------------------------------------------------------------------------------------------------------------------
function read_explist(vars, min_qty, max_qty)
--------------------------------------------------------------------------------------------------------------------------------------------
min_qty, max_qty = min_qty or 1, max_qty or huge
local rvalues = { }
local exp, ends_with_prefixexp
repeat
exp, ends_with_prefixexp = read_exp(vars)
if not exp then
return
end
rvalues[#rvalues + 1] = exp
local qty = #rvalues
local lexem = read_lexem()
if not lexem then
return
end