Skip to content

Commit c588c27

Browse files
committed
Improve keyword disambiguation
1 parent 505d1c7 commit c588c27

File tree

6 files changed

+50
-24
lines changed

6 files changed

+50
-24
lines changed

examples/comment-property.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Issue #12 test
2+
foo = {comment: 'fix'}
3+
return foo.comment

examples/comment-property.return

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix

src/JsPhpize/Lexer/Token.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,56 @@ public function is($value)
2121
return in_array($value, array($this->type, $this->value));
2222
}
2323

24+
protected function typeIn($values)
25+
{
26+
return in_array($this->type, $values);
27+
}
28+
29+
protected function valueIn($values)
30+
{
31+
return in_array($this->value, $values);
32+
}
33+
2434
public function isIn($values)
2535
{
2636
$values = is_array($values) ? $values : func_get_args();
2737

28-
return in_array($this->type, $values) || in_array($this->value, $values);
38+
return $this->typeIn($values) || $this->valueIn($values);
2939
}
3040

3141
public function isValue()
3242
{
33-
return in_array($this->type, array('variable', 'constant', 'string', 'number'));
43+
return $this->typeIn(array('variable', 'constant', 'string', 'number'));
3444
}
3545

3646
protected function isComparison()
3747
{
38-
return in_array($this->type, array('===', '!==', '>=', '<=', '<>', '!=', '==', '>', '<'));
48+
return $this->typeIn(array('===', '!==', '>=', '<=', '<>', '!=', '==', '>', '<'));
3949
}
4050

4151
protected function isLogical()
4252
{
43-
return in_array($this->type, array('&&', '||', '!'));
53+
return $this->typeIn(array('&&', '||', '!'));
4454
}
4555

4656
protected function isBinary()
4757
{
48-
return in_array($this->type, array('&', '|', '^', '~', '>>', '<<', '>>>'));
58+
return $this->typeIn(array('&', '|', '^', '~', '>>', '<<', '>>>'));
4959
}
5060

5161
protected function isArithmetic()
5262
{
53-
return in_array($this->type, array('+', '-', '/', '*', '%', '**', '--', '++'));
63+
return $this->typeIn(array('+', '-', '/', '*', '%', '**', '--', '++'));
5464
}
5565

5666
protected function isVarOperator()
5767
{
58-
return in_array($this->type, array('delete', 'void', 'typeof'));
68+
return $this->typeIn(array('delete', 'void', 'typeof'));
69+
}
70+
71+
public function isLeftHandOperator()
72+
{
73+
return $this->typeIn(array('~', '!', '--', '++', '-', '+')) || $this->isVarOperator();
5974
}
6075

6176
public function isAssignation()
@@ -70,14 +85,19 @@ public function isOperator()
7085

7186
public function isNeutral()
7287
{
73-
return $this->isIn('comment', 'newline');
88+
return $this->typeIn(array('comment', 'newline'));
7489
}
7590

7691
public function expectNoLeftMember()
7792
{
7893
return in_array($this->type, array('!', '~')) || $this->isVarOperator();
7994
}
8095

96+
public function isFunction()
97+
{
98+
return $this->type === 'function' || $this->type === 'keyword' && $this->value === 'function';
99+
}
100+
81101
public function __get($key)
82102
{
83103
return isset($this->data[$key]) ? $this->data[$key] : null;

src/JsPhpize/Parser/BracketsArrayItemKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function parseTypeAndValue()
3131
{
3232
$token = $this->token;
3333

34-
if ($token->is('keyword')) {
34+
if ($token->type === 'keyword') {
3535
return $this->getStringExport($token->value);
3636
}
3737

src/JsPhpize/Parser/Parser.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected function parseParentheses()
7979
while ($token = $this->next()) {
8080
if ($token->is(')')) {
8181
$next = $this->get(0);
82-
if ($next && $next->is('lambda')) {
82+
if ($next && $next->type === 'lambda') {
8383
$this->skip();
8484

8585
return $this->parseLambda($parentheses);
@@ -170,7 +170,7 @@ protected function parseVariable($name)
170170
{
171171
$children = array();
172172
while ($next = $this->get(0)) {
173-
if ($next->is('lambda')) {
173+
if ($next->type === 'lambda') {
174174
$this->skip();
175175
$parenthesis = new Parenthesis();
176176
$parenthesis->addNode(new Variable($name, $children));
@@ -223,7 +223,7 @@ protected function parseTernary(Node $condition)
223223

224224
protected function parseValue($token)
225225
{
226-
return $token->is('variable')
226+
return $token->type === 'variable'
227227
? $this->parseVariable($token->value)
228228
: new Constant($token->type, $token->value);
229229
}
@@ -232,7 +232,7 @@ protected function parseFunction($token)
232232
{
233233
$function = new Block('function');
234234
$token = $this->get(0);
235-
if ($token->is('variable')) {
235+
if ($token->type === 'variable') {
236236
$this->skip();
237237
$token = $this->get(0);
238238
}
@@ -290,7 +290,7 @@ protected function parseKeyword($token)
290290
protected function parseLet($token)
291291
{
292292
$letVariable = $this->get(0);
293-
if (!$letVariable->is('variable')) {
293+
if ($letVariable->type !== 'variable') {
294294
throw $this->unexpected($letVariable, $token);
295295
}
296296

@@ -304,12 +304,14 @@ protected function parseInstructions($block)
304304
if ($token->is($endToken)) {
305305
break;
306306
}
307-
if ($token->isIn('var', 'const')) {
308-
continue;
309-
}
310-
if ($token->is('let')) {
311-
$block->let($this->parseLet($token));
312-
continue;
307+
if ($token->type === 'keyword') {
308+
if ($token->isIn('var', 'const')) {
309+
continue;
310+
}
311+
if ($token->value === 'let') {
312+
$block->let($this->parseLet($token));
313+
continue;
314+
}
313315
}
314316
if ($instruction = $this->getInstructionFromToken($token)) {
315317
$block->addInstruction($instruction);

src/JsPhpize/Parser/TokenExtractor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function getVariableChildFromToken($token)
3535
$this->skip();
3636
$token = $this->next();
3737

38-
if ($token->is('variable')) {
38+
if ($token && $token->type === 'variable') {
3939
return new Constant('string', var_export($token->value, true));
4040
}
4141

@@ -68,7 +68,7 @@ protected function getEndTokenFromBlock($block)
6868

6969
protected function getInstructionFromToken($token)
7070
{
71-
if ($token->is('keyword')) {
71+
if ($token->type === 'keyword') {
7272
return $this->parseKeyword($token);
7373
}
7474

@@ -107,7 +107,7 @@ protected function handleParentheses($keyword, $afterKeyword)
107107

108108
protected function getInitialValue($token)
109109
{
110-
if ($token->is('function')) {
110+
if ($token->isFunction()) {
111111
return $this->parseFunction($token);
112112
}
113113
if ($token->is('(')) {
@@ -119,7 +119,7 @@ protected function getInitialValue($token)
119119
if ($token->is('{')) {
120120
return $this->parseBracketsArray();
121121
}
122-
if ($token->isOperator() && $token->isIn('~', '!', '--', '++', '-', '+', 'delete', 'typeof', 'void')) {
122+
if ($token->isLeftHandOperator()) {
123123
$value = $this->expectValue($this->next(), $token);
124124
$value->prepend($token->type);
125125

0 commit comments

Comments
 (0)