move all parsing related bits to a separate module

This commit is contained in:
Aleksey Kladov
2019-02-20 15:47:32 +03:00
parent 9d0cda4bc8
commit 5222b8aba3
31 changed files with 78 additions and 47 deletions

View File

@@ -18,13 +18,7 @@
pub mod algo; pub mod algo;
pub mod ast; pub mod ast;
mod lexer; mod parsing;
#[macro_use]
mod token_set;
mod grammar;
mod parser_api;
mod parser_impl;
mod reparsing;
mod string_lexing; mod string_lexing;
mod syntax_kinds; mod syntax_kinds;
/// Utilities for simple uses of the parser. /// Utilities for simple uses of the parser.
@@ -36,10 +30,10 @@ mod ptr;
pub use rowan::{SmolStr, TextRange, TextUnit}; pub use rowan::{SmolStr, TextRange, TextUnit};
pub use crate::{ pub use crate::{
ast::AstNode, ast::AstNode,
lexer::{tokenize, Token},
syntax_kinds::SyntaxKind, syntax_kinds::SyntaxKind,
syntax_node::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreeArc}, syntax_node::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreeArc},
ptr::{SyntaxNodePtr, AstPtr}, ptr::{SyntaxNodePtr, AstPtr},
parsing::{tokenize, Token},
}; };
use ra_text_edit::AtomTextEdit; use ra_text_edit::AtomTextEdit;
@@ -59,9 +53,7 @@ impl SourceFile {
} }
pub fn parse(text: &str) -> TreeArc<SourceFile> { pub fn parse(text: &str) -> TreeArc<SourceFile> {
let tokens = tokenize(&text); let (green, errors) = parsing::parse_text(text);
let (green, errors) =
parser_impl::parse_with(syntax_node::GreenBuilder::new(), text, &tokens, grammar::root);
SourceFile::new(green, errors) SourceFile::new(green, errors)
} }
@@ -70,7 +62,7 @@ impl SourceFile {
} }
pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<TreeArc<SourceFile>> { pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<TreeArc<SourceFile>> {
reparsing::incremental_reparse(self.syntax(), edit, self.errors()) parsing::incremental_reparse(self.syntax(), edit, self.errors())
.map(|(green_node, errors)| SourceFile::new(green_node, errors)) .map(|(green_node, errors)| SourceFile::new(green_node, errors))
} }

View File

@@ -0,0 +1,24 @@
#[macro_use]
mod token_set;
mod builder;
mod lexer;
mod parser_impl;
mod parser_api;
mod reparsing;
mod grammar;
use crate::{
parsing::builder::GreenBuilder,
syntax_node::{GreenNode, SyntaxError},
};
pub use self::lexer::{tokenize, Token};
pub(crate) use self::reparsing::incremental_reparse;
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
let tokens = tokenize(&text);
let (green, errors) =
parser_impl::parse_with(GreenBuilder::new(), text, &tokens, grammar::root);
(green, errors)
}

View File

@@ -1,5 +1,5 @@
use crate::{ use crate::{
parser_impl::Sink, parsing::parser_impl::Sink,
syntax_node::{GreenNode, RaTypes, SyntaxError}, syntax_node::{GreenNode, RaTypes, SyntaxError},
SmolStr, SyntaxKind, SmolStr, SyntaxKind,
}; };

View File

