Skip to main content

Module parser

Module parser 

Source
Available on crate feature parser only.
Expand description

Wright’s parser, which turns source code into an abstract syntax tree.

Parsing happens in three stages:

  1. The lexer splits the source into tokens (see lexer.rs in this module).
  2. A resilient LL(1) parser generated by lelwel from the grammar in src/parser/wright.llw builds a lossless concrete syntax tree from the tokens.
  3. The concrete syntax tree is lowered into the AST (see lower.rs).

Parsing never fails outright. Syntax errors are reported as Diagnostics, and the parser recovers and keeps going, so a single parse reports as many problems as possible and still produces an AST for the parts of the source that make sense. See crate::ast for how errors show up in the AST.

use wright::parser::parse_source_file;

let source = Arc::new(Source::new_from_static_str(FileName::None, "func main() { println(\"hi\"); }"));
let parsed = parse_source_file(source);

assert!(!parsed.has_errors());
assert_eq!(parsed.ast.items.len(), 1);

Modules§

cst
The concrete syntax tree (CST) and the parser that produces it.

Structs§

Parsed
The result of parsing some source code: an AST, the syntax tree it was lowered from, and any diagnostics.

Constants§

MAX_NESTING_DEPTH
The maximum nesting depth the parser supports.

Functions§

parse_expr
Parse a source containing a single expression (such as input to a REPL).
parse_source_file
Parse an entire source file.
parse_type
Parse a source containing a single type signature.