Skip to content

Experimental documentation — Work in progress

This page has not completed the documentation audit. Current documentation version: experimental.

Parser

The strict parser consumes the token stream and constructs typed, ownership-safe Sagan syntax trees with source-spanned diagnostics. The separate compiler-owned src/syntax layer wraps it for editor workloads, retaining trivia and exact source while recovering independent top-level declarations after malformed or incomplete input.

Implemented slices

  • Programs and newline-separated declarations and executable root statements
  • Single-name let declarations with optional type annotations and initializers; parallel let declarations with one initializer per name
  • const declarations with mandatory initializers and ASCII SCREAMING_SNAKE_CASE names
  • Identifier, numeric, Boolean, and grouped primary expressions
  • Prefix and postfix operators
  • Calls with zero or more arguments
  • Indexing, member access, safe member access, and chained postfix expressions
  • Ruby-style mutating method names such as normalize!()
  • Ordinary, raw, and multiline string expressions
  • Interpolated strings with complete embedded expressions
  • Arrays and dictionaries, including empty forms and expression keys
  • Cartesian vectors and points with a minimum of two elements
  • Exactly three-component spherical vectors and points using adjacent s<...> and s(...)
  • Trailing commas in calls and every collection form
  • Spread expressions and dictionary spread entries
  • Right-associative exponentiation
  • Multiplicative and additive arithmetic
  • Non-chainable comparisons and equality expressions
  • Logical and and or
  • Conditional expressions
  • Right-associative value-producing := assignment
  • Statement blocks, including empty and nested blocks, plus same-line single-statement control-flow bodies
  • Ordinary and compound (+=, -=, *=, /=, %=, ^=) assignment statements, parallel reassignment, plus expression statements inside blocks
  • if/else statements and else if chains
  • for/in, while, and until loops
  • Unlabeled break and continue, restricted to loop bodies
  • Bare and value-bearing return statements
  • Bare and value-bearing yield statements
  • Braced or same-line single-statement match/case, with an optional final case else
  • hope/unless/finally exception regions and value-bearing scream
  • face signatures and default methods, plus is/has composition lists
  • class fields, methods, leading-dot private methods, and self
  • Newline- or comma-separated enum members, including typed payloads and explicit numeric tags
  • Documentation comments on individual enum members
  • Named block- or expression-bodied functions with optional annotations
  • Anonymous expression-bodied lambdas with optional annotations
  • One optional leading module declaration per source file
  • Top-level import declarations with optional from and as clauses
  • Standalone top-level export declarations with optional aliases
  • Enum cases with zero or more typed payload declarations
  • Declaration-attached /// and /** ... */ documentation comments with retained source spans and focused placement diagnostics
  • Script-style root statements; imported modules are checked separately to remain declaration-only
  • Human-readable AST output and success/error demonstrations
  • DOT and standalone SVG AST rendering
  • A self-contained HTML view that places source input beside the visual tree

Inspecting a tree

The quickest visual demonstration is:

bash scripts/ast_demo.sh

It parses examples/ast.sagan, writes build/ast-demo.html, and opens the page in the default Windows browser. The page embeds its SVG tree and has no Graphviz or network dependency. Its controls provide zoom in, zoom out, fit, and 100% views; the mouse wheel zooms around the pointer and dragging pans the tree. Pass --no-open when only the generated file is wanted.

Text, DOT, SVG, and HTML are also available from bin/sagan through --ast, --ast-dot, --ast-svg, and --ast-html respectively. The renderer currently supports every AST node the parser can produce; new node kinds must be added to the renderer as their parser slices land.

Focused diagnostics cover malformed assignments, postfix and member access, control flow, declarations and types, string interpolation, collections, matching, exceptions, lambdas, modules, documentation-comment placement, and other established grammar rules. bash tests/integration/parser_test.sh runs the broad successful fixture and focused failure fixtures together. Run make editor-tooling-demo to see deliberately incomplete source produce multiple structured diagnostics and a recovered compiler result.

The parser now covers the complete current concrete grammar. Strict compilation still rejects the first malformed construct; editor analysis instead provides bounded recovery without loosening the language grammar.