Skip to content

Commit f78bc60

Browse files
davidoffermansergeyklay
authored andcommitted
Tests for zephir parser (#76)
1 parent ca800dc commit f78bc60

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2507
-12
lines changed

parser.mk

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,15 @@ maintainer-clean:
3838

3939
$(srcdir)/parser/scanner.c: $(srcdir)/parser/scanner.re
4040
$(RE2C) $(RE2C_FLAGS) -d --no-generation-date -o $@ $<
41-
$(SED) s/"#line \([[:digit:]]\+\) \(.*\)\/\(parser\/\)\(.*\)\""/"#line \1 \"\3\4\""/g $@ > $@.tmp && mv -f $@.tmp $@
4241

4342
$(srcdir)/parser/lemon: $(srcdir)/parser/lemon.c
4443
$(CC) $< -o $@
4544

4645
$(srcdir)/parser/parser.c: $(srcdir)/parser/zephir.c $(srcdir)/parser/base.c
4746
@echo "#include <php.h>" > $@
4847
cat $< >> $@
49-
echo "#line 1 \"parser/base.c\"" >> $@
48+
echo "#line 1 \"$(top_srcdir)/parser/base.c\"" >> $@
5049
cat $(top_srcdir)/parser/base.c >> $@
51-
$(SED) s/"#line \([[:digit:]]\+\) \(.*\)\/\(parser\/\)\(.*\)\""/"#line \1 \"\3\4\""/g $@ > $@.tmp && mv -f $@.tmp $@
5250

5351
$(srcdir)/parser/zephir.c: $(srcdir)/parser/zephir.lemon $(srcdir)/parser/lemon
5452
$(top_srcdir)/parser/lemon $<

parser/scanner.re

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,12 @@ int xx_get_token(xx_scanner_state *s, xx_scanner_token *token) {
853853
return 0;
854854
}
855855

856+
"<>" {
857+
s->active_char += 2;
858+
token->opcode = XX_T_NOTEQUALS;
859+
return 0;
860+
}
861+
856862
"===" {
857863
s->active_char += 3;
858864
token->opcode = XX_T_IDENTICAL;

parser/zephir.lemon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,10 @@ xx_for_statement(R) ::= FOR IDENTIFIER(V) IN REVERSE xx_common_expr(E) BRACKET_O
12311231
xx_ret_for_statement(&R, &E, NULL, V, 1, &L, status->scanner_state);
12321232
}
12331233

1234+
xx_for_statement(R) ::= FOR IDENTIFIER(V) IN REVERSE xx_common_expr(E) BRACKET_OPEN BRACKET_CLOSE . {
1235+
xx_ret_for_statement(&R, &E, NULL, V, 1, NULL, status->scanner_state);
1236+
}
1237+
12341238
xx_for_statement(R) ::= FOR IDENTIFIER(K) COMMA IDENTIFIER(V) IN xx_common_expr(E) BRACKET_OPEN xx_statement_list(L) BRACKET_CLOSE . {
12351239
xx_ret_for_statement(&R, &E, K, V, 0, &L, status->scanner_state);
12361240
}

tests/base/declare.phpt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
Tests variable declarations
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test() {
9+
int foo = 10;
10+
uint foo = 10;
11+
long foo = 10;
12+
ulong foo = 10;
13+
char foo = 'a';
14+
uchar foo = 'a';
15+
double foo = 10.00;
16+
float foo = 10.00;
17+
bool foo = true;
18+
boolean foo = true;
19+
string foo = "foobar";
20+
array foo = [10,20,30];
21+
var foo = 10;
22+
}
23+
ZEP;
24+
25+
$ir = zephir_parse_file($code, '(eval code)');
26+
foreach ($ir[0]["statements"] as $statement) {
27+
printf("%s %s %s %s %s\n",
28+
$statement["type"],
29+
$statement["data-type"],
30+
$statement["variables"][0]["variable"],
31+
$statement["variables"][0]["expr"]["type"],
32+
$statement["variables"][0]["expr"]["value"] ?? "-"
33+
);
34+
}
35+
?>
36+
--EXPECT--
37+
declare int foo int 10
38+
declare uint foo int 10
39+
declare long foo int 10
40+
declare ulong foo int 10
41+
declare char foo char a
42+
declare uchar foo char a
43+
declare double foo double 10.00
44+
declare double foo double 10.00
45+
declare bool foo bool true
46+
declare bool foo bool true
47+
declare string foo string foobar
48+
declare array foo array -
49+
declare variable foo int 10

tests/base/literals.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Tests literal values
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test() {
9+
let a = null;
10+
let a = NULL;
11+
12+
let a = true;
13+
let a = TRUE;
14+
15+
let a = false;
16+
let a = FALSE;
17+
}
18+
ZEP;
19+
20+
$ir = zephir_parse_file($code, '(eval code)');
21+
foreach ($ir[0]["statements"] as $statement) {
22+
printf("%s %s\n",
23+
$statement["assignments"][0]["expr"]["type"],
24+
$statement["assignments"][0]["expr"]["value"] ?? "-"
25+
);
26+
}
27+
?>
28+
--EXPECT--
29+
null -
30+
null -
31+
bool true
32+
bool true
33+
bool false
34+
bool false

tests/cf/do-while.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
do-while control statement
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test() {
9+
do { } while true;
10+
}
11+
ZEP;
12+
13+
$ir = zephir_parse_file($code, '(eval code)');
14+
foreach ($ir[0]["statements"] as $statement) {
15+
printf("%s %s %s\n",
16+
$statement["type"],
17+
$statement["expr"]["type"],
18+
$statement["expr"]["value"]
19+
);
20+
}
21+
?>
22+
--EXPECT--
23+
do-while bool true

tests/cf/for.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
For control statement
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test() {
9+
for item in arr { }
10+
for key, value in arr { }
11+
for item in reverse arr { }
12+
}
13+
ZEP;
14+
15+
$ir = zephir_parse_file($code, '(eval code)');
16+
foreach ($ir[0]["statements"] as $statement) {
17+
printf("%s %s %s %s %s %d\n",
18+
$statement["type"],
19+
$statement["key"] ?? "-",
20+
$statement["value"],
21+
$statement["expr"]["type"],
22+
$statement["expr"]["value"],
23+
$statement["reverse"]
24+
);
25+
}
26+
?>
27+
--EXPECT--
28+
for - item variable arr 0
29+
for key value variable arr 0
30+
for - item variable arr 1

tests/cf/if.phpt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
IF control statement
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test() {
9+
if true { }
10+
11+
if true {
12+
let a = 1;
13+
} else {
14+
let a = 2;
15+
}
16+
17+
if true {
18+
let a = 1;
19+
} elseif false {
20+
let a = 2;
21+
} elseif true {
22+
let a = 3;
23+
} else {
24+
let a = 4;
25+
}
26+
}
27+
ZEP;
28+
29+
$ir = zephir_parse_file($code, '(eval code)');
30+
foreach ($ir[0]["statements"] as $statement) {
31+
printf("%s %s %s %d %d %d\n",
32+
$statement["type"],
33+
$statement["expr"]["type"],
34+
$statement["expr"]["value"],
35+
count($statement["statements"] ?? []),
36+
count($statement["elseif_statements"] ?? []),
37+
count($statement["else_statements"] ?? [])
38+
);
39+
40+
if (isset($statement["elseif_statements"])) {
41+
foreach ($statement["elseif_statements"] as $elif) {
42+
printf("elseif %s %s %s %d\n",
43+
$elif["type"],
44+
$elif["expr"]["type"],
45+
$elif["expr"]["value"],
46+
count($elif["statements"] ?? [])
47+
);
48+
}
49+
}
50+
}
51+
?>
52+
--EXPECT--
53+
if bool true 0 0 0
54+
if bool true 1 0 1
55+
if bool true 1 2 1
56+
elseif if bool false 1
57+
elseif if bool true 1

tests/cf/loop.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Loop control statement
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test() {
9+
let n = 40;
10+
11+
loop { }
12+
13+
loop {
14+
let n -= 2;
15+
if n % 5 == 0 { break; }
16+
echo x, "\n";
17+
}
18+
}
19+
ZEP;
20+
21+
$ir = zephir_parse_file($code, '(eval code)');
22+
foreach ($ir[0]["statements"] as $statement) {
23+
if($statement["type"] != "loop") {
24+
continue;
25+
}
26+
27+
printf("%s %s\n",
28+
$statement["type"],
29+
count($statement["statements"] ?? [])
30+
);
31+
}
32+
?>
33+
--EXPECT--
34+
loop 0
35+
loop 3

tests/cf/switch.phpt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--TEST--
2+
Switch control statement
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test() {
9+
var a;
10+
11+
switch 3 {
12+
case 1:
13+
case 2:
14+
let a = "foobar";
15+
break;
16+
case 3:
17+
let a = "baz";
18+
break;
19+
default:
20+
let a = "biz";
21+
break;
22+
}
23+
24+
switch foobar {
25+
case "hello":
26+
case hello:
27+
case HELLO:
28+
default:
29+
break;
30+
}
31+
}
32+
ZEP;
33+
34+
$ir = zephir_parse_file($code, '(eval code)');
35+
foreach ($ir[0]["statements"] as $statement) {
36+
if($statement["type"] != "switch") {
37+
continue;
38+
}
39+
40+
printf("%s %s %s\n",
41+
$statement["type"],
42+
$statement["expr"]["type"],
43+
$statement["expr"]["value"]
44+
);
45+
46+
foreach ($statement["clauses"] as $clause) {
47+
printf("%s %s %s %s\n",
48+
$clause["type"],
49+
$clause["expr"]["type"] ?? "-",
50+
$clause["expr"]["value"] ?? "-",
51+
count($clause["statements"] ?? [])
52+
);
53+
}
54+
}
55+
?>
56+
--EXPECT--
57+
switch int 3
58+
case int 1 0
59+
case int 2 2
60+
case int 3 2
61+
default - - 2
62+
switch variable foobar
63+
case string hello 0
64+
case variable hello 0
65+
case constant HELLO 0
66+
default - - 1

tests/cf/try-catch01.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
try-catch control statement without catch
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test() {
9+
try { }
10+
}
11+
ZEP;
12+
13+
$ir = zephir_parse_file($code, '(eval code)');
14+
var_dump($ir[0]["statements"][0]);
15+
?>
16+
--EXPECT--
17+
array(4) {
18+
["type"]=>
19+
string(9) "try-catch"
20+
["file"]=>
21+
string(11) "(eval code)"
22+
["line"]=>
23+
int(3)
24+
["char"]=>
25+
int(1)
26+
}

0 commit comments

Comments
 (0)