From 00c1e1751a487cbb48edb0d4048fd47826de166b Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 14 May 2018 21:34:38 -0500 Subject: [PATCH] Formatting: cargo fmt. --- src/ast/tree.rs | 34 ++++---- src/build.rs | 18 ++--- src/convert/env.rs | 4 +- src/convert/flags.rs | 4 +- src/convert/json.rs | 4 +- src/convert/mod.rs | 2 +- src/convert/traits.rs | 4 +- src/lib.rs | 6 +- src/main.rs | 4 +- src/parse.rs | 20 ++--- src/tokenizer.rs | 175 +++++++++++++++++++++--------------------- 11 files changed, 138 insertions(+), 137 deletions(-) diff --git a/src/ast/tree.rs b/src/ast/tree.rs index dc1b787..4cecb05 100644 --- a/src/ast/tree.rs +++ b/src/ast/tree.rs @@ -12,15 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. use std; -use std::collections::HashSet; use std::borrow::Borrow; -use std::convert::Into; -use std::cmp::Ordering; -use std::cmp::PartialOrd; use std::cmp::Eq; +use std::cmp::Ordering; 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::Hasher; macro_rules! enum_type_equality { ( $slf:ident, $r:expr, $( $l:pat ),* ) => { @@ -106,10 +106,10 @@ impl Borrow for Token { /// Helper macro for making a Positioned Value. macro_rules! value_node { - ($v: expr, $p: expr) => { + ($v:expr, $p:expr) => { Positioned::new_with_pos($v, $p) }; - ($v: expr, $l: expr, $c: expr) => { + ($v:expr, $l:expr, $c:expr) => { Positioned::new($v, $l, $c) }; } @@ -117,31 +117,31 @@ macro_rules! value_node { /// Helper macro for making a Token. #[allow(unused_macros)] macro_rules! make_tok { - (EOF => $l: expr, $c: expr) => { + (EOF => $l:expr, $c:expr) => { Token::new("", TokenType::END, $l, $c) }; - (WS => $l: expr, $c: expr) => { + (WS => $l:expr, $c:expr) => { 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) }; - (QUOT => $e: expr, $l: expr, $c: expr) => { + (QUOT => $e:expr, $l:expr, $c:expr) => { 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) }; - (DIGIT => $e: expr, $l: expr, $c: expr) => { + (DIGIT => $e:expr, $l:expr, $c:expr) => { 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) }; } @@ -149,15 +149,15 @@ macro_rules! make_tok { /// Helper macro for making expressions. #[allow(unused_macros)] macro_rules! make_expr { - ($e: expr) => { + ($e:expr) => { 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))) }; - ($e: expr => int, $l: expr, $c: expr) => { + ($e:expr => int, $l:expr, $c:expr) => { Expression::Simple(Value::Int(Positioned::new($e, $l, $c))) }; } diff --git a/src/build.rs b/src/build.rs index 6c476ad..857a194 100644 --- a/src/build.rs +++ b/src/build.rs @@ -13,23 +13,23 @@ // limitations under the License. //! 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::{HashMap, HashSet, VecDeque}; +use std::convert::From; +use std::env; +use std::error::Error; use std::fmt; use std::fmt::{Display, Formatter}; +use std::fs::File; +use std::io::Read; use std::ops::Deref; use std::rc::Rc; -use std::convert::From; -use tokenizer::Span; use ast::tree::*; +use error; use format; use parse::parse; -use error; +use tokenizer::Span; impl MacroDef { /// Expands a ucg Macro using the given arguments into a new Tuple. @@ -295,7 +295,7 @@ pub struct Builder { } 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() { $case => { return Ok(Rc::new($result)); diff --git a/src/convert/env.rs b/src/convert/env.rs index 627629a..2733e8c 100644 --- a/src/convert/env.rs +++ b/src/convert/env.rs @@ -13,9 +13,9 @@ // limitations under the License. //! An environment variable converter. -use std::rc::Rc; -use std::io::Write; use std::io::Result; +use std::io::Write; +use std::rc::Rc; use ast::tree::*; use build::Val; diff --git a/src/convert/flags.rs b/src/convert/flags.rs index 07b3965..697c944 100644 --- a/src/convert/flags.rs +++ b/src/convert/flags.rs @@ -13,9 +13,9 @@ // limitations under the License. //! 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::Write; +use std::rc::Rc; use build::Val; use convert::traits::Converter; diff --git a/src/convert/json.rs b/src/convert/json.rs index 22a73f8..16be2f9 100644 --- a/src/convert/json.rs +++ b/src/convert/json.rs @@ -8,9 +8,9 @@ // See the License for the specific language governing permissions and // limitations under the License. //! 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::Write; +use std::rc::Rc; use serde_json; diff --git a/src/convert/mod.rs b/src/convert/mod.rs index 3c32071..e55003f 100644 --- a/src/convert/mod.rs +++ b/src/convert/mod.rs @@ -13,9 +13,9 @@ // limitations under the License. //! The conversion stage of the ucg compiler. +pub mod env; pub mod flags; pub mod json; -pub mod env; pub mod traits; use std::io; diff --git a/src/convert/traits.rs b/src/convert/traits.rs index 38c856e..e0479cb 100644 --- a/src/convert/traits.rs +++ b/src/convert/traits.rs @@ -13,9 +13,9 @@ // limitations under the License. //! 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::Write; +use std::rc::Rc; use build::Val; diff --git a/src/lib.rs b/src/lib.rs index 1ee1e8f..4752247 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -409,17 +409,17 @@ extern crate serde_json; pub mod ast; #[macro_use] pub mod tokenizer; -pub mod parse; pub mod build; pub mod convert; pub mod error; +pub mod parse; mod format; -pub use ast::tree::Value; pub use ast::tree::Expression; pub use ast::tree::Statement; +pub use ast::tree::Value; -pub use parse::parse; pub use build::Builder; pub use build::Val; +pub use parse::parse; diff --git a/src/main.rs b/src/main.rs index d6bd6c5..0928654 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,12 +16,12 @@ extern crate clap; extern crate ucglib; use std::fs::File; -use std::rc::Rc; use std::io; use std::process; +use std::rc::Rc; -use ucglib::build::Val; use ucglib::build; +use ucglib::build::Val; use ucglib::convert::ConverterRunner; // TODO(jwall): List the target output types automatically. diff --git a/src/parse.rs b/src/parse.rs index 5722173..b055ca5 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -13,17 +13,17 @@ // limitations under the License. //! The Parsing stage of the ucg compiler. -use std::str::FromStr; use std::borrow::Borrow; +use std::str::FromStr; -use nom_locate::LocatedSpan; use nom; -use nom::InputLength; use nom::IResult; +use nom::InputLength; +use nom_locate::LocatedSpan; use ast::tree::*; -use tokenizer::*; use error; +use tokenizer::*; type NomResult<'a, O> = nom::IResult, O, error::Error>; @@ -844,14 +844,14 @@ mod test { use super::*; use tokenizer::{tokenize, TokenIter}; - use nom_locate::LocatedSpan; use nom::IResult; + use nom_locate::LocatedSpan; macro_rules! assert_parse { - ($parsemac: ident($i: expr), $out: expr) => { + ($parsemac:ident($i:expr), $out:expr) => { assert_parse!($i, $parsemac, $out) }; - ($i: expr, $f: expr, $out: expr) => {{ + ($i:expr, $f:expr, $out:expr) => {{ let input = LocatedSpan::new($i); match tokenize(input) { Err(e) => assert!(false, format!("Tokenizer Error: {:?}", e)), @@ -866,10 +866,10 @@ mod test { } macro_rules! assert_error { - ($parsemac: ident($i: expr)) => { + ($parsemac:ident($i:expr)) => { assert_error!($i, $parsemac) }; - ($i: expr, $f: expr) => {{ + ($i:expr, $f:expr) => {{ let input = LocatedSpan::new($i); match tokenize(input) { Err(_) => assert!(true), @@ -1801,7 +1801,7 @@ mod test { ( make_tok!("foo", 1, 1), 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), )) ) ); diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 0d5c595..5b33baf 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -13,12 +13,12 @@ // limitations under the License. //! 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 error; +use nom; +use nom::{InputIter, InputLength, Slice}; +use nom::{alpha, digit, is_alphanumeric, multispace}; +use nom_locate::LocatedSpan; use std; 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 // particularly like this but I'm living with it for now. ($i:expr, $type:expr, $tag:expr) => { - do_parse!($i, - span: position!() >> - frag: tag!($tag) >> - (Token{ - typ: $type, - pos: Position::from(span), - fragment: frag.fragment.to_string(), - }) - ) - } + do_parse!( + $i, + span: position!() >> frag: tag!($tag) >> (Token { + typ: $type, + pos: Position::from(span), + fragment: frag.fragment.to_string(), + }) + ) + }; } named!(emptytok( Span ) -> Token, @@ -416,131 +415,133 @@ pub fn token_clone(t: &Token) -> Result { /// nom macro that matches a Token by type and uses an optional conversion handler /// for the matched Token. macro_rules! match_type { - ($i:expr, BOOLEAN => $h:expr) => { + ($i:expr,BOOLEAN => $h:expr) => { match_type!($i, TokenType::BOOLEAN, "Not a Boolean", $h) }; - ($i:expr, BOOLEAN) => { + ($i:expr,BOOLEAN) => { 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) }; - ($i:expr, COMMENT) => { + ($i:expr,COMMENT) => { 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) }; - ($i:expr, BAREWORD) => { + ($i:expr,BAREWORD) => { match_type!($i, BAREWORD => token_clone) }; - ($i:expr, EMPTY => $h:expr) => { + ($i:expr,EMPTY => $h:expr) => { match_type!($i, TokenType::EMPTY, "Not NULL", $h) }; - ($i:expr, EMPTY) => { + ($i:expr,EMPTY) => { 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) }; - ($i:expr, STR) => { + ($i:expr,STR) => { 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) }; - ($i:expr, DIGIT) => { + ($i:expr,DIGIT) => { match_type!($i, DIGIT => token_clone) }; - ($i:expr, PUNCT => $h:expr) => { + ($i:expr,PUNCT => $h:expr) => { match_type!($i, TokenType::PUNCT, "Not PUNCTUATION", $h) }; - ($i:expr, PUNCT) => { + ($i:expr,PUNCT) => { match_type!($i, PUNCT => token_clone) }; - ($i:expr, $t:expr, $msg:expr, $h:expr) => { - { - let i_ = $i.clone(); - use nom::Slice; - use std::convert::Into; - if i_.input_len() == 0 { - nom::IResult::Error( - nom::ErrorKind::Custom(error::Error::new( - format!("End of Input! {}", $msg), - error::ErrorType::IncompleteParsing, - Position{line: 0, column: 0}))) - } else { - let tok = &(i_[0]); - if tok.typ == $t { - match $h(tok) { - Result::Ok(v) => nom::IResult::Done($i.slice(1..), v), - 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()))) + ($i:expr, $t:expr, $msg:expr, $h:expr) => {{ + let i_ = $i.clone(); + use nom::Slice; + use std::convert::Into; + if i_.input_len() == 0 { + nom::IResult::Error(nom::ErrorKind::Custom(error::Error::new( + format!("End of Input! {}", $msg), + error::ErrorType::IncompleteParsing, + Position { line: 0, column: 0 }, + ))) + } else { + let tok = &(i_[0]); + if tok.typ == $t { + match $h(tok) { + Result::Ok(v) => nom::IResult::Done($i.slice(1..), v), + 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(), + ))) } } - }; + }}; } /// nom style macro that matches various Tokens by type and value and allows optional /// conversion handlers for the matched Token. macro_rules! match_token { - ($i:expr, PUNCT => $f:expr) => { + ($i:expr,PUNCT => $f:expr) => { 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) }; - ($i:expr, BAREWORD => $f:expr) => { + ($i:expr,BAREWORD => $f:expr) => { match_token!($i, BAREWORD => $f, token_clone) }; - ($i:expr, BAREWORD => $f:expr, $h:expr) => { - match_token!($i, TokenType::BAREWORD, $f, format!("Not a BAREWORD ({})", $f), $h) + ($i:expr,BAREWORD => $f:expr, $h:expr) => { + match_token!( + $i, + TokenType::BAREWORD, + $f, + format!("Not a BAREWORD ({})", $f), + $h + ) }; - ($i:expr, $t:expr, $f:expr, $msg:expr, $h:expr) => { - { - let i_ = $i.clone(); - use nom::Slice; - use std::convert::Into; - let tok = &(i_[0]); - if tok.typ == $t && &tok.fragment == $f { - match $h(tok) { - Result::Ok(v) => nom::IResult::Done($i.slice(1..), v), - 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()))) + ($i:expr, $t:expr, $f:expr, $msg:expr, $h:expr) => {{ + let i_ = $i.clone(); + use nom::Slice; + use std::convert::Into; + let tok = &(i_[0]); + if tok.typ == $t && &tok.fragment == $f { + match $h(tok) { + Result::Ok(v) => nom::IResult::Done($i.slice(1..), v), + 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(), + ))) } - }; + }}; } /// nom style macro that matches punctuation Tokens. @@ -599,7 +600,7 @@ macro_rules! impl_token_iter_slice { } } } - } + }; } impl_token_iter_slice!(std::ops::Range); @@ -704,16 +705,16 @@ mod tokenizer_test { macro_rules! assert_token { ($input:expr, $typ:expr, $msg:expr) => { - let result = token(LocatedSpan::new($input)); - assert!( - result.is_done(), - format!("result {:?} is not a {}", result, $msg) - ); - if let nom::IResult::Done(_, tok) = result { - assert_eq!(tok.fragment, $input); - assert_eq!(tok.typ, $typ); - } - } + let result = token(LocatedSpan::new($input)); + assert!( + result.is_done(), + format!("result {:?} is not a {}", result, $msg) + ); + if let nom::IResult::Done(_, tok) = result { + assert_eq!(tok.fragment, $input); + assert_eq!(tok.typ, $typ); + } + }; } #[test]