]> git.lizzy.rs Git - rust.git/commitdiff
kill utils module
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 21 Feb 2019 12:51:22 +0000 (15:51 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 21 Feb 2019 12:57:40 +0000 (15:57 +0300)
crates/ra_cli/src/main.rs
crates/ra_ide_api/src/lib.rs
crates/ra_ide_api_light/src/lib.rs
crates/ra_syntax/fuzz/fuzz_targets/parser.rs
crates/ra_syntax/src/lib.rs
crates/ra_syntax/src/parsing/reparsing.rs
crates/ra_syntax/src/syntax_node.rs
crates/ra_syntax/src/utils.rs [deleted file]
crates/ra_syntax/src/validation.rs
crates/ra_syntax/tests/test.rs

index 72e6ae4d5219cd6db0a97f82ad6e9d792a7c0ff9..294f4b8af7884dff1dda07e66169ddd656137b02 100644 (file)
@@ -4,7 +4,7 @@
 
 use clap::{App, Arg, SubCommand};
 use join_to_string::join;
-use ra_ide_api_light::{extend_selection, file_structure, syntax_tree};
+use ra_ide_api_light::{extend_selection, file_structure};
 use ra_syntax::{SourceFile, TextRange, TreeArc, AstNode};
 use tools::collect_tests;
 use flexi_logger::Logger;
@@ -37,7 +37,7 @@ fn main() -> Result<()> {
             let file = file()?;
             let elapsed = start.elapsed();
             if !matches.is_present("no-dump") {
-                println!("{}", syntax_tree(&file));
+                println!("{}", file.syntax().debug_dump());
             }
             eprintln!("parsing: {:?}", elapsed);
             ::std::mem::forget(file);
@@ -94,7 +94,7 @@ fn render_test(file: &Path, line: usize) -> Result<(String, String)> {
         Some((_start_line, test)) => test,
     };
     let file = SourceFile::parse(&test.text);
-    let tree = syntax_tree(&file);
+    let tree = file.syntax().debug_dump();
     Ok((test.text, tree))
 }
 
index 57a490fa75ab4bc908396972e2668c71761c6366..4b9fc9372e5a3ed578c517b87f403349493d9788 100644 (file)
@@ -38,7 +38,7 @@
 
 use std::sync::Arc;
 
-use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit};
+use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit, AstNode};
 use ra_text_edit::TextEdit;
 use ra_db::{
     SourceDatabase, CheckCanceled,
@@ -244,8 +244,7 @@ pub fn matching_brace(&self, position: FilePosition) -> Option<TextUnit> {
     /// Returns a syntax tree represented as `String`, for debug purposes.
     // FIXME: use a better name here.
     pub fn syntax_tree(&self, file_id: FileId) -> String {
-        let file = self.db.parse(file_id);
-        ra_ide_api_light::syntax_tree(&file)
+        self.db.parse(file_id).syntax().debug_dump()
     }
 
     /// Returns an edit to remove all newlines in the range, cleaning up minor
index 6d1ce8dbfb6f0dc99e3e3b758cfe3ab80983a136..43cdd6ea4dd3e94a8a7793660429ea5bee634b13 100644 (file)
@@ -123,10 +123,6 @@ pub fn highlight(root: &SyntaxNode) -> Vec<HighlightedRange> {
     res
 }
 
-pub fn syntax_tree(file: &SourceFile) -> String {
-    ::ra_syntax::utils::dump_tree(file.syntax())
-}
-
 #[cfg(test)]
 mod tests {
     use ra_syntax::AstNode;
index 396c0ecaf31b79226f4befd22838ee2628170ca2..4667d5579fe7a5b32ef7bfbc64f06f21720e6b78 100644 (file)
@@ -4,6 +4,6 @@
 
 fuzz_target!(|data: &[u8]| {
     if let Ok(text) = std::str::from_utf8(data) {
-        ra_syntax::utils::check_fuzz_invariants(text)
+        ra_syntax::check_fuzz_invariants(text)
     }
 });
index 6982b98150f7263bbc8a1433885a849180235c4a..dc4b779e82fd63064783b348932c6dfa02fd2c71 100644 (file)
@@ -27,8 +27,6 @@
 
 pub mod algo;
 pub mod ast;
-/// Utilities for simple uses of the parser.
-pub mod utils;
 
 pub use rowan::{SmolStr, TextRange, TextUnit};
 pub use ra_parser::SyntaxKind;
@@ -51,7 +49,7 @@ impl SourceFile {
     fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreeArc<SourceFile> {
         let root = SyntaxNode::new(green, errors);
         if cfg!(debug_assertions) {
-            utils::validate_block_structure(&root);
+            validation::validate_block_structure(&root);
         }
         assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
         TreeArc::cast(root)
@@ -82,3 +80,10 @@ pub fn errors(&self) -> Vec<SyntaxError> {
         errors
     }
 }
+
+pub fn check_fuzz_invariants(text: &str) {
+    let file = SourceFile::parse(text);
+    let root = file.syntax();
+    validation::validate_block_structure(root);
+    let _ = file.errors();
+}
index 6957c26c0e984cca66f285c0c8e9bf95e205e257..19d8adcfb3701a8ecbe26a91a565aad766582dff 100644 (file)
@@ -143,7 +143,7 @@ fn merge_errors(
 mod tests {
     use test_utils::{extract_range, assert_eq_text};
 
-    use crate::{SourceFile, AstNode, utils::dump_tree};
+    use crate::{SourceFile, AstNode};
     use super::*;
 
     fn do_check<F>(before: &str, replace_with: &str, reparser: F)
@@ -169,8 +169,8 @@ fn do_check<F>(before: &str, replace_with: &str, reparser: F)
         };
 
         assert_eq_text!(
-            &dump_tree(fully_reparsed.syntax()),
-            &dump_tree(incrementally_reparsed.syntax()),
+            &fully_reparsed.syntax().debug_dump(),
+            &incrementally_reparsed.syntax().debug_dump(),
         )
     }
 
index a1bc0b499465f398ac744247bcd96b6bf494b9c6..1ca1c992beff56214e405a92080cc835b6c0f8dc 100644 (file)
@@ -6,12 +6,12 @@
 //! The *real* implementation is in the (language-agnostic) `rowan` crate, this
 //! modules just wraps its API.
 
-use std::{fmt, borrow::Borrow};
+use std::{fmt::{self, Write}, borrow::Borrow};
 
 use rowan::{Types, TransparentNewType};
 
 use crate::{
-    SmolStr, SyntaxKind, TextRange, SyntaxText,
+    SmolStr, SyntaxKind, TextRange, SyntaxText, SourceFile, AstNode,
     syntax_error::SyntaxError,
 };
 
@@ -134,6 +134,50 @@ pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> {
             WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)),
         })
     }
