Formatting: cargo fmt.

This commit is contained in:
Jeremy Wall 2018-05-14 21:34:38 -05:00
parent f4b5cf1b0c
commit 00c1e1751a
11 changed files with 138 additions and 137 deletions

View File

@ -12,15 +12,15 @@
// 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 std; use std;
use std::collections::HashSet;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::convert::Into;
use std::cmp::Ordering;
use std::cmp::PartialOrd;
use std::cmp::Eq; use std::cmp::Eq;
use std::cmp::Ordering;
use std::cmp::PartialEq; use std::cmp::PartialEq;
use std::hash::Hasher; use std::cmp::PartialOrd;
use std::collections::HashSet;
use std::convert::Into;
use std::hash::Hash; use std::hash::Hash;
use std::hash::Hasher;
macro_rules! enum_type_equality { macro_rules! enum_type_equality {
( $slf:ident, $r:expr, $( $l:pat ),* ) => { ( $slf:ident, $r:expr, $( $l:pat ),* ) => {
@ -106,10 +106,10 @@ impl Borrow<str> for Token {
/// Helper macro for making a Positioned Value. /// Helper macro for making a Positioned Value.
macro_rules! value_node { macro_rules! value_node {
($v: expr, $p: expr) => { ($v:expr, $p:expr) => {
Positioned::new_with_pos($v, $p) Positioned::new_with_pos($v, $p)
}; };
($v: expr, $l: expr, $c: expr) => { ($v:expr, $l:expr, $c:expr) => {
Positioned::new($v, $l, $c) Positioned::new($v, $l, $c)
}; };
} }
@ -117,31 +117,31 @@ macro_rules! value_node {
/// Helper macro for making a Token. /// Helper macro for making a Token.
#[allow(unused_macros)] #[allow(unused_macros)]
macro_rules! make_tok { macro_rules! make_tok {
(EOF => $l: expr, $c: expr) => { (EOF => $l:expr, $c:expr) => {
Token::new("", TokenType::END, $l, $c) Token::new("", TokenType::END, $l, $c)
}; };
(WS => $l: expr, $c: expr) => { (WS => $l:expr, $c:expr) => {
Token::new("", TokenType::WS, $l, $c) Token::new("", TokenType::WS, $l, $c)
}; };
(CMT => $e: expr, $l: expr, $c: expr) => { (CMT => $e:expr, $l:expr, $c:expr) => {
Token::new($e, TokenType::COMMENT, $l, $c) Token::new($e, TokenType::COMMENT, $l, $c)
}; };
(QUOT => $e: expr, $l: expr, $c: expr) => { (QUOT => $e:expr, $l:expr, $c:expr) => {
Token::new($e, TokenType::QUOTED, $l, $c) Token::new($e, TokenType::QUOTED, $l, $c)
}; };
(PUNCT => $e: expr, $l: expr, $c: expr) => { (PUNCT => $e:expr, $l:expr, $c:expr) => {
Token::new($e, TokenType::PUNCT, $l, $c) Token::new($e, TokenType::PUNCT, $l, $c)
}; };
(DIGIT => $e: expr, $l: expr, $c: expr) => { (DIGIT => $e:expr, $l:expr, $c:expr) => {
Token::new($e, TokenType::DIGIT, $l, $c) Token::new($e, TokenType::DIGIT, $l, $c)
}; };
($e: expr, $l: expr, $c: expr) => { ($e:expr, $l:expr, $c:expr) => {
Token::new($e, TokenType::BAREWORD, $l, $c) Token::new($e, TokenType::BAREWORD, $l, $c)
}; };
} }
@ -149,15 +149,15 @@ macro_rules! make_tok {
/// Helper macro for making expressions. /// Helper macro for making expressions.
#[allow(unused_macros)] #[allow(unused_macros)]
macro_rules! make_expr { macro_rules! make_expr {
($e: expr) => { ($e:expr) => {
make_expr!($e, 1, 1) make_expr!($e, 1, 1)
}; };
($e: expr, $l: expr, $c: expr) => { ($e:expr, $l:expr, $c:expr) => {
Expression::Simple(Value::Symbol(Positioned::new($e.to_string(), $l, $c))) Expression::Simple(Value::Symbol(Positioned::new($e.to_string(), $l, $c)))
}; };
($e: expr => int, $l: expr, $c: expr) => { ($e:expr => int, $l:expr, $c:expr) => {
Expression::Simple(Value::Int(Positioned::new($e, $l, $c))) Expression::Simple(Value::Int(Positioned::new($e, $l, $c)))
}; };
} }

View File

@ -13,23 +13,23 @@
// limitations under the License. // limitations under the License.
//! The build stage of the ucg compiler. //! The build stage of the ucg compiler.
use std::env;
use std::fs::File;
use std::io::Read;
use std::error::Error;
use std::collections::{HashMap, HashSet, VecDeque};
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet, VecDeque};
use std::convert::From;
use std::env;
use std::error::Error;
use std::fmt; use std::fmt;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::fs::File;
use std::io::Read;
use std::ops::Deref; use std::ops::Deref;
use std::rc::Rc; use std::rc::Rc;
use std::convert::From;
use tokenizer::Span;
use ast::tree::*; use ast::tree::*;
use error;
use format; use format;
use parse::parse; use parse::parse;
use error; use tokenizer::Span;
impl MacroDef { impl MacroDef {
/// Expands a ucg Macro using the given arguments into a new Tuple. /// Expands a ucg Macro using the given arguments into a new Tuple.
@ -295,7 +295,7 @@ pub struct Builder {
} }
macro_rules! eval_binary_expr { macro_rules! eval_binary_expr {
($case: pat, $pos: ident, $rside: ident, $result: expr, $msg: expr) => { ($case:pat, $pos:ident, $rside:ident, $result:expr, $msg:expr) => {
match $rside.as_ref() { match $rside.as_ref() {
$case => { $case => {
return Ok(Rc::new($result)); return Ok(Rc::new($result));

View File

@ -13,9 +13,9 @@
// limitations under the License. // limitations under the License.
//! An environment variable converter. //! An environment variable converter.
use std::rc::Rc;
use std::io::Write;
use std::io::Result; use std::io::Result;
use std::io::Write;
use std::rc::Rc;
use ast::tree::*; use ast::tree::*;
use build::Val; use build::Val;

View File

@ -13,9 +13,9 @@
// limitations under the License. // limitations under the License.
//! Contains code for converting a UCG Val into the command line flag output target. //! Contains code for converting a UCG Val into the command line flag output target.
use std::rc::Rc;
use std::io::Write;
use std::io::Result; use std::io::Result;
use std::io::Write;
use std::rc::Rc;
use build::Val; use build::Val;
use convert::traits::Converter; use convert::traits::Converter;

View File

@ -8,9 +8,9 @@
// 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.
//! Flags contains code for converting a UCG Val into the json output target. //! Flags contains code for converting a UCG Val into the json output target.
use std::rc::Rc;
use std::io::Write;
use std::io::Result; use std::io::Result;
use std::io::Write;
use std::rc::Rc;
use serde_json; use serde_json;

View File

@ -13,9 +13,9 @@
// limitations under the License. // limitations under the License.
//! The conversion stage of the ucg compiler. //! The conversion stage of the ucg compiler.
pub mod env;
pub mod flags; pub mod flags;
pub mod json; pub mod json;
pub mod env;
pub mod traits; pub mod traits;
use std::io; use std::io;

View File

@ -13,9 +13,9 @@
// limitations under the License. // limitations under the License.
//! The traits used by the ucg compiler for converting Val intermediate format into the output formats.. //! The traits used by the ucg compiler for converting Val intermediate format into the output formats..
use std::rc::Rc;
use std::io::Write;
use std::io::Result; use std::io::Result;
use std::io::Write;
use std::rc::Rc;
use build::Val; use build::Val;

View File

@ -409,17 +409,17 @@ extern crate serde_json;
pub mod ast; pub mod ast;
#[macro_use] #[macro_use]
pub mod tokenizer; pub mod tokenizer;
pub mod parse;
pub mod build; pub mod build;
pub mod convert; pub mod convert;
pub mod error; pub mod error;
pub mod parse;
mod format; mod format;
pub use ast::tree::Value;
pub use ast::tree::Expression; pub use ast::tree::Expression;
pub use ast::tree::Statement; pub use ast::tree::Statement;
pub use ast::tree::Value;
pub use parse::parse;
pub use build::Builder; pub use build::Builder;
pub use build::Val; pub use build::Val;
pub use parse::parse;

View File

@ -16,12 +16,12 @@ extern crate clap;
extern crate ucglib; extern crate ucglib;
use std::fs::File; use std::fs::File;
use std::rc::Rc;
use std::io; use std::io;
use std::process; use std::process;
use std::rc::Rc;
use ucglib::build::Val;
use ucglib::build; use ucglib::build;
use ucglib::build::Val;
use ucglib::convert::ConverterRunner; use ucglib::convert::ConverterRunner;
// TODO(jwall): List the target output types automatically. // TODO(jwall): List the target output types automatically.

View File

@ -13,17 +13,17 @@
// limitations under the License. // limitations under the License.
//! The Parsing stage of the ucg compiler. //! The Parsing stage of the ucg compiler.
use std::str::FromStr;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::str::FromStr;
use nom_locate::LocatedSpan;
use nom; use nom;
use nom::InputLength;
use nom::IResult; use nom::IResult;
use nom::InputLength;
use nom_locate::LocatedSpan;
use ast::tree::*; use ast::tree::*;
use tokenizer::*;
use error; use error;
use tokenizer::*;
type NomResult<'a, O> = nom::IResult<TokenIter<'a>, O, error::Error>; type NomResult<'a, O> = nom::IResult<TokenIter<'a>, O, error::Error>;
@ -844,14 +844,14 @@ mod test {
use super::*; use super::*;
use tokenizer::{tokenize, TokenIter}; use tokenizer::{tokenize, TokenIter};
use nom_locate::LocatedSpan;
use nom::IResult; use nom::IResult;
use nom_locate::LocatedSpan;
macro_rules! assert_parse { macro_rules! assert_parse {
($parsemac: ident($i: expr), $out: expr) => { ($parsemac:ident($i:expr), $out:expr) => {
assert_parse!($i, $parsemac, $out) assert_parse!($i, $parsemac, $out)
}; };
($i: expr, $f: expr, $out: expr) => {{ ($i:expr, $f:expr, $out:expr) => {{
let input = LocatedSpan::new($i); let input = LocatedSpan::new($i);
match tokenize(input) { match tokenize(input) {
Err(e) => assert!(false, format!("Tokenizer Error: {:?}", e)), Err(e) => assert!(false, format!("Tokenizer Error: {:?}", e)),
@ -866,10 +866,10 @@ mod test {
} }
macro_rules! assert_error { macro_rules! assert_error {
($parsemac: ident($i: expr)) => { ($parsemac:ident($i:expr)) => {
assert_error!($i, $parsemac) assert_error!($i, $parsemac)
}; };
($i: expr, $f: expr) => {{ ($i:expr, $f:expr) => {{
let input = LocatedSpan::new($i); let input = LocatedSpan::new($i);
match tokenize(input) { match tokenize(input) {
Err(_) => assert!(true), Err(_) => assert!(true),
@ -1801,7 +1801,7 @@ mod test {
( (
make_tok!("foo", 1, 1), make_tok!("foo", 1, 1),
Expression::Simple(Value::Selector( Expression::Simple(Value::Selector(
make_selector!(make_expr!("bar", 1, 7) => [ make_tok!("baz", 1, 11) ] => 1, 7) make_selector!(make_expr!("bar", 1, 7) => [ make_tok!("baz", 1, 11) ] => 1, 7),
)) ))
) )
); );

View File

@ -13,12 +13,12 @@
// limitations under the License. // limitations under the License.
//! The tokenization stage of the ucg compiler. //! The tokenization stage of the ucg compiler.
use nom_locate::LocatedSpan;
use nom;
use nom::{alpha, digit, is_alphanumeric, multispace};
use nom::{InputIter, InputLength, Slice};
use ast::tree::*; use ast::tree::*;
use error; use error;
use nom;
use nom::{InputIter, InputLength, Slice};
use nom::{alpha, digit, is_alphanumeric, multispace};
use nom_locate::LocatedSpan;
use std; use std;
use std::result::Result; use std::result::Result;
@ -121,16 +121,15 @@ macro_rules! do_tag_tok {
// paramater even though we don't explicitely pass it below. I don't // paramater even though we don't explicitely pass it below. I don't
// particularly like this but I'm living with it for now. // particularly like this but I'm living with it for now.
($i:expr, $type:expr, $tag:expr) => { ($i:expr, $type:expr, $tag:expr) => {
do_parse!($i, do_parse!(
span: position!() >> $i,
frag: tag!($tag) >> span: position!() >> frag: tag!($tag) >> (Token {
(Token{ typ: $type,
typ: $type, pos: Position::from(span),
pos: Position::from(span), fragment: frag.fragment.to_string(),
fragment: frag.fragment.to_string(), })
}) )
) };
}
} }
named!(emptytok( Span ) -> Token, named!(emptytok( Span ) -> Token,
@ -416,131 +415,133 @@ pub fn token_clone(t: &Token) -> Result<Token, error::Error> {
/// nom macro that matches a Token by type and uses an optional conversion handler /// nom macro that matches a Token by type and uses an optional conversion handler
/// for the matched Token. /// for the matched Token.
macro_rules! match_type { macro_rules! match_type {
($i:expr, BOOLEAN => $h:expr) => { ($i:expr,BOOLEAN => $h:expr) => {
match_type!($i, TokenType::BOOLEAN, "Not a Boolean", $h) match_type!($i, TokenType::BOOLEAN, "Not a Boolean", $h)
}; };
($i:expr, BOOLEAN) => { ($i:expr,BOOLEAN) => {
match_type!($i, BOOLEAN => token_clone) match_type!($i, BOOLEAN => token_clone)
}; };
($i:expr, COMMENT => $h:expr) => { ($i:expr,COMMENT => $h:expr) => {
match_type!($i, TokenType::COMMENT, "Not a Comment", $h) match_type!($i, TokenType::COMMENT, "Not a Comment", $h)
}; };
($i:expr, COMMENT) => { ($i:expr,COMMENT) => {
match_type!($i, COMMENT => token_clone) match_type!($i, COMMENT => token_clone)
}; };
($i:expr, BAREWORD => $h:expr) => { ($i:expr,BAREWORD => $h:expr) => {
match_type!($i, TokenType::BAREWORD, "Not a Bareword", $h) match_type!($i, TokenType::BAREWORD, "Not a Bareword", $h)
}; };
($i:expr, BAREWORD) => { ($i:expr,BAREWORD) => {
match_type!($i, BAREWORD => token_clone) match_type!($i, BAREWORD => token_clone)
}; };
($i:expr, EMPTY => $h:expr) => { ($i:expr,EMPTY => $h:expr) => {
match_type!($i, TokenType::EMPTY, "Not NULL", $h) match_type!($i, TokenType::EMPTY, "Not NULL", $h)
}; };
($i:expr, EMPTY) => { ($i:expr,EMPTY) => {
match_type!($i, EMPTY => token_clone) match_type!($i, EMPTY => token_clone)
}; };
($i:expr, STR => $h:expr) => { ($i:expr,STR => $h:expr) => {
match_type!($i, TokenType::QUOTED, "Not a String", $h) match_type!($i, TokenType::QUOTED, "Not a String", $h)
}; };
($i:expr, STR) => { ($i:expr,STR) => {
match_type!($i, STR => token_clone) match_type!($i, STR => token_clone)
}; };
($i:expr, DIGIT => $h:expr) => { ($i:expr,DIGIT => $h:expr) => {
match_type!($i, TokenType::DIGIT, "Not a DIGIT", $h) match_type!($i, TokenType::DIGIT, "Not a DIGIT", $h)
}; };
($i:expr, DIGIT) => { ($i:expr,DIGIT) => {
match_type!($i, DIGIT => token_clone) match_type!($i, DIGIT => token_clone)
}; };
($i:expr, PUNCT => $h:expr) => { ($i:expr,PUNCT => $h:expr) => {
match_type!($i, TokenType::PUNCT, "Not PUNCTUATION", $h) match_type!($i, TokenType::PUNCT, "Not PUNCTUATION", $h)
}; };
($i:expr, PUNCT) => { ($i:expr,PUNCT) => {
match_type!($i, PUNCT => token_clone) match_type!($i, PUNCT => token_clone)
}; };
($i:expr, $t:expr, $msg:expr, $h:expr) => { ($i:expr, $t:expr, $msg:expr, $h:expr) => {{
{ let i_ = $i.clone();
let i_ = $i.clone(); use nom::Slice;
use nom::Slice; use std::convert::Into;
use std::convert::Into; if i_.input_len() == 0 {
if i_.input_len() == 0 { nom::IResult::Error(nom::ErrorKind::Custom(error::Error::new(
nom::IResult::Error( format!("End of Input! {}", $msg),
nom::ErrorKind::Custom(error::Error::new( error::ErrorType::IncompleteParsing,
format!("End of Input! {}", $msg), Position { line: 0, column: 0 },
error::ErrorType::IncompleteParsing, )))
Position{line: 0, column: 0}))) } else {
} else { let tok = &(i_[0]);
let tok = &(i_[0]); if tok.typ == $t {
if tok.typ == $t { match $h(tok) {
match $h(tok) { Result::Ok(v) => nom::IResult::Done($i.slice(1..), v),
Result::Ok(v) => nom::IResult::Done($i.slice(1..), v), Result::Err(e) => nom::IResult::Error(nom::ErrorKind::Custom(e.into())),
Result::Err(e) => nom::IResult::Error(
nom::ErrorKind::Custom(e.into())),
}
} else {
nom::IResult::Error(nom::ErrorKind::Custom(error::Error::new(
$msg.to_string(),
error::ErrorType::UnexpectedToken,
tok.pos.clone())))
} }
} else {
nom::IResult::Error(nom::ErrorKind::Custom(error::Error::new(
$msg.to_string(),
error::ErrorType::UnexpectedToken,
tok.pos.clone(),
)))
} }
} }
}; }};
} }
/// nom style macro that matches various Tokens by type and value and allows optional /// nom style macro that matches various Tokens by type and value and allows optional
/// conversion handlers for the matched Token. /// conversion handlers for the matched Token.
macro_rules! match_token { macro_rules! match_token {
($i:expr, PUNCT => $f:expr) => { ($i:expr,PUNCT => $f:expr) => {
match_token!($i, PUNCT => $f, token_clone) match_token!($i, PUNCT => $f, token_clone)
}; };
($i:expr, PUNCT => $f:expr, $h:expr) => { ($i:expr,PUNCT => $f:expr, $h:expr) => {
match_token!($i, TokenType::PUNCT, $f, format!("Not PUNCT ({})", $f), $h) match_token!($i, TokenType::PUNCT, $f, format!("Not PUNCT ({})", $f), $h)
}; };
($i:expr, BAREWORD => $f:expr) => { ($i:expr,BAREWORD => $f:expr) => {
match_token!($i, BAREWORD => $f, token_clone) match_token!($i, BAREWORD => $f, token_clone)
}; };
($i:expr, BAREWORD => $f:expr, $h:expr) => { ($i:expr,BAREWORD => $f:expr, $h:expr) => {
match_token!($i, TokenType::BAREWORD, $f, format!("Not a BAREWORD ({})", $f), $h) match_token!(
$i,
TokenType::BAREWORD,
$f,
format!("Not a BAREWORD ({})", $f),
$h
)
}; };
($i:expr, $t:expr, $f:expr, $msg:expr, $h:expr) => { ($i:expr, $t:expr, $f:expr, $msg:expr, $h:expr) => {{
{ let i_ = $i.clone();
let i_ = $i.clone(); use nom::Slice;
use nom::Slice; use std::convert::Into;
use std::convert::Into; let tok = &(i_[0]);
let tok = &(i_[0]); if tok.typ == $t && &tok.fragment == $f {
if tok.typ == $t && &tok.fragment == $f { match $h(tok) {
match $h(tok) { Result::Ok(v) => nom::IResult::Done($i.slice(1..), v),
Result::Ok(v) => nom::IResult::Done($i.slice(1..), v), Result::Err(e) => nom::IResult::Error(nom::ErrorKind::Custom(e.into())),
Result::Err(e) => nom::IResult::Error(
nom::ErrorKind::Custom(e.into())),
}
} else {
nom::IResult::Error(nom::ErrorKind::Custom(error::Error::new(
format!("{} Instead is ({})", $msg, tok.fragment),
error::ErrorType::UnexpectedToken,
tok.pos.clone())))
} }
} else {
nom::IResult::Error(nom::ErrorKind::Custom(error::Error::new(
format!("{} Instead is ({})", $msg, tok.fragment),
error::ErrorType::UnexpectedToken,
tok.pos.clone(),
)))
} }
}; }};
} }
/// nom style macro that matches punctuation Tokens. /// nom style macro that matches punctuation Tokens.
@ -599,7 +600,7 @@ macro_rules! impl_token_iter_slice {
} }
} }
} }
} };
} }
impl_token_iter_slice!(std::ops::Range<usize>); impl_token_iter_slice!(std::ops::Range<usize>);
@ -704,16 +705,16 @@ mod tokenizer_test {
macro_rules! assert_token { macro_rules! assert_token {
($input:expr, $typ:expr, $msg:expr) => { ($input:expr, $typ:expr, $msg:expr) => {
let result = token(LocatedSpan::new($input)); let result = token(LocatedSpan::new($input));
assert!( assert!(
result.is_done(), result.is_done(),
format!("result {:?} is not a {}", result, $msg) format!("result {:?} is not a {}", result, $msg)
); );
if let nom::IResult::Done(_, tok) = result { if let nom::IResult::Done(_, tok) = result {
assert_eq!(tok.fragment, $input); assert_eq!(tok.fragment, $input);
assert_eq!(tok.typ, $typ); assert_eq!(tok.typ, $typ);
} }
} };
} }
#[test] #[test]