@@ -44,9 +44,11 @@ pub(crate) use self::{
}, },
}; };
use crate::{ use crate::{
parser_api::{CompletedMarker, Marker, Parser},
token_set::TokenSet,
SyntaxKind::{self, *}, SyntaxKind::{self, *},
parsing::{
token_set::TokenSet,
parser_api::{CompletedMarker, Marker, Parser}
},
}; };
pub(crate) fn root(p: &mut Parser) { pub(crate) fn root(p: &mut Parser) {

View File

@@ -1,4 +1,4 @@
use crate::lexer::ptr::Ptr; use crate::parsing::lexer::ptr::Ptr;
use crate::SyntaxKind::{self, *}; use crate::SyntaxKind::{self, *};

View File

@@ -1,5 +1,7 @@
use crate::lexer::classes::*; use crate::parsing::lexer::{
use crate::lexer::ptr::Ptr; ptr::Ptr,
classes::*,
};
use crate::SyntaxKind::{self, *}; use crate::SyntaxKind::{self, *};

View File

@@ -1,6 +1,7 @@
use crate::SyntaxKind::{self, *}; use crate::{
parsing::lexer::ptr::Ptr,
use crate::lexer::ptr::Ptr; SyntaxKind::{self, *},
};
pub(crate) fn is_string_literal_start(c: char, c1: Option<char>, c2: Option<char>) -> bool { pub(crate) fn is_string_literal_start(c: char, c1: Option<char>, c2: Option<char>) -> bool {
match (c, c1, c2) { match (c, c1, c2) {

View File

@@ -1,9 +1,11 @@
use drop_bomb::DropBomb; use drop_bomb::DropBomb;
use crate::{ use crate::{
parser_impl::ParserImpl,
token_set::TokenSet,
SyntaxKind::{self, ERROR}, SyntaxKind::{self, ERROR},
parsing::{
token_set::TokenSet,
parser_impl::ParserImpl
},
}; };
/// `Parser` struct provides the low-level API for /// `Parser` struct provides the low-level API for

View File

@@ -4,18 +4,16 @@ mod input;
use std::cell::Cell; use std::cell::Cell;
use crate::{ use crate::{
SmolStr,
syntax_node::syntax_error::{ParseError, SyntaxError},
parsing::{
lexer::Token, lexer::Token,
parser_api::Parser, parser_api::Parser,
parser_impl::{ parser_impl::{
event::{Event, EventProcessor}, event::{Event, EventProcessor},
input::{InputPosition, ParserInput}, input::{InputPosition, ParserInput},
}, },
SmolStr, }};
syntax_node::syntax_error::{
ParseError,
SyntaxError,
},
};
use crate::SyntaxKind::{self, EOF, TOMBSTONE}; use crate::SyntaxKind::{self, EOF, TOMBSTONE};

View File

@@ -7,9 +7,9 @@
//! tree builder: the parser produces a stream of events like //! tree builder: the parser produces a stream of events like
//! `start node`, `finish node`, and `FileBuilder` converts //! `start node`, `finish node`, and `FileBuilder` converts
//! this stream to a real tree. //! this stream to a real tree.
use std::mem;
use crate::{ use crate::{
lexer::Token,
parser_impl::Sink,
SmolStr, SmolStr,
SyntaxKind::{self, *}, SyntaxKind::{self, *},
TextRange, TextUnit, TextRange, TextUnit,
@@ -18,8 +18,11 @@ use crate::{
SyntaxError, SyntaxError,
SyntaxErrorKind, SyntaxErrorKind,
}, },
parsing::{
lexer::Token,
parser_impl::Sink,
},
}; };
use std::mem;
/// `Parser` produces a flat list of `Event`s. /// `Parser` produces a flat list of `Event`s.
/// They are converted to a tree-structure in /// They are converted to a tree-structure in

View File

@@ -1,4 +1,7 @@
use crate::{lexer::Token, SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit}; use crate::{
SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit,
parsing::lexer::Token,
};
use std::ops::{Add, AddAssign}; use std::ops::{Add, AddAssign};

View File

@@ -1,10 +1,16 @@
use crate::algo; use crate::{
use crate::grammar; SyntaxKind::*, TextRange, TextUnit,
use crate::lexer::{tokenize, Token}; algo,
use crate::parser_api::Parser; syntax_node::{GreenNode, SyntaxError, SyntaxNode},
use crate::parser_impl; parsing::{
use crate::syntax_node::{self, GreenNode, SyntaxError, SyntaxNode}; grammar,
use crate::{SyntaxKind::*, TextRange, TextUnit}; parser_impl,
builder::GreenBuilder,
parser_api::Parser,
lexer::{tokenize, Token},
}
};
use ra_text_edit::AtomTextEdit; use ra_text_edit::AtomTextEdit;
pub(crate) fn incremental_reparse( pub(crate) fn incremental_reparse(
@@ -56,7 +62,7 @@ fn reparse_block<'node>(
return None; return None;
} }
let (green, new_errors) = let (green, new_errors) =
parser_impl::parse_with(syntax_node::GreenBuilder::new(), &text, &tokens, reparser); parser_impl::parse_with(GreenBuilder::new(), &text, &tokens, reparser);
Some((node, green, new_errors)) Some((node, green, new_errors))
} }

View File

@@ -4,19 +4,19 @@ use crate::SyntaxKind;
pub(crate) struct TokenSet(u128); pub(crate) struct TokenSet(u128);
impl TokenSet { impl TokenSet {
pub const fn empty() -> TokenSet { pub(crate) const fn empty() -> TokenSet {
TokenSet(0) TokenSet(0)
} }
pub const fn singleton(kind: SyntaxKind) -> TokenSet { pub(crate) const fn singleton(kind: SyntaxKind) -> TokenSet {
TokenSet(mask(kind)) TokenSet(mask(kind))
} }
pub const fn union(self, other: TokenSet) -> TokenSet { pub(crate) const fn union(self, other: TokenSet) -> TokenSet {
TokenSet(self.0 | other.0) TokenSet(self.0 | other.0)
} }
pub fn contains(&self, kind: SyntaxKind) -> bool { pub(crate) fn contains(&self, kind: SyntaxKind) -> bool {
self.0 & mask(kind) != 0 self.0 & mask(kind) != 0
} }
} }

View File

@@ -1,4 +1,3 @@
mod builder;
pub mod syntax_error; pub mod syntax_error;
mod syntax_text; mod syntax_text;
@@ -8,7 +7,6 @@ use self::syntax_text::SyntaxText;
use crate::{SmolStr, SyntaxKind, TextRange}; use crate::{SmolStr, SyntaxKind, TextRange};
use rowan::{Types, TransparentNewType}; use rowan::{Types, TransparentNewType};
pub(crate) use self::builder::GreenBuilder;
pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location};
pub use rowan::WalkEvent; pub use rowan::WalkEvent;