oxc_regular_expression/
flag_parser.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use oxc_allocator::Allocator;
use oxc_diagnostics::Result;
use rustc_hash::FxHashSet;

use crate::{ast, diagnostics, options::ParserOptions, span::SpanFactory};

pub struct FlagsParser<'a> {
    source_text: &'a str,
    // options: ParserOptions,
    span_factory: SpanFactory,
}

impl<'a> FlagsParser<'a> {
    pub fn new(_allocator: &'a Allocator, source_text: &'a str, options: ParserOptions) -> Self {
        Self {
            source_text,
            // options,
            span_factory: SpanFactory::new(options.span_offset),
        }
    }

    pub fn parse(&mut self) -> Result<ast::Flags> {
        let span = self.span_factory.create(0, self.source_text.len());
        let mut global = false;
        let mut ignore_case = false;
        let mut multiline = false;
        let mut unicode = false;
        let mut sticky = false;
        let mut dot_all = false;
        let mut has_indices = false;
        let mut unicode_sets = false;

        let mut existing_flags = FxHashSet::default();
        for (idx, c) in self.source_text.char_indices() {
            if !existing_flags.insert(c) {
                return Err(diagnostics::duplicated_flag(self.span_factory.create(idx, idx)));
            }

            match c {
                'g' => global = true,
                'i' => ignore_case = true,
                'm' => multiline = true,
                'u' => unicode = true,
                'y' => sticky = true,
                's' => dot_all = true,
                'd' => has_indices = true,
                'v' => unicode_sets = true,
                _ => return Err(diagnostics::unknown_flag(self.span_factory.create(idx, idx))),
            }
        }

        if unicode && unicode_sets {
            return Err(diagnostics::invalid_unicode_flags(span));
        }

        Ok(ast::Flags {
            span,
            global,
            ignore_case,
            multiline,
            unicode,
            sticky,
            dot_all,
            has_indices,
            unicode_sets,
        })
    }
}