-
Notifications
You must be signed in to change notification settings - Fork 10
/
AttributeScanner.php
198 lines (155 loc) · 5.86 KB
/
AttributeScanner.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Tale\Jade\Lexer\Scanner;
use Tale\Jade\Lexer;
use Tale\Jade\Lexer\ScannerInterface;
use Tale\Jade\Lexer\State;
use Tale\Jade\Lexer\Token\AttributeEndToken;
use Tale\Jade\Lexer\Token\AttributeStartToken;
use Tale\Jade\Lexer\Token\AttributeToken;
use Tale\Reader;
class AttributeScanner implements ScannerInterface
{
private function skipComments(Reader $reader)
{
if ($reader->peekString('//')) {
$reader->consume();
$reader->readUntilNewLine();
}
}
public function scan(State $state)
{
$reader = $state->getReader();
if (!$reader->peekChar('('))
return;
$reader->consume();
yield $state->createToken(AttributeStartToken::class);
if ($reader->peekChar(')')) {
$reader->consume();
yield $state->createToken(AttributeEndToken::class);
return;
}
while ($reader->hasLength()) {
//Check for comments
// a( //Now attributes follow!
// a=a...
$reader->readSpaces();
$this->skipComments($reader);
$reader->readSpaces();
//We create the attribute token first (we don't need to yield it
//but we fill it sequentially)
/** @var AttributeToken $token */
$token = $state->createToken(AttributeToken::class);
$token->escape();
$token->check();
//Read the first part of the expression
//e.g.:
// (`a`), (`a`=b), (`$expr`, `$expr2`) (`$expr` `$expr`=a)
$expr = $reader->readExpression([
' ', "\t", "\n", ',', '?!=', '?=', '!=', '=', ')', '//'
]);
//Notice we have the following problem with spaces:
//1. You can separate arguments with spaces
// -> a(a=a b=b c=c)
//2. You can have spaces around anything
// -> a(a =
// a c=c d
// = d)
//3. You can also separate with tabs and line-breaks
// -> a(
// a=a
// b=b
// c=c
// )
//
//This leads to commas actually being just ignored as the most
//simple solution. Attribute finding passes on as long as there's
//no ) or EOF in sight.
//TODO: Afaik this could also lead to
// a(a=(b ? b : c)d=f), where a space or anything else _should_
// be required.
// Check this.
//Ignore the comma. It's mainly just a "visual" separator,
//it's actually completely optional.
if ($reader->peekChar(','))
$reader->consume();
if (empty($expr))
//An empty attribute would mean we did something like
//,, or had a space before a comma (since space is also a valid
//separator
//We just skip that one.
continue;
$token->setName($expr);
//Check for comments at this point
// a(
// href //<- The name of the thing
// = 'value' //<- The value of the thing
$reader->readSpaces();
$this->skipComments($reader);
$reader->readSpaces();
//Check for our assignment-operators.
//Notice that they have to be exactly written in the correct order
//? first, ! second, = last (and required!)
//It's made like this on purpose so that the Jade code is consistent
//later on. It also makes this part of the lexing process easier and
//more reliable.
//If any of the following assignment operators have been found,
//we REQUIRE a following expression as the attribute value
$hasValue = false;
if ($reader->peekString('?!=')) {
$token->unescape();
$token->uncheck();
$hasValue = true;
$reader->consume();
} else if ($reader->peekString('?=')) {
$token->uncheck();
$hasValue = true;
$reader->consume();
} else if ($reader->peekString('!=')) {
$token->unescape();
$hasValue = true;
$reader->consume();
} else if ($reader->peekChar('=')) {
$hasValue = true;
$reader->consume();
}
//Check for comments again
// a(
// href= //Here be value
// 'value'
// )
$reader->readSpaces();
$this->skipComments($reader);
$reader->readSpaces();
if ($hasValue) {
$expr = $reader->readExpression([
' ', "\t", "\n", ',', ')', '//'
]);
$token->setValue($expr);
//Ignore a comma if found
if ($reader->peekChar(','))
$reader->consume();
//And check for comments again
// a(
// href='value' //<- Awesome attribute, i say
// )
$reader->readSpaces();
$this->skipComments($reader);
$reader->readSpaces();
}
yield $token;
if (!$reader->peekChar(')'))
continue;
break;
}
if (!$reader->peekChar(')'))
$state->throwException(
"Unclosed attribute block"
);
$reader->consume();
yield $state->createToken(AttributeEndToken::class);
foreach ($state->scan(ClassScanner::class) as $subToken)
yield $subToken;
foreach ($state->scan(SubScanner::class) as $subToken)
yield $subToken;
}
}