Skip to content

Commit 962ccdc

Browse files
committed
Start back-tick strings support
1 parent 8bd3a87 commit 962ccdc

File tree

9 files changed

+37
-2
lines changed

9 files changed

+37
-2
lines changed

examples/interpolation.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name = 'Bob';
2+
3+
return `Hello ${name}, can you ${(function (verb) { return verb; })('tell')}?`;

examples/interpolation.return

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello Bob, can you tell?

src/JsPhpize/Compiler/Compiler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ function ($pair) use ($visitNode, $indent) {
138138
protected function visitConstant(Constant $constant)
139139
{
140140
$value = $constant->value;
141+
141142
if ($constant->type === 'string' && mb_substr($constant->value, 0, 1) === '"') {
142143
$value = str_replace('$', '\\$', $value);
143144
}
145+
144146
if ($constant->type === 'regexp') {
145147
$regExp = $this->engine->getHelperName('regExp');
146148
$value = $this->helperWrap($regExp, [var_export($value, true)]);
@@ -217,6 +219,7 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
217219
$staticCall = $name . '(' . $arguments . ')';
218220

219221
$functions = str_replace(["\n", "\t", "\r", ' '], '', static::STATIC_CALL_FUNCTIONS);
222+
220223
if ($applicant === 'new' || in_array($name, explode(',', $functions))) {
221224
return $staticCall;
222225
}

src/JsPhpize/JsPhpizeOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct($options = [])
5050
$this->options['patterns'] = [
5151
new Pattern(10, 'newline', '\n'),
5252
new Pattern(20, 'comment', '\/\/.*?\n|\/\*[\s\S]*?\*\/'),
53-
new Pattern(30, 'string', '"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\''),
53+
new Pattern(30, 'string', '"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|`(?:\\\\.|[^`\\\\])*`'),
5454
new Pattern(40, 'number', '0[bB][01]+|0[oO][0-7]+|0[xX][0-9a-fA-F]+|(\d+(\.\d*)?|\.\d+)([eE]-?\d+)?'),
5555
new Pattern(50, 'lambda', '=>'),
5656
new Pattern(60, 'operator', ['delete', 'typeof', 'void'], true),

src/JsPhpize/Lexer/Scanner.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace JsPhpize\Lexer;
44

55
use JsPhpize\JsPhpize;
6+
use JsPhpize\Nodes\Constant;
67

78
class Scanner
89
{
@@ -63,7 +64,16 @@ public function scanNumber($matches)
6364

6465
public function scanString($matches)
6566
{
66-
return $this->valueToken('string', $matches);
67+
/** @var Constant $stringToken */
68+
$stringToken = $this->valueToken('string', $matches);
69+
$delimiter = substr($stringToken->value, 0, 1);
70+
71+
if ($delimiter === '`') {
72+
var_dump($stringToken->value);
73+
exit;
74+
}
75+
76+
return $stringToken;
6777
}
6878

6979
public function scanOperator($matches)

src/JsPhpize/Nodes/Constant.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function __construct($type, $value)
3535
if (!in_array($type, ['constant', 'number', 'string', 'regexp'])) {
3636
throw new Exception("The given type [$type] is not a valid constant type.", 23);
3737
}
38+
3839
$this->type = $type;
3940
$this->value = $value;
4041
}
@@ -44,9 +45,11 @@ public function getNonAssignableReason()
4445
if ($this->type !== 'constant') {
4546
return "{$this->type} is not assignable.";
4647
}
48+
4749
if (in_array($this->value, ['NAN', 'INF'])) {
4850
return "{$this->value} is not assignable.";
4951
}
52+
5053
if (mb_substr($this->value, 0, 2) === 'M_') {
5154
return "'M_' prefix is reserved to mathematical constants.";
5255
}

src/JsPhpize/Parser/Parser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ protected function parseJsonMethod($method)
310310
{
311311
if ($method->type === 'variable' && ($function = $this->jsonMethodToPhpFunction($method->value))) {
312312
$this->skip(2);
313+
313314
if (($next = $this->get(0)) && $next->is('(')) {
314315
$this->skip();
315316

@@ -370,6 +371,7 @@ protected function parseKeywordStatement($token)
370371
{
371372
$name = $token->value;
372373
$keyword = new Block($name);
374+
373375
switch ($name) {
374376
case 'typeof':
375377
throw new Exception('typeof keyword not supported', 26);
@@ -384,9 +386,11 @@ protected function parseKeywordStatement($token)
384386
'clone' => 'Object',
385387
];
386388
$value = $this->get(0);
389+
387390
if (isset($expects[$name]) && !$value) {
388391
throw new Exception($expects[$name] . " expected after '" . $name . "'", 25);
389392
}
393+
390394
$this->handleOptionalValue($keyword, $value, $name);
391395
break;
392396
case 'case':
@@ -407,6 +411,7 @@ protected function parseKeywordStatement($token)
407411
protected function parseKeyword($token)
408412
{
409413
$keyword = $this->parseKeywordStatement($token);
414+
410415
if ($keyword->handleInstructions()) {
411416
$this->parseBlock($keyword);
412417
}
@@ -417,6 +422,7 @@ protected function parseKeyword($token)
417422
protected function parseLet()
418423
{
419424
$letVariable = $this->get(0);
425+
420426
if ($letVariable->type !== 'variable') {
421427
throw $this->unexpected($letVariable);
422428
}
@@ -486,15 +492,19 @@ protected function parseInstructions($block)
486492
public function parseBlock($block)
487493
{
488494
$this->stack[] = $block;
495+
489496
if (!$block->multipleInstructions) {
490497
$next = $this->get(0);
498+
491499
if ($next && $next->is('{')) {
492500
$block->enableMultipleInstructions();
493501
}
502+
494503
if ($block->multipleInstructions) {
495504
$this->skip();
496505
}
497506
}
507+
498508
$this->parseInstructions($block);
499509
array_pop($this->stack);
500510
}

src/JsPhpize/Parser/TokenExtractor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protected function getBracketsArrayItemKeyFromToken($token)
1818
if ($typeAndValue->isValid()) {
1919
list($type, $value) = $typeAndValue->get();
2020
$token = $this->next();
21+
2122
if (!$token) {
2223
throw new Exception('Missing value after ' . $value . $this->exceptionInfos(), 12);
2324
}

tests/render.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public function caseProvider()
1111

1212
$examples = __DIR__ . '/../examples';
1313
foreach (scandir($examples) as $file) {
14+
if ($file !== 'interpolation.return') {
15+
continue;
16+
}
1417
if (substr($file, -7) === '.return') {
1518
$cases[] = [$file, substr($file, 0, -7) . '.js'];
1619
}
@@ -20,6 +23,7 @@ public function caseProvider()
2023
}
2124

2225
/**
26+
* @group i
2327
* @group examples
2428
* @dataProvider caseProvider
2529
*/

0 commit comments

Comments
 (0)