boolangery / py-lua-parser

A Lua parser and AST builder written in Python.
MIT License
123 stars 39 forks source link

"expected chunk" but actually "didn't expect \\n or maybe <" #40

Closed Starshine09 closed 3 weeks ago

Starshine09 commented 1 year ago

Hello,

I'm being stubborn and trying to do things outside my experience level, so I apologize if this is a dumb noob error. I haven't even tried to debug it properly yet but I did manage to find the smoking gun or maybe the smoke-in-mirrors; I see what went wrong but not why and have no idea how to investigate. I attached the source file and parsed tree for reference. Thank you for your work with this; I should be able to continue working through the file now in any case!

I went to try to "walk the tree" and went down a bit of a rabbit hole not realizing that "Chunk" was not actually a Python thing at all before digging into the error...


"E:\Python Projects\ScaleConstructor\venv2\Scripts\python.exe" "E:\Python Projects\ScaleConstructor\convert_from_lua.py" 
Traceback (most recent call last):
  File "E:\Python Projects\ScaleConstructor\convert_from_lua.py", line 11, in <module>
    tree = ast.parse(text)
           ^^^^^^^^^^^^^^^
  File "E:\Python Projects\ScaleConstructor\venv2\Lib\site-packages\luaparser\ast.py", line 14, in parse
    return Builder(source).process()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\Python Projects\ScaleConstructor\venv2\Lib\site-packages\luaparser\builder.py", line 231, in process
    raise SyntaxException("Expecting a chunk")
luaparser.builder.SyntaxException: Error: Expecting a chunk

chonker = open("rig","r")
text = str(chonker)
tree = ast.parse(text)
print(ast.to_pretty_str(tree))

my ultra basic debug output I stuck in places found this:

<luaparser.astnodes.Block object at 0x0000019DDA361DD0>
[@0,0:0='<',<35>,1:0]

the '<' looked out of place here, but wait, there's more...

"String": {
"s": "See the ShowDemoWindow() code in ReaImGui_Demo.lua. <- you are here!",
"delimiter": {},
"stop_char": 15553,
"line": 389
}

what I suspect could be the sneaky escape artist literally just a scroll away

"String": {
"s": "The \"Tools\" menu above gives access to: About Box, Style Editor,\\n",
"delimiter": {},
"start_char": 15243,
"stop_char": 15310,
"line": 384
}

escape_char_issue.zip

fabi321 commented 1 year ago

One issue I see with

chonker = open("rig","r")
text = str(chonker)
tree = ast.parse(text)
print(ast.to_pretty_str(tree))

is, that this is never actually reading the file. if you want to read a file, use chonker.read() instead of str(chonker) str(chonker) looks something along the lines like this: <_io.TextIOWrapper name='chonker' mode='r' encoding='UTF-8'>