mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
cargo fmt
This commit is contained in:
parent
4f3cc3dbf6
commit
0be96dc851
11
src/ast.rs
11
src/ast.rs
@ -80,7 +80,11 @@ impl<T> LocatedNode<T> {
|
|||||||
|
|
||||||
// TODO(jwall): This should take a line and a column as argumentsn now.
|
// TODO(jwall): This should take a line and a column as argumentsn now.
|
||||||
pub fn make_value_node<T>(v: T, line: usize, column: usize) -> LocatedNode<T> {
|
pub fn make_value_node<T>(v: T, line: usize, column: usize) -> LocatedNode<T> {
|
||||||
LocatedNode::new(v, Position{line: line, column: column})
|
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.
|
||||||
@ -191,8 +195,7 @@ impl<T: PartialEq> PartialEq for Positioned<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Eq> Eq for Positioned<T> {
|
impl<T: Eq> Eq for Positioned<T> {}
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Ord> Ord for Positioned<T> {
|
impl<T: Ord> Ord for Positioned<T> {
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
@ -453,7 +456,7 @@ 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![
|
argdefs: vec![
|
||||||
Positioned::new("foo".to_string())
|
Positioned::new("foo".to_string())
|
||||||
],
|
],
|
||||||
|
@ -350,8 +350,7 @@ impl Builder {
|
|||||||
if self.assets.get(&positioned_sym).is_none() {
|
if self.assets.get(&positioned_sym).is_none() {
|
||||||
let mut b = Self::new();
|
let mut b = Self::new();
|
||||||
try!(b.build_file(&val));
|
try!(b.build_file(&val));
|
||||||
let fields: Vec<(Positioned<String>, Rc<Val>)> =
|
let fields: Vec<(Positioned<String>, Rc<Val>)> = b.out.drain().collect();
|
||||||
b.out.drain().collect();
|
|
||||||
let result = Rc::new(Val::Tuple(fields));
|
let result = Rc::new(Val::Tuple(fields));
|
||||||
self.assets.entry(positioned_sym).or_insert(result.clone());
|
self.assets.entry(positioned_sym).or_insert(result.clone());
|
||||||
self.files.insert(val.clone());
|
self.files.insert(val.clone());
|
||||||
@ -1142,7 +1141,11 @@ mod test {
|
|||||||
fn test_let_statement() {
|
fn test_let_statement() {
|
||||||
let mut b = Builder::new();
|
let mut b = Builder::new();
|
||||||
let stmt = Statement::Let {
|
let stmt = Statement::Let {
|
||||||
name: Token::new("foo", Position{line: 1, column: 1}),
|
name: Token::new("foo",
|
||||||
|
Position {
|
||||||
|
line: 1,
|
||||||
|
column: 1,
|
||||||
|
}),
|
||||||
value: Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1))),
|
value: Expression::Simple(Value::String(make_value_node("bar".to_string(), 1, 1))),
|
||||||
};
|
};
|
||||||
b.build_stmt(&stmt).unwrap();
|
b.build_stmt(&stmt).unwrap();
|
||||||
|
@ -52,7 +52,8 @@ impl<V: Into<String> + Clone> Formatter<V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.args.len() != count {
|
if self.args.len() != count {
|
||||||
return Err(Box::new(BuildError::FormatError("Too many arguments to string formatter."
|
return Err(Box::new(BuildError::FormatError("Too many arguments to string \
|
||||||
|
formatter."
|
||||||
.to_string())));
|
.to_string())));
|
||||||
}
|
}
|
||||||
return Ok(buf);
|
return Ok(buf);
|
||||||
|
59
src/parse.rs
59
src/parse.rs
@ -56,7 +56,13 @@ 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 => ("", Position{line: 0, column: 0}),
|
None => {
|
||||||
|
("",
|
||||||
|
Position {
|
||||||
|
line: 0,
|
||||||
|
column: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
Some(ref bs) => (bs.fragment.borrow(), bs.pos.clone()),
|
Some(ref bs) => (bs.fragment.borrow(), bs.pos.clone()),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -69,7 +75,7 @@ fn triple_to_number(v: (Option<Token>, Option<Token>, Option<Token>)) -> ParseRe
|
|||||||
if v.0.is_none() && has_dot {
|
if v.0.is_none() && has_dot {
|
||||||
pref_pos = v.1.unwrap().pos;
|
pref_pos = v.1.unwrap().pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
let suf = match v.2 {
|
let suf = match v.2 {
|
||||||
None => "",
|
None => "",
|
||||||
Some(ref bs) => &bs.fragment,
|
Some(ref bs) => &bs.fragment,
|
||||||
@ -170,12 +176,16 @@ named!(simple_expression( Span ) -> Expression,
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
fn tuple_to_binary_expression(tpl: (Span, BinaryExprType, Value, Expression)) -> ParseResult<Expression> {
|
fn tuple_to_binary_expression(tpl: (Span, BinaryExprType, Value, Expression))
|
||||||
|
-> ParseResult<Expression> {
|
||||||
Ok(Expression::Binary(BinaryOpDef {
|
Ok(Expression::Binary(BinaryOpDef {
|
||||||
kind: tpl.1,
|
kind: tpl.1,
|
||||||
left: tpl.2,
|
left: tpl.2,
|
||||||
right: Box::new(tpl.3),
|
right: Box::new(tpl.3),
|
||||||
pos: Some(Position{line: tpl.0.line as usize, column: tpl.0.offset as usize}),
|
pos: Some(Position {
|
||||||
|
line: tpl.0.line as usize,
|
||||||
|
column: tpl.0.offset as usize,
|
||||||
|
}),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,7 +237,7 @@ named!(grouped_expression( Span ) -> Expression,
|
|||||||
|
|
||||||
fn assert_nonempty_list<T>(v: Vec<T>) -> ParseResult<Vec<T>> {
|
fn assert_nonempty_list<T>(v: Vec<T>) -> ParseResult<Vec<T>> {
|
||||||
if v.is_empty() {
|
if v.is_empty() {
|
||||||
return Err(Box::new(ParseError::EmptyExpression("Selectors can't be empty.".to_string())))
|
return Err(Box::new(ParseError::EmptyExpression("Selectors can't be empty.".to_string())));
|
||||||
}
|
}
|
||||||
return Ok(v);
|
return Ok(v);
|
||||||
}
|
}
|
||||||
@ -243,7 +253,10 @@ fn tuple_to_copy(t: (Span, SelectorList, FieldList)) -> ParseResult<Expression>
|
|||||||
Ok(Expression::Copy(CopyDef {
|
Ok(Expression::Copy(CopyDef {
|
||||||
selector: t.1,
|
selector: t.1,
|
||||||
fields: t.2,
|
fields: t.2,
|
||||||
pos: Some(Position{line: t.0.line as usize, column: t.0.offset as usize}),
|
pos: Some(Position {
|
||||||
|
line: t.0.line as usize,
|
||||||
|
column: t.0.offset as usize,
|
||||||
|
}),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,7 +288,10 @@ fn tuple_to_macro(mut t: (Span, Vec<Value>, Value)) -> ParseResult<Expression> {
|
|||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
fields: v.val,
|
fields: v.val,
|
||||||
pos: Some(Position{line: t.0.line as usize, column: t.0.offset as usize}),
|
pos: Some(Position {
|
||||||
|
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.
|
||||||
@ -310,7 +326,10 @@ 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: Some(Position{line: t.0.line as usize, column: t.0.offset as usize}),
|
pos: Some(Position {
|
||||||
|
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.
|
||||||
@ -361,7 +380,10 @@ fn tuple_to_call(t: (Span, Value, Vec<Expression>)) -> ParseResult<Expression> {
|
|||||||
Ok(Expression::Call(CallDef {
|
Ok(Expression::Call(CallDef {
|
||||||
macroref: sl.val,
|
macroref: sl.val,
|
||||||
arglist: t.2,
|
arglist: t.2,
|
||||||
pos: Some(Position{line: t.0.line as usize, column: t.0.offset as usize}),
|
pos: Some(Position {
|
||||||
|
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))))
|
||||||
@ -609,7 +631,7 @@ mod test {
|
|||||||
},
|
},
|
||||||
value: Expression::Simple(Value::Float(value_node!(1.0, Position{line: 1, column: 11})))
|
value: Expression::Simple(Value::Float(value_node!(1.0, Position{line: 1, column: 11})))
|
||||||
}));
|
}));
|
||||||
|
|
||||||
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{
|
||||||
@ -848,13 +870,13 @@ mod test {
|
|||||||
(Token::new("foo", Position{line: 1, column: 25}),
|
(Token::new("foo", Position{line: 1, column: 25}),
|
||||||
Expression::Simple(Value::Symbol(value_node!("arg1".to_string(), Position{line: 1, column: 31})))),
|
Expression::Simple(Value::Symbol(value_node!("arg1".to_string(), Position{line: 1, column: 31})))),
|
||||||
],
|
],
|
||||||
// FIXME(jwall): I think this is incorrect.
|
// FIXME(jwall): I think this is incorrect.
|
||||||
pos: Some(Position{line: 1, column: 0}),
|
pos: Some(Position{line: 1, column: 0}),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
let select_expr = "select foo, 1, { foo = 2 }";
|
let select_expr = "select foo, 1, { foo = 2 }";
|
||||||
assert_eq!(expression(LocatedSpan::new(select_expr)),
|
assert_eq!(expression(LocatedSpan::new(select_expr)),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
offset: select_expr.len(),
|
offset: select_expr.len(),
|
||||||
@ -871,8 +893,8 @@ mod test {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
let call_expr = "foo.bar (1, \"foo\")";
|
let call_expr = "foo.bar (1, \"foo\")";
|
||||||
assert_eq!(expression(LocatedSpan::new(call_expr)),
|
assert_eq!(expression(LocatedSpan::new(call_expr)),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
offset: call_expr.len(),
|
offset: call_expr.len(),
|
||||||
@ -889,7 +911,7 @@ mod test {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(expression(LocatedSpan::new("(1 + 1)")),
|
assert_eq!(expression(LocatedSpan::new("(1 + 1)")),
|
||||||
IResult::Done(LocatedSpan {
|
IResult::Done(LocatedSpan {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
offset: 7,
|
offset: 7,
|
||||||
@ -1180,7 +1202,7 @@ mod test {
|
|||||||
Expression::Simple(Value::String(value_node!("1".to_string(), Position{line: 1, column: 18}))))
|
Expression::Simple(Value::String(value_node!("1".to_string(), Position{line: 1, column: 18}))))
|
||||||
], Position{line: 1, column: 0}))));
|
], Position{line: 1, column: 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 {
|
||||||
fragment: "",
|
fragment: "",
|
||||||
offset: tuple_expr.len(),
|
offset: tuple_expr.len(),
|
||||||
@ -1256,8 +1278,7 @@ mod test {
|
|||||||
assert!(bad_result.is_err() );
|
assert!(bad_result.is_err() );
|
||||||
|
|
||||||
// Valid parsing tree
|
// Valid parsing tree
|
||||||
let input =
|
let input = LocatedSpan::new("import \"mylib\" as lib;let foo = 1;1+1;");
|
||||||
LocatedSpan::new("import \"mylib\" as lib;let foo = 1;1+1;");
|
|
||||||
let result = parse(input);
|
let result = parse(input);
|
||||||
assert!(result.is_done() );
|
assert!(result.is_done() );
|
||||||
let tpl = result.unwrap();
|
let tpl = result.unwrap();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user