11--[[ ============================================================
22--=
3- --= LuaPreprocess library
3+ --= LuaPreprocess v1.17-dev - preprocessing library
44--= by Marcus 'ReFreezed' Thunström
55--=
66--= License: MIT (see the bottom of this file)
2121 - printf
2222 - run
2323 - tokenize, newToken, concatTokens, removeUselessTokens, eachToken, isToken, getNextUsefulToken
24- - toLua, serialize
24+ - toLua, serialize, evaluate
2525 Only during processing:
2626 - getCurrentPathIn, getCurrentPathOut
2727 - getOutputSoFar, getOutputSizeSoFar, getCurrentLineNumberInOutput
127127
128128
129129
130- local PP_VERSION = " 1.17.0"
130+ local PP_VERSION = " 1.17.0-dev "
131131
132132local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString().
133133
@@ -238,7 +238,7 @@ local loadLuaString, loadLuaFile
238238local outputLineNumber , maybeOutputLineNumber
239239local pack , unpack
240240local printf , printTokens , printError , printfError , printErrorTraceback
241- local serialize , toLua
241+ local serialize , toLua , evaluate
242242local tableInsert , tableRemove , tableInsertFormat
243243local utf8GetCharLength , utf8GetCodepointAndLength
244244
@@ -1059,8 +1059,6 @@ function serialize(buffer, v)
10591059 return true
10601060end
10611061
1062-
1063-
10641062-- luaString = toLua( value )
10651063-- Returns nil and a message on error.
10661064function toLua (v )
@@ -1072,6 +1070,21 @@ function toLua(v)
10721070 return table.concat (buffer )
10731071end
10741072
1073+ -- value = evaluate( expression )
1074+ -- Returns nil and a message on error.
1075+ function evaluate (expression )
1076+ local chunk , err = loadLuaString (" return " .. expression , " @" , getfenv (2 ))
1077+ if not chunk then
1078+ return nil , F (" Invalid expression '%s'. (%s)" , expression , (err :gsub (" ^:%d+: " , " " )))
1079+ end
1080+
1081+ if expression :find (" ," , 1 , true ) and not loadLuaString (" return(" .. expression .. " \n )" , " @" ) then
1082+ return nil , F (" Ambiguous expression '%s'. (Comma-separated list?)" , expression )
1083+ end
1084+
1085+ return (chunk ())
1086+ end
1087+
10751088
10761089
10771090function escapePattern (s )
@@ -1374,6 +1387,12 @@ metaFuncs.toLua = toLua
13741387-- This could avoid allocating unnecessary strings.
13751388metaFuncs .serialize = serialize
13761389
1390+ -- evaluate()
1391+ -- value = evaluate( expression )
1392+ -- Evaluate an expression. Returns nil and a message on error.
1393+ -- Note that nil or false can also be returned if that's the value the expression results in!
1394+ metaFuncs .evaluate = evaluate
1395+
13771396-- escapePattern()
13781397-- escapedString = escapePattern( string )
13791398-- Escape a string so it can be used in a pattern as plain text.
@@ -2458,6 +2477,7 @@ local function expandMacro(tokens, fileBuffers, tokenStack, macroStartTok, isNes
24582477 end
24592478 local argStr = insertTokensAsStringLiteral (tokens , argNonPpTokens , argNonPpStartTok )
24602479
2480+ --[[ :NoInconsistentMacroArgumentValidation
24612481 if isFirstPart then
24622482 local chunk, err = loadLuaString("return ("..argStr..")", "@")
24632483
@@ -2467,6 +2487,7 @@ local function expandMacro(tokens, fileBuffers, tokenStack, macroStartTok, isNes
24672487 -- errorAtToken(fileBuffers, argNonPpStartTok, nil, "Macro", "Syntax error: Invalid table constructor expression. (%s)", err)
24682488 end
24692489 end
2490+ ]]
24702491 end
24712492
24722493 -- Add ')' for end of call.
@@ -2585,6 +2606,7 @@ local function expandMacro(tokens, fileBuffers, tokenStack, macroStartTok, isNes
25852606 end
25862607 local argStr = insertTokensAsStringLiteral (tokens , argNonPpTokens , argNonPpStartTok )
25872608
2609+ --[[ :NoInconsistentMacroArgumentValidation
25882610 if isFirstPart then
25892611 local chunk, err = loadLuaString("return ("..argStr..")", "@")
25902612
@@ -2594,6 +2616,7 @@ local function expandMacro(tokens, fileBuffers, tokenStack, macroStartTok, isNes
25942616 -- errorAtToken(fileBuffers, argNonPpStartTok, nil, "Macro", "Syntax error: Invalid expression for argument #%d. (%s)", argNum, err)
25952617 end
25962618 end
2619+ ]]
25972620
25982621 elseif isFirstPart then
25992622 -- There were no useful tokens for the argument!
@@ -3398,7 +3421,7 @@ return pp
33983421
33993422--[[ !===========================================================
34003423
3401- Copyright © 2018-2021 Marcus 'ReFreezed' Thunström
3424+ Copyright © 2018-2022 Marcus 'ReFreezed' Thunström
34023425
34033426Permission is hereby granted, free of charge, to any person obtaining a copy
34043427of this software and associated documentation files (the "Software"), to deal
0 commit comments