Skip to content

Commit 6c83b17

Browse files
committed
glayzzle#171 - export cast node generator as a function in order to make it extensible
1 parent fe0ee7c commit 6c83b17

File tree

3 files changed

+151
-14
lines changed

3 files changed

+151
-14
lines changed

src/parser/expr.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ module.exports = {
8585
return expr;
8686
},
8787

88+
/**
89+
* Reads a cast expression
90+
*/
91+
read_expr_cast: function(type) {
92+
return this.node("cast")(type, this.text(), this.next().read_expr());
93+
},
94+
8895
/**
8996
* ```ebnf
9097
* Reads an expression
@@ -246,33 +253,27 @@ module.exports = {
246253
return result(expr);
247254

248255
case this.tok.T_INT_CAST:
249-
return this.node("cast")("int", this.text(), this.next().read_expr());
256+
return this.read_expr_cast("int");
250257

251258
case this.tok.T_DOUBLE_CAST:
252-
return this.node("cast")("float", this.text(), this.next().read_expr());
259+
return this.read_expr_cast("float");
253260

254261
case this.tok.T_STRING_CAST:
255-
return this.node("cast")(
256-
this.text().indexOf("binary") !== -1 ? "binary" : "string",
257-
this.text(),
258-
this.next().read_expr()
262+
return this.read_expr_cast(
263+
this.text().indexOf("binary") !== -1 ? "binary" : "string"
259264
);
260265

261266
case this.tok.T_ARRAY_CAST:
262-
return this.node("cast")("array", this.text(), this.next().read_expr());
267+
return this.read_expr_cast("array");
263268

264269
case this.tok.T_OBJECT_CAST:
265-
return this.node("cast")(
266-
"object",
267-
this.text(),
268-
this.next().read_expr()
269-
);
270+
return this.read_expr_cast("object");
270271

271272
case this.tok.T_BOOL_CAST:
272-
return this.node("cast")("bool", this.text(), this.next().read_expr());
273+
return this.read_expr_cast("bool");
273274

274275
case this.tok.T_UNSET_CAST:
275-
return this.node("cast")("unset", this.text(), this.next().read_expr());
276+
return this.read_expr_cast("unset");
276277

277278
case this.tok.T_EXIT: {
278279
const useDie = this.lexer.yytext.toLowerCase() === "die";

test/snapshot/__snapshots__/expr.test.js.snap

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,111 @@ Program {
618618
}
619619
`;
620620

621+
exports[`Test expressions test cast extension - #171 1`] = `
622+
Program {
623+
"children": Array [
624+
Variable {
625+
"byref": false,
626+
"cast": "int",
627+
"curly": false,
628+
"kind": "variable",
629+
"name": "var",
630+
"rawCast": "(int)",
631+
},
632+
Variable {
633+
"byref": false,
634+
"cast": "int",
635+
"curly": false,
636+
"kind": "variable",
637+
"name": "var",
638+
"rawCast": "(integer)",
639+
},
640+
Variable {
641+
"byref": false,
642+
"cast": "bool",
643+
"curly": false,
644+
"kind": "variable",
645+
"name": "var",
646+
"rawCast": "(bool)",
647+
},
648+
Variable {
649+
"byref": false,
650+
"cast": "bool",
651+
"curly": false,
652+
"kind": "variable",
653+
"name": "var",
654+
"rawCast": "(boolean)",
655+
},
656+
Variable {
657+
"byref": false,
658+
"cast": "float",
659+
"curly": false,
660+
"kind": "variable",
661+
"name": "var",
662+
"rawCast": "(float)",
663+
},
664+
Variable {
665+
"byref": false,
666+
"cast": "float",
667+
"curly": false,
668+
"kind": "variable",
669+
"name": "var",
670+
"rawCast": "(double)",
671+
},
672+
Variable {
673+
"byref": false,
674+
"cast": "float",
675+
"curly": false,
676+
"kind": "variable",
677+
"name": "var",
678+
"rawCast": "(real)",
679+
},
680+
Variable {
681+
"byref": false,
682+
"cast": "string",
683+
"curly": false,
684+
"kind": "variable",
685+
"name": "var",
686+
"rawCast": "(string)",
687+
},
688+
Variable {
689+
"byref": false,
690+
"cast": "binary",
691+
"curly": false,
692+
"kind": "variable",
693+
"name": "var",
694+
"rawCast": "(binary)",
695+
},
696+
Variable {
697+
"byref": false,
698+
"cast": "array",
699+
"curly": false,
700+
"kind": "variable",
701+
"name": "var",
702+
"rawCast": "(array)",
703+
},
704+
Variable {
705+
"byref": false,
706+
"cast": "object",
707+
"curly": false,
708+
"kind": "variable",
709+
"name": "var",
710+
"rawCast": "(object)",
711+
},
712+
Variable {
713+
"byref": false,
714+
"cast": "unset",
715+
"curly": false,
716+
"kind": "variable",
717+
"name": "var",
718+
"rawCast": "(unset)",
719+
},
720+
],
721+
"errors": Array [],
722+
"kind": "program",
723+
}
724+
`;
725+
621726
exports[`Test expressions test exit 1`] = `
622727
Program {
623728
"children": Array [

test/snapshot/expr.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,37 @@ describe("Test expressions", function() {
126126
expect(ast).toMatchSnapshot();
127127
});
128128

129+
130+
it("test cast extension - #171", function() {
131+
const ast = parser.parseEval(
132+
`
133+
(int)$var;
134+
(integer)$var;
135+
(bool)$var;
136+
(boolean)$var;
137+
(float)$var;
138+
(double)$var;
139+
(real)$var;
140+
(string)$var;
141+
(binary)$var;
142+
(array)$var;
143+
(object)$var;
144+
(unset)$var;
145+
`, {
146+
parser: {
147+
read_expr_cast: function(cast) {
148+
const rawCast = this.text();
149+
const expr = this.next().read_expr();
150+
expr.cast = cast;
151+
expr.rawCast = rawCast;
152+
return expr;
153+
}
154+
}
155+
}
156+
);
157+
expect(ast).toMatchSnapshot();
158+
});
159+
129160
it("test exit", function() {
130161
const ast = parser.parseEval(
131162
`

0 commit comments

Comments
 (0)