mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
Add a macro for constructing values with or without a position.
This commit is contained in:
parent
c91ff1c218
commit
c091878364
14
src/ast.rs
14
src/ast.rs
@ -13,6 +13,7 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
|
use std::convert::Into;
|
||||||
|
|
||||||
#[derive(Debug,PartialEq,Clone)]
|
#[derive(Debug,PartialEq,Clone)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
@ -20,6 +21,15 @@ pub struct Position {
|
|||||||
pub column: usize,
|
pub column: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! value_node {
|
||||||
|
($v:expr) => {
|
||||||
|
LocatedNode::new($v)
|
||||||
|
};
|
||||||
|
($v:expr, $p:expr) => {
|
||||||
|
LocatedNode::new_with_pos($v, $p)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub type FieldList = Vec<(String, Expression)>; // str is expected to be a symbol
|
pub type FieldList = Vec<(String, Expression)>; // str is expected to be a symbol
|
||||||
pub type SelectorList = Vec<String>; // str is expected to always be a symbol.
|
pub type SelectorList = Vec<String>; // str is expected to always be a symbol.
|
||||||
|
|
||||||
@ -37,9 +47,9 @@ impl<T> LocatedNode<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_with_pos(v: T, pos: Position) -> Self {
|
pub fn new_with_pos<P: Into<Position>>(v: T, pos: P) -> Self {
|
||||||
Self {
|
Self {
|
||||||
pos: Some(pos),
|
pos: Some(pos.into()),
|
||||||
val: v,
|
val: v,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,10 +13,13 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate nom;
|
extern crate nom;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate nom_locate;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate quick_error;
|
extern crate quick_error;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
pub mod parse;
|
pub mod parse;
|
||||||
pub mod build;
|
pub mod build;
|
||||||
|
186
src/parse.rs
186
src/parse.rs
@ -11,6 +11,8 @@
|
|||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
use nom_locate::LocatedSpan;
|
||||||
|
|
||||||
quick_error! {
|
quick_error! {
|
||||||
#[derive(Debug,PartialEq)]
|
#[derive(Debug,PartialEq)]
|
||||||
pub enum ParseError {
|
pub enum ParseError {
|
||||||
@ -21,12 +23,24 @@ quick_error! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Span<'a> = LocatedSpan<&'a str>;
|
||||||
|
|
||||||
|
impl<'a> From<Span<'a>> for Position {
|
||||||
|
fn from(s: Span) -> Position {
|
||||||
|
Position {
|
||||||
|
line: s.line as usize,
|
||||||
|
column: s.get_column() as usize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO(jwall): Convert to tokenizer steps followed by parser steps.
|
// TODO(jwall): Convert to tokenizer steps followed by parser steps.
|
||||||
// TODO(jwall): Error Reporting with Line and Column information.
|
// TODO(jwall): Error Reporting with Line and Column information.
|
||||||
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::convert::From;
|
||||||
|
|
||||||
use nom::{alpha, is_alphanumeric, digit};
|
use nom::{alpha, is_alphanumeric, digit};
|
||||||
|
|
||||||
@ -61,7 +75,7 @@ named!(field<String>,
|
|||||||
);
|
);
|
||||||
|
|
||||||
fn symbol_to_value(s: String) -> ParseResult<Value> {
|
fn symbol_to_value(s: String) -> ParseResult<Value> {
|
||||||
Ok(Value::Symbol(make_value_node(s)))
|
Ok(Value::Symbol(value_node!(s)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// symbol is a bare unquoted field.
|
// symbol is a bare unquoted field.
|
||||||
@ -75,7 +89,7 @@ named!(quoted<String>,
|
|||||||
);
|
);
|
||||||
|
|
||||||
fn str_to_value(s: String) -> ParseResult<Value> {
|
fn str_to_value(s: String) -> ParseResult<Value> {
|
||||||
Ok(Value::String(make_value_node(s)))
|
Ok(Value::String(value_node!(s)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// quoted_value is a quoted string.
|
// quoted_value is a quoted string.
|
||||||
@ -94,7 +108,7 @@ fn triple_to_number(v: (Option<&[u8]>, Option<&[u8]>, Option<&[u8]>))
|
|||||||
let has_dot = v.1.is_some();
|
let has_dot = v.1.is_some();
|
||||||
|
|
||||||
if v.0.is_some() && !has_dot && v.2.is_none() {
|
if v.0.is_some() && !has_dot && v.2.is_none() {
|
||||||
return Ok(Value::Int(make_value_node(try!(FromStr::from_str(pref)))));
|
return Ok(Value::Int(value_node!(try!(FromStr::from_str(pref)))));
|
||||||
}
|
}
|
||||||
|
|
||||||
let suf = match v.2 {
|
let suf = match v.2 {
|
||||||
@ -104,7 +118,7 @@ fn triple_to_number(v: (Option<&[u8]>, Option<&[u8]>, Option<&[u8]>))
|
|||||||
|
|
||||||
let to_parse = pref.to_string() + "." + suf;
|
let to_parse = pref.to_string() + "." + suf;
|
||||||
let f = try!(FromStr::from_str(&to_parse));
|
let f = try!(FromStr::from_str(&to_parse));
|
||||||
return Ok(Value::Float(make_value_node(f)));
|
return Ok(Value::Float(value_node!(f)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE(jwall): HERE THERE BE DRAGONS. The order for these matters
|
// NOTE(jwall): HERE THERE BE DRAGONS. The order for these matters
|
||||||
@ -164,7 +178,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(v: FieldList) -> ParseResult<Value> {
|
fn vec_to_tuple(v: FieldList) -> ParseResult<Value> {
|
||||||
Ok(Value::Tuple(make_value_node(v)))
|
Ok(Value::Tuple(value_node!(v)))
|
||||||
}
|
}
|
||||||
|
|
||||||
named!(field_list<FieldList>,
|
named!(field_list<FieldList>,
|
||||||
@ -381,7 +395,7 @@ fn tuple_to_call(t: (Value, Vec<Expression>)) -> ParseResult<Expression> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn vec_to_selector_value(v: SelectorList) -> ParseResult<Value> {
|
fn vec_to_selector_value(v: SelectorList) -> ParseResult<Value> {
|
||||||
Ok(Value::Selector(make_value_node(v)))
|
Ok(Value::Selector(value_node!(v)))
|
||||||
}
|
}
|
||||||
|
|
||||||
named!(selector_value<Value>,
|
named!(selector_value<Value>,
|
||||||
@ -521,11 +535,11 @@ mod test {
|
|||||||
assert_eq!(statement(&b"let foo = 1.0 ;"[..]),
|
assert_eq!(statement(&b"let foo = 1.0 ;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Let{name: "foo".to_string(),
|
Statement::Let{name: "foo".to_string(),
|
||||||
value: Expression::Simple(Value::Float(make_value_node(1.0)))}));
|
value: Expression::Simple(Value::Float(value_node!(1.0)))}));
|
||||||
assert_eq!(statement(&b"1.0;"[..]),
|
assert_eq!(statement(&b"1.0;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Simple(Value::Float(make_value_node(1.0))))));
|
Expression::Simple(Value::Float(value_node!(1.0))))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -559,15 +573,15 @@ mod test {
|
|||||||
assert_eq!(let_statement(&b"let foo = 1.0 ;"[..]),
|
assert_eq!(let_statement(&b"let foo = 1.0 ;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Let{name: "foo".to_string(),
|
Statement::Let{name: "foo".to_string(),
|
||||||
value: Expression::Simple(Value::Float(make_value_node(1.0)))}));
|
value: Expression::Simple(Value::Float(value_node!(1.0)))}));
|
||||||
assert_eq!(let_statement(&b"let foo= 1.0;"[..]),
|
assert_eq!(let_statement(&b"let foo= 1.0;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Let{name: "foo".to_string(),
|
Statement::Let{name: "foo".to_string(),
|
||||||
value: Expression::Simple(Value::Float(make_value_node(1.0)))}));
|
value: Expression::Simple(Value::Float(value_node!(1.0)))}));
|
||||||
assert_eq!(let_statement(&b"let foo =1.0;"[..]),
|
assert_eq!(let_statement(&b"let foo =1.0;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Let{name: "foo".to_string(),
|
Statement::Let{name: "foo".to_string(),
|
||||||
value: Expression::Simple(Value::Float(make_value_node(1.0)))}));
|
value: Expression::Simple(Value::Float(value_node!(1.0)))}));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -576,77 +590,77 @@ mod test {
|
|||||||
assert_eq!(expression_statement(&b"1.0;"[..]),
|
assert_eq!(expression_statement(&b"1.0;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Simple(Value::Float(make_value_node(1.0))))));
|
Expression::Simple(Value::Float(value_node!(1.0))))));
|
||||||
assert_eq!(expression_statement(&b"1.0 ;"[..]),
|
assert_eq!(expression_statement(&b"1.0 ;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Simple(Value::Float(make_value_node(1.0))))));
|
Expression::Simple(Value::Float(value_node!(1.0))))));
|
||||||
assert_eq!(expression_statement(&b" 1.0;"[..]),
|
assert_eq!(expression_statement(&b" 1.0;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Simple(Value::Float(make_value_node(1.0))))));
|
Expression::Simple(Value::Float(value_node!(1.0))))));
|
||||||
assert_eq!(expression_statement(&b"foo;"[..]),
|
assert_eq!(expression_statement(&b"foo;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Simple(Value::Symbol(make_value_node("foo".to_string()))))));
|
Expression::Simple(Value::Symbol(value_node!("foo".to_string()))))));
|
||||||
assert_eq!(expression_statement(&b"foo ;"[..]),
|
assert_eq!(expression_statement(&b"foo ;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Simple(Value::Symbol(make_value_node("foo".to_string()))))));
|
Expression::Simple(Value::Symbol(value_node!("foo".to_string()))))));
|
||||||
assert_eq!(expression_statement(&b" foo;"[..]),
|
assert_eq!(expression_statement(&b" foo;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Simple(Value::Symbol(make_value_node("foo".to_string()))))));
|
Expression::Simple(Value::Symbol(value_node!("foo".to_string()))))));
|
||||||
assert_eq!(expression_statement(&b"\"foo\";"[..]),
|
assert_eq!(expression_statement(&b"\"foo\";"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))))));
|
Expression::Simple(Value::String(value_node!("foo".to_string()))))));
|
||||||
assert_eq!(expression_statement(&b"\"foo\" ;"[..]),
|
assert_eq!(expression_statement(&b"\"foo\" ;"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))))));
|
Expression::Simple(Value::String(value_node!("foo".to_string()))))));
|
||||||
assert_eq!(expression_statement(&b" \"foo\";"[..]),
|
assert_eq!(expression_statement(&b" \"foo\";"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))))));
|
Expression::Simple(Value::String(value_node!("foo".to_string()))))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_expression_parse() {
|
fn test_expression_parse() {
|
||||||
assert_eq!(expression(&b"1"[..]),
|
assert_eq!(expression(&b"1"[..]),
|
||||||
IResult::Done(&b""[..], Expression::Simple(Value::Int(make_value_node(1)))));
|
IResult::Done(&b""[..], Expression::Simple(Value::Int(value_node!(1)))));
|
||||||
assert_eq!(expression(&b"foo"[..]),
|
assert_eq!(expression(&b"foo"[..]),
|
||||||
IResult::Done(&b""[..], Expression::Simple(Value::Symbol(make_value_node("foo".to_string())))));
|
IResult::Done(&b""[..], Expression::Simple(Value::Symbol(value_node!("foo".to_string())))));
|
||||||
assert_eq!(expression(&b"1 + 1"[..]),
|
assert_eq!(expression(&b"1 + 1"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(&b"1 - 1"[..]),
|
assert_eq!(expression(&b"1 - 1"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Sub,
|
kind: BinaryExprType::Sub,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(&b"1 * 1"[..]),
|
assert_eq!(expression(&b"1 * 1"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Mul,
|
kind: BinaryExprType::Mul,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(&b"1 / 1"[..]),
|
assert_eq!(expression(&b"1 / 1"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Div,
|
kind: BinaryExprType::Div,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
})));
|
})));
|
||||||
|
|
||||||
@ -654,32 +668,32 @@ mod test {
|
|||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(&b"1-1"[..]),
|
assert_eq!(expression(&b"1-1"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Sub,
|
kind: BinaryExprType::Sub,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(&b"1*1"[..]),
|
assert_eq!(expression(&b"1*1"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Mul,
|
kind: BinaryExprType::Mul,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(&b"1/1"[..]),
|
assert_eq!(expression(&b"1/1"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Expression::Binary(BinaryOpDef{
|
Expression::Binary(BinaryOpDef{
|
||||||
kind: BinaryExprType::Div,
|
kind: BinaryExprType::Div,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
})));
|
})));
|
||||||
assert_eq!(expression(&b"macro (arg1, arg2) => { foo = arg1 }"[..]),
|
assert_eq!(expression(&b"macro (arg1, arg2) => { foo = arg1 }"[..]),
|
||||||
@ -690,7 +704,7 @@ mod test {
|
|||||||
"arg2".to_string(),
|
"arg2".to_string(),
|
||||||
],
|
],
|
||||||
fields: vec![
|
fields: vec![
|
||||||
("foo".to_string(), Expression::Simple(Value::Symbol(make_value_node("arg1".to_string())))),
|
("foo".to_string(), Expression::Simple(Value::Symbol(value_node!("arg1".to_string())))),
|
||||||
],
|
],
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
@ -699,10 +713,10 @@ mod test {
|
|||||||
assert_eq!(expression(&b"select foo, 1, { foo = 2 };"[..]),
|
assert_eq!(expression(&b"select foo, 1, { foo = 2 };"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Expression::Select(SelectDef{
|
Expression::Select(SelectDef{
|
||||||
val: Box::new(Expression::Simple(Value::Symbol(make_value_node("foo".to_string())))),
|
val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string())))),
|
||||||
default: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
default: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
tuple: vec![
|
tuple: vec![
|
||||||
("foo".to_string(), Expression::Simple(Value::Int(make_value_node(2))))
|
("foo".to_string(), Expression::Simple(Value::Int(value_node!(2))))
|
||||||
],
|
],
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
@ -713,8 +727,8 @@ mod test {
|
|||||||
Expression::Call(CallDef{
|
Expression::Call(CallDef{
|
||||||
macroref: vec!["foo".to_string(),"bar".to_string()],
|
macroref: vec!["foo".to_string(),"bar".to_string()],
|
||||||
arglist: vec![
|
arglist: vec![
|
||||||
Expression::Simple(Value::Int(make_value_node(1))),
|
Expression::Simple(Value::Int(value_node!(1))),
|
||||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))),
|
Expression::Simple(Value::String(value_node!("foo".to_string()))),
|
||||||
],
|
],
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
@ -727,8 +741,8 @@ mod test {
|
|||||||
Expression::Binary(
|
Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -750,8 +764,8 @@ mod test {
|
|||||||
Expression::Format(
|
Expression::Format(
|
||||||
FormatDef{
|
FormatDef{
|
||||||
template: "foo @ @".to_string(),
|
template: "foo @ @".to_string(),
|
||||||
args: vec![Expression::Simple(Value::Int(make_value_node(1))),
|
args: vec![Expression::Simple(Value::Int(value_node!(1))),
|
||||||
Expression::Simple(Value::Int(make_value_node(2)))],
|
Expression::Simple(Value::Int(value_node!(2)))],
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@ -761,8 +775,8 @@ mod test {
|
|||||||
Expression::Format(
|
Expression::Format(
|
||||||
FormatDef{
|
FormatDef{
|
||||||
template: "foo @ @".to_string(),
|
template: "foo @ @".to_string(),
|
||||||
args: vec![Expression::Simple(Value::Int(make_value_node(1))),
|
args: vec![Expression::Simple(Value::Int(value_node!(1))),
|
||||||
Expression::Simple(Value::Int(make_value_node(2)))],
|
Expression::Simple(Value::Int(value_node!(2)))],
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@ -782,8 +796,8 @@ mod test {
|
|||||||
Expression::Call(CallDef{
|
Expression::Call(CallDef{
|
||||||
macroref: vec!["foo".to_string()],
|
macroref: vec!["foo".to_string()],
|
||||||
arglist: vec![
|
arglist: vec![
|
||||||
Expression::Simple(Value::Int(make_value_node(1))),
|
Expression::Simple(Value::Int(value_node!(1))),
|
||||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))),
|
Expression::Simple(Value::String(value_node!("foo".to_string()))),
|
||||||
],
|
],
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
@ -795,8 +809,8 @@ mod test {
|
|||||||
Expression::Call(CallDef{
|
Expression::Call(CallDef{
|
||||||
macroref: vec!["foo".to_string(),"bar".to_string()],
|
macroref: vec!["foo".to_string(),"bar".to_string()],
|
||||||
arglist: vec![
|
arglist: vec![
|
||||||
Expression::Simple(Value::Int(make_value_node(1))),
|
Expression::Simple(Value::Int(value_node!(1))),
|
||||||
Expression::Simple(Value::String(make_value_node("foo".to_string()))),
|
Expression::Simple(Value::String(value_node!("foo".to_string()))),
|
||||||
],
|
],
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
@ -814,10 +828,10 @@ mod test {
|
|||||||
assert_eq!(select_expression(&b"select foo, 1, { foo = 2 };"[..]),
|
assert_eq!(select_expression(&b"select foo, 1, { foo = 2 };"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Expression::Select(SelectDef{
|
Expression::Select(SelectDef{
|
||||||
val: Box::new(Expression::Simple(Value::Symbol(make_value_node("foo".to_string())))),
|
val: Box::new(Expression::Simple(Value::Symbol(value_node!("foo".to_string())))),
|
||||||
default: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
default: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
tuple: vec![
|
tuple: vec![
|
||||||
("foo".to_string(), Expression::Simple(Value::Int(make_value_node(2))))
|
("foo".to_string(), Expression::Simple(Value::Int(value_node!(2))))
|
||||||
],
|
],
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
@ -844,8 +858,8 @@ mod test {
|
|||||||
Expression::Macro(MacroDef{
|
Expression::Macro(MacroDef{
|
||||||
argdefs: vec!["arg1".to_string(),
|
argdefs: vec!["arg1".to_string(),
|
||||||
"arg2".to_string()],
|
"arg2".to_string()],
|
||||||
fields: vec![("foo".to_string(), Expression::Simple(Value::Int(make_value_node(1)))),
|
fields: vec![("foo".to_string(), Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
("bar".to_string(), Expression::Simple(Value::Int(make_value_node(2))))
|
("bar".to_string(), Expression::Simple(Value::Int(value_node!(2))))
|
||||||
],
|
],
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
@ -872,7 +886,7 @@ mod test {
|
|||||||
Expression::Copy(CopyDef{
|
Expression::Copy(CopyDef{
|
||||||
selector: vec!["foo".to_string()],
|
selector: vec!["foo".to_string()],
|
||||||
fields: vec![("bar".to_string(),
|
fields: vec![("bar".to_string(),
|
||||||
Expression::Simple(Value::Int(make_value_node(1))))],
|
Expression::Simple(Value::Int(value_node!(1))))],
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@ -888,7 +902,7 @@ mod test {
|
|||||||
Expression::Grouped(
|
Expression::Grouped(
|
||||||
Box::new(
|
Box::new(
|
||||||
Expression::Simple(
|
Expression::Simple(
|
||||||
Value::Symbol(make_value_node("foo".to_string()))))))
|
Value::Symbol(value_node!("foo".to_string()))))))
|
||||||
);
|
);
|
||||||
assert_eq!(grouped_expression(&b"(1 + 1)"[..]),
|
assert_eq!(grouped_expression(&b"(1 + 1)"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
@ -897,9 +911,9 @@ mod test {
|
|||||||
Expression::Binary(
|
Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(
|
right: Box::new(Expression::Simple(
|
||||||
Value::Int(make_value_node(1)))),
|
Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -921,28 +935,28 @@ mod test {
|
|||||||
assert_eq!(tuple(&b"{ }"[..]),
|
assert_eq!(tuple(&b"{ }"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Value::Tuple(
|
Value::Tuple(
|
||||||
make_value_node(vec![]))));
|
value_node!(vec![]))));
|
||||||
|
|
||||||
assert_eq!(tuple(&b"{ foo = 1 }"[..]),
|
assert_eq!(tuple(&b"{ foo = 1 }"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Value::Tuple(
|
Value::Tuple(
|
||||||
make_value_node(vec![
|
value_node!(vec![
|
||||||
("foo".to_string(), Expression::Simple(Value::Int(make_value_node(1))))
|
("foo".to_string(), Expression::Simple(Value::Int(value_node!(1))))
|
||||||
]))));
|
]))));
|
||||||
|
|
||||||
assert_eq!(tuple(&b"{ foo = 1, bar = \"1\" }"[..]),
|
assert_eq!(tuple(&b"{ foo = 1, bar = \"1\" }"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Value::Tuple(
|
Value::Tuple(
|
||||||
make_value_node(vec![
|
value_node!(vec![
|
||||||
("foo".to_string(), Expression::Simple(Value::Int(make_value_node(1)))),
|
("foo".to_string(), Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
("bar".to_string(), Expression::Simple(Value::String(make_value_node("1".to_string()))))
|
("bar".to_string(), Expression::Simple(Value::String(value_node!("1".to_string()))))
|
||||||
]))));
|
]))));
|
||||||
assert_eq!(tuple(&b"{ foo = 1, bar = {} }"[..]),
|
assert_eq!(tuple(&b"{ foo = 1, bar = {} }"[..]),
|
||||||
IResult::Done(&b""[..],
|
IResult::Done(&b""[..],
|
||||||
Value::Tuple(
|
Value::Tuple(
|
||||||
make_value_node(vec![
|
value_node!(vec![
|
||||||
("foo".to_string(), Expression::Simple(Value::Int(make_value_node(1)))),
|
("foo".to_string(), Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
("bar".to_string(), Expression::Simple(Value::Tuple(make_value_node(Vec::new()))))
|
("bar".to_string(), Expression::Simple(Value::Tuple(value_node!(Vec::new()))))
|
||||||
]))));
|
]))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -952,13 +966,13 @@ mod test {
|
|||||||
assert!(field_value(&b"foo ="[..]).is_incomplete() );
|
assert!(field_value(&b"foo ="[..]).is_incomplete() );
|
||||||
|
|
||||||
assert_eq!(field_value(&b"foo = 1"[..]),
|
assert_eq!(field_value(&b"foo = 1"[..]),
|
||||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Int(make_value_node(1))))) );
|
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Int(value_node!(1))))) );
|
||||||
assert_eq!(field_value(&b"foo = \"1\""[..]),
|
assert_eq!(field_value(&b"foo = \"1\""[..]),
|
||||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::String(make_value_node("1".to_string()))))) );
|
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::String(value_node!("1".to_string()))))) );
|
||||||
assert_eq!(field_value(&b"foo = bar"[..]),
|
assert_eq!(field_value(&b"foo = bar"[..]),
|
||||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Symbol(make_value_node("bar".to_string()))))) );
|
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Symbol(value_node!("bar".to_string()))))) );
|
||||||
assert_eq!(field_value(&b"foo = bar "[..]),
|
assert_eq!(field_value(&b"foo = bar "[..]),
|
||||||
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Symbol(make_value_node("bar".to_string()))))) );
|
IResult::Done(&b""[..], ("foo".to_string(), Expression::Simple(Value::Symbol(value_node!("bar".to_string()))))) );
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -966,23 +980,23 @@ mod test {
|
|||||||
assert!(number(&b"."[..]).is_err() );
|
assert!(number(&b"."[..]).is_err() );
|
||||||
assert!(number(&b". "[..]).is_err() );
|
assert!(number(&b". "[..]).is_err() );
|
||||||
assert_eq!(number(&b"1.0"[..]),
|
assert_eq!(number(&b"1.0"[..]),
|
||||||
IResult::Done(&b""[..], Value::Float(make_value_node(1.0))) );
|
IResult::Done(&b""[..], Value::Float(value_node!(1.0))) );
|
||||||
assert_eq!(number(&b"1."[..]),
|
assert_eq!(number(&b"1."[..]),
|
||||||
IResult::Done(&b""[..], Value::Float(make_value_node(1.0))) );
|
IResult::Done(&b""[..], Value::Float(value_node!(1.0))) );
|
||||||
assert_eq!(number(&b"1"[..]),
|
assert_eq!(number(&b"1"[..]),
|
||||||
IResult::Done(&b""[..], Value::Int(make_value_node(1))) );
|
IResult::Done(&b""[..], Value::Int(value_node!(1))) );
|
||||||
assert_eq!(number(&b".1"[..]),
|
assert_eq!(number(&b".1"[..]),
|
||||||
IResult::Done(&b""[..], Value::Float(make_value_node(0.1))) );
|
IResult::Done(&b""[..], Value::Float(value_node!(0.1))) );
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_symbol_parsing() {
|
fn test_symbol_parsing() {
|
||||||
assert_eq!(symbol(&b"foo"[..]),
|
assert_eq!(symbol(&b"foo"[..]),
|
||||||
IResult::Done(&b""[..], Value::Symbol(make_value_node("foo".to_string()))) );
|
IResult::Done(&b""[..], Value::Symbol(value_node!("foo".to_string()))) );
|
||||||
assert_eq!(symbol(&b"foo-bar"[..]),
|
assert_eq!(symbol(&b"foo-bar"[..]),
|
||||||
IResult::Done(&b""[..], Value::Symbol(make_value_node("foo-bar".to_string()))) );
|
IResult::Done(&b""[..], Value::Symbol(value_node!("foo-bar".to_string()))) );
|
||||||
assert_eq!(symbol(&b"foo_bar"[..]),
|
assert_eq!(symbol(&b"foo_bar"[..]),
|
||||||
IResult::Done(&b""[..], Value::Symbol(make_value_node("foo_bar".to_string()))) );
|
IResult::Done(&b""[..], Value::Symbol(value_node!("foo_bar".to_string()))) );
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1007,14 +1021,14 @@ mod test {
|
|||||||
},
|
},
|
||||||
Statement::Let{
|
Statement::Let{
|
||||||
name: "foo".to_string(),
|
name: "foo".to_string(),
|
||||||
value: Expression::Simple(Value::Int(make_value_node(1)))
|
value: Expression::Simple(Value::Int(value_node!(1)))
|
||||||
},
|
},
|
||||||
Statement::Expression(
|
Statement::Expression(
|
||||||
Expression::Binary(
|
Expression::Binary(
|
||||||
BinaryOpDef{
|
BinaryOpDef{
|
||||||
kind: BinaryExprType::Add,
|
kind: BinaryExprType::Add,
|
||||||
left: Value::Int(make_value_node(1)),
|
left: Value::Int(value_node!(1)),
|
||||||
right: Box::new(Expression::Simple(Value::Int(make_value_node(1)))),
|
right: Box::new(Expression::Simple(Value::Int(value_node!(1)))),
|
||||||
pos: None,
|
pos: None,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user