Skip to content

Commit

Permalink
Use parse_virtual instead of creating virtual file (#2826)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #2357
  • Loading branch information
ddoktorski authored Jan 7, 2025
1 parent 47f9f00 commit 1058351
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions crates/snforge-scarb-plugin/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
use crate::attributes::{AttributeInfo, ErrorExt};
use cairo_lang_diagnostics::DiagnosticsBuilder;
use cairo_lang_filesystem::ids::{FileKind, FileLongId, VirtualFile};
use cairo_lang_macro::Diagnostic;
use cairo_lang_parser::{parser::Parser, utils::SimpleParserDatabase};
use cairo_lang_parser::utils::SimpleParserDatabase;
use cairo_lang_syntax::node::ast::SyntaxFile;
use cairo_lang_syntax::node::{
ast::{FunctionWithBody, ModuleItem, OptionArgListParenthesized},
db::SyntaxGroup,
helpers::QueryAttrs,
TypedSyntaxNode,
};
use cairo_lang_utils::Upcast;
use indoc::formatdoc;
use std::sync::Arc;

pub fn parse<T: AttributeInfo>(
code: &str,
) -> Result<(SimpleParserDatabase, FunctionWithBody), Diagnostic> {
let simple_db = SimpleParserDatabase::default();
let code = Arc::new(code.to_string());
let (parsed_node, _diagnostics) = simple_db.parse_virtual_with_diagnostics(code);

let db: &dyn SyntaxGroup = simple_db.upcast();
// TODO(#2357): Use `db.parse_virtual` here instead of creating the virtual file manually
let virtual_file = db.intern_file(FileLongId::Virtual(VirtualFile {
parent: None,
name: "test_function".into(),
content: Arc::from(code.as_str()),
code_mappings: Default::default(),
kind: FileKind::Module,
}));
let mut diagnostics = DiagnosticsBuilder::default();
let elements = Parser::parse_file(&simple_db, &mut diagnostics, virtual_file, code.as_str())
let elements = SyntaxFile::from_syntax_node(db, parsed_node)
.items(db)
.elements(db);

Expand Down

0 comments on commit 1058351

Please sign in to comment.