Skip to content

Commit 5c58e9f

Browse files
committed
feat: add AstNode (32-byte) and ParseResult structs
1 parent c9b7e86 commit 5c58e9f

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

include/sql_parser/ast.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#ifndef SQL_PARSER_AST_H
2+
#define SQL_PARSER_AST_H
3+
4+
#include "sql_parser/common.h"
5+
#include "sql_parser/arena.h"
6+
#include <cstdint>
7+
#include <type_traits>
8+
9+
namespace sql_parser {
10+
11+
struct AstNode {
12+
AstNode* first_child;
13+
AstNode* next_sibling;
14+
const char* value_ptr;
15+
uint32_t value_len;
16+
NodeType type;
17+
uint16_t flags;
18+
19+
StringRef value() const { return StringRef{value_ptr, value_len}; }
20+
21+
void set_value(StringRef ref) {
22+
value_ptr = ref.ptr;
23+
value_len = ref.len;
24+
}
25+
26+
void add_child(AstNode* child) {
27+
if (!child) return;
28+
if (!first_child) {
29+
first_child = child;
30+
return;
31+
}
32+
AstNode* last = first_child;
33+
while (last->next_sibling) last = last->next_sibling;
34+
last->next_sibling = child;
35+
}
36+
};
37+
static_assert(sizeof(AstNode) == 32, "AstNode must be 32 bytes");
38+
static_assert(std::is_trivially_copyable_v<AstNode>);
39+
40+
inline AstNode* make_node(Arena& arena, NodeType type, StringRef value = {},
41+
uint16_t flags = 0) {
42+
AstNode* node = arena.allocate_typed<AstNode>();
43+
if (!node) return nullptr;
44+
node->type = type;
45+
node->flags = flags;
46+
node->value_ptr = value.ptr;
47+
node->value_len = value.len;
48+
return node;
49+
}
50+
51+
} // namespace sql_parser
52+
53+
#endif // SQL_PARSER_AST_H

include/sql_parser/parse_result.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef SQL_PARSER_PARSE_RESULT_H
2+
#define SQL_PARSER_PARSE_RESULT_H
3+
4+
#include "sql_parser/common.h"
5+
#include "sql_parser/ast.h"
6+
7+
namespace sql_parser {
8+
9+
struct ErrorInfo {
10+
uint32_t offset = 0;
11+
StringRef message;
12+
};
13+
14+
struct ParseResult {
15+
enum Status : uint8_t { OK = 0, PARTIAL, ERROR };
16+
17+
Status status = ERROR;
18+
StmtType stmt_type = StmtType::UNKNOWN;
19+
AstNode* ast = nullptr;
20+
ErrorInfo error;
21+
StringRef remaining;
22+
23+
StringRef table_name;
24+
StringRef schema_name;
25+
StringRef database_name;
26+
27+
bool ok() const { return status == OK; }
28+
bool has_remaining() const { return !remaining.empty(); }
29+
};
30+
31+
} // namespace sql_parser
32+
33+
#endif // SQL_PARSER_PARSE_RESULT_H

0 commit comments

Comments
 (0)