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},
|
yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use yellow::{GreenNode, SyntaxRoot};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct File {
|
pub struct File {
|
||||||
root: SyntaxNode
|
root: SyntaxNode
|
||||||
}
|
}
|
||||||
|
|
||||||
impl File {
|
impl File {
|
||||||
pub fn parse(text: &str) -> Self {
|
fn new(root: GreenNode, errors: Vec<SyntaxError>) -> File {
|
||||||
let root = ::parse(text);
|
let root = SyntaxRoot::new(root, errors);
|
||||||
|
let root = SyntaxNode::new_owned(root);
|
||||||
|
validate_block_structure(root.borrowed());
|
||||||
File { root }
|
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 {
|
pub fn ast(&self) -> ast::Root {
|
||||||
ast::Root::cast(self.syntax()).unwrap()
|
ast::Root::cast(self.syntax()).unwrap()
|
||||||
}
|
}
|
||||||
|
@ -69,14 +78,6 @@ impl File {
|
||||||
pub fn errors(&self) -> Vec<SyntaxError> {
|
pub fn errors(&self) -> Vec<SyntaxError> {
|
||||||
self.syntax().root.syntax_root().errors.clone()
|
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))]
|
#[cfg(not(debug_assertions))]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use {
|
use {
|
||||||
parser_impl::Sink,
|
parser_impl::Sink,
|
||||||
yellow::{GreenNode, SyntaxError, SyntaxNode, SyntaxRoot},
|
yellow::{GreenNode, SyntaxError},
|
||||||
SyntaxKind, TextRange, TextUnit,
|
SyntaxKind, TextRange, TextUnit,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ pub(crate) struct GreenBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Sink<'a> for GreenBuilder<'a> {
|
impl<'a> Sink<'a> for GreenBuilder<'a> {
|
||||||
type Tree = SyntaxNode;
|
type Tree = (GreenNode, Vec<SyntaxError>);
|
||||||
|
|
||||||
fn new(text: &'a str) -> Self {
|
fn new(text: &'a str) -> Self {
|
||||||
GreenBuilder {
|
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);
|
assert_eq!(self.children.len(), 1);
|
||||||
let root = self.children.pop().unwrap();
|
let root = self.children.pop().unwrap();
|
||||||
let root = SyntaxRoot::new(root, self.errors);
|
(root, self.errors)
|
||||||
SyntaxNode::new_owned(root)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ use std::{
|
||||||
fmt::Write,
|
fmt::Write,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use libsyntax2::File;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn lexer_tests() {
|
fn lexer_tests() {
|
||||||
dir_tests(&["lexer"], |text| {
|
dir_tests(&["lexer"], |text| {
|
||||||
|
@ -20,8 +22,8 @@ fn lexer_tests() {
|
||||||
#[test]
|
#[test]
|
||||||
fn parser_tests() {
|
fn parser_tests() {
|
||||||
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
|
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
|
||||||
let file = libsyntax2::parse(text);
|
let file = File::parse(text);
|
||||||
libsyntax2::utils::dump_tree(file.borrowed())
|
libsyntax2::utils::dump_tree(file.syntax())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue