aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parsing/parser_impl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/parsing/parser_impl.rs')
-rw-r--r--crates/ra_syntax/src/parsing/parser_impl.rs40
1 files changed, 5 insertions, 35 deletions
diff --git a/crates/ra_syntax/src/parsing/parser_impl.rs b/crates/ra_syntax/src/parsing/parser_impl.rs
index 96de32fc2..89439e074 100644
--- a/crates/ra_syntax/src/parsing/parser_impl.rs
+++ b/crates/ra_syntax/src/parsing/parser_impl.rs
@@ -4,47 +4,17 @@ pub(crate) mod input;
4use std::cell::Cell; 4use std::cell::Cell;
5 5
6use crate::{ 6use crate::{
7 SmolStr, 7 syntax_error::ParseError,
8 syntax_error::{ParseError, SyntaxError},
9 parsing::{ 8 parsing::{
9 TreeSink, TokenSource, TokenPos,
10 lexer::Token, 10 lexer::Token,
11 parser_api::Parser, 11 parser_api::Parser,
12 parser_impl::{ 12 parser_impl::event::{Event, EventProcessor},
13 event::{Event, EventProcessor},
14 input::InputPosition,
15 },
16 }, 13 },
17}; 14};
18 15
19use crate::SyntaxKind::{self, EOF, TOMBSTONE}; 16use crate::SyntaxKind::{self, EOF, TOMBSTONE};
20 17
21pub(super) trait TreeSink {
22 type Tree;
23
24 /// Adds new leaf to the current branch.
25 fn leaf(&mut self, kind: SyntaxKind, text: SmolStr);
26
27 /// Start new branch and make it current.
28 fn start_branch(&mut self, kind: SyntaxKind);
29
30 /// Finish current branch and restore previous
31 /// branch as current.
32 fn finish_branch(&mut self);
33
34 fn error(&mut self, error: SyntaxError);
35
36 /// Complete tree building. Make sure that
37 /// `start_branch` and `finish_branch` calls
38 /// are paired!
39 fn finish(self) -> Self::Tree;
40}
41
42pub(super) trait TokenSource {
43 fn token_kind(&self, pos: InputPosition) -> SyntaxKind;
44 fn is_token_joint_to_next(&self, pos: InputPosition) -> bool;
45 fn is_keyword(&self, pos: InputPosition, kw: &str) -> bool;
46}
47
48/// Parse a sequence of tokens into the representative node tree 18/// Parse a sequence of tokens into the representative node tree
49pub(super) fn parse_with<S: TreeSink>( 19pub(super) fn parse_with<S: TreeSink>(
50 sink: S, 20 sink: S,
@@ -67,7 +37,7 @@ pub(super) fn parse_with<S: TreeSink>(
67/// the public API of the `Parser`. 37/// the public API of the `Parser`.
68pub(super) struct ParserImpl<'a> { 38pub(super) struct ParserImpl<'a> {
69 token_source: &'a dyn TokenSource, 39 token_source: &'a dyn TokenSource,
70 pos: InputPosition, 40 pos: TokenPos,
71 events: Vec<Event>, 41 events: Vec<Event>,
72 steps: Cell<u32>, 42 steps: Cell<u32>,
73} 43}
@@ -76,7 +46,7 @@ impl<'a> ParserImpl<'a> {
76 fn new(token_source: &'a dyn TokenSource) -> ParserImpl<'a> { 46 fn new(token_source: &'a dyn TokenSource) -> ParserImpl<'a> {
77 ParserImpl { 47 ParserImpl {
78 token_source, 48 token_source,
79 pos: InputPosition::new(), 49 pos: TokenPos::default(),
80 events: Vec::new(), 50 events: Vec::new(),
81 steps: Cell::new(0), 51 steps: Cell::new(0),
82 } 52 }