Skip to content

Commit 244780c

Browse files
committed
toLua: Fixed "." not always being always being considered when inserting spaces (e.g. for x .. ...).
toLua: Outputting less parentheses around some binary expressions.
1 parent cb5f843 commit 244780c

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

dumbParser.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4941,7 +4941,7 @@ do
49414941
local writeNode
49424942
local writeStatements
49434943

4944-
-- lastOutput = "" | "alphanum" | "number" | "-"
4944+
-- lastOutput = "" | "alphanum" | "number" | "-" | "."
49454945

49464946
local function isNumberInRange(n, min, max)
49474947
return n ~= nil and n >= min and n <= max
@@ -4970,7 +4970,7 @@ do
49704970
end
49714971
local function writeNumber(buffer, pretty, n, lastOutput)
49724972
local nStr = formatNumber(n)
4973-
if lastOutput == "-" and stringByte(nStr, 1) == 45--[[ "-" ]] then
4973+
if (lastOutput == "-" and stringByte(nStr, 1) == 45--[[ "-" ]]) or lastOutput == "." then
49744974
lastOutput = writeLua(buffer, " ", "")
49754975
else
49764976
ensureSpaceIfNotPretty(buffer, pretty, lastOutput, "alphanum","number")
@@ -5160,7 +5160,12 @@ do
51605160

51615161
if binary.operator == ".." then ensureSpaceIfNotPretty(buffer, pretty, lastOutput, "number") end
51625162

5163-
local nextOutput = ((binary.operator == "-" and "-") or (stringFind(binary.operator, "%w") and "alphanum") or (""))
5163+
local nextOutput = (
5164+
(binary.operator == "-" and "-" ) or
5165+
(binary.operator == ".." and "." ) or
5166+
(stringFind(binary.operator, "%w") and "alphanum") or
5167+
""
5168+
)
51645169
if nextOutput ~= "" then ensureSpaceIfNotPretty(buffer, pretty, lastOutput, nextOutput) end
51655170
lastOutput = writeLua(buffer, binary.operator, nextOutput)
51665171

@@ -5193,7 +5198,8 @@ do
51935198
elseif nodeType == "vararg" then
51945199
local vararg = node
51955200
if vararg.adjustToOne then lastOutput = writeLua(buffer, "(", "") end
5196-
lastOutput = writeLua(buffer, "...", "")
5201+
if lastOutput == "." then lastOutput = writeLua(buffer, " ", ".") end
5202+
lastOutput = writeLua(buffer, "...", ".")
51975203
if vararg.adjustToOne then lastOutput = writeLua(buffer, ")", "") end
51985204

51995205
elseif nodeType == "literal" then
@@ -5332,23 +5338,24 @@ do
53325338

53335339
elseif nodeType == "binary" then
53345340
local binary = node
5341+
local op = binary.operator
53355342

53365343
if not maySafelyOmitParens then lastOutput = writeLua(buffer, "(", "") end -- @Polish: Only output parentheses around child unaries/binaries if associativity requires it.
53375344

5338-
if binary.operator == ".." or binary.operator == "and" or binary.operator == "or" then
5345+
if op == ".." or op == "and" or op == "or" or op == "+" or op == "*" or op == "&" or op == "|" then
53395346
local ok;ok, lastOutput = writeBinaryOperatorChain(buffer, pretty, indent, lastOutput, binary, nodeCb)
53405347
if not ok then return nil, lastOutput end
53415348

53425349
else
53435350
local ok;ok, lastOutput = writeNode(buffer, pretty, indent, lastOutput, binary.left, false, nodeCb)
53445351
if not ok then return nil, lastOutput end
53455352

5346-
local operatorOutput = ((binary.operator == "-" and "-") or (stringFind(binary.operator, "%w") and "alphanum") or (""))
5353+
local operatorOutput = ((op == "-" and "-") or (stringFind(op, "%w") and "alphanum") or (""))
53475354

53485355
if pretty then lastOutput = writeLua(buffer, " ", "") end
53495356

53505357
if operatorOutput ~= "" then ensureSpaceIfNotPretty(buffer, pretty, lastOutput, operatorOutput) end
5351-
lastOutput = writeLua(buffer, binary.operator, operatorOutput)
5358+
lastOutput = writeLua(buffer, op, operatorOutput)
53525359

53535360
if pretty then lastOutput = writeLua(buffer, " ", "") end
53545361

test.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ do
1818
1919
\xff\u{b4}"
2020

21+
local x = 1 + 2 + 3
22+
local y = (1 - 2) - 3
23+
local z = 1 - (2 - 3)
24+
2125
do foo() ; o:m() end
2226

2327
while false or true do

0 commit comments

Comments
 (0)