Skip to content

Commit 0e13b05

Browse files
committed
Introduce unexpected tokens for better fallback to other languages
1 parent 2dea45e commit 0e13b05

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

src/JsPhpize/JsPhpizeOptions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function __construct($options = array())
4949
new Pattern(40, 'number', '0[bB][01]+|0[oO][0-7]+|0[xX][0-9a-fA-F]+|(\d+(\.\d*)?|\.\d+)([eE]-?\d+)?'),
5050
new Pattern(50, 'lambda', '=>'),
5151
new Pattern(60, 'operator', array('delete', 'typeof', 'void'), true),
52+
new Pattern(65, 'unexpected', array('::')),
5253
new Pattern(70, 'operator', array('>>>=', '<<=', '>>=', '**=')),
5354
new Pattern(80, 'operator', array('++', '--', '&&', '||', '**', '>>>', '<<', '>>')),
5455
new Pattern(90, 'operator', array('===', '!==', '>=', '<=', '<>', '!=', '==', '>', '<')),

src/JsPhpize/Lexer/Lexer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ protected function scan($pattern, $method)
9999
return false;
100100
}
101101

102+
/**
103+
* Return a unexpected exception for a given token.
104+
*
105+
* @param $token
106+
*
107+
* @return Exception
108+
*/
109+
public function unexpected($token, $className = '\\JsPhpize\\Lexer\\Exception')
110+
{
111+
return new $className('Unexpected ' . $token->type . rtrim(' ' . ($token->value ?: '')) . $this->exceptionInfos(), 8);
112+
}
113+
102114
/**
103115
* @throws Exception
104116
*

src/JsPhpize/Lexer/Scanner.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
class Scanner
88
{
9+
public function scanUnexpected($matches)
10+
{
11+
throw $this->unexpected($this->valueToken('token', $matches));
12+
}
13+
914
public function scanComment($matches)
1015
{
1116
return $this->valueToken('comment', $matches);

src/JsPhpize/Parser/TokenCrawler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ protected function exceptionInfos()
4949

5050
protected function unexpected($token)
5151
{
52-
return new Exception('Unexpected ' . $token->type . rtrim(' ' . ($token->value ?: '')) . $this->exceptionInfos(), 8);
52+
return $this->lexer->unexpected($token, '\\JsPhpize\\Parser\\Exception');
5353
}
5454
}

tests/badSyntaxes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,14 @@ public function testBadFunctionCall()
105105
{
106106
new FunctionCall(new Instruction(), array(), array());
107107
}
108+
109+
/**
110+
* @expectedException \JsPhpize\Lexer\Exception
111+
* @expectedExceptionCode 8
112+
*/
113+
public function testPhpCode()
114+
{
115+
$jsPhpize = new JsPhpize();
116+
$jsPhpize->render('DateTime::createFromFormat(\'j-M-Y\', \'15-Feb-2009\')');
117+
}
108118
}

0 commit comments

Comments
 (0)