Skip to content

Commit

Permalink
Merge branch 'Tomclancysi-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
boolangery committed Oct 20, 2024
2 parents 478542a + 6ce8ba8 commit 2465125
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 28 deletions.
76 changes: 48 additions & 28 deletions luaparser/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,16 +749,20 @@ def parse_expr_list(self) -> List[Expression] or bool:
def parse_do_block(self) -> Block or bool:
self.save()
if self.next_is_rc(Tokens.DO, False):
t: Token = self._LT
self.handle_hidden_right()
block = self.parse_block()
if block:
if self.next_is_rc(Tokens.END):
block.first_token = t
block.last_token = self._LT
self.success()
return block
return self.failure()

def parse_while_stat(self) -> While or bool:
self.save()
first_token = self._stream.LT(1)
if self.next_is_rc(Tokens.WHILE):
self._expected = []
expr = self.parse_expr()
Expand All @@ -767,13 +771,14 @@ def parse_while_stat(self) -> While or bool:
body = self.parse_do_block()
if body:
self.success()
return While(expr, body)
return While(expr, body, first_token=first_token, last_token=body.last_token)
self.abort()

return self.failure()

def parse_repeat_stat(self) -> Repeat or bool:
self.save()
first_token = self._stream.LT(1)
if self.next_is_rc(Tokens.REPEAT, False):
self.handle_hidden_right()
body = self.parse_block()
Expand All @@ -782,7 +787,7 @@ def parse_repeat_stat(self) -> Repeat or bool:
expr = self.parse_expr()
if expr:
self.success()
return Repeat(body, expr)
return Repeat(body, expr, first_token=first_token, last_token=expr.last_token)

return self.failure()

Expand Down Expand Up @@ -851,6 +856,7 @@ def parse_goto_stat(self) -> Goto or bool:

def parse_if_stat(self) -> If or bool:
self.save()
first_token = self._stream.LT(1)
if self.next_is_rc(Tokens.IFTOK):
self._expected = []
test = self.parse_expr()
Expand All @@ -859,7 +865,7 @@ def parse_if_stat(self) -> If or bool:
self.handle_hidden_right()
body = self.parse_block()
if body:
main = If(test, body, None)
main = If(test, body, None, first_token=first_token)
root = main
while True: # zero or more
orelse = self.parse_elseif_stat()
Expand All @@ -872,14 +878,17 @@ def parse_if_stat(self) -> If or bool:
else_exp = self.parse_else_stat() # optional
if else_exp:
root.orelse = else_exp
last_token = self._stream.LT(1)
if self.next_is_rc(Tokens.END):
main.last_token = last_token
self.success()
return main
self.abort()
return self.failure()

def parse_elseif_stat(self) -> ElseIf or bool:
self.save()
first_token = self._stream.LT(1)
if self.next_is_rc(Tokens.ELSEIF):
test = self.parse_expr()
if test:
Expand All @@ -888,7 +897,7 @@ def parse_elseif_stat(self) -> ElseIf or bool:
body = self.parse_block()
if body:
self.success()
return ElseIf(test, body, None) # orelse will be set in parent
return ElseIf(test, body, None, first_token=first_token, last_token=body.last_token) # orelse will be set in parent
return self.failure()

def parse_else_stat(self) -> Block or bool:
Expand All @@ -904,6 +913,7 @@ def parse_else_stat(self) -> Block or bool:

def parse_for_stat(self) -> Fornum or Forin or bool:
self.save()
first_token = self._stream.LT(1)
if self.next_is_rc(Tokens.FOR):
self.save()
if self.next_is_rc(Tokens.NAME):
Expand All @@ -930,7 +940,7 @@ def parse_for_stat(self) -> Fornum or Forin or bool:
return self.failure()
self.success()
self.success()
return Fornum(target, start, stop, step, body)
return Fornum(target, start, stop, step, body, first_token=first_token, last_token=body.last_token)

self.failure_save()
target = self.parse_name_list()
Expand All @@ -941,7 +951,7 @@ def parse_for_stat(self) -> Fornum or Forin or bool:
if body:
self.success()
self.success()
return Forin(body, iter_expr, target)
return Forin(body, iter_expr, target, first_token=first_token, last_token=body.last_token)
self.failure()

return self.failure()
Expand Down Expand Up @@ -1050,17 +1060,19 @@ def parse_param_list(self) -> List[Expression] or bool:
if param_list:
self.save()
if self.next_is_rc(Tokens.COMMA) and self.next_is_rc(Tokens.VARARGS):
t: Token = self._LT
self.success()
param_list.append(Varargs())
param_list.append(Varargs(first_token=t, last_token=t))
return param_list
else:
self.failure()
return param_list

self.save()
if self.next_is_rc(Tokens.VARARGS):
t: Token = self._LT
self.success()
return [Varargs()]
return [Varargs(first_token=t, last_token=t)]

self.success()
return []
Expand Down Expand Up @@ -1103,8 +1115,9 @@ def parse_label(self) -> Label or bool:
last_token=self._LT,
)
if self.next_is_rc(Tokens.COLCOL):
t: Token = self._LT
self.success()
return Label(name)
return Label(name, first_token=t, last_token=t)

return self.failure()

