From 1edb58a802f183f79dc2c4bc15921394ef8abb31 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 14:08:06 +0300 Subject: reformat --- .travis.yml | 2 +- src/lexer/classes.rs | 9 ++++++-- src/lexer/mod.rs | 16 +++++++------- src/lexer/numbers.rs | 2 +- src/lib.rs | 47 ++++++++++++++++++++++++----------------- src/parser/event.rs | 4 ++-- src/parser/grammar/items/mod.rs | 9 ++++---- src/parser/grammar/mod.rs | 11 ++++------ src/parser/input.rs | 6 +----- src/parser/mod.rs | 10 +++------ src/parser/parser/imp.rs | 2 +- src/syntax_kinds/mod.rs | 3 +-- src/yellow/builder.rs | 15 ++++++------- src/yellow/green.rs | 37 +++++++++++++++++--------------- src/yellow/mod.rs | 6 +++--- src/yellow/red.rs | 34 +++++++++++++---------------- src/yellow/syntax.rs | 23 ++++++++------------ tests/parser.rs | 4 ++-- tests/testutils/src/lib.rs | 2 +- tools/src/bin/main.rs | 21 +++++++----------- 20 files changed, 126 insertions(+), 137 deletions(-) diff --git a/.travis.yml b/.travis.yml index f4ee048f4..9949312c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ matrix: before_script: - rustup component add rustfmt-preview script: - - cargo fmt --all -- --write-mode=diff + - cargo fmt --all -- --check - cargo test - cargo gen-kinds --verify - cargo gen-tests --verify diff --git a/src/lexer/classes.rs b/src/lexer/classes.rs index 7fed008af..4235d2648 100644 --- a/src/lexer/classes.rs +++ b/src/lexer/classes.rs @@ -1,12 +1,17 @@ use unicode_xid::UnicodeXID; pub fn is_ident_start(c: char) -> bool { - (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' + (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || c == '_' || (c > '\x7f' && UnicodeXID::is_xid_start(c)) } pub fn is_ident_continue(c: char) -> bool { - (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' + (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9') + || c == '_' || (c > '\x7f' && UnicodeXID::is_xid_continue(c)) } diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 69cab5b57..f647838ea 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -1,22 +1,22 @@ -mod ptr; +mod classes; mod comments; -mod strings; mod numbers; -mod classes; +mod ptr; +mod strings; use { - TextUnit, SyntaxKind::{self, *}, + TextUnit, }; use self::{ - ptr::Ptr, classes::*, + comments::{scan_comment, scan_shebang}, numbers::scan_number, + ptr::Ptr, strings::{ - is_string_literal_start, scan_byte_char_or_string, scan_char, - scan_raw_string, scan_string}, - comments::{scan_comment, scan_shebang}, + is_string_literal_start, scan_byte_char_or_string, scan_char, scan_raw_string, scan_string, + }, }; /// A token of Rust source. diff --git a/src/lexer/numbers.rs b/src/lexer/numbers.rs index 38eac9212..5c4641a2d 100644 --- a/src/lexer/numbers.rs +++ b/src/lexer/numbers.rs @@ -1,5 +1,5 @@ -use lexer::ptr::Ptr; use lexer::classes::*; +use lexer::ptr::Ptr; use SyntaxKind::{self, *}; diff --git a/src/lib.rs b/src/lib.rs index 34c71fd2c..91d060169 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,44 +11,42 @@ //! [rfc#2256]: //! [RFC.md]: -#![forbid(missing_debug_implementations, unconditional_recursion, future_incompatible)] +#![forbid( + missing_debug_implementations, + unconditional_recursion, + future_incompatible +)] #![deny(bad_style, missing_docs)] #![allow(missing_docs)] //#![warn(unreachable_pub)] // rust-lang/rust#47816 -extern crate unicode_xid; extern crate text_unit; +extern crate unicode_xid; mod lexer; mod parser; -mod yellow; mod syntax_kinds; +mod yellow; pub use { - text_unit::{TextRange, TextUnit}, + lexer::{tokenize, Token}, syntax_kinds::SyntaxKind, + text_unit::{TextRange, TextUnit}, yellow::{SyntaxNode, SyntaxNodeRef}, - lexer::{tokenize, Token}, }; -pub(crate) use { - yellow::SyntaxError -}; +pub(crate) use yellow::SyntaxError; pub fn parse(text: String) -> SyntaxNode { let tokens = tokenize(&text); parser::parse::(text, &tokens) } - /// Utilities for simple uses of the parser. pub mod utils { - use std::{ - fmt::Write, - collections::BTreeSet - }; + use std::{collections::BTreeSet, fmt::Write}; - use {SyntaxNode, SyntaxNodeRef, SyntaxError}; + use {SyntaxError, SyntaxNode, SyntaxNodeRef}; /// Parse a file and create a string representation of the resulting parse tree. pub fn dump_tree_green(syntax: &SyntaxNode) -> String { @@ -58,11 +56,19 @@ pub mod utils { go(syntax, &mut result, 0, &mut errors); return result; - fn go(node: SyntaxNodeRef, buff: &mut String, level: usize, errors: &mut BTreeSet) { + fn go( + node: SyntaxNodeRef, + buff: &mut String, + level: usize, + errors: &mut BTreeSet, + ) { buff.push_str(&String::from(" ").repeat(level)); write!(buff, "{:?}\n", node).unwrap(); - let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().start()) - .cloned().collect(); + let my_errors: Vec<_> = errors + .iter() + .filter(|e| e.offset == node.range().start()) + .cloned() + .collect(); for err in my_errors { errors.remove(&err); buff.push_str(&String::from(" ").repeat(level)); @@ -73,8 +79,11 @@ pub mod utils { go(child, buff, level + 1, errors) } - let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().end()) - .cloned().collect(); + let my_errors: Vec<_> = errors + .iter() + .filter(|e| e.offset == node.range().end()) + .cloned() + .collect(); for err in my_errors { errors.remove(&err); buff.push_str(&String::from(" ").repeat(level)); diff --git a/src/parser/event.rs b/src/parser/event.rs index a8d503b3d..0086d32ea 100644 --- a/src/parser/event.rs +++ b/src/parser/event.rs @@ -8,9 +8,9 @@ //! `start node`, `finish node`, and `FileBuilder` converts //! this stream to a real tree. use { - TextUnit, - SyntaxKind::{self, TOMBSTONE}, lexer::Token, + SyntaxKind::{self, TOMBSTONE}, + TextUnit, }; pub(crate) trait Sink { diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs index 1fe646652..5d8d57a80 100644 --- a/src/parser/grammar/items/mod.rs +++ b/src/parser/grammar/items/mod.rs @@ -1,9 +1,9 @@ use super::*; -mod structs; -mod use_item; mod consts; +mod structs; mod traits; +mod use_item; pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { attributes::inner_attributes(p); @@ -12,9 +12,8 @@ pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { } } -pub(super) const ITEM_FIRST: TokenSet = token_set![ - EXTERN_KW, MOD_KW, USE_KW, STRUCT_KW, ENUM_KW, FN_KW, PUB_KW, POUND -]; +pub(super) const ITEM_FIRST: TokenSet = + token_set![EXTERN_KW, MOD_KW, USE_KW, STRUCT_KW, ENUM_KW, FN_KW, PUB_KW, POUND]; fn item(p: &mut Parser) { let item = p.start(); diff --git a/src/parser/grammar/mod.rs b/src/parser/grammar/mod.rs index 085e62d56..e24f1055e 100644 --- a/src/parser/grammar/mod.rs +++ b/src/parser/grammar/mod.rs @@ -21,20 +21,17 @@ //! After adding a new inline-test, run `cargo collect-tests` to extract //! it as a standalone text-fixture into `tests/data/parser/inline`, and //! run `cargo test` once to create the "gold" value. -mod items; mod attributes; mod expressions; -mod types; -mod patterns; +mod items; mod paths; +mod patterns; mod type_params; +mod types; use { + parser::{parser::Parser, token_set::TokenSet}, SyntaxKind::{self, *}, - parser::{ - parser::Parser, - token_set::TokenSet - } }; pub(crate) fn file(p: &mut Parser) { diff --git a/src/parser/input.rs b/src/parser/input.rs index 052981fbc..db76364b2 100644 --- a/src/parser/input.rs +++ b/src/parser/input.rs @@ -1,8 +1,4 @@ -use { - SyntaxKind, TextRange, TextUnit, - SyntaxKind::EOF, - lexer::Token, -}; +use {lexer::Token, SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit}; use std::ops::{Add, AddAssign}; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index e72ab05af..8631baa2e 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,18 +1,14 @@ #[macro_use] mod token_set; -mod parser; -mod input; mod event; mod grammar; +mod input; +mod parser; -use { - lexer::Token, - parser::event::{process} -}; +use {lexer::Token, parser::event::process}; pub(crate) use self::event::Sink; - /// Parse a sequence of tokens into the representative node tree pub(crate) fn parse(text: String, tokens: &[Token]) -> S::Tree { let events = { diff --git a/src/parser/parser/imp.rs b/src/parser/parser/imp.rs index 38237ac06..c653e3524 100644 --- a/src/parser/parser/imp.rs +++ b/src/parser/parser/imp.rs @@ -1,5 +1,5 @@ -use parser::input::{InputPosition, ParserInput}; use parser::event::Event; +use parser::input::{InputPosition, ParserInput}; use SyntaxKind::{self, EOF, TOMBSTONE}; diff --git a/src/syntax_kinds/mod.rs b/src/syntax_kinds/mod.rs index a8e9bfe29..4c21c02ae 100644 --- a/src/syntax_kinds/mod.rs +++ b/src/syntax_kinds/mod.rs @@ -1,7 +1,7 @@ mod generated; use std::fmt; -use ::{SyntaxKind::*}; +use SyntaxKind::*; pub use self::generated::SyntaxKind; @@ -16,7 +16,6 @@ pub(crate) struct SyntaxInfo { pub name: &'static str, } - impl SyntaxKind { pub(crate) fn is_trivia(self: SyntaxKind) -> bool { match self { diff --git a/src/yellow/builder.rs b/src/yellow/builder.rs index 65cc97ff9..0f7ca45d7 100644 --- a/src/yellow/builder.rs +++ b/src/yellow/builder.rs @@ -1,7 +1,7 @@ use { + parser::Sink, + yellow::{GreenNode, GreenNodeBuilder, SyntaxError, SyntaxNode, SyntaxRoot}, SyntaxKind, TextRange, TextUnit, - yellow::{SyntaxNode, SyntaxRoot, GreenNode, GreenNodeBuilder, SyntaxError}, - parser::Sink }; pub(crate) struct GreenBuilder { @@ -12,9 +12,7 @@ pub(crate) struct GreenBuilder { errors: Vec, } -impl GreenBuilder { - -} +impl GreenBuilder {} impl Sink for GreenBuilder { type Tree = SyntaxNode; @@ -53,7 +51,10 @@ impl Sink for GreenBuilder { } fn error(&mut self, message: String) { - self.errors.push(SyntaxError { message, offset: self.pos }) + self.errors.push(SyntaxError { + message, + offset: self.pos, + }) } fn finish(self) -> SyntaxNode { @@ -61,5 +62,3 @@ impl Sink for GreenBuilder { SyntaxNode::new_owned(root) } } - - diff --git a/src/yellow/green.rs b/src/yellow/green.rs index cb9dff128..507e4d57e 100644 --- a/src/yellow/green.rs +++ b/src/yellow/green.rs @@ -1,5 +1,8 @@ use std::sync::Arc; -use {SyntaxKind::{self, *}, TextUnit}; +use { + SyntaxKind::{self, *}, + TextUnit, +}; #[derive(Clone, Debug)] pub(crate) enum GreenNode { @@ -36,9 +39,7 @@ impl GreenNode { fn go(node: &GreenNode, buff: &mut String) { match node { GreenNode::Leaf(l) => buff.push_str(&l.text()), - GreenNode::Branch(b) => { - b.children().iter().for_each(|child| go(child, buff)) - } + GreenNode::Branch(b) => b.children().iter().for_each(|child| go(child, buff)), } } } @@ -71,7 +72,6 @@ impl GreenNodeBuilder { } } - #[test] fn assert_send_sync() { fn f() {} @@ -80,14 +80,8 @@ fn assert_send_sync() { #[derive(Clone, Debug)] pub(crate) enum GreenLeaf { - Whitespace { - newlines: u8, - spaces: u8, - }, - Token { - kind: SyntaxKind, - text: Arc, - }, + Whitespace { newlines: u8, spaces: u8 }, + Token { kind: SyntaxKind, text: Arc }, } impl GreenLeaf { @@ -96,10 +90,16 @@ impl GreenLeaf { let newlines = text.bytes().take_while(|&b| b == b'\n').count(); let spaces = text[newlines..].bytes().take_while(|&b| b == b' ').count(); if newlines + spaces == text.len() && newlines <= N_NEWLINES && spaces <= N_SPACES { - return GreenLeaf::Whitespace { newlines: newlines as u8, spaces: spaces as u8 }; + return GreenLeaf::Whitespace { + newlines: newlines as u8, + spaces: spaces as u8, + }; } } - GreenLeaf::Token { kind, text: text.to_owned().into_boxed_str().into() } + GreenLeaf::Token { + kind, + text: text.to_owned().into_boxed_str().into(), + } } pub(crate) fn kind(&self) -> SyntaxKind { @@ -141,7 +141,11 @@ pub(crate) struct GreenBranch { impl GreenBranch { fn new(kind: SyntaxKind, children: Vec) -> GreenBranch { let text_len = children.iter().map(|x| x.text_len()).sum::(); - GreenBranch { text_len, kind, children } + GreenBranch { + text_len, + kind, + children, + } } pub fn kind(&self) -> SyntaxKind { @@ -156,4 +160,3 @@ impl GreenBranch { self.children.as_slice() } } - diff --git a/src/yellow/mod.rs b/src/yellow/mod.rs index 89eefc98b..cada65d2f 100644 --- a/src/yellow/mod.rs +++ b/src/yellow/mod.rs @@ -1,12 +1,12 @@ +mod builder; mod green; mod red; mod syntax; -mod builder; +pub use self::syntax::{SyntaxNode, SyntaxNodeRef}; pub(crate) use self::{ + builder::GreenBuilder, green::{GreenNode, GreenNodeBuilder}, red::RedNode, syntax::{SyntaxError, SyntaxRoot}, - builder::GreenBuilder, }; -pub use self::syntax::{SyntaxNode, SyntaxNodeRef}; diff --git a/src/yellow/red.rs b/src/yellow/red.rs index 3f0ddd04c..8907100e4 100644 --- a/src/yellow/red.rs +++ b/src/yellow/red.rs @@ -1,11 +1,5 @@ -use std::{ - ptr, - sync::RwLock, -}; -use { - TextUnit, - yellow::GreenNode, -}; +use std::{ptr, sync::RwLock}; +use {yellow::GreenNode, TextUnit}; #[derive(Debug)] pub(crate) struct RedNode { @@ -22,9 +16,7 @@ struct ParentData { } impl RedNode { - pub fn new_root( - green: GreenNode, - ) -> RedNode { + pub fn new_root(green: GreenNode) -> RedNode { RedNode::new(green, None) } @@ -42,13 +34,14 @@ impl RedNode { RedNode::new(green, Some(parent_data)) } - fn new( - green: GreenNode, - parent: Option, - ) -> RedNode { + fn new(green: GreenNode, parent: Option) -> RedNode { let n_children = green.children().len(); let children = (0..n_children).map(|_| None).collect(); - RedNode { green, parent, children: RwLock::new(children) } + RedNode { + green, + parent, + children: RwLock::new(children), + } } pub(crate) fn green(&self) -> &GreenNode { @@ -75,12 +68,15 @@ impl RedNode { if children[idx].is_none() { let green_children = self.green.children(); let start_offset = self.start_offset() - + green_children[..idx].iter().map(|x| x.text_len()).sum::(); - let child = RedNode::new_child(green_children[idx].clone(), self.into(), start_offset, idx); + + green_children[..idx] + .iter() + .map(|x| x.text_len()) + .sum::(); + let child = + RedNode::new_child(green_children[idx].clone(), self.into(), start_offset, idx); children[idx] = Some(child) } children[idx].as_ref().unwrap().into() - } pub(crate) fn parent(&self) -> Option> { diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs index 64af08236..19a9b8ac2 100644 --- a/src/yellow/syntax.rs +++ b/src/yellow/syntax.rs @@ -1,17 +1,12 @@ -use std::{ - fmt, - sync::Arc, - ptr, - ops::Deref, -}; +use std::{fmt, ops::Deref, ptr, sync::Arc}; use { - TextRange, TextUnit, + yellow::{GreenNode, RedNode}, SyntaxKind::{self, *}, - yellow::{RedNode, GreenNode}, + TextRange, TextUnit, }; -pub trait TreeRoot: Deref + Clone {} +pub trait TreeRoot: Deref + Clone {} impl TreeRoot for Arc {} impl<'a> TreeRoot for &'a SyntaxRoot {} @@ -50,7 +45,10 @@ impl SyntaxNode> { pub(crate) fn new_owned(root: SyntaxRoot) -> Self { let root = Arc::new(root); let red_weak = ptr::NonNull::from(&root.red); - SyntaxNode { root, red: red_weak } + SyntaxNode { + root, + red: red_weak, + } } } @@ -68,10 +66,7 @@ impl SyntaxNode { pub fn range(&self) -> TextRange { let red = self.red(); - TextRange::offset_len( - red.start_offset(), - red.green().text_len(), - ) + TextRange::offset_len(red.start_offset(), red.green().text_len()) } pub fn text(&self) -> String { diff --git a/tests/parser.rs b/tests/parser.rs index 3b6670cb0..eb955278e 100644 --- a/tests/parser.rs +++ b/tests/parser.rs @@ -1,8 +1,8 @@ extern crate libsyntax2; extern crate testutils; -use libsyntax2::{parse}; -use libsyntax2::utils::{dump_tree_green}; +use libsyntax2::parse; +use libsyntax2::utils::dump_tree_green; use testutils::dir_tests; #[test] diff --git a/tests/testutils/src/lib.rs b/tests/testutils/src/lib.rs index ae1dea810..43b806541 100644 --- a/tests/testutils/src/lib.rs +++ b/tests/testutils/src/lib.rs @@ -1,8 +1,8 @@ extern crate difference; extern crate file; -use std::path::{Path, PathBuf}; use std::fs::read_dir; +use std::path::{Path, PathBuf}; use difference::Changeset; diff --git a/tools/src/bin/main.rs b/tools/src/bin/main.rs index 6a9793fff..125930127 100644 --- a/tools/src/bin/main.rs +++ b/tools/src/bin/main.rs @@ -1,18 +1,14 @@ extern crate clap; #[macro_use] extern crate failure; -extern crate tera; +extern crate itertools; extern crate ron; +extern crate tera; extern crate walkdir; -extern crate itertools; -use std::{ - fs, - path::{Path}, - collections::HashSet, -}; use clap::{App, Arg, SubCommand}; use itertools::Itertools; +use std::{collections::HashSet, fs, path::Path}; type Result = ::std::result::Result; @@ -29,7 +25,7 @@ fn main() -> Result<()> { Arg::with_name("verify") .long("--verify") .help("Verify that generated code is up-to-date") - .global(true) + .global(true), ) .subcommand(SubCommand::with_name("gen-kinds")) .subcommand(SubCommand::with_name("gen-tests")) @@ -66,9 +62,8 @@ fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { fn get_kinds() -> Result { let grammar = grammar()?; let template = fs::read_to_string(SYNTAX_KINDS_TEMPLATE)?; - let ret = tera::Tera::one_off(&template, &grammar, false).map_err(|e| { - format_err!("template error: {}", e) - })?; + let ret = tera::Tera::one_off(&template, &grammar, false) + .map_err(|e| format_err!("template error: {}", e))?; Ok(ret) } @@ -142,7 +137,8 @@ fn tests_from_dir(dir: &Path) -> Result> { fn collect_tests(s: &str) -> Vec { let mut res = vec![]; let prefix = "// "; - let comment_blocks = s.lines() + let comment_blocks = s + .lines() .map(str::trim_left) .group_by(|line| line.starts_with(prefix)); @@ -181,4 +177,3 @@ fn existing_tests(dir: &Path) -> Result> { } Ok(res) } - -- cgit v1.2.3