swamp_ast/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5pub 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<'_>) -> fmt::Result {
19        write!(f, "<{}:{}>", self.offset, self.length)
20    }
21}
22
23// Common metadata that can be shared across all AST nodes
24#[derive(PartialEq, Eq, Hash, Default, Clone)]
25pub struct Node {
26    pub span: SpanWithoutFileId,
27    // TODO: Add comments and attributes
28}
29
30impl Debug for Node {
31    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
32        write!(f, "{:?}", self.span)
33    }
34}
35
36/// Identifiers ================
37#[derive(Debug, PartialEq, Eq, Clone, Hash)]
38pub struct QualifiedTypeIdentifier {
39    pub name: LocalTypeIdentifier,
40    pub module_path: Option<ModulePath>,
41    pub generic_params: Vec<GenericParameter>,
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<GenericParameter>,
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<GenericParameter>,
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<GenericParameter>,
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 annotation: Option<Type>,
234    pub expression: Box<Expression>,
235}
236
237#[derive(Debug, Clone)]
238pub struct NamedStructDef {
239    pub identifier: LocalTypeIdentifierWithOptionalTypeVariables,
240    pub struct_type: AnonymousStructType,
241}
242
243#[derive(Debug, Clone)]
244pub enum DefinitionKind {
245    AliasDef(AliasType),
246    NamedStructDef(NamedStructDef),
247    EnumDef(
248        LocalTypeIdentifierWithOptionalTypeVariables,
249        Vec<EnumVariantType>,
250    ),
251    FunctionDef(Function),
252    ImplDef(LocalTypeIdentifierWithOptionalTypeVariables, Vec<Function>),
253    Mod(Mod),
254    Use(Use),
255    // Other
256    Constant(ConstantInfo),
257}
258
259#[derive(Debug, Clone)]
260pub struct Definition {
261    pub node: Node,
262    pub kind: DefinitionKind,
263    pub attributes: Vec<Attribute>,
264}
265
266#[derive(Debug, Clone)]
267pub struct ForVar {
268    pub identifier: Node,
269    pub is_mut: Option<Node>,
270}
271
272#[derive(Debug, Clone)]
273pub enum ForPattern {
274    Single(ForVar),
275    Pair(ForVar, ForVar),
276}
277
278impl ForPattern {
279    #[must_use]
280    pub const fn is_key_variable_mut(&self) -> bool {
281        match self {
282            Self::Single(_a) => false,
283            Self::Pair(a, _b) => a.is_mut.is_some(),
284        }
285    }
286    #[must_use]
287    pub fn is_value_mut(&self) -> Option<Node> {
288        match self {
289            Self::Single(a) => a.is_mut.clone(),
290            Self::Pair(a, b) => {
291                assert!(
292                    a.is_mut.is_none(),
293                    "key target var is not allowed to be mut"
294                );
295                b.is_mut.clone()
296            }
297        }
298    }
299}
300
301#[derive(Debug, Clone)]
302pub struct IterableExpression {
303    pub expression: Box<Expression>,
304}
305
306#[derive(Clone, Eq, PartialEq)]
307pub struct Variable {
308    pub name: Node,
309    pub is_mutable: Option<Node>,
310}
311
312#[derive(Debug, Clone)]
313pub struct VariableBinding {
314    pub variable: Variable,
315    pub expression: Option<Expression>,
316}
317
318impl Variable {
319    #[must_use]
320    pub const fn new(name: Node, is_mutable: Option<Node>) -> Self {
321        Self { name, is_mutable }
322    }
323}
324
325// Since this is a helper struct, we want to implement the debug output for it
326// to have it more concise
327impl Debug for Variable {
328    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
329        if let Some(found) = &self.is_mutable {
330            write!(f, "mut {found:?} {:?}", self.name)
331        } else {
332            write!(f, "{:?}", self.name)
333        }
334    }
335}
336
337#[derive(Debug, Eq, Clone, PartialEq)]
338pub struct Parameter {
339    pub variable: Variable,
340    pub param_type: Type,
341}
342
343#[derive(Debug, Clone)]
344pub struct FunctionDeclaration {
345    pub name: Node,
346    pub params: Vec<Parameter>,
347    pub self_parameter: Option<SelfParameter>,
348    pub return_type: Option<Type>,
349}
350
351#[derive(Debug, Clone)]
352pub struct FunctionWithBody {
353    pub attributes: Vec<Attribute>,
354    pub declaration: FunctionDeclaration,
355    pub body: Expression,
356}
357
358#[derive(Debug, Clone)]
359pub enum Function {
360    Internal(FunctionWithBody),
361    External(Node, FunctionDeclaration),
362}
363
364impl Function {
365    #[must_use]
366    pub const fn node(&self) -> &Node {
367        match self {
368            Self::Internal(func_with_body) => &func_with_body.body.node,
369            Self::External(node, _) => node,
370        }
371    }
372}
373
374#[derive(Debug, Clone)]
375pub struct SelfParameter {
376    pub is_mutable: Option<Node>,
377    pub self_node: Node,
378}
379
380#[derive(Debug, PartialEq, Eq)]
381pub enum AssignmentOperatorKind {
382    Compound(CompoundOperatorKind),
383    Assign, // =
384}
385
386#[derive(Debug, PartialEq, Eq, Clone)]
387pub enum CompoundOperatorKind {
388    Add,    // +=
389    Sub,    // -=
390    Mul,    // *=
391    Div,    // /=
392    Modulo, // %=
393}
394
395#[derive(Debug, Clone)]
396pub struct CompoundOperator {
397    pub node: Node,
398    pub kind: CompoundOperatorKind,
399}
400
401#[derive(Debug, Clone)]
402pub enum RangeMode {
403    Inclusive,
404    Exclusive,
405}
406
407#[derive(Clone)]
408pub struct Expression {
409    pub kind: ExpressionKind,
410    pub node: Node,
411}
412
413impl Debug for Expression {
414    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
415        write!(f, "{:?}{:?}", self.node.span, self.kind)
416    }
417}
418
419#[derive(Debug, Clone)]
420pub enum Postfix {
421    FieldAccess(Node),
422    Subscript(Expression),
423    MemberCall(Node, Option<Vec<GenericParameter>>, Vec<Expression>),
424    FunctionCall(Node, Option<Vec<GenericParameter>>, Vec<Expression>),
425    /*
426    AdvancedFunctionCall(
427        Box<Expression>,
428        Node,
429        Option<Vec<GenericParameter>>,
430        Vec<Expression>,
431    ),
432
433     */
434    OptionalChainingOperator(Node),     // ?-postfix
435    NoneCoalescingOperator(Expression), // ??-postfix
436    SubscriptTuple(Expression, Expression),
437}
438
439#[derive(Debug, Clone)]
440pub struct PostfixChain {
441    pub base: Box<Expression>,
442    pub postfixes: Vec<Postfix>,
443}
444
445/// Expressions are things that "converts" to a value when evaluated.
446#[derive(Debug, Clone)]
447pub enum ExpressionKind {
448    // Access
449    PostfixChain(PostfixChain),
450
451    // References
452    ContextAccess, // Context/Lone/Bare-dot
453    VariableReference(Variable),
454    ConstantReference(QualifiedConstantIdentifier),
455    StaticMemberFunctionReference(QualifiedTypeIdentifier, Node),
456    IdentifierReference(QualifiedIdentifier),
457
458    // Assignments
459    VariableDefinition(Variable, Option<Type>, Box<Expression>),
460    VariableAssignment(Variable, Box<Expression>),
461    Assignment(Box<Expression>, Box<Expression>),
462    CompoundAssignment(Box<Expression>, CompoundOperator, Box<Expression>),
463    DestructuringAssignment(Vec<Variable>, Box<Expression>),
464
465    // Operators
466    BinaryOp(Box<Expression>, BinaryOperator, Box<Expression>),
467    UnaryOp(UnaryOperator, Box<Expression>),
468
469    //
470    Block(Vec<Expression>),
471    With(Vec<VariableBinding>, Box<Expression>),
472    When(
473        Vec<VariableBinding>,
474        Box<Expression>,
475        Option<Box<Expression>>,
476    ),
477
478    // Control flow
479    ForLoop(ForPattern, IterableExpression, Box<Expression>),
480    WhileLoop(Box<Expression>, Box<Expression>),
481
482    // Compare and Matching
483    If(Box<Expression>, Box<Expression>, Option<Box<Expression>>),
484    Match(Box<Expression>, Vec<MatchArm>),
485    Guard(Vec<GuardExpr>),
486
487    InterpolatedString(Vec<StringPart>),
488
489    // Literals
490    AnonymousStructLiteral(Vec<FieldExpression>, bool),
491    NamedStructLiteral(QualifiedTypeIdentifier, Vec<FieldExpression>, bool),
492    Range(Box<Expression>, Box<Expression>, RangeMode),
493    Literal(LiteralKind),
494
495    Lambda(Vec<Variable>, Box<Expression>),
496    Error, // Something was wrong in parsing
497}
498
499#[derive(Debug, Clone)]
500pub struct MatchArm {
501    pub pattern: Pattern,
502    pub expression: Expression,
503}
504
505// Are constructed by themselves
506#[derive(Debug, Clone)]
507pub enum LiteralKind {
508    Int,
509    Float,
510    String(String),
511    Bool,
512    EnumVariant(EnumVariantLiteral),
513    Tuple(Vec<Expression>),
514    InternalInitializerList(Vec<Expression>),
515    InternalInitializerPairList(Vec<(Expression, Expression)>),
516    None,
517}
518
519#[derive(Debug, Clone)]
520pub struct FieldExpression {
521    pub field_name: FieldName,
522    pub expression: Expression,
523}
524
525#[derive(Debug, Eq, Hash, Clone, PartialEq)]
526pub struct StructTypeField {
527    pub field_name: FieldName,
528    pub field_type: Type,
529}
530
531#[derive(Debug, Clone)]
532pub enum EnumVariantLiteral {
533    Simple(QualifiedTypeIdentifier, LocalTypeIdentifier),
534    Tuple(
535        QualifiedTypeIdentifier,
536        LocalTypeIdentifier,
537        Vec<Expression>,
538    ),
539    Struct(
540        QualifiedTypeIdentifier,
541        LocalTypeIdentifier,
542        Vec<FieldExpression>,
543        bool,
544    ),
545}
546
547impl EnumVariantLiteral {
548    #[must_use]
549    pub const fn node(&self) -> &Node {
550        match self {
551            Self::Tuple(ident, _, _) | Self::Struct(ident, _, _, _) | Self::Simple(ident, _) => {
552                &ident.name.0
553            }
554        }
555    }
556}
557
558#[derive(Debug, Clone)]
559pub enum EnumVariantType {
560    Simple(Node),
561    Tuple(Node, Vec<Type>),
562    Struct(Node, AnonymousStructType),
563}
564
565#[derive(Debug, PartialEq, Eq, Clone, Hash)]
566pub struct TypeForParameter {
567    pub ast_type: Type,
568    pub is_mutable: bool,
569}
570
571#[derive(Debug, Clone, PartialEq, Eq, Hash)]
572pub enum GenericParameter {
573    Type(Type),
574    UnsignedInt(Node),
575    UnsignedTupleInt(Node, Node),
576}
577
578impl GenericParameter {
579    #[must_use]
580    pub fn get_unsigned_int_node(&self) -> &Node {
581        let Self::UnsignedInt(node) = self else {
582            panic!("wasn't unsigned int")
583        };
584        node
585    }
586
587    #[must_use]
588    pub fn get_unsigned_int_tuple_nodes(&self) -> (&Node, &Node) {
589        let Self::UnsignedTupleInt(first, second) = self else {
590            panic!("wasn't unsigned int tuple")
591        };
592        (first, second)
593    }
594}
595
596impl GenericParameter {
597    #[must_use]
598    pub fn get_type(&self) -> &Type {
599        let Self::Type(ty) = self else {
600            panic!("{}", format!("wasn't type {self:?}"))
601        };
602        ty
603    }
604}
605
606#[derive(Debug, PartialEq, Eq, Clone, Hash)]
607pub enum Type {
608    // Composite
609    FixedCapacityArray(Box<Type>, Node),          // `[T; N]`
610    Slice(Box<Type>), // `[T]`. Contiguous memory segments without ownership, Unsized Type (DST) that has inline data
611    FixedCapacityMap(Box<Type>, Box<Type>, Node), // `[K:V;N]`
612    DynamicLengthMap(Box<Type>, Box<Type>), // `[K:V]`
613
614    AnonymousStruct(AnonymousStructType),
615    Unit,
616    Tuple(Vec<Type>),
617    Function(Vec<TypeForParameter>, Box<Type>),
618
619    Named(QualifiedTypeIdentifier),
620
621    Optional(Box<Type>, Node),
622}
623
624#[derive(Debug, Clone)]
625pub struct BinaryOperator {
626    pub kind: BinaryOperatorKind,
627    pub node: Node,
628}
629
630// Takes a left and right side expression
631#[derive(Debug, Clone)]
632pub enum BinaryOperatorKind {
633    Add,
634    Subtract,
635    Multiply,
636    Divide,
637    Modulo,
638    LogicalOr,
639    LogicalAnd,
640    Equal,
641    NotEqual,
642    LessThan,
643    LessEqual,
644    GreaterThan,
645    GreaterEqual,
646}
647
648// Only takes one expression argument
649#[derive(Debug, Clone)]
650pub enum UnaryOperator {
651    Not(Node),
652    Negate(Node),
653    BorrowMutRef(Node),
654}
655
656#[derive(Debug, Clone)]
657pub struct GuardExpr {
658    pub clause: GuardClause,
659    pub result: Expression,
660}
661
662#[derive(Debug, Clone)]
663pub enum GuardClause {
664    Wildcard(Node),
665    Expression(Expression),
666}
667
668// Patterns are used in matching and destructuring
669#[derive(Debug, Clone)]
670pub enum Pattern {
671    Wildcard(Node),
672    NormalPattern(Node, NormalPattern, Option<GuardClause>),
673}
674
675// Patterns are used in matching and destructuring
676#[derive(Debug, Clone)]
677pub enum NormalPattern {
678    PatternList(Vec<PatternElement>),
679    EnumPattern(Node, Option<Vec<PatternElement>>),
680    Literal(LiteralKind),
681}
682
683#[derive(Debug, Clone)]
684pub enum PatternElement {
685    Variable(Variable),
686    Expression(Expression),
687    Wildcard(Node),
688}
689
690#[derive(Debug, Clone)]
691pub enum StringPart {
692    Literal(Node, String),
693    Interpolation(Box<Expression>, Option<FormatSpecifier>),
694}
695
696#[derive(Debug, Clone)]
697pub enum FormatSpecifier {
698    LowerHex(Node),                      // :x
699    UpperHex(Node),                      // :X
700    Binary(Node),                        // :b
701    Float(Node),                         // :f
702    Precision(u32, Node, PrecisionType), // :..2f or :..5s
703}
704
705#[derive(Debug, Clone)]
706pub enum PrecisionType {
707    Float(Node),
708    String(Node),
709}
710
711#[derive(Debug, Clone)]
712pub enum AttributeArg {
713    /// A path/identifier, e.g. `Debug` or `unix`
714    Path(QualifiedIdentifier),
715    /// A literal value, e.g. `"foo"`, `42`, `true`
716    Literal(AttributeValue),
717    /// A function call, e.g. `any(unix, windows)` or `rename = "foo"`
718    Function(QualifiedIdentifier, Vec<AttributeArg>),
719}
720
721#[derive(Debug, Clone)]
722pub enum AttributeLiteralKind {
723    Int,
724    String(String),
725    Bool,
726}
727
728#[derive(Debug, Clone)]
729pub enum AttributeValue {
730    Literal(Node, AttributeLiteralKind),
731    Path(QualifiedIdentifier),
732    Function(QualifiedIdentifier, Vec<AttributeArg>),
733}
734
735#[derive(Debug, Clone)]
736pub struct Attribute {
737    pub is_inner: bool,
738    pub path: QualifiedIdentifier,
739    pub args: Vec<AttributeArg>,
740    pub node: Node,
741}
742
743#[derive()]
744pub struct Module {
745    pub expression: Option<Expression>,
746    pub definitions: Vec<Definition>,
747}
748
749impl Debug for Module {
750    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
751        for definition in &self.definitions {
752            writeln!(f, "{definition:?}")?;
753        }
754
755        if !self.definitions.is_empty() && self.expression.is_some() {
756            writeln!(f, "---")?;
757        }
758
759        if let Some(found_expression) = &self.expression {
760            match &found_expression.kind {
761                ExpressionKind::Block(expressions) => {
762                    for expression in expressions {
763                        writeln!(f, "{expression:?}")?;
764                    }
765                }
766                _ => writeln!(f, "{found_expression:?}")?,
767            }
768        }
769
770        Ok(())
771    }
772}
773
774impl Module {
775    #[must_use]
776    pub const fn new(definitions: Vec<Definition>, expression: Option<Expression>) -> Self {
777        Self {
778            expression,
779            definitions,
780        }
781    }
782
783    #[must_use]
784    pub const fn expression(&self) -> &Option<Expression> {
785        &self.expression
786    }
787
788    #[must_use]
789    pub const fn definitions(&self) -> &Vec<Definition> {
790        &self.definitions
791    }
792
793    #[must_use]
794    pub fn imports(&self) -> Vec<&Use> {
795        let mut use_items = Vec::new();
796
797        for def in &self.definitions {
798            if let DefinitionKind::Use(use_info) = &def.kind {
799                use_items.push(use_info);
800            }
801        }
802
803        use_items
804    }
805}