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
35 pub allocator:Allocator,
38
39 pub errors:Vec<String>,
41
42 pub file_path:String,
44}
45
46#[derive(Debug, Clone)]
48pub struct ParserConfig {
49 pub target:String,
51
52 pub jsx:bool,
54
55 pub decorators:bool,
57
58 pub typescript:bool,
60}
61
62impl Default for ParserConfig {
63 fn default() -> Self { Self { target:"es2024".to_string(), jsx:false, decorators:true, typescript:true } }
64}
65
66impl ParserConfig {
67 pub fn new(target:String, jsx:bool, decorators:bool, typescript:bool) -> Self {
69 Self { target, jsx, decorators, typescript }
70 }
71}
72
73static PARSE_COUNT:AtomicUsize = AtomicUsize::new(0);
83
84#[tracing::instrument(skip(source_text, config))]
85pub fn parse(source_text:&str, file_path:&str, config:&ParserConfig) -> Result<ParseResult, Vec<String>> {
86 let parse_id = PARSE_COUNT.fetch_add(1, Ordering::SeqCst);
87
88 info!("[Parser #{parse_id}] Starting parse of: {}", file_path);
89
90 trace!("[Parser #{parse_id}] Source text length: {} bytes", source_text.len());
91
92 let allocator = Allocator::default();
93
94 trace!("[Parser #{parse_id}] Allocator created at: {:p}", &allocator);
95
96 let source_type = determine_source_type(file_path, config);
98
99 info!("[Parser #{parse_id}] Parsing {} as {:?}", file_path, source_type);
100
101 let parse_start = std::time::Instant::now();
102
103 let parser_return:ParserReturn = Parser::new(&allocator, source_text, source_type).parse();
105
106 info!("[Parser #{parse_id}] Parse completed in {:?}", parse_start.elapsed());
107
108 let errors:Vec<String> = parser_return.errors.iter().map(|e| e.to_string()).collect();
109
110 if !errors.is_empty() {
111 warn!("[Parser #{parse_id}] Parsing errors for {}: {:?}", file_path, errors);
112
113 return Err(errors);
114 }
115
116 let program = unsafe {
133 std::mem::transmute::<oxc_ast::ast::Program<'_>, oxc_ast::ast::Program<'static>>(parser_return.program)
134 };
135
136 debug!("[Parser #{parse_id}] Program transmute complete at: {:p}", &program);
137
138 trace!(
139 "[Parser #{parse_id}] AST body pointer: {:p}, len: {}",
140 program.body.as_ptr(),
141 program.body.len()
142 );
143
144 info!(
145 "[Parser #{parse_id}] SUCCESS: ParseResult<'static> created (allocator={:p})",
146 &allocator
147 );
148
149 Ok(ParseResult { program, allocator, errors:vec![], file_path:file_path.to_string() })
150}
151
152fn determine_source_type(file_path:&str, _config:&ParserConfig) -> SourceType {
154 let path = std::path::Path::new(file_path);
155
156 let extension = path.extension().and_then(|e| e.to_str()).unwrap_or("");
157
158 match extension {
159 "ts" | "mts" => SourceType::ts(),
160
161 "tsx" => SourceType::tsx(),
162
163 "mjs" => SourceType::mjs(),
164
165 "cjs" => SourceType::cjs(),
166
167 "js" => SourceType::unambiguous(),
168
169 "jsx" => SourceType::jsx(),
170
171 _ => SourceType::unambiguous(),
172 }
173}