Skip to main content

nova_ast/
lib.rs

1//! Abstract Syntax Tree node definitions for the Nova language.
2//!
3//! All nodes carry `Spanned<T>` wrappers so the parser, type checker, and
4//! diagnostics always have precise source locations.
5//!
6//! Major node categories live in sub-modules re-exported here:
7//! - `item` — top-level items (functions, records, traits, …)
8//! - `expr` — expressions
9//! - `pattern` — match patterns
10//! - `ty` — type expressions
11
12pub mod expr;
13pub mod item;
14pub mod pattern;
15pub mod ty;
16
17pub use expr::{BinOp, Expr, FieldInit, Literal, MatchArm, StringPart, UnOp};
18pub use item::{
19    AssignOp, AssocTypeBinding, ConstDecl, ExternBlock, Function, ImplBlock, Import,
20    Module as ModuleDecl, Param, Record, RecordField, TraitDecl, TraitItem, TypeDecl, Visibility,
21    WhereBound,
22};
23pub use pattern::Pattern;
24pub use ty::{Type, TypeParam};
25
26use nova_diagnostics::Spanned;
27
28/// The top-level compilation unit — one `.nova` source file.
29#[derive(Debug, Clone)]
30pub struct File {
31    pub items: Vec<Spanned<Item>>,
32}
33
34/// Every kind of top-level item that can appear in a Nova file.
35#[derive(Debug, Clone)]
36pub enum Item {
37    Function(Function),
38    Record(Record),
39    Type(TypeDecl),
40    Trait(TraitDecl),
41    Impl(ImplBlock),
42    Const(ConstDecl),
43    Import(Import),
44    Module(ModuleDecl),
45    Extern(ExternBlock),
46}
47
48/// A statement — may appear inside a block.
49#[derive(Debug, Clone)]
50pub enum Stmt {
51    Let {
52        is_mut: bool,
53        pattern: Spanned<Pattern>,
54        ty: Option<Spanned<Type>>,
55        init: Option<Spanned<Expr>>,
56    },
57    Expr(Spanned<Expr>),
58    Item(Box<Item>),
59}
60
61/// A `{ ... }` block — a sequence of statements with an optional trailing expression.
62#[derive(Debug, Clone)]
63pub struct Block {
64    pub stmts: Vec<Spanned<Stmt>>,
65    /// The final expression whose value is the block's value, if present.
66    pub trailing: Option<Box<Spanned<Expr>>>,
67}
68
69/// A `path::to::item` reference.
70#[derive(Debug, Clone)]
71pub struct Path {
72    pub segments: Vec<Spanned<String>>,
73}
74
75impl Path {
76    pub fn single(name: Spanned<String>) -> Self {
77        Self {
78            segments: vec![name],
79        }
80    }
81}