Lexer & Parser Architecture
The Lexer and Parser form the front-end of the AXON compiler, transforming raw source text into a structured Abstract Syntax Tree (AST).Lexer (axon/compiler/lexer.py)
Overview
The Lexer is a hand-written, single-pass scanner that converts AXON source code into a sequence of tokens. Key Features:- Keyword vs identifier discrimination via lookup table
- String literals with escape sequences (
\n,\t,\") - Numeric literals: integers, floats, durations (
10s,5m,2h) - Multi-character operators:
->(arrow),..(range) - Comment stripping:
//(line) and/* */(block) - Line/column tracking for precise error reporting
Implementation
Token Types
Keywords (reserved identifiers):{,},(,),[,]— Structural delimiters:,,,.,?— Punctuation->— Arrow (return type, action)..— Range operator==,!=,<,>,<=,>=— Comparisons
STRING:"hello world"with escape supportINTEGER:42,-7FLOAT:3.14,-0.5DURATION:10s,5m,2h,1dBOOL:true,false
Character-Level Scanning
Duration Literal Handling
s (seconds), ms (milliseconds), m (minutes), h (hours), d (days)
Parser (axon/compiler/parser.py)
Overview
The Parser uses recursive descent to transform the token stream into a cognitive Abstract Syntax Tree. Design Principle: Zero mechanical nodes. The AST contains only cognitive concepts:PersonaDefinition(notClassDecl)IntentNode(notFunctionCall)ReasonChain(notForLoop)AnchorConstraint(notAssertStatement)ProbeDirective(notSelectQuery)WeaveNode(notJoinExpression)
Implementation
Grammar Structure
The parser follows the AXON grammar hierarchy:Top-Level Declaration Dispatch
Parsing Example: Flow Definition
Parsing Cognitive Steps
AST Node Hierarchy
Base Node
Declaration Nodes
PersonaDefinition — Cognitive identity:Cognitive Step Nodes
ReasonChain — Explicit reasoning:Error Handling
Lexer Errors
Parser Errors
Example: Parsing a Persona
Input Source:Next Steps
Type Checker
Learn how epistemic types are validated
AST to IR
See how the AST is lowered to model-agnostic IR
