diff options
author | Aleksey Kladov <[email protected]> | 2018-08-10 20:33:29 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-10 20:33:29 +0100 |
commit | 7c67612b8a894187fa3b64725531a5459f9211bf (patch) | |
tree | 9e2a536efa0c880d921fd8d4d74423afc9451fd4 /crates/libsyntax2/src/lexer/ptr.rs | |
parent | 26262aaf05983c5b7f41cc438e287523268fe1eb (diff) |
organizize
Diffstat (limited to 'crates/libsyntax2/src/lexer/ptr.rs')
-rw-r--r-- | crates/libsyntax2/src/lexer/ptr.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/crates/libsyntax2/src/lexer/ptr.rs b/crates/libsyntax2/src/lexer/ptr.rs new file mode 100644 index 000000000..d1391fd5f --- /dev/null +++ b/crates/libsyntax2/src/lexer/ptr.rs | |||
@@ -0,0 +1,74 @@ | |||
1 | use TextUnit; | ||
2 | |||
3 | use std::str::Chars; | ||
4 | |||
5 | pub(crate) struct Ptr<'s> { | ||
6 | text: &'s str, | ||
7 | len: TextUnit, | ||
8 | } | ||
9 | |||
10 | impl<'s> Ptr<'s> { | ||
11 | pub fn new(text: &'s str) -> Ptr<'s> { | ||
12 | Ptr { | ||
13 | text, | ||
14 | len: 0.into(), | ||
15 | } | ||
16 | } | ||
17 | |||
18 | pub fn into_len(self) -> TextUnit { | ||
19 | self.len | ||
20 | } | ||
21 | |||
22 | pub fn next(&self) -> Option<char> { | ||
23 | self.chars().next() | ||
24 | } | ||
25 | |||
26 | pub fn nnext(&self) -> Option<char> { | ||
27 | let mut chars = self.chars(); | ||
28 | chars.next()?; | ||
29 | chars.next() | ||
30 | } | ||
31 | |||
32 | pub fn next_is(&self, c: char) -> bool { | ||
33 | self.next() == Some(c) | ||
34 | } | ||
35 | |||
36 | pub fn nnext_is(&self, c: char) -> bool { | ||
37 | self.nnext() == Some(c) | ||
38 | } | ||
39 | |||
40 | pub fn next_is_p<P: Fn(char) -> bool>(&self, p: P) -> bool { | ||
41 | self.next().map(p) == Some(true) | ||
42 | } | ||
43 | |||
44 | pub fn nnext_is_p<P: Fn(char) -> bool>(&self, p: P) -> bool { | ||
45 | self.nnext().map(p) == Some(true) | ||
46 | } | ||
47 | |||
48 | pub fn bump(&mut self) -> Option<char> { | ||
49 | let ch = self.chars().next()?; | ||
50 | self.len += TextUnit::of_char(ch); | ||
51 | Some(ch) | ||
52 | } | ||
53 | |||
54 | pub fn bump_while<F: Fn(char) -> bool>(&mut self, pred: F) { | ||
55 | loop { | ||
56 | match self.next() { | ||
57 | Some(c) if pred(c) => { | ||
58 | self.bump(); | ||
59 | } | ||
60 | _ => return, | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | pub fn current_token_text(&self) -> &str { | ||
66 | let len: u32 = self.len.into(); | ||
67 | &self.text[..len as usize] | ||
68 | } | ||
69 | |||
70 | fn chars(&self) -> Chars { | ||
71 | let len: u32 = self.len.into(); | ||
72 | self.text[len as usize..].chars() | ||
73 | } | ||
74 | } | ||