From ea7b569e1b133b6c19ef60c9cb2b2fd6b79847da Mon Sep 17 00:00:00 2001 From: csmoe Date: Mon, 31 Dec 2018 20:53:43 +0800 Subject: docing parser methods --- crates/ra_syntax/src/parser_api.rs | 4 ++-- crates/ra_syntax/src/parser_impl.rs | 15 +++++++++++++-- crates/ra_syntax/src/parser_impl/event.rs | 12 ++++++------ crates/ra_syntax/src/yellow/builder.rs | 4 ++-- 4 files changed, 23 insertions(+), 12 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/parser_api.rs b/crates/ra_syntax/src/parser_api.rs index 02421def1..0f740963d 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> { Marker::new(self.0.start()) } - /// Advances the parser by one token. + /// Advances the parser by one token unconditionally. pub(crate) fn bump(&mut self) { self.0.bump(); } @@ -91,7 +91,7 @@ impl<'t> Parser<'t> { self.0.error(message.into()) } - /// Consume the next token if it is `kind`. + /// Consume the next token if `kind` matches. pub(crate) fn eat(&mut self, kind: SyntaxKind) -> bool { if !self.at(kind) { return false; diff --git a/crates/ra_syntax/src/parser_impl.rs b/crates/ra_syntax/src/parser_impl.rs index cb6e370ac..d4032a6d9 100644 --- a/crates/ra_syntax/src/parser_impl.rs +++ b/crates/ra_syntax/src/parser_impl.rs @@ -22,10 +22,21 @@ use crate::SyntaxKind::{self, EOF, TOMBSTONE}; pub(crate) trait Sink { type Tree; + /// Adds new leaf to the current branch. fn leaf(&mut self, kind: SyntaxKind, text: SmolStr); - fn start_internal(&mut self, kind: SyntaxKind); - fn finish_internal(&mut self); + + /// Start new branch and make it current. + fn start_branch(&mut self, kind: SyntaxKind); + + /// Finish current branch and restore previous + /// branch as current. + fn finish_branch(&mut self); + fn error(&mut self, error: SyntaxError); + + /// Complete tree building. Make sure that + /// `start_branch` and `finish_branch` calls + /// are paired! fn finish(self) -> Self::Tree; } diff --git a/crates/ra_syntax/src/parser_impl/event.rs b/crates/ra_syntax/src/parser_impl/event.rs index 3d8b062d5..d6299b5e3 100644 --- a/crates/ra_syntax/src/parser_impl/event.rs +++ b/crates/ra_syntax/src/parser_impl/event.rs @@ -154,7 +154,7 @@ impl<'a, S: Sink> EventProcessor<'a, S> { self.finish(last); } Event::Token { kind, n_raw_tokens } => { - self.eat_ws(); + self.eat_trivias(); let n_raw_tokens = n_raw_tokens as usize; let len = self.tokens[self.token_pos..self.token_pos + n_raw_tokens] .iter() @@ -173,7 +173,7 @@ impl<'a, S: Sink> EventProcessor<'a, S> { fn start(&mut self, kind: SyntaxKind) { if kind == SOURCE_FILE { - self.sink.start_internal(kind); + self.sink.start_branch(kind); return; } let n_trivias = self.tokens[self.token_pos..] @@ -194,18 +194,18 @@ impl<'a, S: Sink> EventProcessor<'a, S> { n_attached_trivias(kind, leading_trivias) }; self.eat_n_trivias(n_trivias - n_attached_trivias); - self.sink.start_internal(kind); + self.sink.start_branch(kind); self.eat_n_trivias(n_attached_trivias); } fn finish(&mut self, last: bool) { if last { - self.eat_ws() + self.eat_trivias() } - self.sink.finish_internal(); + self.sink.finish_branch(); } - fn eat_ws(&mut self) { + fn eat_trivias(&mut self) { while let Some(&token) = self.tokens.get(self.token_pos) { if !token.kind.is_trivia() { break; diff --git a/crates/ra_syntax/src/yellow/builder.rs b/crates/ra_syntax/src/yellow/builder.rs index 9fcebfb93..37ae6329b 100644 --- a/crates/ra_syntax/src/yellow/builder.rs +++ b/crates/ra_syntax/src/yellow/builder.rs @@ -26,11 +26,11 @@ impl Sink for GreenBuilder { self.inner.leaf(kind, text); } - fn start_internal(&mut self, kind: SyntaxKind) { + fn start_branch(&mut self, kind: SyntaxKind) { self.inner.start_internal(kind) } - fn finish_internal(&mut self) { + fn finish_branch(&mut self) { self.inner.finish_internal(); } -- cgit v1.2.3