y_lang/ast/
declaration.rs

1use pest::iterators::Pair;
2
3use super::{Ident, Position, Rule, TypeAnnotation};
4
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
6pub struct Declaration {
7    pub ident: Ident<()>,
8    pub type_annotation: TypeAnnotation,
9    pub position: Position,
10}
11
12impl Declaration {
13    pub fn from_pair(pair: Pair<Rule>, file: &str) -> Declaration {
14        assert_eq!(pair.as_rule(), Rule::declaration);
15
16        let (line, col) = pair.line_col();
17
18        let mut inner = pair.into_inner();
19
20        let ident = inner.next().unwrap();
21        let ident = Ident::from_pair(ident, file);
22
23        let type_annotation = inner.next().unwrap();
24        let type_annotation = TypeAnnotation::from_pair(type_annotation, file);
25
26        Declaration {
27            position: (file.to_owned(), line, col),
28            ident,
29            type_annotation,
30        }
31    }
32}