From 9ce7e8110254e8db476c96bce2eecb2d16983159 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 31 Jul 2018 15:40:40 +0300 Subject: [PATCH] cleanups --- cli/src/main.rs | 19 ++++++++++++----- src/algo/walk.rs | 7 +++--- src/ast.rs | 8 ++++--- src/lib.rs | 13 ++++++------ src/parser/grammar/expressions.rs | 2 +- src/parser/grammar/items/mod.rs | 1 - src/parser/grammar/items/traits.rs | 1 - src/parser/grammar/mod.rs | 7 ++++-- src/parser/grammar/paths.rs | 8 +++++-- src/parser/grammar/patterns.rs | 7 ++---- src/parser/grammar/type_args.rs | 2 +- src/utils.rs | 6 +++--- src/yellow/green.rs | 12 ++++++++--- src/yellow/mod.rs | 2 +- src/yellow/red.rs | 5 +++-- src/yellow/syntax.rs | 12 +++++------ tests/testutils/src/lib.rs | 2 +- tools/src/lib.rs | 11 +++++----- tools/src/main.rs | 34 +++++++++++++++++++++--------- 19 files changed, 94 insertions(+), 65 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index f878781378..546cbb66b8 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -4,8 +4,8 @@ extern crate failure; extern crate libsyntax2; extern crate tools; -use std::{fs, path::Path, io::Read}; use clap::{App, Arg, SubCommand}; +use std::{fs, io::Read, path::Path}; use tools::collect_tests; type Result = ::std::result::Result; @@ -15,8 +15,18 @@ fn main() -> Result<()> { .setting(clap::AppSettings::SubcommandRequiredElseHelp) .subcommand( SubCommand::with_name("render-test") - .arg(Arg::with_name("line").long("--line").required(true).takes_value(true)) - .arg(Arg::with_name("file").long("--file").required(true).takes_value(true)) + .arg( + Arg::with_name("line") + .long("--line") + .required(true) + .takes_value(true), + ) + .arg( + Arg::with_name("file") + .long("--file") + .required(true) + .takes_value(true), + ), ) .subcommand(SubCommand::with_name("parse")) .get_matches(); @@ -24,7 +34,7 @@ fn main() -> Result<()> { ("parse", _) => { let tree = parse()?; println!("{}", tree); - }, + } ("render-test", Some(matches)) => { let file = matches.value_of("file").unwrap(); let file = Path::new(file); @@ -36,7 +46,6 @@ fn main() -> Result<()> { _ => unreachable!(), } Ok(()) - } fn parse() -> Result { diff --git a/src/algo/walk.rs b/src/algo/walk.rs index 86dd82cc9f..c6d0502519 100644 --- a/src/algo/walk.rs +++ b/src/algo/walk.rs @@ -1,6 +1,6 @@ use SyntaxNodeRef; -pub fn preorder<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator> { +pub fn preorder<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator> { walk(root).filter_map(|event| match event { WalkEvent::Enter(node) => Some(node), WalkEvent::Exit(_) => None, @@ -13,7 +13,7 @@ enum WalkEvent<'a> { Exit(SyntaxNodeRef<'a>), } -fn walk<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator> { +fn walk<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator> { let mut done = false; ::itertools::unfold(WalkEvent::Enter(root), move |pos| { if done { @@ -35,7 +35,7 @@ fn walk<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator> { None => match node.parent() { Some(node) => WalkEvent::Exit(node), None => WalkEvent::Exit(node), - } + }, } } } @@ -43,4 +43,3 @@ fn walk<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator> { Some(res) }) } - diff --git a/src/ast.rs b/src/ast.rs index b513eb13ef..48e1d23ac1 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,14 +1,16 @@ use std::sync::Arc; -use {SyntaxNode, TreeRoot, SyntaxRoot}; +use {SyntaxNode, SyntaxRoot, TreeRoot}; #[derive(Debug)] pub struct File> { - syntax: SyntaxNode + syntax: SyntaxNode, } impl File> { pub fn parse(text: &str) -> Self { - File { syntax: ::parse(text.to_owned()) } + File { + syntax: ::parse(text.to_owned()), + } } } diff --git a/src/lib.rs b/src/lib.rs index 9049beb296..953c9b8606 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,25 +20,25 @@ #![allow(missing_docs)] //#![warn(unreachable_pub)] // rust-lang/rust#47816 +extern crate itertools; extern crate text_unit; extern crate unicode_xid; -extern crate itertools; +pub mod algo; +pub mod ast; mod lexer; mod parser; mod syntax_kinds; -mod yellow; /// Utilities for simple uses of the parser. pub mod utils; -pub mod ast; -pub mod algo; +mod yellow; pub use { + ast::File, lexer::{tokenize, Token}, syntax_kinds::SyntaxKind, text_unit::{TextRange, TextUnit}, - yellow::{SyntaxNode, SyntaxNodeRef, TreeRoot, SyntaxRoot}, - ast::File, + yellow::{SyntaxNode, SyntaxNodeRef, SyntaxRoot, TreeRoot}, }; pub(crate) use yellow::SyntaxError; @@ -47,4 +47,3 @@ pub fn parse(text: String) -> SyntaxNode { let tokens = tokenize(&text); parser::parse::(text, &tokens) } - diff --git a/src/parser/grammar/expressions.rs b/src/parser/grammar/expressions.rs index a6456c8d5e..11bb3436b9 100644 --- a/src/parser/grammar/expressions.rs +++ b/src/parser/grammar/expressions.rs @@ -39,7 +39,7 @@ pub(super) fn expr(p: &mut Parser) { fn prefix_expr(p: &mut Parser) -> Option { match p.current() { AMPERSAND => Some(ref_expr(p)), - _ => atom_expr(p) + _ => atom_expr(p), } } diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs index 0d9eccd2ff..9147df87dc 100644 --- a/src/parser/grammar/items/mod.rs +++ b/src/parser/grammar/items/mod.rs @@ -218,7 +218,6 @@ fn extern_block(p: &mut Parser) { p.expect(R_CURLY); } - fn fn_item(p: &mut Parser) { assert!(p.at(FN_KW)); p.bump(); diff --git a/src/parser/grammar/items/traits.rs b/src/parser/grammar/items/traits.rs index c7450b761d..812cacfb7e 100644 --- a/src/parser/grammar/items/traits.rs +++ b/src/parser/grammar/items/traits.rs @@ -8,7 +8,6 @@ pub(super) fn trait_item(p: &mut Parser) { p.expect(R_CURLY); } - // test impl_item // impl Foo {} pub(super) fn impl_item(p: &mut Parser) { diff --git a/src/parser/grammar/mod.rs b/src/parser/grammar/mod.rs index c2da775a26..498b45d443 100644 --- a/src/parser/grammar/mod.rs +++ b/src/parser/grammar/mod.rs @@ -26,12 +26,15 @@ mod expressions; mod items; mod paths; mod patterns; -mod type_params; mod type_args; +mod type_params; mod types; use { - parser::{parser::{Parser, CompletedMarker}, token_set::TokenSet}, + parser::{ + parser::{CompletedMarker, Parser}, + token_set::TokenSet, + }, SyntaxKind::{self, *}, }; diff --git a/src/parser/grammar/paths.rs b/src/parser/grammar/paths.rs index 69ed84665a..6f66701ac3 100644 --- a/src/parser/grammar/paths.rs +++ b/src/parser/grammar/paths.rs @@ -20,7 +20,11 @@ pub(super) fn expr_path(p: &mut Parser) { } #[derive(Clone, Copy, Eq, PartialEq)] -enum Mode { Use, Type, Expr } +enum Mode { + Use, + Type, + Expr, +} fn path(p: &mut Parser, mode: Mode) { if !is_path_start(p) { @@ -55,7 +59,7 @@ fn path_segment(p: &mut Parser, mode: Mode, first: bool) { IDENT => { name_ref(p); path_generic_args(p, mode); - }, + } SELF_KW | SUPER_KW => p.bump(), _ => { p.error("expected identifier"); diff --git a/src/parser/grammar/patterns.rs b/src/parser/grammar/patterns.rs index a5d13a1246..7216807fda 100644 --- a/src/parser/grammar/patterns.rs +++ b/src/parser/grammar/patterns.rs @@ -43,11 +43,8 @@ fn ref_pat(p: &mut Parser) { // } fn bind_pat(p: &mut Parser) { let m = p.start(); - if p.eat(REF_KW) { - p.eat(MUT_KW); - } else { - p.eat(MUT_KW); - } + p.eat(REF_KW); + p.eat(MUT_KW); name(p); if p.eat(AT) { pattern(p); diff --git a/src/parser/grammar/type_args.rs b/src/parser/grammar/type_args.rs index 20e75b4b0d..adac73e7e2 100644 --- a/src/parser/grammar/type_args.rs +++ b/src/parser/grammar/type_args.rs @@ -12,7 +12,7 @@ pub(super) fn list(p: &mut Parser, colon_colon_required: bool) { m = p.start(); p.bump(); } - _ => return + _ => return, }; while !p.at(EOF) && !p.at(R_ANGLE) { diff --git a/src/utils.rs b/src/utils.rs index 826a7d60bb..f99d31b366 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,7 +16,7 @@ pub fn dump_tree(syntax: &SyntaxNode) -> String { errors: &mut BTreeSet, ) { buff.push_str(&String::from(" ").repeat(level)); - write!(buff, "{:?}\n", node).unwrap(); + writeln!(buff, "{:?}", node).unwrap(); let my_errors: Vec<_> = errors .iter() .filter(|e| e.offset == node.range().start()) @@ -25,7 +25,7 @@ pub fn dump_tree(syntax: &SyntaxNode) -> String { for err in my_errors { errors.remove(&err); buff.push_str(&String::from(" ").repeat(level)); - write!(buff, "err: `{}`\n", err.message).unwrap(); + writeln!(buff, "err: `{}`", err.message).unwrap(); } for child in node.children() { @@ -40,7 +40,7 @@ pub fn dump_tree(syntax: &SyntaxNode) -> String { for err in my_errors { errors.remove(&err); buff.push_str(&String::from(" ").repeat(level)); - write!(buff, "err: `{}`\n", err.message).unwrap(); + writeln!(buff, "err: `{}`", err.message).unwrap(); } } } diff --git a/src/yellow/green.rs b/src/yellow/green.rs index cda4e21671..8ddcc74b88 100644 --- a/src/yellow/green.rs +++ b/src/yellow/green.rs @@ -80,8 +80,14 @@ fn assert_send_sync() { #[derive(Clone, Debug)] pub(crate) enum GreenLeaf { - Whitespace { newlines: u8, spaces: u8 }, - Token { kind: SyntaxKind, text: Option> }, + Whitespace { + newlines: u8, + spaces: u8, + }, + Token { + kind: SyntaxKind, + text: Option>, + }, } impl GreenLeaf { @@ -121,7 +127,7 @@ impl GreenLeaf { assert!(newlines <= N_NEWLINES && spaces <= N_SPACES); &WS[N_NEWLINES - newlines..N_NEWLINES + spaces] } - GreenLeaf::Token { kind, text, } => match text { + GreenLeaf::Token { kind, text } => match text { None => kind.static_text().unwrap(), Some(t) => t, }, diff --git a/src/yellow/mod.rs b/src/yellow/mod.rs index 0cc90adbd4..f20be356d1 100644 --- a/src/yellow/mod.rs +++ b/src/yellow/mod.rs @@ -8,5 +8,5 @@ pub(crate) use self::{ builder::GreenBuilder, green::{GreenNode, GreenNodeBuilder}, red::RedNode, - syntax::{SyntaxError}, + syntax::SyntaxError, }; diff --git a/src/yellow/red.rs b/src/yellow/red.rs index a30318c6e8..c3d6c5f4f2 100644 --- a/src/yellow/red.rs +++ b/src/yellow/red.rs @@ -36,7 +36,8 @@ impl RedNode { fn new(green: GreenNode, parent: Option) -> RedNode { let n_children = green.children().len(); - let children = (0..n_children).map(|_| None) + let children = (0..n_children) + .map(|_| None) .collect::>() .into_boxed_slice(); RedNode { @@ -63,7 +64,7 @@ impl RedNode { pub(crate) fn get_child(&self, idx: usize) -> Option> { if idx >= self.n_children() { - return None + return None; } match &self.children.read().unwrap()[idx] { Some(child) => return Some(child.into()), diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs index 41dcf37612..487a4ef1d4 100644 --- a/src/yellow/syntax.rs +++ b/src/yellow/syntax.rs @@ -6,7 +6,7 @@ use { TextRange, TextUnit, }; -pub trait TreeRoot: Deref + Clone {} +pub trait TreeRoot: Deref + Clone {} impl TreeRoot for Arc {} @@ -83,14 +83,12 @@ impl SyntaxNode { self.red().green().text() } - pub fn children<'a>(&'a self) -> impl Iterator> + 'a { + pub fn children<'a>(&'a self) -> impl Iterator> + 'a { let red = self.red(); let n_children = red.n_children(); - (0..n_children).map(move |i| { - SyntaxNode { - root: self.root.clone(), - red: red.get_child(i).unwrap(), - } + (0..n_children).map(move |i| SyntaxNode { + root: self.root.clone(), + red: red.get_child(i).unwrap(), }) } diff --git a/tests/testutils/src/lib.rs b/tests/testutils/src/lib.rs index 7c481156f2..deeb707d3d 100644 --- a/tests/testutils/src/lib.rs +++ b/tests/testutils/src/lib.rs @@ -2,7 +2,7 @@ extern crate difference; use std::{ fs, - path::{Path, PathBuf} + path::{Path, PathBuf}, }; use difference::Changeset; diff --git a/tools/src/lib.rs b/tools/src/lib.rs index 21a9468bca..5a7d846ffb 100644 --- a/tools/src/lib.rs +++ b/tools/src/lib.rs @@ -1,7 +1,7 @@ extern crate itertools; -use std::hash; use itertools::Itertools; +use std::hash; #[derive(Debug)] pub struct Test { @@ -28,18 +28,17 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { match block.next() { Some((idx, line)) if line.starts_with("test ") => { break (idx, line["test ".len()..].to_string()) - }, + } Some(_) => (), None => continue 'outer, } }; let text: String = itertools::join( - block.map(|(_, line)| line) - .chain(::std::iter::once("")), - "\n" + block.map(|(_, line)| line).chain(::std::iter::once("")), + "\n", ); assert!(!text.trim().is_empty() && text.ends_with("\n")); - res.push((start_line, Test {name, text })) + res.push((start_line, Test { name, text })) } res } diff --git a/tools/src/main.rs b/tools/src/main.rs index 3acb6e7ed0..7c6c7a1aa6 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -3,13 +3,17 @@ extern crate clap; extern crate failure; extern crate ron; extern crate tera; -extern crate walkdir; extern crate tools; +extern crate walkdir; #[macro_use] extern crate commandspec; -use std::{collections::{HashMap}, fs, path::{Path, PathBuf}}; use clap::{App, Arg, SubCommand}; +use std::{ + collections::HashMap, + fs, + path::{Path, PathBuf}, +}; use tools::{collect_tests, Test}; type Result = ::std::result::Result; @@ -71,7 +75,8 @@ fn get_kinds() -> Result { tera.add_raw_template("grammar", &template) .map_err(|e| format_err!("template error: {:?}", e))?; tera.register_global_function("concat", Box::new(concat)); - let ret = tera.render("grammar", &grammar) + let ret = tera + .render("grammar", &grammar) .map_err(|e| format_err!("template error: {:?}", e))?; return Ok(ret); @@ -157,7 +162,10 @@ fn existing_tests(dir: &Path) -> Result> { file_name[5..file_name.len() - 3].to_string() }; let text = fs::read_to_string(&path)?; - let test = Test { name: name.clone(), text }; + let test = Test { + name: name.clone(), + text, + }; match res.insert(name, (path, test)) { Some(old) => println!("Duplicate test: {:?}", old), None => (), @@ -167,17 +175,23 @@ fn existing_tests(dir: &Path) -> Result> { } fn install_code_extension() -> Result<()> { - execute!(r" + execute!( + r" cd code npm install - ")?; - execute!(r" + " + )?; + execute!( + r" cd code ./node_modules/vsce/out/vsce package - ")?; - execute!(r" + " + )?; + execute!( + r" cd code code --install-extension ./libsyntax-rust-0.0.1.vsix - ")?; + " + )?; Ok(()) }