Expand Down Expand Up @@ -1141,7 +1154,7 @@ def parse_or_expr(self) -> Expression or Literal[False]:
right = self.parse_and_expr()
if right:
self.success()
left = OrLoOp(left, right)
left = OrLoOp(left, right, first_token=left.first_token, last_token=right.last_token)
else:
self.failure()
return self.failure()
Expand All @@ -1163,7 +1176,7 @@ def parse_and_expr(self) -> Expression or bool:
right = self.parse_rel_expr()
if right:
self.success()
left = AndLoOp(left, right)
left = AndLoOp(left, right, first_token=left.first_token, last_token=right.last_token)
else:
self.failure()
return self.failure()
Expand All @@ -1186,17 +1199,17 @@ def parse_rel_expr(self) -> Expression or bool:
if right:
self.success()
if op == Tokens.LT:
left = LessThanOp(left, right)
left = LessThanOp(left, right, first_token=left.first_token, last_token=right.last_token)
elif op == Tokens.GT:
left = GreaterThanOp(left, right)
left = GreaterThanOp(left, right, first_token=left.first_token, last_token=right.last_token)
elif op == Tokens.LTEQ:
left = LessOrEqThanOp(left, right)
left = LessOrEqThanOp(left, right, first_token=left.first_token, last_token=right.last_token)
elif op == Tokens.GTEQ:
left = GreaterOrEqThanOp(left, right)
left = GreaterOrEqThanOp(left, right, first_token=left.first_token, last_token=right.last_token)
elif op == Tokens.NEQ:
left = NotEqToOp(left, right)
left = NotEqToOp(left, right, first_token=left.first_token, last_token=right.last_token)
elif op == Tokens.EQ:
left = EqToOp(left, right)
left = EqToOp(left, right, first_token=left.first_token, last_token=right.last_token)
else:
self.failure()
return self.failure()
Expand Down Expand Up @@ -1314,7 +1327,7 @@ def parse_concat_expr(self) -> Expression or bool:
right = self.parse_add_expr()
if right:
self.success()
left = Concat(left, right)
left = Concat(left, right, first_token=left.first_token, last_token=right.last_token)
else:
self.failure()
self.failure()
Expand All @@ -1339,9 +1352,9 @@ def parse_add_expr(self) -> Expression or bool:
if right:
self.success()
if op == Tokens.ADD:
left = AddOp(left, right)
left = AddOp(left, right, first_token=left.first_token, last_token=right.last_token)
elif op == Tokens.MINUS:
left = SubOp(left, right)
left = SubOp(left, right, first_token=left.first_token, last_token=right.last_token)
else:
self.failure()
return self.failure()
Expand All @@ -1365,13 +1378,13 @@ def parse_mult_expr(self) -> Expression or bool:
if right:
self.success()
if op == Tokens.MULT:
left = MultOp(left, right)
left = MultOp(left, right, first_token=left.first_token, last_token=right.last_token)
elif op == Tokens.DIV:
left = FloatDivOp(left, right)
left = FloatDivOp(left, right, first_token=left.first_token, last_token=right.last_token)
elif op == Tokens.MOD:
left = ModOp(left, right)
left = ModOp(left, right, first_token=left.first_token, last_token=right.last_token)
elif op == Tokens.FLOOR:
left = FloorDivOp(left, right)
left = FloorDivOp(left, right, first_token=left.first_token, last_token=right.last_token)
else:
self.failure()
return self.failure()
Expand Down Expand Up @@ -1530,10 +1543,12 @@ def parse_function_literal(self) -> AnonymousFunction or bool:
def parse_table_constructor(self, render_last_hidden=True) -> Table or bool:
self.save()
if self.next_is_rc(Tokens.OBRACE, False): # do not render right hidden
first_token = self._LT
self.handle_hidden_right() # render hidden after new level

fields = self.parse_field_list()
if self.next_is_rc(Tokens.CBRACE, render_last_hidden):
last_token = self._LT
self.success()

array_like_index = 1
Expand All @@ -1544,7 +1559,7 @@ def parse_table_constructor(self, render_last_hidden=True) -> Table or bool:
field.between_brackets = True
array_like_index += 1

return Table(fields or [])
return Table(fields or [], first_token=first_token, last_token=last_token)

return self.failure()

Expand Down Expand Up @@ -1583,20 +1598,24 @@ def parse_field(self) -> Tuple[Field or bool, Comments]:
self.save()

if self.next_is_rc(Tokens.OBRACK):
first_token: Token = self._LT
key = self.parse_expr()
if key and self.next_is_rc(Tokens.CBRACK):
if self.next_is_rc(Tokens.ASSIGN):
comments = self.get_comments()
value = self.parse_expr()
if value:
last_token = value.last_token
self.success()
return (
Field(key, value, comments=comments, between_brackets=True),
Field(key, value, comments=comments, between_brackets=True,
first_token=first_token, last_token=last_token),
comments,
)

self.failure_save()
if self.next_is_rc(Tokens.NAME):
first_token = self._LT
key = Name(
self.text,
first_token=self._LT,
Expand All @@ -1605,9 +1624,10 @@ def parse_field(self) -> Tuple[Field or bool, Comments]:
if self.next_is_rc(Tokens.ASSIGN):
comments = self.get_comments()
value = self.parse_expr()
last_token = value.last_token
if value:
self.success()
return Field(key, value, comments=comments), comments
return Field(key, value, comments=comments, first_token=first_token, last_token=last_token), comments

self.failure_save()
comments = self.get_comments()
Expand All @@ -1616,7 +1636,7 @@ def parse_field(self) -> Tuple[Field or bool, Comments]:
self.success()
# noinspection PyTypeChecker
return (
Field(None, value, comments=comments),
Field(None, value, comments=comments, first_token=value.first_token, last_token=value.last_token),
[],
) # Key will be set in parse_table_constructor

Expand Down
Loading

0 comments on commit 2465125

Please sign in to comment.