From 64e4fb6a28e4224879cecd24b3dfe5497d514be3 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 3 Dec 2017 15:15:32 -0600 Subject: [PATCH] Cleanup: Extraneous types, compact code, and fmt. * Got rid of LocatedNode since it was the same thing as Positioned. * Positioned has a constructor now. * Cleaned up some unnecessary macros. * Created some more macros to make code more compact. * Ran cargo fmt * Some fixes to the README. --- README.md | 11 +- TODO.md | 3 +- src/ast.rs | 185 +++++++++++----------- src/build.rs | 427 +++++++++++++++++++++------------------------------ src/parse.rs | 394 ++++++++++++++++++++--------------------------- 5 files changed, 447 insertions(+), 573 deletions(-) diff --git a/README.md b/README.md index 1bef4dd..7bdcb25 100644 --- a/README.md +++ b/README.md @@ -42,14 +42,16 @@ off of the value assigned. ### Simple Expressions -UCG supports simple math expressions using `+`, `-`, `*`, `/`) and string -concatenation using `+`. The expressions enforce the same type between operands. +UCG supports simple math expressions using `+`, `-`, `*`, `/`) as well as string +and list concatenation using `+`. The expressions enforce the same type between operands. 1 + 1; 1.0 + 1.0; "foo" + "bar"; + + [1, 2] + [3, 4]; ### String formatting @@ -58,7 +60,7 @@ shamelessly ripped off from python. "foo @ @ \@" % (1, "bar") -This gets turned into "foo 1 bar {" +This gets turned into "foo 1 bar @" ### Bindings and Tuples. @@ -81,6 +83,9 @@ can not be reassigned to once defined. Lists are an ordered collection of elements. Lists can be indexed using dotted selectors. List indexes start at 0. +Lists do not at present type check their contents so you can mix types freely within the list. In the future we may enforce that lists contain only the same types but it +is as yet unclear if that would be desirable or not. + let hosts = ["db1.local.net", "db2.local.net"]; let host1 = hosts.0; diff --git a/TODO.md b/TODO.md index 21ab78b..8e7ecc9 100644 --- a/TODO.md +++ b/TODO.md @@ -10,8 +10,9 @@ compiled configuration. For some configuration file formats we need a way to specify a particular organiztion for a given configuration structure. Some options here could be -* A Functional Transform (i.e. xslt, or css transforms) +* A Functional Transform similar to xslt or css transforms. * A Templating language +* Annotations. # Minor Fixes and Polish diff --git a/src/ast.rs b/src/ast.rs index c31a775..b83ff92 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -21,12 +21,34 @@ use std::cmp::PartialEq; use std::hash::Hasher; use std::hash::Hash; +macro_rules! enum_type_equality { + ( $slf:ident, $r:expr, $( $l:pat ),* ) => { + match $slf { + $( + $l => { + if let $l = $r { + true + } else { + false + } + } + )* + } + } +} + #[derive(Debug,PartialEq,Eq,Clone,PartialOrd,Ord,Hash)] pub struct Position { pub line: usize, pub column: usize, } +impl Position { + pub fn new(line: usize, column: usize) -> Self { + Position{line:line, column: column} + } +} + #[derive(Debug,PartialEq,Eq,Clone,PartialOrd,Ord,Hash)] pub struct Token { pub fragment: String, @@ -34,9 +56,13 @@ pub struct Token { } impl Token { - pub fn new(f: &str, pos: Position) -> Self { + pub fn new>(f: S, line: usize, col: usize) -> Self { + Self::new_with_pos(f, Position::new(line, col)) + } + + pub fn new_with_pos>(f: S, pos: Position) -> Self { Token { - fragment: f.to_string(), + fragment: f.into(), pos: pos, } } @@ -50,7 +76,10 @@ impl Borrow for Token { macro_rules! value_node { ($v:expr, $p:expr) => { - LocatedNode::new($v, $p) + Positioned::new_with_pos($v, $p) + }; + ($v:expr, $l:expr, $c:expr) => { + Positioned::new($v, $l, $c) }; } @@ -58,46 +87,32 @@ pub type FieldList = Vec<(Token, Expression)>; // Token is expected to be a symb pub type SelectorList = Vec; // Token is expected to always be a symbol. #[derive(Debug,PartialEq,Clone)] -pub struct LocatedNode { - // TODO(jwall): Should we just use positioned instead? +pub struct SelectorDef { pub pos: Position, - pub val: T, + pub sel: SelectorList, } -impl LocatedNode { - pub fn new>(v: T, pos: P) -> Self { - Self { - pos: pos.into(), - val: v, +impl SelectorDef { + pub fn new(sel: SelectorList, line: usize, col: usize) -> Self { + SelectorDef { + pos: Position::new(line, col), + sel: sel, } } - - pub fn val(&self) -> &T { - return &self.val; - } -} - - -pub fn make_value_node(v: T, line: usize, column: usize) -> LocatedNode { - LocatedNode::new(v, - Position { - line: line, - column: column, - }) } /// Value represents a Value in the UCG parsed AST. #[derive(Debug,PartialEq,Clone)] pub enum Value { // Constant Values - Int(LocatedNode), - Float(LocatedNode), - String(LocatedNode), - Symbol(LocatedNode), + Int(Positioned), + Float(Positioned), + String(Positioned), + Symbol(Positioned), // Complex Values - Tuple(LocatedNode), + Tuple(Positioned), List(ListDef), - Selector(LocatedNode), + Selector(SelectorDef), } impl Value { @@ -128,7 +143,7 @@ impl Value { fn elems_to_string(v: &Vec) -> String { return format!("{}", v.len()); } - + pub fn to_string(&self) -> String { match self { &Value::Int(ref i) => format!("{}", i.val), @@ -137,7 +152,7 @@ impl Value { &Value::Symbol(ref s) => format!("{}", s.val), &Value::Tuple(ref fs) => format!("{}", Self::fields_to_string(&fs.val)), &Value::List(ref def) => format!("[{}]", Self::elems_to_string(&def.elems)), - &Value::Selector(ref v) => v.val.join("."), + &Value::Selector(ref v) => v.sel.join("."), } } @@ -152,13 +167,23 @@ impl Value { &Value::Selector(ref v) => &v.pos, } } + + pub fn type_equal(&self, target: &Self) -> bool { + enum_type_equality!(self, target, &Value::Int(_), + &Value::Float(_), + &Value::String(_), + &Value::Symbol(_), + &Value::Tuple(_), + &Value::List(_), + &Value::Selector(_)) + } } /// CallDef represents a call to a Macro that is expected to already have been /// defined. #[derive(PartialEq,Debug,Clone)] pub struct CallDef { - pub macroref: SelectorList, + pub macroref: SelectorDef, pub arglist: Vec, pub pos: Position, } @@ -181,7 +206,11 @@ pub struct Positioned { } impl Positioned { - pub fn new(v: T, pos: Position) -> Self { + pub fn new(v: T, l: usize, c: usize) -> Self { + Self::new_with_pos(v, Position::new(l, c)) + } + + pub fn new_with_pos(v: T, pos: Position) -> Self { Positioned { pos: pos, val: v } } } @@ -221,8 +250,8 @@ impl<'a> From<&'a Token> for Positioned { } } -impl<'a> From<&'a LocatedNode> for Positioned { - fn from(t: &LocatedNode) -> Positioned { +impl<'a> From<&'a Positioned> for Positioned { + fn from(t: &Positioned) -> Positioned { Positioned { pos: t.pos.clone(), val: t.val.clone(), @@ -261,7 +290,7 @@ impl MacroDef { bad_symbols.insert(name.val.clone()); } } else if let &Value::Selector(ref sel_node) = val { - let list = &sel_node.val; + let list = &sel_node.sel; if list.len() > 0 { // We only look to see if the first selector item exists. // This is because only the first one is a symbol all of the @@ -362,7 +391,7 @@ pub struct BinaryOpDef { #[derive(Debug,PartialEq,Clone)] pub struct CopyDef { - pub selector: SelectorList, + pub selector: SelectorDef, pub fields: FieldList, pub pos: Position, } @@ -432,23 +461,16 @@ mod ast_test { #[test] pub fn test_macro_validation_happy_path() { let def = MacroDef { - argdefs: vec![Positioned::new("foo".to_string(), - Position { - line: 1, - column: 0, - })], + argdefs: vec![value_node!("foo".to_string(), 1, 0)], fields: vec![ - (Token::new("f1", Position { line: 1, column: 1}), Expression::Binary(BinaryOpDef{ + (Token::new("f1", 1, 1), Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Symbol(make_value_node("foo".to_string(), 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Symbol(value_node!("foo".to_string(), 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))), + pos: Position::new(1, 0), })), ], - pos: Position { - line: 1, - column: 0, - }, + pos: Position::new(1, 0), }; assert!(def.validate_symbols().unwrap() == ()); } @@ -456,23 +478,16 @@ mod ast_test { #[test] pub fn test_macro_validation_fail() { let def = MacroDef { - argdefs: vec![Positioned::new("foo".to_string(), - Position { - line: 1, - column: 0, - })], + argdefs: vec![value_node!("foo".to_string(), 1, 0)], fields: vec![ - (Token::new("f1", Position{line: 1, column: 1}), Expression::Binary(BinaryOpDef{ + (Token::new("f1", 1, 1), Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Symbol(make_value_node("bar".to_string(), 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Symbol(value_node!("bar".to_string(), 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))), + pos: Position::new(1, 0), })), ], - pos: Position { - line: 1, - column: 0, - }, + pos: Position::new(1, 0), }; let mut expected = HashSet::new(); expected.insert("bar".to_string()); @@ -482,25 +497,18 @@ mod ast_test { #[test] pub fn test_macro_validation_selector_happy_path() { let def = MacroDef { - argdefs: vec![Positioned::new("foo".to_string(), - Position { - line: 1, - column: 0, - })], + argdefs: vec![value_node!("foo".to_string(), 1, 0)], fields: vec![ - (Token::new("f1", Position{line: 1, column: 1}), Expression::Binary(BinaryOpDef{ + (Token::new("f1", 1, 1), Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Selector(make_value_node(vec![ - Token::new("foo", Position{line: 1, column: 1}), - Token::new("quux", Position{line: 1, column: 1})], 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Selector(SelectorDef::new(vec![ + Token::new("foo", 1, 1), + Token::new("quux", 1, 1)], 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))), + pos: Position::new(1, 0), })), ], - pos: Position { - line: 1, - column: 0, - }, + pos: Position::new(1, 0), }; assert!(def.validate_symbols().unwrap() == ()); } @@ -508,21 +516,18 @@ mod ast_test { #[test] pub fn test_macro_validation_selector_fail() { let def = MacroDef { - argdefs: vec![Positioned::new("foo".to_string(), Position {line: 1, column: 0})], + argdefs: vec![value_node!("foo".to_string(), 1, 0)], fields: vec![ - (Token::new("f1", Position{line: 1, column: 1}), Expression::Binary(BinaryOpDef{ + (Token::new("f1", 1, 1), Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Selector(make_value_node(vec![ - Token::new("bar", Position{line: 1, column: 1}), - Token::new("quux", Position{line: 1, column: 1})], 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Selector(SelectorDef::new(vec![ + Token::new("bar", 1, 1), + Token::new("quux", 1, 1)], 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))), + pos: Position::new(1, 0), })), ], - pos: Position { - line: 1, - column: 0, - }, + pos: Position::new(1, 0), }; let mut expected = HashSet::new(); expected.insert("bar".to_string()); diff --git a/src/build.rs b/src/build.rs index d13a64a..687c3aa 100644 --- a/src/build.rs +++ b/src/build.rs @@ -124,50 +124,12 @@ impl Val { } pub fn type_equal(&self, target: &Self) -> bool { - match self { - &Val::Int(_) => { - if let &Val::Int(_) = target { - true - } else { - false - } - } - &Val::Float(_) => { - if let &Val::Float(_) = target { - true - } else { - false - } - } - &Val::String(_) => { - if let &Val::String(_) = target { - true - } else { - false - } - } - &Val::List(_) => { - if let &Val::List(_) = target { - true - } else { - false - } - } - &Val::Tuple(_) => { - if let &Val::Tuple(_) = target { - true - } else { - false - } - } - &Val::Macro(_) => { - if let &Val::Macro(_) = target { - true - } else { - false - } - } - } + enum_type_equality!(self, target, &Val::Int(_), + &Val::Float(_), + &Val::String(_), + &Val::List(_), + &Val::Tuple(_), + &Val::Macro(_)) } pub fn get_fields(&self) -> Option<&Vec<(Positioned, Rc)>> { @@ -284,7 +246,7 @@ impl Builder { return Ok(Rc::new(Val::List(vals))); } &Value::Tuple(ref tuple_node) => { - let fields = tuple_node.val(); + let fields = &tuple_node.val; let mut new_fields = Vec::<(Positioned, Rc)>::new(); for &(ref name, ref expr) in fields.iter() { let val = try!(self.eval_expr(expr)); @@ -294,7 +256,7 @@ impl Builder { Ok(Rc::new(Val::Tuple(new_fields))) } &Value::Selector(ref selector_list_node) => { - self.lookup_selector(&selector_list_node.val) + self.lookup_selector(&selector_list_node.sel) } } } @@ -319,10 +281,7 @@ impl Builder { pub fn get_out_by_name(&self, name: &str) -> Option> { let key = Positioned { - pos: Position { - line: 0, - column: 0, - }, + pos: Position::new(0, 0), val: name.to_string(), }; self.lookup_sym(&key) @@ -362,7 +321,7 @@ impl Builder { fn build_stmt(&mut self, stmt: &Statement) -> BuildResult { match stmt { - &Statement::Let(LetDef{ name: ref sym, value: ref expr }) => { + &Statement::Let(LetDef { name: ref sym, value: ref expr }) => { let val = try!(self.eval_expr(expr)); self.last = Some(val.clone()); match self.out.entry(sym.into()) { @@ -377,7 +336,7 @@ impl Builder { } } } - &Statement::Import(ImportDef{ path: ref val, name: ref sym }) => { + &Statement::Import(ImportDef { path: ref val, name: ref sym }) => { if !self.files.contains(&val.fragment) { // Only parse the file once on import. let positioned_sym = sym.into(); @@ -612,7 +571,7 @@ impl Builder { } } &Expression::Copy(ref def) => { - let v = try!(self.lookup_selector(&def.selector)); + let v = try!(self.lookup_selector(&def.selector.sel)); if let Val::Tuple(ref src_fields) = *v { let mut m = HashMap::, Rc>::new(); // loop through fields and build up a hahsmap @@ -671,7 +630,7 @@ impl Builder { &Expression::Call(ref def) => { let sel = &def.macroref; let args = &def.arglist; - let v = try!(self.lookup_selector(sel)); + let v = try!(self.lookup_selector(&sel.sel)); if let &Val::Macro(ref m) = v.deref() { // Congratulations this is actually a macro. let mut argvals: Vec> = Vec::new(); @@ -740,17 +699,17 @@ mod test { (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Div, - left: Value::Int(make_value_node(2, 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(2, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Int(value_node!(2, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(2, 1, 1)))), + pos: Position::new(1, 0), }), Val::Int(1)), (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Div, - left: Value::Float(make_value_node(2.0, 1, 1)), - right: Box::new(Expression::Simple(Value::Float(make_value_node(2.0, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Float(value_node!(2.0, 1, 1)), + right: Box::new(Expression::Simple(Value::Float(value_node!(2.0, 1, 1)))), + pos: Position::new(1, 0), }), Val::Float(1.0)), ], @@ -765,9 +724,9 @@ mod test { (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Div, - left: Value::Float(make_value_node(2.0, 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(2, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Float(value_node!(2.0, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(2, 1, 1)))), + pos: Position::new(1, 0), }), Val::Float(1.0)), ], @@ -781,17 +740,17 @@ mod test { (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Mul, - left: Value::Int(make_value_node(2, 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(2, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Int(value_node!(2, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(2, 1, 1)))), + pos: Position::new(1, 0), }), Val::Int(4)), (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Mul, - left: Value::Float(make_value_node(2.0, 1, 1)), - right: Box::new(Expression::Simple(Value::Float(make_value_node(2.0, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Float(value_node!(2.0, 1, 1)), + right: Box::new(Expression::Simple(Value::Float(value_node!(2.0, 1, 1)))), + pos: Position::new(1, 0), }), Val::Float(4.0)), ], @@ -806,9 +765,9 @@ mod test { (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Mul, - left: Value::Float(make_value_node(2.0, 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(20, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Float(value_node!(2.0, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(20, 1, 1)))), + pos: Position::new(1, 0), }), Val::Float(1.0)), ], @@ -822,17 +781,17 @@ mod test { (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Sub, - left: Value::Int(make_value_node(2, 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Int(value_node!(2, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))), + pos: Position::new(1, 0), }), Val::Int(1)), (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Sub, - left: Value::Float(make_value_node(2.0, 1, 1)), - right: Box::new(Expression::Simple(Value::Float(make_value_node(1.0, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Float(value_node!(2.0, 1, 1)), + right: Box::new(Expression::Simple(Value::Float(value_node!(1.0, 1, 1)))), + pos: Position::new(1, 0), }), Val::Float(1.0)), ], @@ -847,9 +806,9 @@ mod test { (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Sub, - left: Value::Float(make_value_node(2.0, 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(2, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Float(value_node!(2.0, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(2, 1, 1)))), + pos: Position::new(1, 0), }), Val::Float(1.0)), ], @@ -863,25 +822,25 @@ mod test { (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Int(make_value_node(1, 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Int(value_node!(1, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))), + pos: Position::new(1, 0), }), Val::Int(2)), (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Float(make_value_node(1.0, 1, 1)), - right: Box::new(Expression::Simple(Value::Float(make_value_node(1.0, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Float(value_node!(1.0, 1, 1)), + right: Box::new(Expression::Simple(Value::Float(value_node!(1.0, 1, 1)))), + pos: Position::new(1, 0), }), Val::Float(2.0)), (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::String(make_value_node("foo".to_string(), 1, 1)), - right: Box::new(Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::String(value_node!("foo".to_string(), 1, 1)), + right: Box::new(Expression::Simple(Value::String(value_node!("bar".to_string(), 1, 1)))), + pos: Position::new(1, 0), }), Val::String("foobar".to_string())), (Expression::Binary( @@ -889,15 +848,15 @@ mod test { kind: BinaryExprType::Add, left: Value::List( ListDef{ - elems: vec![Expression::Simple(Value::String(make_value_node("foo".to_string(), 1, 1)))], - pos: Position{line: 1, column: 1}, + elems: vec![Expression::Simple(Value::String(value_node!("foo".to_string(), 1, 1)))], + pos: Position::new(1, 1), }), right: Box::new(Expression::Simple(Value::List( ListDef{ - elems: vec![Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1)))], - pos: Position{line: 1, column: 1}, + elems: vec![Expression::Simple(Value::String(value_node!("bar".to_string(), 1, 1)))], + pos: Position::new(1, 1), }))), - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }), Val::List(vec![Rc::new(Val::String("foo".to_string())), Rc::new(Val::String("bar".to_string()))])), @@ -912,9 +871,9 @@ mod test { (Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Float(make_value_node(2.0, 1, 1)), - right: Box::new(Expression::Simple(Value::Int(make_value_node(2, 1, 1)))), - pos: Position{line: 1, column: 0}, + left: Value::Float(value_node!(2.0, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(2, 1, 1)))), + pos: Position::new(1, 0), }), Val::Float(1.0)), ], @@ -924,14 +883,14 @@ mod test { #[test] fn test_eval_simple_expr() { test_expr_to_val(vec![ - (Expression::Simple(Value::Int(make_value_node(1, 1, 1))), Val::Int(1)), - (Expression::Simple(Value::Float(make_value_node(2.0, 1, 1))), Val::Float(2.0)), - (Expression::Simple(Value::String(make_value_node("foo".to_string(), 1, 1))), + (Expression::Simple(Value::Int(value_node!(1, 1, 1))), Val::Int(1)), + (Expression::Simple(Value::Float(value_node!(2.0, 1, 1))), Val::Float(2.0)), + (Expression::Simple(Value::String(value_node!("foo".to_string(), 1, 1))), Val::String("foo".to_string())), - (Expression::Simple(Value::Tuple(make_value_node(vec![ - (Token::new("bar", Position{line: 1, column: 1}), Expression::Simple(Value::Int(make_value_node(1, 1, 1)))) + (Expression::Simple(Value::Tuple(value_node!(vec![ + (Token::new("bar", 1, 1), Expression::Simple(Value::Int(value_node!(1, 1, 1)))) ], 1, 1))), - Val::Tuple(vec![(Positioned::new("bar".to_string(), Position{line: 1, column: 1}), + Val::Tuple(vec![(value_node!("bar".to_string(), 1, 1), Rc::new(Val::Int(1)))])), ], Builder::new()); @@ -941,14 +900,10 @@ mod test { fn test_eval_simple_lookup_expr() { let mut b = Builder::new(); b.out - .entry(Positioned::new("var1".to_string(), - Position { - line: 1, - column: 0, - })) + .entry(value_node!("var1".to_string(), 1, 0)) .or_insert(Rc::new(Val::Int(1))); test_expr_to_val(vec![ - (Expression::Simple(Value::Symbol(make_value_node("var1".to_string(), 1, 1))), Val::Int(1)), + (Expression::Simple(Value::Symbol(value_node!("var1".to_string(), 1, 1))), Val::Int(1)), ], b); } @@ -957,68 +912,56 @@ mod test { fn test_eval_simple_lookup_error() { let mut b = Builder::new(); b.out - .entry(Positioned::new("var1".to_string(), - Position { - line: 1, - column: 0, - })) + .entry(value_node!("var1".to_string(), 1, 0)) .or_insert(Rc::new(Val::Int(1))); - let expr = Expression::Simple(Value::Symbol(make_value_node("var".to_string(), 1, 1))); + let expr = Expression::Simple(Value::Symbol(value_node!("var".to_string(), 1, 1))); assert!(b.eval_expr(&expr).is_err()); } #[test] fn test_eval_selector_expr() { let mut b = Builder::new(); - b.out.entry(Positioned::new("var1".to_string(), Position{line: 1, column: 0})).or_insert(Rc::new(Val::Tuple(vec![ - (Positioned::new("lvl1".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Tuple( + b.out.entry(value_node!("var1".to_string(), 1, 0)).or_insert(Rc::new(Val::Tuple(vec![ + (value_node!("lvl1".to_string(), 1, 0), Rc::new(Val::Tuple( vec![ - (Positioned::new("lvl2".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(3))), + (value_node!("lvl2".to_string(), 1, 0), Rc::new(Val::Int(3))), ] ))), ]))); b.out - .entry(Positioned::new("var2".to_string(), - Position { - line: 1, - column: 0, - })) + .entry(value_node!("var2".to_string(), 1, 0)) .or_insert(Rc::new(Val::Int(2))); b.out - .entry(Positioned::new("var3".to_string(), - Position { - line: 1, - column: 0, - })) - .or_insert(Rc::new(Val::Tuple(vec![(Positioned::new("lvl1".to_string(), - Position {line: 1, column: 0}), + .entry(value_node!("var3".to_string(), 1, 0)) + .or_insert(Rc::new(Val::Tuple(vec![(value_node!("lvl1".to_string(), + 1, 0), Rc::new(Val::Int(4)))]))); test_expr_to_val(vec![ - (Expression::Simple(Value::Selector(make_value_node(vec![Token::new("var1", Position{line: 1, column: 1})], 1, 1))), Val::Tuple( + (Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("var1", 1, 1)], 1, 1))), Val::Tuple( vec![ - (Positioned::new("lvl1".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Tuple( + (value_node!("lvl1".to_string(), 1, 0), Rc::new(Val::Tuple( vec![ - (Positioned::new("lvl2".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(3))), + (value_node!("lvl2".to_string(), 1, 0), Rc::new(Val::Int(3))), ] ))), ] )), - (Expression::Simple(Value::Selector(make_value_node(vec![Token::new("var1", Position{line: 1, column: 1}), - Token::new("lvl1", Position{line: 1, column: 1})], 1, 1))), + (Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("var1", 1, 1), + Token::new("lvl1", 1, 1)], 1, 1))), Val::Tuple( vec![ - (Positioned::new("lvl2".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(3))), + (value_node!("lvl2".to_string(), 1, 0), Rc::new(Val::Int(3))), ] )), - (Expression::Simple(Value::Selector(make_value_node(vec![Token::new("var1", Position{line: 1, column: 1}), - Token::new("lvl1", Position{line: 1, column: 1}), - Token::new("lvl2", Position{line: 1, column: 1})], 1, 1))), + (Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("var1", 1, 1), + Token::new("lvl1", 1, 1), + Token::new("lvl2", 1, 1)], 1, 1))), Val::Int(3)), - (Expression::Simple(Value::Selector(make_value_node(vec![Token::new("var2", Position{line: 1, column: 1})], 1, 1))), + (Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("var2", 1, 1)], 1, 1))), Val::Int(2)), - (Expression::Simple(Value::Selector(make_value_node(vec![Token::new("var3", Position{line: 1, column: 1}), - Token::new("lvl1", Position{line: 1, column: 1})], 1, 1))), + (Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("var3", 1, 1), + Token::new("lvl1", 1, 1)], 1, 1))), Val::Int(4)), ], b); } @@ -1027,24 +970,20 @@ mod test { fn test_eval_selector_list_expr() { let mut b = Builder::new(); b.out - .entry(Positioned::new("var1".to_string(), - Position { - line: 1, - column: 1, - })) + .entry(value_node!("var1".to_string(), 1, 1)) .or_insert(Rc::new(Val::List(vec![ Rc::new(Val::String("val1".to_string())), Rc::new(Val::Tuple(vec![ - (Positioned::new("var2".to_string(), Position{line: 1, column: 1}), + (value_node!("var2".to_string(), 1, 1), Rc::new(Val::Int(1))), ])), ]))); // TODO(jwall): Assert that we can index into lists using dot syntax. test_expr_to_val(vec![ - (Expression::Simple(Value::Selector(make_value_node(vec![ - Token::new("var1", Position{line: 1, column: 1}), - Token::new("0", Position{line: 1, column: 1}) + (Expression::Simple(Value::Selector(SelectorDef::new(vec![ + Token::new("var1", 1, 1), + Token::new("0", 1, 1) ], 1, 1))), Val::String("val1".to_string())) ], @@ -1056,9 +995,12 @@ mod test { fn test_expr_copy_no_such_tuple() { let b = Builder::new(); test_expr_to_val(vec![ - (Expression::Copy(CopyDef{selector: vec![Token::new("tpl1", Position{line: 1, column: 1})], fields: Vec::new(), pos: Position{line: 1, column: 0}}), + (Expression::Copy(CopyDef{ + selector: SelectorDef::new(vec![Token::new("tpl1", 1, 1)], 1, 1), + fields: Vec::new(), pos: Position::new(1, 0)}), Val::Tuple(Vec::new())), - ], b); + ], + b); } #[test] @@ -1066,35 +1008,34 @@ mod test { fn test_expr_copy_not_a_tuple() { let mut b = Builder::new(); b.out - .entry(Positioned::new("tpl1".to_string(), - Position { - line: 1, - column: 0, - })) + .entry(value_node!("tpl1".to_string(), 1, 0)) .or_insert(Rc::new(Val::Int(1))); test_expr_to_val(vec![ - (Expression::Copy(CopyDef{selector: vec![Token::new("tpl1", Position{line: 1, column: 1})], fields: Vec::new(), pos: Position{line: 1, column: 0}}), + (Expression::Copy(CopyDef{ + selector: SelectorDef::new(vec![Token::new("tpl1", 1, 1)], 1, 1), + fields: Vec::new(), pos: Position::new(1, 0)}), Val::Tuple(Vec::new())), - ], b); + ], + b); } #[test] #[should_panic(expected = "Expected type Integer for field fld1 but got String")] fn test_expr_copy_field_type_error() { let mut b = Builder::new(); - b.out.entry(Positioned::new("tpl1".to_string(), Position{line: 1, column: 0})).or_insert(Rc::new(Val::Tuple(vec![ - (Positioned::new("fld1".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(1))), + b.out.entry(value_node!("tpl1".to_string(), 1, 0)).or_insert(Rc::new(Val::Tuple(vec![ + (value_node!("fld1".to_string(), 1, 0), Rc::new(Val::Int(1))), ]))); test_expr_to_val(vec![ (Expression::Copy( CopyDef{ - selector: vec![Token::new("tpl1", Position{line: 1, column: 1})], - fields: vec![(Token::new("fld1", Position{line: 1, column: 1}), - Expression::Simple(Value::String(make_value_node("2".to_string(), 1, 1))))], - pos: Position{line: 1, column: 0}}), + selector: SelectorDef::new(vec![Token::new("tpl1", 1, 1)], 1, 1), + fields: vec![(Token::new("fld1", 1, 1), + Expression::Simple(Value::String(value_node!("2".to_string(), 1, 1))))], + pos: Position::new(1, 0)}), Val::Tuple( vec![ - (Positioned::new("fld1".to_string(), Position{line: 1, column: 1}), Rc::new(Val::String("2".to_string()))), + (value_node!("fld1".to_string(), 1, 1), Rc::new(Val::String("2".to_string()))), ], )), ], b); @@ -1103,16 +1044,16 @@ mod test { #[test] fn test_expr_copy() { let mut b = Builder::new(); - b.out.entry(Positioned::new("tpl1".to_string(), Position{line: 1, column: 0})).or_insert(Rc::new(Val::Tuple(vec![ - (Positioned::new("fld1".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(1))), + b.out.entry(value_node!("tpl1".to_string(), 1, 0)).or_insert(Rc::new(Val::Tuple(vec![ + (value_node!("fld1".to_string(), 1, 0), Rc::new(Val::Int(1))), ]))); test_expr_to_val(vec![ (Expression::Copy( CopyDef{ - selector: vec![Token::new("tpl1", Position{line: 1, column: 1})], - fields: vec![(Token::new("fld2", Position{line: 1, column: 1}), - Expression::Simple(Value::String(make_value_node("2".to_string(), 1, 1))))], - pos: Position{line: 1, column: 0}, + selector: SelectorDef::new(vec![Token::new("tpl1", 1, 1)], 1, 1), + fields: vec![(Token::new("fld2", 1, 1), + Expression::Simple(Value::String(value_node!("2".to_string(), 1, 1))))], + pos: Position::new(1, 0), }), // Add a new field to the copy Val::Tuple( @@ -1120,33 +1061,33 @@ mod test { // that the compare assertion is correct. The ordering has no // semantics though so at some point we should probably be less restrictive. vec![ - (Positioned::new("fld1".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(1))), - (Positioned::new("fld2".to_string(), Position{line: 1, column: 1}), Rc::new(Val::String("2".to_string()))), + (value_node!("fld1".to_string(), 1, 0), Rc::new(Val::Int(1))), + (value_node!("fld2".to_string(), 1, 1), Rc::new(Val::String("2".to_string()))), ], )), // Overwrite a field in the copy (Expression::Copy( CopyDef{ - selector: vec![Token::new("tpl1", Position{line: 1, column: 1})], + selector: SelectorDef::new(vec![Token::new("tpl1", 1, 1)], 1, 1), fields: vec![ - (Token::new("fld1", Position{line: 1, column: 1}), - Expression::Simple(Value::Int(make_value_node(3, 1, 1)))), - (Token::new("fld2", Position{line: 1, column: 1}), - Expression::Simple(Value::String(make_value_node("2".to_string(), 1, 1)))), + (Token::new("fld1", 1, 1), + Expression::Simple(Value::Int(value_node!(3, 1, 1)))), + (Token::new("fld2", 1, 1), + Expression::Simple(Value::String(value_node!("2".to_string(), 1, 1)))), ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }), Val::Tuple( vec![ - (Positioned::new("fld1".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(3))), - (Positioned::new("fld2".to_string(), Position{line: 1, column: 0}), Rc::new(Val::String("2".to_string()))), + (value_node!("fld1".to_string(), 1, 0), Rc::new(Val::Int(3))), + (value_node!("fld2".to_string(), 1, 0), Rc::new(Val::String("2".to_string()))), ], )), // The source tuple is still unmodified. - (Expression::Simple(Value::Selector(make_value_node(vec![Token::new("tpl1", Position{line: 1, column: 1})], 1, 1))), + (Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("tpl1", 1, 1)], 1, 1))), Val::Tuple( vec![ - (Positioned::new("fld1".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(1))), + (value_node!("fld1".to_string(), 1, 0), Rc::new(Val::Int(1))), ], )), ], b); @@ -1155,21 +1096,21 @@ mod test { #[test] fn test_macro_call() { let mut b = Builder::new(); - b.out.entry(Positioned::new("tstmac".to_string(), Position{line: 1, column: 0})).or_insert(Rc::new(Val::Macro(MacroDef{ - argdefs: vec![Positioned::new("arg1".to_string(), Position{line: 1, column: 0})], + b.out.entry(value_node!("tstmac".to_string(), 1, 0)).or_insert(Rc::new(Val::Macro(MacroDef{ + argdefs: vec![value_node!("arg1".to_string(), 1, 0)], fields: vec![ - (Token::new("foo", Position{line: 1, column: 1}), Expression::Simple(Value::Symbol(make_value_node("arg1".to_string(), 1, 1)))), + (Token::new("foo", 1, 1), Expression::Simple(Value::Symbol(value_node!("arg1".to_string(), 1, 1)))), ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }))); test_expr_to_val(vec![ (Expression::Call(CallDef{ - macroref: vec![Token::new("tstmac", Position{line: 1, column: 1})], - arglist: vec![Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1)))], - pos: Position{line: 1, column: 0}, + macroref: SelectorDef::new(vec![Token::new("tstmac", 1, 1)], 1, 1), + arglist: vec![Expression::Simple(Value::String(value_node!("bar".to_string(), 1, 1)))], + pos: Position::new(1, 0), }), Val::Tuple(vec![ - (Positioned::new("foo".to_string(), Position{line: 1, column: 1}), + (value_node!("foo".to_string(), 1, 1), Rc::new(Val::String("bar".to_string()))), ])), ], b); @@ -1180,27 +1121,23 @@ mod test { fn test_macro_hermetic() { let mut b = Builder::new(); b.out - .entry(Positioned::new("arg1".to_string(), - Position { - line: 1, - column: 0, - })) + .entry(value_node!("arg1".to_string(), 1, 0)) .or_insert(Rc::new(Val::String("bar".to_string()))); - b.out.entry(Positioned::new("tstmac".to_string(), Position{line: 1, column: 0})).or_insert(Rc::new(Val::Macro(MacroDef{ - argdefs: vec![Positioned::new("arg2".to_string(), Position{line: 1, column: 0})], + b.out.entry(value_node!("tstmac".to_string(), 1, 0)).or_insert(Rc::new(Val::Macro(MacroDef{ + argdefs: vec![value_node!("arg2".to_string(), 1, 0)], fields: vec![ - (Token::new("foo", Position{line: 1, column: 1}), Expression::Simple(Value::Symbol(make_value_node("arg1".to_string(), 1, 1)))), + (Token::new("foo", 1, 1), Expression::Simple(Value::Symbol(value_node!("arg1".to_string(), 1, 1)))), ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }))); test_expr_to_val(vec![ (Expression::Call(CallDef{ - macroref: vec![Token::new("tstmac", Position{line: 1, column: 1})], - arglist: vec![Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1)))], - pos: Position{line: 1, column: 1}, + macroref: SelectorDef::new(vec![Token::new("tstmac", 1, 1)], 1, 1), + arglist: vec![Expression::Simple(Value::String(value_node!("bar".to_string(), 1, 1)))], + pos: Position::new(1, 1), }), Val::Tuple(vec![ - (Positioned::new("foo".to_string(), Position{line: 1, column: 0}), Rc::new(Val::String("bar".to_string()))), + (value_node!("foo".to_string(), 1, 0), Rc::new(Val::String("bar".to_string()))), ])), ], b); } @@ -1209,38 +1146,30 @@ mod test { fn test_select_expr() { let mut b = Builder::new(); b.out - .entry(Positioned::new("foo".to_string(), - Position { - line: 1, - column: 0, - })) + .entry(value_node!("foo".to_string(), 1, 0)) .or_insert(Rc::new(Val::String("bar".to_string()))); b.out - .entry(Positioned::new("baz".to_string(), - Position { - line: 1, - column: 0, - })) + .entry(value_node!("baz".to_string(), 1, 0)) .or_insert(Rc::new(Val::String("boo".to_string()))); test_expr_to_val(vec![ (Expression::Select(SelectDef{ - val: Box::new(Expression::Simple(Value::Symbol(make_value_node("foo".to_string(), 1, 1)))), - default: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))), + val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 1)))), + default: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))), tuple: vec![ - (Token::new("foo", Position{line: 1, column: 1}), Expression::Simple(Value::String(make_value_node("2".to_string(), 1, 1)))), - (Token::new("bar", Position{line: 1, column: 1}), Expression::Simple(Value::Int(make_value_node(2, 1, 1)))), + (Token::new("foo", 1, 1), Expression::Simple(Value::String(value_node!("2".to_string(), 1, 1)))), + (Token::new("bar", 1, 1), Expression::Simple(Value::Int(value_node!(2, 1, 1)))), ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }), Val::Int(2)), (Expression::Select(SelectDef{ - val: Box::new(Expression::Simple(Value::Symbol(make_value_node("baz".to_string(), 1, 1)))), - default: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))), + val: Box::new(Expression::Simple(Value::Symbol(value_node!("baz".to_string(), 1, 1)))), + default: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))), tuple: vec![ - (Token::new("bar", Position{line: 1, column: 1}), Expression::Simple(Value::Int(make_value_node(2, 1, 1)))), - (Token::new("quux", Position{line: 1, column: 1}), Expression::Simple(Value::String(make_value_node("2".to_string(), 1, 1)))), + (Token::new("bar", 1, 1), Expression::Simple(Value::Int(value_node!(2, 1, 1)))), + (Token::new("quux", 1, 1), Expression::Simple(Value::String(value_node!("2".to_string(), 1, 1)))), ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }), // If the field doesn't exist then we get the default. Val::Int(1)), @@ -1252,21 +1181,17 @@ mod test { fn test_select_expr_not_a_string() { let mut b = Builder::new(); b.out - .entry(Positioned::new("foo".to_string(), - Position { - line: 1, - column: 0, - })) + .entry(value_node!("foo".to_string(), 1, 0)) .or_insert(Rc::new(Val::Int(4))); test_expr_to_val(vec![ (Expression::Select(SelectDef{ - val: Box::new(Expression::Simple(Value::Symbol(make_value_node("foo".to_string(), 1, 1)))), - default: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))), + val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 1)))), + default: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))), tuple: vec![ - (Token::new("bar", Position{line: 1, column: 1}), Expression::Simple(Value::Int(make_value_node(2, 1, 1)))), - (Token::new("quux", Position{line: 1, column: 1}), Expression::Simple(Value::String(make_value_node("2".to_string(), 1, 1)))), + (Token::new("bar", 1, 1), Expression::Simple(Value::Int(value_node!(2, 1, 1)))), + (Token::new("quux", 1, 1), Expression::Simple(Value::String(value_node!("2".to_string(), 1, 1)))), ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }), Val::Int(2)), ], b); @@ -1276,16 +1201,12 @@ mod test { fn test_let_statement() { let mut b = Builder::new(); let stmt = Statement::Let(LetDef { - name: Token::new("foo", - Position { - line: 1, - column: 1, - }), - value: Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1))), + name: Token::new("foo", 1, 1), + value: Expression::Simple(Value::String(value_node!("bar".to_string(), 1, 1))), }); b.build_stmt(&stmt).unwrap(); test_expr_to_val(vec![ - (Expression::Simple(Value::Symbol(make_value_node("foo".to_string(), 1, 1))), + (Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 1))), Val::String("bar".to_string())), ], b); @@ -1295,27 +1216,23 @@ mod test { fn test_build_file_string() { let mut b = Builder::new(); b.build_file_string("foo.ucg", "let foo = 1;".to_string()).unwrap(); - let key = Positioned::new("foo".to_string(), - Position { - line: 1, - column: 0, - }); + let key = value_node!("foo".to_string(), 1, 0); assert!(b.out.contains_key(&key)); } #[test] fn test_asset_symbol_lookups() { let mut b = Builder::new(); - b.assets.entry(Positioned::new("foo".to_string(), Position{line: 1, column: 0})).or_insert(Rc::new(Val::Tuple(vec![ - (Positioned::new("bar".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Tuple(vec![ - (Positioned::new("quux".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(1))), + b.assets.entry(value_node!("foo".to_string(), 1, 0)).or_insert(Rc::new(Val::Tuple(vec![ + (value_node!("bar".to_string(), 1, 0), Rc::new(Val::Tuple(vec![ + (value_node!("quux".to_string(), 1, 0), Rc::new(Val::Int(1))), ]))), ]))); test_expr_to_val(vec![ - (Expression::Simple(Value::Symbol(make_value_node("foo".to_string(), 1, 1))), + (Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 1))), Val::Tuple(vec![ - (Positioned::new("bar".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Tuple(vec![ - (Positioned::new("quux".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(1))), + (value_node!("bar".to_string(), 1, 0), Rc::new(Val::Tuple(vec![ + (value_node!("quux".to_string(), 1, 0), Rc::new(Val::Int(1))), ]))), ])), ], diff --git a/src/parse.rs b/src/parse.rs index 32a0d26..97f52df 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -59,13 +59,7 @@ named!(quoted_value( Span ) -> Value, // Helper function to make the return types work for down below. fn triple_to_number(v: (Option, Option, Option)) -> ParseResult { let (pref, mut pref_pos) = match v.0 { - None => { - ("", - Position { - line: 0, - column: 0, - }) - } + None => ("", Position::new(0, 0)), Some(ref bs) => (bs.fragment.borrow(), bs.pos.clone()), }; @@ -145,7 +139,7 @@ named!( // Helper function to make the return types work for down below. fn vec_to_tuple(t: (Span, FieldList)) -> ParseResult { - Ok(Value::Tuple(value_node!(t.1, Position{line: t.0.line as usize, column: t.0.offset as usize}))) + Ok(Value::Tuple(value_node!(t.1, t.0.line as usize, t.0.offset as usize))) } named!(field_list( Span ) -> FieldList, @@ -231,10 +225,7 @@ fn tuple_to_binary_expression(tpl: (Span, BinaryExprType, Value, Expression)) kind: tpl.1, left: tpl.2, right: Box::new(tpl.3), - pos: Position { - line: tpl.0.line as usize, - column: tpl.0.offset as usize, - }, + pos: Position::new(tpl.0.line as usize, tpl.0.offset as usize), })) } @@ -298,14 +289,11 @@ named!(selector_list( Span ) -> SelectorList, ) ); -fn tuple_to_copy(t: (Span, SelectorList, FieldList)) -> ParseResult { +fn tuple_to_copy(t: (Span, SelectorDef, FieldList)) -> ParseResult { Ok(Expression::Copy(CopyDef { selector: t.1, fields: t.2, - pos: Position { - line: t.0.line as usize, - column: t.0.offset as usize, - }, + pos: Position::new(t.0.line as usize, t.0.offset as usize), })) } @@ -317,7 +305,7 @@ named!(copy_expression( Span ) -> Expression, lbracetok >> fields: ws!(field_list) >> rbracetok >> - (pos, selector, fields) + (pos, SelectorDef::new(selector, pos.line as usize, pos.offset as usize), fields) ), tuple_to_copy ) @@ -337,10 +325,7 @@ fn tuple_to_macro(mut t: (Span, Vec, Value)) -> ParseResult { }) .collect(), fields: v.val, - pos: Position { - line: t.0.line as usize, - column: t.0.offset as usize, - }, + pos: Position::new(t.0.line as usize, t.0.offset as usize), })) } // TODO(jwall): Show a better version of the unexpected parsed value. @@ -375,10 +360,7 @@ fn tuple_to_select(t: (Span, Expression, Expression, Value)) -> ParseResult Expression, ); fn tuple_to_call(t: (Span, Value, Vec)) -> ParseResult { - if let Value::Selector(sl) = t.1 { + if let Value::Selector(def) = t.1 { Ok(Expression::Call(CallDef { - macroref: sl.val, + macroref: def, arglist: t.2, - pos: Position { - line: t.0.line as usize, - column: t.0.offset as usize, - }, + pos: Position::new(t.0.line as usize, t.0.offset as usize), })) } else { Err(Box::new(ParseError::UnexpectedToken("Selector".to_string(), format!("{:?}", t.0)))) @@ -440,7 +419,7 @@ fn tuple_to_call(t: (Span, Value, Vec)) -> ParseResult { } fn vec_to_selector_value(t: (Span, SelectorList)) -> ParseResult { - Ok(Value::Selector(value_node!(t.1, Position{line: t.0.line as usize, column: t.0.offset as usize}))) + Ok(Value::Selector(SelectorDef::new(t.1, t.0.line as usize, t.0.offset as usize))) } named!(selector_value( Span ) -> Value, @@ -595,13 +574,13 @@ mod test { fn test_symbol_parsing() { assert_eq!(symbol(LocatedSpan::new("foo")), IResult::Done(LocatedSpan{fragment: "", offset: 3, line: 1}, - Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 1}))) ); + Value::Symbol(value_node!("foo".to_string(), 1, 1))) ); assert_eq!(symbol(LocatedSpan::new("foo-bar")), IResult::Done(LocatedSpan{fragment: "", offset: 7, line: 1}, - Value::Symbol(value_node!("foo-bar".to_string(), Position{line: 1, column: 1}))) ); + Value::Symbol(value_node!("foo-bar".to_string(), 1, 1))) ); assert_eq!(symbol(LocatedSpan::new("foo_bar")), IResult::Done(LocatedSpan{fragment: "", offset: 7, line: 1}, - Value::Symbol(value_node!("foo_bar".to_string(), Position{line: 1, column: 1}))) ); + Value::Symbol(value_node!("foo_bar".to_string(), 1, 1))) ); } #[test] @@ -611,15 +590,15 @@ mod test { ); assert_eq!(selector_value(LocatedSpan::new("foo.bar ")), IResult::Done(LocatedSpan{fragment: "", offset: 8, line: 1}, - Value::Selector(value_node!(vec![Token{fragment:"foo".to_string(), pos: Position{line: 1, column: 1}}, - Token{fragment:"bar".to_string(), pos: Position{line: 1, column: 5}}], - Position{line: 1, column: 0}))) + Value::Selector(SelectorDef::new(vec![Token::new("foo".to_string(), 1, 1), + Token::new("bar", 1, 5)], + 1, 0))) ); assert_eq!(selector_value(LocatedSpan::new("foo.bar;")), IResult::Done(LocatedSpan{fragment: ";", offset: 7, line: 1}, - Value::Selector(value_node!(vec![Token{fragment:"foo".to_string(), pos: Position{line: 1, column: 1}}, - Token{fragment:"bar".to_string(), pos: Position{line: 1, column: 5}}], - Position{line: 1, column: 0}))) + Value::Selector(SelectorDef::new(vec![Token{fragment:"foo".to_string(), pos: Position::new(1, 1)}, + Token{fragment:"bar".to_string(), pos: Position::new(1, 5)}], + 1, 0))) ); } @@ -630,12 +609,12 @@ mod test { ); assert_eq!(selector_or_symbol(LocatedSpan::new("foo")), IResult::Done(LocatedSpan{fragment: "", offset: 3, line: 1}, - Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 1}))) ); + Value::Symbol(value_node!("foo".to_string(), 1, 1))) ); assert_eq!(selector_or_symbol(LocatedSpan::new("foo.bar ")), IResult::Done(LocatedSpan{fragment: "", offset: 8, line: 1}, - Value::Selector(value_node!(vec![Token{fragment:"foo".to_string(), pos: Position{line: 1, column: 1}}, - Token{fragment:"bar".to_string(), pos: Position{line: 1, column: 5}}], - Position{line: 1, column: 0}))) + Value::Selector(SelectorDef::new(vec![Token{fragment:"foo".to_string(), pos: Position::new(1, 1)}, + Token{fragment:"bar".to_string(), pos: Position::new(1, 5)}], + 1, 0))) ); } @@ -653,17 +632,11 @@ mod test { Statement::Import(ImportDef{ path: Token{ fragment: "foo".to_string(), - pos: Position { - line: 1, - column: 8, - } + pos: Position::new(1,8) }, name: Token{ fragment: "foo".to_string(), - pos: Position{ - line: 1, - column: 17, - }, + pos: Position::new(1,17), } }) ) @@ -683,12 +656,9 @@ mod test { Statement::Let(LetDef{ name: Token{ fragment: "foo".to_string(), - pos: Position { - line: 1, - column: 5, - }, + pos: Position::new(1,5), }, - value: Expression::Simple(Value::Float(value_node!(1.0, Position{line: 1, column: 11}))) + value: Expression::Simple(Value::Float(value_node!(1.0, 1, 11))) }))); stmt = "1.0;"; let input = LocatedSpan::new(stmt); @@ -700,7 +670,7 @@ mod test { fragment: "", }, Statement::Expression( - Expression::Simple(Value::Float(value_node!(1.0, Position{line: 1, column: 1})))))); + Expression::Simple(Value::Float(value_node!(1.0, 1, 1)))))); } #[test] @@ -720,17 +690,11 @@ mod test { Statement::Import(ImportDef{ path: Token{ fragment: "foo".to_string(), - pos: Position{ - line: 1, - column: 8, - }, + pos: Position::new(1, 8), }, name: Token{ fragment: "foo".to_string(), - pos: Position{ - line: 1, - column: 17, - }, + pos: Position::new(1,17), } }) ) @@ -757,12 +721,9 @@ mod test { }, Statement::Let(LetDef{name: Token{ fragment: "foo".to_string(), - pos: Position{ - line: 1, - column: 5, - }, + pos: Position::new(1,5), }, - value: Expression::Simple(Value::Float(value_node!(1.0, Position{line: 1, column: 11}))) + value: Expression::Simple(Value::Float(value_node!(1.0, 1, 11))) }))); let_stmt = "let foo= 1.0;"; @@ -774,12 +735,9 @@ mod test { }, Statement::Let(LetDef{name: Token{ fragment: "foo".to_string(), - pos: Position{ - line: 1, - column: 5, - } + pos: Position::new(1,5), }, - value: Expression::Simple(Value::Float(value_node!(1.0, Position{line: 1, column: 10})))}))); + value: Expression::Simple(Value::Float(value_node!(1.0, 1, 10)))}))); let_stmt = "let foo =1.0;"; assert_eq!(let_statement(LocatedSpan::new(let_stmt)), IResult::Done(LocatedSpan{ @@ -789,12 +747,9 @@ mod test { }, Statement::Let(LetDef{name: Token{ fragment: "foo".to_string(), - pos: Position{ - line: 1, - column: 5, - } + pos: Position::new(1,5), }, - value: Expression::Simple(Value::Float(value_node!(1.0, Position{line: 1, column: 10})))}))); + value: Expression::Simple(Value::Float(value_node!(1.0, 1, 10)))}))); } #[test] @@ -807,7 +762,7 @@ mod test { line: 1, }, Statement::Expression( - Expression::Simple(Value::Float(value_node!(1.0, Position{line: 1, column: 1})))))); + Expression::Simple(Value::Float(value_node!(1.0, 1, 1)))))); assert_eq!(expression_statement(LocatedSpan::new("1.0 ;")), IResult::Done(LocatedSpan { fragment: "", @@ -815,7 +770,7 @@ mod test { line: 1, }, Statement::Expression( - Expression::Simple(Value::Float(value_node!(1.0, Position{line: 1, column: 1})))))); + Expression::Simple(Value::Float(value_node!(1.0, 1, 1)))))); assert_eq!(expression_statement(LocatedSpan::new(" 1.0;")), IResult::Done(LocatedSpan { fragment: "", @@ -823,7 +778,7 @@ mod test { line: 1, }, Statement::Expression( - Expression::Simple(Value::Float(value_node!(1.0, Position{line: 1, column: 2})))))); + Expression::Simple(Value::Float(value_node!(1.0, 1, 2)))))); assert_eq!(expression_statement(LocatedSpan::new("foo;")), IResult::Done(LocatedSpan { fragment: "", @@ -831,7 +786,7 @@ mod test { line: 1, }, Statement::Expression( - Expression::Simple(Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 1})))))); + Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 1)))))); assert_eq!(expression_statement(LocatedSpan::new("foo ;")), IResult::Done(LocatedSpan { fragment: "", @@ -839,7 +794,7 @@ mod test { line: 1, }, Statement::Expression( - Expression::Simple(Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 1})))))); + Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 1)))))); assert_eq!(expression_statement(LocatedSpan::new(" foo;")), IResult::Done(LocatedSpan { fragment: "", @@ -847,7 +802,7 @@ mod test { line: 1, }, Statement::Expression( - Expression::Simple(Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 2})))))); + Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 2)))))); assert_eq!(expression_statement(LocatedSpan::new("\"foo\";")), IResult::Done(LocatedSpan { fragment: "", @@ -855,7 +810,7 @@ mod test { line: 1, }, Statement::Expression( - Expression::Simple(Value::String(value_node!("foo".to_string(), Position{line: 1, column: 1})))))); + Expression::Simple(Value::String(value_node!("foo".to_string(), 1, 1)))))); assert_eq!(expression_statement(LocatedSpan::new("\"foo\" ;")), IResult::Done(LocatedSpan { fragment: "", @@ -863,7 +818,7 @@ mod test { line: 1, }, Statement::Expression( - Expression::Simple(Value::String(value_node!("foo".to_string(), Position{line: 1, column: 1})))))); + Expression::Simple(Value::String(value_node!("foo".to_string(), 1, 1)))))); assert_eq!(expression_statement(LocatedSpan::new(" \"foo\";")), IResult::Done(LocatedSpan { fragment: "", @@ -871,7 +826,7 @@ mod test { line: 1, }, Statement::Expression( - Expression::Simple(Value::String(value_node!("foo".to_string(), Position{line: 1, column: 2})))))); + Expression::Simple(Value::String(value_node!("foo".to_string(), 1, 2)))))); } #[test] @@ -882,22 +837,22 @@ mod test { offset: 1, line: 1, }, - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 1}))))); + Expression::Simple(Value::Int(value_node!(1, 1, 1))))); assert_eq!(expression(LocatedSpan::new("foo")), IResult::Done(LocatedSpan { fragment: "", offset: 3, line: 1, }, - Expression::Simple(Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 1}))))); + Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 1))))); assert_eq!(expression(LocatedSpan::new("foo.bar ")), IResult::Done(LocatedSpan { fragment: "", offset: 8, line: 1, }, - Expression::Simple(Value::Selector(make_value_node(vec![Token::new("foo", Position{line: 1, column: 1}), - Token::new("bar", Position{line: 1, column: 5})], 1, 0))))); + Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("foo", 1, 1), + Token::new("bar", 1, 5)], 1, 0))))); assert_eq!(expression(LocatedSpan::new("1 + 1")), IResult::Done(LocatedSpan { fragment: "", @@ -906,9 +861,9 @@ mod test { }, Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Int(value_node!(1, Position{line: 1, column: 1})), - right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5})))), - pos: Position { line: 1, column: 0 }, + left: Value::Int(value_node!(1, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 5)))), + pos: Position::new( 1, 0 ), }))); assert_eq!(expression(LocatedSpan::new("1 - 1")), IResult::Done(LocatedSpan { @@ -918,9 +873,9 @@ mod test { }, Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Sub, - left: Value::Int(value_node!(1, Position{line: 1, column: 1})), - right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5})))), - pos: Position { line: 1, column: 0 }, + left: Value::Int(value_node!(1, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 5)))), + pos: Position::new(1, 0), }))); assert_eq!(expression(LocatedSpan::new("1 * 1")), IResult::Done(LocatedSpan { @@ -930,9 +885,9 @@ mod test { }, Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Mul, - left: Value::Int(value_node!(1, Position{line: 1, column: 1})), - right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5})))), - pos: Position { line: 1, column: 0 }, + left: Value::Int(value_node!(1, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 5)))), + pos: Position::new(1, 0), }))); assert_eq!(expression(LocatedSpan::new("1 / 1")), IResult::Done(LocatedSpan { @@ -942,9 +897,9 @@ mod test { }, Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Div, - left: Value::Int(value_node!(1, Position{line: 1, column: 1})), - right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5})))), - pos: Position { line: 1, column: 0 }, + left: Value::Int(value_node!(1, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 5)))), + pos: Position::new(1, 0), }))); assert_eq!(expression(LocatedSpan::new("1+1")), @@ -955,9 +910,9 @@ mod test { }, Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Int(value_node!(1, Position{line: 1, column: 1})), - right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 3})))), - pos: Position { line: 1, column: 0 }, + left: Value::Int(value_node!(1, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 3)))), + pos: Position::new(1, 0), }))); assert_eq!(expression(LocatedSpan::new("1-1")), IResult::Done(LocatedSpan { @@ -967,9 +922,9 @@ mod test { }, Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Sub, - left: Value::Int(value_node!(1, Position{line: 1, column: 1})), - right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 3})))), - pos: Position { line: 1, column: 0 }, + left: Value::Int(value_node!(1, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 3)))), + pos: Position::new(1, 0), }))); assert_eq!(expression(LocatedSpan::new("1*1")), IResult::Done(LocatedSpan { @@ -979,9 +934,9 @@ mod test { }, Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Mul, - left: Value::Int(value_node!(1, Position{line: 1, column: 1})), - right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 3})))), - pos: Position { line: 1, column: 0 }, + left: Value::Int(value_node!(1, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 3)))), + pos: Position::new(1, 0), }))); assert_eq!(expression(LocatedSpan::new("1/1")), IResult::Done(LocatedSpan { @@ -991,9 +946,9 @@ mod test { }, Expression::Binary(BinaryOpDef{ kind: BinaryExprType::Div, - left: Value::Int(value_node!(1, Position{line: 1, column: 1})), - right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 3})))), - pos: Position { line: 1, column: 0 }, + left: Value::Int(value_node!(1, 1, 1)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 3)))), + pos: Position::new(1, 0), }))); let macro_expr = "macro (arg1, arg2) => { foo = arg1 }"; assert_eq!(expression(LocatedSpan::new(macro_expr)), @@ -1004,14 +959,14 @@ mod test { }, Expression::Macro(MacroDef{ argdefs: vec![ - Positioned::new("arg1".to_string(), Position{line: 1, column: 8}), - Positioned::new("arg2".to_string(), Position{line: 1, column: 14}), + value_node!("arg1".to_string(), 1, 8), + value_node!("arg2".to_string(), 1, 14), ], fields: vec![ - (Token::new("foo", Position{line: 1, column: 25}), - Expression::Simple(Value::Symbol(value_node!("arg1".to_string(), Position{line: 1, column: 31})))), + (Token::new("foo", 1, 25), + Expression::Simple(Value::Symbol(value_node!("arg1".to_string(), 1, 31)))), ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }) ) ); @@ -1023,13 +978,13 @@ mod test { line: 1, }, Expression::Select(SelectDef{ - val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 8})))), - default: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 13})))), + val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 8)))), + default: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 13)))), tuple: vec![ - (Token::new("foo", Position{line: 1, column: 18}), - Expression::Simple(Value::Int(value_node!(2, Position{line: 1, column: 24})))) + (Token::new("foo", 1, 18), + Expression::Simple(Value::Int(value_node!(2, 1, 24)))) ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }) ) ); @@ -1041,13 +996,13 @@ mod test { line: 1, }, Expression::Call(CallDef{ - macroref: vec![Token::new("foo", Position{line:1,column: 1}), - Token::new("bar", Position{line:1,column: 5})], + macroref: SelectorDef::new(vec![Token::new("foo", 1,1), + Token::new("bar", 1,5)], 1, 0), arglist: vec![ - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 10}))), - Expression::Simple(Value::String(value_node!("foo".to_string(), Position{line: 1, column: 13}))), + Expression::Simple(Value::Int(value_node!(1, 1, 10))), + Expression::Simple(Value::String(value_node!("foo".to_string(), 1, 13))), ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }) ) ); @@ -1062,9 +1017,9 @@ mod test { Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Int(value_node!(1, Position{line: 1, column: 2})), - right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 6})))), - pos: Position { line: 1, column: 1 }, + left: Value::Int(value_node!(1, 1, 2)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 6)))), + pos: Position::new(1, 1), } ) ) @@ -1076,10 +1031,10 @@ mod test { Expression::Simple(Value::List( ListDef{ elems: vec![ - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 2}))), - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5}))), + Expression::Simple(Value::Int(value_node!(1, 1, 2))), + Expression::Simple(Value::Int(value_node!(1, 1, 5))), ], - pos: Position{line: 1, column: 1}, + pos: Position::new(1, 1), } ) ) @@ -1103,9 +1058,9 @@ mod test { Expression::Format( FormatDef{ template: "foo @ @".to_string(), - args: vec![Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 14}))), - Expression::Simple(Value::Int(value_node!(2, Position{line: 1, column: 17})))], - pos: Position{line: 1, column: 1}, + args: vec![Expression::Simple(Value::Int(value_node!(1, 1, 14))), + Expression::Simple(Value::Int(value_node!(2, 1, 17)))], + pos: Position::new(1, 1), } ) ) @@ -1121,9 +1076,9 @@ mod test { Expression::Format( FormatDef{ template: "foo @ @".to_string(), - args: vec![Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 12}))), - Expression::Simple(Value::Int(value_node!(2, Position{line: 1, column: 15})))], - pos: Position { line: 1, column: 1 }, + args: vec![Expression::Simple(Value::Int(value_node!(1, 1, 12))), + Expression::Simple(Value::Int(value_node!(2, 1, 15)))], + pos: Position::new(1, 1), } ) ) @@ -1147,12 +1102,12 @@ mod test { offset: copy_expr.len(), }, Expression::Call(CallDef{ - macroref: vec![Token::new("foo", Position{line:1, column: 1})], + macroref: SelectorDef::new(vec![Token::new("foo", 1, 1)], 1, 0), arglist: vec![ - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 6}))), - Expression::Simple(Value::String(value_node!("foo".to_string(), Position{line: 1, column: 9}))), + Expression::Simple(Value::Int(value_node!(1, 1, 6))), + Expression::Simple(Value::String(value_node!("foo".to_string(), 1, 9))), ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }) ) ); @@ -1166,13 +1121,13 @@ mod test { offset: copy_expr.len(), }, Expression::Call(CallDef{ - macroref: vec![Token::new("foo", Position{line: 1, column: 1}), - Token::new("bar", Position{line: 1, column: 5})], + macroref: SelectorDef::new(vec![Token::new("foo", 1, 1), + Token::new("bar", 1, 5)], 1, 0), arglist: vec![ - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 10}))), - Expression::Simple(Value::String(value_node!("foo".to_string(), Position{line: 1, column: 13}))), + Expression::Simple(Value::Int(value_node!(1, 1, 10))), + Expression::Simple(Value::String(value_node!("foo".to_string(), 1, 13))), ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }) ) ); @@ -1193,12 +1148,12 @@ mod test { line: 1, }, Expression::Select(SelectDef{ - val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 8})))), - default: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 13})))), + val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 8)))), + default: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 13)))), tuple: vec![ - (Token::new("foo", Position{line: 1, column: 18}), Expression::Simple(Value::Int(value_node!(2, Position{line: 1, column: 24})))) + (Token::new("foo", 1, 18), Expression::Simple(Value::Int(value_node!(2, 1, 24)))) ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }) ) ); @@ -1227,12 +1182,12 @@ mod test { line: 1 }, Expression::Macro(MacroDef{ - argdefs: vec![Positioned::new("arg1".to_string(), Position{line: 1, column: 8}), - Positioned::new("arg2".to_string(), Position{line: 1, column: 14})], - fields: vec![(Token::new("foo", Position{line: 1, column: 24}), Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 28})))), - (Token::new("bar", Position{line: 1, column: 30}), Expression::Simple(Value::Int(value_node!(2, Position{line: 1, column: 34})))) + argdefs: vec![value_node!("arg1".to_string(), 1, 8), + value_node!("arg2".to_string(), 1, 14)], + fields: vec![(Token::new("foo", 1, 24), Expression::Simple(Value::Int(value_node!(1, 1, 28)))), + (Token::new("bar", 1, 30), Expression::Simple(Value::Int(value_node!(2, 1, 34)))) ], - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }) ) ); @@ -1253,9 +1208,9 @@ mod test { line: 1 }, Expression::Copy(CopyDef{ - selector: vec![Token::new("foo", Position{line: 1, column: 1})], + selector: SelectorDef::new(vec![Token::new("foo", 1, 1)], 1, 0), fields: Vec::new(), - pos: Position{line: 1, column: 0}, + pos: Position::new(1, 0), }) ) ); @@ -1269,10 +1224,10 @@ mod test { line: 1 }, Expression::Copy(CopyDef{ - selector: vec![Token::new("foo", Position{line: 1, column: 1})], - fields: vec![(Token::new("bar", Position{line: 1, column: 5}), - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 9}))))], - pos: Position{line: 1, column: 0}, + selector: SelectorDef::new(vec![Token::new("foo", 1, 1)], 1, 0), + fields: vec![(Token::new("bar", 1, 5), + Expression::Simple(Value::Int(value_node!(1, 1, 9))))], + pos: Position::new(1, 0), }) ) ); @@ -1287,7 +1242,7 @@ mod test { Expression::Grouped( Box::new( Expression::Simple( - Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 2})))))) + Value::Symbol(value_node!("foo".to_string(), 1, 2)))))) ); assert_eq!(grouped_expression(LocatedSpan::new("(1 + 1)")), IResult::Done(LocatedSpan{fragment: "", offset: 7, line: 1}, @@ -1296,10 +1251,10 @@ mod test { Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Int(value_node!(1, Position{line: 1, column: 2})), + left: Value::Int(value_node!(1, 1, 2)), right: Box::new(Expression::Simple( - Value::Int(value_node!(1, Position{line: 1, column: 6})))), - pos: Position { line: 1, column: 1 }, + Value::Int(value_node!(1, 1, 6)))), + pos: Position::new(1, 1), } ) ) @@ -1317,9 +1272,9 @@ mod test { Value::List( ListDef{ elems: vec![ - Expression::Simple(Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 2}))) + Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 2))) ], - pos: Position{ line: 1, column: 1} + pos: Position::new(1, 1), } ) ) @@ -1330,10 +1285,10 @@ mod test { Value::List( ListDef{ elems: vec![ - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 2}))), - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5}))), + Expression::Simple(Value::Int(value_node!(1, 1, 2))), + Expression::Simple(Value::Int(value_node!(1, 1, 5))), ], - pos: Position{line: 1, column: 1}, + pos: Position::new(1, 1), } ) ) @@ -1357,7 +1312,7 @@ mod test { line: 1, }, Value::Tuple( - value_node!(vec![], Position{line: 1, column: 0})))); + value_node!(vec![], 1, 0)))); tuple_expr = "{ foo = 1 }"; assert_eq!(tuple(LocatedSpan::new(tuple_expr)), @@ -1368,9 +1323,9 @@ mod test { }, Value::Tuple( value_node!(vec![ - (Token::new("foo", Position{line:1, column: 3}), - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 9})))) - ], Position{line: 1, column: 0})))); + (Token::new("foo", 1, 3), + Expression::Simple(Value::Int(value_node!(1, 1, 9)))) + ], 1, 0)))); tuple_expr = "{ foo = 1, bar = \"1\" }"; assert_eq!(tuple(LocatedSpan::new(tuple_expr)), @@ -1381,11 +1336,11 @@ mod test { }, Value::Tuple( value_node!(vec![ - (Token::new("foo", Position{line: 1, column: 3}), - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 9})))), - (Token::new("bar", Position{line: 1, column: 12}), - Expression::Simple(Value::String(value_node!("1".to_string(), Position{line: 1, column: 18})))) - ], Position{line: 1, column: 0})))); + (Token::new("foo", 1, 3), + Expression::Simple(Value::Int(value_node!(1, 1, 9)))), + (Token::new("bar", 1, 12), + Expression::Simple(Value::String(value_node!("1".to_string(), Position::new(1, 18))))) + ], 1, 0)))); tuple_expr = "{ foo = 1, bar = {} }"; assert_eq!(tuple(LocatedSpan::new(tuple_expr)), IResult::Done(LocatedSpan { @@ -1395,11 +1350,11 @@ mod test { }, Value::Tuple( value_node!(vec![ - (Token::new("foo", Position{line: 1, column: 3}), - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 9})))), - (Token::new("bar", Position{line: 1, column: 12}), - Expression::Simple(Value::Tuple(value_node!(Vec::new(), Position{line: 1, column: 17})))) - ], Position{line: 1, column: 0})))); + (Token::new("foo", 1, 3), + Expression::Simple(Value::Int(value_node!(1, Position::new(1, 9))))), + (Token::new("bar", 1, 12), + Expression::Simple(Value::Tuple(value_node!(Vec::new(), Position::new(1, 17))))) + ], 1, 0)))); } #[test] @@ -1409,24 +1364,24 @@ mod test { assert_eq!(field_value(LocatedSpan::new("foo = 1")), IResult::Done(LocatedSpan { offset: 7, line: 1, fragment: "" }, - (Token::new("foo", Position{line: 1, column: 1}), - Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 7}))))) ); + (Token::new("foo", 1, 1), + Expression::Simple(Value::Int(value_node!(1, 1, 7))))) ); assert_eq!(field_value(LocatedSpan::new("foo = \"1\"")), IResult::Done(LocatedSpan { offset: 9, line: 1, fragment: "" }, - (Token::new("foo", Position{line: 1, column: 1}), - Expression::Simple(Value::String(value_node!("1".to_string(), Position{line: 1, column: 7}))))) ); + (Token::new("foo", 1, 1), + Expression::Simple(Value::String(value_node!("1".to_string(), 1, 7))))) ); assert_eq!(field_value(LocatedSpan::new("foo = bar")), IResult::Done(LocatedSpan { offset: 9, line: 1, fragment: "" }, - (Token::new("foo", Position{line: 1, column: 1}), - Expression::Simple(Value::Symbol(value_node!("bar".to_string(), Position{line: 1, column: 7}))))) ); + (Token::new("foo", 1, 1), + Expression::Simple(Value::Symbol(value_node!("bar".to_string(), 1, 7))))) ); assert_eq!(field_value(LocatedSpan::new("foo = bar ")), IResult::Done(LocatedSpan { offset: 10, line: 1, fragment: "" }, - (Token::new("foo", Position{line: 1, column: 1}), - Expression::Simple(Value::Symbol(value_node!("bar".to_string(), Position{line: 1, column: 7}))))) ); + (Token::new("foo", 1, 1), + Expression::Simple(Value::Symbol(value_node!("bar".to_string(), 1, 7))))) ); assert_eq!(field_value(LocatedSpan::new("foo = bar.baz ")), IResult::Done(LocatedSpan { offset: 14, line: 1, fragment: "" }, - (Token::new("foo", Position{line: 1, column: 1}), - Expression::Simple(Value::Selector(make_value_node(vec![Token::new("bar", Position{line: 1, column: 7}), Token::new("baz", Position{line: 1, column: 11})], 1, 6)))))); + (Token::new("foo", 1, 1), + Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("bar", 1, 7), Token::new("baz", 1, 11)], 1, 6)))))); } #[test] @@ -1435,16 +1390,16 @@ mod test { assert!(number(LocatedSpan::new(". ")).is_err() ); assert_eq!(number(LocatedSpan::new("1.0")), IResult::Done(LocatedSpan{fragment: "", offset: 3, line: 1}, - Value::Float(value_node!(1.0, Position{line: 1, column: 1}))) ); + Value::Float(value_node!(1.0, 1, 1))) ); assert_eq!(number(LocatedSpan::new("1.")), IResult::Done(LocatedSpan{fragment: "", offset: 2, line: 1}, - Value::Float(value_node!(1.0, Position{line: 1, column: 1}))) ); + Value::Float(value_node!(1.0, 1, 1))) ); assert_eq!(number(LocatedSpan::new("1")), IResult::Done(LocatedSpan{fragment: "", offset: 1, line: 1}, - Value::Int(value_node!(1, Position{line: 1, column: 1}))) ); + Value::Int(value_node!(1, 1, 1))) ); assert_eq!(number(LocatedSpan::new(".1")), IResult::Done(LocatedSpan{fragment: "", offset: 2, line: 1}, - Value::Float(value_node!(0.1, Position{line: 1, column: 1}))) ); + Value::Float(value_node!(0.1, 1, 1))) ); } #[test] @@ -1464,36 +1419,27 @@ mod test { Statement::Import(ImportDef{ path: Token{ fragment: "mylib".to_string(), - pos: Position{ - line: 1, - column: 8, - } + pos: Position::new(1, 8), }, name: Token{ fragment: "lib".to_string(), - pos: Position{ - line: 1, - column: 19, - } + pos: Position::new(1, 19), } }), Statement::Let(LetDef{ name: Token{ fragment: "foo".to_string(), - pos: Position{ - line: 1, - column: 27, - } + pos: Position::new(1, 27), }, - value: Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 33}))) + value: Expression::Simple(Value::Int(value_node!(1, 1, 33))) }), Statement::Expression( Expression::Binary( BinaryOpDef{ kind: BinaryExprType::Add, - left: Value::Int(value_node!(1, Position{line: 1, column: 35})), - right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 37})))), - pos: Position { line: 1, column: 34 }, + left: Value::Int(value_node!(1, 1, 35)), + right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 37)))), + pos: Position::new(1, 34), }) ) ]);