use super::{Parser, ParserCursor};
use crate::{ParserOptions, Syntax, tokenizer::Tokenizer};
use oxc_allocator::Allocator;
pub struct ParserBuilder<'a> {
allocator: &'a Allocator,
source: &'a str,
syntax: Syntax,
options: Option<ParserOptions>,
collect_comments: bool,
}
impl<'a> ParserBuilder<'a> {
pub fn new(allocator: &'a Allocator, source: &'a str) -> Self {
let source = source.strip_prefix('\u{feff}').unwrap_or(source);
ParserBuilder {
allocator,
source,
options: None,
syntax: Syntax::default(),
collect_comments: false,
}
}
pub fn syntax(mut self, syntax: Syntax) -> Self {
self.syntax = syntax;
self
}
pub fn options(mut self, options: ParserOptions) -> Self {
self.options = Some(options);
self
}
pub fn comments(mut self) -> Self {
self.collect_comments = true;
self
}
pub fn ignore_comments(mut self) -> Self {
self.collect_comments = false;
self
}
pub fn build(self) -> Parser<'a> {
let options = self.options.unwrap_or_default();
debug_assert!(
options.template_placeholder.is_none() || self.syntax == Syntax::Scss,
"template_placeholder requires Syntax::Scss (backtick is Less's inline-JS delimiter)",
);
Parser {
allocator: self.allocator,
source: self.source,
syntax: self.syntax,
options,
cursor: ParserCursor::new(Tokenizer::new(
self.allocator,
self.source,
self.syntax,
options.template_placeholder,
self.collect_comments,
)),
state: Default::default(),
recoverable_errors: vec![],
sass_pending_indents: 0,
}
}
}