-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
Lexer.php
176 lines (148 loc) · 5.16 KB
/
Lexer.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Neos\Fusion\Core\ObjectTreeParser;
/*
* This file is part of the Neos.Fusion package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
class Lexer
{
// Difference to: Neos\Eel\Package::EelExpressionRecognizer
// added an atomic group (to prevent catastrophic backtracking) and removed the end anchor $
protected const PATTERN_EEL_EXPRESSION = <<<'REGEX'
/
^\${(?P<exp>
(?>
{ (?P>exp) } # match object literal expression recursively
|[^{}"']+ # simple eel expression without quoted strings
|"[^"\\]* # double quoted strings with possibly escaped double quotes
(?:
\\. # escaped character (quote)
[^"\\]* # unrolled loop following Jeffrey E.F. Friedl
)*"
|'[^'\\]* # single quoted strings with possibly escaped single quotes
(?:
\\. # escaped character (quote)
[^'\\]* # unrolled loop following Jeffrey E.F. Friedl
)*'
)*
)}
/x
REGEX;
protected const TOKEN_REGEX = [
Token::SLASH_COMMENT => '/^\\/\\/.*/',
Token::HASH_COMMENT => '/^#.*/',
Token::MULTILINE_COMMENT => <<<'REGEX'
`^
/\* # start of a comment '/*'
[^*]* # consume until special case '*'
\*+ # consume all '*'
(?:
[^/] # break if its the end: '/'
[^*]* # unrolled loop following Jeffrey E.F. Friedl
\*+
)*
/ # the end of a comment.
`x
REGEX,
Token::NEWLINE => '/^[\n\r]+/',
Token::SPACE => '/^[ \t]+/',
// VALUE ASSIGNMENT
Token::TRUE_VALUE => '/^(?>true|TRUE)\\b/',
Token::FALSE_VALUE => '/^(?>false|FALSE)\\b/',
Token::NULL_VALUE => '/^(?>null|NULL)\\b/',
Token::INTEGER => '/^-?[0-9]+/',
Token::FLOAT => '/^-?[0-9]+\.[0-9]+/',
Token::DSL_EXPRESSION_START => '/^[a-zA-Z0-9\.]++(?=`)/',
Token::DSL_EXPRESSION_CONTENT => '/^`[^`]*+`/',
Token::EEL_EXPRESSION => self::PATTERN_EEL_EXPRESSION,
// Object type part
Token::FUSION_OBJECT_NAME => '/^[0-9a-zA-Z.]+:[0-9a-zA-Z.]+/',
// Keywords
Token::INCLUDE => '/^include\\s*:/',
// Object path segments
Token::PROTOTYPE_START => '/^prototype\(/',
Token::META_PATH_START => '/^@/',
Token::OBJECT_PATH_PART => '/^[a-zA-Z0-9_:-]+/',
// Operators
Token::ASSIGNMENT => '/^=/',
Token::COPY => '/^</',
Token::UNSET => '/^>/',
// Symbols
Token::DOT => '/^\./',
Token::COLON => '/^:/',
Token::RPAREN => '/^\)/',
Token::LBRACE => '/^{/',
Token::RBRACE => '/^}/',
// Strings
Token::STRING_DOUBLE_QUOTED => <<<'REGEX'
/^
"[^"\\]* # double quoted strings with possibly escaped double quotes
(?:
\\. # escaped character (quote)
[^"\\]* # unrolled loop following Jeffrey E.F. Friedl
)*
"
/x
REGEX,
Token::STRING_SINGLE_QUOTED => <<<'REGEX'
/^
'[^'\\]* # single quoted strings with possibly escaped single quotes
(?:
\\. # escaped character (quote)
[^'\\]* # unrolled loop following Jeffrey E.F. Friedl
)*
'
/x
REGEX,
Token::FILE_PATTERN => '`^[a-zA-Z0-9.*:/_-]+`',
];
protected string $code = '';
protected int $codeLen = 0;
protected int $cursor = 0;
protected ?Token $lookahead = null;
public function __construct(string $code)
{
$code = str_replace(["\r\n", "\r"], "\n", $code);
$this->code = $code;
$this->codeLen = strlen($code);
}
public function getCode(): string
{
return $this->code;
}
public function getCursor(): int
{
return $this->cursor;
}
public function consumeLookahead(): Token
{
$token = $this->lookahead;
$this->lookahead = null;
return $token;
}
public function getCachedLookaheadOrTryToGenerateLookaheadForTokenAndGetLookahead(int $tokenType): ?Token
{
if ($this->lookahead !== null) {
return $this->lookahead;
}
if ($this->cursor === $this->codeLen) {
return $this->lookahead = new Token(Token::EOF, '');
}
if ($tokenType === Token::EOF) {
return null;
}
$regexForToken = self::TOKEN_REGEX[$tokenType];
$remainingCode = substr($this->code, $this->cursor);
if (preg_match($regexForToken, $remainingCode, $matches) !== 1) {
return null;
}
$this->cursor += \strlen($matches[0]);
return $this->lookahead = new Token($tokenType, $matches[0]);
}
}