sway_core/language/parsed/
mod.rs

1//! Contains all the code related to parsing Sway source code.
2mod code_block;
3pub mod declaration;
4mod expression;
5mod include_statement;
6mod module;
7mod program;
8mod use_statement;
9
10pub use code_block::*;
11pub use declaration::*;
12pub use expression::*;
13pub use include_statement::IncludeStatement;
14pub use module::{ModuleEvaluationOrder, ParseModule, ParseSubmodule};
15pub use program::{ParseProgram, TreeType};
16use sway_error::handler::ErrorEmitted;
17use sway_types::span::Span;
18pub use use_statement::{ImportType, UseStatement};
19
20use crate::{
21    engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext},
22    Engines,
23};
24
25/// Represents some exportable information that results from compiling some
26/// Sway source code.
27#[derive(Debug, Clone)]
28pub struct ParseTree {
29    /// The untyped AST nodes that constitute this tree's root nodes.
30    pub root_nodes: Vec<AstNode>,
31    /// The [Span] of the entire tree.
32    pub span: Span,
33}
34
35/// A single [AstNode] represents a node in the parse tree. Note that [AstNode]
36/// is a recursive type and can contain other [AstNode], thus populating the tree.
37#[derive(Debug, Clone)]
38pub struct AstNode {
39    /// The content of this ast node, which could be any control flow structure or other
40    /// basic organizational component.
41    pub content: AstNodeContent,
42    /// The [Span] representing this entire [AstNode].
43    pub span: Span,
44}
45
46impl EqWithEngines for AstNode {}
47impl PartialEqWithEngines for AstNode {
48    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
49        self.content.eq(&other.content, ctx)
50    }
51}
52
53/// Represents the various structures that constitute a Sway program.
54#[allow(clippy::large_enum_variant)]
55#[derive(Debug, Clone)]
56pub enum AstNodeContent {
57    /// A statement of the form `use foo::bar;` or `use ::foo::bar;`
58    UseStatement(UseStatement),
59    /// Any type of declaration, of which there are quite a few. See [Declaration] for more details
60    /// on the possible variants.
61    Declaration(Declaration),
62    /// Any type of expression, of which there are quite a few. See [Expression] for more details.
63    Expression(Expression),
64    /// A statement of the form `mod foo::bar;` which imports/includes another source file.
65    IncludeStatement(IncludeStatement),
66    /// A malformed statement.
67    ///
68    /// Used for parser recovery when we cannot form a more specific node.
69    /// The list of `Span`s are for consumption by the LSP and are,
70    /// when joined, the same as that stored in `statement.span`.
71    Error(Box<[Span]>, ErrorEmitted),
72}
73
74impl EqWithEngines for AstNodeContent {}
75impl PartialEqWithEngines for AstNodeContent {
76    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
77        match (self, other) {
78            (AstNodeContent::UseStatement(lhs), AstNodeContent::UseStatement(rhs)) => lhs.eq(rhs),
79            (AstNodeContent::Declaration(lhs), AstNodeContent::Declaration(rhs)) => {
80                lhs.eq(rhs, ctx)
81            }
82            (AstNodeContent::Expression(lhs), AstNodeContent::Expression(rhs)) => lhs.eq(rhs, ctx),
83            (AstNodeContent::IncludeStatement(lhs), AstNodeContent::IncludeStatement(rhs)) => {
84                lhs.eq(rhs)
85            }
86            (AstNodeContent::Error(lhs, ..), AstNodeContent::Error(rhs, ..)) => lhs.eq(rhs),
87            _ => false,
88        }
89    }
90}
91
92impl ParseTree {
93    /// Excludes all test functions from the parse tree.
94    pub(crate) fn exclude_tests(&mut self, engines: &Engines) {
95        self.root_nodes.retain(|node| !node.is_test(engines));
96    }
97}
98
99impl AstNode {
100    /// Checks if this `AstNode` is a test.
101    pub(crate) fn is_test(&self, engines: &Engines) -> bool {
102        if let AstNodeContent::Declaration(decl) = &self.content {
103            decl.is_test(engines)
104        } else {
105            false
106        }
107    }
108}