y_lang/ast/
assignment.rs

1use pest::iterators::Pair;
2
3use super::{Expression, Position, Rule};
4
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
6pub struct Assignment<T> {
7    pub lhs: Expression<T>,
8    pub value: Expression<T>,
9    pub position: Position,
10    pub info: T,
11}
12
13impl Assignment<()> {
14    pub fn from_pair(pair: Pair<Rule>, file: &str) -> Assignment<()> {
15        let mut inner = pair.clone().into_inner();
16
17        let (line, col) = pair.line_col();
18
19        let ident = Expression::from_pair(
20            inner.next().unwrap_or_else(|| {
21                panic!(
22                    "Expected lvalue in assignment '{}' at {}:{}",
23                    pair.as_str(),
24                    pair.line_col().0,
25                    pair.line_col().1
26                )
27            }),
28            file,
29        );
30
31        let value = inner.next().unwrap_or_else(|| {
32            panic!(
33                "Expected rvalue in assignment '{}' at {}:{}",
34                pair.as_str(),
35                pair.line_col().0,
36                pair.line_col().1
37            )
38        });
39        let value = Expression::from_pair(value, file);
40
41        Assignment {
42            lhs: ident,
43            value,
44            position: (file.to_owned(), line, col),
45            info: (),
46        }
47    }
48}