nom_operator/expr.rs
1// Copyright 2017 The nom-operator project developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Expression tree
10
11/// A parsed expression
12#[derive(PartialEq)]
13pub enum Expr<Atom, Operator> {
14 /// An atom is whatever the return type of the atom parser returns
15 Atom(Atom),
16
17 /// UnExpr is a unary expression for example -10
18 UnExpr {
19 /// Item is the sub expression that the operator applies to.
20 /// It is not called lhs because it can be either postfix
21 /// or prefix
22 item: Box<Expr<Atom, Operator>>,
23
24 /// Operator token
25 op: Operator,
26 },
27
28 /// BinExpr is a binary expression for example 10 * 10
29 BinExpr {
30 /// Left hand side sub expression
31 left: Box<Expr<Atom, Operator>>,
32
33 /// Operator token
34 op: Operator,
35
36 /// Right hand side sub expression
37 right: Box<Expr<Atom, Operator>>,
38 },
39
40 /// TreExpr is a trenary expression such as true ? 2 : 3
41 TreExpr {
42 /// Left side sub expression
43 left: Box<Expr<Atom, Operator>>,
44
45 /// First operator token
46 lop: Operator,
47
48 /// Middle sub expression
49 middle: Box<Expr<Atom, Operator>>,
50
51 /// Right operator token
52 rop: Operator,
53
54 /// Right side sub expression
55 right: Box<Expr<Atom, Operator>>,
56 }
57}
58
59use std::fmt;
60impl<Atom, Operator> fmt::Debug for Expr<Atom, Operator>
61 where Atom: fmt::Debug, Operator: fmt::Debug {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 use expr::Expr::*;
64 match *self {
65 Atom(ref a) => write!(f, "A({:?})", a),
66 UnExpr {
67 ref item,
68 ref op
69 } => write!(f, "U({:?} {:?})", op, item),
70 BinExpr {
71 ref left,
72 ref op,
73 ref right
74 } => write!(f, "B({:?} {:?} {:?})", left, op, right),
75 TreExpr{
76 ref left,
77 ref lop,
78 ref middle,
79 ref rop,
80 ref right
81 } => write!(f, "{:?} {:?} {:?} {:?} {:?}",
82 left, lop, middle, rop, right),
83 }
84 }
85 }
86