Interface ExpressionVisitor<T>
- Type Parameters:
T- the return type of the visit operations | 访问操作的返回类型
public interface ExpressionVisitor<T>
Expression Visitor Interface
表达式访问者接口
Generic visitor interface for traversing expression Abstract Syntax Trees (ASTs). Implements the Visitor design pattern to enable operations over the AST without modifying the node classes.
用于遍历表达式抽象语法树(AST)的泛型访问者接口。 实现访问者设计模式,允许在不修改节点类的情况下对AST执行操作。
Features | 主要功能:
- Type-safe visitor pattern with generic return type - 带泛型返回类型的类型安全访问者模式
- Complete coverage of all AST node types - 覆盖所有AST节点类型
- Supports core nodes (literal, identifier, binary, unary, ternary) - 支持核心节点(字面量、标识符、二元、一元、三元)
- Supports access nodes (property, index, method, function) - 支持访问节点(属性、索引、方法、函数)
- Supports collection nodes (filter, project, list literal) - 支持集合节点(过滤、投影、列表字面量)
- Supports V1.0.3 nodes (elvis, in, between, bitwise, lambda, map literal, string interpolation) - 支持V1.0.3节点(elvis、in、between、位运算、lambda、映射字面量、字符串插值)
Usage Examples | 使用示例:
// Create a visitor that converts AST to string representation
ExpressionVisitor<String> printer = new ExpressionVisitor<>() {
@Override
public String visit(LiteralNode node) {
return String.valueOf(node.value());
}
@Override
public String visit(BinaryOpNode node) {
return "(" + visit(node.left()) + " " + node.operator() + " " + visit(node.right()) + ")";
}
// ... implement other visit methods
};
// Create a visitor that collects all function names
ExpressionVisitor<Set<String>> functionCollector = new ExpressionVisitor<>() {
@Override
public Set<String> visit(FunctionCallNode node) {
Set<String> names = new HashSet<>();
names.add(node.functionName());
for (Node arg : node.arguments()) {
names.addAll(arg.accept(this));
}
return names;
}
// ... implement other visit methods
};
Security | 安全性:
- Thread-safe: Depends on implementation - 线程安全: 取决于实现
- Null-safe: Implementations should guard against null nodes - 空值安全: 实现应防御null节点
Performance | 性能:
- O(n) traversal where n is the number of nodes in the AST - O(n)遍历,n为AST中的节点数
- No additional memory allocation beyond implementation-specific state - 除实现特定状态外无额外内存分配
- Since:
- JDK 25, opencode-base-expression V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvisit(BetweenNode node) Visit abetweenrange test node 访问between范围测试节点visit(BinaryOpNode node) Visit a binary operation node 访问二元运算节点visit(BitwiseOpNode node) Visit a bitwise operation node 访问位运算节点visit(CollectionFilterNode node) Visit a collection filter node 访问集合过滤节点visit(CollectionProjectNode node) Visit a collection project node 访问集合投影节点Visit an elvis (null-coalescing) node 访问Elvis(空值合并)节点visit(FunctionCallNode node) Visit a function call node 访问函数调用节点visit(IdentifierNode node) Visit an identifier node 访问标识符节点visit(IndexAccessNode node) Visit an index access node 访问索引访问节点Visit aninmembership test node 访问in成员测试节点visit(LambdaNode node) Visit a lambda expression node 访问Lambda表达式节点visit(ListLiteralNode node) Visit a list literal node 访问列表字面量节点visit(LiteralNode node) Visit a literal node 访问字面量节点visit(MapLiteralNode node) Visit a map literal node 访问映射字面量节点visit(MethodCallNode node) Visit a method call node 访问方法调用节点visit(PropertyAccessNode node) Visit a property access node 访问属性访问节点visit(StringInterpolationNode node) Visit a string interpolation node 访问字符串插值节点visit(TernaryOpNode node) Visit a ternary operation node 访问三元运算节点visit(UnaryOpNode node) Visit a unary operation node 访问一元运算节点
-
Method Details
-
visit
Visit a literal node 访问字面量节点- Parameters:
node- the literal node | 字面量节点- Returns:
- the visit result | 访问结果
-
visit
Visit an identifier node 访问标识符节点- Parameters:
node- the identifier node | 标识符节点- Returns:
- the visit result | 访问结果
-
visit
Visit a binary operation node 访问二元运算节点- Parameters:
node- the binary operation node | 二元运算节点- Returns:
- the visit result | 访问结果
-
visit
Visit a unary operation node 访问一元运算节点- Parameters:
node- the unary operation node | 一元运算节点- Returns:
- the visit result | 访问结果
-
visit
Visit a ternary operation node 访问三元运算节点- Parameters:
node- the ternary operation node | 三元运算节点- Returns:
- the visit result | 访问结果
-
visit
Visit a property access node 访问属性访问节点- Parameters:
node- the property access node | 属性访问节点- Returns:
- the visit result | 访问结果
-
visit
Visit an index access node 访问索引访问节点- Parameters:
node- the index access node | 索引访问节点- Returns:
- the visit result | 访问结果
-
visit
Visit a method call node 访问方法调用节点- Parameters:
node- the method call node | 方法调用节点- Returns:
- the visit result | 访问结果
-
visit
Visit a function call node 访问函数调用节点- Parameters:
node- the function call node | 函数调用节点- Returns:
- the visit result | 访问结果
-
visit
Visit a collection filter node 访问集合过滤节点- Parameters:
node- the collection filter node | 集合过滤节点- Returns:
- the visit result | 访问结果
-
visit
Visit a collection project node 访问集合投影节点- Parameters:
node- the collection project node | 集合投影节点- Returns:
- the visit result | 访问结果
-
visit
Visit a list literal node 访问列表字面量节点- Parameters:
node- the list literal node | 列表字面量节点- Returns:
- the visit result | 访问结果
-
visit
-
visit
-
visit
Visit abetweenrange test node 访问between范围测试节点- Parameters:
node- the between node | between节点- Returns:
- the visit result | 访问结果
-
visit
Visit a bitwise operation node 访问位运算节点- Parameters:
node- the bitwise operation node | 位运算节点- Returns:
- the visit result | 访问结果
-
visit
Visit a lambda expression node 访问Lambda表达式节点- Parameters:
node- the lambda node | Lambda节点- Returns:
- the visit result | 访问结果
-
visit
Visit a map literal node 访问映射字面量节点- Parameters:
node- the map literal node | 映射字面量节点- Returns:
- the visit result | 访问结果
-
visit
Visit a string interpolation node 访问字符串插值节点- Parameters:
node- the string interpolation node | 字符串插值节点- Returns:
- the visit result | 访问结果
-