Skip to content

Commit 3513d53

Browse files
committed
Fixed lookups and calls that shouldn't parse.
1 parent 3474f70 commit 3513d53

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

Changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Changelog
22
Dumb Lua Parser
33

4+
v1.2.1 (2021-06-01)
5+
- Fixed lookups and calls that shouldn't parse, like "":sub().
6+
47
v1.2.0 (2021-05-13)
58
- Added parser.updateReferences().
69
- Changed arguments for parser.traverseTree() and the callback.

dumbParser.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
--= Dumb Lua Parser - Lua parsing library
44
--= by Marcus 'ReFreezed' Thunström
55
--=
6-
--= v1.2 (2021-05-13)
6+
--= v1.2.1 (2021-06-01)
77
--=
88
--= License: MIT (see the bottom of this file)
99
--= Website: https://github.com/ReFreezed/DumbLuaParser
@@ -154,7 +154,7 @@
154154
155155
--============================================================]]
156156

157-
local PARSER_VERSION = "1.2.0"
157+
local PARSER_VERSION = "1.2.1"
158158

159159
local F = string.format
160160
local find = string.find
@@ -1006,14 +1006,16 @@ end
10061006

10071007
function parseExpression(tokens, tok, lastPrecedence) --> expression, token
10081008
local expr
1009+
local canParseLookupOrCall = false
10091010

10101011
-- identifier
10111012
if isTokenType(tokens, tok, "identifier") then
10121013
local ident, tokNext = parseIdentifier(tokens, tok)
10131014
if not ident then return false, tok end
10141015
tok = tokNext
10151016

1016-
expr = ident
1017+
expr = ident
1018+
canParseLookupOrCall = true
10171019

10181020
-- ...
10191021
elseif isToken(tokens, tok, "punctuation", "...") then
@@ -1096,7 +1098,8 @@ function parseExpression(tokens, tok, lastPrecedence) --> expression, token
10961098
end
10971099
tok = tok + 1 -- ')'
10981100

1099-
expr = _expr
1101+
expr = _expr
1102+
canParseLookupOrCall = true
11001103

11011104
else
11021105
reportErrorAtToken(tokens, tok, "Parser", "Failed parsing expression.")
@@ -1105,7 +1108,7 @@ function parseExpression(tokens, tok, lastPrecedence) --> expression, token
11051108

11061109
assert(expr)
11071110

1108-
-- Binary expressions etc.
1111+
-- Binary expressions, including lookups and calls.
11091112
while true do
11101113
-- a + b
11111114
if
@@ -1131,6 +1134,9 @@ function parseExpression(tokens, tok, lastPrecedence) --> expression, token
11311134

11321135
expr = binary
11331136

1137+
elseif not canParseLookupOrCall then
1138+
break
1139+
11341140
-- t.k
11351141
elseif isToken(tokens, tok, "punctuation", ".") then
11361142
local lookup = AstLookup(tok)
@@ -2969,7 +2975,7 @@ do
29692975
lastOutput = writeAlphanum(buffer, pretty, "end", lastOutput)
29702976

29712977
else
2972-
printfError("Error: Unknown node type '%s'.", nodeType)
2978+
printfError("Error: Unknown node type '%s'.", tostring(nodeType))
29732979
return false, lastOutput
29742980
end
29752981
return true, lastOutput
@@ -2978,6 +2984,8 @@ do
29782984
-- lua = toLua( astNode [, prettyOuput=false ] )
29792985
-- Returns nil on error.
29802986
function toLua(node, pretty)
2987+
if type(node) ~= "table" then error(F("bad argument #1 to 'toLua' (AST node expected, got %s)", type(node)), 2) end
2988+
29812989
local buffer = {}
29822990

29832991
local ok

runTest.lua

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ do
4545
print(luaEdit)
4646
end
4747

48-
assert(loadLuaString(lua, "@lua"))
48+
assert(loadLuaString(lua, "@<luastring>"))
4949

5050
-- Round-trip.
5151
local tripTokens = assert(parser.tokenizeString(lua))
@@ -77,7 +77,7 @@ do
7777
local lua = assert(parser.toLua(ast, pretty))
7878
print(lua)
7979

80-
assert(loadLuaString(lua, "@lua"))
80+
assert(loadLuaString(lua, "@<luastring>"))
8181
end
8282

