• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pomsky-lang / pomsky / 12099739427

30 Nov 2024 09:51PM UTC coverage: 80.473% (-0.6%) from 81.072%
12099739427

push

github

Aloso
feat: character set intersections

200 of 274 new or added lines in 19 files covered. (72.99%)

3 existing lines in 3 files now uncovered.

4422 of 5495 relevant lines covered (80.47%)

391063.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.34
/pomsky-lib/src/exprs/intersection.rs
1
use crate::{
2
    compile::{CompileResult, CompileState},
3
    diagnose::{CompileErrorKind, Feature},
4
    options::{CompileOptions, RegexFlavor},
5
    regex::Regex,
6
    unicode_set::UnicodeSet,
7
};
8

9
use super::{
10
    char_class::{RegexCharSet, RegexCompoundCharSet},
11
    group::RegexGroupKind,
12
    Compile, Intersection,
13
};
14

15
impl Compile for Intersection {
16
    fn compile<'c>(
7✔
17
        &'c self,
7✔
18
        options: CompileOptions,
7✔
19
        state: &mut CompileState<'c>,
7✔
20
    ) -> CompileResult {
7✔
21
        // this would be much easier to write with try_reduce, but that's unstable
7✔
22

7✔
23
        let mut rules = self.rules.iter().map(|r| (r.span(), r.compile(options, state)));
14✔
24
        let (first_span, first) = rules.next().expect("Intersection is empty");
7✔
25

26
        let regex = rules.try_fold(first?, |a, (right_span, b)| match as_sets(a, b?) {
7✔
27
            Ok((left, right)) => Ok(left.add(right)),
7✔
NEW
28
            Err(kind) => Err(kind.at(first_span.join(right_span))),
×
29
        })?;
7✔
30

31
        if let Regex::CompoundCharSet(_) = regex {
7✔
32
            if let RegexFlavor::Java | RegexFlavor::Ruby | RegexFlavor::Rust = options.flavor {
6✔
33
                // supported
5✔
34
            } else {
5✔
35
                return Err(CompileErrorKind::Unsupported(
1✔
36
                    Feature::CharSetIntersection,
1✔
37
                    options.flavor,
1✔
38
                )
1✔
39
                .at(self.span));
1✔
40
            }
41
        }
1✔
42

43
        Ok(regex)
6✔
44
    }
7✔
45
}
46

47
fn as_sets(a: Regex, b: Regex) -> Result<(RegexCompoundCharSet, RegexCharSet), CompileErrorKind> {
7✔
48
    match (expand_regex(a), expand_regex(b)) {
7✔
NEW
49
        (Regex::CompoundCharSet(a), Regex::CharSet(b)) => Ok((a, b)),
×
50
        (Regex::CharSet(a), Regex::CharSet(b)) => Ok((RegexCompoundCharSet::new(a), b)),
7✔
NEW
51
        _ => Err(CompileErrorKind::BadIntersection),
×
52
    }
53
}
7✔
54

55
fn expand_regex(r: Regex) -> Regex {
16✔
56
    match r {
2✔
NEW
57
        Regex::Literal(ref lit) => {
×
NEW
58
            let mut chars = lit.chars();
×
NEW
59
            if let Some(char) = chars.next() {
×
NEW
60
                if chars.next().is_none() {
×
NEW
61
                    let mut set = UnicodeSet::new();
×
NEW
62
                    set.add_char(char);
×
NEW
63
                    return Regex::CharSet(RegexCharSet::new(set));
×
NEW
64
                }
×
NEW
65
            }
×
NEW
66
            r
×
67
        }
68
        Regex::Group(g) if g.kind == RegexGroupKind::Normal && g.parts.len() == 1 => {
2✔
69
            expand_regex(g.parts.into_iter().next().unwrap())
2✔
70
        }
71
        _ => r,
14✔
72
    }
73
}
16✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc