From 2b5e336ce7172914686b33c8ac1522911366fcf0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Feb 2019 22:19:12 +0300 Subject: move abstract traits to top --- crates/ra_syntax/src/parsing/builder.rs | 2 +- crates/ra_syntax/src/parsing/parser_impl.rs | 40 +++-------------------- crates/ra_syntax/src/parsing/parser_impl/input.rs | 32 +++--------------- 3 files changed, 10 insertions(+), 64 deletions(-) (limited to 'crates/ra_syntax/src/parsing') diff --git a/crates/ra_syntax/src/parsing/builder.rs b/crates/ra_syntax/src/parsing/builder.rs index 118f43b2c..a05e7f84b 100644 --- a/crates/ra_syntax/src/parsing/builder.rs +++ b/crates/ra_syntax/src/parsing/builder.rs @@ -1,5 +1,5 @@ use crate::{ - parsing::parser_impl::TreeSink, + parsing::TreeSink, syntax_node::{GreenNode, RaTypes}, SmolStr, SyntaxKind, SyntaxError, }; 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; use std::cell::Cell; use crate::{ - SmolStr, - syntax_error::{ParseError, SyntaxError}, + syntax_error::ParseError, parsing::{ + TreeSink, TokenSource, TokenPos, lexer::Token, parser_api::Parser, - parser_impl::{ - event::{Event, EventProcessor}, - input::InputPosition, - }, + parser_impl::event::{Event, EventProcessor}, }, }; use crate::SyntaxKind::{self, EOF, TOMBSTONE}; -pub(super) trait TreeSink { - type Tree; - - /// Adds new leaf to the current branch. - fn leaf(&mut self, kind: SyntaxKind, text: SmolStr); - - /// 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; -} - -pub(super) trait TokenSource { - fn token_kind(&self, pos: InputPosition) -> SyntaxKind; - fn is_token_joint_to_next(&self, pos: InputPosition) -> bool; - fn is_keyword(&self, pos: InputPosition, kw: &str) -> bool; -} - /// Parse a sequence of tokens into the representative node tree pub(super) fn parse_with( sink: S, @@ -67,7 +37,7 @@ pub(super) fn parse_with( /// the public API of the `Parser`. pub(super) struct ParserImpl<'a> { token_source: &'a dyn TokenSource, - pos: InputPosition, + pos: TokenPos, events: Vec, steps: Cell, } @@ -76,7 +46,7 @@ impl<'a> ParserImpl<'a> { fn new(token_source: &'a dyn TokenSource) -> ParserImpl<'a> { ParserImpl { token_source, - pos: InputPosition::new(), + pos: TokenPos::default(), events: Vec::new(), steps: Cell::new(0), } diff --git a/crates/ra_syntax/src/parsing/parser_impl/input.rs b/crates/ra_syntax/src/parsing/parser_impl/input.rs index 8ebbd3825..e9735e526 100644 --- a/crates/ra_syntax/src/parsing/parser_impl/input.rs +++ b/crates/ra_syntax/src/parsing/parser_impl/input.rs @@ -1,22 +1,21 @@ use crate::{ SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit, parsing::{ + TokenPos, parser_impl::TokenSource, lexer::Token, }, }; -use std::ops::{Add, AddAssign}; - impl<'t> TokenSource for ParserInput<'t> { - fn token_kind(&self, pos: InputPosition) -> SyntaxKind { + fn token_kind(&self, pos: TokenPos) -> SyntaxKind { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { return EOF; } self.tokens[idx].kind } - fn is_token_joint_to_next(&self, pos: InputPosition) -> bool { + fn is_token_joint_to_next(&self, pos: TokenPos) -> bool { let idx_curr = pos.0 as usize; let idx_next = pos.0 as usize; if !(idx_next < self.tokens.len()) { @@ -24,7 +23,7 @@ impl<'t> TokenSource for ParserInput<'t> { } self.start_offsets[idx_curr] + self.tokens[idx_curr].len == self.start_offsets[idx_next] } - fn is_keyword(&self, pos: InputPosition, kw: &str) -> bool { + fn is_keyword(&self, pos: TokenPos, kw: &str) -> bool { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { return false; @@ -72,26 +71,3 @@ impl<'t> ParserInput<'t> { ParserInput { text, start_offsets, tokens } } } - -#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] -pub(crate) struct InputPosition(u32); - -impl InputPosition { - pub fn new() -> Self { - InputPosition(0) - } -} - -impl Add for InputPosition { - type Output = InputPosition; - - fn add(self, rhs: u32) -> InputPosition { - InputPosition(self.0 + rhs) - } -} - -impl AddAssign for InputPosition { - fn add_assign(&mut self, rhs: u32) { - self.0 += rhs - } -} -- cgit v1.2.3