oxc_css_parser/parser/
builder.rs1use super::Parser;
2use crate::{ParserOptions, Syntax, tokenizer::Tokenizer};
3use oxc_allocator::Allocator;
4
5pub struct ParserBuilder<'a> {
12 allocator: &'a Allocator,
13 source: &'a str,
14 syntax: Syntax,
15 options: Option<ParserOptions>,
16 collect_comments: bool,
17}
18
19impl<'a> ParserBuilder<'a> {
20 pub fn new(allocator: &'a Allocator, source: &'a str) -> Self {
22 let source = source.strip_prefix('\u{feff}').unwrap_or(source);
23 ParserBuilder {
24 allocator,
25 source,
26 options: None,
27 syntax: Syntax::default(),
28 collect_comments: false,
29 }
30 }
31
32 pub fn syntax(mut self, syntax: Syntax) -> Self {
34 self.syntax = syntax;
35 self
36 }
37
38 pub fn options(mut self, options: ParserOptions) -> Self {
40 self.options = Some(options);
41 self
42 }
43
44 pub fn comments(mut self) -> Self {
46 self.collect_comments = true;
47 self
48 }
49
50 pub fn ignore_comments(mut self) -> Self {
55 self.collect_comments = false;
56 self
57 }
58
59 pub fn build(self) -> Parser<'a> {
61 let options = self.options.unwrap_or_default();
62 debug_assert!(
65 options.template_placeholder.is_none() || self.syntax == Syntax::Scss,
66 "template_placeholder requires Syntax::Scss (backtick is Less's inline-JS delimiter)",
67 );
68 Parser {
69 allocator: self.allocator,
70 source: self.source,
71 syntax: self.syntax,
72 options,
73 tokenizer: Tokenizer::new(
74 self.allocator,
75 self.source,
76 self.syntax,
77 options.template_placeholder,
78 self.collect_comments,
79 ),
80 state: Default::default(),
81 recoverable_errors: vec![],
82 cached_token: None,
83 }
84 }
85}