mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
Realized we can get the source type from the AST. The next PR will introduce `unambiguous` to `SourceType` and directly set `Program::source_type` to either `script` or `module`.
55 lines
1.9 KiB
Rust
55 lines
1.9 KiB
Rust
#![allow(clippy::print_stdout)]
|
|
use std::{env, path::Path, sync::Arc};
|
|
|
|
use itertools::Itertools;
|
|
use oxc_allocator::Allocator;
|
|
use oxc_parser::Parser;
|
|
use oxc_semantic::SemanticBuilder;
|
|
use oxc_span::SourceType;
|
|
|
|
// Instruction:
|
|
// create a `test.js`,
|
|
// run `cargo run -p oxc_semantic --example simple`
|
|
// or `just watch "run -p oxc_semantic --example simple"`
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
|
|
let path = Path::new(&name);
|
|
let source_text = Arc::new(std::fs::read_to_string(path)?);
|
|
let source_type = SourceType::from_path(path).unwrap();
|
|
// Memory arena where Semantic and Parser allocate objects
|
|
let allocator = Allocator::default();
|
|
|
|
// Parse the source text into an AST
|
|
let parser_ret = Parser::new(&allocator, &source_text, source_type).parse();
|
|
if !parser_ret.errors.is_empty() {
|
|
let error_message: String = parser_ret
|
|
.errors
|
|
.into_iter()
|
|
.map(|error| format!("{:?}", error.with_source_code(Arc::clone(&source_text))))
|
|
.join("\n");
|
|
println!("Parsing failed:\n\n{error_message}",);
|
|
return Ok(());
|
|
}
|
|
|
|
let program = allocator.alloc(parser_ret.program);
|
|
|
|
let semantic = SemanticBuilder::new(&source_text)
|
|
.build_module_record(path, program)
|
|
// Enable additional syntax checks not performed by the parser
|
|
.with_check_syntax_error(true)
|
|
// Inform Semantic about comments found while parsing
|
|
.with_trivias(parser_ret.trivias)
|
|
.build(program);
|
|
|
|
if !semantic.errors.is_empty() {
|
|
let error_message: String = semantic
|
|
.errors
|
|
.into_iter()
|
|
.map(|error| format!("{:?}", error.with_source_code(Arc::clone(&source_text))))
|
|
.join("\n");
|
|
println!("Semantic analysis failed:\n\n{error_message}",);
|
|
}
|
|
|
|
Ok(())
|
|
}
|