Between   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 8
c 2
b 0
f 0
dl 0
loc 23
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A these() 0 8 1
A escaped() 0 10 2
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Parser\Helpers;
4
5
use Stratadox\Parser\Parser;
6
use Stratadox\Parser\Parsers\AllOrNothing;
7
use Stratadox\Parser\Parsers\Any;
8
use Stratadox\Parser\Parsers\Either;
9
use Stratadox\Parser\Parsers\Except;
10
use Stratadox\Parser\Parsers\Ignore;
11
use Stratadox\Parser\Parsers\Map;
12
use Stratadox\Parser\Parsers\Repeatable;
13
use Stratadox\Parser\Parsers\Sequence;
14
15
/**
16
 * Between
17
 *
18
 * Matches the parser's content between start and end.
19
 */
20
final class Between
21
{
22
    public static function these(
23
        Parser|string $from,
24
        Parser|string $to,
25
        Parser|string $search
26
    ): Parser {
27
        return Map::the(
28
            Sequence::of(Ignore::the($from), $search, Ignore::the($to)),
29
            fn($a) => $a[0]
30
        );
31
    }
32
33
    public static function escaped(
34
        Parser|string $from,
35
        Parser|string $to,
36
        string $escape,
37
        string $escape2 = null,
38
    ): Parser {
39
        return Between::these($from, $to, Join::the(Repeatable::parser(AllOrNothing::in(Either::of(
40
            Map::the(($escape2 ?: $escape) . $escape, fn() => $escape), // escaped escape
41
            Join::the(Sequence::of(Ignore::the($escape), $to)),         // escaped end
42
            Except::for($to, Any::symbol()),                            // anything else
43
        )))));
44
    }
45
}
46