Skip to content

Commit 45bd09c

Browse files
committed
TableField.key is optional if generatedKey is true.
Fixed parsing error for chunks ending in return statements without values.
1 parent c3b7fe4 commit 45bd09c

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

dumbParser.lua

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ local function AstLiteral (token,v)return populateCommonNodeFields(token,{
629629
})end
630630
local function AstTable (token)return populateCommonNodeFields(token,{
631631
type = "table",
632-
fields = {}, -- Array of {key=expression, value=expression, generatedKey=bool}. generatedKey is true for implicit keys (i.e. {x,y}) and false for explicit keys (i.e. {a=x,b=y}). Note that the state of generatedKey affects the output of toLua()!
632+
fields = {}, -- Array of {key=expression, value=expression, generatedKey=bool}. generatedKey is true for implicit keys (i.e. {x,y}) and false for explicit keys (i.e. {a=x,b=y}). Note that the state of generatedKey affects the output of toLua()! 'key' may be nil if generatedKey is true.
633633
})end
634634
local function AstLookup (token)return populateCommonNodeFields(token,{
635635
type = "lookup",
@@ -2471,7 +2471,11 @@ local function parseOneOrPossiblyMoreStatements(tokens, tokStart, statements) --
24712471
local returnNode = AstReturn(currentToken)
24722472
tok = tok + 1 -- 'return'
24732473

2474-
if tokens[tok] and not ((isTokenType(tokens[tok], "keyword") and isTokenAnyValue(tokens[tok], BLOCK_END_TOKEN_TYPES)) or isToken(tokens[tok], "punctuation", ";")) then
2474+
if tokens[tok] and not (
2475+
(isTokenType(tokens[tok], "keyword") and isTokenAnyValue(tokens[tok], BLOCK_END_TOKEN_TYPES))
2476+
or isToken(tokens[tok], "punctuation", ";")
2477+
or isTokenType(tokens[tok], "end")
2478+
) then
24752479
local ok, tokNext, err = parseExpressionList(tokens, tok, returnNode.values)
24762480
if not ok then return false, tok, err end
24772481
tok = tokNext
@@ -3136,8 +3140,7 @@ do
31363140
indent = indent+1
31373141

31383142
if key ~= nil then
3139-
ioWrite(tostring(key))
3140-
ioWrite(" ")
3143+
ioWrite(tostring(key), " ")
31413144
end
31423145

31433146
_printNode(node)
@@ -3146,8 +3149,13 @@ do
31463149

31473150
if nodeType == "table" then
31483151
for i, tableField in ipairs(node.fields) do
3149-
if tableField.key then _printTree(tableField.key, indent, i..(tableField.generatedKey and "KEYGEN" or "KEY ")) end
3150-
if tableField.value then _printTree(tableField.value, indent, i..( "VALUE ")) end
3152+
if tableField.key then
3153+
_printTree(tableField.key, indent, i..(tableField.generatedKey and "KEYGEN" or "KEY "))
3154+
elseif tableField.generatedKey then
3155+
for i = 1, indent do ioWrite(parser.indentation) end
3156+
ioWrite(i, "KEYGEN -\n")
3157+
end
3158+
if tableField.value then _printTree(tableField.value, indent, i.."VALUE ") end
31513159
end
31523160

31533161
elseif nodeType == "lookup" then
@@ -5210,6 +5218,7 @@ do
52105218
lastOutput = writeNumber(buffer, pretty, literal.value, lastOutput)
52115219

52125220
elseif type(literal.value) == "string" then
5221+
-- @Speed: Cache!
52135222
local R = isNumberInRange
52145223
local s = literal.value
52155224
local doubleCount = countString(s, '"', true)
@@ -5272,7 +5281,7 @@ do
52725281
end
52735282

52745283
if tableField.generatedKey then
5275-
if nodeCb then nodeCb(tableField.key, buffer) end
5284+
if nodeCb and tableField.key then nodeCb(tableField.key, buffer) end
52765285

52775286
else
52785287
if canNodeBeName(tableField.key) then
@@ -5724,6 +5733,8 @@ end
57245733

57255734

57265735
function formatNumber(n)
5736+
-- @Speed: Cache!
5737+
57275738
-- 64-bit int in LuaJIT (is what we assume, anyway).
57285739
if jit and type(n) == "cdata" then
57295740
local nStr = tostring(n)
@@ -6243,7 +6254,9 @@ do
62436254
for i, tableField in ipairs(tableNode.fields) do
62446255
-- @Incomplete: Should we detect nil literal keys? :DetectRuntimeErrors
62456256
if not tableField.key then
6257+
if not tableField.generatedKey then
62466258
addValidationError(path, errors, "Missing 'key' field for table field %d.", i)
6259+
end
62476260
elseif not EXPRESSION_NODES[tableField.key.type] then
62486261
addValidationError(path, errors, "The key for table field %d is not an expression. (It is '%s'.)", i, tableField.key.type)
62496262
elseif tableField.generatedKey and tableField.key.type ~= "literal" then

test.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,5 @@ do
148148
--]]
149149
end
150150
--]===]
151+
152+
return

0 commit comments

Comments
 (0)