rustre_parser/
ast.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! Structures and enums to represent Lustre's syntax tree
//!
//! # Internal representation
//!
//! The structures in this module are all merely wrappers around
//! [rowan's `SyntaxNode`][rowan::SyntaxNode] that provide useful specific getters for each node
//! kind.

pub mod expr_visitor;

#[allow(warnings, unused)]
mod generated {
    include!(concat!(env!("OUT_DIR"), "/ast_generated.rs"));
}

use crate::lexer::Token;
use crate::{SyntaxNode, SyntaxToken};
#[doc(inline)]
pub use generated::*;

// These two traits have been stolen from rust-analyzer
// (and a lot of other things in this crate is actually inspired by RA)

pub trait AstNode {
    fn can_cast(kind: Token) -> bool
    where
        Self: Sized;

    fn cast(syntax: SyntaxNode) -> Option<Self>
    where
        Self: Sized;
    fn expect(syntax: SyntaxNode) -> Self
    where
        Self: Sized;

    fn syntax(&self) -> &SyntaxNode;
    fn clone_for_update(&self) -> Self
    where
        Self: Sized,
    {
        Self::cast(self.syntax().clone_for_update()).unwrap()
    }
    fn clone_subtree(&self) -> Self
    where
        Self: Sized,
    {
        Self::cast(self.syntax().clone_subtree()).unwrap()
    }
}

pub trait AstToken {
    fn can_cast(token: Token) -> bool
    where
        Self: Sized;

    fn cast(syntax: SyntaxToken) -> Option<Self>
    where
        Self: Sized;

    fn expect(syntax: SyntaxToken) -> Self
    where
        Self: Sized;

    fn syntax(&self) -> &SyntaxToken;

    fn text(&self) -> &str {
        self.syntax().text()
    }
}

fn debug_ast_node<N: AstNode>(
    node: &N,
    f: &mut std::fmt::Formatter<'_>,
    name: &str,
) -> std::fmt::Result {
    write!(f, "{}@{:?}", name, node.syntax().text_range())
}

// Additional methods that the build script can't generate

impl NodeProfileNode {
    pub fn params(&self) -> Option<ParamsNode> {
        self.syntax
            .children()
            .next()
            .filter(|s| s.kind() == Token::ParamsNode)
            .map(|syntax| ParamsNode { syntax })
    }

    pub fn return_params(&self) -> Option<ParamsNode> {
        self.syntax
            .children_with_tokens()
            .skip_while(|s| s.kind() != Token::Returns)
            .find(|s| s.kind() == Token::ParamsNode)
            .map(|syntax| ParamsNode {
                syntax: syntax.into_node().unwrap(),
            })
    }
}

impl IdRefNode {
    fn package_and_member(&self) -> (Option<Ident>, Ident) {
        let mut idents = self.all_ident();

        match (idents.next(), idents.next()) {
            (Some(member), None) => (None, member),
            (Some(package), Some(member)) => (Some(package), member),
            (None, None) => unimplemented!("unparseable"),
            (None, Some(_)) => unimplemented!("this is an iterator, of course this won't happen"),
        }
    }

    #[inline]
    pub fn package(&self) -> Option<Ident> {
        self.package_and_member().0
    }

    #[inline]
    pub fn member(&self) -> Ident {
        self.package_and_member().1
    }
}

impl Str {
    pub fn unescaped(&self) -> String {
        // TODO: handle escape sequences
        let text = self.syntax.text();
        text[1..text.len() - 1].to_owned()
    }
}

pub trait BinaryExpression {
    fn left(&self) -> Option<ExpressionNode>;
    fn right(&self) -> Option<ExpressionNode>;
}

pub trait UnaryExpression {
    fn operand(&self) -> Option<ExpressionNode>;
}

pub trait VariadicExpr {
    fn list(&self) -> Option<ExpressionListNode>;
}

macro_rules! impl_bin_expr {
    ($name:ident) => {
        impl BinaryExpression for $name {
            fn left(&self) -> Option<ExpressionNode> {
                $name::left(self)
            }

            fn right(&self) -> Option<ExpressionNode> {
                $name::right(self)
            }
        }
    };
}

impl_bin_expr!(WhenExpressionNode);
impl_bin_expr!(FbyExpressionNode);
impl_bin_expr!(ArrowExpressionNode);
impl_bin_expr!(AndExpressionNode);
impl_bin_expr!(OrExpressionNode);
impl_bin_expr!(XorExpressionNode);
impl_bin_expr!(ImplExpressionNode);
impl_bin_expr!(EqExpressionNode);
impl_bin_expr!(NeqExpressionNode);
impl_bin_expr!(LtExpressionNode);
impl_bin_expr!(LteExpressionNode);
impl_bin_expr!(GtExpressionNode);
impl_bin_expr!(GteExpressionNode);
impl_bin_expr!(DivExpressionNode);
impl_bin_expr!(ModExpressionNode);
impl_bin_expr!(SubExpressionNode);
impl_bin_expr!(AddExpressionNode);
impl_bin_expr!(MulExpressionNode);
impl_bin_expr!(PowerExpressionNode);
impl_bin_expr!(HatExpressionNode);

macro_rules! impl_un_expr {
    ($name:ident) => {
        impl UnaryExpression for $name {
            fn operand(&self) -> Option<ExpressionNode> {
                $name::operand(self)
            }
        }
    };
}

impl_un_expr!(NotExpressionNode);
impl_un_expr!(NegExpressionNode);
impl_un_expr!(PreExpressionNode);
impl_un_expr!(CurrentExpressionNode);
impl_un_expr!(IntExpressionNode);
impl_un_expr!(RealExpressionNode);

macro_rules! impl_variadic_expr {
    ($name:ident) => {
        impl VariadicExpr for $name {
            fn list(&self) -> Option<ExpressionListNode> {
                $name::list(self)
            }
        }
    };
}

impl_variadic_expr!(DieseExpressionNode);
impl_variadic_expr!(NorExpressionNode);