move parser to a separate crate

This commit is contained in:
Aleksey Kladov 2019-02-21 13:27:45 +03:00
parent 18b0c509f7
commit d334b5a1db
24 changed files with 91 additions and 18 deletions

7
Cargo.lock generated
View file

@ -1071,6 +1071,13 @@ dependencies = [
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ra_parser"
version = "0.1.0"
dependencies = [
"drop_bomb 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ra_project_model"
version = "0.1.0"

View file

@ -0,0 +1,9 @@
[package]
edition = "2018"
name = "ra_parser"
version = "0.1.0"
authors = ["rust-analyzer developers"]
publish = false
[dependencies]
drop_bomb = "0.1.4"

View file

@ -10,8 +10,8 @@
use std::mem;
use crate::{
ParseError, TreeSink,
SyntaxKind::{self, *},
parsing::{ParseError, TreeSink},
};
/// `Parser` produces a flat list of `Event`s.
@ -84,7 +84,7 @@ impl Event {
}
/// Generate the syntax tree with the control of events.
pub(super) fn process(sink: &mut impl TreeSink, mut events: Vec<Event>) {
pub(super) fn process(sink: &mut dyn TreeSink, mut events: Vec<Event>) {
let mut forward_parents = Vec::new();
for i in 0..events.len() {

View file

@ -38,20 +38,18 @@ mod types;
use crate::{
SyntaxKind::{self, *},
parsing::{
token_set::TokenSet,
parser::{CompletedMarker, Marker, Parser}
},
TokenSet,
parser::{CompletedMarker, Marker, Parser},
};
pub(super) fn root(p: &mut Parser) {
pub(crate) fn root(p: &mut Parser) {
let m = p.start();
p.eat(SHEBANG);
items::mod_contents(p, false);
m.complete(p, SOURCE_FILE);
}
pub(super) fn reparser(
pub(crate) fn reparser(
node: SyntaxKind,
first_child: Option<SyntaxKind>,
parent: Option<SyntaxKind>,

View file

@ -0,0 +1,64 @@
#[macro_use]
mod token_set;
mod syntax_kind;
mod event;
mod parser;
mod grammar;
pub(crate) use token_set::TokenSet;
pub use syntax_kind::SyntaxKind;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ParseError(pub String);
/// `TreeSink` abstracts details of a particular syntax tree implementation.
pub trait TreeSink {
/// Adds new leaf to the current branch.
fn leaf(&mut self, kind: SyntaxKind, n_tokens: u8);
/// Start new branch and make it current.
fn start_branch(&mut self, kind: SyntaxKind, root: bool);
/// Finish current branch and restore previous
/// branch as current.
fn finish_branch(&mut self, root: bool);
fn error(&mut self, error: ParseError);
}
/// `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!
pub trait TokenSource {
fn token_kind(&self, pos: usize) -> SyntaxKind;
fn is_token_joint_to_next(&self, pos: usize) -> bool;
fn is_keyword(&self, pos: usize, kw: &str) -> bool;
}
pub fn parse(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
let mut p = parser::Parser::new(token_source);
grammar::root(&mut p);
let events = p.finish();
event::process(tree_sink, events);
}
pub struct Reparser(fn(&mut parser::Parser));
impl Reparser {
pub fn for_node(
node: SyntaxKind,
first_child: Option<SyntaxKind>,
parent: Option<SyntaxKind>,
) -> Option<Reparser> {
grammar::reparser(node, first_child, parent).map(Reparser)
}
}
pub fn reparse(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink, reparser: Reparser) {
let Reparser(r) = reparser;
let mut p = parser::Parser::new(token_source);
r(&mut p);
let events = p.finish();
event::process(tree_sink, events);
}

View file

@ -4,11 +4,8 @@ use drop_bomb::DropBomb;
use crate::{
SyntaxKind::{self, ERROR, EOF, TOMBSTONE},
parsing::{
TokenSource, ParseError,
token_set::TokenSet,
event::Event,
},
TokenSource, ParseError, TokenSet,
event::Event,
};
/// `Parser` struct provides the low-level API for

View file

@ -2,8 +2,6 @@ mod generated;
use std::fmt;
use crate::SyntaxKind::*;
pub use self::generated::SyntaxKind;
impl fmt::Debug for SyntaxKind {
@ -20,7 +18,7 @@ pub(crate) struct SyntaxInfo {
impl SyntaxKind {
pub fn is_trivia(self) -> bool {
match self {
WHITESPACE | COMMENT => true,
SyntaxKind::WHITESPACE | SyntaxKind::COMMENT => true,
_ => false,
}
}

View file

@ -568,7 +568,7 @@ impl SyntaxKind {
EOF => &SyntaxInfo { name: "EOF" },
}
}
pub(crate) fn from_keyword(ident: &str) -> Option<SyntaxKind> {
pub fn from_keyword(ident: &str) -> Option<SyntaxKind> {
let kw = match ident {
"use" => USE_KW,
"fn" => FN_KW,
@ -610,7 +610,7 @@ impl SyntaxKind {
Some(kw)
}
pub(crate) fn from_char(c: char) -> Option<SyntaxKind> {
pub fn from_char(c: char) -> Option<SyntaxKind> {
let tok = match c {
';' => SEMI,
',' => COMMA,