// Copyright 2014 Pierre Talbot (IRCAM) // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use oak::oak; oak! { // Optional stream declaration. type Stream<'a> = StrStream<'a>; program = spacing expression expression = term (term_op term)* > fold_left term = exponent (factor_op exponent)* > fold_left exponent = (factor exponent_op)* factor > fold_right factor = number > number_expr / identifier > variable_expr / let_expr > let_in_expr / lparen expression rparen let_expr = let_kw let_binding in_kw expression let_binding = identifier bind_op expression term_op = add_op > add_bin_op / sub_op > sub_bin_op factor_op = mul_op > mul_bin_op / div_op > div_bin_op exponent_op = exp_op > exp_bin_op identifier = !digit !keyword ident_char+ spacing > to_string ident_char = ["a-zA-Z0-9_"] digit = ["0-9"] number = digit+ spacing > to_number spacing = [" \n\r\t"]*:(^) kw_tail = !ident_char spacing keyword = let_kw / in_kw let_kw = "let" kw_tail in_kw = "in" kw_tail bind_op = "=" spacing add_op = "+" spacing sub_op = "-" spacing mul_op = "*" spacing div_op = "/" spacing exp_op = "^" spacing lparen = "(" spacing rparen = ")" spacing use std::str::FromStr; use self::Expression::*; use self::BinOp::*; pub type PExpr = Box; #[derive(Debug)] pub enum Expression { Variable(String), Number(u32), BinaryExpr(BinOp, PExpr, PExpr), LetIn(String, PExpr, PExpr) } #[derive(Debug)] pub enum BinOp { Add, Sub, Mul, Div, Exp } fn to_number(raw_text: Vec) -> u32 { u32::from_str(&*to_string(raw_text)).unwrap() } fn number_expr(value: u32) -> PExpr { Box::new(Number(value)) } fn variable_expr(ident: String) -> PExpr { Box::new(Variable(ident)) } fn to_string(raw_text: Vec) -> String { raw_text.into_iter().collect() } fn fold_left(head: PExpr, rest: Vec<(BinOp, PExpr)>) -> PExpr { rest.into_iter().fold(head, |accu, (op, expr)| Box::new(BinaryExpr(op, accu, expr))) } fn fold_right(front: Vec<(PExpr, BinOp)>, last: PExpr) -> PExpr { front.into_iter().rev().fold(last, |accu, (expr, op)| Box::new(BinaryExpr(op, expr, accu))) } fn let_in_expr(var: String, value: PExpr, expr: PExpr) -> PExpr { Box::new(LetIn(var, value, expr)) } fn add_bin_op() -> BinOp { Add } fn sub_bin_op() -> BinOp { Sub } fn mul_bin_op() -> BinOp { Mul } fn div_bin_op() -> BinOp { Div } fn exp_bin_op() -> BinOp { Exp } }