Skip to content

Commit f7f2f08

Browse files
committed
Allow empty strings
1 parent 022da8b commit f7f2f08

6 files changed

Lines changed: 89 additions & 3 deletions

File tree

src/Parser/Tokenizer/Buffer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public function value(): string
4747
return $this->fragment?->value ?? '';
4848
}
4949

50+
public function isEmpty(): bool
51+
{
52+
return $this->fragment === null;
53+
}
54+
5055
public function flush(TokenType $tokenType): \Iterator
5156
{
5257
if ($this->fragment !== null) {

src/Parser/Tokenizer/Token.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ public static function fromFragment(
4848
);
4949
}
5050

51+
public static function emptyFromDelimitingFragments(
52+
TokenType $type,
53+
Fragment $startFragment,
54+
Fragment $endFragment
55+
): Token {
56+
return new Token(
57+
$type,
58+
'',
59+
Boundaries::fromPositions($startFragment->start, $endFragment->end),
60+
$startFragment->source
61+
);
62+
}
63+
5164
public function equals(Token $other): bool
5265
{
5366
return ($this->type === $other->type

src/Parser/Tokenizer/Tokenizer.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,23 @@ private static function flushRemainder(Buffer $buffer): \Iterator
123123
*/
124124
private static function string(\Iterator $fragments): \Iterator
125125
{
126-
$delimiter = $fragments->current()->value;
126+
$delimiter = $fragments->current();
127127
$fragments->next();
128128

129129
$buffer = Buffer::empty();
130130

131131
while ($fragments->valid()) {
132132
switch ($fragments->current()->value) {
133-
case $delimiter:
134-
yield from $buffer->flush(TokenType::STRING_QUOTED);
133+
case $delimiter->value:
134+
if ($buffer->isEmpty()) {
135+
yield Token::emptyFromDelimitingFragments(
136+
TokenType::STRING_QUOTED,
137+
$delimiter,
138+
$fragments->current()
139+
);
140+
} else {
141+
yield from $buffer->flush(TokenType::STRING_QUOTED);
142+
}
135143
$fragments->next();
136144
return;
137145

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export component Empty { return "" }
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[
2+
{
3+
"type": "KEYWORD_EXPORT",
4+
"value": "export"
5+
},
6+
{
7+
"type": "SPACE",
8+
"value": " "
9+
},
10+
{
11+
"type": "KEYWORD_COMPONENT",
12+
"value": "component"
13+
},
14+
{
15+
"type": "SPACE",
16+
"value": " "
17+
},
18+
{
19+
"type": "STRING",
20+
"value": "Empty"
21+
},
22+
{
23+
"type": "SPACE",
24+
"value": " "
25+
},
26+
{
27+
"type": "BRACKET_CURLY_OPEN",
28+
"value": "{"
29+
},
30+
{
31+
"type": "SPACE",
32+
"value": " "
33+
},
34+
{
35+
"type": "KEYWORD_RETURN",
36+
"value": "return"
37+
},
38+
{
39+
"type": "SPACE",
40+
"value": " "
41+
},
42+
{
43+
"type": "STRING_QUOTED",
44+
"value": ""
45+
},
46+
{
47+
"type": "SPACE",
48+
"value": " "
49+
},
50+
{
51+
"type": "BRACKET_CURLY_CLOSE",
52+
"value": "}"
53+
},
54+
{
55+
"type": "END_OF_LINE",
56+
"value": "\n"
57+
}
58+
]

test/Integration/TokenizerIntegrationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function tokenizationExamples(): array
3636
return [
3737
'Comment' => ["Comment"],
3838
'Component' => ["Component"],
39+
'ComponentEmpty' => ["ComponentEmpty"],
3940
'ComponentWithKeywords' => ["ComponentWithKeywords"],
4041
'ComponentWithNesting' => ["ComponentWithNesting"],
4142
'Enum' => ["Enum"],

0 commit comments

Comments
 (0)