mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
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.
This commit is contained in:
parent
091cde9ffb
commit
64e4fb6a28
11
README.md
11
README.md
@ -42,14 +42,16 @@ off of the value assigned.
|
|||||||
|
|
||||||
### Simple Expressions
|
### Simple Expressions
|
||||||
|
|
||||||
UCG supports simple math expressions using `+`, `-`, `*`, `/`) and string
|
UCG supports simple math expressions using `+`, `-`, `*`, `/`) as well as string
|
||||||
concatenation using `+`. The expressions enforce the same type between operands.
|
and list concatenation using `+`. The expressions enforce the same type between operands.
|
||||||
|
|
||||||
1 + 1;
|
1 + 1;
|
||||||
|
|
||||||
1.0 + 1.0;
|
1.0 + 1.0;
|
||||||
|
|
||||||
"foo" + "bar";
|
"foo" + "bar";
|
||||||
|
|
||||||
|
[1, 2] + [3, 4];
|
||||||
|
|
||||||
### String formatting
|
### String formatting
|
||||||
|
|
||||||
@ -58,7 +60,7 @@ shamelessly ripped off from python.
|
|||||||
|
|
||||||
"foo @ @ \@" % (1, "bar")
|
"foo @ @ \@" % (1, "bar")
|
||||||
|
|
||||||
This gets turned into "foo 1 bar {"
|
This gets turned into "foo 1 bar @"
|
||||||
|
|
||||||
### Bindings and Tuples.
|
### 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 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 hosts = ["db1.local.net", "db2.local.net"];
|
||||||
|
|
||||||
let host1 = hosts.0;
|
let host1 = hosts.0;
|
||||||
|
3
TODO.md
3
TODO.md
@ -10,8 +10,9 @@ compiled configuration.
|
|||||||
For some configuration file formats we need a way to specify a particular
|
For some configuration file formats we need a way to specify a particular
|
||||||
organiztion for a given configuration structure. Some options here could be
|
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
|
* A Templating language
|
||||||
|
* Annotations.
|
||||||
|
|
||||||
# Minor Fixes and Polish
|
# Minor Fixes and Polish
|
||||||
|
|
||||||
|
185
src/ast.rs
185
src/ast.rs
@ -21,12 +21,34 @@ use std::cmp::PartialEq;
|
|||||||
use std::hash::Hasher;
|
use std::hash::Hasher;
|
||||||
use std::hash::Hash;
|
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)]
|
#[derive(Debug,PartialEq,Eq,Clone,PartialOrd,Ord,Hash)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
pub line: usize,
|
pub line: usize,
|
||||||
pub column: 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)]
|
#[derive(Debug,PartialEq,Eq,Clone,PartialOrd,Ord,Hash)]
|
||||||
pub struct Token {
|
pub struct Token {
|
||||||
pub fragment: String,
|
pub fragment: String,
|
||||||
@ -34,9 +56,13 @@ pub struct Token {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Token {
|
impl Token {
|
||||||
pub fn new(f: &str, pos: Position) -> Self {
|
pub fn new<S: Into<String>>(f: S, line: usize, col: usize) -> Self {
|
||||||
|
Self::new_with_pos(f, Position::new(line, col))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_with_pos<S: Into<String>>(f: S, pos: Position) -> Self {
|
||||||
Token {
|
Token {
|
||||||
fragment: f.to_string(),
|
fragment: f.into(),
|
||||||
pos: pos,
|
pos: pos,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -50,7 +76,10 @@ impl Borrow<str> for Token {
|
|||||||
|
|
||||||
macro_rules! value_node {
|
macro_rules! value_node {
|
||||||
($v:expr, $p:expr) => {
|
($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>; // Token is expected to always be a symbol.
|
pub type SelectorList = Vec<Token>; // Token is expected to always be a symbol.
|
||||||
|
|
||||||
#[derive(Debug,PartialEq,Clone)]
|
#[derive(Debug,PartialEq,Clone)]
|
||||||
pub struct LocatedNode<T> {
|
pub struct SelectorDef {
|
||||||
// TODO(jwall): Should we just use positioned instead?
|
|
||||||
pub pos: Position,
|
pub pos: Position,
|
||||||
pub val: T,
|
pub sel: SelectorList,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> LocatedNode<T> {
|
impl SelectorDef {
|
||||||
pub fn new<P: Into<Position>>(v: T, pos: P) -> Self {
|
pub fn new(sel: SelectorList, line: usize, col: usize) -> Self {
|
||||||
Self {
|
SelectorDef {
|
||||||
pos: pos.into(),
|
pos: Position::new(line, col),
|
||||||
val: v,
|
sel: sel,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn val(&self) -> &T {
|
|
||||||
return &self.val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn make_value_node<T>(v: T, line: usize, column: usize) -> LocatedNode<T> {
|
|
||||||
LocatedNode::new(v,
|
|
||||||
Position {
|
|
||||||
line: line,
|
|
||||||
column: column,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Value represents a Value in the UCG parsed AST.
|
/// Value represents a Value in the UCG parsed AST.
|
||||||
#[derive(Debug,PartialEq,Clone)]
|
#[derive(Debug,PartialEq,Clone)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
// Constant Values
|
// Constant Values
|
||||||
Int(LocatedNode<i64>),
|
Int(Positioned<i64>),
|
||||||
Float(LocatedNode<f64>),
|
Float(Positioned<f64>),
|
||||||
String(LocatedNode<String>),
|
String(Positioned<String>),
|
||||||
Symbol(LocatedNode<String>),
|
Symbol(Positioned<String>),
|
||||||
// Complex Values
|
// Complex Values
|
||||||
Tuple(LocatedNode<FieldList>),
|
Tuple(Positioned<FieldList>),
|
||||||
List(ListDef),
|
List(ListDef),
|
||||||
Selector(LocatedNode<SelectorList>),
|
Selector(SelectorDef),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
@ -128,7 +143,7 @@ impl Value {
|
|||||||
fn elems_to_string(v: &Vec<Expression>) -> String {
|
fn elems_to_string(v: &Vec<Expression>) -> String {
|
||||||
return format!("{}", v.len());
|
return format!("{}", v.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_string(&self) -> String {
|
pub fn to_string(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
&Value::Int(ref i) => format!("{}", i.val),
|
&Value::Int(ref i) => format!("{}", i.val),
|
||||||
@ -137,7 +152,7 @@ impl Value {
|
|||||||
&Value::Symbol(ref s) => format!("{}", s.val),
|
&Value::Symbol(ref s) => format!("{}", s.val),
|
||||||
&Value::Tuple(ref fs) => format!("{}", Self::fields_to_string(&fs.val)),
|
&Value::Tuple(ref fs) => format!("{}", Self::fields_to_string(&fs.val)),
|
||||||
&Value::List(ref def) => format!("[{}]", Self::elems_to_string(&def.elems)),
|
&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,
|
&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
|
/// CallDef represents a call to a Macro that is expected to already have been
|
||||||
/// defined.
|
/// defined.
|
||||||
#[derive(PartialEq,Debug,Clone)]
|
#[derive(PartialEq,Debug,Clone)]
|
||||||
pub struct CallDef {
|
pub struct CallDef {
|
||||||
pub macroref: SelectorList,
|
pub macroref: SelectorDef,
|
||||||
pub arglist: Vec<Expression>,
|
pub arglist: Vec<Expression>,
|
||||||
pub pos: Position,
|
pub pos: Position,
|
||||||
}
|
}
|
||||||
@ -181,7 +206,11 @@ pub struct Positioned<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Positioned<T> {
|
impl<T> Positioned<T> {
|
||||||
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 }
|
Positioned { pos: pos, val: v }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -221,8 +250,8 @@ impl<'a> From<&'a Token> for Positioned<String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a LocatedNode<String>> for Positioned<String> {
|
impl<'a> From<&'a Positioned<String>> for Positioned<String> {
|
||||||
fn from(t: &LocatedNode<String>) -> Positioned<String> {
|
fn from(t: &Positioned<String>) -> Positioned<String> {
|
||||||
Positioned {
|
Positioned {
|
||||||
pos: t.pos.clone(),
|
pos: t.pos.clone(),
|
||||||
val: t.val.clone(),
|
val: t.val.clone(),
|
||||||
@ -261,7 +290,7 @@ impl MacroDef {
|
|||||||
bad_symbols.insert(name.val.clone());
|
bad_symbols.insert(name.val.clone());
|
||||||
}
|
}
|
||||||
} else if let &Value::Selector(ref sel_node) = val {
|
} else if let &Value::Selector(ref sel_node) = val {
|
||||||
let list = &sel_node.val;
|
let list = &sel_node.sel;
|
||||||
if list.len() > 0 {
|
if list.len() > 0 {
|
||||||
// We only look to see if the first selector item exists.
|
// We only look to see if the first selector item exists.
|
||||||
// This is because only the first one is a symbol all of the
|
// This is because only the first one is a symbol all of the
|
||||||
@ -362,7 +391,7 @@ pub struct BinaryOpDef {
|
|||||||
|
|
||||||
#[derive(Debug,PartialEq,Clone)]
|
#[derive(Debug,PartialEq,Clone)]
|
||||||
pub struct CopyDef {
|
pub struct CopyDef {
|
||||||
pub selector: SelectorList,
|
pub selector: SelectorDef,
|
||||||
pub fields: FieldList,
|
pub fields: FieldList,
|
||||||
pub pos: Position,
|
pub pos: Position,
|
||||||
}
|
}
|
||||||
@ -432,23 +461,16 @@ mod ast_test {
|
|||||||
#[test]
|
#[test]
|
||||||
pub fn test_macro_validation_happy_path() {
|
pub fn test_macro_validation_happy_path() {
|
||||||
let def = MacroDef {
|
let def = MacroDef {
|
||||||
argdefs: vec![Positioned::new("foo".to_string(),
|
argdefs: vec![value_node!("foo".to_string(), 1, 0)],
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
})],
|
|
||||||
fields: vec![
|
fields: vec![
|
||||||
(Token::new("f1", Position { line: 1, column: 1}), Expression::Binary(BinaryOpDef{
|
(Token::new("f1", 1, 1), Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Symbol(make_value_node("foo".to_string(), 1, 1)),
|
left: Value::Symbol(value_node!("foo".to_string(), 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
})),
|
})),
|
||||||
],
|
],
|
||||||
pos: Position {
|
pos: Position::new(1, 0),
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
assert!(def.validate_symbols().unwrap() == ());
|
assert!(def.validate_symbols().unwrap() == ());
|
||||||
}
|
}
|
||||||
@ -456,23 +478,16 @@ mod ast_test {
|
|||||||
#[test]
|
#[test]
|
||||||
pub fn test_macro_validation_fail() {
|
pub fn test_macro_validation_fail() {
|
||||||
let def = MacroDef {
|
let def = MacroDef {
|
||||||
argdefs: vec![Positioned::new("foo".to_string(),
|
argdefs: vec![value_node!("foo".to_string(), 1, 0)],
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
})],
|
|
||||||
fields: vec![
|
fields: vec![
|
||||||
(Token::new("f1", Position{line: 1, column: 1}), Expression::Binary(BinaryOpDef{
|
(Token::new("f1", 1, 1), Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Symbol(make_value_node("bar".to_string(), 1, 1)),
|
left: Value::Symbol(value_node!("bar".to_string(), 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
})),
|
})),
|
||||||
],
|
],
|
||||||
pos: Position {
|
pos: Position::new(1, 0),
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
let mut expected = HashSet::new();
|
let mut expected = HashSet::new();
|
||||||
expected.insert("bar".to_string());
|
expected.insert("bar".to_string());
|
||||||
@ -482,25 +497,18 @@ mod ast_test {
|
|||||||
#[test]
|
#[test]
|
||||||
pub fn test_macro_validation_selector_happy_path() {
|
pub fn test_macro_validation_selector_happy_path() {
|
||||||
let def = MacroDef {
|
let def = MacroDef {
|
||||||
argdefs: vec![Positioned::new("foo".to_string(),
|
argdefs: vec![value_node!("foo".to_string(), 1, 0)],
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
})],
|
|
||||||
fields: vec![
|
fields: vec![
|
||||||
(Token::new("f1", Position{line: 1, column: 1}), Expression::Binary(BinaryOpDef{
|
(Token::new("f1", 1, 1), Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Selector(make_value_node(vec![
|
left: Value::Selector(SelectorDef::new(vec![
|
||||||
Token::new("foo", Position{line: 1, column: 1}),
|
Token::new("foo", 1, 1),
|
||||||
Token::new("quux", Position{line: 1, column: 1})], 1, 1)),
|
Token::new("quux", 1, 1)], 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
})),
|
})),
|
||||||
],
|
],
|
||||||
pos: Position {
|
pos: Position::new(1, 0),
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
assert!(def.validate_symbols().unwrap() == ());
|
assert!(def.validate_symbols().unwrap() == ());
|
||||||
}
|
}
|
||||||
@ -508,21 +516,18 @@ mod ast_test {
|
|||||||
#[test]
|
#[test]
|
||||||
pub fn test_macro_validation_selector_fail() {
|
pub fn test_macro_validation_selector_fail() {
|
||||||
let def = MacroDef {
|
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![
|
fields: vec![
|
||||||
(Token::new("f1", Position{line: 1, column: 1}), Expression::Binary(BinaryOpDef{
|
(Token::new("f1", 1, 1), Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Selector(make_value_node(vec![
|
left: Value::Selector(SelectorDef::new(vec![
|
||||||
Token::new("bar", Position{line: 1, column: 1}),
|
Token::new("bar", 1, 1),
|
||||||
Token::new("quux", Position{line: 1, column: 1})], 1, 1)),
|
Token::new("quux", 1, 1)], 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
})),
|
})),
|
||||||
],
|
],
|
||||||
pos: Position {
|
pos: Position::new(1, 0),
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
let mut expected = HashSet::new();
|
let mut expected = HashSet::new();
|
||||||
expected.insert("bar".to_string());
|
expected.insert("bar".to_string());
|
||||||
|
427
src/build.rs
427
src/build.rs
@ -124,50 +124,12 @@ impl Val {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn type_equal(&self, target: &Self) -> bool {
|
pub fn type_equal(&self, target: &Self) -> bool {
|
||||||
match self {
|
enum_type_equality!(self, target, &Val::Int(_),
|
||||||
&Val::Int(_) => {
|
&Val::Float(_),
|
||||||
if let &Val::Int(_) = target {
|
&Val::String(_),
|
||||||
true
|
&Val::List(_),
|
||||||
} else {
|
&Val::Tuple(_),
|
||||||
false
|
&Val::Macro(_))
|
||||||
}
|
|
||||||
}
|
|
||||||
&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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_fields(&self) -> Option<&Vec<(Positioned<String>, Rc<Val>)>> {
|
pub fn get_fields(&self) -> Option<&Vec<(Positioned<String>, Rc<Val>)>> {
|
||||||
@ -284,7 +246,7 @@ impl Builder {
|
|||||||
return Ok(Rc::new(Val::List(vals)));
|
return Ok(Rc::new(Val::List(vals)));
|
||||||
}
|
}
|
||||||
&Value::Tuple(ref tuple_node) => {
|
&Value::Tuple(ref tuple_node) => {
|
||||||
let fields = tuple_node.val();
|
let fields = &tuple_node.val;
|
||||||
let mut new_fields = Vec::<(Positioned<String>, Rc<Val>)>::new();
|
let mut new_fields = Vec::<(Positioned<String>, Rc<Val>)>::new();
|
||||||
for &(ref name, ref expr) in fields.iter() {
|
for &(ref name, ref expr) in fields.iter() {
|
||||||
let val = try!(self.eval_expr(expr));
|
let val = try!(self.eval_expr(expr));
|
||||||
@ -294,7 +256,7 @@ impl Builder {
|
|||||||
Ok(Rc::new(Val::Tuple(new_fields)))
|
Ok(Rc::new(Val::Tuple(new_fields)))
|
||||||
}
|
}
|
||||||
&Value::Selector(ref selector_list_node) => {
|
&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<Rc<Val>> {
|
pub fn get_out_by_name(&self, name: &str) -> Option<Rc<Val>> {
|
||||||
let key = Positioned {
|
let key = Positioned {
|
||||||
pos: Position {
|
pos: Position::new(0, 0),
|
||||||
line: 0,
|
|
||||||
column: 0,
|
|
||||||
},
|
|
||||||
val: name.to_string(),
|
val: name.to_string(),
|
||||||
};
|
};
|
||||||
self.lookup_sym(&key)
|
self.lookup_sym(&key)
|
||||||
@ -362,7 +321,7 @@ impl Builder {
|
|||||||
|
|
||||||
fn build_stmt(&mut self, stmt: &Statement) -> BuildResult {
|
fn build_stmt(&mut self, stmt: &Statement) -> BuildResult {
|
||||||
match stmt {
|
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));
|
let val = try!(self.eval_expr(expr));
|
||||||
self.last = Some(val.clone());
|
self.last = Some(val.clone());
|
||||||
match self.out.entry(sym.into()) {
|
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) {
|
if !self.files.contains(&val.fragment) {
|
||||||
// Only parse the file once on import.
|
// Only parse the file once on import.
|
||||||
let positioned_sym = sym.into();
|
let positioned_sym = sym.into();
|
||||||
@ -612,7 +571,7 @@ impl Builder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
&Expression::Copy(ref def) => {
|
&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 {
|
if let Val::Tuple(ref src_fields) = *v {
|
||||||
let mut m = HashMap::<Positioned<String>, Rc<Val>>::new();
|
let mut m = HashMap::<Positioned<String>, Rc<Val>>::new();
|
||||||
// loop through fields and build up a hahsmap
|
// loop through fields and build up a hahsmap
|
||||||
@ -671,7 +630,7 @@ impl Builder {
|
|||||||
&Expression::Call(ref def) => {
|
&Expression::Call(ref def) => {
|
||||||
let sel = &def.macroref;
|
let sel = &def.macroref;
|
||||||
let args = &def.arglist;
|
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() {
|
if let &Val::Macro(ref m) = v.deref() {
|
||||||
// Congratulations this is actually a macro.
|
// Congratulations this is actually a macro.
|
||||||
let mut argvals: Vec<Rc<Val>> = Vec::new();
|
let mut argvals: Vec<Rc<Val>> = Vec::new();
|
||||||
@ -740,17 +699,17 @@ mod test {
|
|||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Div,
|
kind: BinaryExprType::Div,
|
||||||
left: Value::Int(make_value_node(2, 1, 1)),
|
left: Value::Int(value_node!(2, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(2, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(2, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Int(1)),
|
Val::Int(1)),
|
||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Div,
|
kind: BinaryExprType::Div,
|
||||||
left: Value::Float(make_value_node(2.0, 1, 1)),
|
left: Value::Float(value_node!(2.0, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Float(make_value_node(2.0, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Float(value_node!(2.0, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Float(1.0)),
|
Val::Float(1.0)),
|
||||||
],
|
],
|
||||||
@ -765,9 +724,9 @@ mod test {
|
|||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Div,
|
kind: BinaryExprType::Div,
|
||||||
left: Value::Float(make_value_node(2.0, 1, 1)),
|
left: Value::Float(value_node!(2.0, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(2, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(2, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Float(1.0)),
|
Val::Float(1.0)),
|
||||||
],
|
],
|
||||||
@ -781,17 +740,17 @@ mod test {
|
|||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Mul,
|
kind: BinaryExprType::Mul,
|
||||||
left: Value::Int(make_value_node(2, 1, 1)),
|
left: Value::Int(value_node!(2, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(2, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(2, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Int(4)),
|
Val::Int(4)),
|
||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Mul,
|
kind: BinaryExprType::Mul,
|
||||||
left: Value::Float(make_value_node(2.0, 1, 1)),
|
left: Value::Float(value_node!(2.0, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Float(make_value_node(2.0, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Float(value_node!(2.0, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Float(4.0)),
|
Val::Float(4.0)),
|
||||||
],
|
],
|
||||||
@ -806,9 +765,9 @@ mod test {
|
|||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Mul,
|
kind: BinaryExprType::Mul,
|
||||||
left: Value::Float(make_value_node(2.0, 1, 1)),
|
left: Value::Float(value_node!(2.0, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(20, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(20, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Float(1.0)),
|
Val::Float(1.0)),
|
||||||
],
|
],
|
||||||
@ -822,17 +781,17 @@ mod test {
|
|||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Sub,
|
kind: BinaryExprType::Sub,
|
||||||
left: Value::Int(make_value_node(2, 1, 1)),
|
left: Value::Int(value_node!(2, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Int(1)),
|
Val::Int(1)),
|
||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Sub,
|
kind: BinaryExprType::Sub,
|
||||||
left: Value::Float(make_value_node(2.0, 1, 1)),
|
left: Value::Float(value_node!(2.0, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Float(make_value_node(1.0, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Float(value_node!(1.0, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Float(1.0)),
|
Val::Float(1.0)),
|
||||||
],
|
],
|
||||||
@ -847,9 +806,9 @@ mod test {
|
|||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Sub,
|
kind: BinaryExprType::Sub,
|
||||||
left: Value::Float(make_value_node(2.0, 1, 1)),
|
left: Value::Float(value_node!(2.0, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(2, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(2, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Float(1.0)),
|
Val::Float(1.0)),
|
||||||
],
|
],
|
||||||
@ -863,25 +822,25 @@ mod test {
|
|||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Int(make_value_node(1, 1, 1)),
|
left: Value::Int(value_node!(1, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Int(2)),
|
Val::Int(2)),
|
||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Float(make_value_node(1.0, 1, 1)),
|
left: Value::Float(value_node!(1.0, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Float(make_value_node(1.0, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Float(value_node!(1.0, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Float(2.0)),
|
Val::Float(2.0)),
|
||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::String(make_value_node("foo".to_string(), 1, 1)),
|
left: Value::String(value_node!("foo".to_string(), 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1)))),
|
right: Box::new(Expression::Simple(Value::String(value_node!("bar".to_string(), 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::String("foobar".to_string())),
|
Val::String("foobar".to_string())),
|
||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
@ -889,15 +848,15 @@ mod test {
|
|||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::List(
|
left: Value::List(
|
||||||
ListDef{
|
ListDef{
|
||||||
elems: vec![Expression::Simple(Value::String(make_value_node("foo".to_string(), 1, 1)))],
|
elems: vec![Expression::Simple(Value::String(value_node!("foo".to_string(), 1, 1)))],
|
||||||
pos: Position{line: 1, column: 1},
|
pos: Position::new(1, 1),
|
||||||
}),
|
}),
|
||||||
right: Box::new(Expression::Simple(Value::List(
|
right: Box::new(Expression::Simple(Value::List(
|
||||||
ListDef{
|
ListDef{
|
||||||
elems: vec![Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1)))],
|
elems: vec![Expression::Simple(Value::String(value_node!("bar".to_string(), 1, 1)))],
|
||||||
pos: Position{line: 1, column: 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())),
|
Val::List(vec![Rc::new(Val::String("foo".to_string())),
|
||||||
Rc::new(Val::String("bar".to_string()))])),
|
Rc::new(Val::String("bar".to_string()))])),
|
||||||
@ -912,9 +871,9 @@ mod test {
|
|||||||
(Expression::Binary(
|
(Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Float(make_value_node(2.0, 1, 1)),
|
left: Value::Float(value_node!(2.0, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(2, 1, 1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(2, 1, 1)))),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Float(1.0)),
|
Val::Float(1.0)),
|
||||||
],
|
],
|
||||||
@ -924,14 +883,14 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_eval_simple_expr() {
|
fn test_eval_simple_expr() {
|
||||||
test_expr_to_val(vec![
|
test_expr_to_val(vec![
|
||||||
(Expression::Simple(Value::Int(make_value_node(1, 1, 1))), Val::Int(1)),
|
(Expression::Simple(Value::Int(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::Float(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::String(value_node!("foo".to_string(), 1, 1))),
|
||||||
Val::String("foo".to_string())),
|
Val::String("foo".to_string())),
|
||||||
(Expression::Simple(Value::Tuple(make_value_node(vec![
|
(Expression::Simple(Value::Tuple(value_node!(vec![
|
||||||
(Token::new("bar", Position{line: 1, column: 1}), Expression::Simple(Value::Int(make_value_node(1, 1, 1))))
|
(Token::new("bar", 1, 1), Expression::Simple(Value::Int(value_node!(1, 1, 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)))])),
|
Rc::new(Val::Int(1)))])),
|
||||||
],
|
],
|
||||||
Builder::new());
|
Builder::new());
|
||||||
@ -941,14 +900,10 @@ mod test {
|
|||||||
fn test_eval_simple_lookup_expr() {
|
fn test_eval_simple_lookup_expr() {
|
||||||
let mut b = Builder::new();
|
let mut b = Builder::new();
|
||||||
b.out
|
b.out
|
||||||
.entry(Positioned::new("var1".to_string(),
|
.entry(value_node!("var1".to_string(), 1, 0))
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
}))
|
|
||||||
.or_insert(Rc::new(Val::Int(1)));
|
.or_insert(Rc::new(Val::Int(1)));
|
||||||
test_expr_to_val(vec![
|
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);
|
b);
|
||||||
}
|
}
|
||||||
@ -957,68 +912,56 @@ mod test {
|
|||||||
fn test_eval_simple_lookup_error() {
|
fn test_eval_simple_lookup_error() {
|
||||||
let mut b = Builder::new();
|
let mut b = Builder::new();
|
||||||
b.out
|
b.out
|
||||||
.entry(Positioned::new("var1".to_string(),
|
.entry(value_node!("var1".to_string(), 1, 0))
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
}))
|
|
||||||
.or_insert(Rc::new(Val::Int(1)));
|
.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());
|
assert!(b.eval_expr(&expr).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_eval_selector_expr() {
|
fn test_eval_selector_expr() {
|
||||||
let mut b = Builder::new();
|
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![
|
b.out.entry(value_node!("var1".to_string(), 1, 0)).or_insert(Rc::new(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![
|
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
|
b.out
|
||||||
.entry(Positioned::new("var2".to_string(),
|
.entry(value_node!("var2".to_string(), 1, 0))
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
}))
|
|
||||||
.or_insert(Rc::new(Val::Int(2)));
|
.or_insert(Rc::new(Val::Int(2)));
|
||||||
b.out
|
b.out
|
||||||
.entry(Positioned::new("var3".to_string(),
|
.entry(value_node!("var3".to_string(), 1, 0))
|
||||||
Position {
|
.or_insert(Rc::new(Val::Tuple(vec![(value_node!("lvl1".to_string(),
|
||||||
line: 1,
|
1, 0),
|
||||||
column: 0,
|
|
||||||
}))
|
|
||||||
.or_insert(Rc::new(Val::Tuple(vec![(Positioned::new("lvl1".to_string(),
|
|
||||||
Position {line: 1, column: 0}),
|
|
||||||
Rc::new(Val::Int(4)))])));
|
Rc::new(Val::Int(4)))])));
|
||||||
|
|
||||||
test_expr_to_val(vec![
|
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![
|
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![
|
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}),
|
(Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("var1", 1, 1),
|
||||||
Token::new("lvl1", Position{line: 1, column: 1})], 1, 1))),
|
Token::new("lvl1", 1, 1)], 1, 1))),
|
||||||
Val::Tuple(
|
Val::Tuple(
|
||||||
vec![
|
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}),
|
(Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("var1", 1, 1),
|
||||||
Token::new("lvl1", Position{line: 1, column: 1}),
|
Token::new("lvl1", 1, 1),
|
||||||
Token::new("lvl2", Position{line: 1, column: 1})], 1, 1))),
|
Token::new("lvl2", 1, 1)], 1, 1))),
|
||||||
Val::Int(3)),
|
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)),
|
Val::Int(2)),
|
||||||
(Expression::Simple(Value::Selector(make_value_node(vec![Token::new("var3", Position{line: 1, column: 1}),
|
(Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("var3", 1, 1),
|
||||||
Token::new("lvl1", Position{line: 1, column: 1})], 1, 1))),
|
Token::new("lvl1", 1, 1)], 1, 1))),
|
||||||
Val::Int(4)),
|
Val::Int(4)),
|
||||||
], b);
|
], b);
|
||||||
}
|
}
|
||||||
@ -1027,24 +970,20 @@ mod test {
|
|||||||
fn test_eval_selector_list_expr() {
|
fn test_eval_selector_list_expr() {
|
||||||
let mut b = Builder::new();
|
let mut b = Builder::new();
|
||||||
b.out
|
b.out
|
||||||
.entry(Positioned::new("var1".to_string(),
|
.entry(value_node!("var1".to_string(), 1, 1))
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 1,
|
|
||||||
}))
|
|
||||||
.or_insert(Rc::new(Val::List(vec![
|
.or_insert(Rc::new(Val::List(vec![
|
||||||
Rc::new(Val::String("val1".to_string())),
|
Rc::new(Val::String("val1".to_string())),
|
||||||
Rc::new(Val::Tuple(vec![
|
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))),
|
Rc::new(Val::Int(1))),
|
||||||
])),
|
])),
|
||||||
])));
|
])));
|
||||||
// TODO(jwall): Assert that we can index into lists using dot syntax.
|
// TODO(jwall): Assert that we can index into lists using dot syntax.
|
||||||
|
|
||||||
test_expr_to_val(vec![
|
test_expr_to_val(vec![
|
||||||
(Expression::Simple(Value::Selector(make_value_node(vec![
|
(Expression::Simple(Value::Selector(SelectorDef::new(vec![
|
||||||
Token::new("var1", Position{line: 1, column: 1}),
|
Token::new("var1", 1, 1),
|
||||||
Token::new("0", Position{line: 1, column: 1})
|
Token::new("0", 1, 1)
|
||||||
], 1, 1))),
|
], 1, 1))),
|
||||||
Val::String("val1".to_string()))
|
Val::String("val1".to_string()))
|
||||||
],
|
],
|
||||||
@ -1056,9 +995,12 @@ mod test {
|
|||||||
fn test_expr_copy_no_such_tuple() {
|
fn test_expr_copy_no_such_tuple() {
|
||||||
let b = Builder::new();
|
let b = Builder::new();
|
||||||
test_expr_to_val(vec![
|
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())),
|
Val::Tuple(Vec::new())),
|
||||||
], b);
|
],
|
||||||
|
b);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1066,35 +1008,34 @@ mod test {
|
|||||||
fn test_expr_copy_not_a_tuple() {
|
fn test_expr_copy_not_a_tuple() {
|
||||||
let mut b = Builder::new();
|
let mut b = Builder::new();
|
||||||
b.out
|
b.out
|
||||||
.entry(Positioned::new("tpl1".to_string(),
|
.entry(value_node!("tpl1".to_string(), 1, 0))
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
}))
|
|
||||||
.or_insert(Rc::new(Val::Int(1)));
|
.or_insert(Rc::new(Val::Int(1)));
|
||||||
test_expr_to_val(vec![
|
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())),
|
Val::Tuple(Vec::new())),
|
||||||
], b);
|
],
|
||||||
|
b);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "Expected type Integer for field fld1 but got String")]
|
#[should_panic(expected = "Expected type Integer for field fld1 but got String")]
|
||||||
fn test_expr_copy_field_type_error() {
|
fn test_expr_copy_field_type_error() {
|
||||||
let mut b = Builder::new();
|
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![
|
b.out.entry(value_node!("tpl1".to_string(), 1, 0)).or_insert(Rc::new(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))),
|
||||||
])));
|
])));
|
||||||
test_expr_to_val(vec![
|
test_expr_to_val(vec![
|
||||||
(Expression::Copy(
|
(Expression::Copy(
|
||||||
CopyDef{
|
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}),
|
fields: vec![(Token::new("fld1", 1, 1),
|
||||||
Expression::Simple(Value::String(make_value_node("2".to_string(), 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(
|
Val::Tuple(
|
||||||
vec![
|
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);
|
], b);
|
||||||
@ -1103,16 +1044,16 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_expr_copy() {
|
fn test_expr_copy() {
|
||||||
let mut b = Builder::new();
|
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![
|
b.out.entry(value_node!("tpl1".to_string(), 1, 0)).or_insert(Rc::new(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))),
|
||||||
])));
|
])));
|
||||||
test_expr_to_val(vec![
|
test_expr_to_val(vec![
|
||||||
(Expression::Copy(
|
(Expression::Copy(
|
||||||
CopyDef{
|
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("fld2", Position{line: 1, column: 1}),
|
fields: vec![(Token::new("fld2", 1, 1),
|
||||||
Expression::Simple(Value::String(make_value_node("2".to_string(), 1, 1))))],
|
Expression::Simple(Value::String(value_node!("2".to_string(), 1, 1))))],
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
// Add a new field to the copy
|
// Add a new field to the copy
|
||||||
Val::Tuple(
|
Val::Tuple(
|
||||||
@ -1120,33 +1061,33 @@ mod test {
|
|||||||
// that the compare assertion is correct. The ordering has no
|
// that the compare assertion is correct. The ordering has no
|
||||||
// semantics though so at some point we should probably be less restrictive.
|
// semantics though so at some point we should probably be less restrictive.
|
||||||
vec![
|
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))),
|
||||||
(Positioned::new("fld2".to_string(), Position{line: 1, column: 1}), Rc::new(Val::String("2".to_string()))),
|
(value_node!("fld2".to_string(), 1, 1), Rc::new(Val::String("2".to_string()))),
|
||||||
],
|
],
|
||||||
)),
|
)),
|
||||||
// Overwrite a field in the copy
|
// Overwrite a field in the copy
|
||||||
(Expression::Copy(
|
(Expression::Copy(
|
||||||
CopyDef{
|
CopyDef{
|
||||||
selector: vec![Token::new("tpl1", Position{line: 1, column: 1})],
|
selector: SelectorDef::new(vec![Token::new("tpl1", 1, 1)], 1, 1),
|
||||||
fields: vec![
|
fields: vec![
|
||||||
(Token::new("fld1", Position{line: 1, column: 1}),
|
(Token::new("fld1", 1, 1),
|
||||||
Expression::Simple(Value::Int(make_value_node(3, 1, 1)))),
|
Expression::Simple(Value::Int(value_node!(3, 1, 1)))),
|
||||||
(Token::new("fld2", Position{line: 1, column: 1}),
|
(Token::new("fld2", 1, 1),
|
||||||
Expression::Simple(Value::String(make_value_node("2".to_string(), 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(
|
Val::Tuple(
|
||||||
vec![
|
vec![
|
||||||
(Positioned::new("fld1".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(3))),
|
(value_node!("fld1".to_string(), 1, 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!("fld2".to_string(), 1, 0), Rc::new(Val::String("2".to_string()))),
|
||||||
],
|
],
|
||||||
)),
|
)),
|
||||||
// The source tuple is still unmodified.
|
// 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(
|
Val::Tuple(
|
||||||
vec![
|
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);
|
], b);
|
||||||
@ -1155,21 +1096,21 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_macro_call() {
|
fn test_macro_call() {
|
||||||
let mut b = Builder::new();
|
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{
|
b.out.entry(value_node!("tstmac".to_string(), 1, 0)).or_insert(Rc::new(Val::Macro(MacroDef{
|
||||||
argdefs: vec![Positioned::new("arg1".to_string(), Position{line: 1, column: 0})],
|
argdefs: vec![value_node!("arg1".to_string(), 1, 0)],
|
||||||
fields: vec![
|
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![
|
test_expr_to_val(vec![
|
||||||
(Expression::Call(CallDef{
|
(Expression::Call(CallDef{
|
||||||
macroref: vec![Token::new("tstmac", Position{line: 1, column: 1})],
|
macroref: SelectorDef::new(vec![Token::new("tstmac", 1, 1)], 1, 1),
|
||||||
arglist: vec![Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1)))],
|
arglist: vec![Expression::Simple(Value::String(value_node!("bar".to_string(), 1, 1)))],
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
}),
|
}),
|
||||||
Val::Tuple(vec![
|
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()))),
|
Rc::new(Val::String("bar".to_string()))),
|
||||||
])),
|
])),
|
||||||
], b);
|
], b);
|
||||||
@ -1180,27 +1121,23 @@ mod test {
|
|||||||
fn test_macro_hermetic() {
|
fn test_macro_hermetic() {
|
||||||
let mut b = Builder::new();
|
let mut b = Builder::new();
|
||||||
b.out
|
b.out
|
||||||
.entry(Positioned::new("arg1".to_string(),
|
.entry(value_node!("arg1".to_string(), 1, 0))
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
}))
|
|
||||||
.or_insert(Rc::new(Val::String("bar".to_string())));
|
.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{
|
b.out.entry(value_node!("tstmac".to_string(), 1, 0)).or_insert(Rc::new(Val::Macro(MacroDef{
|
||||||
argdefs: vec![Positioned::new("arg2".to_string(), Position{line: 1, column: 0})],
|
argdefs: vec![value_node!("arg2".to_string(), 1, 0)],
|
||||||
fields: vec![
|
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![
|
test_expr_to_val(vec![
|
||||||
(Expression::Call(CallDef{
|
(Expression::Call(CallDef{
|
||||||
macroref: vec![Token::new("tstmac", Position{line: 1, column: 1})],
|
macroref: SelectorDef::new(vec![Token::new("tstmac", 1, 1)], 1, 1),
|
||||||
arglist: vec![Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1)))],
|
arglist: vec![Expression::Simple(Value::String(value_node!("bar".to_string(), 1, 1)))],
|
||||||
pos: Position{line: 1, column: 1},
|
pos: Position::new(1, 1),
|
||||||
}),
|
}),
|
||||||
Val::Tuple(vec![
|
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);
|
], b);
|
||||||
}
|
}
|
||||||
@ -1209,38 +1146,30 @@ mod test {
|
|||||||
fn test_select_expr() {
|
fn test_select_expr() {
|
||||||
let mut b = Builder::new();
|
let mut b = Builder::new();
|
||||||
b.out
|
b.out
|
||||||
.entry(Positioned::new("foo".to_string(),
|
.entry(value_node!("foo".to_string(), 1, 0))
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
}))
|
|
||||||
.or_insert(Rc::new(Val::String("bar".to_string())));
|
.or_insert(Rc::new(Val::String("bar".to_string())));
|
||||||
b.out
|
b.out
|
||||||
.entry(Positioned::new("baz".to_string(),
|
.entry(value_node!("baz".to_string(), 1, 0))
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
}))
|
|
||||||
.or_insert(Rc::new(Val::String("boo".to_string())));
|
.or_insert(Rc::new(Val::String("boo".to_string())));
|
||||||
test_expr_to_val(vec![
|
test_expr_to_val(vec![
|
||||||
(Expression::Select(SelectDef{
|
(Expression::Select(SelectDef{
|
||||||
val: Box::new(Expression::Simple(Value::Symbol(make_value_node("foo".to_string(), 1, 1)))),
|
val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 1)))),
|
||||||
default: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))),
|
default: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||||
tuple: vec![
|
tuple: vec![
|
||||||
(Token::new("foo", Position{line: 1, column: 1}), Expression::Simple(Value::String(make_value_node("2".to_string(), 1, 1)))),
|
(Token::new("foo", 1, 1), Expression::Simple(Value::String(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("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)),
|
Val::Int(2)),
|
||||||
(Expression::Select(SelectDef{
|
(Expression::Select(SelectDef{
|
||||||
val: Box::new(Expression::Simple(Value::Symbol(make_value_node("baz".to_string(), 1, 1)))),
|
val: Box::new(Expression::Simple(Value::Symbol(value_node!("baz".to_string(), 1, 1)))),
|
||||||
default: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))),
|
default: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||||
tuple: vec![
|
tuple: vec![
|
||||||
(Token::new("bar", Position{line: 1, column: 1}), Expression::Simple(Value::Int(make_value_node(2, 1, 1)))),
|
(Token::new("bar", 1, 1), Expression::Simple(Value::Int(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("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.
|
// If the field doesn't exist then we get the default.
|
||||||
Val::Int(1)),
|
Val::Int(1)),
|
||||||
@ -1252,21 +1181,17 @@ mod test {
|
|||||||
fn test_select_expr_not_a_string() {
|
fn test_select_expr_not_a_string() {
|
||||||
let mut b = Builder::new();
|
let mut b = Builder::new();
|
||||||
b.out
|
b.out
|
||||||
.entry(Positioned::new("foo".to_string(),
|
.entry(value_node!("foo".to_string(), 1, 0))
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
}))
|
|
||||||
.or_insert(Rc::new(Val::Int(4)));
|
.or_insert(Rc::new(Val::Int(4)));
|
||||||
test_expr_to_val(vec![
|
test_expr_to_val(vec![
|
||||||
(Expression::Select(SelectDef{
|
(Expression::Select(SelectDef{
|
||||||
val: Box::new(Expression::Simple(Value::Symbol(make_value_node("foo".to_string(), 1, 1)))),
|
val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 1)))),
|
||||||
default: Box::new(Expression::Simple(Value::Int(make_value_node(1, 1, 1)))),
|
default: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||||
tuple: vec![
|
tuple: vec![
|
||||||
(Token::new("bar", Position{line: 1, column: 1}), Expression::Simple(Value::Int(make_value_node(2, 1, 1)))),
|
(Token::new("bar", 1, 1), Expression::Simple(Value::Int(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("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)),
|
Val::Int(2)),
|
||||||
], b);
|
], b);
|
||||||
@ -1276,16 +1201,12 @@ mod test {
|
|||||||
fn test_let_statement() {
|
fn test_let_statement() {
|
||||||
let mut b = Builder::new();
|
let mut b = Builder::new();
|
||||||
let stmt = Statement::Let(LetDef {
|
let stmt = Statement::Let(LetDef {
|
||||||
name: Token::new("foo",
|
name: Token::new("foo", 1, 1),
|
||||||
Position {
|
value: Expression::Simple(Value::String(value_node!("bar".to_string(), 1, 1))),
|
||||||
line: 1,
|
|
||||||
column: 1,
|
|
||||||
}),
|
|
||||||
value: Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1))),
|
|
||||||
});
|
});
|
||||||
b.build_stmt(&stmt).unwrap();
|
b.build_stmt(&stmt).unwrap();
|
||||||
test_expr_to_val(vec![
|
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())),
|
Val::String("bar".to_string())),
|
||||||
],
|
],
|
||||||
b);
|
b);
|
||||||
@ -1295,27 +1216,23 @@ mod test {
|
|||||||
fn test_build_file_string() {
|
fn test_build_file_string() {
|
||||||
let mut b = Builder::new();
|
let mut b = Builder::new();
|
||||||
b.build_file_string("foo.ucg", "let foo = 1;".to_string()).unwrap();
|
b.build_file_string("foo.ucg", "let foo = 1;".to_string()).unwrap();
|
||||||
let key = Positioned::new("foo".to_string(),
|
let key = value_node!("foo".to_string(), 1, 0);
|
||||||
Position {
|
|
||||||
line: 1,
|
|
||||||
column: 0,
|
|
||||||
});
|
|
||||||
assert!(b.out.contains_key(&key));
|
assert!(b.out.contains_key(&key));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_asset_symbol_lookups() {
|
fn test_asset_symbol_lookups() {
|
||||||
let mut b = Builder::new();
|
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![
|
b.assets.entry(value_node!("foo".to_string(), 1, 0)).or_insert(Rc::new(Val::Tuple(vec![
|
||||||
(Positioned::new("bar".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Tuple(vec![
|
(value_node!("bar".to_string(), 1, 0), Rc::new(Val::Tuple(vec![
|
||||||
(Positioned::new("quux".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(1))),
|
(value_node!("quux".to_string(), 1, 0), Rc::new(Val::Int(1))),
|
||||||
]))),
|
]))),
|
||||||
])));
|
])));
|
||||||
test_expr_to_val(vec![
|
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![
|
Val::Tuple(vec![
|
||||||
(Positioned::new("bar".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Tuple(vec![
|
(value_node!("bar".to_string(), 1, 0), Rc::new(Val::Tuple(vec![
|
||||||
(Positioned::new("quux".to_string(), Position{line: 1, column: 0}), Rc::new(Val::Int(1))),
|
(value_node!("quux".to_string(), 1, 0), Rc::new(Val::Int(1))),
|
||||||
]))),
|
]))),
|
||||||
])),
|
])),
|
||||||
],
|
],
|
||||||
|
394
src/parse.rs
394
src/parse.rs
@ -59,13 +59,7 @@ named!(quoted_value( Span ) -> Value,
|
|||||||
// Helper function to make the return types work for down below.
|
// Helper function to make the return types work for down below.
|
||||||
fn triple_to_number(v: (Option<Token>, Option<Token>, Option<Token>)) -> ParseResult<Value> {
|
fn triple_to_number(v: (Option<Token>, Option<Token>, Option<Token>)) -> ParseResult<Value> {
|
||||||
let (pref, mut pref_pos) = match v.0 {
|
let (pref, mut pref_pos) = match v.0 {
|
||||||
None => {
|
None => ("", Position::new(0, 0)),
|
||||||
("",
|
|
||||||
Position {
|
|
||||||
line: 0,
|
|
||||||
column: 0,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Some(ref bs) => (bs.fragment.borrow(), bs.pos.clone()),
|
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.
|
// Helper function to make the return types work for down below.
|
||||||
fn vec_to_tuple(t: (Span, FieldList)) -> ParseResult<Value> {
|
fn vec_to_tuple(t: (Span, FieldList)) -> ParseResult<Value> {
|
||||||
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,
|
named!(field_list( Span ) -> FieldList,
|
||||||
@ -231,10 +225,7 @@ fn tuple_to_binary_expression(tpl: (Span, BinaryExprType, Value, Expression))
|
|||||||
kind: tpl.1,
|
kind: tpl.1,
|
||||||
left: tpl.2,
|
left: tpl.2,
|
||||||
right: Box::new(tpl.3),
|
right: Box::new(tpl.3),
|
||||||
pos: Position {
|
pos: Position::new(tpl.0.line as usize, tpl.0.offset as usize),
|
||||||
line: tpl.0.line as usize,
|
|
||||||
column: tpl.0.offset as usize,
|
|
||||||
},
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,14 +289,11 @@ named!(selector_list( Span ) -> SelectorList,
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
fn tuple_to_copy(t: (Span, SelectorList, FieldList)) -> ParseResult<Expression> {
|
fn tuple_to_copy(t: (Span, SelectorDef, FieldList)) -> ParseResult<Expression> {
|
||||||
Ok(Expression::Copy(CopyDef {
|
Ok(Expression::Copy(CopyDef {
|
||||||
selector: t.1,
|
selector: t.1,
|
||||||
fields: t.2,
|
fields: t.2,
|
||||||
pos: Position {
|
pos: Position::new(t.0.line as usize, t.0.offset as usize),
|
||||||
line: t.0.line as usize,
|
|
||||||
column: t.0.offset as usize,
|
|
||||||
},
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,7 +305,7 @@ named!(copy_expression( Span ) -> Expression,
|
|||||||
lbracetok >>
|
lbracetok >>
|
||||||
fields: ws!(field_list) >>
|
fields: ws!(field_list) >>
|
||||||
rbracetok >>
|
rbracetok >>
|
||||||
(pos, selector, fields)
|
(pos, SelectorDef::new(selector, pos.line as usize, pos.offset as usize), fields)
|
||||||
),
|
),
|
||||||
tuple_to_copy
|
tuple_to_copy
|
||||||
)
|
)
|
||||||
@ -337,10 +325,7 @@ fn tuple_to_macro(mut t: (Span, Vec<Value>, Value)) -> ParseResult<Expression> {
|
|||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
fields: v.val,
|
fields: v.val,
|
||||||
pos: Position {
|
pos: Position::new(t.0.line as usize, t.0.offset as usize),
|
||||||
line: t.0.line as usize,
|
|
||||||
column: t.0.offset as usize,
|
|
||||||
},
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
// TODO(jwall): Show a better version of the unexpected parsed value.
|
// 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<Expr
|
|||||||
val: Box::new(t.1),
|
val: Box::new(t.1),
|
||||||
default: Box::new(t.2),
|
default: Box::new(t.2),
|
||||||
tuple: v.val,
|
tuple: v.val,
|
||||||
pos: Position {
|
pos: Position::new(t.0.line as usize, t.0.offset as usize),
|
||||||
line: t.0.line as usize,
|
|
||||||
column: t.0.offset as usize,
|
|
||||||
},
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
// TODO(jwall): Show a better version of the unexpected parsed value.
|
// TODO(jwall): Show a better version of the unexpected parsed value.
|
||||||
@ -425,14 +407,11 @@ named!(format_expression( Span ) -> Expression,
|
|||||||
);
|
);
|
||||||
|
|
||||||
fn tuple_to_call(t: (Span, Value, Vec<Expression>)) -> ParseResult<Expression> {
|
fn tuple_to_call(t: (Span, Value, Vec<Expression>)) -> ParseResult<Expression> {
|
||||||
if let Value::Selector(sl) = t.1 {
|
if let Value::Selector(def) = t.1 {
|
||||||
Ok(Expression::Call(CallDef {
|
Ok(Expression::Call(CallDef {
|
||||||
macroref: sl.val,
|
macroref: def,
|
||||||
arglist: t.2,
|
arglist: t.2,
|
||||||
pos: Position {
|
pos: Position::new(t.0.line as usize, t.0.offset as usize),
|
||||||
line: t.0.line as usize,
|
|
||||||
column: t.0.offset as usize,
|
|
||||||
},
|
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
Err(Box::new(ParseError::UnexpectedToken("Selector".to_string(), format!("{:?}", t.0))))
|
Err(Box::new(ParseError::UnexpectedToken("Selector".to_string(), format!("{:?}", t.0))))
|
||||||
@ -440,7 +419,7 @@ fn tuple_to_call(t: (Span, Value, Vec<Expression>)) -> ParseResult<Expression> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn vec_to_selector_value(t: (Span, SelectorList)) -> ParseResult<Value> {
|
fn vec_to_selector_value(t: (Span, SelectorList)) -> ParseResult<Value> {
|
||||||
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,
|
named!(selector_value( Span ) -> Value,
|
||||||
@ -595,13 +574,13 @@ mod test {
|
|||||||
fn test_symbol_parsing() {
|
fn test_symbol_parsing() {
|
||||||
assert_eq!(symbol(LocatedSpan::new("foo")),
|
assert_eq!(symbol(LocatedSpan::new("foo")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 3, line: 1},
|
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")),
|
assert_eq!(symbol(LocatedSpan::new("foo-bar")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 7, line: 1},
|
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")),
|
assert_eq!(symbol(LocatedSpan::new("foo_bar")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 7, line: 1},
|
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]
|
#[test]
|
||||||
@ -611,15 +590,15 @@ mod test {
|
|||||||
);
|
);
|
||||||
assert_eq!(selector_value(LocatedSpan::new("foo.bar ")),
|
assert_eq!(selector_value(LocatedSpan::new("foo.bar ")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 8, line: 1},
|
IResult::Done(LocatedSpan{fragment: "", offset: 8, line: 1},
|
||||||
Value::Selector(value_node!(vec![Token{fragment:"foo".to_string(), pos: Position{line: 1, column: 1}},
|
Value::Selector(SelectorDef::new(vec![Token::new("foo".to_string(), 1, 1),
|
||||||
Token{fragment:"bar".to_string(), pos: Position{line: 1, column: 5}}],
|
Token::new("bar", 1, 5)],
|
||||||
Position{line: 1, column: 0})))
|
1, 0)))
|
||||||
);
|
);
|
||||||
assert_eq!(selector_value(LocatedSpan::new("foo.bar;")),
|
assert_eq!(selector_value(LocatedSpan::new("foo.bar;")),
|
||||||
IResult::Done(LocatedSpan{fragment: ";", offset: 7, line: 1},
|
IResult::Done(LocatedSpan{fragment: ";", offset: 7, line: 1},
|
||||||
Value::Selector(value_node!(vec![Token{fragment:"foo".to_string(), pos: Position{line: 1, column: 1}},
|
Value::Selector(SelectorDef::new(vec![Token{fragment:"foo".to_string(), pos: Position::new(1, 1)},
|
||||||
Token{fragment:"bar".to_string(), pos: Position{line: 1, column: 5}}],
|
Token{fragment:"bar".to_string(), pos: Position::new(1, 5)}],
|
||||||
Position{line: 1, column: 0})))
|
1, 0)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -630,12 +609,12 @@ mod test {
|
|||||||
);
|
);
|
||||||
assert_eq!(selector_or_symbol(LocatedSpan::new("foo")),
|
assert_eq!(selector_or_symbol(LocatedSpan::new("foo")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 3, line: 1},
|
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 ")),
|
assert_eq!(selector_or_symbol(LocatedSpan::new("foo.bar ")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 8, line: 1},
|
IResult::Done(LocatedSpan{fragment: "", offset: 8, line: 1},
|
||||||
Value::Selector(value_node!(vec![Token{fragment:"foo".to_string(), pos: Position{line: 1, column: 1}},
|
Value::Selector(SelectorDef::new(vec![Token{fragment:"foo".to_string(), pos: Position::new(1, 1)},
|
||||||
Token{fragment:"bar".to_string(), pos: Position{line: 1, column: 5}}],
|
Token{fragment:"bar".to_string(), pos: Position::new(1, 5)}],
|
||||||
Position{line: 1, column: 0})))
|
1, 0)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -653,17 +632,11 @@ mod test {
|
|||||||
Statement::Import(ImportDef{
|
Statement::Import(ImportDef{
|
||||||
path: Token{
|
path: Token{
|
||||||
fragment: "foo".to_string(),
|
fragment: "foo".to_string(),
|
||||||
pos: Position {
|
pos: Position::new(1,8)
|
||||||
line: 1,
|
|
||||||
column: 8,
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
name: Token{
|
name: Token{
|
||||||
fragment: "foo".to_string(),
|
fragment: "foo".to_string(),
|
||||||
pos: Position{
|
pos: Position::new(1,17),
|
||||||
line: 1,
|
|
||||||
column: 17,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@ -683,12 +656,9 @@ mod test {
|
|||||||
Statement::Let(LetDef{
|
Statement::Let(LetDef{
|
||||||
name: Token{
|
name: Token{
|
||||||
fragment: "foo".to_string(),
|
fragment: "foo".to_string(),
|
||||||
pos: Position {
|
pos: Position::new(1,5),
|
||||||
line: 1,
|
|
||||||
column: 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;";
|
stmt = "1.0;";
|
||||||
let input = LocatedSpan::new(stmt);
|
let input = LocatedSpan::new(stmt);
|
||||||
@ -700,7 +670,7 @@ mod test {
|
|||||||
fragment: "",
|
fragment: "",
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
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]
|
#[test]
|
||||||
@ -720,17 +690,11 @@ mod test {
|
|||||||
Statement::Import(ImportDef{
|
Statement::Import(ImportDef{
|
||||||
path: Token{
|
path: Token{
|
||||||
fragment: "foo".to_string(),
|
fragment: "foo".to_string(),
|
||||||
pos: Position{
|
pos: Position::new(1, 8),
|
||||||
line: 1,
|
|
||||||
column: 8,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
name: Token{
|
name: Token{
|
||||||
fragment: "foo".to_string(),
|
fragment: "foo".to_string(),
|
||||||
pos: Position{
|
pos: Position::new(1,17),
|
||||||
line: 1,
|
|
||||||
column: 17,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@ -757,12 +721,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Statement::Let(LetDef{name: Token{
|
Statement::Let(LetDef{name: Token{
|
||||||
fragment: "foo".to_string(),
|
fragment: "foo".to_string(),
|
||||||
pos: Position{
|
pos: Position::new(1,5),
|
||||||
line: 1,
|
|
||||||
column: 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;";
|
let_stmt = "let foo= 1.0;";
|
||||||
@ -774,12 +735,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Statement::Let(LetDef{name: Token{
|
Statement::Let(LetDef{name: Token{
|
||||||
fragment: "foo".to_string(),
|
fragment: "foo".to_string(),
|
||||||
pos: Position{
|
pos: Position::new(1,5),
|
||||||
line: 1,
|
|
||||||
column: 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;";
|
let_stmt = "let foo =1.0;";
|
||||||
assert_eq!(let_statement(LocatedSpan::new(let_stmt)),
|
assert_eq!(let_statement(LocatedSpan::new(let_stmt)),
|
||||||
IResult::Done(LocatedSpan{
|
IResult::Done(LocatedSpan{
|
||||||
@ -789,12 +747,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Statement::Let(LetDef{name: Token{
|
Statement::Let(LetDef{name: Token{
|
||||||
fragment: "foo".to_string(),
|
fragment: "foo".to_string(),
|
||||||
pos: Position{
|
pos: Position::new(1,5),
|
||||||
line: 1,
|
|
||||||
column: 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]
|
#[test]
|
||||||
@ -807,7 +762,7 @@ mod test {
|
|||||||
line: 1,
|
line: 1,
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
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 ;")),
|
assert_eq!(expression_statement(LocatedSpan::new("1.0 ;")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
@ -815,7 +770,7 @@ mod test {
|
|||||||
line: 1,
|
line: 1,
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
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;")),
|
assert_eq!(expression_statement(LocatedSpan::new(" 1.0;")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
@ -823,7 +778,7 @@ mod test {
|
|||||||
line: 1,
|
line: 1,
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
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;")),
|
assert_eq!(expression_statement(LocatedSpan::new("foo;")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
@ -831,7 +786,7 @@ mod test {
|
|||||||
line: 1,
|
line: 1,
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
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 ;")),
|
assert_eq!(expression_statement(LocatedSpan::new("foo ;")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
@ -839,7 +794,7 @@ mod test {
|
|||||||
line: 1,
|
line: 1,
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
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;")),
|
assert_eq!(expression_statement(LocatedSpan::new(" foo;")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
@ -847,7 +802,7 @@ mod test {
|
|||||||
line: 1,
|
line: 1,
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
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\";")),
|
assert_eq!(expression_statement(LocatedSpan::new("\"foo\";")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
@ -855,7 +810,7 @@ mod test {
|
|||||||
line: 1,
|
line: 1,
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
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\" ;")),
|
assert_eq!(expression_statement(LocatedSpan::new("\"foo\" ;")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
@ -863,7 +818,7 @@ mod test {
|
|||||||
line: 1,
|
line: 1,
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
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\";")),
|
assert_eq!(expression_statement(LocatedSpan::new(" \"foo\";")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
@ -871,7 +826,7 @@ mod test {
|
|||||||
line: 1,
|
line: 1,
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
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]
|
#[test]
|
||||||
@ -882,22 +837,22 @@ mod test {
|
|||||||
offset: 1,
|
offset: 1,
|
||||||
line: 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")),
|
assert_eq!(expression(LocatedSpan::new("foo")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
offset: 3,
|
offset: 3,
|
||||||
line: 1,
|
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 ")),
|
assert_eq!(expression(LocatedSpan::new("foo.bar ")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
offset: 8,
|
offset: 8,
|
||||||
line: 1,
|
line: 1,
|
||||||
},
|
},
|
||||||
Expression::Simple(Value::Selector(make_value_node(vec![Token::new("foo", Position{line: 1, column: 1}),
|
Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("foo", 1, 1),
|
||||||
Token::new("bar", Position{line: 1, column: 5})], 1, 0)))));
|
Token::new("bar", 1, 5)], 1, 0)))));
|
||||||
assert_eq!(expression(LocatedSpan::new("1 + 1")),
|
assert_eq!(expression(LocatedSpan::new("1 + 1")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
@ -906,9 +861,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Int(value_node!(1, Position{line: 1, column: 1})),
|
left: Value::Int(value_node!(1, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5})))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 5)))),
|
||||||
pos: Position { line: 1, column: 0 },
|
pos: Position::new( 1, 0 ),
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(LocatedSpan::new("1 - 1")),
|
assert_eq!(expression(LocatedSpan::new("1 - 1")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
@ -918,9 +873,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Sub,
|
kind: BinaryExprType::Sub,
|
||||||
left: Value::Int(value_node!(1, Position{line: 1, column: 1})),
|
left: Value::Int(value_node!(1, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5})))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 5)))),
|
||||||
pos: Position { line: 1, column: 0 },
|
pos: Position::new(1, 0),
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(LocatedSpan::new("1 * 1")),
|
assert_eq!(expression(LocatedSpan::new("1 * 1")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
@ -930,9 +885,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Mul,
|
kind: BinaryExprType::Mul,
|
||||||
left: Value::Int(value_node!(1, Position{line: 1, column: 1})),
|
left: Value::Int(value_node!(1, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5})))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 5)))),
|
||||||
pos: Position { line: 1, column: 0 },
|
pos: Position::new(1, 0),
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(LocatedSpan::new("1 / 1")),
|
assert_eq!(expression(LocatedSpan::new("1 / 1")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
@ -942,9 +897,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Div,
|
kind: BinaryExprType::Div,
|
||||||
left: Value::Int(value_node!(1, Position{line: 1, column: 1})),
|
left: Value::Int(value_node!(1, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5})))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 5)))),
|
||||||
pos: Position { line: 1, column: 0 },
|
pos: Position::new(1, 0),
|
||||||
})));
|
})));
|
||||||
|
|
||||||
assert_eq!(expression(LocatedSpan::new("1+1")),
|
assert_eq!(expression(LocatedSpan::new("1+1")),
|
||||||
@ -955,9 +910,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Int(value_node!(1, Position{line: 1, column: 1})),
|
left: Value::Int(value_node!(1, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 3})))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 3)))),
|
||||||
pos: Position { line: 1, column: 0 },
|
pos: Position::new(1, 0),
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(LocatedSpan::new("1-1")),
|
assert_eq!(expression(LocatedSpan::new("1-1")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
@ -967,9 +922,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Sub,
|
kind: BinaryExprType::Sub,
|
||||||
left: Value::Int(value_node!(1, Position{line: 1, column: 1})),
|
left: Value::Int(value_node!(1, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 3})))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 3)))),
|
||||||
pos: Position { line: 1, column: 0 },
|
pos: Position::new(1, 0),
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(LocatedSpan::new("1*1")),
|
assert_eq!(expression(LocatedSpan::new("1*1")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
@ -979,9 +934,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Mul,
|
kind: BinaryExprType::Mul,
|
||||||
left: Value::Int(value_node!(1, Position{line: 1, column: 1})),
|
left: Value::Int(value_node!(1, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 3})))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 3)))),
|
||||||
pos: Position { line: 1, column: 0 },
|
pos: Position::new(1, 0),
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(LocatedSpan::new("1/1")),
|
assert_eq!(expression(LocatedSpan::new("1/1")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
@ -991,9 +946,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Div,
|
kind: BinaryExprType::Div,
|
||||||
left: Value::Int(value_node!(1, Position{line: 1, column: 1})),
|
left: Value::Int(value_node!(1, 1, 1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 3})))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 3)))),
|
||||||
pos: Position { line: 1, column: 0 },
|
pos: Position::new(1, 0),
|
||||||
})));
|
})));
|
||||||
let macro_expr = "macro (arg1, arg2) => { foo = arg1 }";
|
let macro_expr = "macro (arg1, arg2) => { foo = arg1 }";
|
||||||
assert_eq!(expression(LocatedSpan::new(macro_expr)),
|
assert_eq!(expression(LocatedSpan::new(macro_expr)),
|
||||||
@ -1004,14 +959,14 @@ mod test {
|
|||||||
},
|
},
|
||||||
Expression::Macro(MacroDef{
|
Expression::Macro(MacroDef{
|
||||||
argdefs: vec![
|
argdefs: vec![
|
||||||
Positioned::new("arg1".to_string(), Position{line: 1, column: 8}),
|
value_node!("arg1".to_string(), 1, 8),
|
||||||
Positioned::new("arg2".to_string(), Position{line: 1, column: 14}),
|
value_node!("arg2".to_string(), 1, 14),
|
||||||
],
|
],
|
||||||
fields: vec![
|
fields: vec![
|
||||||
(Token::new("foo", Position{line: 1, column: 25}),
|
(Token::new("foo", 1, 25),
|
||||||
Expression::Simple(Value::Symbol(value_node!("arg1".to_string(), Position{line: 1, column: 31})))),
|
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,
|
line: 1,
|
||||||
},
|
},
|
||||||
Expression::Select(SelectDef{
|
Expression::Select(SelectDef{
|
||||||
val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 8})))),
|
val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 8)))),
|
||||||
default: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 13})))),
|
default: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 13)))),
|
||||||
tuple: vec![
|
tuple: vec![
|
||||||
(Token::new("foo", Position{line: 1, column: 18}),
|
(Token::new("foo", 1, 18),
|
||||||
Expression::Simple(Value::Int(value_node!(2, Position{line: 1, column: 24}))))
|
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,
|
line: 1,
|
||||||
},
|
},
|
||||||
Expression::Call(CallDef{
|
Expression::Call(CallDef{
|
||||||
macroref: vec![Token::new("foo", Position{line:1,column: 1}),
|
macroref: SelectorDef::new(vec![Token::new("foo", 1,1),
|
||||||
Token::new("bar", Position{line:1,column: 5})],
|
Token::new("bar", 1,5)], 1, 0),
|
||||||
arglist: vec![
|
arglist: vec![
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 10}))),
|
Expression::Simple(Value::Int(value_node!(1, 1, 10))),
|
||||||
Expression::Simple(Value::String(value_node!("foo".to_string(), Position{line: 1, column: 13}))),
|
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(
|
Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
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})))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 6)))),
|
||||||
pos: Position { line: 1, column: 1 },
|
pos: Position::new(1, 1),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -1076,10 +1031,10 @@ mod test {
|
|||||||
Expression::Simple(Value::List(
|
Expression::Simple(Value::List(
|
||||||
ListDef{
|
ListDef{
|
||||||
elems: vec![
|
elems: vec![
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 2}))),
|
Expression::Simple(Value::Int(value_node!(1, 1, 2))),
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5}))),
|
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(
|
Expression::Format(
|
||||||
FormatDef{
|
FormatDef{
|
||||||
template: "foo @ @".to_string(),
|
template: "foo @ @".to_string(),
|
||||||
args: vec![Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 14}))),
|
args: vec![Expression::Simple(Value::Int(value_node!(1, 1, 14))),
|
||||||
Expression::Simple(Value::Int(value_node!(2, Position{line: 1, column: 17})))],
|
Expression::Simple(Value::Int(value_node!(2, 1, 17)))],
|
||||||
pos: Position{line: 1, column: 1},
|
pos: Position::new(1, 1),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -1121,9 +1076,9 @@ mod test {
|
|||||||
Expression::Format(
|
Expression::Format(
|
||||||
FormatDef{
|
FormatDef{
|
||||||
template: "foo @ @".to_string(),
|
template: "foo @ @".to_string(),
|
||||||
args: vec![Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 12}))),
|
args: vec![Expression::Simple(Value::Int(value_node!(1, 1, 12))),
|
||||||
Expression::Simple(Value::Int(value_node!(2, Position{line: 1, column: 15})))],
|
Expression::Simple(Value::Int(value_node!(2, 1, 15)))],
|
||||||
pos: Position { line: 1, column: 1 },
|
pos: Position::new(1, 1),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -1147,12 +1102,12 @@ mod test {
|
|||||||
offset: copy_expr.len(),
|
offset: copy_expr.len(),
|
||||||
},
|
},
|
||||||
Expression::Call(CallDef{
|
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![
|
arglist: vec![
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 6}))),
|
Expression::Simple(Value::Int(value_node!(1, 1, 6))),
|
||||||
Expression::Simple(Value::String(value_node!("foo".to_string(), Position{line: 1, column: 9}))),
|
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(),
|
offset: copy_expr.len(),
|
||||||
},
|
},
|
||||||
Expression::Call(CallDef{
|
Expression::Call(CallDef{
|
||||||
macroref: vec![Token::new("foo", Position{line: 1, column: 1}),
|
macroref: SelectorDef::new(vec![Token::new("foo", 1, 1),
|
||||||
Token::new("bar", Position{line: 1, column: 5})],
|
Token::new("bar", 1, 5)], 1, 0),
|
||||||
arglist: vec![
|
arglist: vec![
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 10}))),
|
Expression::Simple(Value::Int(value_node!(1, 1, 10))),
|
||||||
Expression::Simple(Value::String(value_node!("foo".to_string(), Position{line: 1, column: 13}))),
|
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,
|
line: 1,
|
||||||
},
|
},
|
||||||
Expression::Select(SelectDef{
|
Expression::Select(SelectDef{
|
||||||
val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), Position{line: 1, column: 8})))),
|
val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string(), 1, 8)))),
|
||||||
default: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 13})))),
|
default: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 13)))),
|
||||||
tuple: vec![
|
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
|
line: 1
|
||||||
},
|
},
|
||||||
Expression::Macro(MacroDef{
|
Expression::Macro(MacroDef{
|
||||||
argdefs: vec![Positioned::new("arg1".to_string(), Position{line: 1, column: 8}),
|
argdefs: vec![value_node!("arg1".to_string(), 1, 8),
|
||||||
Positioned::new("arg2".to_string(), Position{line: 1, column: 14})],
|
value_node!("arg2".to_string(), 1, 14)],
|
||||||
fields: vec![(Token::new("foo", Position{line: 1, column: 24}), Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 28})))),
|
fields: vec![(Token::new("foo", 1, 24), Expression::Simple(Value::Int(value_node!(1, 1, 28)))),
|
||||||
(Token::new("bar", Position{line: 1, column: 30}), Expression::Simple(Value::Int(value_node!(2, Position{line: 1, column: 34}))))
|
(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
|
line: 1
|
||||||
},
|
},
|
||||||
Expression::Copy(CopyDef{
|
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(),
|
fields: Vec::new(),
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -1269,10 +1224,10 @@ mod test {
|
|||||||
line: 1
|
line: 1
|
||||||
},
|
},
|
||||||
Expression::Copy(CopyDef{
|
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![(Token::new("bar", Position{line: 1, column: 5}),
|
fields: vec![(Token::new("bar", 1, 5),
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 9}))))],
|
Expression::Simple(Value::Int(value_node!(1, 1, 9))))],
|
||||||
pos: Position{line: 1, column: 0},
|
pos: Position::new(1, 0),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -1287,7 +1242,7 @@ mod test {
|
|||||||
Expression::Grouped(
|
Expression::Grouped(
|
||||||
Box::new(
|
Box::new(
|
||||||
Expression::Simple(
|
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)")),
|
assert_eq!(grouped_expression(LocatedSpan::new("(1 + 1)")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 7, line: 1},
|
IResult::Done(LocatedSpan{fragment: "", offset: 7, line: 1},
|
||||||
@ -1296,10 +1251,10 @@ mod test {
|
|||||||
Expression::Binary(
|
Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
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(
|
right: Box::new(Expression::Simple(
|
||||||
Value::Int(value_node!(1, Position{line: 1, column: 6})))),
|
Value::Int(value_node!(1, 1, 6)))),
|
||||||
pos: Position { line: 1, column: 1 },
|
pos: Position::new(1, 1),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -1317,9 +1272,9 @@ mod test {
|
|||||||
Value::List(
|
Value::List(
|
||||||
ListDef{
|
ListDef{
|
||||||
elems: vec![
|
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(
|
Value::List(
|
||||||
ListDef{
|
ListDef{
|
||||||
elems: vec![
|
elems: vec![
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 2}))),
|
Expression::Simple(Value::Int(value_node!(1, 1, 2))),
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 5}))),
|
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,
|
line: 1,
|
||||||
},
|
},
|
||||||
Value::Tuple(
|
Value::Tuple(
|
||||||
value_node!(vec![], Position{line: 1, column: 0}))));
|
value_node!(vec![], 1, 0))));
|
||||||
|
|
||||||
tuple_expr = "{ foo = 1 }";
|
tuple_expr = "{ foo = 1 }";
|
||||||
assert_eq!(tuple(LocatedSpan::new(tuple_expr)),
|
assert_eq!(tuple(LocatedSpan::new(tuple_expr)),
|
||||||
@ -1368,9 +1323,9 @@ mod test {
|
|||||||
},
|
},
|
||||||
Value::Tuple(
|
Value::Tuple(
|
||||||
value_node!(vec![
|
value_node!(vec![
|
||||||
(Token::new("foo", Position{line:1, column: 3}),
|
(Token::new("foo", 1, 3),
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 9}))))
|
Expression::Simple(Value::Int(value_node!(1, 1, 9))))
|
||||||
], Position{line: 1, column: 0}))));
|
], 1, 0))));
|
||||||
|
|
||||||
tuple_expr = "{ foo = 1, bar = \"1\" }";
|
tuple_expr = "{ foo = 1, bar = \"1\" }";
|
||||||
assert_eq!(tuple(LocatedSpan::new(tuple_expr)),
|
assert_eq!(tuple(LocatedSpan::new(tuple_expr)),
|
||||||
@ -1381,11 +1336,11 @@ mod test {
|
|||||||
},
|
},
|
||||||
Value::Tuple(
|
Value::Tuple(
|
||||||
value_node!(vec![
|
value_node!(vec![
|
||||||
(Token::new("foo", Position{line: 1, column: 3}),
|
(Token::new("foo", 1, 3),
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 9})))),
|
Expression::Simple(Value::Int(value_node!(1, 1, 9)))),
|
||||||
(Token::new("bar", Position{line: 1, column: 12}),
|
(Token::new("bar", 1, 12),
|
||||||
Expression::Simple(Value::String(value_node!("1".to_string(), Position{line: 1, column: 18}))))
|
Expression::Simple(Value::String(value_node!("1".to_string(), Position::new(1, 18)))))
|
||||||
], Position{line: 1, column: 0}))));
|
], 1, 0))));
|
||||||
tuple_expr = "{ foo = 1, bar = {} }";
|
tuple_expr = "{ foo = 1, bar = {} }";
|
||||||
assert_eq!(tuple(LocatedSpan::new(tuple_expr)),
|
assert_eq!(tuple(LocatedSpan::new(tuple_expr)),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
@ -1395,11 +1350,11 @@ mod test {
|
|||||||
},
|
},
|
||||||
Value::Tuple(
|
Value::Tuple(
|
||||||
value_node!(vec![
|
value_node!(vec![
|
||||||
(Token::new("foo", Position{line: 1, column: 3}),
|
(Token::new("foo", 1, 3),
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 9})))),
|
Expression::Simple(Value::Int(value_node!(1, Position::new(1, 9))))),
|
||||||
(Token::new("bar", Position{line: 1, column: 12}),
|
(Token::new("bar", 1, 12),
|
||||||
Expression::Simple(Value::Tuple(value_node!(Vec::new(), Position{line: 1, column: 17}))))
|
Expression::Simple(Value::Tuple(value_node!(Vec::new(), Position::new(1, 17)))))
|
||||||
], Position{line: 1, column: 0}))));
|
], 1, 0))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1409,24 +1364,24 @@ mod test {
|
|||||||
|
|
||||||
assert_eq!(field_value(LocatedSpan::new("foo = 1")),
|
assert_eq!(field_value(LocatedSpan::new("foo = 1")),
|
||||||
IResult::Done(LocatedSpan { offset: 7, line: 1, fragment: "" },
|
IResult::Done(LocatedSpan { offset: 7, line: 1, fragment: "" },
|
||||||
(Token::new("foo", Position{line: 1, column: 1}),
|
(Token::new("foo", 1, 1),
|
||||||
Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 7}))))) );
|
Expression::Simple(Value::Int(value_node!(1, 1, 7))))) );
|
||||||
assert_eq!(field_value(LocatedSpan::new("foo = \"1\"")),
|
assert_eq!(field_value(LocatedSpan::new("foo = \"1\"")),
|
||||||
IResult::Done(LocatedSpan { offset: 9, line: 1, fragment: "" },
|
IResult::Done(LocatedSpan { offset: 9, line: 1, fragment: "" },
|
||||||
(Token::new("foo", Position{line: 1, column: 1}),
|
(Token::new("foo", 1, 1),
|
||||||
Expression::Simple(Value::String(value_node!("1".to_string(), Position{line: 1, column: 7}))))) );
|
Expression::Simple(Value::String(value_node!("1".to_string(), 1, 7))))) );
|
||||||
assert_eq!(field_value(LocatedSpan::new("foo = bar")),
|
assert_eq!(field_value(LocatedSpan::new("foo = bar")),
|
||||||
IResult::Done(LocatedSpan { offset: 9, line: 1, fragment: "" },
|
IResult::Done(LocatedSpan { offset: 9, line: 1, fragment: "" },
|
||||||
(Token::new("foo", Position{line: 1, column: 1}),
|
(Token::new("foo", 1, 1),
|
||||||
Expression::Simple(Value::Symbol(value_node!("bar".to_string(), Position{line: 1, column: 7}))))) );
|
Expression::Simple(Value::Symbol(value_node!("bar".to_string(), 1, 7))))) );
|
||||||
assert_eq!(field_value(LocatedSpan::new("foo = bar ")),
|
assert_eq!(field_value(LocatedSpan::new("foo = bar ")),
|
||||||
IResult::Done(LocatedSpan { offset: 10, line: 1, fragment: "" },
|
IResult::Done(LocatedSpan { offset: 10, line: 1, fragment: "" },
|
||||||
(Token::new("foo", Position{line: 1, column: 1}),
|
(Token::new("foo", 1, 1),
|
||||||
Expression::Simple(Value::Symbol(value_node!("bar".to_string(), Position{line: 1, column: 7}))))) );
|
Expression::Simple(Value::Symbol(value_node!("bar".to_string(), 1, 7))))) );
|
||||||
assert_eq!(field_value(LocatedSpan::new("foo = bar.baz ")),
|
assert_eq!(field_value(LocatedSpan::new("foo = bar.baz ")),
|
||||||
IResult::Done(LocatedSpan { offset: 14, line: 1, fragment: "" },
|
IResult::Done(LocatedSpan { offset: 14, line: 1, fragment: "" },
|
||||||
(Token::new("foo", Position{line: 1, column: 1}),
|
(Token::new("foo", 1, 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))))));
|
Expression::Simple(Value::Selector(SelectorDef::new(vec![Token::new("bar", 1, 7), Token::new("baz", 1, 11)], 1, 6))))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1435,16 +1390,16 @@ mod test {
|
|||||||
assert!(number(LocatedSpan::new(". ")).is_err() );
|
assert!(number(LocatedSpan::new(". ")).is_err() );
|
||||||
assert_eq!(number(LocatedSpan::new("1.0")),
|
assert_eq!(number(LocatedSpan::new("1.0")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 3, line: 1},
|
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.")),
|
assert_eq!(number(LocatedSpan::new("1.")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 2, line: 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")),
|
assert_eq!(number(LocatedSpan::new("1")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 1, line: 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")),
|
assert_eq!(number(LocatedSpan::new(".1")),
|
||||||
IResult::Done(LocatedSpan{fragment: "", offset: 2, line: 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]
|
#[test]
|
||||||
@ -1464,36 +1419,27 @@ mod test {
|
|||||||
Statement::Import(ImportDef{
|
Statement::Import(ImportDef{
|
||||||
path: Token{
|
path: Token{
|
||||||
fragment: "mylib".to_string(),
|
fragment: "mylib".to_string(),
|
||||||
pos: Position{
|
pos: Position::new(1, 8),
|
||||||
line: 1,
|
|
||||||
column: 8,
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
name: Token{
|
name: Token{
|
||||||
fragment: "lib".to_string(),
|
fragment: "lib".to_string(),
|
||||||
pos: Position{
|
pos: Position::new(1, 19),
|
||||||
line: 1,
|
|
||||||
column: 19,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
Statement::Let(LetDef{
|
Statement::Let(LetDef{
|
||||||
name: Token{
|
name: Token{
|
||||||
fragment: "foo".to_string(),
|
fragment: "foo".to_string(),
|
||||||
pos: Position{
|
pos: Position::new(1, 27),
|
||||||
line: 1,
|
|
||||||
column: 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(
|
Statement::Expression(
|
||||||
Expression::Binary(
|
Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Int(value_node!(1, Position{line: 1, column: 35})),
|
left: Value::Int(value_node!(1, 1, 35)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, Position{line: 1, column: 37})))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 37)))),
|
||||||
pos: Position { line: 1, column: 34 },
|
pos: Position::new(1, 34),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
]);
|
]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user