Skip to content

Commit 87704df

Browse files
committed
Added info.linesOfCode to processFile/processString(). --saveinfo now saves all info that processFile() returns for each file. Added concatTokens().
1 parent 489fc28 commit 87704df

File tree

4 files changed

+82
-48
lines changed

4 files changed

+82
-48
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LuaPreprocess
22

3-
![](https://img.shields.io/github/release/ReFreezed/LuaPreprocess.svg)
3+
[![](https://img.shields.io/github/release/ReFreezed/LuaPreprocess.svg)](https://github.com/ReFreezed/LuaPreprocess/releases/latest)
44

55
A small and straightforward Lua preprocessor featuring a simple syntax.
66
Write embedded metaprograms to generate code using normal Lua inside your Lua files.

RunExamples.cmd

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
@ECHO OFF
22

3-
SET example=examples\namedConstants.lua
3+
SET example=namedConstants
44
ECHO.
55
ECHO Running %example%...
6-
lua main.lua %example%2p --debug --silent
7-
lua %example%
6+
lua main.lua examples\%example%.lua2p --debug --silent
7+
lua examples\%example%.lua
88

9-
SET example=examples\optimizeDataAccess.lua
9+
SET example=optimizeDataAccess
1010
ECHO.
1111
ECHO Running %example%...
12-
lua main.lua %example%2p --debug --silent
13-
lua %example%
12+
lua main.lua examples\%example%.lua2p --debug --silent
13+
lua examples\%example%.lua
1414

15-
SET example=examples\parseFile.lua
15+
SET example=parseFile
1616
ECHO.
1717
ECHO Running %example%...
18-
lua main.lua %example%2p --debug --silent
19-
lua %example%
18+
lua main.lua examples\%example%.lua2p --debug --silent
19+
lua examples\%example%.lua
2020

21-
SET example=examples\selectiveFunctionality.lua
21+
SET example=selectiveFunctionality
2222
ECHO.
2323
ECHO Running %example%...
24-
lua main.lua %example%2p --debug --silent
25-
lua %example%
24+
lua main.lua examples\%example%.lua2p --debug --silent
25+
lua examples\%example%.lua
26+
27+
ECHO.
28+
ECHO All examples finished!

main.lua

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ local isDebug = false
100100
local outputExtension = "lua"
101101
local processingInfoPath = ""
102102
local silent = false
103+
local outputMeta = false
103104

104105
--==============================================================
105106
--= Local Functions ============================================
@@ -116,6 +117,8 @@ function formatBytes(n)
116117
return F("%.2f MB", n/(1024*1024))
117118
elseif n >= 1024 then
118119
return F("%.2f kB", n/(1024))
120+
elseif n == 1 then
121+
return F("1 byte", n)
119122
else
120123
return F("%d bytes", n)
121124
end
@@ -150,7 +153,6 @@ math.random() -- Must kickstart...
150153

151154
local processOptions = true
152155
local messageHandlerPath = ""
153-
local outputMeta = false
154156
local paths = {}
155157

156158
for _, arg in ipairs(args) do
@@ -240,9 +242,10 @@ local processingInfo = {
240242
files = {},
241243
}
242244

243-
local byteCount = 0
244-
local lineCount = 0
245-
local tokenCount = 0
245+
local byteCount = 0
246+
local lineCount = 0
247+
local lineCountCode = 0
248+
local tokenCount = 0
246249

247250
for _, path in ipairs(paths) do
248251
local startClockForPath = os.clock()
@@ -286,17 +289,16 @@ for _, path in ipairs(paths) do
286289

287290
if messageHandler then messageHandler("filedone", path, pathOut) end
288291

289-
byteCount = byteCount+info.processedByteCount
290-
lineCount = lineCount+info.lineCount
291-
tokenCount = tokenCount+info.tokenCount
292+
byteCount = byteCount+info.processedByteCount
293+
lineCount = lineCount+info.lineCount
294+
lineCountCode = lineCountCode+info.linesOfCode
295+
tokenCount = tokenCount+info.tokenCount
292296

293297
if processingInfoPath ~= "" then
294298

295299
-- :SavedInfo
296-
table.insert(processingInfo.files, {
297-
path = path,
298-
hasPreprocessorCode = info.hasPreprocessorCode,
299-
})
300+
info.path = path -- See 'ProcessInfo' in preprocess.lua to what more 'info' contains.
301+
table.insert(processingInfo.files, info)
300302

301303
end
302304

@@ -317,8 +319,13 @@ if processingInfoPath ~= "" then
317319
end
318320

319321
printfNoise(
320-
"All done! (%.3fs, %.0f lines, %.0f tokens, %s)",
321-
os.clock()-startClock, lineCount, tokenCount, formatBytes(byteCount)
322+
"All done! (%.3fs, %.0f file%s, %.0f LOC, %.0f line%s, %.0f token%s, %s)",
323+
os.clock()-startClock,
324+
#paths, #paths == 1 and "" or "s",
325+
lineCountCode,
326+
lineCount, lineCount == 1 and "" or "s",
327+
tokenCount, tokenCount == 1 and "" or "s",
328+
formatBytes(byteCount)
322329
)
323330

324331
--[[!===========================================================

preprocess.lua

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
- getFileContents, fileExists
7272
- printf
7373
- run
74-
- tokenize, newToken
74+
- tokenize, newToken, concatTokens
7575
- toLua, serialize
7676
Only in metaprogram:
7777
- outputValue, outputLua
@@ -88,7 +88,7 @@
8888
8989
--============================================================]]
9090

91-
local VERSION = "1.2.0"
91+
local VERSION = "1.3.0"
9292

9393
local KEYWORDS = {
9494
"and","break","do","else","elseif","end","false","for","function","if","in",
@@ -429,6 +429,7 @@ function tokenize(s, path, allowMetaTokens)
429429
tok.position = tokenPos
430430

431431
ln = ln+countString(tok.representation, "\n", true)
432+
tok.lineEnd = ln
432433

433434
table.insert(tokens, tok)
434435
-- print(#tokens, tok.type, tok.representation)
@@ -593,7 +594,7 @@ function serialize(buffer, v)
593594
return true
594595
end
595596

596-
-- luaString, errorMessage = toLua( value )
597+
-- luaString, error = toLua( value )
597598
function toLua(v)
598599
local buffer = {}
599600

@@ -701,12 +702,12 @@ metaFuncs.fileExists = fileExists
701702
-- toLua()
702703
-- Convert a value to a Lua literal. Does not work with certain types, like functions or userdata.
703704
-- Returns nil and a message if an error ocurred.
704-
-- luaString, errorMessage = toLua( value )
705+
-- luaString, error = toLua( value )
705706
metaFuncs.toLua = toLua
706707

707708
-- serialize()
708709
-- Same as toLua() except adds the result to an array instead of returning the Lua code as a string.
709-
-- success, errorMessage = serialize( buffer, value )
710+
-- success, error = serialize( buffer, value )
710711
metaFuncs.serialize = serialize
711712

712713
-- escapePattern()
@@ -777,8 +778,8 @@ end
777778

778779
-- tokenize()
779780
-- Convert Lua code to tokens. Returns nil and a message on error. (See newToken() for token types.)
780-
-- tokens, errorMessage = tokenize( luaString [, allowPreprocessorTokens=false ] )
781-
-- token = { type=tokenType, representation=representation, value=value, line=lineNumber, position=bytePosition, ... }
781+
-- tokens, error = tokenize( luaString [, allowPreprocessorTokens=false ] )
782+
-- token = { type=tokenType, representation=representation, value=value, line=lineNumber, lineEnd=lineNumber, position=bytePosition, ... }
782783
function metaFuncs.tokenize(lua, allowMetaTokens)
783784
local tokens, err = tokenize(lua, "<string>", allowMetaTokens)
784785
return tokens, err
@@ -927,6 +928,13 @@ function metaFuncs.newToken(tokType, ...)
927928
end
928929
end
929930

931+
-- concatTokens()
932+
-- Concatinate tokens by their representations.
933+
-- luaString = concatTokens( tokens )
934+
function metaFuncs.concatTokens(tokens)
935+
return concatTokens(tokens)
936+
end
937+
930938

931939

932940
for k, v in pairs(metaFuncs) do metaEnv[k] = v end
@@ -936,6 +944,20 @@ metaEnv.__LUA = metaEnv.outputLua
936944

937945

938946

947+
local function getLineCountWithCode(tokens)
948+
local lineCount = 0
949+
local lastLine = 0
950+
951+
for _, tok in ipairs(tokens) do
952+
if not (tok.type == "comment" or tok.type == "whitespace") and tok.lineEnd > lastLine then
953+
lineCount = lineCount+(tok.lineEnd-tok.line+1)
954+
lastLine = tok.lineEnd
955+
end
956+
end
957+
958+
return lineCount
959+
end
960+
939961
local function _processFileOrString(params, isFile)
940962
if isFile then
941963
if not params.pathIn then error("Missing 'pathIn' in params.", 2) end
@@ -972,7 +994,8 @@ local function _processFileOrString(params, isFile)
972994

973995
-- Info variables.
974996
local processedByteCount = #luaUnprocessed
975-
local lineCount = lastToken and lastToken.line+countString(lastToken.representation, "\n") or 0
997+
local lineCount = (specialFirstLine and 1 or 0) + (lastToken and lastToken.line + countString(lastToken.representation, "\n") or 0)
998+
local lineCountCode = getLineCountWithCode(tokens)
976999
local tokenCount = #tokens
9771000
local hasPreprocessorCode = false
9781001

@@ -1289,6 +1312,7 @@ local function _processFileOrString(params, isFile)
12891312
local info = {
12901313
processedByteCount = processedByteCount,
12911314
lineCount = lineCount,
1315+
linesOfCode = lineCountCode,
12921316
tokenCount = tokenCount,
12931317
hasPreprocessorCode = hasPreprocessorCode,
12941318
}
@@ -1358,16 +1382,16 @@ local lib = {
13581382
-- error: Error message, or nil if no error happened.
13591383
--
13601384
-- params: Table with these fields:
1361-
-- pathIn = pathToInputFile -- [Required]
1362-
-- pathMeta = pathForMetaprogram -- [Optional] You can inspect this temporary output file if an error ocurrs in the metaprogram.
1363-
-- pathOut = pathToOutputFile -- [Required]
1385+
-- pathIn = pathToInputFile -- [Required]
1386+
-- pathMeta = pathForMetaprogram -- [Optional] You can inspect this temporary output file if an error ocurrs in the metaprogram.
1387+
-- pathOut = pathToOutputFile -- [Required]
13641388
--
1365-
-- addLineNumbers = boolean -- [Optional] Add comments with line numbers to the output.
1366-
-- debug = boolean -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
1389+
-- addLineNumbers = boolean -- [Optional] Add comments with line numbers to the output.
1390+
-- debug = boolean -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
13671391
--
1368-
-- onAfterMeta = function( luaCode ) -- [Optional] Here you can modify and return the Lua code before it's written to 'pathOut'.
1369-
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
1370-
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as what is returned from processFile().
1392+
-- onAfterMeta = function( luaString ) -- [Optional] Here you can modify and return the Lua code before it's written to 'pathOut'.
1393+
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
1394+
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as what is returned from processFile().
13711395
--
13721396
processFile = processFile,
13731397

@@ -1378,14 +1402,14 @@ local lib = {
13781402
-- info: Table with various information, or a message if an error happened. See 'ProcessInfo' for more info.
13791403
--
13801404
-- params: Table with these fields:
1381-
-- code = luaCode -- [Required]
1382-
-- pathMeta = pathForMetaprogram -- [Optional] You can inspect this temporary output file if an error ocurrs in the metaprogram.
1405+
-- code = luaString -- [Required]
1406+
-- pathMeta = pathForMetaprogram -- [Optional] You can inspect this temporary output file if an error ocurrs in the metaprogram.
13831407
--
1384-
-- addLineNumbers = boolean -- [Optional] Add comments with line numbers to the output.
1385-
-- debug = boolean -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
1408+
-- addLineNumbers = boolean -- [Optional] Add comments with line numbers to the output.
1409+
-- debug = boolean -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
13861410
--
1387-
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
1388-
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as the second returned value from processString().
1411+
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
1412+
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as the second returned value from processString().
13891413
--
13901414
processString = processString,
13911415

0 commit comments

Comments
 (0)