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 variant of an enum: a unit variant (
Point), a tuple variant (Circle(f64)), or a variant with named fields (Click { x: i32 }, M4 spec 2.5). - Expr
- An expression node: its kind plus the span it spans in the source.
- Field
Decl - A struct field:
name: T, optionallypub(spec 3.4). Visibility follows the same rule as everything else (section 3.2): a private field is visible in the declaring module and its descendants, which includes the struct’s own methods. - 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). - Path
- A
::-separated path (spec 3.1, 3.3): an optional keyword prefix (crate,self,super) followed by name segments. What the segments (and any name that follows them, for a type or expression path) resolve to – a chain of modules, a type, a function, a variant – is decided by where the path appears and, ultimately, the compiler’s resolver, never the parser. - 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(spec 4.1). Whether a field is visible follows the same rule as everything else (spec 3.2): seeFieldDecl::is_pub.- 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 a module path (m::User,shop::cart::Cart, spec 2.10, 3.1) and generic arguments (Vec<i32>,Result<User, string>, nested freely, spec 2.6); the parser only records them, and the resolver checks that the path, the name, and the argument count exist. - UseDecl
use path;oruse path as name;(spec 3.3): introduces a local alias for whateverpathnames – a module, a struct, an enum, or a function – resolved and checked by the compiler’s resolver, never the parser.- Variant
Field - A named field of a variant:
name: T. It has no visibility of its own: a variant’s fields are as visible as the enum, and Rust rejectspubon them.
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): an integer range, half-open (a..b) orinclusive(a..=b, M4 spec 2.11), parsed only here since an expression range exists nowhere else in Varyk, or an expression iterated element by element as aVec. - 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. - Literal
- The value of a literal pattern or of a range pattern’s end, with the raw text of a number as lexed.
- Path
Start - Where a path starts (spec 3.1, 3.3): one of the three keyword prefixes,
or nothing (a plain module name declared in this file, or no module
segment at all). The parser records whichever prefix is written;
resolving
CrateandSuperto an actual module, and rejectingSuperin the crate root (V0111), is the compiler’s resolver’s job. - Pattern
- A pattern (spec 2.3, M4 spec 2.5), in a
matcharm, anif let, or awhile let, nested to any depth. Not in Varyk, eachV0001from the parser: guards (pattern if cond), alternatives (a | b), rest 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). - 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.
- Variant
Fields - What a variant holds: nothing, values by position (
Circle(f64); an emptyCircle()is aTupleof no types), or named fields.
Constants§
- KEYWORDS
- Varyk’s own keywords (spec 4.1), with the token each lexes to;
trueandfalseare the two bool literals.keyword_kindlooks words up here, so this list and the lexer cannot differ. - RESERVED_
KEYWORDS - Every other Rust keyword and reserved word, including the 2024-edition ones.
- 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.