From 7912189ec304b28c4df0030b5282cf3d21074154 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 31 Jul 2018 23:38:19 +0300 Subject: [PATCH] reorganize --- src/{parser => }/grammar/attributes.rs | 0 src/{parser => }/grammar/expressions.rs | 0 src/{parser => }/grammar/items/consts.rs | 0 src/{parser => }/grammar/items/mod.rs | 0 src/{parser => }/grammar/items/structs.rs | 0 src/{parser => }/grammar/items/traits.rs | 0 src/{parser => }/grammar/items/use_item.rs | 0 src/{parser => }/grammar/mod.rs | 9 ++-- src/{parser => }/grammar/params.rs | 1 - src/{parser => }/grammar/paths.rs | 0 src/{parser => }/grammar/patterns.rs | 0 src/{parser => }/grammar/type_args.rs | 0 src/{parser => }/grammar/type_params.rs | 1 - src/{parser => }/grammar/types.rs | 0 src/lib.rs | 8 +++- src/parser/mod.rs | 24 ----------- src/parser/token_set.rs | 24 ----------- src/{parser/parser/mod.rs => parser_api.rs} | 29 +++++++++++-- src/{parser => parser_impl}/event.rs | 14 +------ src/{parser => parser_impl}/input.rs | 0 .../parser/imp.rs => parser_impl/mod.rs} | 41 ++++++++++++++++++- src/yellow/builder.rs | 2 +- tools/src/main.rs | 2 +- 23 files changed, 77 insertions(+), 78 deletions(-) rename src/{parser => }/grammar/attributes.rs (100%) rename src/{parser => }/grammar/expressions.rs (100%) rename src/{parser => }/grammar/items/consts.rs (100%) rename src/{parser => }/grammar/items/mod.rs (100%) rename src/{parser => }/grammar/items/structs.rs (100%) rename src/{parser => }/grammar/items/traits.rs (100%) rename src/{parser => }/grammar/items/use_item.rs (100%) rename src/{parser => }/grammar/mod.rs (97%) rename src/{parser => }/grammar/params.rs (99%) rename src/{parser => }/grammar/paths.rs (100%) rename src/{parser => }/grammar/patterns.rs (100%) rename src/{parser => }/grammar/type_args.rs (100%) rename src/{parser => }/grammar/type_params.rs (99%) rename src/{parser => }/grammar/types.rs (100%) delete mode 100644 src/parser/mod.rs delete mode 100644 src/parser/token_set.rs rename src/{parser/parser/mod.rs => parser_api.rs} (90%) rename src/{parser => parser_impl}/event.rs (94%) rename src/{parser => parser_impl}/input.rs (100%) rename src/{parser/parser/imp.rs => parser_impl/mod.rs} (73%) diff --git a/src/parser/grammar/attributes.rs b/src/grammar/attributes.rs similarity index 100% rename from src/parser/grammar/attributes.rs rename to src/grammar/attributes.rs diff --git a/src/parser/grammar/expressions.rs b/src/grammar/expressions.rs similarity index 100% rename from src/parser/grammar/expressions.rs rename to src/grammar/expressions.rs diff --git a/src/parser/grammar/items/consts.rs b/src/grammar/items/consts.rs similarity index 100% rename from src/parser/grammar/items/consts.rs rename to src/grammar/items/consts.rs diff --git a/src/parser/grammar/items/mod.rs b/src/grammar/items/mod.rs similarity index 100% rename from src/parser/grammar/items/mod.rs rename to src/grammar/items/mod.rs diff --git a/src/parser/grammar/items/structs.rs b/src/grammar/items/structs.rs similarity index 100% rename from src/parser/grammar/items/structs.rs rename to src/grammar/items/structs.rs diff --git a/src/parser/grammar/items/traits.rs b/src/grammar/items/traits.rs similarity index 100% rename from src/parser/grammar/items/traits.rs rename to src/grammar/items/traits.rs diff --git a/src/parser/grammar/items/use_item.rs b/src/grammar/items/use_item.rs similarity index 100% rename from src/parser/grammar/items/use_item.rs rename to src/grammar/items/use_item.rs diff --git a/src/parser/grammar/mod.rs b/src/grammar/mod.rs similarity index 97% rename from src/parser/grammar/mod.rs rename to src/grammar/mod.rs index d4f9b80cfd..b558da477d 100644 --- a/src/parser/grammar/mod.rs +++ b/src/grammar/mod.rs @@ -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, *}, }; diff --git a/src/parser/grammar/params.rs b/src/grammar/params.rs similarity index 99% rename from src/parser/grammar/params.rs rename to src/grammar/params.rs index efa8823943..be985c80f9 100644 --- a/src/parser/grammar/params.rs +++ b/src/grammar/params.rs @@ -68,4 +68,3 @@ fn self_param(p: &mut Parser) { p.expect(COMMA); } } - diff --git a/src/parser/grammar/paths.rs b/src/grammar/paths.rs similarity index 100% rename from src/parser/grammar/paths.rs rename to src/grammar/paths.rs diff --git a/src/parser/grammar/patterns.rs b/src/grammar/patterns.rs similarity index 100% rename from src/parser/grammar/patterns.rs rename to src/grammar/patterns.rs diff --git a/src/parser/grammar/type_args.rs b/src/grammar/type_args.rs similarity index 100% rename from src/parser/grammar/type_args.rs rename to src/grammar/type_args.rs diff --git a/src/parser/grammar/type_params.rs b/src/grammar/type_params.rs similarity index 99% rename from src/parser/grammar/type_params.rs rename to src/grammar/type_params.rs index b31bf52b62..1227482ad4 100644 --- a/src/parser/grammar/type_params.rs +++ b/src/grammar/type_params.rs @@ -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(); diff --git a/src/parser/grammar/types.rs b/src/grammar/types.rs similarity index 100% rename from src/parser/grammar/types.rs rename to src/grammar/types.rs diff --git a/src/lib.rs b/src/lib.rs index a72d9e3cbc..8f25de9a44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::(text, &tokens) + parser_impl::parse::(text, &tokens) } diff --git a/src/parser/mod.rs b/src/parser/mod.rs deleted file mode 100644 index 8631baa2e9..0000000000 --- a/src/parser/mod.rs +++ /dev/null @@ -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(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() -} diff --git a/src/parser/token_set.rs b/src/parser/token_set.rs deleted file mode 100644 index a800f200db..0000000000 --- a/src/parser/token_set.rs +++ /dev/null @@ -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),*) - }; -} diff --git a/src/parser/parser/mod.rs b/src/parser_api.rs similarity index 90% rename from src/parser/parser/mod.rs rename to src/parser_api.rs index 0930ff9e47..5a0da32c95 100644 --- a/src/parser/parser/mod.rs +++ b/src/parser_api.rs @@ -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 diff --git a/src/parser/event.rs b/src/parser_impl/event.rs similarity index 94% rename from src/parser/event.rs rename to src/parser_impl/event.rs index 0086d32eae..eb5d0a4be4 100644 --- a/src/parser/event.rs +++ b/src/parser_impl/event.rs @@ -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`. diff --git a/src/parser/input.rs b/src/parser_impl/input.rs similarity index 100% rename from src/parser/input.rs rename to src/parser_impl/input.rs diff --git a/src/parser/parser/imp.rs b/src/parser_impl/mod.rs similarity index 73% rename from src/parser/parser/imp.rs rename to src/parser_impl/mod.rs index c653e35245..b58094be38 100644 --- a/src/parser/parser/imp.rs +++ b/src/parser_impl/mod.rs @@ -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(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`. diff --git a/src/yellow/builder.rs b/src/yellow/builder.rs index 0f7ca45d77..b68ba789eb 100644 --- a/src/yellow/builder.rs +++ b/src/yellow/builder.rs @@ -1,5 +1,5 @@ use { - parser::Sink, + parser_impl::Sink, yellow::{GreenNode, GreenNodeBuilder, SyntaxError, SyntaxNode, SyntaxRoot}, SyntaxKind, TextRange, TextUnit, }; diff --git a/tools/src/main.rs b/tools/src/main.rs index 7c6c7a1aa6..714158f554 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -18,7 +18,7 @@ use tools::{collect_tests, Test}; type Result = ::std::result::Result; -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";