Skip to content

Commit 63cafc5

Browse files
committed
WIP standard / aggressive value matching modes
1 parent 4cc1d9e commit 63cafc5

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ composer-update:
77

88
test:
99
docker-compose run --rm php-${PHP} php -v
10-
docker-compose run --rm php-${PHP} php /app/vendor/bin/phpunit -c /app/phpunit.xml.dist
10+
docker-compose run --rm php-${PHP} php /app/vendor/bin/phpunit -c /app/phpunit.xml.dist --filter "${TEST}"
1111
test-local:
1212
php -v
1313
php vendor/bin/phpunit

src/Parser/RegularParser.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ final class RegularParser implements ParserInterface
3535
const TOKEN_STRING = 6;
3636
const TOKEN_WS = 7;
3737

38+
const VALUE_REGULAR = 0x01;
39+
const VALUE_AGGRESSIVE = 0x02;
40+
41+
/** @var int */
42+
public $valueMode = self::VALUE_REGULAR;
43+
3844
public function __construct(SyntaxInterface $syntax = null)
3945
{
4046
$this->lexerRegex = $this->prepareLexer($syntax ?: new CommonSyntax());
@@ -239,7 +245,16 @@ private function value()
239245
}
240246

241247
if($this->lookahead(self::TOKEN_STRING) || $this->lookahead(self::TOKEN_MARKER)) {
242-
while(false === ($this->lookahead(self::TOKEN_WS) || $this->lookahead(self::TOKEN_CLOSE) || $this->lookaheadN(array(self::TOKEN_MARKER, self::TOKEN_CLOSE)))) {
248+
while(true) {
249+
if($this->lookahead(self::TOKEN_WS) || $this->lookahead(self::TOKEN_CLOSE)) {
250+
break;
251+
}
252+
if($this->lookaheadN(array(self::TOKEN_MARKER, self::TOKEN_CLOSE))) {
253+
if($this->valueMode === self::VALUE_AGGRESSIVE) {
254+
$value .= $this->match(null, false);
255+
}
256+
break;
257+
}
243258
$value .= $this->match(null, false);
244259
}
245260

@@ -301,6 +316,11 @@ private function lookahead($type)
301316
return $this->position < $this->tokensCount && $this->tokens[$this->position][0] === $type;
302317
}
303318

319+
/**
320+
* @param int[] $types
321+
*
322+
* @return bool
323+
*/
304324
private function lookaheadN(array $types)
305325
{
306326
$count = count($types);
@@ -323,6 +343,12 @@ private function lookaheadN(array $types)
323343
return true;
324344
}
325345

346+
/**
347+
* @param int|null $type
348+
* @param bool $ws
349+
*
350+
* @return string
351+
*/
326352
private function match($type, $ws)
327353
{
328354
if($this->position >= $this->tokensCount) {

tests/ParserTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public function provideShortcodes()
252252
*
253253
* Tests cases from array above with identifiers in the array below must be skipped.
254254
*/
255-
$wordpressSkip = array(3, 6, 16, 21, 22, 23, 25, 32, 33, 34, 46, 47, 49, 51);
255+
$wordpressSkip = array(3, 6, 16, 21, 22, 23, 25, 32, 33, 34, 46, 47, 49, 51, 52);
256256
$result = array();
257257
foreach($tests as $key => $test) {
258258
$syntax = array_shift($test);
@@ -276,12 +276,35 @@ public function testIssue77()
276276
new ParsedShortcode(new Shortcode('x', array(), '', null), '[x][/x]', 3),
277277
new ParsedShortcode(new Shortcode('y', array(), 'x', null), '[y]x[/y]', 22),
278278
));
279-
280279
$this->assertShortcodes($parser->parse('[a k="v][x][/x]'), array(
281280
new ParsedShortcode(new Shortcode('x', array(), '', null), '[x][/x]', 8),
282281
));
283282
}
284283

284+
public function testValueModeAggressive()
285+
{
286+
$parser = new RegularParser(new CommonSyntax());
287+
$parser->valueMode = RegularParser::VALUE_AGGRESSIVE;
288+
$parsed = $parser->parse('[x=/[/] [y a=/"//] [z=http://url/] [a=http://url ]');
289+
$tested = array(
290+
new ParsedShortcode(new Shortcode('x', array(), null, '/[/'), '[x=/[/]', 0),
291+
new ParsedShortcode(new Shortcode('y', array('a' => '/"//'), null, null), '[y a=/"//]', 8),
292+
new ParsedShortcode(new Shortcode('z', array(), null, 'http://url/'), '[z=http://url/]', 19),
293+
new ParsedShortcode(new Shortcode('a', array(), null, 'http://url'), '[a=http://url ]', 35),
294+
);
295+
296+
$count = count($tested);
297+
static::assertCount($count, $parsed, 'counts');
298+
for ($i = 0; $i < $count; $i++) {
299+
static::assertSame($tested[$i]->getName(), $parsed[$i]->getName(), 'name');
300+
static::assertSame($tested[$i]->getParameters(), $parsed[$i]->getParameters(), 'parameters');
301+
static::assertSame($tested[$i]->getContent(), $parsed[$i]->getContent(), 'content');
302+
static::assertSame($tested[$i]->getText(), $parsed[$i]->getText(), 'text');
303+
static::assertSame($tested[$i]->getOffset(), $parsed[$i]->getOffset(), 'offset');
304+
static::assertSame($tested[$i]->getBbCode(), $parsed[$i]->getBbCode(), 'bbCode');
305+
}
306+
}
307+
285308
public function testWordPress()
286309
{
287310
$parser = new WordpressParser();

0 commit comments

Comments
 (0)