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:
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:
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:
Whitespace handling: - Leading/trailing whitespace ignored - Whitespace between tokens ignored - Comments (;...) skipped
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:
-
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