Parser Architecture¶
The parser transforms the flat token stream produced by the lexer into structured, strongly-typed Abstract Syntax Tree (AST) nodes using recursive-descent parsing.
Source Location¶
- Headers:
src/parser/parser.h,src/parser/ast.h - Implementations:
parser_core.cpp— Token navigation, expectation checks, and mismatch reporting.parser_statement.cpp— Top-level statement dispatch.parser_select.cpp—SELECTstatement and flexible clause ordering.parser_ddl.cpp—CREATE TABLE,DROP TABLE,DESCRIBE TABLE,SHOW TABLES.parser_dml.cpp—INSERT,UPDATE,DELETE.parser_compare.cpp—COMPARE,EVOLUTION,HISTORY.parser_rollback.cpp—ROLLBACKandCOMPACT.parser_leaf.cpp— Literals, values, date/timestamp parsing, operators, conditions.
The Abstract Syntax Tree (AST)¶
MemoraDB uses modern C++17 type-safe unions (std::variant) to represent statements:
using Statement = std::variant<
CreateTableStmt,
DropTableStmt,
DescribeTableStmt,
ShowTablesStmt,
InsertStmt,
UpdateStmt,
DeleteStmt,
SelectStmt,
CompareStmt,
EvolutionStmt,
HistoryStmt,
RollbackStmt,
CompactStmt
>;
Key AST Structures (src/parser/ast.h)¶
SelectStmt: ContainsselectAll,columns,tableName,temporalMode, optional date literals (asOfDate,betweenStart,betweenEnd,snapshotDate), optionalwherecondition,orderByColumn,orderDescending, andlimit.Condition: Containscolumn,CompareOp(EQ,NE,LT,LE,GT,GE,SIMILAR_TO), andValue.DateLiteral: Encapsulates year, month, day, hour, minute, second, millisecond, andhasTimeflag.Value: Type-tagged union storingrawtext,intVal,floatVal, andstrVal.
Recursive-Descent Mechanics¶
The parser maintains a cursor size_t current over std::vector<Token>:
peek(offset): Inspects upcoming tokens without consuming them.match(type): Consumes the current token if it matchestype; returnsfalseotherwise.expect(type, context): Asserts that the next token matchestype. If it does not, throws a detailedParseError.
Flexible Clause Parsing in SELECT¶
The parser supports writing <temporal-clause> and WHERE in either order using a lookahead loop:
for (int32_t guard = 0; guard < 2; ++guard) {
if (stmt.temporalMode == TemporalMode::NONE &&
(check(TokenType::AS) || check(TokenType::BETWEEN) || check(TokenType::SNAPSHOT))) {
parseTemporalClause(stmt);
continue;
}
if (!stmt.where && match(TokenType::WHERE)) {
stmt.where = parseCondition();
continue;
}
break;
}
Calendar & Timestamp Validation¶
In parser_leaf.cpp, date literals undergo strict calendar validation:
static bool isLeapYear(int32_t year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
static int32_t daysInMonth(int32_t year, int32_t month) {
static const int32_t lengths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year)) return 29;
return lengths[month - 1];
}
If a user provides 2026-02-29 (a non-leap year), the parser throws:
Error Handling¶
Errors throw a ParseError struct containing the error message, source line, and column:
The REPL catches ParseError and prints a clear diagnostic pointing to the exact character where the failure occurred.