use Parse in mbe

This commit is contained in:
Aleksey Kladov
2019-07-18 23:19:04 +03:00
parent a6224f3620
commit df33e7685b
7 changed files with 64 additions and 63 deletions

View File

@@ -2,7 +2,7 @@ use std::sync::Arc;
use parking_lot::Mutex; use parking_lot::Mutex;
use ra_db::{salsa, SourceDatabase}; use ra_db::{salsa, SourceDatabase};
use ra_syntax::{ast, SmolStr, SyntaxNode, TreeArc}; use ra_syntax::{ast, Parse, SmolStr, SyntaxNode, TreeArc};
use crate::{ use crate::{
adt::{EnumData, StructData}, adt::{EnumData, StructData},
@@ -69,7 +69,7 @@ pub trait AstDatabase: InternDatabase {
fn parse_or_expand(&self, file_id: HirFileId) -> Option<TreeArc<SyntaxNode>>; fn parse_or_expand(&self, file_id: HirFileId) -> Option<TreeArc<SyntaxNode>>;
#[salsa::invoke(crate::ids::HirFileId::parse_macro_query)] #[salsa::invoke(crate::ids::HirFileId::parse_macro_query)]
fn parse_macro(&self, macro_file: ids::MacroFile) -> Option<TreeArc<SyntaxNode>>; fn parse_macro(&self, macro_file: ids::MacroFile) -> Option<Parse<SyntaxNode>>;
#[salsa::invoke(crate::ids::macro_def_query)] #[salsa::invoke(crate::ids::macro_def_query)]
fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>; fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>;

View File

@@ -6,7 +6,7 @@ use std::{
use mbe::MacroRules; use mbe::MacroRules;
use ra_db::{salsa, FileId}; use ra_db::{salsa, FileId};
use ra_prof::profile; use ra_prof::profile;
use ra_syntax::{ast, AstNode, SyntaxNode, TreeArc}; use ra_syntax::{ast, AstNode, Parse, SyntaxNode, TreeArc};
use crate::{AstDatabase, AstId, DefDatabase, FileAstId, InternDatabase, Module, Source}; use crate::{AstDatabase, AstId, DefDatabase, FileAstId, InternDatabase, Module, Source};
@@ -61,14 +61,16 @@ impl HirFileId {
) -> Option<TreeArc<SyntaxNode>> { ) -> Option<TreeArc<SyntaxNode>> {
match file_id.0 { match file_id.0 {
HirFileIdRepr::File(file_id) => Some(db.parse(file_id).tree().syntax().to_owned()), HirFileIdRepr::File(file_id) => Some(db.parse(file_id).tree().syntax().to_owned()),
HirFileIdRepr::Macro(macro_file) => db.parse_macro(macro_file), HirFileIdRepr::Macro(macro_file) => {
db.parse_macro(macro_file).map(|it| it.tree().to_owned())
}
} }
} }
pub(crate) fn parse_macro_query( pub(crate) fn parse_macro_query(
db: &impl AstDatabase, db: &impl AstDatabase,
macro_file: MacroFile, macro_file: MacroFile,
) -> Option<TreeArc<SyntaxNode>> { ) -> Option<Parse<SyntaxNode>> {
let _p = profile("parse_macro_query"); let _p = profile("parse_macro_query");
let macro_call_id = macro_file.macro_call_id; let macro_call_id = macro_file.macro_call_id;
let tt = db let tt = db
@@ -85,10 +87,8 @@ impl HirFileId {
}) })
.ok()?; .ok()?;
match macro_file.macro_file_kind { match macro_file.macro_file_kind {
MacroFileKind::Items => Some(mbe::token_tree_to_ast_item_list(&tt).syntax().to_owned()), MacroFileKind::Items => Some(Parse::to_syntax(mbe::token_tree_to_ast_item_list(&tt))),
MacroFileKind::Expr => { MacroFileKind::Expr => mbe::token_tree_to_expr(&tt).ok().map(Parse::to_syntax),
mbe::token_tree_to_expr(&tt).ok().map(|it| it.syntax().to_owned())
}
} }
} }
} }

View File

