Expand description
varyk-syntax: spans, tokens, lexer, parser, and AST for Varyk.
This crate carries no semantic analysis, so a formatter or a language server can depend on it without pulling in the type checker or borrow analysis.
Structs§
- Block
{ stmts...; tail }. The tail expression, if present, is the block’s value.- Enum
Decl enum Name { variants... }(spec 2.2), optionallypub. An enum has one or more variants; an empty{ }isV0002, since the parser found a complete-looking declaration missing the one thing it must have.- Enum
Variant - A unit variant (
Point) or a tuple variant (Circle(f64));fieldsis empty for a unit variant. Named-field variants (Circle { x: f64 }) are milestone 4 and never produce one of these: they areV0001. - Expr
- An expression node: its kind plus the span it spans in the source.
- Field
Decl - FileId
- Identifies a source file within a compilation. A small newtype over
u32so file ids stay cheap to copy and compare. - FixIt
- A suggested edit attached to a
SyntaxError: replace the text atspanwithreplacement. - Function
fn name(params) -> ReturnType { body }, optionallypub. Inside animplblock the parameter list may start with aselfreceiver (self_mode), which is never aParam(spec 2.5).- Ident
- A name plus the span it was written at.
- Impl
Block impl Name { fns... }(spec 2.5): a block of functions and methods for a struct or enum declared in the same file. Whethernameactually names a struct or enum is the compiler’sresolvemodule’s job.- Match
Arm - One arm of a
match(spec 2.3):pattern => body, with an optional trailing comma the parser consumes but does not record. - ModDecl
mod name;(spec 4.5): declares a submodule resolved toname.vrorname.rsin the same directory. Resolving and merging the submodule’s items is the compiler’sresolvemodule’s job; this node only records the declaration.- Param
- One parameter:
name: Tis a shared borrow,mut name: Tis a mutable borrow (spec 4.2). - Program
- A whole parsed source file: the top-level items it declares, in order.
- Source
File - A single source file: its id, path, full text, and a line index built once so byte offsets convert to line/column cheaply.
- Span
- A byte range
[start, end)within a single source file. - Struct
Decl struct Name { fields... }, optionallypub(which makes every field public; field-levelpubis milestone 2, spec 4.1).- Syntax
Error - A lexical or syntactic error, carrying a span, a message, and one of the diagnostic codes defined above.
- Token
- A single lexical token: its kind plus the span it came from.
- Type
Expr - A named type:
string,i32,User, and so on are all just names, resolved later by the compiler’s resolver. A type may carry an optional module segment (m::User, spec 2.10) and generic arguments (Vec<i32>,Result<User, string>, nested freely, spec 2.6); the parser only records them, and the resolver checks that the module, the name, and the argument count exist. - Variant
Pattern - A variant pattern (spec 2.3):
name, ortype_::name, ormodule::type_::name, each with an optional parenthesized list of sub-patterns. A single remaining segment always fillstype_, nevermodule: unlikeExprKind::Path, a pattern never calls anything, so there is no bare-module case to weigh it against. A pattern path longer than three segments isV0001.
Enums§
- Binary
Op - Binary operators (spec 4.1), in Rust’s precedence order from loosest to
tightest:
||,&&, the comparisons (non-associative),+ -, then* / %. - Expr
Kind - ForHead
- The head of a
forloop (spec 2.4): a half-open integer range (a..b), parsed only here since ranges exist nowhere else in Varyk, or an expression iterated element by element as aVec...=never produces aForHead::Range: it isV0001, milestone 4. - Item
- A top-level declaration (spec 4.1, 2.2, 2.5): a function, a struct, an
enum, an
implblock, or a module declaration.mod name;only declares a submodule; resolving it to the submodule’s own file and items is the compiler’sresolvemodule’s job. - Pattern
- A
matchpattern, one level deep (spec 2.3). Not in milestone 2, eachV0001: nested patterns, literal patterns, guards (pattern if cond), alternatives (a | b), rest patterns (..), range patterns, and@bindings. - Self
Mode - Whether a function is a plain function, or a method with a
selfreceiver (spec 2.5):selfis a shared borrow,mut selfa mutable borrow. There is no ownedselfin milestone 2. - Stmt
- A statement:
let, assignment,return,while,break,continue, or an expression used for its side effects (or as a block’s tail, distinguished byhas_semionStmt::Exprso the type checker can tell a mid-block statement from a block’s value). - SubPattern
- A sub-pattern inside a variant pattern’s parentheses (spec 2.3): a name
or
_. Nothing else is one level deep. - Token
Kind - The kind of a lexical token. Keywords that are part of the Varyk
surface (spec 4.1) each get their own variant; every other Rust keyword
and reserved word, including the 2024-edition ones, lexes as
TokenKind::ReservedKeywordcarrying its text, so the parser can report it by name. - UnaryOp
- Unary operators (spec 4.1). Unary binds tighter than any binary operator.
Constants§
- V0001
- Unsupported construct, naming it.
- V0002
- Unexpected token or malformed syntax.
- V0003
- Bad string escape, or an unterminated string.
- V0010
&xor&mut xwritten at a call site.- V0011
&Tor&mut Twritten in a parameter type.- V0012
- Lifetime syntax, such as
<'a>or&'a T.
Functions§
- lex
- Lexes
fileinto tokens and any syntax errors found along the way. Lexing never stops at the first error: an unknown character or a bad string is recorded and scanning continues after it. - parse
- Parses a whole token stream for a single file into a
Programplus every syntax error found. On the firstV0002(or an unrecoverableV0001, such as a reserved keyword where an item is expected), parsing stops and the items parsed so far are returned alongside the errors. - parse_
source - Lexes
filethen parses it, merging the lexer’s errors first. If the lexer reported anything, parsing is skipped: a file with lexical errors has no reliable token stream to parse from, so itsProgramis empty and only the lexer’s errors are returned. The compiler’sresolvemodule calls this for every file it loads.