Skip to content

Commit d83a8e3

Browse files
committed
Merge branch 'master' into poc_add_line_numbers
2 parents 6b8b346 + bba8f39 commit d83a8e3

File tree

4 files changed

+111
-14
lines changed

4 files changed

+111
-14
lines changed

luaparser/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.0.0"
1+
__version__ = "3.0.1"

luaparser/builder.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from luaparser.astnodes import *
77
from luaparser.parser.LuaLexer import LuaLexer
8+
from typing import List, Tuple
9+
from antlr4.Token import Token
810

911

1012
class SyntaxException(Exception):
@@ -1485,7 +1487,7 @@ def parse_table_constructor(self, render_last_hidden=True) -> Table or bool:
14851487
def parse_field_list(self) -> List[Field] or bool:
14861488
field_list = []
14871489
self.save()
1488-
field = self.parse_field()
1490+
field, _ = self.parse_field()
14891491
if field:
14901492
field_list.append(field)
14911493
while True:
@@ -1494,16 +1496,17 @@ def parse_field_list(self) -> List[Field] or bool:
14941496
inline_com = self.get_inline_comment()
14951497
if inline_com:
14961498
field.comments.append(inline_com)
1497-
field = self.parse_field()
1499+
prev_field = field
1500+
field, remaining_comments = self.parse_field()
14981501
if field:
14991502
field_list.append(field)
15001503
self.success()
15011504
else:
1505+
prev_field.comments.extend(remaining_comments)
15021506
self.success()
15031507
self.success()
15041508
return field_list
15051509
else:
1506-
15071510
field.comments.extend(self.get_comments())
15081511
self.failure()
15091512
break
@@ -1512,7 +1515,7 @@ def parse_field_list(self) -> List[Field] or bool:
15121515
return field_list
15131516
return self.failure()
15141517

1515-
def parse_field(self) -> Field or bool:
1518+
def parse_field(self) -> Tuple[Field or bool, Comments]:
15161519
self.save()
15171520

15181521
if self.next_is_rc(Tokens.OBRACK):
@@ -1523,8 +1526,9 @@ def parse_field(self) -> Field or bool:
15231526
value = self.parse_expr()
15241527
if value:
15251528
self.success()
1526-
return Field(
1527-
key, value, comments=comments, between_brackets=True
1529+
return (
1530+
Field(key, value, comments=comments, between_brackets=True),
1531+
comments,
15281532
)
15291533

15301534
self.failure_save()
@@ -1539,19 +1543,20 @@ def parse_field(self) -> Field or bool:
15391543
value = self.parse_expr()
15401544
if value:
15411545
self.success()
1542-
return Field(key, value, comments=comments)
1546+
return Field(key, value, comments=comments), comments
15431547

15441548
self.failure_save()
15451549
comments = self.get_comments()
15461550
value = self.parse_expr()
15471551
if value:
15481552
self.success()
15491553
# noinspection PyTypeChecker
1550-
return Field(
1551-
None, value, comments=comments
1554+
return (
1555+
Field(None, value, comments=comments),
1556+
[],
15521557
) # Key will be set in parse_table_constructor
15531558

1554-
return self.failure()
1559+
return self.failure(), comments
15551560

15561561
def parse_field_sep(self) -> bool:
15571562
self.save()

luaparser/printers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ def visit(self, obj):
192192
xml_nodes.append(self.visit(itemValue))
193193
return xml_nodes
194194

195+
@visitor(Enum)
196+
def visit(self, node):
197+
return str(node)
198+
195199
@visitor(Node)
196200
def visit(self, node):
197201
xml_node = ElementTree.Element(node.display_name)

luaparser/tests/test_integration.py

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from luaparser.utils import tests
1+
import textwrap
2+
23
from luaparser import ast
34
from luaparser.astnodes import *
4-
import textwrap
5+
from luaparser.utils import tests
56

67

78
class IntegrationTestCase(tests.TestCase):
@@ -128,7 +129,9 @@ def test_cont_int_3(self):
128129
exp = Chunk(Block([Index(idx=Name("a"), value=Name("x"))]))
129130
self.assertEqual(exp, tree)
130131

131-
# luaparser.utils.visitor.VisitorException: No visitor found for class <enum 'StringDelimiter'> #11
132+
# luaparser.utils.visitor.VisitorException: No visitor found for class <enum 'StringDelimiter'>
133+
# #11
134+
# #14
132135
def test_cont_int_4(self):
133136
tree = ast.parse(
134137
textwrap.dedent(
@@ -174,3 +177,88 @@ def test_cont_int_4(self):
174177
args: [] 0 item"""
175178
)
176179
self.assertEqual(exp, pretty_str)
180+
ast.to_xml_str(tree)
181+
182+
# Cant walk the ast tree if lua file has semicolon(;) or repeat until loop and multiple args(...) #9
183+
def test_cont_int_5(self):
184+
tree = ast.parse(
185+
textwrap.dedent(
186+
"""
187+
function table.pack(...)
188+
repeat
189+
print("value of a:", a)
190+
a = a + 1;
191+
until( a > 15 )
192+
end
193+
"""
194+
)
195+
)
196+
nodes = ast.walk(tree)
197+
expected_cls = [
198+
Chunk,
199+
Block,
200+
Function,
201+
Index,
202+
Name,
203+
Name,
204+
Varargs,
205+
Block,
206+
Repeat,
207+
Block,
208+
Call,
209+
Name,
210+
String,
211+
Name,
212+
Assign,
213+
Name,
214+
AddOp,
215+
Name,
216+
Number,
217+
SemiColon,
218+
GreaterThanOp,
219+
Name,
220+
Number,
221+
]
222+
for node, exp in zip(nodes, expected_cls):
223+
self.assertIsInstance(node, exp)
224+
225+
# Comments are ignored if they are written after the last line of a table ending with a comma #15
226+
def test_cont_int_6(self):
227+
tree = ast.parse(
228+
textwrap.dedent(
229+
"""
230+
foo = {
231+
mykey = "myvalue",
232+
-- this comment is ignored if previous line ends with a comma
233+
}
234+
"""
235+
)
236+
)
237+
exp = Chunk(
238+
Block(
239+
[
240+
Assign(
241+
[Name("foo")],
242+
[
243+
Table(
244+
[
245+
Field(
246+
Name("mykey"),
247+
String(
248+
"myvalue",
249+
delimiter=StringDelimiter.DOUBLE_QUOTE,
250+
),
251+
comments=[
252+
Comment(
253+
"-- this comment is ignored if previous line ends with a comma"
254+
)
255+
],
256+
),
257+
]
258+
)
259+
],
260+
)
261+
]
262+
)
263+
)
264+
self.assertEqual(exp, tree)

0 commit comments

Comments
 (0)