66--= Tokenize Lua code or create ASTs (Abstract Syntax Trees)
77--= and convert the data back to Lua.
88--=
9- --= Version: 2.2 (2022-06-02)
9+ --= Version: 2.2-dev
1010--=
1111--= License: MIT (see the bottom of this file)
1212--= Website: http://refreezed.com/luaparser/
@@ -62,6 +62,7 @@ simplify, optimize, minify
6262toLua
6363printTokens, printNode, printTree
6464formatMessage
65+ findDeclaredNames, findGlobalReferences, findShadows
6566
6667tokenize()
6768 tokens = parser.tokenize( luaString [, pathForErrorMessages="?" ] )
@@ -258,6 +259,23 @@ formatMessage()
258259 print(parser.formatMessage(currentStatement, "Current statement."))
259260 end
260261
262+ findDeclaredNames()
263+ identifiers = parser.findDeclaredNames( astNode )
264+ Find all declared names in the tree (i.e. identifiers from AstDeclaration, AstFunction and AstFor nodes).
265+
266+ findGlobalReferences()
267+ identifiers = parser.findGlobalReferences( astNode )
268+ Find all identifiers not referring to local variables in the tree.
269+ Note: updateReferences() must be called at some point before you call this - otherwise all variables will be seen as globals!
270+
271+ findShadows()
272+ shadowSequences = parser.findShadows( astNode )
273+ shadowSequences = { shadowSequence1, ... }
274+ shadowSequence = { shadowingIdentifier, shadowedIdentifier1, ... }
275+ Find local variable shadowing in the tree. Each shadowSequence is an array of declared identifiers where each identifier shadows the next one.
276+ Note: updateReferences() must be called at some point before you call this - otherwise all variables will be seen as globals!
277+ Note: Shadowing of globals cannot be detected by the function as that would require knowledge of all potential globals in your program. (See findGlobalReferences())
278+
261279
2622802.2 - Constants
263281----------------------------------------------------------------
@@ -436,7 +454,7 @@ Special number notation rules.
436454
437455-============================================================]=]
438456
439- local PARSER_VERSION = " 2.2.0"
457+ local PARSER_VERSION = " 2.2.0-dev "
440458
441459local NORMALIZE_MINUS_ZERO , HANDLE_ENV
442460do
@@ -5037,6 +5055,7 @@ local function minify(node, doOptimize)
50375055 _optimize (node , stats )
50385056 end
50395057
5058+ -- @Cleanup: Use findShadows()?
50405059 local identInfos , declIdentWatchers = getInformationAboutIdentifiersAndUpdateReferences (node )
50415060 -- local funcInfos = getInformationAboutFunctions(node)
50425061 -- local declIdentReadCount, declIdentAssignmentCount = getAccessesOfDeclaredNames(funcInfos, identInfos, declIdentWatchers)
@@ -6597,7 +6616,7 @@ end
65976616
65986617
65996618
6600- -- identifiers = findGlobalReferences( astNode ) @Doc
6619+ -- identifiers = findGlobalReferences( astNode )
66016620local function findGlobalReferences (theNode )
66026621 local idents = {}
66036622
@@ -6612,11 +6631,14 @@ end
66126631
66136632
66146633
6615- -- identifiers = findDeclaredNames( astNode ) @Doc
6634+ -- identifiers = findDeclaredNames( astNode )
66166635local function findDeclaredNames (theNode )
66176636 local declIdents = {}
66186637
66196638 traverseTree (theNode , function (node )
6639+ -- Note: We don't now, but if we would require updateReferences() to be called first
6640+ -- we could just check the type and if node.declaration==node. Decisions...
6641+
66206642 if node .type == " declaration" or node .type == " for" then
66216643 for _ , declIdent in ipairs (node .names ) do
66226644 tableInsert (declIdents , declIdent )
@@ -6661,7 +6683,7 @@ local function maybeRegisterShadow(shadowSequences, shadowSequenceByIdent, shado
66616683 end
66626684end
66636685
6664- -- shadowSequences = findShadows( theNode ) @Doc
6686+ -- shadowSequences = findShadows( astNode )
66656687-- shadowSequences = { shadowSequence1, ... }
66666688-- shadowSequence = { shadowingIdentifier, shadowedIdentifier1, ... }
66676689local function findShadows (theNode )
@@ -6775,9 +6797,9 @@ parser = {
67756797 printTree = printTree ,
67766798 formatMessage = formatMessage ,
67776799
6778- -- Utilies.
6779- findGlobalReferences = findGlobalReferences ,
6800+ -- Utilities.
67806801 findDeclaredNames = findDeclaredNames ,
6802+ findGlobalReferences = findGlobalReferences ,
67816803 findShadows = findShadows ,
67826804
67836805 -- Misc.
0 commit comments