55--=
66--= License: MIT (see the bottom of this file)
77--= Website: https://github.com/ReFreezed/LuaPreprocess
8+ --= Documentation: https://github.com/ReFreezed/LuaPreprocess/wiki
89--=
910--= Tested with Lua 5.1, 5.2, 5.3 and 5.4.
1011--=
3738
3839 How to metaprogram:
3940
40- The exclamation mark (!) is used to indicate what code is part
41- of the metaprogram. There are 4 ways to write metaprogram code:
41+ The exclamation mark (!) is used to indicate what code is part of
42+ the metaprogram. There are 4 main ways to write metaprogram code:
4243
4344 !... The line will simply run during preprocessing. The line can span multiple actual lines if it contains brackets.
4445 !!... The line will appear in both the metaprogram and the final program. The line must be an assignment.
7980
8081 -- Extended preprocessor line. (Lines are consumed until brackets
8182 -- are balanced when the end of the line has been reached.)
82- !newClass{
83+ !newClass{ -- Starts here.
8384 name = "Entity",
8485 props = {x=0, y=0},
85- }
86+ } -- Ends here.
8687
8788 -- Preprocessor block.
8889 !(
100101
101102 -- Dual code (both preprocessor line and final output).
102103 !!local partial = "Hello"
103- local whole = partial.. !(partial..", world!")
104+ local whole = partial .. !(partial..", world!")
104105 print(whole) -- HelloHello, world!
105106
106107 -- Beware in preprocessor blocks that only call a single function!
107- !(func()) -- This will bee seen as an inline block and output whatever value func() returns as a literal.
108- !(func();) -- If that's not wanted then a trailing ";" will prevent that. This line won't output anything by itself.
109- -- When the full metaprogram is generated, " !(func())" translates into " outputValue(func())"
110- -- while " !(func();)" simply translates into " func();" (because " outputValue(func();)" would be invalid Lua code).
111- -- Though in this specific case a preprocessor line would be nicer:
108+ !( func() ) -- This will bee seen as an inline block and output whatever value func() returns as a literal.
109+ !( func(); ) -- If that's not wanted then a trailing `;` will prevent that. This line won't output anything by itself.
110+ -- When the full metaprogram is generated, ` !(func())` translates into ` outputValue(func())`
111+ -- while ` !(func();)` simply translates into ` func();` (because ` outputValue(func();)` would be invalid Lua code).
112+ -- Though in this specific case a preprocessor line (without the parenthesis) would be nicer:
112113 !func()
113114
114- --============================================================ ]]
115+ -- For the full documentation, see: https://github.com/ReFreezed/LuaPreprocess/wiki
115116
117+ --============================================================]]
116118
117119
118- --[[ Make sure the library doesn't add globals.
119- setmetatable(_G, {__newindex=function(_G, k, v)
120- print(debug.traceback("WARNING: Setting global '"..tostring(k).."'.", 2))
121- rawset(_G, k, v)
122- end})
123- --]]
124120
125121local PP_VERSION = " 1.12.0"
126122
@@ -206,7 +202,7 @@ local _tokenize
206202local assertarg
207203local copyTable
208204local countString , countSubString
209- local error , errorline , errorOnLine , errorInFile , errorAtToken
205+ local error , errorLine , errorOnLine , errorInFile , errorAtToken
210206local errorIfNotRunningMeta
211207local escapePattern
212208local F , tryToFormatError
@@ -232,9 +228,9 @@ function tryToFormatError(err0)
232228 local err , path , ln = nil
233229
234230 if type (err0 ) == " string" then
235- do path , ln , err = err0 :match ' ^(%a:[%w_/\\ .]+):(%d+): (.*)'
236- if not err then path , ln , err = err0 :match ' ^([%w_/\\ .]+):(%d+): (.*)'
237- if not err then path , ln , err = err0 :match ' ^(.-):(%d+): (.*)'
231+ do path , ln , err = err0 :match " ^(%a:[%w_/\\ .]+):(%d+): (.*)"
232+ if not err then path , ln , err = err0 :match " ^([%w_/\\ .]+):(%d+): (.*)"
233+ if not err then path , ln , err = err0 :match " ^(.-):(%d+): (.*)"
238234 end end end
239235 end
240236
@@ -296,7 +292,7 @@ function error(err, level)
296292 currentErrorHandler (err , level )
297293end
298294
299- function errorline (err )
295+ function errorLine (err )
300296 print (tryToFormatError (err ))
301297 currentErrorHandler (err , 2 )
302298end
@@ -674,9 +670,14 @@ function _tokenize(s, path, allowMetaTokens, allowBacktickStrings, allowJitSynta
674670
675671 -- Preprocessor: Keyword.
676672 elseif allowMetaTokens and s :find (" ^@" , ptr ) then
677- local i1 , i2 , repr , word = s :find (" ^(@([%a_][%w_]*))" , ptr )
678- ptr = i2 + 1
679- tok = {type = " pp_keyword" , representation = repr , value = word }
673+ if s :find (" ^@@" , ptr ) then
674+ ptr = ptr + 2
675+ tok = {type = " pp_keyword" , representation = " @@" , value = " insert" }
676+ else
677+ local i1 , i2 , repr , word = s :find (" ^(@([%a_][%w_]*))" , ptr )
678+ ptr = i2 + 1
679+ tok = {type = " pp_keyword" , representation = repr , value = word }
680+ end
680681
681682 else
682683 return nil , errorInFile (s , path , ptr , " Tokenizer" , " Unknown character." )
@@ -1179,7 +1180,7 @@ function metaFuncs.run(path, ...)
11791180 assertarg (1 , path , " string" )
11801181
11811182 local mainChunk , err = loadLuaFile (path , metaEnv )
1182- if not mainChunk then errorline (err ) end
1183+ if not mainChunk then errorLine (err ) end
11831184
11841185 -- We want multiple return values while avoiding a tail call to preserve stack info.
11851186 local returnValues = pack (mainChunk (... ))
@@ -1627,7 +1628,7 @@ local function processKeywords(tokensRaw, fileBuffers, params, stats)
16271628 if not (isTokenAndNotNil (tokNext , " string" ) or isTokenAndNotNil (tokNext , " identifier" )) then
16281629 errorAtToken (
16291630 fileBuffers , ppKeywordTok , (tokNext and tokNext .position or ppKeywordTok .position +# ppKeywordTok .representation ),
1630- " Parser" , " Expected a string or identifier after @insert. "
1631+ " Parser" , " Expected a string or identifier after %s. " , ppKeywordTok . representation
16311632 )
16321633 end
16331634
@@ -1950,7 +1951,7 @@ local function _processFileOrString(params, isFile)
19501951 luaUnprocessed , err = getFileContents (pathIn )
19511952
19521953 if not luaUnprocessed then
1953- errorline (" Could not read file: " .. err )
1954+ errorLine (" Could not read file: " .. err )
19541955 end
19551956
19561957 currentPathIn = params .pathIn
@@ -2376,7 +2377,7 @@ local function _processFileOrString(params, isFile)
23762377 if type (luaModified ) == " string" then
23772378 lua = luaModified
23782379 elseif luaModified ~= nil then
2379- errorline (" onAfterMeta() did not return a string. (Got " .. type (luaModified ).. " )" )
2380+ errorLine (" onAfterMeta() did not return a string. (Got " .. type (luaModified ).. " )" )
23802381 end
23812382 end
23822383
0 commit comments