aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parser_api.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-04 04:36:40 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-04 04:36:40 +0000
commit6295bbe6ec9741538307cd619ada1c9566f99a5d (patch)
tree9a004a531fce65c4a01e2d243dff638fb76ae373 /crates/ra_syntax/src/parser_api.rs
parent2fcc6bdafa1caffd4ddf98968ee967e9fb091cc0 (diff)
parent58139c558aa085588264ba659b8483a036c1da0e (diff)
Merge #391
391: docing parser methods r=csmoe a=csmoe Co-authored-by: csmoe <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/parser_api.rs')
-rw-r--r--crates/ra_syntax/src/parser_api.rs21
1 files changed, 16 insertions, 5 deletions
diff --git a/crates/ra_syntax/src/parser_api.rs b/crates/ra_syntax/src/parser_api.rs
index 02421def1..3487aef85 100644
--- a/crates/ra_syntax/src/parser_api.rs
+++ b/crates/ra_syntax/src/parser_api.rs
@@ -61,7 +61,7 @@ impl<'t> Parser<'t> {
61 Marker::new(self.0.start()) 61 Marker::new(self.0.start())
62 } 62 }
63 63
64 /// Advances the parser by one token. 64 /// Advances the parser by one token unconditionally.
65 pub(crate) fn bump(&mut self) { 65 pub(crate) fn bump(&mut self) {
66 self.0.bump(); 66 self.0.bump();
67 } 67 }
@@ -91,7 +91,7 @@ impl<'t> Parser<'t> {
91 self.0.error(message.into()) 91 self.0.error(message.into())
92 } 92 }
93 93
94 /// Consume the next token if it is `kind`. 94 /// Consume the next token if `kind` matches.
95 pub(crate) fn eat(&mut self, kind: SyntaxKind) -> bool { 95 pub(crate) fn eat(&mut self, kind: SyntaxKind) -> bool {
96 if !self.at(kind) { 96 if !self.at(kind) {
97 return false; 97 return false;
@@ -142,11 +142,13 @@ impl Marker {
142 } 142 }
143 } 143 }
144 144
145 /// Finishes the syntax tree node and assigns `kind` to it. 145 /// Finishes the syntax tree node and assigns `kind` to it,
146 /// and mark the create a `CompletedMarker` for possible future
147 /// operation like `.precede()` to deal with forward_parent.
146 pub(crate) fn complete(mut self, p: &mut Parser, kind: SyntaxKind) -> CompletedMarker { 148 pub(crate) fn complete(mut self, p: &mut Parser, kind: SyntaxKind) -> CompletedMarker {
147 self.bomb.defuse(); 149 self.bomb.defuse();
148 p.0.complete(self.pos, kind); 150 p.0.complete(self.pos, kind);
149 CompletedMarker(self.pos, kind) 151 CompletedMarker::new(self.pos, kind)
150 } 152 }
151 153
152 /// Abandons the syntax tree node. All its children 154 /// Abandons the syntax tree node. All its children
@@ -160,13 +162,22 @@ impl Marker {
160pub(crate) struct CompletedMarker(u32, SyntaxKind); 162pub(crate) struct CompletedMarker(u32, SyntaxKind);
161 163
162impl CompletedMarker { 164impl CompletedMarker {
163 /// This one is tricky :-) 165 fn new(pos: u32, kind: SyntaxKind) -> Self {
166 CompletedMarker(pos, kind)
167 }
168
164 /// This method allows to create a new node which starts 169 /// This method allows to create a new node which starts
165 /// *before* the current one. That is, parser could start 170 /// *before* the current one. That is, parser could start
166 /// node `A`, then complete it, and then after parsing the 171 /// node `A`, then complete it, and then after parsing the
167 /// whole `A`, decide that it should have started some node 172 /// whole `A`, decide that it should have started some node
168 /// `B` before starting `A`. `precede` allows to do exactly 173 /// `B` before starting `A`. `precede` allows to do exactly
169 /// that. See also docs about `forward_parent` in `Event::Start`. 174 /// that. See also docs about `forward_parent` in `Event::Start`.
175 ///
176 /// Given completed events `[START, FINISH]` and its corresponding
177 /// `CompletedMarker(pos: 0, _)`.
178 /// Append a new `START` events as `[START, FINISH, NEWSTART]`,
179 /// then mark `NEWSTART` as `START`'s parent with saving its relative
180 /// distance to `NEWSTART` into forward_parent(=2 in this case);
170 pub(crate) fn precede(self, p: &mut Parser) -> Marker { 181 pub(crate) fn precede(self, p: &mut Parser) -> Marker {
171 Marker::new(p.0.precede(self.0)) 182 Marker::new(p.0.precede(self.0))
172 } 183 }