aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/lexer
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-02-20 12:47:32 +0000
committerAleksey Kladov <[email protected]>2019-02-20 12:47:32 +0000
commit5222b8aba3b1c2c68706aacf6869423a8e4fe6d5 (patch)
treec8a6e999b8ac5f1f29bde86a2e0b3a53466bb369 /crates/ra_syntax/src/lexer
parent9d0cda4bc84350961f3884e75a1c20e62c449ede (diff)
move all parsing related bits to a separate module
Diffstat (limited to 'crates/ra_syntax/src/lexer')
-rw-r--r--crates/ra_syntax/src/lexer/classes.rs26
-rw-r--r--crates/ra_syntax/src/lexer/comments.rs57
-rw-r--r--crates/ra_syntax/src/lexer/numbers.rs67
-rw-r--r--crates/ra_syntax/src/lexer/ptr.rs162
-rw-r--r--crates/ra_syntax/src/lexer/strings.rs111
5 files changed, 0 insertions, 423 deletions
diff --git a/crates/ra_syntax/src/lexer/classes.rs b/crates/ra_syntax/src/lexer/classes.rs
deleted file mode 100644
index 4235d2648..000000000
--- a/crates/ra_syntax/src/lexer/classes.rs
+++ /dev/null
@@ -1,26 +0,0 @@
1use unicode_xid::UnicodeXID;
2
3pub fn is_ident_start(c: char) -> bool {
4 (c >= 'a' && c <= 'z')
5 || (c >= 'A' && c <= 'Z')
6 || c == '_'
7 || (c > '\x7f' && UnicodeXID::is_xid_start(c))
8}
9
10pub fn is_ident_continue(c: char) -> bool {
11 (c >= 'a' && c <= 'z')
12 || (c >= 'A' && c <= 'Z')
13 || (c >= '0' && c <= '9')
14 || c == '_'
15 || (c > '\x7f' && UnicodeXID::is_xid_continue(c))
16}
17
18pub fn is_whitespace(c: char) -> bool {
19 //FIXME: use is_pattern_whitespace
20 //https://github.com/behnam/rust-unic/issues/192
21 c.is_whitespace()
22}
23
24pub fn is_dec_digit(c: char) -> bool {
25 '0' <= c && c <= '9'
26}
diff --git a/crates/ra_syntax/src/lexer/comments.rs b/crates/ra_syntax/src/lexer/comments.rs
deleted file mode 100644
index afe6886a1..000000000
--- a/crates/ra_syntax/src/lexer/comments.rs
+++ /dev/null
@@ -1,57 +0,0 @@
1use crate::lexer::ptr::Ptr;
2
3use crate::SyntaxKind::{self, *};
4
5pub(crate) fn scan_shebang(ptr: &mut Ptr) -> bool {
6 if ptr.at_str("!/") {
7 ptr.bump();
8 ptr.bump();
9 bump_until_eol(ptr);
10 true
11 } else {
12 false
13 }
14}
15
16fn scan_block_comment(ptr: &mut Ptr) -> Option<SyntaxKind> {
17 if ptr.at('*') {
18 ptr.bump();
19 let mut depth: u32 = 1;
20 while depth > 0 {
21 if ptr.at_str("*/") {
22 depth -= 1;
23 ptr.bump();
24 ptr.bump();
25 } else if ptr.at_str("/*") {
26 depth += 1;
27 ptr.bump();
28 ptr.bump();
29 } else if ptr.bump().is_none() {
30 break;
31 }
32 }
33 Some(COMMENT)
34 } else {
35 None
36 }
37}
38
39pub(crate) fn scan_comment(ptr: &mut Ptr) -> Option<SyntaxKind> {
40 if ptr.at('/') {
41 bump_until_eol(ptr);
42 Some(COMMENT)
43 } else {
44 scan_block_comment(ptr)
45 }
46}
47
48fn bump_until_eol(ptr: &mut Ptr) {
49 loop {
50 if ptr.at('\n') || ptr.at_str("\r\n") {
51 return;
52 }
53 if ptr.bump().is_none() {
54 break;
55 }
56 }
57}
diff --git a/crates/ra_syntax/src/lexer/numbers.rs b/crates/ra_syntax/src/lexer/numbers.rs
deleted file mode 100644
index 46daf5e52..000000000
--- a/crates/ra_syntax/src/lexer/numbers.rs
+++ /dev/null
@@ -1,67 +0,0 @@
1use crate::lexer::classes::*;
2use crate::lexer::ptr::Ptr;
3
4use crate::SyntaxKind::{self, *};
5
6pub(crate) fn scan_number(c: char, ptr: &mut Ptr) -> SyntaxKind {
7 if c == '0' {
8 match ptr.current().unwrap_or('\0') {
9 'b' | 'o' => {
10 ptr.bump();
11 scan_digits(ptr, false);
12 }
13 'x' => {
14 ptr.bump();
15 scan_digits(ptr, true);
16 }
17 '0'...'9' | '_' | '.' | 'e' | 'E' => {
18 scan_digits(ptr, true);
19 }
20 _ => return INT_NUMBER,
21 }
22 } else {
23 scan_digits(ptr, false);
24 }
25
26 // might be a float, but don't be greedy if this is actually an
27 // integer literal followed by field/method access or a range pattern
28 // (`0..2` and `12.foo()`)
29 if ptr.at('.') && !(ptr.at_str("..") || ptr.nth_is_p(1, is_ident_start)) {
30 // might have stuff after the ., and if it does, it needs to start
31 // with a number
32 ptr.bump();
33 scan_digits(ptr, false);
34 scan_float_exponent(ptr);
35 return FLOAT_NUMBER;
36 }
37 // it might be a float if it has an exponent
38 if ptr.at('e') || ptr.at('E') {
39 scan_float_exponent(ptr);
40 return FLOAT_NUMBER;
41 }
42 INT_NUMBER
43}
44
45fn scan_digits(ptr: &mut Ptr, allow_hex: bool) {
46 while let Some(c) = ptr.current() {
47 match c {
48 '_' | '0'...'9' => {
49 ptr.bump();
50 }
51 'a'...'f' | 'A'...'F' if allow_hex => {
52 ptr.bump();
53 }
54 _ => return,
55 }
56 }
57}
58
59fn scan_float_exponent(ptr: &mut Ptr) {
60 if ptr.at('e') || ptr.at('E') {
61 ptr.bump();
62 if ptr.at('-') || ptr.at('+') {
63 ptr.bump();
64 }
65 scan_digits(ptr, false);
66 }
67}
diff --git a/crates/ra_syntax/src/lexer/ptr.rs b/crates/ra_syntax/src/lexer/ptr.rs
deleted file mode 100644
index c341c4176..000000000
--- a/crates/ra_syntax/src/lexer/ptr.rs
+++ /dev/null
@@ -1,162 +0,0 @@
1use crate::TextUnit;
2
3use std::str::Chars;
4
5/// A simple view into the characters of a string.
6pub(crate) struct Ptr<'s> {
7 text: &'s str,
8 len: TextUnit,
9}
10
11impl<'s> Ptr<'s> {
12 /// Creates a new `Ptr` from a string.
13 pub fn new(text: &'s str) -> Ptr<'s> {
14 Ptr { text, len: 0.into() }
15 }
16
17 /// Gets the length of the remaining string.
18 pub fn into_len(self) -> TextUnit {
19 self.len
20 }
21
22 /// Gets the current character, if one exists.
23 pub fn current(&self) -> Option<char> {
24 self.chars().next()
25 }
26
27 /// Gets the nth character from the current.
28 /// For example, 0 will return the current character, 1 will return the next, etc.
29 pub fn nth(&self, n: u32) -> Option<char> {
30 self.chars().nth(n as usize)
31 }
32
33 /// Checks whether the current character is `c`.
34 pub fn at(&self, c: char) -> bool {
35 self.current() == Some(c)
36 }
37
38 /// Checks whether the next characters match `s`.
39 pub fn at_str(&self, s: &str) -> bool {
40 let chars = self.chars();
41 chars.as_str().starts_with(s)
42 }
43
44 /// Checks whether the current character satisfies the predicate `p`.
45 pub fn at_p<P: Fn(char) -> bool>(&self, p: P) -> bool {
46 self.current().map(p) == Some(true)
47 }
48
49 /// Checks whether the nth character satisfies the predicate `p`.
50 pub fn nth_is_p<P: Fn(char) -> bool>(&self, n: u32, p: P) -> bool {
51 self.nth(n).map(p) == Some(true)
52 }
53
54 /// Moves to the next character.
55 pub fn bump(&mut self) -> Option<char> {
56 let ch = self.chars().next()?;
57 self.len += TextUnit::of_char(ch);
58 Some(ch)
59 }
60
61 /// Moves to the next character as long as `pred` is satisfied.
62 pub fn bump_while<F: Fn(char) -> bool>(&mut self, pred: F) {
63 loop {
64 match self.current() {
65 Some(c) if pred(c) => {
66 self.bump();
67 }
68 _ => return,
69 }
70 }
71 }
72
73 /// Returns the text up to the current point.
74 pub fn current_token_text(&self) -> &str {
75 let len: u32 = self.len.into();
76 &self.text[..len as usize]
77 }
78
79 /// Returns an iterator over the remaining characters.
80 fn chars(&self) -> Chars {
81 let len: u32 = self.len.into();
82 self.text[len as usize..].chars()
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn test_current() {
92 let ptr = Ptr::new("test");
93 assert_eq!(ptr.current(), Some('t'));
94 }
95
96 #[test]
97 fn test_nth() {
98 let ptr = Ptr::new("test");
99 assert_eq!(ptr.nth(0), Some('t'));
100 assert_eq!(ptr.nth(1), Some('e'));
101 assert_eq!(ptr.nth(2), Some('s'));
102 assert_eq!(ptr.nth(3), Some('t'));
103 assert_eq!(ptr.nth(4), None);
104 }
105
106 #[test]
107 fn test_at() {
108 let ptr = Ptr::new("test");
109 assert!(ptr.at('t'));
110 assert!(!ptr.at('a'));
111 }
112
113 #[test]
114 fn test_at_str() {
115 let ptr = Ptr::new("test");
116 assert!(ptr.at_str("t"));
117 assert!(ptr.at_str("te"));
118 assert!(ptr.at_str("test"));
119 assert!(!ptr.at_str("tests"));
120 assert!(!ptr.at_str("rust"));
121 }
122
123 #[test]
124 fn test_at_p() {
125 let ptr = Ptr::new("test");
126 assert!(ptr.at_p(|c| c == 't'));
127 assert!(!ptr.at_p(|c| c == 'e'));
128 }
129
130 #[test]
131 fn test_nth_is_p() {
132 let ptr = Ptr::new("test");
133 assert!(ptr.nth_is_p(0, |c| c == 't'));
134 assert!(!ptr.nth_is_p(1, |c| c == 't'));
135 assert!(ptr.nth_is_p(3, |c| c == 't'));
136 assert!(!ptr.nth_is_p(150, |c| c == 't'));
137 }
138
139 #[test]
140 fn test_bump() {
141 let mut ptr = Ptr::new("test");
142 assert_eq!(ptr.current(), Some('t'));
143 ptr.bump();
144 assert_eq!(ptr.current(), Some('e'));
145 ptr.bump();
146 assert_eq!(ptr.current(), Some('s'));
147 ptr.bump();
148 assert_eq!(ptr.current(), Some('t'));
149 ptr.bump();
150 assert_eq!(ptr.current(), None);
151 ptr.bump();
152 assert_eq!(ptr.current(), None);
153 }
154
155 #[test]
156 fn test_bump_while() {
157 let mut ptr = Ptr::new("test");
158 assert_eq!(ptr.current(), Some('t'));
159 ptr.bump_while(|c| c != 's');
160 assert_eq!(ptr.current(), Some('s'));
161 }
162}
diff --git a/crates/ra_syntax/src/lexer/strings.rs b/crates/ra_syntax/src/lexer/strings.rs
deleted file mode 100644
index 5c1cf3e9c..000000000
--- a/crates/ra_syntax/src/lexer/strings.rs
+++ /dev/null
@@ -1,111 +0,0 @@
1use crate::SyntaxKind::{self, *};
2
3use crate::lexer::ptr::Ptr;
4
5pub(crate) fn is_string_literal_start(c: char, c1: Option<char>, c2: Option<char>) -> bool {
6 match (c, c1, c2) {
7 ('r', Some('"'), _)
8 | ('r', Some('#'), Some('"'))
9 | ('r', Some('#'), Some('#'))
10 | ('b', Some('"'), _)
11 | ('b', Some('\''), _)
12 | ('b', Some('r'), Some('"'))
13 | ('b', Some('r'), Some('#')) => true,
14 _ => false,
15 }
16}
17
18pub(crate) fn scan_char(ptr: &mut Ptr) {
19 while let Some(c) = ptr.current() {
20 match c {
21 '\\' => {
22 ptr.bump();
23 if ptr.at('\\') || ptr.at('\'') {
24 ptr.bump();
25 }
26 }
27 '\'' => {
28 ptr.bump();
29 return;
30 }
31 '\n' => return,
32 _ => {
33 ptr.bump();
34 }
35 }
36 }
37}
38
39pub(crate) fn scan_byte_char_or_string(ptr: &mut Ptr) -> SyntaxKind {
40 // unwrapping and not-exhaustive match are ok
41 // because of string_literal_start
42 let c = ptr.bump().unwrap();
43 match c {
44 '\'' => {
45 scan_byte(ptr);
46 BYTE
47 }
48 '"' => {
49 scan_byte_string(ptr);
50 BYTE_STRING
51 }
52 'r' => {
53 scan_raw_string(ptr);
54 RAW_BYTE_STRING
55 }
56 _ => unreachable!(),
57 }
58}
59
60pub(crate) fn scan_string(ptr: &mut Ptr) {
61 while let Some(c) = ptr.current() {
62 match c {
63 '\\' => {
64 ptr.bump();
65 if ptr.at('\\') || ptr.at('"') {
66 ptr.bump();
67 }
68 }
69 '"' => {
70 ptr.bump();
71 return;
72 }
73 _ => {
74 ptr.bump();
75 }
76 }
77 }
78}
79
80pub(crate) fn scan_raw_string(ptr: &mut Ptr) {
81 let mut hashes = 0;
82 while ptr.at('#') {
83 hashes += 1;
84 ptr.bump();
85 }
86 if !ptr.at('"') {
87 return;
88 }
89 ptr.bump();
90
91 while let Some(c) = ptr.bump() {
92 if c == '"' {
93 let mut hashes_left = hashes;
94 while ptr.at('#') && hashes_left > 0 {
95 hashes_left -= 1;
96 ptr.bump();
97 }
98 if hashes_left == 0 {
99 return;
100 }
101 }
102 }
103}
104
105fn scan_byte(ptr: &mut Ptr) {
106 scan_char(ptr)
107}
108
109fn scan_byte_string(ptr: &mut Ptr) {
110 scan_string(ptr)
111}