@@ -216,7 +216,7 @@ local isToken, isTokenAndNotNil
216216local loadLuaString , loadLuaFile
217217local outputLineNumber , maybeOutputLineNumber
218218local pack , unpack
219- local printf , printTokens , printTraceback
219+ local printf , printTokens , printError , printfError , printErrorTraceback
220220local pushErrorHandler , pushErrorHandlerIfOverridingDefault , popErrorHandler
221221local serialize , toLua
222222
@@ -256,18 +256,25 @@ function printTokens(tokens, filter)
256256 end
257257end
258258
259- function printTraceback (message , level )
260- print (message )
261- print (" stack traceback:" )
259+ function printError (s )
260+ io.stderr :write (s , " \n " )
261+ end
262+ function printfError (s , ...)
263+ io.stderr :write (s :format (... ), " \n " )
264+ end
265+
266+ function printErrorTraceback (message , level )
267+ printError (message )
268+ printError (" stack traceback:" )
262269
263270 for level = 1 + (level or 1 ), 1 / 0 do
264271 local info = debug.getinfo (level , " nSl" )
265272 if not info then break end
266273
267- -- print (level, "source ", info.source)
268- -- print (level, "short_src", info.short_src)
269- -- print (level, "name ", info.name)
270- -- print (level, "what ", info.what)
274+ -- printError (level, "source ", info.source)
275+ -- printError (level, "short_src", info.short_src)
276+ -- printError (level, "name ", info.name)
277+ -- printError (level, "what ", info.what)
271278
272279 local where = info .source :match " ^@(.+)" or info .short_src
273280 local lnStr = info .currentline > 0 and " :" .. info .currentline or " "
@@ -279,7 +286,7 @@ function printTraceback(message, level)
279286 or info .what == " tail" and " tail call"
280287 or " ?"
281288
282- print (" \t " .. where .. lnStr .. " (" .. name .. " )" )
289+ printError (" \t " .. where .. lnStr .. " (" .. name .. " )" )
283290 end
284291end
285292
@@ -288,21 +295,21 @@ end
288295function error (err , level )
289296 -- @Check: Should we prepend the path? And, in all or just some cases?
290297 level = 1 + (level or 1 )
291- printTraceback (tryToFormatError (err ), level )
298+ printErrorTraceback (tryToFormatError (err ), level )
292299 currentErrorHandler (err , level )
293300end
294301
295302function errorLine (err )
296- print (tryToFormatError (err ))
303+ printError (tryToFormatError (err ))
297304 currentErrorHandler (err , 2 )
298305end
299306
300307function errorOnLine (path , ln , agent , s , ...)
301308 s = s :format (... )
302309 if agent then
303- printf (" Error @ %s:%d: [%s] %s\n " , path , ln , agent , s )
310+ printfError (" Error @ %s:%d: [%s] %s\n " , path , ln , agent , s )
304311 else
305- printf (" Error @ %s:%d: %s\n " , path , ln , s )
312+ printfError (" Error @ %s:%d: %s\n " , path , ln , s )
306313 end
307314 currentErrorHandler (s , 2 )
308315 return s
336343 local linePre1End = findEndOfLine (contents , linePre1Start - 1 )
337344 local linePre2Start = findStartOfLine (contents , linePre1Start - 1 , false )
338345 local linePre2End = findEndOfLine (contents , linePre2Start - 1 )
339- -- printf ("pos %d | lines %d..%d, %d..%d, %d..%d", pos, linePre2Start,linePre2End+1, linePre1Start,linePre1End+1, lineStart,lineEnd+1) -- DEBUG
346+ -- printfError ("pos %d | lines %d..%d, %d..%d, %d..%d", pos, linePre2Start,linePre2End+1, linePre1Start,linePre1End+1, lineStart,lineEnd+1) -- DEBUG
340347
341348 local contextStr = F (" >\n %s%s%s>-%s^" ,
342349 (linePre2Start < linePre1Start and linePre2Start <= linePre2End ) and F (" > %s\n " , (contents :sub (linePre2Start , linePre2End ):gsub (" \t " , " " ))) or " " ,
346353 nil
347354 )
348355
349- if agent then printf (" Error @ %s:%d: [%s] %s\n %s" , path , ln , agent , s , contextStr )
350- else printf (" Error @ %s:%d: %s" , path , ln , s , contextStr )
356+ if agent then printfError (" Error @ %s:%d: [%s] %s\n %s" , path , ln , agent , s , contextStr )
357+ else printfError (" Error @ %s:%d: %s" , path , ln , s , contextStr )
351358 end
352359
353360 currentErrorHandler (s , 2 )
904911
905912
906913
907- -- luaString, error = toLua( value )
914+ -- luaString = toLua( value )
915+ -- Returns nil and a message on error.
908916function toLua (v )
909917 local buffer = {}
910918
@@ -1140,7 +1148,7 @@ metaFuncs.printf = printf
11401148
11411149-- getFileContents()
11421150-- Get the entire contents of a binary file or text file. Returns nil and a message on error.
1143- -- contents, error = getFileContents( path [, isTextFile=false ] )
1151+ -- contents = getFileContents( path [, isTextFile=false ] )
11441152metaFuncs .getFileContents = getFileContents
11451153
11461154-- fileExists()
@@ -1150,8 +1158,8 @@ metaFuncs.fileExists = fileExists
11501158
11511159-- toLua()
11521160-- Convert a value to a Lua literal. Does not work with certain types, like functions or userdata.
1153- -- Returns nil and a message if an error ocurred .
1154- -- luaString, error = toLua( value )
1161+ -- Returns nil and a message on error.
1162+ -- luaString = toLua( value )
11551163metaFuncs .toLua = toLua
11561164
11571165-- serialize()
@@ -1295,7 +1303,7 @@ end
12951303
12961304-- tokenize()
12971305-- Convert Lua code to tokens. Returns nil and a message on error. (See newToken() for token types.)
1298- -- tokens, error = tokenize( luaString [, allowPreprocessorCode=false ] )
1306+ -- tokens = tokenize( luaString [, allowPreprocessorCode=false ] )
12991307-- token = {
13001308-- type=tokenType, representation=representation, value=value,
13011309-- line=lineNumber, lineEnd=lineNumber, position=bytePosition, file=filePath,
@@ -1557,6 +1565,9 @@ function metaFuncs.concatTokens(tokens)
15571565 return _concatTokens (tokens )
15581566end
15591567
1568+ -- Extra stuff used by the command line program:
1569+ metaFuncs .tryToFormatError = tryToFormatError
1570+
15601571
15611572
15621573for k , v in pairs (metaFuncs ) do metaEnv [k ] = v end
@@ -1872,7 +1883,7 @@ local function doExpansions(tokensRaw, fileBuffers, params, stats)
18721883 local startTok = depthStack [# depthStack ].startToken
18731884 errorAtToken (
18741885 fileBuffers , tok , nil , " Macro" ,
1875- " Expected '%s' (to end '%s' %s) but got '%s'." ,
1886+ " Expected '%s' (to close '%s' %s) but got '%s'." ,
18761887 depthStack [# depthStack ][2 ], startTok .value , getRelativeLocationText (startTok , tok ), tok .value
18771888 )
18781889 end
@@ -2473,7 +2484,7 @@ local function processFileOrString(params, isFile)
24732484 errorToReturn = err
24742485
24752486 if not levelFromOurError then
2476- printTraceback (tryToFormatError (err ), 2 )
2487+ printErrorTraceback (tryToFormatError (err ), 2 )
24772488 end
24782489
24792490 if params .onError then params .onError (errorToReturn ) end
0 commit comments