8383
-- AST manipulations.
@@ -103,7 +103,7 @@ do
103103

104104
local block = assert(parser.parse([[
105105
local x = 49
106-
]], "<inline lua>"))
106+
]], "<luastring>"))
107107

108108
assert(block.type == "block")
109109
assert(block.statements[1])
@@ -121,7 +121,53 @@ do
121121
local lua = assert(parser.toLua(block, pretty))
122122
print(lua)
123123

124-
assert(loadLuaString(lua, "@lua"))
124+
assert(loadLuaString(lua, "@<luastring>"))
125125
end
126126

127-
print("Tests passed!")
127+
-- Tests that should fail.
128+
do
129+
assert( parser.parse([[ x = tbl () ]], "<luastring>"))
130+
assert( parser.parse([[ x = tbl "" ]], "<luastring>"))
131+
assert( parser.parse([[ x = tbl .m() ]], "<luastring>"))
132+
assert( parser.parse([[ x = tbl :m"" ]], "<luastring>"))
133+
assert( parser.parse([[ x = (tbl) () ]], "<luastring>"))
134+
assert( parser.parse([[ x = (tbl) "" ]], "<luastring>"))
135+
assert( parser.parse([[ x = (tbl).m() ]], "<luastring>"))
136+
assert( parser.parse([[ x = (tbl):m"" ]], "<luastring>"))
137+
assert(not parser.parse([[ x = " " () ]], "<luastring>"))
138+
assert(not parser.parse([[ x = " " "" ]], "<luastring>"))
139+
assert(not parser.parse([[ x = " " .m() ]], "<luastring>"))
140+
assert(not parser.parse([[ x = " " :m"" ]], "<luastring>"))
141+
assert( parser.parse([[ x = (" ") () ]], "<luastring>"))
142+
assert( parser.parse([[ x = (" ") "" ]], "<luastring>"))
143+
assert( parser.parse([[ x = (" ").m() ]], "<luastring>"))
144+
assert( parser.parse([[ x = (" "):m"" ]], "<luastring>"))
145+
assert(not parser.parse([[ x = { } () ]], "<luastring>"))
146+
assert(not parser.parse([[ x = { } "" ]], "<luastring>"))
147+
assert(not parser.parse([[ x = { } .m() ]], "<luastring>"))
148+
assert(not parser.parse([[ x = { } :m"" ]], "<luastring>"))
149+
assert( parser.parse([[ x = ({ }) () ]], "<luastring>"))
150+
assert( parser.parse([[ x = ({ }) "" ]], "<luastring>"))
151+
assert( parser.parse([[ x = ({ }).m() ]], "<luastring>"))
152+
assert( parser.parse([[ x = ({ }):m"" ]], "<luastring>"))
153+
assert(not parser.parse([[ x = 123 () ]], "<luastring>"))
154+
assert(not parser.parse([[ x = 123 "" ]], "<luastring>"))
155+
assert(not parser.parse([[ x = 123 .m() ]], "<luastring>"))
156+
assert(not parser.parse([[ x = 123 :m"" ]], "<luastring>"))
157+
assert( parser.parse([[ x = (123) () ]], "<luastring>"))
158+
assert( parser.parse([[ x = (123) "" ]], "<luastring>"))
159+
assert( parser.parse([[ x = (123).m() ]], "<luastring>"))
160+
assert( parser.parse([[ x = (123):m"" ]], "<luastring>"))
161+
assert(not parser.parse([[ x = nil () ]], "<luastring>"))
162+
assert(not parser.parse([[ x = nil "" ]], "<luastring>"))
163+
assert(not parser.parse([[ x = nil .m() ]], "<luastring>"))
164+
assert(not parser.parse([[ x = nil :m"" ]], "<luastring>"))
165+
assert( parser.parse([[ x = (nil) () ]], "<luastring>"))
166+
assert( parser.parse([[ x = (nil) "" ]], "<luastring>"))
167+
assert( parser.parse([[ x = (nil).m() ]], "<luastring>"))
168+
assert( parser.parse([[ x = (nil):m"" ]], "<luastring>"))
169+
end
170+
171+
print()
172+
print("Tests passed successfully!")
173+
print()

0 commit comments

Comments
 (0)