Skip to content

Commit

Permalink
evaluate Lua string, fix #43
Browse files Browse the repository at this point in the history
  • Loading branch information
boolangery committed Oct 27, 2024
1 parent 7bf6bcf commit fe8e483
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions luaparser/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,4 +659,9 @@ def visitString(self, ctx: LuaParser.StringContext):
elif p.match(lua_str):
lua_str = p.search(lua_str).group(1)

# Eval string to unescape:
try:
lua_str = ast.literal_eval(F'"{lua_str}"')
except:
pass
return String(lua_str, delimiter)
16 changes: 15 additions & 1 deletion luaparser/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ def test_cont_int_10(self):
exp = Chunk(
Block([
If(
test=AndLoOp(left=GreaterThanOp(left=ULengthOP(Name("setting")), right=Number(10)), right=EqToOp(left=Name("setting_name"), right=String("user", StringDelimiter.DOUBLE_QUOTE))),
test=AndLoOp(left=GreaterThanOp(left=ULengthOP(Name("setting")), right=Number(10)),
right=EqToOp(left=Name("setting_name"),
right=String("user", StringDelimiter.DOUBLE_QUOTE))),
body=Block([
Return([Number(100)])
]),
Expand All @@ -330,3 +332,15 @@ def test_cont_int_10(self):
])
)
self.assertEqual(exp, tree)

# Strings are not being parsed #43
def test_cont_int_12(self):
tree = ast.parse(textwrap.dedent("""
a='\\0\\n\\ta'
"""))
exp = Chunk(
Block([
Assign([Name("a")], [String("\x00\n\ta", StringDelimiter.SINGLE_QUOTE)])
])
)
self.assertEqual(exp, tree)
2 changes: 1 addition & 1 deletion luaparser/tests/test_types_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_string_escape(self):
targets=[Name("b")],
values=[
String(
r"one line\nnext line\n\"in quotes\", 'in quotes'",
"one line\nnext line\n\"in quotes\", 'in quotes'",
StringDelimiter.DOUBLE_QUOTE,
)
],
Expand Down

0 comments on commit fe8e483

Please sign in to comment.