From 79b0c96d4c5e2854bf74278a95f8cdb8a29408a8 Mon Sep 17 00:00:00 2001 From: Afonso Bordado Date: Fri, 21 Apr 2017 17:04:34 +0000 Subject: [PATCH] Add fieldsep --- examples/repl.rs | 4 +--- src/exp.rs | 27 ++++++++++++++++++++------- src/number.rs | 2 +- src/op.rs | 48 ++++-------------------------------------------- 4 files changed, 26 insertions(+), 55 deletions(-) diff --git a/examples/repl.rs b/examples/repl.rs index 54afbb1..21f3838 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -20,15 +20,13 @@ extern crate nom_lua; -use nom_lua::op; - fn exec_repl() { use std::io::{BufRead, Write}; let stdin = std::io::stdin(); let stdout = std::io::stdout(); for line in stdin.lock().lines() { if let nom_lua::IResult::Done(_, string) = - op::parse_op(line.expect("Failed to read line").as_bytes()) { + nom_lua::exp::parse_exp(line.expect("Failed to read line").as_bytes()) { println!("EVAL: {}", string); } else { println!("ERROR: Parse Error"); diff --git a/src/exp.rs b/src/exp.rs index 739c33a..05241b9 100644 --- a/src/exp.rs +++ b/src/exp.rs @@ -22,24 +22,30 @@ use ast::ASTNode; use number::parse_number; +use op::parse_op; named!(parse_vararg< ASTNode >, map!(tag!("..."), |_| ASTNode::VarArg)); named!(parse_nil< ASTNode >, map!(tag!("nil"), |_| ASTNode::Nil)); named!(parse_bool< ASTNode >, alt!(map!(tag!("false"), |_| ASTNode::Bool(false)) | map!(tag!("true"), |_| ASTNode::Bool(true)))); -named!(pub parse_exp< ASTNode >, alt!( +named!(parse_prefixexp< ASTNode >, alt!( + //parse_var | + //parse_functioncall | + delimited!(tag!("("), ws!(parse_exp), tag!(")")) +)); + +named!(pub parse_exp< ASTNode >, dbg!(alt!( + parse_number | parse_nil | parse_bool | - parse_number | + parse_op | // parse_literal_string - parse_vararg + parse_vararg | // parse_functiondef | - // prefixexp + parse_prefixexp // parse_tableconstructior - // exp binop exp - // unop exp - )); + ))); // TODO: Missing tests //named!(parse_tableconstructor< ASTNode >, @@ -51,6 +57,11 @@ named!(pub parse_exp< ASTNode >, alt!( //named!(parse_fieldlist< ASTNode >, unimplemented!()); +//named!(parse_field, alt!( +// delimited!(tag!("["), ws!(parse_exp), tag!("]")) | +// tag!(";") +//)); +named!(parse_fieldsep, alt!(tag!(",") | tag!(";"))); #[cfg(test)] mod tests { @@ -58,6 +69,8 @@ mod tests { ast_test!(test_parse_bool_t, parse_bool, "true", ASTNode::Bool(true)); ast_test!(test_parse_bool_f, parse_bool, "false", ASTNode::Bool(false)); ast_test!(test_parse_vararg, parse_vararg, "...", ASTNode::VarArg); + ast_valid!(test_parse_fieldsep_1, parse_fieldsep, ";"); + ast_valid!(test_parse_fieldsep_2, parse_fieldsep, ","); //make a generalized example of this test, ie: any random char after a tag //ast_panic_test!(test_parse_vararg_false, parse_vararg, "....", ASTNode::VarArg); diff --git a/src/number.rs b/src/number.rs index 3e004a3..0f6a0a2 100644 --- a/src/number.rs +++ b/src/number.rs @@ -175,6 +175,6 @@ mod tests { //TODO: Enable these tests //ast_test!(test_parse_number_1, parse_number, "20", ASTNode::Integer(20)); - //ast_test!(test_parse_number_2, parse_number, "20.0", ASTNode::Float(20.0)); + ast_test!(test_parse_number_2, parse_number, "20.0", ASTNode::Float(20.0)); //ast_test!(test_parse_number_3, parse_number, "1000000000000000000000000", ASTNode::Float(1e+24)); } diff --git a/src/op.rs b/src/op.rs index e4b43d1..c21ee56 100644 --- a/src/op.rs +++ b/src/op.rs @@ -31,26 +31,7 @@ use std::str::FromStr; use ast::ASTNode; use super::exp::parse_exp; - -//use self::relational_ops::parse_relational_ops; -//use self::arithmetic_ops::{parse_addsub, parse_exponent}; -//use self::binary_ops::{parse_binary_or}; -//use self::logic_ops::{parse_logic_or}; -//use self::concat_ops::{parse_concat}; - -/// Here is the call chain for the exp parser -/// fold_arithmetic_ops ( ^ ) -/// fold_unary_ops ( all of them ) -/// fold_arithmetic_ops ( * // % / ) -/// fold_arithmetic_ops ( + - ) -/// fold_concat_ops ( + - ) -/// fold_binary_ops ( << >> ) -/// fold_binary_ops ( & ) -/// fold_binary_ops ( ~ ) -/// fold_binary_ops ( | ) -/// parse_relational_ops ( < <= > >= == ~= ) -/// logic_ops ( and ) -/// logic_ops ( or ) +use super::number::parse_number; //This is marked just for convenience so users know where to enter named!(pub parse_op, dbg!(alt!(parse_unop | parse_binop))); @@ -82,9 +63,9 @@ fn fold_binop(bop: BinOp, left: ASTNode, right: ASTNode) -> ASTNode { } named!(pub parse_binop, do_parse!( - left: factor >> + left: parse_exp >> bop: binop >> - right: factor >> (fold_binop(bop, left, right)))); + right: parse_exp >> (fold_binop(bop, left, right)))); named!(binop, alt!( ws!(tag!("^")) => { |_| BinOp::Exp } | @@ -138,7 +119,7 @@ pub enum BinOp { named!(parse_unop, do_parse!( unop: unop >> - right: factor >> (fold_unop(unop, right)))); + right: parse_exp >> (fold_unop(unop, right)))); fn fold_unop(unop: UnOp, right: ASTNode) -> ASTNode { @@ -165,24 +146,3 @@ pub enum UnOp { UMin, BinNot } - - -named!(pub parens< ASTNode >, - delimited!( - ws!(tag!("(")), - map!(map!(parse_op, Box::new), ASTNode::Paren), - ws!(tag!(")")) - )); - -named!(pub factor< ASTNode >, - alt_complete!( - map!( - map_res!( - map_res!( - ws!(digit), - str::from_utf8), - FromStr::from_str), - ASTNode::Integer) - | - parens - ));