use super::Parser;
use crate::{
ParserOptions, Syntax,
tokenizer::{Tokenizer, token::Comment},
};
pub struct ParserBuilder<'cmt, 's: 'cmt> {
source: &'s str,
syntax: Syntax,
options: Option<ParserOptions>,
comments: Option<&'cmt mut Vec<Comment<'s>>>,
}
impl<'cmt, 's: 'cmt> ParserBuilder<'cmt, 's> {
pub fn new(source: &'s str) -> Self {
let source = source.strip_prefix('\u{feff}').unwrap_or(source);
ParserBuilder {
source,
options: None,
syntax: Syntax::default(),
comments: None,
}
}
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, comments: &'cmt mut Vec<Comment<'s>>) -> Self {
self.comments = Some(comments);
self
}
pub fn ignore_comments(mut self) -> Self {
self.comments = None;
self
}
pub fn build(self) -> Parser<'cmt, 's> {
Parser {
source: self.source,
syntax: self.syntax,
options: self.options.unwrap_or_default(),
tokenizer: Tokenizer::new(self.source, self.syntax, self.comments),
state: Default::default(),
recoverable_errors: vec![],
cached_token: None,
}
}
}