+
+    pub fn debug_dump(&self) -> String {
+        let mut errors: Vec<_> = match self.ancestors().find_map(SourceFile::cast) {
+            Some(file) => file.errors(),
+            None => self.root_data().to_vec(),
+        };
+        errors.sort_by_key(|e| e.offset());
+        let mut err_pos = 0;
+        let mut level = 0;
+        let mut buf = String::new();
+        macro_rules! indent {
+            () => {
+                for _ in 0..level {
+                    buf.push_str("  ");
+                }
+            };
+        }
+
+        for event in self.preorder() {
+            match event {
+                WalkEvent::Enter(node) => {
+                    indent!();
+                    writeln!(buf, "{:?}", node).unwrap();
+                    if node.first_child().is_none() {
+                        let off = node.range().end();
+                        while err_pos < errors.len() && errors[err_pos].offset() <= off {
+                            indent!();
+                            writeln!(buf, "err: `{}`", errors[err_pos]).unwrap();
+                            err_pos += 1;
+                        }
+                    }
+                    level += 1;
+                }
+                WalkEvent::Leave(_) => level -= 1,
+            }
+        }
+
+        assert_eq!(level, 0);
+        for err in errors[err_pos..].iter() {
+            writeln!(buf, "err: `{}`", err).unwrap();
+        }
+
+        buf
+    }
 }
 
 impl ToOwned for SyntaxNode {
diff --git a/crates/ra_syntax/src/utils.rs b/crates/ra_syntax/src/utils.rs
deleted file mode 100644 (file)
index 2e1b42d..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-use std::{str, fmt::Write};
-
-use crate::{SourceFile, SyntaxKind, WalkEvent, AstNode, SyntaxNode};
-
-/// Parse a file and create a string representation of the resulting parse tree.
-pub fn dump_tree(syntax: &SyntaxNode) -> String {
-    let mut errors: Vec<_> = match syntax.ancestors().find_map(SourceFile::cast) {
-        Some(file) => file.errors(),
-        None => syntax.root_data().to_vec(),
-    };
-    errors.sort_by_key(|e| e.offset());
-    let mut err_pos = 0;
-    let mut level = 0;
-    let mut buf = String::new();
-    macro_rules! indent {
-        () => {
-            for _ in 0..level {
-                buf.push_str("  ");
-            }
-        };
-    }
-
-    for event in syntax.preorder() {
-        match event {
-            WalkEvent::Enter(node) => {
-                indent!();
-                writeln!(buf, "{:?}", node).unwrap();
-                if node.first_child().is_none() {
-                    let off = node.range().end();
-                    while err_pos < errors.len() && errors[err_pos].offset() <= off {
-                        indent!();
-                        writeln!(buf, "err: `{}`", errors[err_pos]).unwrap();
-                        err_pos += 1;
-                    }
-                }
-                level += 1;
-            }
-            WalkEvent::Leave(_) => level -= 1,
-        }
-    }
-
-    assert_eq!(level, 0);
-    for err in errors[err_pos..].iter() {
-        writeln!(buf, "err: `{}`", err).unwrap();
-    }
-
-    buf
-}
-
-pub fn check_fuzz_invariants(text: &str) {
-    let file = SourceFile::parse(text);
-    let root = file.syntax();
-    validate_block_structure(root);
-    let _ = file.errors();
-}
-
-pub(crate) fn validate_block_structure(root: &SyntaxNode) {
-    let mut stack = Vec::new();
-    for node in root.descendants() {
-        match node.kind() {
-            SyntaxKind::L_CURLY => stack.push(node),
-            SyntaxKind::R_CURLY => {
-                if let Some(pair) = stack.pop() {
-                    assert_eq!(
-                        node.parent(),
-                        pair.parent(),
-                        "\nunpaired curleys:\n{}\n{}\n",
-                        root.text(),
-                        dump_tree(root),
-                    );
-                    assert!(
-                        node.next_sibling().is_none() && pair.prev_sibling().is_none(),
-                        "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
-                        node,
-                        root.text(),
-                        node.text(),
-                    );
-                }
-            }
-            _ => (),
-        }
-    }
-}
index 69958f0d74fc53d14ad62681ffcf2a321c1fe12f..69f344d65248afa3868396008cc16f2943919f72 100644 (file)
@@ -5,7 +5,8 @@
 mod block;
 
 use crate::{
-    SourceFile, SyntaxError, AstNode,
+    SourceFile, SyntaxError, AstNode, SyntaxNode,
+    SyntaxKind::{L_CURLY, R_CURLY},
     ast,
     algo::visit::{visitor_ctx, VisitorCtx},
 };
@@ -14,12 +15,40 @@ pub(crate) fn validate(file: &SourceFile) -> Vec<SyntaxError> {
     let mut errors = Vec::new();
     for node in file.syntax().descendants() {
         let _ = visitor_ctx(&mut errors)
-            .visit::<ast::Byte, _>(self::byte::validate_byte_node)
-            .visit::<ast::ByteString, _>(self::byte_string::validate_byte_string_node)
-            .visit::<ast::Char, _>(self::char::validate_char_node)
-            .visit::<ast::String, _>(self::string::validate_string_node)
-            .visit::<ast::Block, _>(self::block::validate_block_node)
+            .visit::<ast::Byte, _>(byte::validate_byte_node)
+            .visit::<ast::ByteString, _>(byte_string::validate_byte_string_node)
+            .visit::<ast::Char, _>(char::validate_char_node)
+            .visit::<ast::String, _>(string::validate_string_node)
+            .visit::<ast::Block, _>(block::validate_block_node)
             .accept(node);
     }
     errors
 }
+
+pub(crate) fn validate_block_structure(root: &SyntaxNode) {
+    let mut stack = Vec::new();
+    for node in root.descendants() {
+        match node.kind() {
+            L_CURLY => stack.push(node),
+            R_CURLY => {
+                if let Some(pair) = stack.pop() {
+                    assert_eq!(
+                        node.parent(),
+                        pair.parent(),
+                        "\nunpaired curleys:\n{}\n{}\n",
+                        root.text(),
+                        root.debug_dump(),
+                    );
+                    assert!(
+                        node.next_sibling().is_none() && pair.prev_sibling().is_none(),
+                        "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
+                        node,
+                        root.text(),
+                        node.text(),
+                    );
+                }
+            }
+            _ => (),
+        }
+    }
+}
index 168d0623dc9832750333be5c8d2d90f2c56a7dc0..458740c138ed5ebea72a610a1275731f0bb66dc9 100644 (file)
@@ -8,10 +8,7 @@
 };
 
 use test_utils::{project_dir, dir_tests, read_text, collect_tests};
-use ra_syntax::{
-    SourceFile, AstNode,
-    utils::{check_fuzz_invariants, dump_tree},
-};
+use ra_syntax::{SourceFile, AstNode, check_fuzz_invariants};
 
 #[test]
 fn lexer_tests() {
@@ -32,7 +29,7 @@ fn parser_tests() {
             "There should be no errors in the file {:?}",
             path.display()
         );
-        dump_tree(file.syntax())
+        file.syntax().debug_dump()
     });
     dir_tests(&test_data_dir(), &["parser/err", "parser/inline/err"], |text, path| {
         let file = SourceFile::parse(text);
@@ -43,7 +40,7 @@ fn parser_tests() {
             "There should be errors in the file {:?}",
             path.display()
         );
-        dump_tree(file.syntax())
+        file.syntax().debug_dump()
     });
 }