Skip to content

Lexer Architecture

The lexical analyzer (lexer) converts raw SQL character streams into a sequence of discrete Token structures for consumption by the parser.


Source Location

  • Headers: src/lexer/token.h, src/lexer/lexer.h
  • Implementation: src/lexer/lexer.cpp

Token Structure

Every token generated by the lexer captures its lexical category, literal value, and source location:

struct Token {
    TokenType type;
    std::string value;
    int32_t line;
    int32_t column;
};

Supported Token Types (TokenType)

  • SQL Keywords: CREATE, DROP, DESCRIBE, TABLE, SHOW, TABLES, INSERT, INTO, VALUES, UPDATE, SET, DELETE, SELECT, FROM, WHERE, ORDER, BY, LIMIT, ASC, DESC, PRIMARY, KEY, SEMANTIC.
  • Temporal Keywords: AS, OF, BETWEEN, SNAPSHOT, COMPARE, EVOLUTION, HISTORY, COMPACT, ROLLBACK, TO.
  • Semantic Keyword: SIMILAR.
  • Type Identifiers: INT, FLOAT, STRING, BOOL.
  • Literals: IDENTIFIER, INTEGER_LITERAL, FLOAT_LITERAL, STRING_LITERAL.
  • Comparison Operators: EQUAL (=), NOT_EQUAL (!=), LESS (<), LESS_EQUAL (<=), GREATER (>), GREATER_EQUAL (>=).
  • Delimiters & Symbols: STAR (*), LPAREN ((), RPAREN ()), COMMA (,), SEMICOLON (;), COLON (:), MINUS (-).
  • Control: END_OF_FILE, UNKNOWN.

Lexing Mechanics

1. Keyword Recognition

Keywords are recognized case-insensitively. The lexer buffers identifier characters ([a-zA-Z_][a-zA-Z0-9_]*) and converts them to uppercase to perform keyword lookup. If the string matches a reserved word, the corresponding TokenType is returned; otherwise, it is classified as TokenType::IDENTIFIER.

2. Numeric Scanning & Unary Minus

  • Numbers are scanned by reading sequences of digits (0-9).
  • If a decimal point (.) is encountered followed by digits, it is categorized as FLOAT_LITERAL.
  • Unary minus (-) is handled as a separate MINUS token at the lexical level; the parser's parseValue() inspects whether a MINUS precedes a numeric literal and prepends the negative sign.

3. String Literal Scanning

Strings can be bounded by either single quotes (') or double quotes ("): * Supports backslash escaping (\", \', \\). * Multi-line strings are supported, with the lexer updating its internal line and column counters accordingly.

4. No SQL Comments

The lexer does not define comment tokens (such as -- or /* */). Any -- is lexed as two consecutive MINUS tokens, which causes a syntax error in the parser.