@@ -9,7 +9,7 @@ use ra_db::{
FileTextQuery, SourceRootId, FileTextQuery, SourceRootId,
}; };
use ra_prof::{memory_usage, Bytes}; use ra_prof::{memory_usage, Bytes};
use ra_syntax::{ast, AstNode, Parse, SyntaxNode, TreeArc}; use ra_syntax::{ast, AstNode, Parse, SyntaxNode};
use crate::{ use crate::{
db::RootDatabase, db::RootDatabase,
@@ -96,15 +96,15 @@ impl FromIterator<TableEntry<FileId, Parse<ast::SourceFile>>> for SyntaxTreeStat
} }
} }
impl FromIterator<TableEntry<MacroFile, Option<TreeArc<SyntaxNode>>>> for SyntaxTreeStats { impl FromIterator<TableEntry<MacroFile, Option<Parse<SyntaxNode>>>> for SyntaxTreeStats {
fn from_iter<T>(iter: T) -> SyntaxTreeStats fn from_iter<T>(iter: T) -> SyntaxTreeStats
where where
T: IntoIterator<Item = TableEntry<MacroFile, Option<TreeArc<SyntaxNode>>>>, T: IntoIterator<Item = TableEntry<MacroFile, Option<Parse<SyntaxNode>>>>,
{ {
let mut res = SyntaxTreeStats::default(); let mut res = SyntaxTreeStats::default();
for entry in iter { for entry in iter {
res.total += 1; res.total += 1;
if let Some(tree) = entry.value.and_then(|it| it) { if let Some(tree) = entry.value.and_then(|it| it).map(|it| it.tree().to_owned()) {
res.retained += 1; res.retained += 1;
res.retained_size += tree.memory_size_of_subtree(); res.retained_size += tree.memory_size_of_subtree();
} }

View File

@@ -2,8 +2,8 @@ use crate::subtree_source::SubtreeTokenSource;
use crate::ExpandError; use crate::ExpandError;
use ra_parser::{ParseError, TreeSink}; use ra_parser::{ParseError, TreeSink};
use ra_syntax::{ use ra_syntax::{
ast, AstNode, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxTreeBuilder, ast, AstNode, Parse, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxNode,
TextRange, TextUnit, TreeArc, T, SyntaxTreeBuilder, TextRange, TextUnit, T,
}; };
use tt::buffer::{Cursor, TokenBuffer}; use tt::buffer::{Cursor, TokenBuffer};
@@ -45,7 +45,7 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
// //
// //
fn token_tree_to_syntax_node<F>(tt: &tt::Subtree, f: F) -> Result<TreeArc<SyntaxNode>, ExpandError> fn token_tree_to_syntax_node<F>(tt: &tt::Subtree, f: F) -> Result<Parse<SyntaxNode>, ExpandError>
where where
F: Fn(&mut dyn ra_parser::TokenSource, &mut dyn ra_parser::TreeSink), F: Fn(&mut dyn ra_parser::TokenSource, &mut dyn ra_parser::TreeSink),
{ {
@@ -58,50 +58,44 @@ where
return Err(ExpandError::ConversionError); return Err(ExpandError::ConversionError);
} }
//FIXME: would be cool to report errors //FIXME: would be cool to report errors
let (tree, _errors) = tree_sink.inner.finish(); let parse = tree_sink.inner.finish();
Ok(tree) Ok(parse)
} }
/// Parses the token tree (result of macro expansion) to an expression /// Parses the token tree (result of macro expansion) to an expression
pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<TreeArc<ast::Expr>, ExpandError> { pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<Parse<ast::Expr>, ExpandError> {
let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_expr)?; let parse = token_tree_to_syntax_node(tt, ra_parser::parse_expr)?;
ast::Expr::cast(&syntax) parse.cast().ok_or_else(|| crate::ExpandError::ConversionError)
.map(|m| m.to_owned())
.ok_or_else(|| crate::ExpandError::ConversionError)
} }
/// Parses the token tree (result of macro expansion) to a Pattern /// Parses the token tree (result of macro expansion) to a Pattern
pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<TreeArc<ast::Pat>, ExpandError> { pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<Parse<ast::Pat>, ExpandError> {
let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_pat)?; let parse = token_tree_to_syntax_node(tt, ra_parser::parse_pat)?;
ast::Pat::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) parse.cast().ok_or_else(|| crate::ExpandError::ConversionError)
} }
/// Parses the token tree (result of macro expansion) to a Type /// Parses the token tree (result of macro expansion) to a Type
pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<TreeArc<ast::TypeRef>, ExpandError> { pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<Parse<ast::TypeRef>, ExpandError> {
let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_ty)?; let parse = token_tree_to_syntax_node(tt, ra_parser::parse_ty)?;
ast::TypeRef::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) parse.cast().ok_or_else(|| crate::ExpandError::ConversionError)
} }
/// Parses the token tree (result of macro expansion) as a sequence of stmts /// Parses the token tree (result of macro expansion) as a sequence of stmts
pub fn token_tree_to_macro_stmts( pub fn token_tree_to_macro_stmts(tt: &tt::Subtree) -> Result<Parse<ast::MacroStmts>, ExpandError> {
tt: &tt::Subtree, let parse = token_tree_to_syntax_node(tt, ra_parser::parse_macro_stmts)?;
) -> Result<TreeArc<ast::MacroStmts>, ExpandError> { parse.cast().ok_or_else(|| crate::ExpandError::ConversionError)
let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_macro_stmts)?;
ast::MacroStmts::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
} }
/// Parses the token tree (result of macro expansion) as a sequence of items /// Parses the token tree (result of macro expansion) as a sequence of items
pub fn token_tree_to_macro_items( pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> Result<Parse<ast::MacroItems>, ExpandError> {
tt: &tt::Subtree, let parse = token_tree_to_syntax_node(tt, ra_parser::parse_macro_items)?;
) -> Result<TreeArc<ast::MacroItems>, ExpandError> { parse.cast().ok_or_else(|| crate::ExpandError::ConversionError)
let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_macro_items)?;
ast::MacroItems::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
} }
/// Parses the token tree (result of macro expansion) as a sequence of items /// Parses the token tree (result of macro expansion) as a sequence of items
pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> TreeArc<ast::SourceFile> { pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> Parse<ast::SourceFile> {
let syntax = token_tree_to_syntax_node(tt, ra_parser::parse).unwrap(); let parse = token_tree_to_syntax_node(tt, ra_parser::parse).unwrap();
ast::SourceFile::cast(&syntax).unwrap().to_owned() parse.cast().unwrap()
} }
impl TokenMap { impl TokenMap {

View File

@@ -72,7 +72,7 @@ pub(crate) fn expand_to_items(
invocation: &str, invocation: &str,
) -> ra_syntax::TreeArc<ast::MacroItems> { ) -> ra_syntax::TreeArc<ast::MacroItems> {
let expanded = expand(rules, invocation); let expanded = expand(rules, invocation);
token_tree_to_macro_items(&expanded).unwrap() token_tree_to_macro_items(&expanded).unwrap().tree().to_owned()
} }
#[allow(unused)] #[allow(unused)]
@@ -81,7 +81,7 @@ pub(crate) fn expand_to_stmts(
invocation: &str, invocation: &str,
) -> ra_syntax::TreeArc<ast::MacroStmts> { ) -> ra_syntax::TreeArc<ast::MacroStmts> {
let expanded = expand(rules, invocation); let expanded = expand(rules, invocation);
token_tree_to_macro_stmts(&expanded).unwrap() token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned()
} }
pub(crate) fn expand_to_expr( pub(crate) fn expand_to_expr(
@@ -89,7 +89,7 @@ pub(crate) fn expand_to_expr(
invocation: &str, invocation: &str,
) -> ra_syntax::TreeArc<ast::Expr> { ) -> ra_syntax::TreeArc<ast::Expr> {
let expanded = expand(rules, invocation); let expanded = expand(rules, invocation);
token_tree_to_expr(&expanded).unwrap() token_tree_to_expr(&expanded).unwrap().tree().to_owned()
} }
pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree { pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree {
@@ -164,22 +164,22 @@ pub(crate) fn assert_expansion(
let (expanded_tree, expected_tree) = match kind { let (expanded_tree, expected_tree) = match kind {
MacroKind::Items => { MacroKind::Items => {
let expanded_tree = token_tree_to_macro_items(&expanded); let expanded_tree = token_tree_to_macro_items(&expanded).unwrap().tree().to_owned();
let expected_tree = token_tree_to_macro_items(&expected); let expected_tree = token_tree_to_macro_items(&expected).unwrap().tree().to_owned();
( (
debug_dump_ignore_spaces(expanded_tree.unwrap().syntax()).trim().to_string(), debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(),
debug_dump_ignore_spaces(expected_tree.unwrap().syntax()).trim().to_string(), debug_dump_ignore_spaces(expected_tree.syntax()).trim().to_string(),
) )
} }
MacroKind::Stmts => { MacroKind::Stmts => {
let expanded_tree = token_tree_to_macro_stmts(&expanded); let expanded_tree = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned();
let expected_tree = token_tree_to_macro_stmts(&expected); let expected_tree = token_tree_to_macro_stmts(&expected).unwrap().tree().to_owned();
( (
debug_dump_ignore_spaces(expanded_tree.unwrap().syntax()).trim().to_string(), debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(),
debug_dump_ignore_spaces(expected_tree.unwrap().syntax()).trim().to_string(), debug_dump_ignore_spaces(expected_tree.syntax()).trim().to_string(),
) )
} }
}; };
@@ -419,9 +419,9 @@ fn test_expand_to_item_list() {
", ",
); );
let expansion = expand(&rules, "structs!(Foo, Bar);"); let expansion = expand(&rules, "structs!(Foo, Bar);");
let tree = token_tree_to_macro_items(&expansion); let tree = token_tree_to_macro_items(&expansion).unwrap().tree().to_owned();
assert_eq!( assert_eq!(
tree.unwrap().syntax().debug_dump().trim(), tree.syntax().debug_dump().trim(),
r#" r#"
MACRO_ITEMS@[0; 40) MACRO_ITEMS@[0; 40)
STRUCT_DEF@[0; 20) STRUCT_DEF@[0; 20)
@@ -537,10 +537,10 @@ fn test_tt_to_stmts() {
); );
let expanded = expand(&rules, "foo!{}"); let expanded = expand(&rules, "foo!{}");
let stmts = token_tree_to_macro_stmts(&expanded); let stmts = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned();
assert_eq!( assert_eq!(
stmts.unwrap().syntax().debug_dump().trim(), stmts.syntax().debug_dump().trim(),
r#"MACRO_STMTS@[0; 15) r#"MACRO_STMTS@[0; 15)
LET_STMT@[0; 7) LET_STMT@[0; 7)
LET_KW@[0; 3) "let" LET_KW@[0; 3) "let"

View File

@@ -35,7 +35,7 @@ use std::{fmt::Write, sync::Arc};
use ra_text_edit::AtomTextEdit; use ra_text_edit::AtomTextEdit;
use crate::syntax_node::{GreenNode, SyntaxNodeWrapper}; use crate::syntax_node::GreenNode;
pub use crate::{ pub use crate::{
ast::{AstNode, AstToken}, ast::{AstNode, AstToken},
@@ -43,8 +43,8 @@ pub use crate::{
ptr::{AstPtr, SyntaxNodePtr}, ptr::{AstPtr, SyntaxNodePtr},
syntax_error::{Location, SyntaxError, SyntaxErrorKind}, syntax_error::{Location, SyntaxError, SyntaxErrorKind},
syntax_node::{ syntax_node::{
Direction, InsertPosition, SyntaxElement, SyntaxNode, SyntaxToken, SyntaxTreeBuilder, Direction, InsertPosition, SyntaxElement, SyntaxNode, SyntaxNodeWrapper, SyntaxToken,
TreeArc, WalkEvent, SyntaxTreeBuilder, TreeArc, WalkEvent,
}, },
syntax_text::SyntaxText, syntax_text::SyntaxText,
}; };
@@ -91,6 +91,12 @@ impl<T: SyntaxNodeWrapper> Parse<T> {
} }
} }
impl<T: AstNode> Parse<T> {
pub fn to_syntax(this: Self) -> Parse<SyntaxNode> {
Parse { tree: this.tree().syntax().to_owned(), errors: this.errors }
}
}
impl Parse<SourceFile> { impl Parse<SourceFile> {
pub fn debug_dump(&self) -> String { pub fn debug_dump(&self) -> String {
let mut buf = self.tree.syntax().debug_dump(); let mut buf = self.tree.syntax().debug_dump();

View File

@@ -18,7 +18,8 @@ use rowan::{GreenNodeBuilder, TransparentNewType};
use crate::{ use crate::{
syntax_error::{SyntaxError, SyntaxErrorKind}, syntax_error::{SyntaxError, SyntaxErrorKind},
AstNode, SmolStr, SourceFile, SyntaxKind, SyntaxNodePtr, SyntaxText, TextRange, TextUnit, AstNode, Parse, SmolStr, SourceFile, SyntaxKind, SyntaxNodePtr, SyntaxText, TextRange,
TextUnit,
}; };
pub use rowan::WalkEvent; pub use rowan::WalkEvent;
@@ -594,13 +595,13 @@ impl SyntaxTreeBuilder {
(green, self.errors) (green, self.errors)
} }
pub fn finish(self) -> (TreeArc<SyntaxNode>, Vec<SyntaxError>) { pub fn finish(self) -> Parse<SyntaxNode> {
let (green, errors) = self.finish_raw(); let (green, errors) = self.finish_raw();
let node = SyntaxNode::new(green); let node = SyntaxNode::new(green);
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
crate::validation::validate_block_structure(&node); crate::validation::validate_block_structure(&node);
} }
(node, errors) Parse::new(node, errors)
} }
pub fn token(&mut self, kind: SyntaxKind, text: SmolStr) { pub fn token(&mut self, kind: SyntaxKind, text: SmolStr) {