-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathParser.gd
427 lines (383 loc) · 12.8 KB
/
Parser.gd
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
class_name Parser
extends Object
##
## This file contains GDScript parsing functions
## Developed by Johannes Witt and Hugo Locurcio
## Placed into the Public Domain
##
signal warning_generated(line, string)
func warn(line: int, string: String) -> void:
emit_signal("warning_generated", line, string)
### Get Parser ###
# Get Parser take snippets of a line and return them processed #
# Those parser should not generate complete lines #
## Returns converison type
static func get_isas(string: String) -> String:
return string.substr(2).strip_edges()
static func get_in(string: String) -> Array:
return Array(string.split(" in "))
## Returns position of the first occurrence of [.,<space>,<comma>,[,(]
static func get_end_of_expression(string: String) -> int:
var position := 0
for i in string:
if i in [".", " ", "[", "]", "(", ")", ","]:
return position
position += 1
return -1
## Returns return value of string
static func get_return_value(string: String) -> String:
return string.substr(6).strip_edges()
## Returns type of variable declaration
static func get_type_from_td(string: String) -> String:
var is_inferred := string.count(":=") > 0
if is_inferred:
return ""
var colon := Utility.find_not_in_string(string, ":")
if colon == -1:
return ""
return string.right(colon + 1).split("=")[0].strip_edges()
## Returns [name, type, default value] # null: valid | "": invalid
static func get_type_and_default_value(string: String) -> Array:
var arg_name = ""
var type = null
var default_value = null
if string.count(":=") > 0:
var default_split := string.split(":=")
if default_split.size() > 1:
default_value = default_split[1].strip_edges()
arg_name = default_split[0].strip_edges()
else:
arg_name = default_split[0]
default_value = ""
elif string.count(":") > 0:
var split := string.split(":")
if split.size() > 1:
arg_name = split[0]
if split[1].count("=") > 0:
var default_split = (split[1] as String).split("=")
if default_split.size() > 1:
type = default_split[0]
default_value = default_split[1]
else:
type = default_split[0]
default_value = ""
else:
type = split[1].strip_edges()
elif string.count("=") > 0:
var split := string.split("=")
if split.size() > 1:
arg_name = split[0]
default_value = split[1]
else:
arg_name = split[0]
default_value = ""
else:
arg_name = string
if type != null:
type = type.strip_edges()
if default_value != null:
default_value = default_value.strip_edges()
return [arg_name.strip_edges(), type, default_value]
## Returns function name from declaration
static func get_function_name_from_d(string: String) -> String:
var begin = 12 if Detector.is_static_function(string) else 5
if string.count("(") == 0:
return string.substr(begin)
return string.substr(begin, string.find("(") - begin).strip_edges()
## Returns the return value of function declaration
static func get_function_retval(string: String) -> String:
var arrow := string.find("->") + 2
if arrow == 1:
return ""
var colon := string.find_last(":")
if colon == -1:
colon += arrow
return string.substr(arrow, colon - arrow).strip_edges()
## Returns a list of the arguments of function declaration
## List structure: [[name, type, default_value], ...]
static func get_function_arguments(string: String) -> Array:
var begin = string.find("(") + 1
if begin == 0:
return []
var end = string.find(")")
if end < begin:
end = begin - 1
var args := []
var argstr := string.substr(begin, end - begin)
if argstr.empty():
return []
for arg in argstr.split(","):
args.push_back(get_type_and_default_value(arg))
return args
## Returns the right side of an assignment
static func get_assignment(string: String) -> String:
return Utility.split_assignment(string)[2].strip_edges()
## Returns the math expression
static func get_math(string: String) -> Array:
assert(false) # Unimplemented
return []
## Returns the correct comma position after closing brace
static func get_correct_comma(string: String, offset := 0) -> int:
var braces := 0
for i in string.length():
if string[i] == "(":
braces += 1
continue
if string[i] == ")":
braces -= 1
continue
if i < offset:
continue
if braces == 0 && string[i] == ",":
return i
return -1
static func get_nodepath(string: String) -> String:
var end = string.find('"', 2)
if end != -1:
end -= 2
return string.substr(2, end)
static func get_getnode(string: String) -> String:
if string.begins_with("\""):
return string.substr(0, string.find("\"", 1) + 1)
var end = get_end_of_expression(string)
return string.substr(0, end)
static func get_brace_content(string: String) -> String:
var braces := 0
var first_brace := -1
for i in string.length():
if string[i] == "(":
if braces == 0:
first_brace = i + 1
braces += 1
continue
if string[i] == ")":
braces -= 1
if braces == 0 && first_brace != -1:
return string.substr(first_brace, i - first_brace)
return string.substr(first_brace)
## Adds using to the using dict, if method requires it
static func parse_using(method: String, usings):
if method in Utility.METHOD_USINGS:
if !Utility.METHOD_USINGS[method] in usings:
usings.push_back(Utility.METHOD_USINGS[method])
### Parser ###
# Parser read complex input and output #
# the result structured. #
## Parses ordinary code and tokenizes it
func parse_statement(line: int, string: String) -> Array:
var res := []
var i := 0
var skip_math := false
while !string.empty():
string = string.strip_edges()
i += 1
#print("[%d] " % i, string)
if Detector.is_string(string):
res.push_back(["string", string])
string = ""
elif string.is_valid_integer():
res.push_back(["int", string])
string = ""
elif string.is_valid_float():
res.push_back(["float", string])
string = ""
elif Detector.is_bool_exp(string):
res.push_back(["bool", string])
string = ""
elif Detector.is_return(string):
res.push_back(["return", parse_statement(line, get_return_value(string))])
string = ""
elif Detector.is_pass(string):
res.push_back(["pass"])
string = ""
elif Detector.is_probably_property_string(string):
var end = string.find("]")
end -= 1 if end != -1 else 0
res.push_back(["property", string.substr(1, end)])
warn(line, "Possible Property. Please use .get or .set instead")
if end == -1:
string = ""
else:
string.erase(0, end + 3)
elif Detector.is_subscription(string):
var end = string.find("]")
end -= 1 if end != -1 else 0
res.push_back(["subscription", parse_statement(line, string.substr(1, end))])
if end == -1:
string = ""
else:
string.erase(0, end + 3)
elif Detector.is_array(string):
var end = string.find("]")
var last_comma := 1
var comma := string.find(",")
var elements := []
while comma != -1:
var debug := string.substr(last_comma, comma - last_comma)
elements.push_back(parse_statement(line, string.substr(last_comma, comma - last_comma)))
last_comma = comma + 1
comma = string.find(",", last_comma)
elements.push_back(parse_statement(line, string.substr(last_comma, end - last_comma)))
res.push_back(["array", elements])
if end == -1:
string = ""
else:
string.erase(0, end + 1)
elif Detector.is_getnode(string):
var node_path = get_getnode(string.right(1))
if node_path.begins_with('"'):
node_path.erase(0, 1)
node_path.erase(node_path.length() - 1, 1)
res.push_back(["get_node", node_path])
string = string.substr(node_path.length() + 3)
else:
res.push_back(["get_node", node_path])
string = string.substr(node_path.length() + 1)
elif Detector.is_nodepath(string):
var nodepath = get_nodepath(string)
res.push_back(["nodepath", nodepath])
string = string.substr(nodepath.length() + 3)
elif Detector.is_constructor(string):
var dot = string.find(".")
var brace_content = get_brace_content(string)
var end = brace_content.length()
res.push_back([
"constructor",
string.substr(0, dot).strip_edges(),
parse_statement(line, "new(%s)" % brace_content)
])
string = string.substr(dot + end + 6)
elif Detector.is_method(string):
var m := ["method", string.substr(0, string.find("(")), []]
var brace_content := get_brace_content(string)
var last_comma := 0
var comma := Utility.find_not_in_string(brace_content, ",")
while comma != -1:
var s: String = brace_content.substr(last_comma, comma - last_comma).strip_edges()
if s.find("(") != -1:
var correct = get_correct_comma(brace_content.substr(last_comma))
if correct != -1:
s = brace_content.substr(last_comma, correct).strip_edges()
comma = last_comma + correct
else:
comma = -1
if !s.empty():
m[2].push_back(parse_statement(line, s))
last_comma = comma + 1
comma = get_correct_comma(brace_content, last_comma)
var s = brace_content.substr(last_comma).strip_edges()
if !s.empty():
m[2].push_back(parse_statement(line, s))
string = string.substr(m[1].length() + brace_content.length() + 2)
res.push_back(m)
elif Detector.is_group(string):
var position := 0
var braces := 0
for character in string:
if character == "(":
braces += 1
elif character == ")":
braces -= 1
if braces == 0:
break
position += 1
res.push_back(["group", parse_statement(line, string.substr(1, position - 1))])
string.erase(0, position + 1)
elif Detector.is_is(string):
res.push_back(["is", get_isas(string)])
string = ""
elif !skip_math && Detector.is_math(string):
var math = Utility.split_math(string)
var data = ["math", parse_statement(line, math[0]),
math[1], parse_statement(line, math[2])]
for d in data[1]:
if d[0] == "subscription":
skip_math = true;
break
if skip_math:
continue
res.push_back(data)
print(res[-1])
string = ""
elif Detector.is_bitwise(string):
var bitwise = Utility.split_bitwise(string)
res.push_back(["bitwise", parse_statement(line, bitwise[0]),
bitwise[1], parse_statement(line, bitwise[2])])
string = ""
elif Detector.is_comparison(string):
var comparison = Utility.split_comparison(string)
res.push_back(["comparison", parse_statement(line, comparison[0]),
comparison[1], parse_statement(line, comparison[2])])
string = ""
elif Detector.is_negation(string):
res.push_back(["negation"])
if string[0] == "!":
string.erase(0, 1)
else:
string.erase(0, 3)
elif Detector.is_as(string):
res.push_back(["as", get_isas(string)])
string = ""
elif Detector.is_in(string):
var sides := get_in(string)
res.push_back(["in", parse_statement(line, sides[0]), parse_statement(line, sides[1] if sides.size() > 1 else [])])
string = ""
elif Detector.is_assignment(string):
var assignment = Utility.split_assignment(string)
res.push_back(["assignment", parse_statement(line, assignment[0]),
assignment[1], parse_statement(line, assignment[2])])
string = ""
elif Detector.is_constant(string):
res.push_back(["const", string])
string = ""
elif Detector.is_var(string):
var dot = string.find(".")
var bracket = string.find("[")
#var space = string.find (" ")
var use_dot = (dot < bracket && dot != -1) || (bracket == -1)
var var_end = dot if use_dot else bracket
#var_end = space if space < var_end && space != -1 else var_end
res.push_back(["var", string.substr(0, var_end)])
if var_end != -1:
string = string.substr(var_end + (1 if use_dot else 0))
else:
string = ""
# Reset skipper
if string.begins_with("."):
# Workaround. Maybe put into attribute?
string.erase(0, 1)
skip_math = false
if !string.empty():
# Something unspecified is left (Likely a problem of this func not parsing everything)
res.push_back(["?", string])
return res
## Parses variable declaration and does basic type guessing
## Structure: [name, type, default_value, type_unsafe:bool]
static func parse_variable_d(string: String, gsv, lsv, is_const := false) -> Array:
var result = [null, null, null, false]
if Detector.is_typed_declaration(string) && !get_type_from_td(string).empty():
result[1] = get_type_from_td(string)
var vname = Utility.get_var_name_from_d(string, is_const)
if !vname.empty():
result[0] = vname
if Detector.is_initialization(string):
var val := get_assignment(string)
if !val.empty():
if result[1] == null:
# We try to deduct the type of the assignment
if val.is_valid_integer():
result[1] = "int"
result[3] = 1
elif val.is_valid_float():
result[1] = "float"
result[3] = 1
elif Detector.is_string(val) || Detector.is_constructor(val) || Detector.is_builtin(val):
result[1] = "var" # We use var here, since a string is safely deducted
elif Detector.is_local_var(val, lsv):
result[1] = Utility.get_var_in_local_vars(val, lsv)[1]
result[3] = 2
elif Detector.is_global_var(val, gsv):
result[2] = gsv[val][1]
result[3] = 2
result[2] = val
return result