Skip to content

Commit 16308a3

Browse files
committed
improved parser regex to report single tokens for series of non-token characters, added opcode optimized function aliases
1 parent dd33d8c commit 16308a3

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/Parser/RegularParser.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function parse($text)
4242
$this->tokens = $this->tokenize($text);
4343
$this->backtracks = array();
4444
$this->position = 0;
45-
$this->tokensCount = count($this->tokens);
45+
$this->tokensCount = \count($this->tokens);
4646

4747
$shortcodes = array();
4848
while($this->position < $this->tokensCount) {
@@ -52,7 +52,7 @@ public function parse($text)
5252
$names = array();
5353
$this->beginBacktrack();
5454
$matches = $this->shortcode($names);
55-
if(is_array($matches)) {
55+
if(\is_array($matches)) {
5656
foreach($matches as $shortcode) {
5757
$shortcodes[] = $shortcode;
5858
}
@@ -130,11 +130,11 @@ private function content(array &$names)
130130

131131
$this->beginBacktrack();
132132
$matchedShortcodes = $this->shortcode($names);
133-
if(is_string($matchedShortcodes)) {
133+
if(\is_string($matchedShortcodes)) {
134134
$closingName = $matchedShortcodes;
135135
break;
136136
}
137-
if(is_array($matchedShortcodes)) {
137+
if(\is_array($matchedShortcodes)) {
138138
foreach($matchedShortcodes as $matchedShortcode) {
139139
$shortcodes[] = $matchedShortcode;
140140
}
@@ -168,7 +168,7 @@ private function close(array &$names)
168168
if(!$this->match(self::TOKEN_STRING, $setName, true)) { return false; }
169169
if(!$this->match(self::TOKEN_CLOSE)) { return false; }
170170

171-
return in_array($closingName, $names, true) ? $closingName : false;
171+
return \in_array($closingName, $names, true) ? $closingName : false;
172172
}
173173

174174
private function bbCode()
@@ -237,7 +237,7 @@ private function getBacktrack()
237237
private function backtrack($modifyPosition = true)
238238
{
239239
$tokens = array_pop($this->backtracks);
240-
$count = count($tokens);
240+
$count = \count($tokens);
241241
if($modifyPosition) {
242242
$this->position -= $count;
243243
}
@@ -273,6 +273,7 @@ private function match($type, $callback = null, $ws = false)
273273
}
274274
unset($backtrack);
275275

276+
/** @var callable $callback */
276277
$callback && $callback($token);
277278
$this->position++;
278279

@@ -321,21 +322,21 @@ private function getTokenizerRegex(SyntaxInterface $syntax)
321322
return preg_replace('/(.)/us', '\\\\$0', $text);
322323
};
323324

325+
$symbols = array_map($quote, [
326+
$syntax->getOpeningTag(),
327+
$syntax->getClosingTag(),
328+
$syntax->getClosingTagMarker(),
329+
$syntax->getParameterValueSeparator(),
330+
$syntax->getParameterValueDelimiter(),
331+
]);
324332
$rules = array(
325333
$group($syntax->getOpeningTag(), 'open'),
326334
$group($syntax->getClosingTag(), 'close'),
327335
$group($syntax->getClosingTagMarker(), 'marker'),
328336
$group($syntax->getParameterValueSeparator(), 'separator'),
329337
$group($syntax->getParameterValueDelimiter(), 'delimiter'),
330338
'(?<ws>\s+)',
331-
'(?<string>\\\\.|(?:(?!'.implode('|', array(
332-
$quote($syntax->getOpeningTag()),
333-
$quote($syntax->getClosingTag()),
334-
$quote($syntax->getClosingTagMarker()),
335-
$quote($syntax->getParameterValueSeparator()),
336-
$quote($syntax->getParameterValueDelimiter()),
337-
'\s+',
338-
)).').)+)',
339+
'(?<string>(?:(?!'.implode('|', $symbols).'|\s)(?:\\\\.|.))+)',
339340
);
340341

341342
return '~('.implode('|', $rules).')~us';

0 commit comments

Comments
 (0)