mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-23 18:29:50 -04:00
Add datastructure to support tracking position of Value nodes in the AST.
This commit is contained in:
parent
6c3662d361
commit
b3677861c6
84
src/ast.rs
84
src/ast.rs
@ -14,20 +14,53 @@
|
||||
use std::collections::HashSet;
|
||||
use std::borrow::Borrow;
|
||||
|
||||
#[derive(Debug,PartialEq,Clone)]
|
||||
pub struct Position {
|
||||
pub line: usize,
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
pub type FieldList = Vec<(String, Expression)>; // str is expected to be a symbol
|
||||
pub type SelectorList = Vec<String>; // str is expected to always be a symbol.
|
||||
|
||||
#[derive(Debug,PartialEq,Clone)]
|
||||
pub struct LocatedNode<T> {
|
||||
pub pos: Option<Position>,
|
||||
pub val: T,
|
||||
}
|
||||
|
||||
impl<T> LocatedNode<T> {
|
||||
pub fn new(v: T) -> Self {
|
||||
Self {
|
||||
pos: None,
|
||||
val: v,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_pos(v: T, pos: Position) -> Self {
|
||||
Self {
|
||||
pos: Some(pos),
|
||||
val: v,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn make_value_node<T>(v: T) -> LocatedNode<T> {
|
||||
LocatedNode::new(v)
|
||||
}
|
||||
|
||||
/// Value represents a Value in the UCG parsed AST.
|
||||
#[derive(Debug,PartialEq,Clone)]
|
||||
pub enum Value {
|
||||
// Constant Values
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
String(String),
|
||||
Symbol(String),
|
||||
Int(LocatedNode<i64>),
|
||||
Float(LocatedNode<f64>),
|
||||
String(LocatedNode<String>),
|
||||
Symbol(LocatedNode<String>),
|
||||
// Complex Values
|
||||
Tuple(FieldList),
|
||||
Selector(SelectorList),
|
||||
Tuple(LocatedNode<FieldList>),
|
||||
Selector(LocatedNode<SelectorList>),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
@ -56,12 +89,12 @@ impl Value {
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
&Value::Int(ref i) => format!("{}", i),
|
||||
&Value::Float(ref f) => format!("{}", f),
|
||||
&Value::String(ref s) => format!("{}", s),
|
||||
&Value::Symbol(ref s) => format!("{}", s),
|
||||
&Value::Tuple(ref fs) => format!("{}", Self::fields_to_string(fs)),
|
||||
&Value::Selector(ref v) => v.join("."),
|
||||
&Value::Int(ref i) => format!("{}", i.val),
|
||||
&Value::Float(ref f) => format!("{}", f.val),
|
||||
&Value::String(ref s) => format!("{}", s.val),
|
||||
&Value::Symbol(ref s) => format!("{}", s.val),
|
||||
&Value::Tuple(ref fs) => format!("{}", Self::fields_to_string(&fs.val)),
|
||||
&Value::Selector(ref v) => v.val.join("."),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -99,12 +132,13 @@ impl MacroDef {
|
||||
if let &Value::Symbol(ref name) = val {
|
||||
let mut ok = true;
|
||||
for arg in self.argdefs.iter() {
|
||||
ok &= arg == name
|
||||
ok &= arg == &name.val
|
||||
}
|
||||
if !ok {
|
||||
bad_symbols.insert(name.clone());
|
||||
bad_symbols.insert(name.val.clone());
|
||||
}
|
||||
} else if let &Value::Selector(ref list) = val {
|
||||
} else if let &Value::Selector(ref sel_node) = val {
|
||||
let list = &sel_node.val;
|
||||
let mut ok = true;
|
||||
if list.len() > 0 {
|
||||
// We only look to see if the first selector item exists.
|
||||
@ -119,7 +153,8 @@ impl MacroDef {
|
||||
bad_symbols.insert(list[0].clone());
|
||||
}
|
||||
}
|
||||
} else if let &Value::Tuple(ref fields) = val {
|
||||
} else if let &Value::Tuple(ref tuple_node) = val {
|
||||
let fields = &tuple_node.val;
|
||||
for &(_, ref expr) in fields.iter() {
|
||||
stack.push(expr);
|
||||
}
|
||||
@ -257,8 +292,8 @@ mod ast_test {
|
||||
],
|
||||
fields: vec![
|
||||
("f1".to_string(), Expression::Add(BinaryExpression(
|
||||
Value::Symbol("foo".to_string()),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))),
|
||||
Value::Symbol(make_value_node("foo".to_string())),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))),
|
||||
],
|
||||
};
|
||||
assert!(def.validate_symbols().unwrap() == ());
|
||||
@ -272,9 +307,10 @@ mod ast_test {
|
||||
],
|
||||
fields: vec![
|
||||
("f1".to_string(), Expression::Add(BinaryExpression(
|
||||
Value::Symbol("bar".to_string()),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))),
|
||||
Value::Symbol(make_value_node("bar".to_string())),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))),
|
||||
],
|
||||
|
||||
};
|
||||
let mut expected = HashSet::new();
|
||||
expected.insert("bar".to_string());
|
||||
@ -289,8 +325,8 @@ mod ast_test {
|
||||
],
|
||||
fields: vec![
|
||||
("f1".to_string(), Expression::Add(BinaryExpression(
|
||||
Value::Selector(vec!["foo".to_string(), "quux".to_string()]),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))),
|
||||
Value::Selector(make_value_node(vec!["foo".to_string(), "quux".to_string()])),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))),
|
||||
],
|
||||
};
|
||||
assert!(def.validate_symbols().unwrap() == ());
|
||||
@ -304,8 +340,8 @@ mod ast_test {
|
||||
],
|
||||
fields: vec![
|
||||
("f1".to_string(), Expression::Add(BinaryExpression(
|
||||
Value::Selector(vec!["bar".to_string(), "quux".to_string()]),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))),
|
||||
Value::Selector(make_value_node(vec!["bar".to_string(), "quux".to_string()])),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))),
|
||||
],
|
||||
};
|
||||
let mut expected = HashSet::new();
|
||||
|
134
src/build.rs
134
src/build.rs
@ -220,14 +220,15 @@ impl Builder {
|
||||
/// new_builder constructs Builder with initialized fields ready to parse.
|
||||
fn value_to_val(&self, v: Value) -> Result<Rc<Val>, Box<Error>> {
|
||||
match v {
|
||||
Value::Int(i) => Ok(Rc::new(Val::Int(i))),
|
||||
Value::Float(f) => Ok(Rc::new(Val::Float(f))),
|
||||
Value::String(s) => Ok(Rc::new(Val::String(s.to_string()))),
|
||||
Value::Int(i) => Ok(Rc::new(Val::Int(i.val))),
|
||||
Value::Float(f) => Ok(Rc::new(Val::Float(f.val))),
|
||||
Value::String(s) => Ok(Rc::new(Val::String(s.val.to_string()))),
|
||||
Value::Symbol(s) => {
|
||||
self.lookup_sym(&s).ok_or(Box::new(
|
||||
BuildError::NoSuchSymbol(format!("Unable to find {}", s))))
|
||||
self.lookup_sym(&s.val).ok_or(Box::new(
|
||||
BuildError::NoSuchSymbol(format!("Unable to find {}", s.val))))
|
||||
},
|
||||
Value::Tuple(mut fields) => {
|
||||
Value::Tuple(mut tuple_node) => {
|
||||
let fields = &mut tuple_node.val;
|
||||
let mut new_fields = Vec::new();
|
||||
for (name, expr) in fields.drain(0..) {
|
||||
let val = try!(self.eval_expr(expr));
|
||||
@ -236,8 +237,8 @@ impl Builder {
|
||||
new_fields.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
Ok(Rc::new(Val::Tuple(new_fields)))
|
||||
},
|
||||
Value::Selector(selector_list) => {
|
||||
self.lookup_selector(selector_list)
|
||||
Value::Selector(selector_list_node) => {
|
||||
self.lookup_selector(selector_list_node.val)
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -619,9 +620,11 @@ mod test {
|
||||
fn test_eval_div_expr() {
|
||||
let b = Builder::new();
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Div(BinaryExpression(Value::Int(2), Box::new(Expression::Simple(Value::Int(2))))),
|
||||
(Expression::Div(BinaryExpression(Value::Int(make_value_node(2)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(2)))))),
|
||||
Val::Int(1)),
|
||||
(Expression::Div(BinaryExpression(Value::Float(2.0), Box::new(Expression::Simple(Value::Float(2.0))))),
|
||||
(Expression::Div(BinaryExpression(Value::Float(make_value_node(2.0)),
|
||||
Box::new(Expression::Simple(Value::Float(make_value_node(2.0)))))),
|
||||
Val::Float(1.0)),
|
||||
], b);
|
||||
}
|
||||
@ -631,7 +634,8 @@ mod test {
|
||||
fn test_eval_div_expr_fail() {
|
||||
let b = Builder::new();
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Div(BinaryExpression(Value::Float(2.0), Box::new(Expression::Simple(Value::Int(2))))),
|
||||
(Expression::Div(BinaryExpression(Value::Float(make_value_node(2.0)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(2)))))),
|
||||
Val::Float(1.0)),
|
||||
], b);
|
||||
}
|
||||
@ -640,9 +644,11 @@ mod test {
|
||||
fn test_eval_mul_expr() {
|
||||
let b = Builder::new();
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Mul(BinaryExpression(Value::Int(2), Box::new(Expression::Simple(Value::Int(2))))),
|
||||
(Expression::Mul(BinaryExpression(Value::Int(make_value_node(2)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(2)))))),
|
||||
Val::Int(4)),
|
||||
(Expression::Mul(BinaryExpression(Value::Float(2.0), Box::new(Expression::Simple(Value::Float(2.0))))),
|
||||
(Expression::Mul(BinaryExpression(Value::Float(make_value_node(2.0)),
|
||||
Box::new(Expression::Simple(Value::Float(make_value_node(2.0)))))),
|
||||
Val::Float(4.0)),
|
||||
], b);
|
||||
}
|
||||
@ -652,7 +658,8 @@ mod test {
|
||||
fn test_eval_mul_expr_fail() {
|
||||
let b = Builder::new();
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Mul(BinaryExpression(Value::Float(2.0), Box::new(Expression::Simple(Value::Int(2))))),
|
||||
(Expression::Mul(BinaryExpression(Value::Float(make_value_node(2.0)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(20)))))),
|
||||
Val::Float(1.0)),
|
||||
], b);
|
||||
}
|
||||
@ -661,9 +668,11 @@ mod test {
|
||||
fn test_eval_subtract_expr() {
|
||||
let b = Builder::new();
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Sub(BinaryExpression(Value::Int(2), Box::new(Expression::Simple(Value::Int(1))))),
|
||||
(Expression::Sub(BinaryExpression(Value::Int(make_value_node(2)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1)))))),
|
||||
Val::Int(1)),
|
||||
(Expression::Sub(BinaryExpression(Value::Float(2.0), Box::new(Expression::Simple(Value::Float(1.0))))),
|
||||
(Expression::Sub(BinaryExpression(Value::Float(make_value_node(2.0)),
|
||||
Box::new(Expression::Simple(Value::Float(make_value_node(1.0)))))),
|
||||
Val::Float(1.0)),
|
||||
], b);
|
||||
}
|
||||
@ -673,7 +682,8 @@ mod test {
|
||||
fn test_eval_subtract_expr_fail() {
|
||||
let b = Builder::new();
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Sub(BinaryExpression(Value::Float(2.0), Box::new(Expression::Simple(Value::Int(2))))),
|
||||
(Expression::Sub(BinaryExpression(Value::Float(make_value_node(2.0)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(2)))))),
|
||||
Val::Float(1.0)),
|
||||
], b);
|
||||
}
|
||||
@ -682,11 +692,14 @@ mod test {
|
||||
fn test_eval_add_expr() {
|
||||
let b = Builder::new();
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Add(BinaryExpression(Value::Int(1), Box::new(Expression::Simple(Value::Int(1))))),
|
||||
(Expression::Add(BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1)))))),
|
||||
Val::Int(2)),
|
||||
(Expression::Add(BinaryExpression(Value::Float(1.0), Box::new(Expression::Simple(Value::Float(1.0))))),
|
||||
(Expression::Add(BinaryExpression(Value::Float(make_value_node(1.0)),
|
||||
Box::new(Expression::Simple(Value::Float(make_value_node(1.0)))))),
|
||||
Val::Float(2.0)),
|
||||
(Expression::Add(BinaryExpression(Value::String("foo".to_string()), Box::new(Expression::Simple(Value::String("bar".to_string()))))),
|
||||
(Expression::Add(BinaryExpression(Value::String(make_value_node("foo".to_string())),
|
||||
Box::new(Expression::Simple(Value::String(make_value_node("bar".to_string())))))),
|
||||
Val::String("foobar".to_string())),
|
||||
], b);
|
||||
}
|
||||
@ -696,7 +709,8 @@ mod test {
|
||||
fn test_eval_add_expr_fail() {
|
||||
let b = Builder::new();
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Add(BinaryExpression(Value::Float(2.0), Box::new(Expression::Simple(Value::Int(2))))),
|
||||
(Expression::Add(BinaryExpression(Value::Float(make_value_node(2.0)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(2)))))),
|
||||
Val::Float(1.0)),
|
||||
], b);
|
||||
}
|
||||
@ -704,12 +718,12 @@ mod test {
|
||||
#[test]
|
||||
fn test_eval_simple_expr() {
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Simple(Value::Int(1)), Val::Int(1)),
|
||||
(Expression::Simple(Value::Float(2.0)), Val::Float(2.0)),
|
||||
(Expression::Simple(Value::String("foo".to_string())),
|
||||
(Expression::Simple(Value::Int(make_value_node(1))), Val::Int(1)),
|
||||
(Expression::Simple(Value::Float(make_value_node(2.0))), Val::Float(2.0)),
|
||||
(Expression::Simple(Value::String(make_value_node("foo".to_string()))),
|
||||
Val::String("foo".to_string())),
|
||||
(Expression::Simple(Value::Tuple(vec![("bar".to_string(),
|
||||
Expression::Simple(Value::Int(1)))])),
|
||||
(Expression::Simple(Value::Tuple(make_value_node(vec![("bar".to_string(),
|
||||
Expression::Simple(Value::Int(make_value_node(1))))]))),
|
||||
Val::Tuple(vec![("bar".to_string(), Rc::new(Val::Int(1)))])),
|
||||
], Builder::new());
|
||||
}
|
||||
@ -719,7 +733,7 @@ mod test {
|
||||
let mut b = Builder::new();
|
||||
b.out.entry("var1".to_string()).or_insert(Rc::new(Val::Int(1)));
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Simple(Value::Symbol("var1".to_string())), Val::Int(1)),
|
||||
(Expression::Simple(Value::Symbol(make_value_node("var1".to_string()))), Val::Int(1)),
|
||||
], b);
|
||||
}
|
||||
|
||||
@ -727,7 +741,7 @@ mod test {
|
||||
fn test_eval_simple_lookup_error() {
|
||||
let mut b = Builder::new();
|
||||
b.out.entry("var1".to_string()).or_insert(Rc::new(Val::Int(1)));
|
||||
assert!(b.eval_expr(Expression::Simple(Value::Symbol("var".to_string()))).is_err());
|
||||
assert!(b.eval_expr(Expression::Simple(Value::Symbol(make_value_node("var".to_string())))).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -746,7 +760,7 @@ mod test {
|
||||
("lvl1".to_string(), Rc::new(Val::Int(4)))
|
||||
])));
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Simple(Value::Selector(vec!["var1".to_string()])), Val::Tuple(
|
||||
(Expression::Simple(Value::Selector(make_value_node(vec!["var1".to_string()]))), Val::Tuple(
|
||||
vec![
|
||||
("lvl1".to_string(), Rc::new(Val::Tuple(
|
||||
vec![
|
||||
@ -755,21 +769,21 @@ mod test {
|
||||
))),
|
||||
]
|
||||
)),
|
||||
(Expression::Simple(Value::Selector(vec!["var1".to_string(),
|
||||
"lvl1".to_string()])),
|
||||
(Expression::Simple(Value::Selector(make_value_node(vec!["var1".to_string(),
|
||||
"lvl1".to_string()]))),
|
||||
Val::Tuple(
|
||||
vec![
|
||||
("lvl2".to_string(), Rc::new(Val::Int(3))),
|
||||
]
|
||||
)),
|
||||
(Expression::Simple(Value::Selector(vec!["var1".to_string(),
|
||||
(Expression::Simple(Value::Selector(make_value_node(vec!["var1".to_string(),
|
||||
"lvl1".to_string(),
|
||||
"lvl2".to_string()])),
|
||||
"lvl2".to_string()]))),
|
||||
Val::Int(3)),
|
||||
(Expression::Simple(Value::Selector(vec!["var2".to_string()])),
|
||||
(Expression::Simple(Value::Selector(make_value_node(vec!["var2".to_string()]))),
|
||||
Val::Int(2)),
|
||||
(Expression::Simple(Value::Selector(vec!["var3".to_string(),
|
||||
"lvl1".to_string()])),
|
||||
(Expression::Simple(Value::Selector(make_value_node(vec!["var3".to_string(),
|
||||
"lvl1".to_string()]))),
|
||||
Val::Int(4)),
|
||||
], b);
|
||||
}
|
||||
@ -805,7 +819,7 @@ mod test {
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Copy(vec!["tpl1".to_string()],
|
||||
vec![("fld1".to_string(),
|
||||
Expression::Simple(Value::String("2".to_string())))]),
|
||||
Expression::Simple(Value::String(make_value_node("2".to_string()))))]),
|
||||
Val::Tuple(
|
||||
vec![
|
||||
("fld1".to_string(), Rc::new(Val::String("2".to_string()))),
|
||||
@ -826,7 +840,7 @@ mod test {
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Copy(vec!["tpl1".to_string()],
|
||||
vec![("fld2".to_string(),
|
||||
Expression::Simple(Value::String("2".to_string())))]),
|
||||
Expression::Simple(Value::String(make_value_node("2".to_string()))))]),
|
||||
// Add a new field to the copy
|
||||
Val::Tuple(
|
||||
// NOTE(jwall): The order of these is important in order to ensure
|
||||
@ -841,9 +855,9 @@ mod test {
|
||||
(Expression::Copy(vec!["tpl1".to_string()],
|
||||
vec![
|
||||
("fld1".to_string(),
|
||||
Expression::Simple(Value::Int(3))),
|
||||
Expression::Simple(Value::Int(make_value_node(3)))),
|
||||
("fld2".to_string(),
|
||||
Expression::Simple(Value::String("2".to_string()))),
|
||||
Expression::Simple(Value::String(make_value_node("2".to_string())))),
|
||||
]),
|
||||
Val::Tuple(
|
||||
vec![
|
||||
@ -852,7 +866,7 @@ mod test {
|
||||
],
|
||||
)),
|
||||
// The source tuple is still unmodified.
|
||||
(Expression::Simple(Value::Selector(vec!["tpl1".to_string()])),
|
||||
(Expression::Simple(Value::Selector(make_value_node(vec!["tpl1".to_string()]))),
|
||||
Val::Tuple(
|
||||
vec![
|
||||
("fld1".to_string(), Rc::new(Val::Int(1))),
|
||||
@ -867,13 +881,13 @@ mod test {
|
||||
b.out.entry("tstmac".to_string()).or_insert(Rc::new(Val::Macro(MacroDef{
|
||||
argdefs: vec!["arg1".to_string()],
|
||||
fields: vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Symbol("arg1".to_string()))),
|
||||
("foo".to_string(), Expression::Simple(Value::Symbol(make_value_node("arg1".to_string())))),
|
||||
],
|
||||
})));
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Call(CallDef{
|
||||
macroref: vec!["tstmac".to_string()],
|
||||
arglist: vec![Expression::Simple(Value::String("bar".to_string()))],
|
||||
arglist: vec![Expression::Simple(Value::String(make_value_node("bar".to_string())))],
|
||||
}),
|
||||
Val::Tuple(vec![
|
||||
("foo".to_string(), Rc::new(Val::String("bar".to_string()))),
|
||||
@ -889,13 +903,13 @@ mod test {
|
||||
b.out.entry("tstmac".to_string()).or_insert(Rc::new(Val::Macro(MacroDef{
|
||||
argdefs: vec!["arg2".to_string()],
|
||||
fields: vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Symbol("arg1".to_string()))),
|
||||
("foo".to_string(), Expression::Simple(Value::Symbol(make_value_node("arg1".to_string())))),
|
||||
],
|
||||
})));
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Call(CallDef{
|
||||
macroref: vec!["tstmac".to_string()],
|
||||
arglist: vec![Expression::Simple(Value::String("bar".to_string()))],
|
||||
arglist: vec![Expression::Simple(Value::String(make_value_node("bar".to_string())))],
|
||||
}),
|
||||
Val::Tuple(vec![
|
||||
("foo".to_string(), Rc::new(Val::String("bar".to_string()))),
|
||||
@ -910,20 +924,20 @@ mod test {
|
||||
b.out.entry("baz".to_string()).or_insert(Rc::new(Val::String("boo".to_string())));
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Select(SelectDef{
|
||||
val: Box::new(Expression::Simple(Value::Symbol("foo".to_string()))),
|
||||
default: Box::new(Expression::Simple(Value::Int(1))),
|
||||
val: Box::new(Expression::Simple(Value::Symbol(make_value_node("foo".to_string())))),
|
||||
default: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
||||
tuple: vec![
|
||||
("bar".to_string(), Expression::Simple(Value::Int(2))),
|
||||
("quux".to_string(), Expression::Simple(Value::String("2".to_string()))),
|
||||
("bar".to_string(), Expression::Simple(Value::Int(make_value_node(2)))),
|
||||
("quux".to_string(), Expression::Simple(Value::String(make_value_node("2".to_string())))),
|
||||
],
|
||||
}),
|
||||
Val::Int(2)),
|
||||
(Expression::Select(SelectDef{
|
||||
val: Box::new(Expression::Simple(Value::Symbol("baz".to_string()))),
|
||||
default: Box::new(Expression::Simple(Value::Int(1))),
|
||||
val: Box::new(Expression::Simple(Value::Symbol(make_value_node("baz".to_string())))),
|
||||
default: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
||||
tuple: vec![
|
||||
("bar".to_string(), Expression::Simple(Value::Int(2))),
|
||||
("quux".to_string(), Expression::Simple(Value::String("2".to_string()))),
|
||||
("bar".to_string(), Expression::Simple(Value::Int(make_value_node(2)))),
|
||||
("quux".to_string(), Expression::Simple(Value::String(make_value_node("2".to_string())))),
|
||||
],
|
||||
}),
|
||||
// If the field doesn't exist then we get the default.
|
||||
@ -938,11 +952,11 @@ mod test {
|
||||
b.out.entry("foo".to_string()).or_insert(Rc::new(Val::Int(4)));
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Select(SelectDef{
|
||||
val: Box::new(Expression::Simple(Value::Symbol("foo".to_string()))),
|
||||
default: Box::new(Expression::Simple(Value::Int(1))),
|
||||
val: Box::new(Expression::Simple(Value::Symbol(make_value_node("foo".to_string())))),
|
||||
default: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
||||
tuple: vec![
|
||||
("bar".to_string(), Expression::Simple(Value::Int(2))),
|
||||
("quux".to_string(), Expression::Simple(Value::String("2".to_string()))),
|
||||
("bar".to_string(), Expression::Simple(Value::Int(make_value_node(2)))),
|
||||
("quux".to_string(), Expression::Simple(Value::String(make_value_node("2".to_string())))),
|
||||
],
|
||||
}),
|
||||
Val::Int(2)),
|
||||
@ -954,10 +968,10 @@ mod test {
|
||||
let mut b = Builder::new();
|
||||
b.build_stmt(Statement::Let{
|
||||
name:"foo".to_string(),
|
||||
value: Expression::Simple(Value::String("bar".to_string())),
|
||||
value: Expression::Simple(Value::String(make_value_node("bar".to_string()))),
|
||||
}).unwrap();
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Simple(Value::Symbol("foo".to_string())),
|
||||
(Expression::Simple(Value::Symbol(make_value_node("foo".to_string()))),
|
||||
Val::String("bar".to_string())),
|
||||
], b);
|
||||
}
|
||||
@ -979,7 +993,7 @@ mod test {
|
||||
]))),
|
||||
])));
|
||||
test_expr_to_val(vec![
|
||||
(Expression::Simple(Value::Symbol("foo".to_string())),
|
||||
(Expression::Simple(Value::Symbol(make_value_node("foo".to_string()))),
|
||||
Val::Tuple(vec![
|
||||
("bar".to_string(), Rc::new(Val::Tuple(vec![
|
||||
("quux".to_string(), Rc::new(Val::Int(1))),
|
||||
|
193
src/parse.rs
193
src/parse.rs
@ -53,7 +53,6 @@ named!(fatcomma, tag!("=>"));
|
||||
fn is_symbol_char(c: u8) -> bool {
|
||||
is_alphanumeric(c) || c == '-' as u8 || c == '_' as u8
|
||||
}
|
||||
|
||||
// a field is the building block of symbols and tuple field names.
|
||||
named!(field<String>,
|
||||
map_res!(preceded!(peek!(alpha), take_while!(is_symbol_char)),
|
||||
@ -62,7 +61,7 @@ named!(field<String>,
|
||||
);
|
||||
|
||||
fn symbol_to_value(s: String) -> ParseResult<Value> {
|
||||
Ok(Value::Symbol(s))
|
||||
Ok(Value::Symbol(make_value_node(s)))
|
||||
}
|
||||
|
||||
// symbol is a bare unquoted field.
|
||||
@ -76,7 +75,7 @@ named!(quoted<String>,
|
||||
);
|
||||
|
||||
fn str_to_value(s: String) -> ParseResult<Value> {
|
||||
Ok(Value::String(s))
|
||||
Ok(Value::String(make_value_node(s)))
|
||||
}
|
||||
|
||||
// quoted_value is a quoted string.
|
||||
@ -95,7 +94,7 @@ fn triple_to_number(v: (Option<&[u8]>, Option<&[u8]>, Option<&[u8]>))
|
||||
let has_dot = v.1.is_some();
|
||||
|
||||
if v.0.is_some() && !has_dot && v.2.is_none() {
|
||||
return Ok(Value::Int(try!(FromStr::from_str(pref))));
|
||||
return Ok(Value::Int(make_value_node(try!(FromStr::from_str(pref)))));
|
||||
}
|
||||
|
||||
let suf = match v.2 {
|
||||
@ -105,7 +104,7 @@ fn triple_to_number(v: (Option<&[u8]>, Option<&[u8]>, Option<&[u8]>))
|
||||
|
||||
let to_parse = pref.to_string() + "." + suf;
|
||||
let f = try!(FromStr::from_str(&to_parse));
|
||||
return Ok(Value::Float(f));
|
||||
return Ok(Value::Float(make_value_node(f)));
|
||||
}
|
||||
|
||||
// NOTE(jwall): HERE THERE BE DRAGONS. The order for these matters
|
||||
@ -165,7 +164,7 @@ named!(
|
||||
|
||||
// Helper function to make the return types work for down below.
|
||||
fn vec_to_tuple(v: FieldList) -> ParseResult<Value> {
|
||||
Ok(Value::Tuple(v))
|
||||
Ok(Value::Tuple(make_value_node(v)))
|
||||
}
|
||||
|
||||
named!(field_list<FieldList>,
|
||||
@ -299,7 +298,7 @@ fn tuple_to_macro(mut t: (Vec<Value>, Value)) -> ParseResult<Expression> {
|
||||
Value::Tuple(v) => {
|
||||
Ok(Expression::Macro(MacroDef {
|
||||
argdefs: t.0.drain(0..).map(|s| s.to_string()).collect(),
|
||||
fields: v,
|
||||
fields: v.val,
|
||||
}))
|
||||
}
|
||||
// TODO(jwall): Show a better version of the unexpected parsed value.
|
||||
@ -333,7 +332,7 @@ fn tuple_to_select(t: (Expression, Expression, Value))
|
||||
Ok(Expression::Select(SelectDef{
|
||||
val: Box::new(t.0),
|
||||
default: Box::new(t.1),
|
||||
tuple: v,
|
||||
tuple: v.val,
|
||||
}))
|
||||
}
|
||||
// TODO(jwall): Show a better version of the unexpected parsed value.
|
||||
@ -377,7 +376,7 @@ named!(format_expression<Expression>,
|
||||
fn tuple_to_call(t: (Value, Vec<Expression>)) -> ParseResult<Expression> {
|
||||
if let Value::Selector(sl) = t.0 {
|
||||
Ok(Expression::Call(CallDef{
|
||||
macroref: sl,
|
||||
macroref: sl.val,
|
||||
arglist: t.1,
|
||||
}))
|
||||
} else {
|
||||
@ -386,7 +385,7 @@ fn tuple_to_call(t: (Value, Vec<Expression>)) -> ParseResult<Expression> {
|
||||
}
|
||||
|
||||
fn vec_to_selector_value(v: SelectorList) -> ParseResult<Value> {
|
||||
Ok(Value::Selector(v))
|
||||
Ok(Value::Selector(make_value_node(v)))
|
||||
}
|
||||
|
||||
named!(selector_value<Value>,
|
||||
@ -501,14 +500,16 @@ named!(pub parse<Vec<Statement> >, many1!(ws!(statement)));
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::str::from_utf8;
|
||||
|
||||
use super::{Statement, Expression, Value, MacroDef, SelectDef, CallDef};
|
||||
use super::{number, symbol, parse, field_value, tuple, grouped_expression};
|
||||
use super::{arglist, copy_expression, macro_expression, select_expression};
|
||||
use super::{format_expression, call_expression, expression};
|
||||
use super::{expression_statement, let_statement, import_statement, statement};
|
||||
use nom::IResult;
|
||||
use ast::*;
|
||||
|
||||
use nom::IResult;
|
||||
|
||||
#[test]
|
||||
fn test_statement_parse() {
|
||||
assert_eq!(statement(&b"import \"foo\" as foo;"[..]),
|
||||
@ -524,11 +525,11 @@ mod test {
|
||||
assert_eq!(statement(&b"let foo = 1.0 ;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Let{name: "foo".to_string(),
|
||||
value: Expression::Simple(Value::Float(1.0))}));
|
||||
value: Expression::Simple(Value::Float(make_value_node(1.0)))}));
|
||||
assert_eq!(statement(&b"1.0;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Expression(
|
||||
Expression::Simple(Value::Float(1.0)))));
|
||||
Expression::Simple(Value::Float(make_value_node(1.0))))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -562,15 +563,15 @@ mod test {
|
||||
assert_eq!(let_statement(&b"let foo = 1.0 ;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Let{name: "foo".to_string(),
|
||||
value: Expression::Simple(Value::Float(1.0))}));
|
||||
value: Expression::Simple(Value::Float(make_value_node(1.0)))}));
|
||||
assert_eq!(let_statement(&b"let foo= 1.0;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Let{name: "foo".to_string(),
|
||||
value: Expression::Simple(Value::Float(1.0))}));
|
||||
value: Expression::Simple(Value::Float(make_value_node(1.0)))}));
|
||||
assert_eq!(let_statement(&b"let foo =1.0;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Let{name: "foo".to_string(),
|
||||
value: Expression::Simple(Value::Float(1.0))}));
|
||||
value: Expression::Simple(Value::Float(make_value_node(1.0)))}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -579,80 +580,80 @@ mod test {
|
||||
assert_eq!(expression_statement(&b"1.0;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Expression(
|
||||
Expression::Simple(Value::Float(1.0)))));
|
||||
Expression::Simple(Value::Float(make_value_node(1.0))))));
|
||||
assert_eq!(expression_statement(&b"1.0 ;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Expression(
|
||||
Expression::Simple(Value::Float(1.0)))));
|
||||
Expression::Simple(Value::Float(make_value_node(1.0))))));
|
||||
assert_eq!(expression_statement(&b" 1.0;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Expression(
|
||||
Expression::Simple(Value::Float(1.0)))));
|
||||
Expression::Simple(Value::Float(make_value_node(1.0))))));
|
||||
assert_eq!(expression_statement(&b"foo;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Expression(
|
||||
Expression::Simple(Value::Symbol("foo".to_string())))));
|
||||
Expression::Simple(Value::Symbol(make_value_node("foo".to_string()))))));
|
||||
assert_eq!(expression_statement(&b"foo ;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Expression(
|
||||
Expression::Simple(Value::Symbol("foo".to_string())))));
|
||||
Expression::Simple(Value::Symbol(make_value_node("foo".to_string()))))));
|
||||
assert_eq!(expression_statement(&b" foo;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Expression(
|
||||
Expression::Simple(Value::Symbol("foo".to_string())))));
|
||||
Expression::Simple(Value::Symbol(make_value_node("foo".to_string()))))));
|
||||
assert_eq!(expression_statement(&b"\"foo\";"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Expression(
|
||||
Expression::Simple(Value::String("foo".to_string())))));
|
||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))))));
|
||||
assert_eq!(expression_statement(&b"\"foo\" ;"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Expression(
|
||||
Expression::Simple(Value::String("foo".to_string())))));
|
||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))))));
|
||||
assert_eq!(expression_statement(&b" \"foo\";"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Statement::Expression(
|
||||
Expression::Simple(Value::String("foo".to_string())))));
|
||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expression_parse() {
|
||||
assert_eq!(expression(&b"1"[..]),
|
||||
IResult::Done(&b""[..], Expression::Simple(Value::Int(1))));
|
||||
IResult::Done(&b""[..], Expression::Simple(Value::Int(make_value_node(1)))));
|
||||
assert_eq!(expression(&b"foo"[..]),
|
||||
IResult::Done(&b""[..], Expression::Simple(Value::Symbol("foo".to_string()))));
|
||||
IResult::Done(&b""[..], Expression::Simple(Value::Symbol(make_value_node("foo".to_string())))));
|
||||
assert_eq!(expression(&b"1 + 1"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Add(BinaryExpression(Value::Int(1),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))));
|
||||
Expression::Add(BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))));
|
||||
assert_eq!(expression(&b"1 - 1"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Sub(BinaryExpression(Value::Int(1),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))));
|
||||
Expression::Sub(BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))));
|
||||
assert_eq!(expression(&b"1 * 1"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Mul(BinaryExpression(Value::Int(1),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))));
|
||||
Expression::Mul(BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))));
|
||||
assert_eq!(expression(&b"1 / 1"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Div(BinaryExpression(Value::Int(1),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))));
|
||||
Expression::Div(BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))));
|
||||
|
||||
assert_eq!(expression(&b"1+1"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Add(BinaryExpression(Value::Int(1),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))));
|
||||
Expression::Add(BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))));
|
||||
assert_eq!(expression(&b"1-1"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Sub(BinaryExpression(Value::Int(1),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))));
|
||||
Expression::Sub(BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))));
|
||||
assert_eq!(expression(&b"1*1"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Mul(BinaryExpression(Value::Int(1),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))));
|
||||
Expression::Mul(BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))));
|
||||
assert_eq!(expression(&b"1/1"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Div(BinaryExpression(Value::Int(1),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))));
|
||||
Expression::Div(BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))));
|
||||
assert_eq!(expression(&b"macro (arg1, arg2) => { foo = arg1 }"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Macro(MacroDef{
|
||||
@ -661,7 +662,7 @@ mod test {
|
||||
"arg2".to_string(),
|
||||
],
|
||||
fields: vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Symbol("arg1".to_string()))),
|
||||
("foo".to_string(), Expression::Simple(Value::Symbol(make_value_node("arg1".to_string())))),
|
||||
],
|
||||
})
|
||||
)
|
||||
@ -669,10 +670,10 @@ mod test {
|
||||
assert_eq!(expression(&b"select foo, 1, { foo = 2 };"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Select(SelectDef{
|
||||
val: Box::new(Expression::Simple(Value::Symbol("foo".to_string()))),
|
||||
default: Box::new(Expression::Simple(Value::Int(1))),
|
||||
val: Box::new(Expression::Simple(Value::Symbol(make_value_node("foo".to_string())))),
|
||||
default: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
||||
tuple: vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Int(2)))
|
||||
("foo".to_string(), Expression::Simple(Value::Int(make_value_node(2))))
|
||||
]
|
||||
})
|
||||
)
|
||||
@ -682,8 +683,8 @@ mod test {
|
||||
Expression::Call(CallDef{
|
||||
macroref: vec!["foo".to_string(),"bar".to_string()],
|
||||
arglist: vec![
|
||||
Expression::Simple(Value::Int(1)),
|
||||
Expression::Simple(Value::String("foo".to_string())),
|
||||
Expression::Simple(Value::Int(make_value_node(1))),
|
||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))),
|
||||
],
|
||||
})
|
||||
)
|
||||
@ -693,8 +694,8 @@ mod test {
|
||||
Expression::Grouped(
|
||||
Box::new(
|
||||
Expression::Add(
|
||||
BinaryExpression(Value::Int(1),
|
||||
Box::new(Expression::Simple(Value::Int(1))))
|
||||
BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1)))))
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -712,15 +713,15 @@ mod test {
|
||||
assert_eq!(format_expression(&b"\"foo @ @\" % (1, 2)"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Format("foo @ @".to_string(),
|
||||
vec![Expression::Simple(Value::Int(1)),
|
||||
Expression::Simple(Value::Int(2))])
|
||||
vec![Expression::Simple(Value::Int(make_value_node(1))),
|
||||
Expression::Simple(Value::Int(make_value_node(2)))])
|
||||
)
|
||||
);
|
||||
assert_eq!(format_expression(&b"\"foo @ @\"%(1, 2)"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Format("foo @ @".to_string(),
|
||||
vec![Expression::Simple(Value::Int(1)),
|
||||
Expression::Simple(Value::Int(2))])
|
||||
vec![Expression::Simple(Value::Int(make_value_node(1))),
|
||||
Expression::Simple(Value::Int(make_value_node(2)))])
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -738,8 +739,8 @@ mod test {
|
||||
Expression::Call(CallDef{
|
||||
macroref: vec!["foo".to_string()],
|
||||
arglist: vec![
|
||||
Expression::Simple(Value::Int(1)),
|
||||
Expression::Simple(Value::String("foo".to_string())),
|
||||
Expression::Simple(Value::Int(make_value_node(1))),
|
||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))),
|
||||
],
|
||||
})
|
||||
)
|
||||
@ -750,8 +751,8 @@ mod test {
|
||||
Expression::Call(CallDef{
|
||||
macroref: vec!["foo".to_string(),"bar".to_string()],
|
||||
arglist: vec![
|
||||
Expression::Simple(Value::Int(1)),
|
||||
Expression::Simple(Value::String("foo".to_string())),
|
||||
Expression::Simple(Value::Int(make_value_node(1))),
|
||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))),
|
||||
],
|
||||
})
|
||||
)
|
||||
@ -768,10 +769,10 @@ mod test {
|
||||
assert_eq!(select_expression(&b"select foo, 1, { foo = 2 };"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Select(SelectDef{
|
||||
val: Box::new(Expression::Simple(Value::Symbol("foo".to_string()))),
|
||||
default: Box::new(Expression::Simple(Value::Int(1))),
|
||||
val: Box::new(Expression::Simple(Value::Symbol(make_value_node("foo".to_string())))),
|
||||
default: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
||||
tuple: vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Int(2)))
|
||||
("foo".to_string(), Expression::Simple(Value::Int(make_value_node(2))))
|
||||
]
|
||||
})
|
||||
)
|
||||
@ -797,8 +798,8 @@ mod test {
|
||||
Expression::Macro(MacroDef{
|
||||
argdefs: vec!["arg1".to_string(),
|
||||
"arg2".to_string()],
|
||||
fields: vec![("foo".to_string(), Expression::Simple(Value::Int(1))),
|
||||
("bar".to_string(), Expression::Simple(Value::Int(2)))
|
||||
fields: vec![("foo".to_string(), Expression::Simple(Value::Int(make_value_node(1)))),
|
||||
("bar".to_string(), Expression::Simple(Value::Int(make_value_node(2))))
|
||||
]
|
||||
})
|
||||
)
|
||||
@ -811,8 +812,8 @@ mod test {
|
||||
assert!(arglist(&b"arg1, arg2"[..]).is_done());
|
||||
assert_eq!(arglist(&b"arg1, arg2"[..]), IResult::Done(&b""[..],
|
||||
vec![
|
||||
Value::Symbol("arg1".to_string()),
|
||||
Value::Symbol("arg2".to_string())
|
||||
Value::Symbol(make_value_node("arg1".to_string())),
|
||||
Value::Symbol(make_value_node("arg2".to_string()))
|
||||
]));
|
||||
}
|
||||
|
||||
@ -830,7 +831,7 @@ mod test {
|
||||
assert_eq!(copy_expression(&b"foo{bar=1}"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Copy(vec!["foo".to_string()],
|
||||
vec![("bar".to_string(), Expression::Simple(Value::Int(1)))])
|
||||
vec![("bar".to_string(), Expression::Simple(Value::Int(make_value_node(1))))])
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -844,16 +845,16 @@ mod test {
|
||||
Expression::Grouped(
|
||||
Box::new(
|
||||
Expression::Simple(
|
||||
Value::Symbol("foo".to_string())))))
|
||||
Value::Symbol(make_value_node("foo".to_string()))))))
|
||||
);
|
||||
assert_eq!(grouped_expression(&b"(1 + 1)"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Expression::Grouped(
|
||||
Box::new(
|
||||
Expression::Add(
|
||||
BinaryExpression(Value::Int(1),
|
||||
BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(
|
||||
Value::Int(1))))
|
||||
Value::Int(make_value_node(1)))))
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -873,29 +874,29 @@ mod test {
|
||||
assert_eq!(tuple(&b"{ }"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Value::Tuple(
|
||||
vec![])));
|
||||
make_value_node(vec![]))));
|
||||
|
||||
assert_eq!(tuple(&b"{ foo = 1 }"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Value::Tuple(
|
||||
vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Int(1)))
|
||||
])));
|
||||
make_value_node(vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Int(make_value_node(1))))
|
||||
]))));
|
||||
|
||||
assert_eq!(tuple(&b"{ foo = 1, bar = \"1\" }"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Value::Tuple(
|
||||
vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Int(1))),
|
||||
("bar".to_string(), Expression::Simple(Value::String("1".to_string())))
|
||||
])));
|
||||
make_value_node(vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Int(make_value_node(1)))),
|
||||
("bar".to_string(), Expression::Simple(Value::String(make_value_node("1".to_string()))))
|
||||
]))));
|
||||
assert_eq!(tuple(&b"{ foo = 1, bar = {} }"[..]),
|
||||
IResult::Done(&b""[..],
|
||||
Value::Tuple(
|
||||
vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Int(1))),
|
||||
("bar".to_string(), Expression::Simple(Value::Tuple(Vec::new())))
|
||||
])));
|
||||
make_value_node(vec![
|
||||
("foo".to_string(), Expression::Simple(Value::Int(make_value_node(1)))),
|
||||
("bar".to_string(), Expression::Simple(Value::Tuple(make_value_node(Vec::new()))))
|
||||
]))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -904,13 +905,13 @@ mod test {
|
||||
assert!(field_value(&b"foo ="[..]).is_incomplete() );
|
||||
|
||||
assert_eq!(field_value(&b"foo = 1"[..]),
|
||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Int(1)))) );
|
||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Int(make_value_node(1))))) );
|
||||
assert_eq!(field_value(&b"foo = \"1\""[..]),
|
||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::String("1".to_string())))) );
|
||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::String(make_value_node("1".to_string()))))) );
|
||||
assert_eq!(field_value(&b"foo = bar"[..]),
|
||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Symbol("bar".to_string())))) );
|
||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Symbol(make_value_node("bar".to_string()))))) );
|
||||
assert_eq!(field_value(&b"foo = bar "[..]),
|
||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Symbol("bar".to_string())))) );
|
||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Symbol(make_value_node("bar".to_string()))))) );
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -918,23 +919,23 @@ mod test {
|
||||
assert!(number(&b"."[..]).is_err() );
|
||||
assert!(number(&b". "[..]).is_err() );
|
||||
assert_eq!(number(&b"1.0"[..]),
|
||||
IResult::Done(&b""[..], Value::Float(1.0)) );
|
||||
IResult::Done(&b""[..], Value::Float(make_value_node(1.0))) );
|
||||
assert_eq!(number(&b"1."[..]),
|
||||
IResult::Done(&b""[..], Value::Float(1.0)) );
|
||||
IResult::Done(&b""[..], Value::Float(make_value_node(1.0))) );
|
||||
assert_eq!(number(&b"1"[..]),
|
||||
IResult::Done(&b""[..], Value::Int(1)) );
|
||||
IResult::Done(&b""[..], Value::Int(make_value_node(1))) );
|
||||
assert_eq!(number(&b".1"[..]),
|
||||
IResult::Done(&b""[..], Value::Float(0.1)) );
|
||||
IResult::Done(&b""[..], Value::Float(make_value_node(0.1))) );
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_symbol_parsing() {
|
||||
assert_eq!(symbol(&b"foo"[..]),
|
||||
IResult::Done(&b""[..], Value::Symbol("foo".to_string())) );
|
||||
IResult::Done(&b""[..], Value::Symbol(make_value_node("foo".to_string()))) );
|
||||
assert_eq!(symbol(&b"foo-bar"[..]),
|
||||
IResult::Done(&b""[..], Value::Symbol("foo-bar".to_string())) );
|
||||
IResult::Done(&b""[..], Value::Symbol(make_value_node("foo-bar".to_string()))) );
|
||||
assert_eq!(symbol(&b"foo_bar"[..]),
|
||||
IResult::Done(&b""[..], Value::Symbol("foo_bar".to_string())) );
|
||||
IResult::Done(&b""[..], Value::Symbol(make_value_node("foo_bar".to_string()))) );
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -959,11 +960,11 @@ mod test {
|
||||
},
|
||||
Statement::Let{
|
||||
name: "foo".to_string(),
|
||||
value: Expression::Simple(Value::Int(1))
|
||||
value: Expression::Simple(Value::Int(make_value_node(1)))
|
||||
},
|
||||
Statement::Expression(
|
||||
Expression::Add(BinaryExpression(Value::Int(1),
|
||||
Box::new(Expression::Simple(Value::Int(1)))))
|
||||
Expression::Add(BinaryExpression(Value::Int(make_value_node(1)),
|
||||
Box::new(Expression::Simple(Value::Int(make_value_node(1))))))
|
||||
)
|
||||
]);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user