22
33local T = require ("./ast_types" )
44
5- type Visitor = {
5+ export type Visitor = {
66 visitBlock : (T .AstStatBlock ) -> boolean ,
7+ visitBlockEnd : (T .AstStatBlock ) -> (),
78 visitIf : (T .AstStatIf ) -> boolean ,
89 visitWhile : (T .AstStatWhile ) -> boolean ,
910 visitRepeat : (T .AstStatRepeat ) -> boolean ,
@@ -17,6 +18,8 @@ type Visitor = {
1718 visitLocalFunction : (T .AstStatLocalFunction ) -> boolean ,
1819 visitTypeAlias : (T .AstStatTypeAlias ) -> boolean ,
1920
21+ visitExpression : (T .AstExpr ) -> boolean ,
22+ visitExpressionEnd : (T .AstExpr ) -> (),
2023 visitLocalReference : (T .AstExprLocal ) -> boolean ,
2124 visitGlobal : (T .AstExprGlobal ) -> boolean ,
2225 visitCall : (T .AstExprCall ) -> boolean ,
5053
5154local defaultVisitor : Visitor = {
5255 visitBlock = alwaysVisit :: any ,
56+ visitBlockEnd = alwaysVisit :: any ,
5357 visitIf = alwaysVisit :: any ,
5458 visitWhile = alwaysVisit :: any ,
5559 visitRepeat = alwaysVisit :: any ,
@@ -63,6 +67,8 @@ local defaultVisitor: Visitor = {
6367 visitLocalFunction = alwaysVisit :: any ,
6468 visitTypeAlias = alwaysVisit :: any ,
6569
70+ visitExpression = alwaysVisit :: any ,
71+ visitExpressionEnd = alwaysVisit :: any ,
6672 visitLocalReference = alwaysVisit :: any ,
6773 visitGlobal = alwaysVisit :: any ,
6874 visitCall = alwaysVisit :: any ,
@@ -118,6 +124,8 @@ local function visitBlock(block: T.AstStatBlock, visitor: Visitor)
118124 for _ , statement in block .statements do
119125 visitStatement (statement , visitor )
120126 end
127+
128+ visitor .visitBlockEnd (block )
121129 end
122130end
123131
@@ -447,38 +455,42 @@ local function visitTypeGroup(node: T.AstTypeGroup, visitor: Visitor)
447455end
448456
449457function visitExpression (expression : T .AstExpr , visitor : Visitor )
450- if expression .tag == "nil" then
451- visitNil (expression , visitor )
452- elseif expression .tag == "boolean" then
453- visitBoolean (expression , visitor )
454- elseif expression .tag == "number" then
455- visitNumber (expression , visitor )
456- elseif expression .tag == "string" then
457- visitString (expression , visitor )
458- elseif expression .tag == "local" then
459- visitLocalReference (expression , visitor )
460- elseif expression .tag == "global" then
461- visitGlobal (expression , visitor )
462- elseif expression .tag == "vararg" then
463- visitVarargs (expression , visitor )
464- elseif expression .tag == "call" then
465- visitCall (expression , visitor )
466- elseif expression .tag == "unary" then
467- visitUnary (expression , visitor )
468- elseif expression .tag == "binary" then
469- visitBinary (expression , visitor )
470- elseif expression .tag == "function" then
471- visitAnonymousFunction (expression , visitor )
472- elseif expression .tag == "table" then
473- visitTable (expression , visitor )
474- elseif expression .tag == "indexname" then
475- visitIndexName (expression , visitor )
476- elseif expression .tag == "index" then
477- visitIndexExpr (expression , visitor )
478- elseif expression .tag == "group" then
479- visitGroup (expression , visitor )
480- else
481- exhaustiveMatch (expression .tag )
458+ if visitor .visitExpression (expression ) then
459+ if expression .tag == "nil" then
460+ visitNil (expression , visitor )
461+ elseif expression .tag == "boolean" then
462+ visitBoolean (expression , visitor )
463+ elseif expression .tag == "number" then
464+ visitNumber (expression , visitor )
465+ elseif expression .tag == "string" then
466+ visitString (expression , visitor )
467+ elseif expression .tag == "local" then
468+ visitLocalReference (expression , visitor )
469+ elseif expression .tag == "global" then
470+ visitGlobal (expression , visitor )
471+ elseif expression .tag == "vararg" then
472+ visitVarargs (expression , visitor )
473+ elseif expression .tag == "call" then
474+ visitCall (expression , visitor )
475+ elseif expression .tag == "unary" then
476+ visitUnary (expression , visitor )
477+ elseif expression .tag == "binary" then
478+ visitBinary (expression , visitor )
479+ elseif expression .tag == "function" then
480+ visitAnonymousFunction (expression , visitor )
481+ elseif expression .tag == "table" then
482+ visitTable (expression , visitor )
483+ elseif expression .tag == "indexname" then
484+ visitIndexName (expression , visitor )
485+ elseif expression .tag == "index" then
486+ visitIndexExpr (expression , visitor )
487+ elseif expression .tag == "group" then
488+ visitGroup (expression , visitor )
489+ else
490+ exhaustiveMatch (expression .tag )
491+ end
492+
493+ visitor .visitExpressionEnd (expression )
482494 end
483495end
484496
543555return {
544556 createVisitor = createVisitor ,
545557 visitBlock = visitBlock ,
558+ visitStatement = visitStatement ,
559+ visitExpression = visitExpression ,
560+ visitType = visitType ,
546561}
0 commit comments