|
| 1 | +#ifndef SQL_PARSER_AST_TRANSFORM_H |
| 2 | +#define SQL_PARSER_AST_TRANSFORM_H |
| 3 | + |
| 4 | +#include "sql_parser/ast_walk.h" |
| 5 | +#include <unordered_set> |
| 6 | + |
| 7 | +namespace sql_parser { |
| 8 | + |
| 9 | +enum class AstError { |
| 10 | + None, LimitExceeded, AllocationFailure, InvalidTree, InvalidReplacement, |
| 11 | + NotFound, UnsupportedRoot, UnsupportedContext, InvalidPlaceholder, ParameterOverflow |
| 12 | +}; |
| 13 | + |
| 14 | +struct AstCloneResult { |
| 15 | + AstNode* ast = nullptr; |
| 16 | + AstError error = AstError::None; |
| 17 | + bool ok() const { return error == AstError::None; } |
| 18 | +}; |
| 19 | + |
| 20 | +// Unlike make_node(), copies value and source into the destination arena. |
| 21 | +inline AstNode* make_owned_node(Arena& arena, NodeType type, StringRef value = {}, |
| 22 | + uint16_t flags = 0, StringRef source = {}) { |
| 23 | + if ((value.len && !value.ptr) || (source.len && !source.ptr)) return nullptr; |
| 24 | + StringRef owned_value; |
| 25 | + StringRef owned_source; |
| 26 | + if (value.len) { |
| 27 | + owned_value = arena.allocate_string(value.ptr, value.len); |
| 28 | + if (!owned_value.ptr) return nullptr; |
| 29 | + } |
| 30 | + if (source.len) { |
| 31 | + if (source.ptr == value.ptr && source.len == value.len) owned_source = owned_value; |
| 32 | + else owned_source = arena.allocate_string(source.ptr, source.len); |
| 33 | + if (!owned_source.ptr) return nullptr; |
| 34 | + } |
| 35 | + AstNode* node = make_node(arena, type, owned_value, flags); |
| 36 | + if (node) node->set_source(owned_source); |
| 37 | + return node; |
| 38 | +} |
| 39 | + |
| 40 | +// Copies exactly one subtree, including all text. On failure ast is null; |
| 41 | +// partial allocations remain in the destination arena until its next reset. |
| 42 | +inline AstCloneResult clone_ast(const AstNode* root, Arena& arena, |
| 43 | + AstWalkLimits limits = {}) { |
| 44 | + AstCloneResult result; |
| 45 | + std::unordered_set<const AstNode*> seen; |
| 46 | + std::vector<AstNode*> ancestors; |
| 47 | + std::vector<AstNode*> last_child; |
| 48 | + auto walked = walk_ast(root, [&](const AstNode& node, const AstVisitContext& ctx) { |
| 49 | + if (!seen.insert(&node).second || (node.value_len && !node.value_ptr) || |
| 50 | + (node.source_len && !node.source_ptr)) { |
| 51 | + result.error = AstError::InvalidTree; |
| 52 | + return AstVisitAction::Stop; |
| 53 | + } |
| 54 | + AstNode* copy = make_owned_node(arena, node.type, node.value(), node.flags, node.source()); |
| 55 | + if (!copy) { |
| 56 | + result.error = AstError::AllocationFailure; |
| 57 | + return AstVisitAction::Stop; |
| 58 | + } |
| 59 | + ancestors.resize(ctx.depth + 1); |
| 60 | + last_child.resize(ctx.depth + 1); |
| 61 | + ancestors[ctx.depth] = copy; |
| 62 | + last_child[ctx.depth] = nullptr; |
| 63 | + if (!ctx.parent) result.ast = copy; |
| 64 | + else { |
| 65 | + if (last_child[ctx.depth - 1]) last_child[ctx.depth - 1]->next_sibling = copy; |
| 66 | + else ancestors[ctx.depth - 1]->first_child = copy; |
| 67 | + last_child[ctx.depth - 1] = copy; |
| 68 | + } |
| 69 | + return AstVisitAction::Continue; |
| 70 | + }, limits); |
| 71 | + if (walked.status == AstWalkStatus::LimitExceeded) result.error = AstError::LimitExceeded; |
| 72 | + if (!result.ok()) result.ast = nullptr; |
| 73 | + return result; |
| 74 | +} |
| 75 | + |
| 76 | +// Replaces/removes target within root, preserving its sibling position. A |
| 77 | +// replacement must be a detached subtree, disjoint from the whole input tree. |
| 78 | +// root must be a standalone tree (no next_sibling). Validation completes |
| 79 | +// before any mutation; target is detached on success. |
| 80 | +inline AstError replace_ast_subtree(AstNode*& root, AstNode* target, |
| 81 | + AstNode* replacement, AstWalkLimits limits = {}) { |
| 82 | + if (!target) return AstError::NotFound; |
| 83 | + if (root && root->next_sibling) return AstError::InvalidTree; |
| 84 | + if (replacement && replacement->next_sibling) return AstError::InvalidReplacement; |
| 85 | + std::unordered_set<const AstNode*> original; |
| 86 | + AstNode* parent = nullptr; |
| 87 | + bool found = false; |
| 88 | + AstError error = AstError::None; |
| 89 | + auto walked = walk_ast(root, [&](const AstNode& node, const AstVisitContext& ctx) { |
| 90 | + if (!original.insert(&node).second) { |
| 91 | + error = AstError::InvalidTree; |
| 92 | + return AstVisitAction::Stop; |
| 93 | + } |
| 94 | + if (&node == target) { |
| 95 | + parent = const_cast<AstNode*>(ctx.parent); |
| 96 | + found = true; |
| 97 | + } |
| 98 | + return AstVisitAction::Continue; |
| 99 | + }, limits); |
| 100 | + if (walked.status == AstWalkStatus::LimitExceeded) return AstError::LimitExceeded; |
| 101 | + if (error != AstError::None) return error; |
| 102 | + if (!found) return AstError::NotFound; |
| 103 | + std::unordered_set<const AstNode*> incoming; |
| 104 | + walked = walk_ast(replacement, [&](const AstNode& node, const AstVisitContext&) { |
| 105 | + if (original.count(&node) || !incoming.insert(&node).second) { |
| 106 | + error = AstError::InvalidReplacement; |
| 107 | + return AstVisitAction::Stop; |
| 108 | + } |
| 109 | + return AstVisitAction::Continue; |
| 110 | + }, limits); |
| 111 | + if (walked.status == AstWalkStatus::LimitExceeded) return AstError::LimitExceeded; |
| 112 | + if (error != AstError::None) return error; |
| 113 | + AstNode** slot = parent ? &parent->first_child : &root; |
| 114 | + while (*slot != target) slot = &(*slot)->next_sibling; |
| 115 | + if (replacement) replacement->next_sibling = target->next_sibling; |
| 116 | + *slot = replacement ? replacement : target->next_sibling; |
| 117 | + target->next_sibling = nullptr; |
| 118 | + return AstError::None; |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace sql_parser |
| 122 | + |
| 123 | +#endif // SQL_PARSER_AST_TRANSFORM_H |
0 commit comments