From cb063946853e767e027edb5db2a97bbbaee25d25 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Tue, 13 Apr 2021 16:48:09 +0200 Subject: [PATCH] Remove some more use of SyntaxNode and SyntaxToken --- api/sixtyfps-rs/sixtyfps-macros/lib.rs | 17 ++++---- sixtyfps_compiler/parser.rs | 46 +++++++--------------- sixtyfps_compiler/parser_test_macro/lib.rs | 5 ++- 3 files changed, 25 insertions(+), 43 deletions(-) diff --git a/api/sixtyfps-rs/sixtyfps-macros/lib.rs b/api/sixtyfps-rs/sixtyfps-macros/lib.rs index 5dbc557d5..a982ad2c3 100644 --- a/api/sixtyfps-rs/sixtyfps-macros/lib.rs +++ b/api/sixtyfps-rs/sixtyfps-macros/lib.rs @@ -320,21 +320,18 @@ pub fn sixtyfps(stream: TokenStream) -> TokenStream { let mut tokens = vec![]; fill_token_vec(token_iter, &mut tokens); - let mut diag = BuildDiagnostics::default(); - let syntax_node = parser::parse_tokens(tokens.clone(), &mut diag); - if diag.has_error() { - return diag.report_macro_diagnostic(&tokens); - } - let source_file = if let Some(cargo_manifest) = std::env::var_os("CARGO_MANIFEST_DIR") { let mut path: std::path::PathBuf = cargo_manifest.into(); path.push("Cargo.toml"); - Some(diagnostics::SourceFileInner::from_path_only(path)) + diagnostics::SourceFileInner::from_path_only(path) } else { - None + diagnostics::SourceFileInner::from_path_only(Default::default()) }; - - let syntax_node = parser::SyntaxNodeWithSourceFile { node: syntax_node, source_file }; + let mut diag = BuildDiagnostics::default(); + let syntax_node = parser::parse_tokens(tokens.clone(), source_file.into(), &mut diag); + if diag.has_error() { + return diag.report_macro_diagnostic(&tokens); + } //println!("{:#?}", syntax_node); let mut compiler_config = diff --git a/sixtyfps_compiler/parser.rs b/sixtyfps_compiler/parser.rs index 4f56a5aa2..ed166ae91 100644 --- a/sixtyfps_compiler/parser.rs +++ b/sixtyfps_compiler/parser.rs @@ -42,7 +42,7 @@ pub trait SyntaxNodeVerify { const KIND: SyntaxKind; /// Asserts that the node is of the given SyntaxKind and that it has the expected children /// Panic if this is not the case - fn verify(node: SyntaxNode) { + fn verify(node: SyntaxNodeWithSourceFile) { assert_eq!(node.kind(), Self::KIND) } } @@ -202,7 +202,7 @@ macro_rules! declare_syntax { #[cfg(test)] impl SyntaxNodeVerify for $nodekind { const KIND: SyntaxKind = SyntaxKind::$nodekind; - fn verify(node: SyntaxNode) { + fn verify(node: SyntaxNodeWithSourceFile) { assert_eq!(node.kind(), Self::KIND); verify_node!(node, $children); } @@ -619,42 +619,17 @@ impl rowan::Language for Language { } } -type SyntaxNode = rowan::SyntaxNode; -type SyntaxToken = rowan::SyntaxToken; - -/// Helper functions to easily get the children of a given kind. -/// This traits is only supposed to be implemented on SyntaxNope -pub trait SyntaxNodeEx { - fn child_node(&self, kind: SyntaxKind) -> Option; - fn child_token(&self, kind: SyntaxKind) -> Option; - fn child_text(&self, kind: SyntaxKind) -> Option; -} - -impl SyntaxNodeEx for SyntaxNode { - fn child_node(&self, kind: SyntaxKind) -> Option { - self.children().find(|n| n.kind() == kind) - } - fn child_token(&self, kind: SyntaxKind) -> Option { - self.children_with_tokens().find(|n| n.kind() == kind).and_then(|x| x.into_token()) - } - fn child_text(&self, kind: SyntaxKind) -> Option { - self.children_with_tokens() - .find(|n| n.kind() == kind) - .and_then(|x| x.as_token().map(|x| x.text().to_string())) - } -} - #[derive(Debug, Clone, derive_more::Deref)] pub struct SyntaxNodeWithSourceFile { #[deref] - pub node: SyntaxNode, + pub node: rowan::SyntaxNode, pub source_file: Option, } #[derive(Debug, Clone, derive_more::Deref)] pub struct SyntaxTokenWithSourceFile { #[deref] - pub token: SyntaxToken, + pub token: rowan::SyntaxToken, pub source_file: Option, } @@ -845,7 +820,7 @@ pub fn parse( None }; document::parse_document(&mut p); - SyntaxNodeWithSourceFile { node: SyntaxNode::new_root(p.builder.finish()), source_file } + SyntaxNodeWithSourceFile { node: rowan::SyntaxNode::new_root(p.builder.finish()), source_file } } pub fn parse_file>( @@ -858,8 +833,15 @@ pub fn parse_file>( Some(parse(source, Some(path.as_ref()), build_diagnostics)) } -pub fn parse_tokens(tokens: Vec, diags: &mut BuildDiagnostics) -> SyntaxNode { +pub fn parse_tokens( + tokens: Vec, + source_file: SourceFile, + diags: &mut BuildDiagnostics, +) -> SyntaxNodeWithSourceFile { let mut p = DefaultParser::from_tokens(tokens, diags); document::parse_document(&mut p); - SyntaxNode::new_root(p.builder.finish()) + SyntaxNodeWithSourceFile { + node: rowan::SyntaxNode::new_root(p.builder.finish()), + source_file: Some(source_file), + } } diff --git a/sixtyfps_compiler/parser_test_macro/lib.rs b/sixtyfps_compiler/parser_test_macro/lib.rs index b35b98222..cd9fb0302 100644 --- a/sixtyfps_compiler/parser_test_macro/lib.rs +++ b/sixtyfps_compiler/parser_test_macro/lib.rs @@ -59,7 +59,10 @@ fn generate_test(fn_name: &str, doc: &str) -> String { None => String::new(), Some(kind) => { format!( - "syntax_nodes::{}::verify(rowan::SyntaxNode::new_root(p.builder.finish()));", + "syntax_nodes::{}::verify(SyntaxNodeWithSourceFile {{ + node: rowan::SyntaxNode::new_root(p.builder.finish()), + source_file: Default::default(), + }});", kind ) }