y_lang/ast/
mod.rs

1//! Module for parsing Y programs.
2//!
3//! It contains all structs for the internal representation of Y (i.e., the AST).
4mod array;
5mod assignment;
6mod binary_expr;
7mod binary_op;
8mod block;
9mod boolean;
10mod call;
11mod character;
12mod compiler_directive;
13mod declaration;
14mod definition;
15mod expression;
16mod fn_def;
17mod ident;
18mod if_statement;
19mod import;
20mod indexing;
21mod inline_asm;
22mod integer;
23mod intrinsic;
24mod param;
25mod parse_error;
26mod parser;
27mod postfix_expr;
28mod postfix_op;
29mod prefix_expr;
30mod prefix_op;
31mod statement;
32mod str;
33mod type_annotation;
34mod types;
35mod while_loop;
36
37pub use self::array::*;
38pub use self::assignment::*;
39pub use self::binary_expr::*;
40pub use self::binary_op::*;
41pub use self::block::*;
42pub use self::boolean::*;
43pub use self::call::*;
44pub use self::character::*;
45pub use self::compiler_directive::*;
46pub use self::declaration::*;
47pub use self::definition::*;
48pub use self::expression::*;
49pub use self::fn_def::*;
50pub use self::ident::*;
51pub use self::if_statement::*;
52pub use self::import::*;
53pub use self::indexing::*;
54pub use self::inline_asm::*;
55pub use self::integer::*;
56pub use self::intrinsic::*;
57pub use self::param::*;
58pub use self::parser::*;
59pub use self::postfix_expr::*;
60pub use self::postfix_op::*;
61pub use self::prefix_expr::*;
62pub use self::prefix_op::*;
63pub use self::statement::*;
64pub use self::str::*;
65pub use self::type_annotation::*;
66pub use self::types::*;
67pub use self::while_loop::*;
68
69use pest::iterators::Pair;
70
71pub use self::parser::Rule;
72
73pub use self::parser::*;
74
75/// A position within a file (i.e., line and column)
76pub type Position = (String, usize, usize);
77
78/// AST, representing a single Y program.
79#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
80pub struct Ast<T> {
81    /// Nodes within this AST.
82    nodes: Vec<Statement<T>>,
83}
84
85impl Ast<()> {
86    /// Create a new AST from a given pair of rules.
87    /// Note: This AST is not type-correct by default.
88    pub fn from_program(program: Vec<Pair<Rule>>, file: &str) -> Ast<()> {
89        let mut ast = vec![];
90
91        for statement in program {
92            if statement.as_rule() != Rule::EOI {
93                ast.push(Statement::from_pair(statement, file));
94            }
95        }
96        Self { nodes: ast }
97    }
98}
99
100impl<T> Ast<T>
101where
102    T: Clone,
103{
104    pub fn from_nodes(nodes: Vec<Statement<T>>) -> Ast<T> {
105        Self { nodes }
106    }
107
108    pub fn nodes(&self) -> Vec<Statement<T>> {
109        self.nodes.clone()
110    }
111}