reorganize

This commit is contained in:
Aleksey Kladov 2018-07-31 23:38:19 +03:00
parent d1400e95d7
commit 7912189ec3
23 changed files with 77 additions and 78 deletions

View file

@ -24,18 +24,15 @@
mod attributes;
mod expressions;
mod items;
mod params;
mod paths;
mod patterns;
mod params;
mod type_params;
mod type_args;
mod type_params;
mod types;
use {
parser::{
parser::{CompletedMarker, Parser},
token_set::TokenSet,
},
parser_api::{CompletedMarker, Parser, TokenSet},
SyntaxKind::{self, *},
};

View file

@ -68,4 +68,3 @@ fn self_param(p: &mut Parser) {
p.expect(COMMA);
}
}

View file

@ -83,7 +83,6 @@ pub(super) fn bounds_without_colon(p: &mut Parser) {
}
}
pub(super) fn where_clause(p: &mut Parser) {
if p.at(WHERE_KW) {
let m = p.start();

View file

@ -27,7 +27,11 @@ extern crate unicode_xid;
pub mod algo;
pub mod ast;
mod lexer;
mod parser;
#[macro_use]
mod parser_api;
mod grammar;
mod parser_impl;
mod syntax_kinds;
/// Utilities for simple uses of the parser.
pub mod utils;
@ -43,5 +47,5 @@ pub use {
pub fn parse(text: String) -> SyntaxNode {
let tokens = tokenize(&text);
parser::parse::<yellow::GreenBuilder>(text, &tokens)
parser_impl::parse::<yellow::GreenBuilder>(text, &tokens)
}

View file

@ -1,24 +0,0 @@
#[macro_use]
mod token_set;
mod event;
mod grammar;
mod input;
mod parser;
use {lexer::Token, parser::event::process};
pub(crate) use self::event::Sink;
/// Parse a sequence of tokens into the representative node tree
pub(crate) fn parse<S: Sink>(text: String, tokens: &[Token]) -> S::Tree {
let events = {
let input = input::ParserInput::new(&text, tokens);
let parser_impl = parser::imp::ParserImpl::new(&input);
let mut parser = parser::Parser(parser_impl);
grammar::file(&mut parser);
parser.0.into_events()
};
let mut sink = S::new(text);
process(&mut sink, tokens, events);
sink.finish()
}

View file

@ -1,24 +0,0 @@
use SyntaxKind;
pub(crate) struct TokenSet {
pub tokens: &'static [SyntaxKind],
}
impl TokenSet {
pub fn contains(&self, kind: SyntaxKind) -> bool {
self.tokens.contains(&kind)
}
}
#[macro_export]
macro_rules! token_set {
($($t:ident),*) => {
TokenSet {
tokens: &[$($t),*],
}
};
($($t:ident),* ,) => {
token_set!($($t),*)
};
}

View file

@ -1,7 +1,30 @@
use SyntaxKind::{self, ERROR};
use {
parser_impl::ParserImpl,
SyntaxKind::{self, ERROR},
};
pub(super) mod imp;
use self::imp::ParserImpl;
pub(crate) struct TokenSet {
pub tokens: &'static [SyntaxKind],
}
impl TokenSet {
pub fn contains(&self, kind: SyntaxKind) -> bool {
self.tokens.contains(&kind)
}
}
#[macro_export]
macro_rules! token_set {
($($t:ident),*) => {
TokenSet {
tokens: &[$($t),*],
}
};
($($t:ident),* ,) => {
token_set!($($t),*)
};
}
/// `Parser` struct provides the low-level API for
/// navigating through the stream of tokens and

View file

@ -9,22 +9,10 @@
//! this stream to a real tree.
use {
lexer::Token,
parser_impl::Sink,
SyntaxKind::{self, TOMBSTONE},
TextUnit,
};
pub(crate) trait Sink {
type Tree;
fn new(text: String) -> Self;
fn leaf(&mut self, kind: SyntaxKind, len: TextUnit);
fn start_internal(&mut self, kind: SyntaxKind);
fn finish_internal(&mut self);
fn error(&mut self, err: String);
fn finish(self) -> Self::Tree;
}
/// `Parser` produces a flat list of `Event`s.
/// They are converted to a tree-structure in
/// a separate pass, via `TreeBuilder`.

View file

@ -1,8 +1,45 @@
use parser::event::Event;
use parser::input::{InputPosition, ParserInput};
mod event;
mod input;
use {
grammar,
lexer::Token,
parser_api::Parser,
parser_impl::{
event::{process, Event},
input::{InputPosition, ParserInput},
},
TextUnit,
};
use SyntaxKind::{self, EOF, TOMBSTONE};
pub(crate) trait Sink {
type Tree;
fn new(text: String) -> Self;
fn leaf(&mut self, kind: SyntaxKind, len: TextUnit);
fn start_internal(&mut self, kind: SyntaxKind);
fn finish_internal(&mut self);
fn error(&mut self, err: String);
fn finish(self) -> Self::Tree;
}
/// Parse a sequence of tokens into the representative node tree
pub(crate) fn parse<S: Sink>(text: String, tokens: &[Token]) -> S::Tree {
let events = {
let input = input::ParserInput::new(&text, tokens);
let parser_impl = ParserImpl::new(&input);
let mut parser_api = Parser(parser_impl);
grammar::file(&mut parser_api);
parser_api.0.into_events()
};
let mut sink = S::new(text);
process(&mut sink, tokens, events);
sink.finish()
}
/// Implementation details of `Parser`, extracted
/// to a separate struct in order not to pollute
/// the public API of the `Parser`.

View file

@ -1,5 +1,5 @@
use {
parser::Sink,
parser_impl::Sink,
yellow::{GreenNode, GreenNodeBuilder, SyntaxError, SyntaxNode, SyntaxRoot},
SyntaxKind, TextRange, TextUnit,
};

View file

@ -18,7 +18,7 @@ use tools::{collect_tests, Test};
type Result<T> = ::std::result::Result<T, failure::Error>;
const GRAMMAR_DIR: &str = "./src/parser/grammar";
const GRAMMAR_DIR: &str = "./src/grammar";
const INLINE_TESTS_DIR: &str = "tests/data/parser/inline";
const GRAMMAR: &str = "./src/grammar.ron";
const SYNTAX_KINDS: &str = "./src/syntax_kinds/generated.rs";