Skip to main content

Rest/Fn/OXC/
Parser.rs

1//! OXC TypeScript Parser module
2//!
3//! This module provides TypeScript source code parsing using the OXC parser.
4//!
5//! DIAGNOSTIC LOGGING:
6//! - All operations log with tracing::debug! for memory lifecycle tracking
7//! - Use RUST_LOG=debug to see detailed parser operations
8
9use 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
16/// Result of parsing a TypeScript source file
17///
18/// CRITICAL FIX FOR SEGFAULT:
19/// The Program has 'static lifetime via safe transmute. ParseResult owns BOTH
20/// the Program AND the Allocator, ensuring they're dropped together.
21///
22/// The ORIGINAL segfault occurred when code did:
23///   let mut program = parse_result.program;  // Moves Program out!
24///   Transformer::transform(&parse_result.allocator, ...);
25///   // parse_result dropped here -> allocator freed -> program is dangling!
26///
27/// THE FIX in Compiler.rs - NEVER move Program out:
28///   let program = &mut parse_result.program;  // Borrow mutably
29///   Transformer::transform(&parse_result.allocator, program, ...);
30///   // parse_result stays in scope -> allocator stays alive -> no segfault!
31pub struct ParseResult {
32	/// The parsed AST program with 'static lifetime (safe transmute)
33	pub program:oxc_ast::ast::Program<'static>,
34	/// The allocator used for the AST - owns the memory for the Program
35	/// CRITICAL: Must not be dropped separately from program
36	pub allocator:Allocator,
37	/// Any parsing errors encountered
38	pub errors:Vec<String>,
39	/// File path for debugging
40	#[allow(dead_code)]
41	pub file_path:String,
42}
43
44/// Parser configuration options
45#[derive(Debug, Clone)]
46pub struct ParserConfig {
47	/// Target ECMAScript version (e.g., "es2024")
48	pub target:String,
49	/// Whether to support JSX syntax
50	pub jsx:bool,
51	/// Whether to support decorators
52	pub decorators:bool,
53	/// Whether to support TypeScript
54	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	/// Create a new parser configuration
63	pub fn new(target:String, jsx:bool, decorators:bool, typescript:bool) -> Self {
64		Self { target, jsx, decorators, typescript }
65	}
66}
67
68/// Parse TypeScript source code into an AST
69///
70/// # Arguments
71/// * `source_text` - The TypeScript source code to parse
72/// * `file_path` - The path to the source file (used for determining file type)
73/// * `config` - Parser configuration options
74///
75/// # Returns
76/// A ParseResult containing the parsed AST and any errors
77static 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	// Determine source type based on file extension and config
90	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	// Parse the source code
95	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	// SAFETY: Transmute program lifetime to 'static.
106	// This is SAFE because:
107	// 1. ParseResult owns BOTH the Program AND the Allocator
108	// 2. The Program's memory comes from the Allocator
109	// 3. Both are dropped together when ParseResult is dropped
110	// 4. The Program NEVER outlives the Allocator
111	//
112	// CRITICAL BUG FIX: The original segfault happened because code did:
113	//   let mut program = parse_result.program;  // Moves Program out!
114	//   Transformer::transform(&parse_result.allocator, ...);
115	//   // parse_result dropped -> allocator freed -> program is dangling!
116	//
117	// THE FIX in Compiler.rs:
118	//   let program = &mut parse_result.program;  // Borrow mutably, don't move!
119	//   Transformer::transform(&parse_result.allocator, program, ...);
120	//   // parse_result stays in scope -> allocator stays alive
121	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
140/// Determine the source type based on file path and configuration
141fn 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}