Skip to main content

microcad_lang_parse/ast/
def.rs

1// Copyright © 2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Definition nodes in the AST that will eventually be resolved into Symbols.
5//!
6//! Each definition must have syntax elements in that order:
7//! * A span
8//! * Optional Item extras like comments
9//! * Optional Doc block
10//! * Attributes
11//! * An optional visibility
12//! * A keyword (with span)
13//! * An `id` (except Use definitions)
14
15use crate::ast::{
16    Attribute, Body, DocBlock, Expression, Identifier, ItemExtras, ParameterList, Span, Type,
17};
18
19use microcad_lang_base::Spanned;
20pub use microcad_lang_base::WorkbenchKind;
21
22/// An optional visibility modifier
23///
24/// it can be part of constant, module, function or workbench definitions.
25#[derive(Debug, PartialEq, Clone)]
26pub enum Visibility {
27    /// `pub`
28    Public,
29}
30
31#[derive(Debug, PartialEq)]
32#[allow(missing_docs)]
33pub struct Workbench {
34    pub span: Span,
35    pub keyword_span: Span,
36    pub extras: ItemExtras,
37    pub doc: DocBlock,
38    pub kind: WorkbenchKind,
39    pub attr: Vec<Attribute>,
40    pub vis: Option<Spanned<Visibility>>,
41    pub id: Identifier,
42    pub parameters: ParameterList,
43    pub body: Body,
44}
45
46/// A definition of a module
47#[derive(Debug, PartialEq)]
48#[allow(missing_docs)]
49pub struct InlineModule {
50    pub span: Span,
51    pub keyword_span: Span,
52    pub extras: ItemExtras,
53    pub doc: DocBlock,
54    pub attr: Vec<Attribute>,
55    pub vis: Option<Spanned<Visibility>>,
56    pub id: Identifier,
57    pub body: Body,
58}
59
60/// A definition of a module
61#[derive(Debug, PartialEq)]
62#[allow(missing_docs)]
63pub struct FileModule {
64    pub span: Span,
65    pub keyword_span: Span,
66    pub extras: ItemExtras,
67    pub doc: DocBlock,
68    pub attr: Vec<Attribute>,
69    pub vis: Option<Spanned<Visibility>>,
70    pub id: Identifier,
71}
72
73/// A definition of a function
74#[derive(Debug, PartialEq)]
75#[allow(missing_docs)]
76pub struct Function {
77    pub span: Span,
78    pub keyword_span: Span,
79    pub extras: ItemExtras,
80    pub doc: DocBlock,
81    pub attr: Vec<Attribute>,
82    pub vis: Option<Spanned<Visibility>>,
83    pub id: Identifier,
84    pub parameters: ParameterList,
85    pub return_type: Option<Type>,
86    pub body: Body,
87}
88
89/// A use definition will become an alias or a wildcard.
90#[derive(Debug, PartialEq)]
91#[allow(missing_docs)]
92pub struct Use {
93    pub span: Span,
94    pub attr: Vec<Attribute>,
95    pub keyword_span: Span,
96    pub extras: ItemExtras,
97    pub vis: Option<Spanned<Visibility>>,
98    pub name: UseName,
99    pub use_as: Option<Identifier>,
100}
101
102/// The name of the item being imported
103#[derive(Debug, PartialEq)]
104#[allow(missing_docs)]
105pub struct UseName {
106    pub span: Span,
107    pub extras: ItemExtras,
108    pub parts: Vec<UseStatementPart>,
109}
110
111/// The parts a [`UseName`] consists of, separated by `::`
112#[derive(Debug, PartialEq)]
113#[allow(missing_docs)]
114pub enum UseStatementPart {
115    Identifier(Identifier),
116    Glob(Span),
117    Error(Span),
118}
119
120/// A const assignment: `const A = 42` / `pub A = 32`
121#[derive(Debug, PartialEq)]
122#[allow(missing_docs)]
123pub struct Constant {
124    pub span: Span,
125    pub keyword_span: Span,
126    pub extras: ItemExtras,
127    pub doc: DocBlock,
128    pub attr: Vec<Attribute>,
129    pub vis: Option<Spanned<Visibility>>,
130    pub id: Identifier,
131    pub ty: Option<Type>,
132    pub expr: Box<Expression>,
133}