mago_ast/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::fmt::Debug;

use serde::Deserialize;
use serde::Serialize;

use mago_source::SourceIdentifier;
use mago_span::HasSpan;
use mago_span::Position;
use mago_span::Span;

pub use crate::ast::*;
pub use crate::node::*;
pub use crate::sequence::Sequence;
pub use crate::trivia::Trivia;
pub use crate::trivia::TriviaKind;

pub mod ast;
pub mod node;
pub mod sequence;
pub mod trivia;

#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct Program {
    pub source: SourceIdentifier,
    pub trivia: Sequence<Trivia>,
    pub statements: Sequence<Statement>,
}

impl Program {
    pub fn has_script(&self) -> bool {
        for statement in self.statements.iter() {
            if !matches!(statement, Statement::Inline(_)) {
                return true;
            }
        }

        false
    }
}

impl HasSpan for Program {
    fn span(&self) -> Span {
        let start =
            self.statements.first().map(|stmt| stmt.span().start).unwrap_or_else(|| Position::start_of(self.source));

        let end = self.statements.last().map(|stmt| stmt.span().end).unwrap_or_else(|| Position::start_of(self.source));

        Span::new(start, end)
    }
}