Skip to content

Commit 41e5fe3

Browse files
committed
Handling nested [[...]]
1 parent ffe7517 commit 41e5fe3

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

preprocess.lua

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ local errorIfNotRunningMeta
184184
local escapePattern
185185
local F
186186
local getFileContents, fileExists
187+
local getLineNumber
187188
local getNextUsableToken
188189
local insertTokenRepresentations
189190
local isAny
@@ -365,6 +366,22 @@ function tokenize(s, path, allowBacktickStrings, allowMetaTokens)
365366
end
366367
end
367368

369+
if tok.long then
370+
-- Check for nesting of [[...]], which is depricated in Lua.
371+
local chunk, err = loadLuaString("--"..tok.representation)
372+
if not chunk then
373+
local lnInString, _err = err:match"^%[string \".-\"%]:(%d+): (.*)"
374+
if not _err then
375+
return nil, errorInFile(s, path, reprStart, "Tokenizer", "Malformed long comment.")
376+
end
377+
378+
return nil, errorOnLine(
379+
path, getLineNumber(s, reprStart)+tonumber(lnInString)-1,
380+
"Tokenizer", "Malformed long comment: %s", _err
381+
)
382+
end
383+
end
384+
368385
tok.type = "comment"
369386
tok.representation = s:sub(reprStart, ptr-1)
370387

@@ -430,9 +447,18 @@ function tokenize(s, path, allowBacktickStrings, allowMetaTokens)
430447
end
431448
end
432449

433-
local valueChunk = loadLuaString("return"..tok.representation)
450+
-- Check for nesting of [[...]], which is depricated in Lua.
451+
local valueChunk, err = loadLuaString("return"..tok.representation)
434452
if not valueChunk then
435-
return nil, errorInFile(s, path, reprStart, "Tokenizer", "Malformed long string.")
453+
local lnInString, _err = err:match"^%[string \".-\"%]:(%d+): (.*)"
454+
if not _err then
455+
return nil, errorInFile(s, path, reprStart, "Tokenizer", "Malformed long string.")
456+
end
457+
458+
return nil, errorOnLine(
459+
path, getLineNumber(s, reprStart)+tonumber(lnInString)-1,
460+
"Tokenizer", "Malformed long string: %s", _err
461+
)
436462
end
437463

438464
local v = valueChunk()
@@ -811,6 +837,11 @@ function isToken(tok, tokType, v)
811837
return tok.type == tokType and (v == nil or tok.value == v)
812838
end
813839

840+
function getLineNumber(s, ptr)
841+
local _, nlCount = s:sub(1, ptr):gsub("\n", "\n")
842+
return 1+nlCount
843+
end
844+
814845
--==============================================================
815846
--= Preprocessor Functions =====================================
816847
--==============================================================

0 commit comments

Comments
 (0)