mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-02 03:33:59 +00:00
move abstract traits to top
This commit is contained in:
parent
d2bce118ae
commit
2b5e336ce7
@ -8,7 +8,7 @@ mod grammar;
|
|||||||
mod reparsing;
|
mod reparsing;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
SyntaxError,
|
SyntaxError, SyntaxKind, SmolStr,
|
||||||
parsing::builder::GreenBuilder,
|
parsing::builder::GreenBuilder,
|
||||||
syntax_node::GreenNode,
|
syntax_node::GreenNode,
|
||||||
};
|
};
|
||||||
@ -23,3 +23,51 @@ pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
|
|||||||
parser_impl::parse_with(GreenBuilder::new(), text, &tokens, grammar::root);
|
parser_impl::parse_with(GreenBuilder::new(), text, &tokens, grammar::root);
|
||||||
(green, errors)
|
(green, errors)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `TreeSink` abstracts details of a particular syntax tree implementation.
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `TokenSource` abstracts the source of the tokens parser operates one.
|
||||||
|
///
|
||||||
|
/// Hopefully this will allow us to treat text and token trees in the same way!
|
||||||
|
trait TokenSource {
|
||||||
|
fn token_kind(&self, pos: TokenPos) -> SyntaxKind;
|
||||||
|
fn is_token_joint_to_next(&self, pos: TokenPos) -> bool;
|
||||||
|
fn is_keyword(&self, pos: TokenPos, kw: &str) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Default)]
|
||||||
|
pub(crate) struct TokenPos(pub u32);
|
||||||
|
|
||||||
|
impl std::ops::Add<u32> for TokenPos {
|
||||||
|
type Output = TokenPos;
|
||||||
|
|
||||||
|
fn add(self, rhs: u32) -> TokenPos {
|
||||||
|
TokenPos(self.0 + rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::AddAssign<u32> for TokenPos {
|
||||||
|
fn add_assign(&mut self, rhs: u32) {
|
||||||
|
self.0 += rhs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
parsing::parser_impl::TreeSink,
|
parsing::TreeSink,
|
||||||
syntax_node::{GreenNode, RaTypes},
|
syntax_node::{GreenNode, RaTypes},
|
||||||
SmolStr, SyntaxKind, SyntaxError,
|
SmolStr, SyntaxKind, SyntaxError,
|
||||||
};
|
};
|
||||||
|
@ -4,47 +4,17 @@ pub(crate) mod input;
|
|||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
SmolStr,
|
syntax_error::ParseError,
|
||||||
syntax_error::{ParseError, SyntaxError},
|
|
||||||
parsing::{
|
parsing::{
|
||||||
|
TreeSink, TokenSource, TokenPos,
|
||||||
lexer::Token,
|
lexer::Token,
|
||||||
parser_api::Parser,
|
parser_api::Parser,
|
||||||
parser_impl::{
|
parser_impl::event::{Event, EventProcessor},
|
||||||
event::{Event, EventProcessor},
|
|
||||||
input::InputPosition,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::SyntaxKind::{self, EOF, TOMBSTONE};
|
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
|
/// Parse a sequence of tokens into the representative node tree
|
||||||
pub(super) fn parse_with<S: TreeSink>(
|
pub(super) fn parse_with<S: TreeSink>(
|
||||||
sink: S,
|
sink: S,
|
||||||
@ -67,7 +37,7 @@ pub(super) fn parse_with<S: TreeSink>(
|
|||||||
/// the public API of the `Parser`.
|
/// the public API of the `Parser`.
|
||||||
pub(super) struct ParserImpl<'a> {
|
pub(super) struct ParserImpl<'a> {
|
||||||
token_source: &'a dyn TokenSource,
|
token_source: &'a dyn TokenSource,
|
||||||
pos: InputPosition,
|
pos: TokenPos,
|
||||||
events: Vec<Event>,
|
events: Vec<Event>,
|
||||||
steps: Cell<u32>,
|
steps: Cell<u32>,
|
||||||
}
|
}
|
||||||
@ -76,7 +46,7 @@ impl<'a> ParserImpl<'a> {
|
|||||||
fn new(token_source: &'a dyn TokenSource) -> ParserImpl<'a> {
|
fn new(token_source: &'a dyn TokenSource) -> ParserImpl<'a> {
|
||||||
ParserImpl {
|
ParserImpl {
|
||||||
token_source,
|
token_source,
|
||||||
pos: InputPosition::new(),
|
pos: TokenPos::default(),
|
||||||
events: Vec::new(),
|
events: Vec::new(),
|
||||||
steps: Cell::new(0),
|
steps: Cell::new(0),
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,21 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit,
|
SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit,
|
||||||
parsing::{
|
parsing::{
|
||||||
|
TokenPos,
|
||||||
parser_impl::TokenSource,
|
parser_impl::TokenSource,
|
||||||
lexer::Token,
|
lexer::Token,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::ops::{Add, AddAssign};
|
|
||||||
|
|
||||||
impl<'t> TokenSource for ParserInput<'t> {
|
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;
|
let idx = pos.0 as usize;
|
||||||
if !(idx < self.tokens.len()) {
|
if !(idx < self.tokens.len()) {
|
||||||
return EOF;
|
return EOF;
|
||||||
}
|
}
|
||||||
self.tokens[idx].kind
|
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_curr = pos.0 as usize;
|
||||||
let idx_next = pos.0 as usize;
|
let idx_next = pos.0 as usize;
|
||||||
if !(idx_next < self.tokens.len()) {
|
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]
|
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;
|
let idx = pos.0 as usize;
|
||||||
if !(idx < self.tokens.len()) {
|
if !(idx < self.tokens.len()) {
|
||||||
return false;
|
return false;
|
||||||
@ -72,26 +71,3 @@ impl<'t> ParserInput<'t> {
|
|||||||
ParserInput { text, start_offsets, tokens }
|
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<u32> for InputPosition {
|
|
||||||
type Output = InputPosition;
|
|
||||||
|
|
||||||
fn add(self, rhs: u32) -> InputPosition {
|
|
||||||
InputPosition(self.0 + rhs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AddAssign<u32> for InputPosition {
|
|
||||||
fn add_assign(&mut self, rhs: u32) {
|
|
||||||
self.0 += rhs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user