Repository navigation
Expand file tree
/
Copy pathRewriteState.php
More file actions
130 lines (112 loc) · 3.44 KB
/
Copy pathRewriteState.php
File metadata and controls
130 lines (112 loc) · 3.44 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
/*
* This file is part of the PHPRegex package.
*
* (c) Younes ENNAJI <younes.ennaji.pro@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPRegex\Optimizer;
use PHPRegex\Parser\Analysis\CharSetAnalyzer;
use PHPRegex\Parser\Node\NodeInterface;
use PHPRegex\Parser\Node\RegexNode;
use PHPRegex\Parser\Printer\PatternPrinter;
/**
* What every part of Rewriter shares about the pattern it rewrites.
*
* Rewriter walks the tree; ClassRewrites, RepeatRewrites,
* AlternationRewrites, PossessiveRewrites and FlagRewrites each rewrite a
* family of constructs for it. The modes the flags of the pattern set are
* kept here, read again from each pattern visited whole, with the kinds of
* rewrite made so far, which every part records, and the printer that
* spells a node to compare it with another. A tree visited without its
* pattern reads the modes of no flag.
*
* @internal
*/
final class RewriteState
{
/**
* Whether a character is a UTF-8 sequence: "u".
*/
public bool $unicodeMode = false;
/**
* Whether "\d", "\s" and "\w" read Unicode properties: "/u", or
* "(*UCP)".
*/
public bool $unicodeProperties = false;
/**
* Whether "U" makes a plain quantifier lazy and "??" greedy.
*/
public bool $lazyByDefault = false;
/**
* Whether an inline flag stands anywhere in the pattern visited.
*/
public bool $inlineFlags = false;
/**
* The flags of the pattern visited.
*/
private string $flags = '';
/**
* The character sets of the nodes, built once a rewrite asks for them.
*/
private ?CharSetAnalyzer $charSets = null;
/**
* The kinds of rewrite made, in the order first made.
*
* @var array<string, true>
*/
private array $changes = [];
private ?PatternPrinter $printer = null;
/**
* Reads the modes of a pattern about to be rewritten, and forgets the
* rewrites made on the one before.
*
* @param bool $inlineFlags whether an inline flag stands anywhere in it
*/
public function start(RegexNode $node, bool $inlineFlags): void
{
$this->flags = $node->flags;
$this->unicodeMode = str_contains($node->flags, 'u');
$this->unicodeProperties = UnicodeProperties::on($node);
$this->charSets = null;
$this->lazyByDefault = str_contains($node->flags, 'U');
$this->inlineFlags = $inlineFlags;
$this->changes = [];
}
/**
* The character sets of the nodes, read under the outer flags of the
* pattern visited.
*/
public function charSets(): CharSetAnalyzer
{
return $this->charSets ??= new CharSetAnalyzer($this->flags);
}
/**
* Records a kind of rewrite made.
*/
public function changed(string $change): void
{
$this->changes[$change] = true;
}
/**
* The kinds of rewrite made, each once, in the order first made.
*
* @return list<string>
*/
public function changes(): array
{
return array_keys($this->changes);
}
/**
* The node as the pattern writes it, to tell two nodes apart.
*/
public function spelling(NodeInterface $node): string
{
$this->printer ??= PatternPrinter::asWritten();
$this->printer->resetState();
return $node->accept($this->printer);
}
}