Skip to content

Commit

Permalink
Add fieldsep
Browse files Browse the repository at this point in the history
  • Loading branch information
afonso360 committed Apr 21, 2017
1 parent 0b19588 commit 79b0c96
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 55 deletions.
4 changes: 1 addition & 3 deletions examples/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
27 changes: 20 additions & 7 deletions src/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 >,
Expand All @@ -51,13 +57,20 @@ 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 {
ast_test!(test_parse_nil, parse_nil, "nil", ASTNode::Nil);
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);
Expand Down
2 changes: 1 addition & 1 deletion src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
48 changes: 4 additions & 44 deletions src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ASTNode>, dbg!(alt!(parse_unop | parse_binop)));
Expand Down Expand Up @@ -82,9 +63,9 @@ fn fold_binop(bop: BinOp, left: ASTNode, right: ASTNode) -> ASTNode {
}

named!(pub parse_binop<ASTNode>, 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<BinOp>, alt!(
ws!(tag!("^")) => { |_| BinOp::Exp } |
Expand Down Expand Up @@ -138,7 +119,7 @@ pub enum BinOp {

named!(parse_unop<ASTNode>, 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 {
Expand All @@ -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
));

0 comments on commit 79b0c96

Please sign in to comment.