26 lines
837 B
Rust
26 lines
837 B
Rust
use anyhow::{anyhow, Context, Result};
|
|
use oxc_allocator::Allocator;
|
|
use oxc_ast::ast::Program;
|
|
use oxc_parser::Parser;
|
|
use oxc_span::SourceType;
|
|
use std::fs;
|
|
use std::path::Path;
|
|
|
|
pub fn parse_file<'a>(allocator: &'a Allocator, path: &Path) -> Result<Program<'a>> {
|
|
let source_text = fs::read_to_string(path)
|
|
.with_context(|| format!("Failed to read file: {:?}", path))?;
|
|
|
|
let source_text_ptr = allocator.alloc_str(&source_text);
|
|
let source_type = SourceType::from_path(path).unwrap_or_default();
|
|
let parser_ret = Parser::new(allocator, source_text_ptr, source_type).parse();
|
|
|
|
if !parser_ret.errors.is_empty() {
|
|
for error in parser_ret.errors {
|
|
eprintln!("{:?}", error);
|
|
}
|
|
return Err(anyhow!("Failed to parse module: {:?}", path));
|
|
}
|
|
|
|
Ok(parser_ret.program)
|
|
}
|