diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 47 |
1 files changed, 28 insertions, 19 deletions
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 @@ | |||
11 | //! [rfc#2256]: <https://github.com/rust-lang/rfcs/pull/2256> | 11 | //! [rfc#2256]: <https://github.com/rust-lang/rfcs/pull/2256> |
12 | //! [RFC.md]: <https://github.com/matklad/libsyntax2/blob/master/docs/RFC.md> | 12 | //! [RFC.md]: <https://github.com/matklad/libsyntax2/blob/master/docs/RFC.md> |
13 | 13 | ||
14 | #![forbid(missing_debug_implementations, unconditional_recursion, future_incompatible)] | 14 | #![forbid( |
15 | missing_debug_implementations, | ||
16 | unconditional_recursion, | ||
17 | future_incompatible | ||
18 | )] | ||
15 | #![deny(bad_style, missing_docs)] | 19 | #![deny(bad_style, missing_docs)] |
16 | #![allow(missing_docs)] | 20 | #![allow(missing_docs)] |
17 | //#![warn(unreachable_pub)] // rust-lang/rust#47816 | 21 | //#![warn(unreachable_pub)] // rust-lang/rust#47816 |
18 | 22 | ||
19 | extern crate unicode_xid; | ||
20 | extern crate text_unit; | 23 | extern crate text_unit; |
24 | extern crate unicode_xid; | ||
21 | 25 | ||
22 | mod lexer; | 26 | mod lexer; |
23 | mod parser; | 27 | mod parser; |
24 | mod yellow; | ||
25 | mod syntax_kinds; | 28 | mod syntax_kinds; |
29 | mod yellow; | ||
26 | 30 | ||
27 | pub use { | 31 | pub use { |
28 | text_unit::{TextRange, TextUnit}, | 32 | lexer::{tokenize, Token}, |
29 | syntax_kinds::SyntaxKind, | 33 | syntax_kinds::SyntaxKind, |
34 | text_unit::{TextRange, TextUnit}, | ||
30 | yellow::{SyntaxNode, SyntaxNodeRef}, | 35 | yellow::{SyntaxNode, SyntaxNodeRef}, |
31 | lexer::{tokenize, Token}, | ||
32 | }; | 36 | }; |
33 | 37 | ||
34 | pub(crate) use { | 38 | pub(crate) use yellow::SyntaxError; |
35 | yellow::SyntaxError | ||
36 | }; | ||
37 | 39 | ||
38 | pub fn parse(text: String) -> SyntaxNode { | 40 | pub fn parse(text: String) -> SyntaxNode { |
39 | let tokens = tokenize(&text); | 41 | let tokens = tokenize(&text); |
40 | parser::parse::<yellow::GreenBuilder>(text, &tokens) | 42 | parser::parse::<yellow::GreenBuilder>(text, &tokens) |
41 | } | 43 | } |
42 | 44 | ||
43 | |||
44 | /// Utilities for simple uses of the parser. | 45 | /// Utilities for simple uses of the parser. |
45 | pub mod utils { | 46 | pub mod utils { |
46 | use std::{ | 47 | use std::{collections::BTreeSet, fmt::Write}; |
47 | fmt::Write, | ||
48 | collections::BTreeSet | ||
49 | }; | ||
50 | 48 | ||
51 | use {SyntaxNode, SyntaxNodeRef, SyntaxError}; | 49 | use {SyntaxError, SyntaxNode, SyntaxNodeRef}; |
52 | 50 | ||
53 | /// Parse a file and create a string representation of the resulting parse tree. | 51 | /// Parse a file and create a string representation of the resulting parse tree. |
54 | pub fn dump_tree_green(syntax: &SyntaxNode) -> String { | 52 | pub fn dump_tree_green(syntax: &SyntaxNode) -> String { |
@@ -58,11 +56,19 @@ pub mod utils { | |||
58 | go(syntax, &mut result, 0, &mut errors); | 56 | go(syntax, &mut result, 0, &mut errors); |
59 | return result; | 57 | return result; |
60 | 58 | ||
61 | fn go(node: SyntaxNodeRef, buff: &mut String, level: usize, errors: &mut BTreeSet<SyntaxError>) { | 59 | fn go( |
60 | node: SyntaxNodeRef, | ||
61 | buff: &mut String, | ||
62 | level: usize, | ||
63 | errors: &mut BTreeSet<SyntaxError>, | ||
64 | ) { | ||
62 | buff.push_str(&String::from(" ").repeat(level)); | 65 | buff.push_str(&String::from(" ").repeat(level)); |
63 | write!(buff, "{:?}\n", node).unwrap(); | 66 | write!(buff, "{:?}\n", node).unwrap(); |
64 | let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().start()) | 67 | let my_errors: Vec<_> = errors |
65 | .cloned().collect(); | 68 | .iter() |
69 | .filter(|e| e.offset == node.range().start()) | ||
70 | .cloned() | ||
71 | .collect(); | ||
66 | for err in my_errors { | 72 | for err in my_errors { |
67 | errors.remove(&err); | 73 | errors.remove(&err); |
68 | buff.push_str(&String::from(" ").repeat(level)); | 74 | buff.push_str(&String::from(" ").repeat(level)); |
@@ -73,8 +79,11 @@ pub mod utils { | |||
73 | go(child, buff, level + 1, errors) | 79 | go(child, buff, level + 1, errors) |
74 | } | 80 | } |
75 | 81 | ||
76 | let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().end()) | 82 | let my_errors: Vec<_> = errors |
77 | .cloned().collect(); | 83 | .iter() |
84 | .filter(|e| e.offset == node.range().end()) | ||
85 | .cloned() | ||
86 | .collect(); | ||
78 | for err in my_errors { | 87 | for err in my_errors { |
79 | errors.remove(&err); | 88 | errors.remove(&err); |
80 | buff.push_str(&String::from(" ").repeat(level)); | 89 | buff.push_str(&String::from(" ").repeat(level)); |