Skip to content

Commit b9fcfd2

Browse files
committed
Handle number, boolean, and constants
1 parent 31cea14 commit b9fcfd2

File tree

10 files changed

+52
-17
lines changed

10 files changed

+52
-17
lines changed

examples/constant.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
false;
2+
23;
3+
.34;
4+
-2.01;
5+
12e-3;
6+
0xf5D;
7+
[ null, undefined ];

examples/constant.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
false;
2+
23;
3+
.34;
4+
- 2.01;
5+
12e-3;
6+
0xf5D;
7+
array( null, null );

src/JsPhpize/Lexer/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function next()
9999
$patterns = array(
100100
'\/\/.*?\n|\/\*[\\s\\S]*?\*\/' => 'comment',
101101
'"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'' => 'string',
102-
'0[bB][01]+|0[oO][0-7]+|0[xX][0-9a-fA-F]|(\d+(\.\d*)?|\.\d+)([eE]-?\d+)?' => 'number',
102+
'0[bB][01]+|0[oO][0-7]+|0[xX][0-9a-fA-F]+|(\d+(\.\d*)?|\.\d+)([eE]-?\d+)?' => 'number',
103103
'=>' => 'lambda',
104104
'delete|typeof|void' => 'operator',
105105
'>>>=|<<=|>>=|\*\*=' => 'operator',

src/JsPhpize/Lexer/Scanner.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@ public function scanComment($matches)
1111

1212
public function scanConstant($matches)
1313
{
14-
if (substr($matches[0], 0, 5) === '__JP_') {
14+
$constant = trim($matches[0]);
15+
if (substr($constant, 0, 5) === '__JP_') {
1516
throw new Exception('Constants cannot start with __JP_, this prefix is reserved for JsPhpize' . $this->exceptionInfos(), 1);
1617
}
1718
$translate = array(
1819
'Infinity' => 'INF',
1920
'NaN' => 'NAN',
2021
'undefined' => 'null',
2122
);
22-
if (isset($translate[$matches[0]])) {
23-
$matches[0] = $translate[$matches[0]];
23+
if (isset($translate[$constant])) {
24+
$constant = $translate[$constant];
2425
} elseif (substr($matches[0], 0, 5) === 'Math.') {
25-
$matches[0] = 'M_' . substr($matches[0], 5);
26+
$constant = 'M_' . substr($constant, 5);
2627
}
28+
$this->consume($matches[0]);
2729

28-
return $this->valueToken('constant', $matches);
30+
return $this->token('constant', $constant);
2931
}
3032

3133
public function scanFunction($matches)

src/JsPhpize/Nodes/HooksArray.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ class HooksArray extends ArrayBase
66
{
77
public function addItem($value)
88
{
9-
$this->data[] = $value;
9+
if (!empty($value)) {
10+
$this->data[] = $value;
11+
}
1012
}
1113

1214
public function __toString()

src/JsPhpize/Parser/ExpressionParser.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,20 @@ protected function getHooksArray()
7272
{
7373
$array = new HooksArray();
7474

75+
$item = '';
7576
while ($token = $this->next()) {
7677
if ($token->type === ']') {
78+
$array->addItem($item);
79+
7780
return $array;
7881
}
82+
if ($token->type === ',') {
83+
$array->addItem($item);
84+
$item = '';
85+
continue;
86+
}
7987

80-
$array->addItem($this->expressionFromToken($token));
88+
$item .= $this->expressionFromToken($token);
8189
}
8290

8391
throw new Exception('Missing ] after array items list' . $this->exceptionInfos(), 6);

src/JsPhpize/Parser/Parser.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,17 @@ public function parse()
132132
continue;
133133
}
134134
$method = 'visit' . ucfirst($token->type);
135-
if (!method_exists($this, $method)) {
136-
$this->unexpected($token);
135+
$token = method_exists($this, $method)
136+
? $this->$method($token)
137+
: $this->visitNode($token);
138+
if (!is_array($token)) {
139+
$token = array($token);
137140
}
138-
$block->addNodes((array) $this->$method($token));
141+
$block->addNodes($token);
142+
// if (!method_exists($this, $method)) {
143+
// $this->unexpected($token);
144+
// }
145+
// $block->addNodes((array) $this->$method($token));
139146
}
140147
$block->addDependencies($this->dependencies);
141148

src/JsPhpize/Parser/Visitor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ protected function visitComment($token)
1111
return new Comment($token);
1212
}
1313

14-
protected function visitNumber($token)
14+
protected function visitNode($token)
1515
{
16-
return $token->value;
16+
$this->prepend($token);
17+
18+
return $this->getExpression();
1719
}
1820

1921
protected function visitVariable($token)

tests/compile.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function testJsPhpizeGeneration($phpFile, $jsFile)
2727
$expected = file_get_contents($phpFile);
2828
$result = $jsPhpize->compile($jsFile);
2929

30-
$expected = trim($expected);
31-
$actual = trim($result);
30+
$expected = str_replace("\r", '', trim($expected));
31+
$actual = str_replace("\r", '', trim($result));
3232

3333
$this->assertSame($expected, $actual, $jsFile . ' should compile into ' . $expected);
3434
}

tests/render.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function testJsPhpizeGeneration($returnFile, $jsFile)
2727
$expected = file_get_contents($returnFile);
2828
$result = $jsPhpize->render($jsFile);
2929

30-
$expected = trim($expected);
31-
$actual = trim($result);
30+
$expected = str_replace("\r", '', trim($expected));
31+
$actual = str_replace("\r", '', trim($result));
3232

3333
$this->assertSame($expected, $actual, $jsFile . ' should render ' . $expected);
3434
}

0 commit comments

Comments
 (0)