1pub mod prelude;
6
7use std::fmt;
8use std::fmt::{Debug, Formatter};
9use std::hash::Hash;
10
11#[derive(PartialEq, Eq, Hash, Default, Clone)]
12pub struct SpanWithoutFileId {
13 pub offset: u32,
14 pub length: u16,
15}
16
17impl Debug for SpanWithoutFileId {
18 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19 write!(f, "<{}:{}>", self.offset, self.length)
20 }
21}
22
23#[derive(PartialEq, Eq, Hash, Default, Clone)]
25pub struct Node {
26 pub span: SpanWithoutFileId,
27 }
29
30impl Debug for Node {
31 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
32 write!(f, "{:?}", self.span)
33 }
34}
35
36#[derive(Debug, PartialEq, Eq, Clone, Hash)]
38pub struct QualifiedTypeIdentifier {
39 pub name: LocalTypeIdentifier,
40 pub module_path: Option<ModulePath>,
41 pub generic_params: Vec<Type>,
42}
43
44impl QualifiedTypeIdentifier {
45 #[must_use]
46 pub fn new(name: LocalTypeIdentifier, module_path: Vec<Node>) -> Self {
47 let module_path = if module_path.is_empty() {
48 None
49 } else {
50 Some(ModulePath(module_path))
51 };
52
53 Self {
54 name,
55 module_path,
56 generic_params: Vec::new(),
57 }
58 }
59
60 #[must_use]
61 pub fn new_with_generics(
62 name: LocalTypeIdentifier,
63 module_path: Vec<Node>,
64 generic_params: Vec<Type>,
65 ) -> Self {
66 let module_path = if module_path.is_empty() {
67 None
68 } else {
69 Some(ModulePath(module_path))
70 };
71
72 Self {
73 name,
74 module_path,
75 generic_params,
76 }
77 }
78}
79
80#[derive(Debug, PartialEq, Eq, Hash, Clone)]
81pub struct QualifiedIdentifier {
82 pub name: Node,
83 pub module_path: Option<ModulePath>,
84 pub generic_params: Vec<Type>,
85}
86
87impl QualifiedIdentifier {
88 #[must_use]
89 pub fn new(name: Node, module_path: Vec<Node>) -> Self {
90 let module_path = if module_path.is_empty() {
91 None
92 } else {
93 Some(ModulePath(module_path))
94 };
95
96 Self {
97 name,
98 module_path,
99 generic_params: vec![],
100 }
101 }
102
103 #[must_use]
104 pub fn new_with_generics(
105 name: Node,
106 module_path: Vec<Node>,
107 generic_params: Vec<Type>,
108 ) -> Self {
109 let module_path = if module_path.is_empty() {
110 None
111 } else {
112 Some(ModulePath(module_path))
113 };
114
115 Self {
116 name,
117 module_path,
118 generic_params,
119 }
120 }
121}
122
123#[derive(Debug, PartialEq, Eq, Hash, Default, Clone)]
124pub struct LocalTypeIdentifier(pub Node);
125
126impl LocalTypeIdentifier {
127 #[must_use]
128 pub const fn new(node: Node) -> Self {
129 Self(node)
130 }
131}
132
133#[derive(Debug, PartialEq, Eq, Hash, Default, Clone)]
134pub struct TypeVariable(pub Node);
135
136#[derive(Debug, PartialEq, Eq, Hash, Default, Clone)]
137pub struct LocalTypeIdentifierWithOptionalTypeVariables {
138 pub name: Node,
139 pub type_variables: Vec<TypeVariable>,
140}
141
142#[derive(PartialEq, Eq, Hash, Debug, Clone)]
143pub struct LocalIdentifier(pub Node);
144
145impl LocalIdentifier {
146 #[must_use]
147 pub const fn new(node: Node) -> Self {
148 Self(node)
149 }
150}
151
152#[derive(Debug, PartialEq, Eq, Hash, Default, Clone)]
153pub struct LocalConstantIdentifier(pub Node);
154
155#[derive(Debug, PartialEq, Eq, Clone, Hash)]
156pub struct QualifiedConstantIdentifier {
157 pub name: Node,
158 pub module_path: Option<ModulePath>,
159}
160
161impl QualifiedConstantIdentifier {
162 #[must_use]
163 pub const fn new(name: Node, module_path: Option<ModulePath>) -> Self {
164 Self { name, module_path }
165 }
166}
167
168#[derive(Debug, Eq, Hash, Clone, PartialEq)]
169pub struct FieldName(pub Node);
170
171#[derive(Debug, Eq, Hash, PartialEq, Clone)]
172pub struct ModulePath(pub Vec<Node>);
173
174impl Default for ModulePath {
175 fn default() -> Self {
176 Self::new()
177 }
178}
179
180impl ModulePath {
181 #[must_use]
182 pub const fn new() -> Self {
183 Self(vec![])
184 }
185}
186
187#[derive(Debug, Clone)]
188pub enum ImportItem {
189 Identifier(LocalIdentifier),
190 Type(LocalTypeIdentifier),
191}
192
193#[derive(Debug, Clone)]
194pub enum ImportItems {
195 Nothing,
196 Items(Vec<ImportItem>),
197 All,
198}
199
200#[derive(Debug, Clone)]
201pub struct Mod {
202 pub module_path: ModulePath,
203 pub items: ImportItems,
204}
205
206#[derive(Debug, Clone)]
207pub struct Use {
208 pub module_path: ModulePath,
209 pub items: ImportItems,
210}
211
212#[derive(Debug, Eq, Clone, PartialEq)]
213pub struct AliasType {
214 pub identifier: LocalTypeIdentifier,
215 pub referenced_type: Type,
216}
217
218#[derive(Debug, Eq, PartialEq, Hash, Clone, Default)]
219pub struct AnonymousStructType {
220 pub fields: Vec<StructTypeField>,
221}
222
223impl AnonymousStructType {
224 #[must_use]
225 pub const fn new(fields: Vec<StructTypeField>) -> Self {
226 Self { fields }
227 }
228}
229
230#[derive(Debug, Clone)]
231pub struct ConstantInfo {
232 pub constant_identifier: LocalConstantIdentifier,
233 pub expression: Box<Expression>,
234}
235
236#[derive(Debug, Clone)]
237pub struct NamedStructDef {
238 pub identifier: LocalTypeIdentifierWithOptionalTypeVariables,
239 pub struct_type: AnonymousStructType,
240}
241
242#[derive(Debug, Clone)]
243pub enum Definition {
244 AliasDef(AliasType),
245 NamedStructDef(NamedStructDef),
246 EnumDef(
247 LocalTypeIdentifierWithOptionalTypeVariables,
248 Vec<EnumVariantType>,
249 ),
250 FunctionDef(Function),
251 ImplDef(LocalTypeIdentifierWithOptionalTypeVariables, Vec<Function>),
252 Mod(Mod),
253 Use(Use),
254 Constant(ConstantInfo),
256}
257
258#[derive(Debug, Clone)]
259pub struct ForVar {
260 pub identifier: Node,
261 pub is_mut: Option<Node>,
262}
263
264#[derive(Debug, Clone)]
265pub enum ForPattern {
266 Single(ForVar),
267 Pair(ForVar, ForVar),
268}
269
270impl ForPattern {
271 #[must_use]
272 pub fn any_mut(&self) -> Option<Node> {
273 match self {
274 Self::Single(a) => a.is_mut.clone(),
275 Self::Pair(a, b) => a.is_mut.clone().or_else(|| b.is_mut.clone()),
276 }
277 }
278}
279
280#[derive(Debug, Clone)]
281pub struct IterableExpression {
282 pub expression: Box<MutableOrImmutableExpression>,
283}
284
285#[derive(Clone, Eq, PartialEq)]
286pub struct Variable {
287 pub name: Node,
288 pub is_mutable: Option<Node>,
289}
290
291#[derive(Debug, Clone)]
292pub struct VariableBinding {
293 pub variable: Variable,
294 pub expression: MutableOrImmutableExpression,
295}
296
297#[derive(Debug, Clone)]
298pub struct WhenBinding {
299 pub variable: Variable,
300 pub expression: Option<MutableOrImmutableExpression>,
301}
302
303impl Variable {
304 #[must_use]
305 pub const fn new(name: Node, is_mutable: Option<Node>) -> Self {
306 Self { name, is_mutable }
307 }
308}
309
310impl Debug for Variable {
313 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
314 if let Some(found) = &self.is_mutable {
315 write!(f, "mut {found:?} {:?}", self.name)
316 } else {
317 write!(f, "{:?}", self.name)
318 }
319 }
320}
321
322#[derive(Debug, Eq, Clone, PartialEq)]
323pub struct Parameter {
324 pub variable: Variable,
325 pub param_type: Type,
326}
327
328#[derive(Debug, Clone)]
329pub struct FunctionDeclaration {
330 pub name: Node,
331 pub params: Vec<Parameter>,
332 pub self_parameter: Option<SelfParameter>,
333 pub return_type: Option<Type>,
334}
335
336#[derive(Debug, Clone)]
337pub struct FunctionWithBody {
338 pub declaration: FunctionDeclaration,
339 pub body: Expression,
340}
341
342#[derive(Debug, Clone)]
343pub enum Function {
344 Internal(FunctionWithBody),
345 External(FunctionDeclaration),
346}
347
348#[derive(Debug, Clone)]
349pub struct SelfParameter {
350 pub is_mutable: Option<Node>,
351 pub self_node: Node,
352}
353
354#[derive(Debug, PartialEq, Eq)]
355pub enum AssignmentOperatorKind {
356 Compound(CompoundOperatorKind),
357 Assign, }
359
360#[derive(Debug, PartialEq, Eq, Clone)]
361pub enum CompoundOperatorKind {
362 Add, Sub, Mul, Div, Modulo, }
368
369#[derive(Debug, Clone)]
370pub struct CompoundOperator {
371 pub node: Node,
372 pub kind: CompoundOperatorKind,
373}
374
375#[derive(Debug, Clone)]
376pub enum RangeMode {
377 Inclusive,
378 Exclusive,
379}
380
381#[derive(Debug, Clone)]
382pub struct MutableOrImmutableExpression {
383 pub is_mutable: Option<Node>,
384 pub expression: Expression,
385}
386
387#[derive(Clone)]
388pub struct Expression {
389 pub kind: ExpressionKind,
390 pub node: Node,
391}
392
393impl Debug for Expression {
394 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
395 write!(f, "{:?}{:?}", self.node.span, self.kind)
396 }
397}
398
399#[derive(Debug, Clone)]
400pub enum Postfix {
401 FieldAccess(Node),
402 Subscript(Expression),
403 MemberCall(Node, Vec<MutableOrImmutableExpression>),
404 FunctionCall(Node, Vec<MutableOrImmutableExpression>),
405 OptionUnwrap(Node), NoneCoalesce(Expression), }
408
409#[derive(Debug, Clone)]
410pub struct PostfixChain {
411 pub base: Box<Expression>,
412 pub postfixes: Vec<Postfix>,
413}
414
415#[derive(Debug, Clone)]
417pub enum ExpressionKind {
418 PostfixChain(PostfixChain),
420
421 VariableReference(Variable),
423 ConstantReference(QualifiedConstantIdentifier),
424 StaticMemberFunctionReference(QualifiedTypeIdentifier, Node),
425 IdentifierReference(QualifiedIdentifier),
426
427 VariableDefinition(Variable, Option<Type>, Box<MutableOrImmutableExpression>),
429 VariableAssignment(Variable, Box<MutableOrImmutableExpression>),
430 Assignment(Box<Expression>, Box<Expression>),
431 CompoundAssignment(Box<Expression>, CompoundOperator, Box<Expression>),
432 DestructuringAssignment(Vec<Variable>, Box<Expression>),
433
434 BinaryOp(Box<Expression>, BinaryOperator, Box<Expression>),
436 UnaryOp(UnaryOperator, Box<Expression>),
437
438 Block(Vec<Expression>),
440 With(Vec<VariableBinding>, Box<Expression>),
441 When(Vec<WhenBinding>, Box<Expression>, Option<Box<Expression>>),
442
443 ForLoop(ForPattern, IterableExpression, Box<Expression>),
445 WhileLoop(Box<Expression>, Box<Expression>),
446 Return(Option<Box<Expression>>),
447 Break,
448 Continue,
449
450 If(Box<Expression>, Box<Expression>, Option<Box<Expression>>),
452 Match(Box<MutableOrImmutableExpression>, Vec<MatchArm>),
453 Guard(Vec<GuardExpr>),
454
455 InterpolatedString(Vec<StringPart>),
456
457 AnonymousStructLiteral(Vec<FieldExpression>, bool),
459 NamedStructLiteral(QualifiedTypeIdentifier, Vec<FieldExpression>, bool),
460 Range(Box<Expression>, Box<Expression>, RangeMode),
461 Literal(LiteralKind),
462}
463
464#[derive(Debug, Clone)]
465pub struct MatchArm {
466 pub pattern: Pattern,
467 pub expression: Expression,
468}
469
470#[derive(Debug, Clone)]
472pub enum LiteralKind {
473 Int,
474 Float,
475 String(String),
476 Bool,
477 EnumVariant(EnumVariantLiteral),
478 Tuple(Vec<Expression>),
479 Slice(Vec<Expression>),
480 SlicePair(Vec<(Expression, Expression)>),
481 None,
482}
483
484#[derive(Debug, Clone)]
485pub struct FieldExpression {
486 pub field_name: FieldName,
487 pub expression: Expression,
488}
489
490#[derive(Debug, Eq, Hash, Clone, PartialEq)]
491pub struct StructTypeField {
492 pub field_name: FieldName,
493 pub field_type: Type,
494}
495
496#[derive(Debug, Clone)]
497pub enum EnumVariantLiteral {
498 Simple(QualifiedTypeIdentifier, LocalTypeIdentifier),
499 Tuple(
500 QualifiedTypeIdentifier,
501 LocalTypeIdentifier,
502 Vec<Expression>,
503 ),
504 Struct(
505 QualifiedTypeIdentifier,
506 LocalTypeIdentifier,
507 Vec<FieldExpression>,
508 bool,
509 ),
510}
511
512#[derive(Debug, Clone)]
513pub enum EnumVariantType {
514 Simple(Node),
515 Tuple(Node, Vec<Type>),
516 Struct(Node, AnonymousStructType),
517}
518
519#[derive(Debug, PartialEq, Eq, Clone, Hash)]
520pub struct TypeForParameter {
521 pub ast_type: Type,
522 pub is_mutable: bool,
523}
524
525#[derive(Debug, PartialEq, Eq, Clone, Hash)]
526pub enum Type {
527 Slice(Box<Type>), SlicePair(Box<Type>, Box<Type>), AnonymousStruct(AnonymousStructType),
531 Tuple(Vec<Type>),
532 Function(Vec<TypeForParameter>, Box<Type>),
533
534 Named(QualifiedTypeIdentifier),
535
536 Optional(Box<Type>, Node),
537}
538
539#[derive(Debug, Clone)]
540pub struct BinaryOperator {
541 pub kind: BinaryOperatorKind,
542 pub node: Node,
543}
544
545#[derive(Debug, Clone)]
547pub enum BinaryOperatorKind {
548 Add,
549 Subtract,
550 Multiply,
551 Divide,
552 Modulo,
553 LogicalOr,
554 LogicalAnd,
555 Equal,
556 NotEqual,
557 LessThan,
558 LessEqual,
559 GreaterThan,
560 GreaterEqual,
561 RangeExclusive,
562}
563
564#[derive(Debug, Clone)]
566pub enum UnaryOperator {
567 Not(Node),
568 Negate(Node),
569}
570
571#[derive(Debug, Clone)]
572pub struct GuardExpr {
573 pub clause: GuardClause,
574 pub result: Expression,
575}
576
577#[derive(Debug, Clone)]
578pub enum GuardClause {
579 Wildcard(Node),
580 Expression(Expression),
581}
582
583#[derive(Debug, Clone)]
585pub enum Pattern {
586 Wildcard(Node),
587 NormalPattern(Node, NormalPattern, Option<GuardClause>),
588}
589
590#[derive(Debug, Clone)]
592pub enum NormalPattern {
593 PatternList(Vec<PatternElement>),
594 EnumPattern(Node, Option<Vec<PatternElement>>),
595 Literal(LiteralKind),
596}
597
598#[derive(Debug, Clone)]
599pub enum PatternElement {
600 Variable(Variable),
601 Expression(Expression),
602 Wildcard(Node),
603}
604
605#[derive(Debug, Clone)]
606pub enum StringPart {
607 Literal(Node, String),
608 Interpolation(Box<Expression>, Option<FormatSpecifier>),
609}
610
611#[derive(Debug, Clone)]
612pub enum FormatSpecifier {
613 LowerHex(Node), UpperHex(Node), Binary(Node), Float(Node), Precision(u32, Node, PrecisionType), }
619
620#[derive(Debug, Clone)]
621pub enum PrecisionType {
622 Float(Node),
623 String(Node),
624}
625
626#[derive()]
627pub struct Module {
628 pub expression: Option<Expression>,
629 pub definitions: Vec<Definition>,
630}
631
632impl Debug for Module {
633 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
634 for definition in &self.definitions {
635 writeln!(f, "{definition:?}")?;
636 }
637
638 if !self.definitions.is_empty() && self.expression.is_some() {
639 writeln!(f, "---")?;
640 }
641
642 if let Some(found_expression) = &self.expression {
643 match &found_expression.kind {
644 ExpressionKind::Block(expressions) => {
645 for expression in expressions {
646 writeln!(f, "{expression:?}")?;
647 }
648 }
649 _ => writeln!(f, "{found_expression:?}")?,
650 }
651 }
652
653 Ok(())
654 }
655}
656
657impl Module {
658 #[must_use]
659 pub const fn new(definitions: Vec<Definition>, expression: Option<Expression>) -> Self {
660 Self {
661 expression,
662 definitions,
663 }
664 }
665
666 #[must_use]
667 pub const fn expression(&self) -> &Option<Expression> {
668 &self.expression
669 }
670
671 #[must_use]
672 pub const fn definitions(&self) -> &Vec<Definition> {
673 &self.definitions
674 }
675
676 #[must_use]
677 pub fn imports(&self) -> Vec<&Use> {
678 let mut use_items = Vec::new();
679
680 for def in &self.definitions {
681 if let Definition::Use(use_info) = def {
682 use_items.push(use_info);
683 }
684 }
685
686 use_items
687 }
688}