use oxc_allocator::{Allocator, Dummy};
use oxc_ast::ast::Program;
use oxc_diagnostics::OxcDiagnostic;
use oxc_parser::ParseOptions;
use oxc_span::Span;
use oxc_syntax::module_record::ModuleRecord;
use crate::parser::{ParserImpl, ParserImplReturn};
mod irregular_whitespaces;
mod parser;
#[cfg(test)]
mod test;
pub struct VueOxcParser<'a> {
allocator: &'a Allocator,
source_text: &'a str,
options: ParseOptions,
}
#[non_exhaustive]
pub struct VueParserReturn<'a> {
pub program: Program<'a>,
pub module_record: ModuleRecord<'a>,
pub errors: Vec<OxcDiagnostic>,
pub irregular_whitespaces: Box<[Span]>,
pub panicked: bool,
}
impl<'a> VueOxcParser<'a> {
pub fn new(allocator: &'a Allocator, source_text: &'a str) -> Self {
Self {
allocator,
source_text,
options: ParseOptions::default(),
}
}
#[must_use]
pub const fn with_options(mut self, options: ParseOptions) -> Self {
self.options = options;
self
}
}
impl<'a> VueOxcParser<'a> {
#[must_use]
pub fn parse(self) -> VueParserReturn<'a> {
let ParserImplReturn {
program,
errors,
fatal,
} = ParserImpl::new(self.allocator, self.source_text, self.options).parse();
if fatal {
VueParserReturn {
program: Program::dummy(self.allocator),
module_record: ModuleRecord::new(self.allocator),
errors,
irregular_whitespaces: Box::new([]),
panicked: true,
}
} else {
VueParserReturn {
program,
errors,
panicked: false,
irregular_whitespaces: self.get_irregular_whitespaces(),
module_record: ModuleRecord::new(self.allocator),
}
}
}
}