Skip to content

Commit 08adf74

Browse files
committed
Fix lua 5.4 operator precedence, fix #59
1 parent 29cd67c commit 08adf74

File tree

7 files changed

+391
-315
lines changed

7 files changed

+391
-315
lines changed

cmd/gen.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/zsh
2+
3+
java -jar ~/Downloads/antlr-4.13.2-complete.jar -visitor -Dlanguage=Python3 luaparser/parser/LuaParser.g4

luaparser/parser/LuaParser.g4

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,13 @@ exp
9898
| exp op = ('*' | '/' | '%' | '//') exp
9999
| exp op = ('+' | '-') exp
100100
| <assoc = right> exp (op = '..') exp
101+
| exp op = ('<<' | '>>') exp
102+
| exp (op = '&') exp
103+
| exp (op = '~') exp
104+
| exp (op = '|') exp
101105
| exp op = ('<' | '>' | '<=' | '>=' | '~=' | '==') exp
102106
| exp (op = 'and') exp
103107
| exp (op = 'or') exp
104-
| exp op = ('&' | '|' | '~' | '<<' | '>>') exp
105108
;
106109

107110
// var ::= Name | prefixexp '[' exp ']' | prefixexp '.' Name

luaparser/parser/LuaParser.interp

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

luaparser/parser/LuaParser.py

Lines changed: 369 additions & 311 deletions
Large diffs are not rendered by default.

luaparser/parser/LuaParserListener.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated from ./luaparser/parser/LuaParser.g4 by ANTLR 4.13.2
1+
# Generated from luaparser/parser/LuaParser.g4 by ANTLR 4.13.2
22
from antlr4 import *
33
if "." in __name__:
44
from .LuaParser import LuaParser

luaparser/parser/LuaParserVisitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated from ./luaparser/parser/LuaParser.g4 by ANTLR 4.13.2
1+
# Generated from luaparser/parser/LuaParser.g4 by ANTLR 4.13.2
22
from antlr4 import *
33
if "." in __name__:
44
from .LuaParser import LuaParser

luaparser/tests/test_integration.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,15 @@ def test_cont_int_13(self):
372372
])
373373
)
374374
self.assertEqual(exp, tree)
375+
376+
# Bitwise operators mis-parsed #59
377+
def test_cont_int_14(self):
378+
source = "x = h1 >> 6 | h2 << 20"
379+
tree = ast.parse(source)
380+
exp = Chunk(
381+
Block([
382+
Assign([Name("x")], [BOrOp(left=BShiftROp(Name("h1"), Number(6)), right=BShiftLOp(Name("h2"), Number(20)))]),
383+
])
384+
)
385+
self.assertEqual(source, ast.to_lua_source(ast.parse(source)))
386+
self.assertEqual(exp, tree)

0 commit comments

Comments
 (0)