1use std::sync::atomic::{AtomicUsize, Ordering};
10
11use oxc_allocator::Allocator;
12use oxc_parser::{Parser, ParserReturn};
13use oxc_span::SourceType;
14use tracing::{debug, info, trace, warn};
15
16pub struct ParseResult {
32 pub program:oxc_ast::ast::Program<'static>,
34 pub allocator:Allocator,
37 pub errors:Vec<String>,
39 #[allow(dead_code)]
41 pub file_path:String,
42}
43
44#[derive(Debug, Clone)]
46pub struct ParserConfig {
47 pub target:String,
49 pub jsx:bool,
51 pub decorators:bool,
53 pub typescript:bool,
55}
56
57impl Default for ParserConfig {
58 fn default() -> Self { Self { target:"es2024".to_string(), jsx:false, decorators:true, typescript:true } }
59}
60
61impl ParserConfig {
62 pub fn new(target:String, jsx:bool, decorators:bool, typescript:bool) -> Self {
64 Self { target, jsx, decorators, typescript }
65 }
66}
67
68static PARSE_COUNT:AtomicUsize = AtomicUsize::new(0);
78
79#[tracing::instrument(skip(source_text, config))]
80pub fn parse(source_text:&str, file_path:&str, config:&ParserConfig) -> Result<ParseResult, Vec<String>> {
81 let parse_id = PARSE_COUNT.fetch_add(1, Ordering::SeqCst);
82
83 info!("[Parser #{parse_id}] Starting parse of: {}", file_path);
84 trace!("[Parser #{parse_id}] Source text length: {} bytes", source_text.len());
85
86 let allocator = Allocator::default();
87 trace!("[Parser #{parse_id}] Allocator created at: {:p}", &allocator);
88
89 let source_type = determine_source_type(file_path, config);
91 info!("[Parser #{parse_id}] Parsing {} as {:?}", file_path, source_type);
92
93 let parse_start = std::time::Instant::now();
94 let parser_return:ParserReturn = Parser::new(&allocator, source_text, source_type).parse();
96 info!("[Parser #{parse_id}] Parse completed in {:?}", parse_start.elapsed());
97
98 let errors:Vec<String> = parser_return.errors.iter().map(|e| e.to_string()).collect();
99
100 if !errors.is_empty() {
101 warn!("[Parser #{parse_id}] Parsing errors for {}: {:?}", file_path, errors);
102 return Err(errors);
103 }
104
105 let program = unsafe {
122 std::mem::transmute::<oxc_ast::ast::Program<'_>, oxc_ast::ast::Program<'static>>(parser_return.program)
123 };
124
125 debug!("[Parser #{parse_id}] Program transmute complete at: {:p}", &program);
126 trace!(
127 "[Parser #{parse_id}] AST body pointer: {:p}, len: {}",
128 program.body.as_ptr(),
129 program.body.len()
130 );
131
132 info!(
133 "[Parser #{parse_id}] SUCCESS: ParseResult<'static> created (allocator={:p})",
134 &allocator
135 );
136
137 Ok(ParseResult { program, allocator, errors:vec![], file_path:file_path.to_string() })
138}
139
140fn determine_source_type(file_path:&str, _config:&ParserConfig) -> SourceType {
142 let path = std::path::Path::new(file_path);
143 let extension = path.extension().and_then(|e| e.to_str()).unwrap_or("");
144
145 match extension {
146 "ts" | "mts" => SourceType::ts(),
147 "tsx" => SourceType::tsx(),
148 "mjs" => SourceType::mjs(),
149 "cjs" => SourceType::cjs(),
150 "js" => SourceType::unambiguous(),
151 "jsx" => SourceType::jsx(),
152 _ => SourceType::unambiguous(),
153 }
154}