welly_parser/expr/
atom.rs

1use super::{Tree, Op};
2
3/// Describes one of Welly's built-in operators. See also [`Op`].
4///
5/// In general, operators have two meanings. For example `-` can mean
6/// negation or subtraction. The cases are distinguished according to whether
7/// the operator is preceded by an expression.
8///
9/// If the operator has only one meaning, we put it in both slots.
10#[derive(Debug, Copy, Clone)]
11pub struct Operator {
12    /// The meaning of the operator if has a left operand.
13    pub with_left: Op,
14
15    /// The meaning of the operator if has no left operand.
16    pub without_left: Op,
17}
18
19impl Operator {
20    /// Make an `Operator` describing an operator that has two meanings.
21    pub const fn new_ambiguous(with_left: Op, without_left: Op) -> Self {
22        Self {with_left, without_left }
23    }
24
25    /// Make an `Operator` describing an operator that has one meaning.
26    pub const fn new(op: Op) -> Self { Self::new_ambiguous(op, op) }
27}
28
29impl Tree for Operator {
30    fn declare_keywords(mut declare: impl FnMut(&'static str, Self)) {
31        declare("?", Operator::new(Op::Query));
32        declare("**", Operator::new(Op::Pow));
33        declare("~", Operator::new(Op::BitNot));
34        declare("$", Operator::new(Op::Share));
35        declare("*", Operator::new_ambiguous(Op::Mul, Op::Clone));
36        declare("/", Operator::new(Op::Div));
37        declare("%", Operator::new(Op::Rem));
38        declare("+", Operator::new_ambiguous(Op::Add, Op::Plus));
39        declare("-", Operator::new_ambiguous(Op::Sub, Op::Minus));
40        declare("<<", Operator::new(Op::SL));
41        declare(">>", Operator::new(Op::ASR));
42        declare(">>>", Operator::new(Op::LSR));
43        declare("&", Operator::new_ambiguous(Op::BitAnd, Op::Borrow));
44        declare("^", Operator::new(Op::BitXor));
45        declare("|", Operator::new(Op::BitOr));
46        declare(":", Operator::new(Op::Cast));
47        declare("..", Operator::new(Op::Exclusive));
48        declare("...", Operator::new(Op::Inclusive));
49        declare("<", Operator::new(Op::LT));
50        declare(">", Operator::new(Op::GT));
51        declare("<=", Operator::new(Op::LE));
52        declare(">=", Operator::new(Op::GE));
53        declare("<>", Operator::new(Op::LG));
54        declare("==", Operator::new(Op::EQ));
55        declare("!=", Operator::new(Op::NE));
56        declare("in", Operator::new(Op::In));
57        declare("not", Operator::new(Op::BoolNot));
58        declare("and", Operator::new(Op::BoolAnd));
59        declare("or", Operator::new(Op::BoolOr));
60    }
61}