diff options
Diffstat (limited to 'crates/libsyntax2/src/parser_api.rs')
-rw-r--r-- | crates/libsyntax2/src/parser_api.rs | 178 |
1 files changed, 0 insertions, 178 deletions
diff --git a/crates/libsyntax2/src/parser_api.rs b/crates/libsyntax2/src/parser_api.rs deleted file mode 100644 index 772d753af..000000000 --- a/crates/libsyntax2/src/parser_api.rs +++ /dev/null | |||
@@ -1,178 +0,0 @@ | |||
1 | use { | ||
2 | token_set::TokenSet, | ||
3 | parser_impl::ParserImpl, | ||
4 | SyntaxKind::{self, ERROR}, | ||
5 | drop_bomb::DropBomb, | ||
6 | }; | ||
7 | |||
8 | /// `Parser` struct provides the low-level API for | ||
9 | /// navigating through the stream of tokens and | ||
10 | /// constructing the parse tree. The actual parsing | ||
11 | /// happens in the `grammar` module. | ||
12 | /// | ||
13 | /// However, the result of this `Parser` is not a real | ||
14 | /// tree, but rather a flat stream of events of the form | ||
15 | /// "start expression, consume number literal, | ||
16 | /// finish expression". See `Event` docs for more. | ||
17 | pub(crate) struct Parser<'t>(pub(super) ParserImpl<'t>); | ||
18 | |||
19 | impl<'t> Parser<'t> { | ||
20 | /// Returns the kind of the current token. | ||
21 | /// If parser has already reached the end of input, | ||
22 | /// the special `EOF` kind is returned. | ||
23 | pub(crate) fn current(&self) -> SyntaxKind { | ||
24 | self.nth(0) | ||
25 | } | ||
26 | |||
27 | /// Lookahead operation: returns the kind of the next nth | ||
28 | /// token. | ||
29 | pub(crate) fn nth(&self, n: u32) -> SyntaxKind { | ||
30 | self.0.nth(n) | ||
31 | } | ||
32 | |||
33 | /// Checks if the current token is `kind`. | ||
34 | pub(crate) fn at(&self, kind: SyntaxKind) -> bool { | ||
35 | self.current() == kind | ||
36 | } | ||
37 | |||
38 | /// Checks if the current token is `kind`. | ||
39 | pub(crate) fn at_ts(&self, kinds: TokenSet) -> bool { | ||
40 | kinds.contains(self.current()) | ||
41 | } | ||
42 | |||
43 | pub(crate) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> { | ||
44 | self.0.next2() | ||
45 | } | ||
46 | |||
47 | pub(crate) fn next3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> { | ||
48 | self.0.next3() | ||
49 | } | ||
50 | |||
51 | /// Checks if the current token is contextual keyword with text `t`. | ||
52 | pub(crate) fn at_contextual_kw(&self, t: &str) -> bool { | ||
53 | self.0.at_kw(t) | ||
54 | } | ||
55 | |||
56 | /// Starts a new node in the syntax tree. All nodes and tokens | ||
57 | /// consumed between the `start` and the corresponding `Marker::complete` | ||
58 | /// belong to the same node. | ||
59 | pub(crate) fn start(&mut self) -> Marker { | ||
60 | Marker::new(self.0.start()) | ||
61 | } | ||
62 | |||
63 | /// Advances the parser by one token. | ||
64 | pub(crate) fn bump(&mut self) { | ||
65 | self.0.bump(); | ||
66 | } | ||
67 | |||
68 | /// Advances the parser by one token, remapping its kind. | ||
69 | /// This is useful to create contextual keywords from | ||
70 | /// identifiers. For example, the lexer creates an `union` | ||
71 | /// *identifier* token, but the parser remaps it to the | ||
72 | /// `union` keyword, and keyword is what ends up in the | ||
73 | /// final tree. | ||
74 | pub(crate) fn bump_remap(&mut self, kind: SyntaxKind) { | ||
75 | self.0.bump_remap(kind); | ||
76 | } | ||
77 | |||
78 | /// Advances the parser by `n` tokens, remapping its kind. | ||
79 | /// This is useful to create compound tokens from parts. For | ||
80 | /// example, an `<<` token is two consecutive remapped `<` tokens | ||
81 | pub(crate) fn bump_compound(&mut self, kind: SyntaxKind, n: u8) { | ||
82 | self.0.bump_compound(kind, n); | ||
83 | } | ||
84 | |||
85 | /// Emit error with the `message` | ||
86 | /// TODO: this should be much more fancy and support | ||
87 | /// structured errors with spans and notes, like rustc | ||
88 | /// does. | ||
89 | pub(crate) fn error<T: Into<String>>(&mut self, message: T) { | ||
90 | self.0.error(message.into()) | ||
91 | } | ||
92 | |||
93 | /// Consume the next token if it is `kind`. | ||
94 | pub(crate) fn eat(&mut self, kind: SyntaxKind) -> bool { | ||
95 | if !self.at(kind) { | ||
96 | return false; | ||
97 | } | ||
98 | self.bump(); | ||
99 | true | ||
100 | } | ||
101 | |||
102 | /// Consume the next token if it is `kind` or emit an error | ||
103 | /// otherwise. | ||
104 | pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool { | ||
105 | if self.eat(kind) { | ||
106 | return true; | ||
107 | } | ||
108 | self.error(format!("expected {:?}", kind)); | ||
109 | false | ||
110 | } | ||
111 | |||
112 | /// Create an error node and consume the next token. | ||
113 | pub(crate) fn err_and_bump(&mut self, message: &str) { | ||
114 | self.err_recover(message, TokenSet::EMPTY); | ||
115 | } | ||
116 | |||
117 | /// Create an error node and consume the next token. | ||
118 | pub(crate) fn err_recover(&mut self, message: &str, recovery: TokenSet) { | ||
119 | if self.at(SyntaxKind::L_CURLY) | ||
120 | || self.at(SyntaxKind::R_CURLY) | ||
121 | || self.at_ts(recovery) { | ||
122 | self.error(message); | ||
123 | } else { | ||
124 | let m = self.start(); | ||
125 | self.error(message); | ||
126 | self.bump(); | ||
127 | m.complete(self, ERROR); | ||
128 | }; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | /// See `Parser::start`. | ||
133 | pub(crate) struct Marker { | ||
134 | pos: u32, | ||
135 | bomb: DropBomb, | ||
136 | } | ||
137 | |||
138 | impl Marker { | ||
139 | fn new(pos: u32) -> Marker { | ||
140 | Marker { | ||
141 | pos, | ||
142 | bomb: DropBomb::new("Marker must be either completed or abandoned"), | ||
143 | } | ||
144 | } | ||
145 | |||
146 | /// Finishes the syntax tree node and assigns `kind` to it. | ||
147 | pub(crate) fn complete(mut self, p: &mut Parser, kind: SyntaxKind) -> CompletedMarker { | ||
148 | self.bomb.defuse(); | ||
149 | p.0.complete(self.pos, kind); | ||
150 | CompletedMarker(self.pos, kind) | ||
151 | } | ||
152 | |||
153 | /// Abandons the syntax tree node. All its children | ||
154 | /// are attached to its parent instead. | ||
155 | pub(crate) fn abandon(mut self, p: &mut Parser) { | ||
156 | self.bomb.defuse(); | ||
157 | p.0.abandon(self.pos); | ||
158 | } | ||
159 | } | ||
160 | |||
161 | pub(crate) struct CompletedMarker(u32, SyntaxKind); | ||
162 | |||
163 | impl CompletedMarker { | ||
164 | /// This one is tricky :-) | ||
165 | /// This method allows to create a new node which starts | ||
166 | /// *before* the current one. That is, parser could start | ||
167 | /// node `A`, then complete it, and then after parsing the | ||
168 | /// whole `A`, decide that it should have started some node | ||
169 | /// `B` before starting `A`. `precede` allows to do exactly | ||
170 | /// that. See also docs about `forward_parent` in `Event::Start`. | ||
171 | pub(crate) fn precede(self, p: &mut Parser) -> Marker { | ||
172 | Marker::new(p.0.precede(self.0)) | ||
173 | } | ||
174 | |||
175 | pub(crate) fn kind(&self) -> SyntaxKind { | ||
176 | self.1 | ||
177 | } | ||
178 | } | ||