66--= Tokenize Lua code or create ASTs (Abstract Syntax Trees)
77--= and convert the data back to Lua.
88--=
9- --= Version: 2.2-dev
9+ --= Version: 2.3
1010--=
1111--= License: MIT (see the bottom of this file)
1212--= Website: http://refreezed.com/luaparser/
@@ -454,9 +454,9 @@ Special number notation rules.
454454
455455-============================================================]=]
456456
457- local PARSER_VERSION = " 2.2.0-dev "
457+ local PARSER_VERSION = " 2.3.0 "
458458
459- local NORMALIZE_MINUS_ZERO , HANDLE_ENV
459+ local NORMALIZE_MINUS_ZERO , HANDLE_ENV -- Should HANDLE_ENV be a setting?
460460do
461461 local n = 0
462462 NORMALIZE_MINUS_ZERO = tostring (- n ) == " 0" -- Lua 5.3+ normalizes -0 to 0.
@@ -478,7 +478,6 @@ local tonumber = tonumber
478478local tostring = tostring
479479local type = type
480480
481- local io = io
482481local ioOpen = io.open
483482local ioWrite = io.write
484483
@@ -574,7 +573,6 @@ local TOKEN_BYTES = {
574573 NAME_START = newCharSet " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" ,
575574 DASH = newCharSet " -" ,
576575 NUM = newCharSet " 0123456789" ,
577- -- NUM_OR_DOT = newCharSet"0123456789.",
578576 QUOTE = newCharSet " \" '" ,
579577 SQUARE = newCharSet " [" ,
580578 DOT = newCharSet " ." ,
602600 MIN_INT = math.mininteger or - MAX_INT - 1
603601end
604602
605- -- local EMPTY_TABLE = {}
606-
607603local nextSerialNumber = 1
608604
609605
858854 [" \r " ] = " {CR}" ,
859855 }
860856
861- function ensurePrintable (s )
857+ --[[ local ]] function ensurePrintable (s )
862858 return (stringGsub (s , " [%z\1 -\31\127 -\255 ]" , function (c )
863859 return CONTROL_TO_READABLE [c ] or (stringByte (c ) <= 31 or stringByte (c ) >= 127 ) and F (" {%d}" , stringByte (c )) or nil
864860 end ))
932928 return len
933929 end
934930
935- function formatMessageInFile (prefix , contents , path , pos , agent , s , ...)
931+ --[[ local ]] function formatMessageInFile (prefix , contents , path , pos , agent , s , ...)
936932 if agent ~= " " then
937933 agent = " [" .. agent .. " ] "
938934 end
12941290 end
12951291
12961292 -- tokens, error = tokenize( luaString [, keepWhitespaceTokens=false ] [, pathForErrorMessages="?" ] )
1297- function tokenize (s , keepWhitespaceTokens , path )
1293+ --[[ local ]] function tokenize (s , keepWhitespaceTokens , path )
12981294 assertArg1 (" tokenize" , 1 , s , " string" )
12991295
13001296 if type (keepWhitespaceTokens ) == " string" then
13181314 local BYTES_NAME_START = TOKEN_BYTES .NAME_START
13191315 local BYTES_DASH = TOKEN_BYTES .DASH
13201316 local BYTES_NUM = TOKEN_BYTES .NUM
1321- -- local BYTES_NUM_OR_DOT = TOKEN_BYTES.NUM_OR_DOT
13221317 local BYTES_QUOTE = TOKEN_BYTES .QUOTE
13231318 local BYTES_SQUARE = TOKEN_BYTES .SQUARE
13241319 local BYTES_DOT = TOKEN_BYTES .DOT
@@ -1740,6 +1735,8 @@ end
17401735-- updateToken( whitespaceToken, contents )
17411736--
17421737local function updateToken (tok , tokValue )
1738+ -- @Copypaste from newToken().
1739+
17431740 if tok .type == " keyword" then
17441741 if type (tokValue ) ~= " string" then errorf (2 , " Expected string value for 'keyword' token. (Got %s)" , type (tokValue )) end
17451742 if not KEYWORDS [tokValue ] then errorf (2 , " Invalid keyword '%s'." , tokValue ) end
@@ -2009,7 +2006,7 @@ local function parseTable(tokens, tokStart) --> tableNode, token, error
20092006 return tableNode , tok
20102007end
20112008
2012- function parseExpressionInternal (tokens , tokStart , lastPrecedence ) -- > expression, token, error
2009+ --[[ local ]] function parseExpressionInternal (tokens , tokStart , lastPrecedence ) -- > expression, token, error
20132010 local tok = tokStart
20142011 local canParseLookupOrCall = false
20152012 local currentToken = tokens [tok ]
@@ -2323,7 +2320,7 @@ function parseExpressionInternal(tokens, tokStart, lastPrecedence) --> expressio
23232320 return expr , tok
23242321end
23252322
2326- function parseExpressionList (tokens , tok , expressions ) -- > success, token, error
2323+ --[[ local ]] function parseExpressionList (tokens , tok , expressions ) -- > success, token, error
23272324 while true do
23282325 local expr , tokNext , err = parseExpressionInternal (tokens , tok , 0 )
23292326 if not expr then return false , tok , err end
@@ -2338,7 +2335,7 @@ function parseExpressionList(tokens, tok, expressions) --> success, token, error
23382335 end
23392336end
23402337
2341- function parseFunctionParametersAndBody (tokens , tokStart , funcTok ) -- > func, token, error
2338+ --[[ local ]] function parseFunctionParametersAndBody (tokens , tokStart , funcTok ) -- > func, token, error
23422339 local tok = tokStart
23432340 local func = AstFunction (tokens [funcTok ])
23442341
@@ -2799,7 +2796,7 @@ end
27992796
28002797local statementErrorReported = false
28012798
2802- function parseBlock (tokens , tok , blockTok , stopAtEndKeyword ) -- > block, token, error
2799+ --[[ local ]] function parseBlock (tokens , tok , blockTok , stopAtEndKeyword ) -- > block, token, error
28032800 local block = AstBlock (tokens [blockTok ])
28042801 local statements = block .statements
28052802
@@ -3303,7 +3300,7 @@ local function cloneNodeAndMaybeChildren(node, cloneChildren)
33033300 return clone
33043301end
33053302
3306- function cloneNodeArrayAndChildren (cloneArray , sourceArray )
3303+ --[[ local ]] function cloneNodeArrayAndChildren (cloneArray , sourceArray )
33073304 for i , node in ipairs (sourceArray ) do
33083305 cloneArray [i ] = cloneNodeAndMaybeChildren (node , true )
33093306 end
@@ -3541,11 +3538,11 @@ do
35413538 end
35423539 end
35433540
3544- function printNode (node )
3541+ --[[ local ]] function printNode (node )
35453542 _printNode (node )
35463543 end
35473544
3548- function printTree (node )
3545+ --[[ local ]] function printTree (node )
35493546 _printTree (node , 0 , nil )
35503547 end
35513548end
51125109
51135110 local cache = {}
51145111
5115- function generateName (nameGeneration )
5112+ --[[ local ]] function generateName (nameGeneration )
51165113 if not cache [nameGeneration ] then
51175114 -- @Cleanup: Output the most significant byte first. (We need to know the length beforehand then, probably, so we use the correct bank.)
51185115 local charBytes = {}
54215418 end
54225419
54235420 -- Returns nil and a message or error.
5424- function writeStatements (buffer , pretty , indent , lastOutput , statements , nodeCb )
5421+ --[[ local ]] function writeStatements (buffer , pretty , indent , lastOutput , statements , nodeCb )
54255422 local skipNext = false
54265423
54275424 for i , statement in ipairs (statements ) do
55845581
55855582 -- success, lastOutput = writeNode( buffer, pretty, indent, lastOutput, node, maySafelyOmitParens, nodeCallback )
55865583 -- Returns nil and a message or error.
5587- function writeNode (buffer , pretty , indent , lastOutput , node , maySafelyOmitParens , nodeCb )
5584+ --[[ local ]] function writeNode (buffer , pretty , indent , lastOutput , node , maySafelyOmitParens , nodeCb )
55885585 if nodeCb then nodeCb (node , buffer ) end
55895586 pretty = choosePretty (node , pretty )
55905587
61226119 -- luaString = toLua( astNode [, prettyOuput=false, nodeCallback ] )
61236120 -- nodeCallback = function( node, outputBuffer )
61246121 -- Returns nil and a message on error.
6125- function toLua (node , pretty , nodeCb )
6122+ --[[ local ]] function toLua (node , pretty , nodeCb )
61266123 assertArg1 (" toLua" , 1 , node , " table" )
61276124
61286125 local buffer = {}
66236620 end
66246621
66256622 -- isValid, errors = validateTree( astNode )
6626- function validateTree (node )
6623+ --[[ local ]] function validateTree (node )
66276624 local path = {}
66286625 local errors = {}
66296626
@@ -6712,6 +6709,7 @@ end
67126709
67136710
67146711-- identifiers = findGlobalReferences( astNode )
6712+ -- Note: updateReferences() must have been called first!
67156713local function findGlobalReferences (theNode )
67166714 local idents = {}
67176715
@@ -6781,6 +6779,7 @@ end
67816779-- shadowSequences = findShadows( astNode )
67826780-- shadowSequences = { shadowSequence1, ... }
67836781-- shadowSequence = { shadowingIdentifier, shadowedIdentifier1, ... }
6782+ -- Note: updateReferences() must have been called first!
67846783local function findShadows (theNode )
67856784 local shadowSequences = {}
67866785 local shadowSequenceByIdent = {}
0 commit comments