Skip to content

Commit e7c162b

Browse files
evilebottnawialexander-akait
authored andcommitted
refactor: dereferencable_scalar (#396)
1 parent 628cd8f commit e7c162b

File tree

3 files changed

+217
-32
lines changed

3 files changed

+217
-32
lines changed

src/parser/scalar.js

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,17 @@ module.exports = {
4242
}
4343
);
4444
},
45+
4546
/**
46-
* ```ebnf
47-
* scalar ::= T_MAGIC_CONST
48-
* | T_LNUMBER | T_DNUMBER
49-
* | T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE? T_END_HEREDOC
50-
* | '"' encaps_list '"'
51-
* | T_START_HEREDOC encaps_list T_END_HEREDOC
52-
* | namespace_name (T_DOUBLE_COLON T_STRING)?
53-
* ```
47+
* Reads dereferencable scalar
5448
*/
55-
read_scalar: function() {
56-
if (this.is("T_MAGIC_CONST")) {
57-
return this.get_magic_constant();
58-
} else {
59-
let value, node;
60-
switch (this.token) {
61-
// TEXTS
62-
case this.tok.T_CONSTANT_ENCAPSED_STRING: {
63-
value = this.node("string");
49+
read_dereferencable_scalar: function() {
50+
let result = null;
51+
52+
switch (this.token) {
53+
case this.tok.T_CONSTANT_ENCAPSED_STRING:
54+
{
55+
let value = this.node("string");
6456
const text = this.text();
6557
let offset = 0;
6658
if (text[0] === "b" || text[0] === "B") {
@@ -80,12 +72,48 @@ module.exports = {
8072
);
8173
if (this.token === this.tok.T_DOUBLE_COLON) {
8274
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1151
83-
return this.read_static_getter(value);
75+
result = this.read_static_getter(value);
8476
} else {
8577
// dirrect string
86-
return value;
78+
result = value;
8779
}
8880
}
81+
break;
82+
case this.tok.T_ARRAY: // array parser
83+
result = this.read_array();
84+
break;
85+
case "[": // short array format
86+
result = this.read_array();
87+
break;
88+
}
89+
90+
return result;
91+
},
92+
93+
/**
94+
* ```ebnf
95+
* scalar ::= T_MAGIC_CONST
96+
* | T_LNUMBER | T_DNUMBER
97+
* | T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE? T_END_HEREDOC
98+
* | '"' encaps_list '"'
99+
* | T_START_HEREDOC encaps_list T_END_HEREDOC
100+
* | namespace_name (T_DOUBLE_COLON T_STRING)?
101+
* ```
102+
*/
103+
read_scalar: function() {
104+
if (this.is("T_MAGIC_CONST")) {
105+
return this.get_magic_constant();
106+
} else {
107+
let value, node;
108+
switch (this.token) {
109+
// NUMERIC
110+
case this.tok.T_LNUMBER: // long
111+
case this.tok.T_DNUMBER: { // double
112+
const result = this.node("number");
113+
value = this.text();
114+
this.next();
115+
return result(value, null);
116+
}
89117
case this.tok.T_START_HEREDOC:
90118
if (this.lexer.curCondition === "ST_NOWDOC") {
91119
const start = this.lexer.yylloc.first_offset;
@@ -130,21 +158,11 @@ module.exports = {
130158
return this.read_encapsed_string('"', true);
131159
}
132160

133-
// NUMERIC
134-
case this.tok.T_LNUMBER: // long
135-
case this.tok.T_DNUMBER: {
136-
// double
137-
const result = this.node("number");
138-
value = this.text();
139-
this.next();
140-
return result(value, null);
141-
}
142-
143-
// ARRAYS
161+
// TEXTS
162+
case this.tok.T_CONSTANT_ENCAPSED_STRING:
144163
case this.tok.T_ARRAY: // array parser
145-
return this.read_array();
146164
case "[": // short array format
147-
return this.read_array();
165+
return this.read_dereferencable_scalar();
148166
default: {
149167
const err = this.error("SCALAR");
150168
// graceful mode : ignore token & return error node

test/snapshot/__snapshots__/scalar.test.js.snap

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Test scalar statements test constants #2 1`] = `
4+
Program {
5+
"children": Array [
6+
ExpressionStatement {
7+
"expression": Assign {
8+
"kind": "assign",
9+
"left": Variable {
10+
"curly": false,
11+
"kind": "variable",
12+
"name": "a",
13+
},
14+
"operator": "=",
15+
"right": ClassReference {
16+
"kind": "classreference",
17+
"name": "Foo",
18+
"resolution": "uqn",
19+
},
20+
},
21+
"kind": "expressionstatement",
22+
},
23+
],
24+
"errors": Array [],
25+
"kind": "program",
26+
}
27+
`;
28+
29+
exports[`Test scalar statements test constants #2 2`] = `
30+
Program {
31+
"children": Array [
32+
ExpressionStatement {
33+
"expression": Assign {
34+
"kind": "assign",
35+
"left": Variable {
36+
"curly": false,
37+
"kind": "variable",
38+
"name": "a",
39+
},
40+
"operator": "=",
41+
"right": StaticLookup {
42+
"kind": "staticlookup",
43+
"offset": Identifier {
44+
"kind": "identifier",
45+
"name": "foo",
46+
},
47+
"what": Variable {
48+
"curly": false,
49+
"kind": "variable",
50+
"name": "var",
51+
},
52+
},
53+
},
54+
"kind": "expressionstatement",
55+
},
56+
],
57+
"errors": Array [],
58+
"kind": "program",
59+
}
60+
`;
61+
362
exports[`Test scalar statements test constants 1`] = `
463
Program {
564
"children": Array [
@@ -221,3 +280,93 @@ Program {
221280
"kind": "program",
222281
}
223282
`;
283+
284+
exports[`Test scalar statements test dereferencable_scalar #2 1`] = `
285+
Program {
286+
"children": Array [
287+
ExpressionStatement {
288+
"expression": Assign {
289+
"kind": "assign",
290+
"left": Variable {
291+
"curly": false,
292+
"kind": "variable",
293+
"name": "var",
294+
},
295+
"operator": "=",
296+
"right": Array {
297+
"items": Array [
298+
Number {
299+
"kind": "number",
300+
"value": "1",
301+
},
302+
],
303+
"kind": "array",
304+
"shortForm": true,
305+
},
306+
},
307+
"kind": "expressionstatement",
308+
},
309+
],
310+
"errors": Array [],
311+
"kind": "program",
312+
}
313+
`;
314+
315+
exports[`Test scalar statements test dereferencable_scalar #3 1`] = `
316+
Program {
317+
"children": Array [
318+
ExpressionStatement {
319+
"expression": Assign {
320+
"kind": "assign",
321+
"left": Variable {
322+
"curly": false,
323+
"kind": "variable",
324+
"name": "var",
325+
},
326+
"operator": "=",
327+
"right": String {
328+
"isDoubleQuote": true,
329+
"kind": "string",
330+
"raw": "\\"test\\"",
331+
"unicode": false,
332+
"value": "test",
333+
},
334+
},
335+
"kind": "expressionstatement",
336+
},
337+
],
338+
"errors": Array [],
339+
"kind": "program",
340+
}
341+
`;
342+
343+
exports[`Test scalar statements test dereferencable_scalar 1`] = `
344+
Program {
345+
"children": Array [
346+
ExpressionStatement {
347+
"expression": Assign {
348+
"kind": "assign",
349+
"left": Variable {
350+
"curly": false,
351+
"kind": "variable",
352+
"name": "var",
353+
},
354+
"operator": "=",
355+
"right": Array {
356+
"items": Array [
357+
Number {
358+
"kind": "number",
359+
"value": "1",
360+
},
361+
],
362+
"kind": "array",
363+
"shortForm": false,
364+
},
365+
},
366+
"kind": "expressionstatement",
367+
},
368+
],
369+
"errors": Array [],
370+
"kind": "program",
371+
}
372+
`;

test/snapshot/scalar.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ describe("Test scalar statements", function() {
55
expect(parser.parseEval('$a = foo::ref[-5];')).toMatchSnapshot();
66
});
77

8+
it("test constants #2", function() {
9+
expect(parser.parseEval('$a = Foo;')).toMatchSnapshot();
10+
});
11+
12+
it("test constants #2", function() {
13+
expect(parser.parseEval('$a = $var::foo;')).toMatchSnapshot();
14+
});
15+
816
it("test dereferencable", function() {
917
expect(parser.parseEval(`
1018
$a = foo::bar()[5]->test;
@@ -13,4 +21,14 @@ describe("Test scalar statements", function() {
1321
$d = (function($a) { return $a * 2; })(5);
1422
`)).toMatchSnapshot();
1523
});
24+
25+
it("test dereferencable_scalar", function() {
26+
expect(parser.parseEval('$var = array(1);')).toMatchSnapshot();
27+
});
28+
it("test dereferencable_scalar #2", function() {
29+
expect(parser.parseEval('$var = [1];')).toMatchSnapshot();
30+
});
31+
it("test dereferencable_scalar #3", function() {
32+
expect(parser.parseEval('$var = "test";')).toMatchSnapshot();
33+
});
1634
});

0 commit comments

Comments
 (0)