-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
ast-visitor.ts
100 lines (93 loc) · 2.72 KB
/
ast-visitor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Expr, ExprBinary, ExprUnary, ExprRef, ExprList, ExprCall, ExprCast, ExprCase, ExprMember, ExprArrayIndex, ExprTernary, SelectStatement } from './parser/syntax/ast.ts';
import { nil, NotSupported } from './interfaces-private.ts';
export class AstVisitor {
visit(val: Expr | nil) {
if (!val) {
return;
}
switch (val.type) {
case 'binary':
return this.visitBinary(val);
case 'unary':
return this.visitUnary(val);
case 'ref':
return this.visitRef(val);
case 'string':
case 'numeric':
case 'integer':
case 'boolean':
case 'constant':
return this.visitConstant(val.value);
case 'null':
return this.visitConstant(null);
case 'list':
return this.visitArray(val);
case 'call':
return this.visitCall(val);
case 'cast':
return this.visitCast(val)
case 'case':
return this.visitCase(val);
case 'member':
return this.visitMember(val);
case 'arrayIndex':
return this.visitArrayIndex(val);
case 'ternary':
return this.visitTernary(val);
case 'select':
return this.visitSelection(val);
default:
throw NotSupported.never(val);
}
}
visitSelection(val: SelectStatement) {
for (const c of val.columns!) {
this.visit(c.expr);
}
this.visit(val.where);
}
visitTernary(val: ExprTernary) {
this.visit(val.value);
this.visit(val.lo);
this.visit(val.hi);
}
visitArrayIndex(val: ExprArrayIndex) {
this.visit(val.array);
this.visit(val.index);
}
visitMember(val: ExprMember) {
this.visit(val.operand);
}
visitCase(val: ExprCase) {
this.visit(val.value);
for (const w of val.whens) {
this.visit(w.when);
this.visit(w.value);
}
this.visit(val.else);
}
visitCast(val: ExprCast) {
this.visit(val.operand);
}
visitCall(val: ExprCall) {
for (const a of val.args) {
this.visit(a);
}
}
visitArray(val: ExprList) {
for (const e of val.expressions) {
this.visit(e);
}
}
visitConstant(value: string | number | boolean | null) {
}
visitRef(val: ExprRef) {
}
visitUnary(val: ExprUnary) {
this.visit(val.operand);
}
visitBinary(val: ExprBinary) {
this.visit(val.left);
this.visit(val.right);
}
}