mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
Refactor
This commit is contained in:
parent
a44428fc79
commit
87cd57d56a
3 changed files with 19 additions and 17 deletions
|
@ -50,16 +50,25 @@ pub use {
|
|||
yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError},
|
||||
};
|
||||
|
||||
use yellow::{GreenNode, SyntaxRoot};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct File {
|
||||
root: SyntaxNode
|
||||
}
|
||||
|
||||
impl File {
|
||||
pub fn parse(text: &str) -> Self {
|
||||
let root = ::parse(text);
|
||||
fn new(root: GreenNode, errors: Vec<SyntaxError>) -> File {
|
||||
let root = SyntaxRoot::new(root, errors);
|
||||
let root = SyntaxNode::new_owned(root);
|
||||
validate_block_structure(root.borrowed());
|
||||
File { root }
|
||||
}
|
||||
pub fn parse(text: &str) -> Self {
|
||||
let tokens = tokenize(&text);
|
||||
let (root, errors) = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
|
||||
File::new(root, errors)
|
||||
}
|
||||
pub fn ast(&self) -> ast::Root {
|
||||
ast::Root::cast(self.syntax()).unwrap()
|
||||
}
|
||||
|
@ -69,14 +78,6 @@ impl File {
|
|||
pub fn errors(&self) -> Vec<SyntaxError> {
|
||||
self.syntax().root.syntax_root().errors.clone()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn parse(text: &str) -> SyntaxNode {
|
||||
let tokens = tokenize(&text);
|
||||
let res = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
|
||||
validate_block_structure(res.borrowed());
|
||||
res
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use {
|
||||
parser_impl::Sink,
|
||||
yellow::{GreenNode, SyntaxError, SyntaxNode, SyntaxRoot},
|
||||
yellow::{GreenNode, SyntaxError},
|
||||
SyntaxKind, TextRange, TextUnit,
|
||||
};
|
||||
|
||||
|
@ -13,7 +13,7 @@ pub(crate) struct GreenBuilder<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Sink<'a> for GreenBuilder<'a> {
|
||||
type Tree = SyntaxNode;
|
||||
type Tree = (GreenNode, Vec<SyntaxError>);
|
||||
|
||||
fn new(text: &'a str) -> Self {
|
||||
GreenBuilder {
|
||||
|
@ -56,10 +56,9 @@ impl<'a> Sink<'a> for GreenBuilder<'a> {
|
|||
})
|
||||
}
|
||||
|
||||
fn finish(mut self) -> SyntaxNode {
|
||||
fn finish(mut self) -> (GreenNode, Vec<SyntaxError>) {
|
||||
assert_eq!(self.children.len(), 1);
|
||||
let root = self.children.pop().unwrap();
|
||||
let root = SyntaxRoot::new(root, self.errors);
|
||||
SyntaxNode::new_owned(root)
|
||||
(root, self.errors)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ use std::{
|
|||
fmt::Write,
|
||||
};
|
||||
|
||||
use libsyntax2::File;
|
||||
|
||||
#[test]
|
||||
fn lexer_tests() {
|
||||
dir_tests(&["lexer"], |text| {
|
||||
|
@ -20,8 +22,8 @@ fn lexer_tests() {
|
|||
#[test]
|
||||
fn parser_tests() {
|
||||
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
|
||||
let file = libsyntax2::parse(text);
|
||||
libsyntax2::utils::dump_tree(file.borrowed())
|
||||
let file = File::parse(text);
|
||||
libsyntax2::utils::dump_tree(file.syntax())
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue