Skip to content

Commit f6504eb

Browse files
committed
Added support for LuaJIT binary numerals.
Added getOutputSoFarOnLine.
1 parent 27ea859 commit f6504eb

2 files changed

Lines changed: 75 additions & 11 deletions

File tree

preprocess.lua

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--[[============================================================
22
--=
3-
--= LuaPreprocess v1.18 - preprocessing library
3+
--= LuaPreprocess v1.18-dev - preprocessing library
44
--= by Marcus 'ReFreezed' Thunström
55
--=
66
--= License: MIT (see the bottom of this file)
@@ -26,7 +26,7 @@
2626
- toLua, serialize, evaluate
2727
Only during processing:
2828
- getCurrentPathIn, getCurrentPathOut
29-
- getOutputSoFar, getOutputSizeSoFar, getCurrentLineNumberInOutput
29+
- getOutputSoFar, getOutputSoFarOnLine, getOutputSizeSoFar, getCurrentLineNumberInOutput
3030
- loadResource
3131
- outputValue, outputLua, outputLuaTemplate
3232
- startInterceptingOutput, stopInterceptingOutput
@@ -130,7 +130,7 @@
130130

131131

132132

133-
local PP_VERSION = "1.18.0"
133+
local PP_VERSION = "1.18.0-dev"
134134

135135
local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString().
136136

@@ -189,12 +189,7 @@ else
189189
major = tonumber(major)
190190
minor = tonumber(minor)
191191
end
192-
local IS_LUA_51 = (major == 5 and minor == 1)
193-
local IS_LUA_52 = (major == 5 and minor == 2)
194-
local IS_LUA_53 = (major == 5 and minor == 3)
195-
local IS_LUA_51_OR_LATER = (major == 5 and minor >= 1) or (major ~= nil and major > 5)
196192
local IS_LUA_52_OR_LATER = (major == 5 and minor >= 2) or (major ~= nil and major > 5)
197-
local IS_LUA_53_OR_LATER = (major == 5 and minor >= 3) or (major ~= nil and major > 5)
198193

199194
local metaEnv = nil
200195
local dummyEnv = {}
@@ -517,6 +512,49 @@ function _tokenize(s, path, allowPpTokens, allowBacktickStrings, allowJitSyntax)
517512
tok = {type="identifier", representation=word, value=word}
518513
end
519514

515+
-- Number (binary).
516+
elseif s:find("^0b", ptr) then
517+
if not allowJitSyntax then
518+
errorInFile(s, path, ptr, "Tokenizer", "Encountered binary numeral. (Feature not enabled.)")
519+
end
520+
521+
local i1, i2, numStr = s:find("^(..[01]+)", ptr)
522+
523+
-- @Copypaste from below.
524+
if not numStr then
525+
errorInFile(s, path, ptr, "Tokenizer", "Malformed number.")
526+
end
527+
528+
local numStrFallback = numStr
529+
530+
do
531+
if s:find("^[Ii]", i2+1) then -- Imaginary part of complex number.
532+
numStr = s:sub(i1, i2+1)
533+
i2 = i2 + 1
534+
535+
elseif s:find("^[Uu][Ll][Ll]", i2+1) then -- Unsigned 64-bit integer.
536+
numStr = s:sub(i1, i2+3)
537+
i2 = i2 + 3
538+
elseif s:find("^[Ll][Ll]", i2+1) then -- Signed 64-bit integer.
539+
numStr = s:sub(i1, i2+2)
540+
i2 = i2 + 2
541+
end
542+
end
543+
544+
local n = tonumber(numStr) or tonumber(numStrFallback)
545+
546+
if not n then
547+
errorInFile(s, path, ptr, "Tokenizer", "Invalid number.")
548+
end
549+
550+
if s:find("^[%w_]", i2+1) then
551+
-- This is actually not an error in Lua 5.2 and 5.3. Maybe we should issue a warning instead of an error here?
552+
errorInFile(s, path, i2+1, "Tokenizer", "Malformed number.")
553+
end
554+
555+
ptr = i2 + 1
556+
tok = {type="number", representation=numStrFallback, value=n}
557+
520558
-- Number.
521559
elseif s:find("^%.?%d", ptr) then
522560
local pat, maybeInt, lua52Hex, i1, i2, numStr = NUM_HEX_FRAC_EXP, false, true , s:find(NUM_HEX_FRAC_EXP, ptr)
@@ -542,6 +580,7 @@ function _tokenize(s, path, allowPpTokens, allowBacktickStrings, allowJitSyntax)
542580

543581
elseif not maybeInt or numStr:find(".", 1, true) then
544582
-- void
583+
545584
elseif s:find("^[Uu][Ll][Ll]", i2+1) then -- Unsigned 64-bit integer.
546585
numStr = s:sub(i1, i2+3)
547586
i2 = i2 + 3
@@ -578,12 +617,12 @@ function _tokenize(s, path, allowPpTokens, allowBacktickStrings, allowJitSyntax)
578617
end
579618

580619
if s:find("^[%w_]", i2+1) then
581-
-- This is actually only an error in Lua 5.1. Maybe we should issue a warning instead of an error here?
620+
-- This is actually not an error in Lua 5.2 and 5.3. Maybe we should issue a warning instead of an error here?
582621
errorInFile(s, path, i2+1, "Tokenizer", "Malformed number.")
583622
end
584623

585624
ptr = i2+1
586-
tok = {type="number", representation=numStr, value=n}
625+
tok = {type="number", representation=numStrFallback, value=n}
587626

588627
-- Comment.
589628
elseif s:find("^%-%-", ptr) then
@@ -1649,6 +1688,29 @@ function metaFuncs.getOutputSoFar(asTable)
16491688
return asTable and copyArray(current_meta_outputStack[1]) or table.concat(current_meta_outputStack[1])
16501689
end
16511690

1691+
-- getOutputSoFarOnLine()
1692+
-- luaString = getOutputSoFarOnLine( )
1693+
-- Get Lua code that's been outputted so far on the current line.
1694+
-- Raises an error if no file or string is being processed.
1695+
function metaFuncs.getOutputSoFarOnLine()
1696+
errorIfNotRunningMeta(2)
1697+
1698+
local lineFragments = {}
1699+
1700+
-- Should there be a way to get the contents of current_meta_output etc.? :GetMoreOutputFromStack
1701+
for i = #current_meta_outputStack[1], 1, -1 do
1702+
local fragment = current_meta_outputStack[1][i]
1703+
1704+
if fragment:find("\n", 1, true) then
1705+
tableInsert(lineFragments, (fragment:gsub(".*\n", "")))
1706+
break
1707+
end
1708+
tableInsert(lineFragments, fragment)
1709+
end
1710+
1711+
return table.concat(lineFragments)
1712+
end
1713+
16521714
-- getOutputSizeSoFar()
16531715
-- size = getOutputSizeSoFar( )
16541716
-- Get the amount of bytes outputted so far.
@@ -1805,7 +1867,7 @@ local numberFormatters = {
18051867
-- stringToken = newToken( "string", contents [, longForm=false ] )
18061868
-- whitespaceToken = newToken( "whitespace", contents )
18071869
-- ppEntryToken = newToken( "pp_entry", isDouble )
1808-
-- ppKeywordToken = newToken( "pp_keyword", ppKeyword ) -- ppKeyword can be "@".
1870+
-- ppKeywordToken = newToken( "pp_keyword", ppKeyword ) -- ppKeyword can be "file", "insert", "line" or "@".
18091871
-- ppSymbolToken = newToken( "pp_symbol", identifier )
18101872
--
18111873
-- commentToken = { type="comment", representation=string, value=string, long=isLongForm }

tests/runExamples.cmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CD /D "%~dp0.."
44
SETLOCAL EnableDelayedExpansion
55
SET _fails=0
66

7+
ECHO Running examples.
8+
79
FOR /R examples %%G IN (*.lua2p) DO (
810
ECHO. & ECHO Processing example '%%~nxG'...
911
lua ./preprocess-cl.lua "%%G" --debug --silent

0 commit comments

Comments
 (0)