Skip to content

Commit 41a3f8e

Browse files
committed
Add an example of custom pattern in unit tests
1 parent c605226 commit 41a3f8e

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

src/JsPhpize/Lexer/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function getNextParseLength($input = null)
8888
return $length - mb_strlen($parser->rest());
8989
}
9090

91-
protected function consume($consumed)
91+
public function consume($consumed)
9292
{
9393
$consumed = is_int($consumed) ? mb_substr($this->input, 0, $consumed) : $consumed;
9494
$this->consumed = mb_strlen(trim($consumed)) > 1 ? $consumed : $this->consumed . $consumed;

src/JsPhpize/Lexer/StringLexer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace JsPhpize\Lexer;
44

5-
use JsPhpize\Parser\Exception;
6-
75
class StringLexer
86
{
97
/**
@@ -54,7 +52,7 @@ public function getBackTickString(): string
5452
}
5553

5654
if (!preg_match('/^(\\\\.|\\$(?!{)|[^`$])*`/U', $this->input, $match)) {
57-
throw new Exception('Unterminated ` string after ' . $this->string);
55+
throw new Exception('Unterminated ` string after ' . $this->string, 27);
5856
}
5957

6058
return $this->string . $match[0];

tests/CustomPattern.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class CustomPattern extends \JsPhpize\Lexer\Pattern
4+
{
5+
public function __construct()
6+
{
7+
parent::__construct(42, 'string', []);
8+
}
9+
10+
public function lexWith(\JsPhpize\Lexer\Lexer $lexer): Generator
11+
{
12+
if (substr($lexer->rest(), 0, 1) === '@') {
13+
$lexer->consume(1);
14+
yield new \JsPhpize\Lexer\Token('string', ['value' => '"@1"']);
15+
yield new \JsPhpize\Lexer\Token(',', []);
16+
yield new \JsPhpize\Lexer\Token('string', ['value' => '"@2"']);
17+
}
18+
}
19+
}

tests/badSyntaxes.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,15 @@ public function testPhpCode()
137137
$jsPhpize = new JsPhpize();
138138
$jsPhpize->render('DateTime::createFromFormat(\'j-M-Y\', \'15-Feb-2009\')');
139139
}
140+
141+
/**
142+
* @expectedException \JsPhpize\Lexer\Exception
143+
* @expectedExceptionCode 27
144+
* @expectedExceptionMessage Unterminated ` string after `foo ${`bar`}
145+
*/
146+
public function testUnterminatedString()
147+
{
148+
$jsPhpize = new JsPhpize();
149+
$jsPhpize->render('`foo ${`bar`}');
150+
}
140151
}

tests/options.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ public function testPatterns()
121121
$this->assertSame('1 @ 8;', trim($code));
122122
}
123123

124+
/**
125+
* @group patterns
126+
*/
127+
public function testCustomPattern()
128+
{
129+
include_once __DIR__ . '/CustomPattern.php';
130+
$jsPhpize = new JsPhpize();
131+
$jsPhpize->addPattern(new CustomPattern());
132+
$code = $jsPhpize->compile('foo(@)');
133+
$this->assertSame('(function_exists(\'foo\') ? foo("@1", "@2") : $foo("@1", "@2"));', trim($code));
134+
}
135+
124136
/**
125137
* @group patterns
126138
* @expectedException \JsPhpize\Lexer\Exception

0 commit comments

Comments
 (0)