Skip to content

Parser

class Parser

@defgroup parser Parser @brief S-expression parsing and tokenization

The Parser converts WAL source text into an Abstract Syntax Tree (AST). It handles: - Tokenization (whitespace, comments, delimiters) - Recursive descent parsing of S-expressions - Support for quoted forms - Number and string literal parsing

Grammar:

program    ::= expression*
expression ::= atom | list | quote
atom       ::= symbol | number | string | keyword
list       ::= '(' expression* ')'
quote      ::= '\'' expression        → (quote expression)
quasiquote ::= '`' expression         → (quasiquote expression)
unquote    ::= ',' expression         → (unquote expression)

symbol     ::= [a-zA-Z_][a-zA-Z0-9_-]*
number     ::= integer | float
string     ::= '"' ... '"'
keyword    ::= ':' symbol

comment    ::= ';' .*

Parsing Example:

@mermaid graph TD A["Input: (+ 1 2)"] --> B["Tokenize"] B --> C["Tokens: '(' '+' '1' '2' ')'"] C --> D["Parse"] D --> E["WList with:
- Symbol +
- Number 1
- Number 2"] @endmermaid

See
"AST Types" for value representation
See
"Evaluator" for how AST is evaluated

@{ Recursive descent parser for WAL S-expressions

Converts WAL source code strings into Value AST nodes. Single-pass parsing with minimal lookahead.

Usage:

1
2
3
4
5
6
Parser parser;
Value expr = parser.parse("(+ 1 2)");
std::vector<Value> program = parser.parse_program(
    "(define x 10) "
    "(print x)"
);

State: - Maintains input string and position - Resets on each parse() call - Suitable for parsing many independent expressions

Functions

Name Description
skip_whitespace @name Tokenization Helpers @{ Skip whitespace and update position
skip_comment Skip comment until end of line
peek Peek at current character without advancing
next Consume and return current character, advance position
read_until_delimiter Read characters until delimiter (space, paren, etc.)
parse_atom @} @name Parsing Helpers @{ Parse an atom (number, symbol, string, etc.)
parse_list Parse a list (parenthesized expressions)
parse_quoted Parse quoted expression (handles ', `, ,)
parse_unquote ,expr and ,@expr
parse_subscripts postfix expr[i]
parse_number Parse number literal (integer or float)
parse @} Create a new parser Parser is stateless - create once and reuse.
parse_program Parse multiple expressions from source Parses all top-level expressions in input and returns them.
parse_one Parse single expression at current position Advanced: parse one expression from current parser state. Used internally and for incremental parsing. Return : The parsed expression @throws ParseError if at end of input

Function Details

next

char next()

Consume and return current character, advance position

parse

Value parse(const std::string& source)

@} Create a new parser

Parser is stateless - create once and reuse. @name Parsing Interface @{ Parse a complete WAL expression from string

Parses a single top-level expression. If input contains multiple expressions, only the first is parsed. To parse all, use parse_program().

Examples:

1
2
3
4
5
parser.parse("42")                    // Number(42)
parser.parse("x")                     // Symbol("x")
parser.parse("(+ 1 2)")              // WList{Symbol("+"), 1, 2}
parser.parse("'x")                    // WList{Symbol("quote"), Symbol("x")}
parser.parse("(lambda (x) (* x x))") // Lambda with quoted body

Whitespace handling: - Leading/trailing whitespace ignored - Whitespace between tokens ignored - Comments (;...) skipped

source
The source code to parse
Return
The parsed expression as a Value @throws ParseError if syntax is invalid: - Unmatched parentheses - Invalid number format - Unexpected end of input - Invalid string escapes

parse_atom

Value parse_atom()

@} @name Parsing Helpers @{ Parse an atom (number, symbol, string, etc.)

parse_list

Value parse_list()

Parse a list (parenthesized expressions)

parse_number

Value parse_number(const std::string& token)

Parse number literal (integer or float)

parse_one

Value parse_one()

Parse single expression at current position

Advanced: parse one expression from current parser state. Used internally and for incremental parsing.

Return
The parsed expression @throws ParseError if at end of input

parse_program

std::vector<Value> parse_program(const std::string& source)

Parse multiple expressions from source

Parses all top-level expressions in input and returns them. Useful for parsing WAL programs with multiple forms.

Example:

1
2
3
4
5
6
auto program = parser.parse_program(
    "(define x 10) "
    "(define y 20) "
    "(+ x y)"
);
// program.size() == 3

source
The source code
Return
Vector of parsed expressions in order @throws ParseError if any expression syntax is invalid

parse_quoted

Value parse_quoted(const std::string& quote_type)

Parse quoted expression (handles ', `, ,)

parse_subscripts

Value parse_subscripts(Value target)

postfix expr[i]

parse_unquote

Value parse_unquote()

,expr and ,@expr

peek

char peek() const

Peek at current character without advancing

read_until_delimiter

std::string read_until_delimiter()

Read characters until delimiter (space, paren, etc.)

skip_comment

void skip_comment()

Skip comment until end of line

skip_whitespace

void skip_whitespace()

@name Tokenization Helpers @{ Skip whitespace and update position