mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
cargo fmt pass with the new rustfmt-preview
This commit is contained in:
parent
f105d93c14
commit
bafe5b3c30
58
src/ast.rs
58
src/ast.rs
@ -33,11 +33,11 @@ pub struct ParseError {
|
||||
|
||||
impl std::fmt::Display for ParseError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||
write!(f,
|
||||
write!(
|
||||
f,
|
||||
"Parsing Error {} at line: {} column: {}",
|
||||
self.description,
|
||||
self.pos.line,
|
||||
self.pos.column)
|
||||
self.description, self.pos.line, self.pos.column
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,7 +47,6 @@ impl std::error::Error for ParseError {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
macro_rules! enum_type_equality {
|
||||
( $slf:ident, $r:expr, $( $l:pat ),* ) => {
|
||||
match $slf {
|
||||
@ -397,13 +396,17 @@ impl Value {
|
||||
|
||||
/// Returns true if called on a Value that is the same type as itself.
|
||||
pub fn type_equal(&self, target: &Self) -> bool {
|
||||
enum_type_equality!(self, target, &Value::Int(_),
|
||||
enum_type_equality!(
|
||||
self,
|
||||
target,
|
||||
&Value::Int(_),
|
||||
&Value::Float(_),
|
||||
&Value::String(_),
|
||||
&Value::Symbol(_),
|
||||
&Value::Tuple(_),
|
||||
&Value::List(_),
|
||||
&Value::Selector(_))
|
||||
&Value::Selector(_)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -512,10 +515,11 @@ impl MacroDef {
|
||||
return false;
|
||||
}
|
||||
|
||||
fn validate_value_symbols<'a>(&self,
|
||||
fn validate_value_symbols<'a>(
|
||||
&self,
|
||||
stack: &mut Vec<&'a Expression>,
|
||||
val: &'a Value)
|
||||
-> HashSet<String> {
|
||||
val: &'a Value,
|
||||
) -> HashSet<String> {
|
||||
let mut bad_symbols = HashSet::new();
|
||||
if let &Value::Symbol(ref name) = val {
|
||||
if !self.symbol_is_in_args(&name.val) {
|
||||
@ -572,11 +576,9 @@ impl MacroDef {
|
||||
stack.push(expr);
|
||||
}
|
||||
}
|
||||
&Expression::Call(ref def) => {
|
||||
for expr in def.arglist.iter() {
|
||||
&Expression::Call(ref def) => for expr in def.arglist.iter() {
|
||||
stack.push(expr);
|
||||
}
|
||||
}
|
||||
},
|
||||
&Expression::Simple(ref val) => {
|
||||
let mut syms_set = self.validate_value_symbols(&mut stack, val);
|
||||
bad_symbols.extend(syms_set.drain());
|
||||
@ -707,12 +709,15 @@ mod ast_test {
|
||||
let def = MacroDef {
|
||||
argdefs: vec![value_node!("foo".to_string(), 1, 0)],
|
||||
fields: vec![
|
||||
(make_tok!("f1", 1, 1), Expression::Binary(BinaryOpDef{
|
||||
(
|
||||
make_tok!("f1", 1, 1),
|
||||
Expression::Binary(BinaryOpDef {
|
||||
kind: BinaryExprType::Add,
|
||||
left: Value::Symbol(value_node!("foo".to_string(), 1, 1)),
|
||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||
pos: Position::new(1, 0),
|
||||
})),
|
||||
}),
|
||||
),
|
||||
],
|
||||
pos: Position::new(1, 0),
|
||||
};
|
||||
@ -724,12 +729,15 @@ mod ast_test {
|
||||
let def = MacroDef {
|
||||
argdefs: vec![value_node!("foo".to_string(), 1, 0)],
|
||||
fields: vec![
|
||||
(make_tok!("f1", 1, 1), Expression::Binary(BinaryOpDef{
|
||||
(
|
||||
make_tok!("f1", 1, 1),
|
||||
Expression::Binary(BinaryOpDef {
|
||||
kind: BinaryExprType::Add,
|
||||
left: Value::Symbol(value_node!("bar".to_string(), 1, 1)),
|
||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||
pos: Position::new(1, 0),
|
||||
})),
|
||||
}),
|
||||
),
|
||||
],
|
||||
pos: Position::new(1, 0),
|
||||
};
|
||||
@ -743,13 +751,16 @@ mod ast_test {
|
||||
let def = MacroDef {
|
||||
argdefs: vec![value_node!("foo".to_string(), 1, 0)],
|
||||
fields: vec![
|
||||
(make_tok!("f1", 1, 1), Expression::Binary(BinaryOpDef{
|
||||
(
|
||||
make_tok!("f1", 1, 1),
|
||||
Expression::Binary(BinaryOpDef {
|
||||
kind: BinaryExprType::Add,
|
||||
left: Value::Selector(make_selector!(make_expr!("foo", 1, 1) => [
|
||||
make_tok!("quux", 1, 1) ] => 1, 1)),
|
||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||
pos: Position::new(1, 0),
|
||||
})),
|
||||
}),
|
||||
),
|
||||
],
|
||||
pos: Position::new(1, 0),
|
||||
};
|
||||
@ -761,13 +772,16 @@ mod ast_test {
|
||||
let def = MacroDef {
|
||||
argdefs: vec![value_node!("foo".to_string(), 1, 0)],
|
||||
fields: vec![
|
||||
(make_tok!("f1", 1, 1), Expression::Binary(BinaryOpDef{
|
||||
(
|
||||
make_tok!("f1", 1, 1),
|
||||
Expression::Binary(BinaryOpDef {
|
||||
kind: BinaryExprType::Add,
|
||||
left: Value::Selector(make_selector!(make_expr!("bar", 1, 1) => [
|
||||
make_tok!("quux", 1, 1) ] => 1, 1)),
|
||||
right: Box::new(Expression::Simple(Value::Int(value_node!(1, 1, 1)))),
|
||||
pos: Position::new(1, 0),
|
||||
})),
|
||||
}),
|
||||
),
|
||||
],
|
||||
pos: Position::new(1, 0),
|
||||
};
|
||||
|
@ -34,12 +34,14 @@ impl JsonConverter {
|
||||
Ok(serde_json::Value::Array(v))
|
||||
}
|
||||
|
||||
fn convert_tuple(&self,
|
||||
items: &Vec<(ast::Positioned<String>, Rc<Val>)>)
|
||||
-> Result<serde_json::Value> {
|
||||
fn convert_tuple(
|
||||
&self,
|
||||
items: &Vec<(ast::Positioned<String>, Rc<Val>)>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let mut mp = serde_json::Map::new();
|
||||
for &(ref k, ref v) in items.iter() {
|
||||
mp.entry(k.val.clone()).or_insert(try!(self.convert_value(v)));
|
||||
mp.entry(k.val.clone())
|
||||
.or_insert(try!(self.convert_value(v)));
|
||||
}
|
||||
Ok(serde_json::Value::Object(mp))
|
||||
}
|
||||
|
10
src/error.rs
10
src/error.rs
@ -75,10 +75,7 @@ impl fmt::Debug for Error {
|
||||
write!(
|
||||
w,
|
||||
"{}: \"{}\" {}:{}",
|
||||
self.err_type,
|
||||
self.msg,
|
||||
self.pos.line,
|
||||
self.pos.column
|
||||
self.err_type, self.msg, self.pos.line, self.pos.column
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -88,10 +85,7 @@ impl fmt::Display for Error {
|
||||
write!(
|
||||
w,
|
||||
"{}: \"{}\" {}:{}",
|
||||
self.err_type,
|
||||
self.msg,
|
||||
self.pos.line,
|
||||
self.pos.column
|
||||
self.err_type, self.msg, self.pos.line, self.pos.column
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -45,10 +45,12 @@ impl<V: Into<String> + Clone> Formatter<V> {
|
||||
for c in self.tmpl.chars() {
|
||||
if c == '@' && !should_escape {
|
||||
if count == self.args.len() {
|
||||
return Err(Box::new(error::Error::new("Too few arguments to string \
|
||||
return Err(Box::new(error::Error::new(
|
||||
"Too few arguments to string \
|
||||
formatter.",
|
||||
error::ErrorType::FormatError,
|
||||
pos.clone())));
|
||||
pos.clone(),
|
||||
)));
|
||||
}
|
||||
let arg = self.args[count].clone();
|
||||
let strval = arg.into();
|
||||
@ -61,10 +63,12 @@ impl<V: Into<String> + Clone> Formatter<V> {
|
||||
}
|
||||
}
|
||||
if self.args.len() != count {
|
||||
return Err(Box::new(error::Error::new("Too many arguments to string \
|
||||
return Err(Box::new(error::Error::new(
|
||||
"Too many arguments to string \
|
||||
formatter.",
|
||||
error::ErrorType::FormatError,
|
||||
pos.clone())));
|
||||
pos.clone(),
|
||||
)));
|
||||
}
|
||||
return Ok(buf);
|
||||
}
|
||||
@ -78,30 +82,21 @@ mod test {
|
||||
#[test]
|
||||
fn test_format_happy_path() {
|
||||
let formatter = Formatter::new("foo @ @ \\@", vec!["bar", "quux"]);
|
||||
let pos = Position {
|
||||
line: 0,
|
||||
column: 0,
|
||||
};
|
||||
let pos = Position { line: 0, column: 0 };
|
||||
assert_eq!(formatter.render(&pos).unwrap(), "foo bar quux @");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_happy_wrong_too_few_args() {
|
||||
let formatter = Formatter::new("foo @ @ \\@", vec!["bar"]);
|
||||
let pos = Position {
|
||||
line: 0,
|
||||
column: 0,
|
||||
};
|
||||
let pos = Position { line: 0, column: 0 };
|
||||
assert!(formatter.render(&pos).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_happy_wrong_too_many_args() {
|
||||
let formatter = Formatter::new("foo @ @ \\@", vec!["bar", "quux", "baz"]);
|
||||
let pos = Position {
|
||||
line: 0,
|
||||
column: 0,
|
||||
};
|
||||
let pos = Position { line: 0, column: 0 };
|
||||
assert!(formatter.render(&pos).is_err());
|
||||
}
|
||||
}
|
||||
|
897
src/parse.rs
897
src/parse.rs
File diff suppressed because it is too large
Load Diff
154
src/tokenizer.rs
154
src/tokenizer.rs
@ -15,8 +15,8 @@
|
||||
//! The tokenization stage of the ucg compiler.
|
||||
use nom_locate::LocatedSpan;
|
||||
use nom;
|
||||
use nom::{alpha, is_alphanumeric, digit, multispace};
|
||||
use nom::{InputLength, InputIter, Slice};
|
||||
use nom::{alpha, digit, is_alphanumeric, multispace};
|
||||
use nom::{InputIter, InputLength, Slice};
|
||||
use ast::*;
|
||||
use std;
|
||||
use std::result::Result;
|
||||
@ -204,9 +204,11 @@ named!(astok( Span ) -> Token,
|
||||
fn end_of_input(input: Span) -> nom::IResult<Span, Token> {
|
||||
match eof!(input,) {
|
||||
nom::IResult::Done(_, _) => {
|
||||
return nom::IResult::Done(input,
|
||||
return nom::IResult::Done(
|
||||
input,
|
||||
make_tok!(EOF => input.line as usize,
|
||||
input.get_column() as usize));
|
||||
input.get_column() as usize),
|
||||
);
|
||||
}
|
||||
nom::IResult::Incomplete(_) => {
|
||||
return nom::IResult::Incomplete(nom::Needed::Unknown);
|
||||
@ -220,22 +222,29 @@ fn end_of_input(input: Span) -> nom::IResult<Span, Token> {
|
||||
fn comment(input: Span) -> nom::IResult<Span, Token> {
|
||||
match tag!(input, "//") {
|
||||
nom::IResult::Done(rest, _) => {
|
||||
match alt!(rest, take_until_and_consume!("\r\n") | take_until_and_consume!("\n")) {
|
||||
match alt!(
|
||||
rest,
|
||||
take_until_and_consume!("\r\n") | take_until_and_consume!("\n")
|
||||
) {
|
||||
nom::IResult::Done(rest, cmt) => {
|
||||
return nom::IResult::Done(rest,
|
||||
return nom::IResult::Done(
|
||||
rest,
|
||||
make_tok!(CMT => cmt.fragment.to_string(),
|
||||
input.line as usize,
|
||||
input.get_column() as usize));
|
||||
input.get_column() as usize),
|
||||
);
|
||||
}
|
||||
// If we didn't find a new line then we just grab everything.
|
||||
_ => {
|
||||
let blen = rest.input_len();
|
||||
let next = rest.slice(blen..);
|
||||
let tok = rest.slice(..blen);
|
||||
return nom::IResult::Done(next,
|
||||
return nom::IResult::Done(
|
||||
next,
|
||||
make_tok!(CMT => tok.fragment.to_string(),
|
||||
input.line as usize, input.get_column() as usize
|
||||
));
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -461,11 +470,13 @@ pub fn pos(i: TokenIter) -> nom::IResult<TokenIter, Position, ParseError> {
|
||||
let tok = &i[0];
|
||||
let line = tok.pos.line;
|
||||
let column = tok.pos.column;
|
||||
nom::IResult::Done(i.clone(),
|
||||
nom::IResult::Done(
|
||||
i.clone(),
|
||||
Position {
|
||||
line: line,
|
||||
column: column,
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// TokenIter wraps a slice of Tokens and implements the various necessary
|
||||
@ -528,7 +539,8 @@ impl<'a> InputIter for TokenIter<'a> {
|
||||
}
|
||||
|
||||
fn position<P>(&self, predicate: P) -> Option<usize>
|
||||
where P: Fn(Self::RawItem) -> bool
|
||||
where
|
||||
P: Fn(Self::RawItem) -> bool,
|
||||
{
|
||||
for (o, v) in self.iter_indices() {
|
||||
if predicate(v.clone()) {
|
||||
@ -580,10 +592,10 @@ mod tokenizer_test {
|
||||
|
||||
#[test]
|
||||
fn test_tokenize_one_of_each() {
|
||||
// 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2
|
||||
// 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7
|
||||
let result = tokenize(LocatedSpan::new("let import macro select as => [ ] { } ; = % / * \
|
||||
+ - . ( ) , 1 . foo \"bar\" // comment\n ;"));
|
||||
let result = tokenize(LocatedSpan::new(
|
||||
"let import macro select as => [ ] { } ; = % / * \
|
||||
+ - . ( ) , 1 . foo \"bar\" // comment\n ;",
|
||||
));
|
||||
assert!(result.is_ok(), format!("result {:?} is not ok", result));
|
||||
let v = result.unwrap();
|
||||
for (i, t) in v.iter().enumerate() {
|
||||
@ -606,40 +618,71 @@ mod tokenizer_test {
|
||||
fn test_parse_comment() {
|
||||
assert!(comment(LocatedSpan::new("// comment\n")).is_done());
|
||||
assert!(comment(LocatedSpan::new("// comment")).is_done());
|
||||
assert_eq!(comment(LocatedSpan::new("// comment\n")),
|
||||
nom::IResult::Done(LocatedSpan{fragment: "", offset: 11, line: 2},
|
||||
assert_eq!(
|
||||
comment(LocatedSpan::new("// comment\n")),
|
||||
nom::IResult::Done(
|
||||
LocatedSpan {
|
||||
fragment: "",
|
||||
offset: 11,
|
||||
line: 2,
|
||||
},
|
||||
Token {
|
||||
typ: TokenType::COMMENT,
|
||||
fragment: " comment".to_string(),
|
||||
pos: Position { line: 1, column: 1 },
|
||||
}));
|
||||
}
|
||||
)
|
||||
);
|
||||
assert!(comment(LocatedSpan::new("// comment\r\n")).is_done());
|
||||
assert_eq!(comment(LocatedSpan::new("// comment\r\n")),
|
||||
nom::IResult::Done(LocatedSpan{fragment: "", offset: 12, line: 2},
|
||||
Token{
|
||||
typ: TokenType::COMMENT,
|
||||
fragment: " comment".to_string(),
|
||||
pos: Position{column: 1, line: 1}
|
||||
}));
|
||||
assert!(comment(LocatedSpan::new("// comment\r\n ")).is_done());
|
||||
assert_eq!(comment(LocatedSpan::new("// comment\r\n ")),
|
||||
nom::IResult::Done(LocatedSpan{fragment: " ", offset: 12, line: 2},
|
||||
assert_eq!(
|
||||
comment(LocatedSpan::new("// comment\r\n")),
|
||||
nom::IResult::Done(
|
||||
LocatedSpan {
|
||||
fragment: "",
|
||||
offset: 12,
|
||||
line: 2,
|
||||
},
|
||||
Token {
|
||||
typ: TokenType::COMMENT,
|
||||
fragment: " comment".to_string(),
|
||||
pos: Position { column: 1, line: 1 },
|
||||
}));
|
||||
}
|
||||
)
|
||||
);
|
||||
assert!(comment(LocatedSpan::new("// comment\r\n ")).is_done());
|
||||
assert_eq!(
|
||||
comment(LocatedSpan::new("// comment\r\n ")),
|
||||
nom::IResult::Done(
|
||||
LocatedSpan {
|
||||
fragment: " ",
|
||||
offset: 12,
|
||||
line: 2,
|
||||
},
|
||||
Token {
|
||||
typ: TokenType::COMMENT,
|
||||
fragment: " comment".to_string(),
|
||||
pos: Position { column: 1, line: 1 },
|
||||
}
|
||||
)
|
||||
);
|
||||
assert!(comment(LocatedSpan::new("// comment")).is_done());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_match_word() {
|
||||
let input = vec![Token{
|
||||
let input = vec![
|
||||
Token {
|
||||
fragment: "foo".to_string(),
|
||||
typ: TokenType::BAREWORD,
|
||||
pos: Position{line: 1, column: 1}
|
||||
}];
|
||||
let result = word!(TokenIter{source: input.as_slice()}, "foo");
|
||||
pos: Position { line: 1, column: 1 },
|
||||
},
|
||||
];
|
||||
let result = word!(
|
||||
TokenIter {
|
||||
source: input.as_slice(),
|
||||
},
|
||||
"foo"
|
||||
);
|
||||
match result {
|
||||
nom::IResult::Done(_, tok) => assert_eq!(tok, input[0]),
|
||||
res => assert!(false, format!("Fail: {:?}", res)),
|
||||
@ -648,12 +691,19 @@ mod tokenizer_test {
|
||||
|
||||
#[test]
|
||||
fn test_match_word_empty_input() {
|
||||
let input = vec![Token{
|
||||
let input = vec![
|
||||
Token {
|
||||
fragment: "".to_string(),
|
||||
typ: TokenType::END,
|
||||
pos: Position { line: 1, column: 1 },
|
||||
}];
|
||||
let result = word!(TokenIter{source: input.as_slice()}, "foo");
|
||||
},
|
||||
];
|
||||
let result = word!(
|
||||
TokenIter {
|
||||
source: input.as_slice(),
|
||||
},
|
||||
"foo"
|
||||
);
|
||||
match result {
|
||||
nom::IResult::Done(_, _) => assert!(false, "Should have been an error but was Done"),
|
||||
nom::IResult::Incomplete(_) => {
|
||||
@ -667,12 +717,19 @@ mod tokenizer_test {
|
||||
|
||||
#[test]
|
||||
fn test_match_punct() {
|
||||
let input = vec![Token{
|
||||
let input = vec![
|
||||
Token {
|
||||
fragment: "!".to_string(),
|
||||
typ: TokenType::PUNCT,
|
||||
pos: Position{line: 1, column: 1}
|
||||
}];
|
||||
let result = punct!(TokenIter{source: input.as_slice()}, "!");
|
||||
pos: Position { line: 1, column: 1 },
|
||||
},
|
||||
];
|
||||
let result = punct!(
|
||||
TokenIter {
|
||||
source: input.as_slice(),
|
||||
},
|
||||
"!"
|
||||
);
|
||||
match result {
|
||||
nom::IResult::Done(_, tok) => assert_eq!(tok, input[0]),
|
||||
res => assert!(false, format!("Fail: {:?}", res)),
|
||||
@ -681,12 +738,19 @@ mod tokenizer_test {
|
||||
|
||||
#[test]
|
||||
fn test_match_type() {
|
||||
let input = vec![Token{
|
||||
let input = vec![
|
||||
Token {
|
||||
fragment: "foo".to_string(),
|
||||
typ: TokenType::BAREWORD,
|
||||
pos: Position{line: 1, column: 1}
|
||||
}];
|
||||
let result = match_type!(TokenIter{source: input.as_slice()}, BAREWORD);
|
||||
pos: Position { line: 1, column: 1 },
|
||||
},
|
||||
];
|
||||
let result = match_type!(
|
||||
TokenIter {
|
||||
source: input.as_slice(),
|
||||
},
|
||||
BAREWORD
|
||||
);
|
||||
match result {
|
||||
nom::IResult::Done(_, tok) => assert_eq!(tok, input[0]),
|
||||
res => assert!(false, format!("Fail: {:?}", res)),
|
||||
|
Loading…
x
Reference in New Issue
Block a user