nom-operator 0.0.2

Precedence climbing operator parser written with nom
Documentation
// Copyright 2017 The nom-operator project developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Expression tree

/// A parsed expression
#[derive(PartialEq)]
pub enum Expr<Atom, Operator> {
    /// An atom is whatever the return type of the atom parser returns
    Atom(Atom),

    /// UnExpr is a unary expression for example -10
    UnExpr {
        /// Item is the sub expression that the operator applies to.
        /// It is not called lhs because it can be either postfix
        /// or prefix
        item: Box<Expr<Atom, Operator>>,

        /// Operator token
        op: Operator,
    },

    /// BinExpr is a binary expression for example 10 * 10
    BinExpr {
        /// Left hand side sub expression
        left: Box<Expr<Atom, Operator>>,

        /// Operator token
        op: Operator,

        /// Right hand side sub expression
        right: Box<Expr<Atom, Operator>>,
    },

    /// TreExpr is a trenary expression such as true ? 2 : 3
    TreExpr {
        /// Left side sub expression
        left: Box<Expr<Atom, Operator>>,

        /// First operator token
        lop: Operator,

        /// Middle sub expression
        middle: Box<Expr<Atom, Operator>>,

        /// Right operator token
        rop: Operator,

        /// Right side sub expression
        right: Box<Expr<Atom, Operator>>,
    }
}

use std::fmt;
impl<Atom, Operator> fmt::Debug for Expr<Atom, Operator>
    where Atom: fmt::Debug, Operator: fmt::Debug {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            use expr::Expr::*;
            match *self {
                Atom(ref a) => write!(f, "A({:?})", a),
                UnExpr {
                    ref item,
                    ref op
                } => write!(f, "U({:?} {:?})", op, item),
                BinExpr {
                    ref left,
                    ref op,
                    ref right
                } => write!(f, "B({:?} {:?} {:?})", left, op, right),
                TreExpr{
                    ref left,
                    ref lop,
                    ref middle,
                    ref rop,
                    ref right
                } => write!(f, "{:?} {:?} {:?} {:?} {:?}",
                            left, lop, middle, rop, right),
            }
        }
    }