|
1 | | -package ast |
2 | | - |
3 | | -import ( |
4 | | - "fmt" |
5 | | - "reflect" |
6 | | - "strings" |
7 | | -) |
8 | | - |
9 | | -// SpelNode represents a node in the SpEL Abstract Syntax Tree |
10 | | -type SpelNode interface { |
11 | | - // GetValue evaluates this node and returns the result |
12 | | - GetValue(state *ExpressionState) (interface{}, error) |
13 | | - |
14 | | - // GetTypedValue evaluates this node and returns a typed result |
15 | | - GetTypedValue(state *ExpressionState) (*TypedValue, error) |
16 | | - |
17 | | - // IsWritable returns true if this node can be assigned a value |
18 | | - IsWritable(state *ExpressionState) bool |
19 | | - |
20 | | - // SetValue assigns a value to this node |
21 | | - SetValue(state *ExpressionState, value interface{}) error |
22 | | - |
23 | | - // ToStringAST returns the string representation of this AST node |
24 | | - ToStringAST() string |
25 | | - |
26 | | - // GetStartPosition returns the start position in the expression string |
27 | | - GetStartPosition() int |
28 | | - |
29 | | - // GetEndPosition returns the end position in the expression string |
30 | | - GetEndPosition() int |
31 | | - |
32 | | - // GetChildren returns child nodes |
33 | | - GetChildren() []SpelNode |
34 | | - |
35 | | - // IsCompilable returns true if this node can be compiled |
36 | | - IsCompilable() bool |
37 | | -} |
38 | | - |
39 | | -// SpelNodeImpl provides a base implementation for SpEL nodes |
40 | | -type SpelNodeImpl struct { |
41 | | - StartPos int |
42 | | - EndPos int |
43 | | - Children []SpelNode |
44 | | -} |
45 | | - |
46 | | -// NewSpelNodeImpl creates a new base SpEL node |
47 | | -func NewSpelNodeImpl(startPos, endPos int, children ...SpelNode) *SpelNodeImpl { |
48 | | - return &SpelNodeImpl{ |
49 | | - StartPos: startPos, |
50 | | - EndPos: endPos, |
51 | | - Children: children, |
52 | | - } |
53 | | -} |
54 | | - |
55 | | -func (n *SpelNodeImpl) GetStartPosition() int { |
56 | | - return n.StartPos |
57 | | -} |
58 | | - |
59 | | -func (n *SpelNodeImpl) GetEndPosition() int { |
60 | | - return n.EndPos |
61 | | -} |
62 | | - |
63 | | -func (n *SpelNodeImpl) GetChildren() []SpelNode { |
64 | | - return n.Children |
65 | | -} |
66 | | - |
67 | | -func (n *SpelNodeImpl) IsCompilable() bool { |
68 | | - return false // Default implementation |
69 | | -} |
70 | | - |
71 | | -func (n *SpelNodeImpl) IsWritable(state *ExpressionState) bool { |
72 | | - return false // Default implementation |
73 | | -} |
74 | | - |
75 | | -func (n *SpelNodeImpl) SetValue(state *ExpressionState, value interface{}) error { |
76 | | - return fmt.Errorf("cannot set value on %T", n) |
77 | | -} |
78 | | - |
79 | | -// TypedValue represents a value with type information |
80 | | -type TypedValue struct { |
81 | | - Value interface{} |
82 | | - Type string |
83 | | -} |
84 | | - |
85 | | -func NewTypedValue(value interface{}) *TypedValue { |
86 | | - return &TypedValue{ |
87 | | - Value: value, |
88 | | - Type: fmt.Sprintf("%T", value), |
89 | | - } |
90 | | -} |
91 | | - |
92 | | -// ExpressionState holds the evaluation context and configuration |
93 | | -type ExpressionState struct { |
94 | | - EvaluationContext interface{} // Placeholder for evaluation context |
95 | | - Configuration *SpelParserConfiguration |
96 | | - RootObject *TypedValue |
97 | | -} |
98 | | - |
99 | | -func NewExpressionState(config *SpelParserConfiguration) *ExpressionState { |
100 | | - return &ExpressionState{ |
101 | | - Configuration: config, |
102 | | - RootObject: NewTypedValue(nil), |
103 | | - } |
104 | | -} |
105 | | - |
106 | | -func NewExpressionStateWithRoot(config *SpelParserConfiguration, rootObject *TypedValue) *ExpressionState { |
107 | | - return &ExpressionState{ |
108 | | - Configuration: config, |
109 | | - RootObject: rootObject, |
110 | | - } |
111 | | -} |
112 | | - |
113 | | -// SpelParserConfiguration holds parser configuration |
114 | | -type SpelParserConfiguration struct { |
115 | | - MaximumExpressionLength int |
116 | | - AutoGrowCollections bool |
117 | | - AutoGrowNullReferences bool |
118 | | -} |
119 | | - |
120 | | -func NewSpelParserConfiguration() *SpelParserConfiguration { |
121 | | - return &SpelParserConfiguration{ |
122 | | - MaximumExpressionLength: 10000, |
123 | | - AutoGrowCollections: false, |
124 | | - AutoGrowNullReferences: false, |
125 | | - } |
126 | | -} |
127 | | - |
128 | | -// ParserContext holds context information for parsing expressions |
129 | | -type ParserContext struct { |
130 | | - IsTemplate bool |
131 | | - ExpressionPrefix string |
132 | | - ExpressionSuffix string |
133 | | -} |
134 | | - |
135 | | -func NewParserContext() *ParserContext { |
136 | | - return &ParserContext{ |
137 | | - IsTemplate: false, |
138 | | - ExpressionPrefix: "#{", |
139 | | - ExpressionSuffix: "}", |
140 | | - } |
141 | | -} |
142 | | - |
143 | | -func NewTemplateParserContext() *ParserContext { |
144 | | - return &ParserContext{ |
145 | | - IsTemplate: true, |
146 | | - ExpressionPrefix: "#{", |
147 | | - ExpressionSuffix: "}", |
148 | | - } |
149 | | -} |
150 | | - |
151 | | -func NewTemplateParserContextWithDelimiters(prefix, suffix string) *ParserContext { |
152 | | - return &ParserContext{ |
153 | | - IsTemplate: true, |
154 | | - ExpressionPrefix: prefix, |
155 | | - ExpressionSuffix: suffix, |
156 | | - } |
157 | | -} |
158 | | - |
159 | | -// PrintAST 递归打印AST节点结构(参数使用接口类型) |
160 | | -func PrintAST(node SpelNode, level int) { |
161 | | - if node == nil { |
162 | | - return |
163 | | - } |
164 | | - |
165 | | - // 打印缩进 |
166 | | - indent := strings.Repeat(" ", level) |
167 | | - |
168 | | - // 获取节点类型名称 |
169 | | - nodeType := reflect.TypeOf(node).String() |
170 | | - // 移除包名前缀,只保留类型名 |
171 | | - if idx := strings.LastIndex(nodeType, "."); idx >= 0 { |
172 | | - nodeType = nodeType[idx+1:] |
173 | | - } |
174 | | - // 移除指针符号 |
175 | | - nodeType = strings.TrimPrefix(nodeType, "*") |
176 | | - |
177 | | - // 打印节点信息 |
178 | | - fmt.Printf("%s节点类型: %s, 表达式片段: '%s'\n", |
179 | | - indent, nodeType, node.ToStringAST()) |
180 | | - |
181 | | - // 递归打印子节点 |
182 | | - children := node.GetChildren() |
183 | | - for _, child := range children { |
184 | | - // fmt.Printf("%s子节点[%d]:\n", indent, i) |
185 | | - PrintAST(child, level+1) |
186 | | - } |
187 | | -} |
188 | | - |
189 | | -// PrintASTWithTitle 打印带标题的 AST 树结构 |
190 | | -func PrintASTWithTitle(node SpelNode, title string) { |
191 | | - fmt.Printf("\n=== %s ===\n", title) |
192 | | - PrintAST(node, 0) |
193 | | - fmt.Println() |
194 | | -} |
| 1 | +package ast |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// SpelNode represents a node in the SpEL Abstract Syntax Tree |
| 10 | +type SpelNode interface { |
| 11 | + // GetValue evaluates this node and returns the result |
| 12 | + GetValue(state *ExpressionState) (interface{}, error) |
| 13 | + |
| 14 | + // GetTypedValue evaluates this node and returns a typed result |
| 15 | + GetTypedValue(state *ExpressionState) (*TypedValue, error) |
| 16 | + |
| 17 | + // IsWritable returns true if this node can be assigned a value |
| 18 | + IsWritable(state *ExpressionState) bool |
| 19 | + |
| 20 | + // SetValue assigns a value to this node |
| 21 | + SetValue(state *ExpressionState, value interface{}) error |
| 22 | + |
| 23 | + // ToStringAST returns the string representation of this AST node |
| 24 | + ToStringAST() string |
| 25 | + |
| 26 | + // GetStartPosition returns the start position in the expression string |
| 27 | + GetStartPosition() int |
| 28 | + |
| 29 | + // GetEndPosition returns the end position in the expression string |
| 30 | + GetEndPosition() int |
| 31 | + |
| 32 | + // GetChildren returns child nodes |
| 33 | + GetChildren() []SpelNode |
| 34 | + |
| 35 | + // IsCompilable returns true if this node can be compiled |
| 36 | + IsCompilable() bool |
| 37 | +} |
| 38 | + |
| 39 | +// SpelNodeImpl provides a base implementation for SpEL nodes |
| 40 | +type SpelNodeImpl struct { |
| 41 | + StartPos int |
| 42 | + EndPos int |
| 43 | + Children []SpelNode |
| 44 | +} |
| 45 | + |
| 46 | +// NewSpelNodeImpl creates a new base SpEL node |
| 47 | +func NewSpelNodeImpl(startPos, endPos int, children ...SpelNode) *SpelNodeImpl { |
| 48 | + return &SpelNodeImpl{ |
| 49 | + StartPos: startPos, |
| 50 | + EndPos: endPos, |
| 51 | + Children: children, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func (n *SpelNodeImpl) GetStartPosition() int { |
| 56 | + return n.StartPos |
| 57 | +} |
| 58 | + |
| 59 | +func (n *SpelNodeImpl) GetEndPosition() int { |
| 60 | + return n.EndPos |
| 61 | +} |
| 62 | + |
| 63 | +func (n *SpelNodeImpl) GetChildren() []SpelNode { |
| 64 | + return n.Children |
| 65 | +} |
| 66 | + |
| 67 | +func (n *SpelNodeImpl) IsCompilable() bool { |
| 68 | + return false // Default implementation |
| 69 | +} |
| 70 | + |
| 71 | +func (n *SpelNodeImpl) IsWritable(state *ExpressionState) bool { |
| 72 | + return false // Default implementation |
| 73 | +} |
| 74 | + |
| 75 | +func (n *SpelNodeImpl) SetValue(state *ExpressionState, value interface{}) error { |
| 76 | + return fmt.Errorf("cannot set value on %T", n) |
| 77 | +} |
| 78 | + |
| 79 | +// TypedValue represents a value with type information |
| 80 | +type TypedValue struct { |
| 81 | + Value interface{} |
| 82 | + Type string |
| 83 | +} |
| 84 | + |
| 85 | +func NewTypedValue(value interface{}) *TypedValue { |
| 86 | + return &TypedValue{ |
| 87 | + Value: value, |
| 88 | + Type: fmt.Sprintf("%T", value), |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// ExpressionState holds the evaluation context and configuration |
| 93 | +type ExpressionState struct { |
| 94 | + EvaluationContext interface{} // Placeholder for evaluation context |
| 95 | + Configuration *SpelParserConfiguration |
| 96 | + RootObject *TypedValue |
| 97 | +} |
| 98 | + |
| 99 | +func NewExpressionState(config *SpelParserConfiguration) *ExpressionState { |
| 100 | + return &ExpressionState{ |
| 101 | + Configuration: config, |
| 102 | + RootObject: NewTypedValue(nil), |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func NewExpressionStateWithRoot(config *SpelParserConfiguration, rootObject *TypedValue) *ExpressionState { |
| 107 | + return &ExpressionState{ |
| 108 | + Configuration: config, |
| 109 | + RootObject: rootObject, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// SpelParserConfiguration holds parser configuration |
| 114 | +type SpelParserConfiguration struct { |
| 115 | + MaximumExpressionLength int |
| 116 | + AutoGrowCollections bool |
| 117 | + AutoGrowNullReferences bool |
| 118 | +} |
| 119 | + |
| 120 | +func NewSpelParserConfiguration() *SpelParserConfiguration { |
| 121 | + return &SpelParserConfiguration{ |
| 122 | + MaximumExpressionLength: 10000, |
| 123 | + AutoGrowCollections: false, |
| 124 | + AutoGrowNullReferences: false, |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// ParserContext holds context information for parsing expressions |
| 129 | +type ParserContext struct { |
| 130 | + IsTemplate bool |
| 131 | + ExpressionPrefix string |
| 132 | + ExpressionSuffix string |
| 133 | +} |
| 134 | + |
| 135 | +func NewParserContext() *ParserContext { |
| 136 | + return &ParserContext{ |
| 137 | + IsTemplate: false, |
| 138 | + ExpressionPrefix: "#{", |
| 139 | + ExpressionSuffix: "}", |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func NewTemplateParserContext() *ParserContext { |
| 144 | + return &ParserContext{ |
| 145 | + IsTemplate: true, |
| 146 | + ExpressionPrefix: "#{", |
| 147 | + ExpressionSuffix: "}", |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func NewTemplateParserContextWithDelimiters(prefix, suffix string) *ParserContext { |
| 152 | + return &ParserContext{ |
| 153 | + IsTemplate: true, |
| 154 | + ExpressionPrefix: prefix, |
| 155 | + ExpressionSuffix: suffix, |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +// PrintAST 递归打印AST节点结构(参数使用接口类型) |
| 160 | +func PrintAST(node SpelNode, level int) { |
| 161 | + if node == nil { |
| 162 | + return |
| 163 | + } |
| 164 | + |
| 165 | + // 打印缩进 |
| 166 | + indent := strings.Repeat(" ", level) |
| 167 | + |
| 168 | + // 获取节点类型名称 |
| 169 | + nodeType := reflect.TypeOf(node).String() |
| 170 | + // 移除包名前缀,只保留类型名 |
| 171 | + if idx := strings.LastIndex(nodeType, "."); idx >= 0 { |
| 172 | + nodeType = nodeType[idx+1:] |
| 173 | + } |
| 174 | + // 移除指针符号 |
| 175 | + nodeType = strings.TrimPrefix(nodeType, "*") |
| 176 | + |
| 177 | + // 打印节点信息 |
| 178 | + fmt.Printf("%s节点类型: %s, 表达式片段: '%s'\n", |
| 179 | + indent, nodeType, node.ToStringAST()) |
| 180 | + |
| 181 | + // 递归打印子节点 |
| 182 | + children := node.GetChildren() |
| 183 | + for _, child := range children { |
| 184 | + // fmt.Printf("%s子节点[%d]:\n", indent, i) |
| 185 | + PrintAST(child, level+1) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +// PrintASTWithTitle 打印带标题的 AST 树结构 |
| 190 | +func PrintASTWithTitle(node SpelNode, title string) { |
| 191 | + fmt.Printf("\n=== %s ===\n", title) |
| 192 | + PrintAST(node, 0) |
| 193 | + fmt.Println() |
| 194 | +} |
0 commit comments