package pfff

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file unit_parsing_php.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
(*s: unit_parsing_php.ml *)
open Common
open OUnit

open Cst_php
module Ast = Cst_php
module Flag = Flag_parsing

(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)

(* old: 
 * Back when the PHP parser was quite fragile we used to do some error
 * recovery in case of a parse error, and instead of failing hard we
 * were returning a NotParsedCorrectly toplevel element. Now
 * we fail hard because the PHP parser is better. So the function below
 * is not useful anymore:
 *
 * let assert_no_parser_error ast = 
 *  assert_bool "bad: have a NotParsedCorrectly" 
 *  (List.for_all (function NotParsedCorrectly _ -> false | _ -> true) ast);
 *  ()
 *)

(*****************************************************************************)
(* Unit tests *)
(*****************************************************************************)

let unittest =
  "parsing_php" >::: [

    (*-----------------------------------------------------------------------*)
    (* Lexing *)
    (*-----------------------------------------------------------------------*)

    "lexing regular code" >:: (fun () ->
      let toks = Parse_php.tokens_of_string "echo 1+2;" in
      assert_bool "it should have a Echo token" 
        (toks |> List.exists (function 
          Parser_php.T_ECHO _ -> true | _ -> false));
    );

    "lexing and case sensitivity" >:: (fun () ->
      let toks = Parse_php.tokens_of_string 
          "function foo() { echo __function__; }" in
      assert_bool "it should have a __FUNCTION__ token" 
        (toks |> List.exists (function 
          Parser_php.T_FUNC_C _ -> true | _ -> false));
    );

    (*-----------------------------------------------------------------------*)
    (* Parsing *)
    (*-----------------------------------------------------------------------*)

    "parsing regular code" >:: (fun () ->
      let _ast = Parse_php.program_of_string "echo 1+2;" in
      ()
    );
    (* had such a bug one day ... *)
    "parsing empty comments" >:: (fun () ->
      let _ast = Parse_php.program_of_string "$a/**/ =1;" in
      ()
    );

    "rejecting bad code" >:: (fun () ->
      Flag.show_parsing_error := false;
      try 
        let _ = Parse_php.program_of_string "echo 1+" in
        assert_failure "it should have thrown a Parse_error exception"
      with
       Parse_php.Parse_error _ -> 
         ()
      (* old:
       * The PHP parser does not return an exception when a PHP file contains
       * an error, to allow some form of error recovery by not stopping 
       * at the first mistake. Instead it returns a NotParsedCorrectly 
       * AST toplevel element for parts of the code that were not parsed.
       * Here we check that correctly formed code do not contain such 
       * NotParsedCorrectly element.
       *
       *  assert_bool "bad: should have a NotParsedCorrectly" 
       * (List.exists (function NotParsedCorrectly _ -> true | _ -> false) ast)
       *)
    );

    "rejecting variadic param with default" >:: (fun () ->
      Flag.show_parsing_error := false;
      try
        let _ = Parse_php.program_of_string "function foo($x, ...$rest=123) {}" in
        assert_failure "it should have thrown a Parse_error exception"
      with
       Parse_php.Parse_error _ ->
         ()
    );

    "rejecting multiple variadic params" >:: (fun () ->
      Flag.show_parsing_error := false;
      try
        let _ = Parse_php.program_of_string "function foo($x, ...$rest, ...$another) {}" in
        assert_failure "it should have thrown a Parse_error exception"
      with
       Parse_php.Parse_error _ ->
         ()
    );
    "rejecting non-tail variadic param without variable name" >:: (fun () ->
      Flag.show_parsing_error := false;
      try
        let _ = Parse_php.program_of_string "function foo($x, ..., ...$rest) {}" in
        assert_failure "it should have thrown a Parse_error exception"
      with
       Parse_php.Parse_error _ ->
         ()
    );

    "rejecting ellipsis with optional constructs" >:: (fun () ->
      Flag.show_parsing_error := false;
      try
        let _ = Parse_php.program_of_string "function foo(int ...) {}" in
        assert_failure "it should have thrown a Parse_error exception"
      with
       Parse_php.Parse_error _ ->
         ()
    );

    "regression files" >:: (fun () ->
      let dir = Filename.concat Config_pfff.path "/tests/php/parsing" in
      let files = Common2.glob (spf "%s/*.php" dir) in
      files |> List.iter (fun file ->
        try
          let _ = Parse_php.parse_program file in
          ()
        with Parse_php.Parse_error _ ->
          assert_failure (spf "it should correctly parse %s" file)
      )
    );

    (*-----------------------------------------------------------------------*)
    (* XHP *)
    (*-----------------------------------------------------------------------*)

    "parsing xhp code" >:: (fun () ->
      (* old: 
       * The PHP parser now understands PHP code containing XHP elements.
       * In the past, pfff would call a preprocessor before parsing a file. By
       * setting the preprocessor to "xhpize", the XHP command line 
       * preprocessor, we could then parse the regular preprocessed code.
       * Now pfff can directly parse XHP code.
       * 
       * Flag_parsing_php.pp_default := Some "xhpize"; 
       *)
      let _ast = Parse_php.program_of_string "return <x:frag />;"  in
      let _ast = Parse_php.program_of_string "return $this->foo()[2];"  in
      ()
    );


    (* XHP was mainly a preprocessor to allow embbeding HTML-like tags in
     * PHP. It also fixes some limitations of the original PHP grammar 
     * regarding array access. You can do foo()['fld'] in XHP, which is 
     * not allowed in PHP (for stupid reasons IMHO).
     * The pfff PHP parser must handle  this syntactic sugar too.
     *)
    "parsing xhp fn_idx sugar code" >:: (fun () ->

      let _ast = Parse_php.program_of_string "return foo()[2];"  in
      (* If the rule is added in the wrong place in the grammar, then
       * the previous test will work but not this one.
       *)
      let _ast = Parse_php.program_of_string "return $this->foo()[2];"  in
      ()
    );

    (*-----------------------------------------------------------------------*)
    (* Types *)
    (*-----------------------------------------------------------------------*)

    "sphp" >:: (fun () ->
      let t x = 
        try
          let _ = Parse_php.program_of_string x in
          ()
        with Parse_php.Parse_error _ ->
          assert_failure (spf "it should correctly parse %s" x)
      in

      t "class A<T> { }";
      t "class A<T1, T2> { }";
      t "trait A<T1, T2> { }";
      t "interface A<T1, T2> { }";
      t "class A<T> extends B<int> { }";
      t "interface A extends B<int>, C {}";
      t "class A { use B<int>; }";
      t "function foo(): int { }";
      t "class A { public function foo(): int { }}";
      t "function foo(mixed $x): int { }";
      t "function foo(): void { }";
      t "function id<T>(T $x): T { return $x; }";
      t "function id((A, B) $x): T { return $x; }";
      t "function id(?(A, B) $x): ?int { return $x; }";
      t "function id( (function(?A) : int) $x): int { return $x; }"; 
      t "function id( (function() : int) $x): int { }"; 
      t "function test(int $x) { return 0; }";
      t "class A { private ?(int, int) $x; }";
      t "class A { const ?A<T1, T2> X = 0; }";
      t "$x = function(): ?int { return null; };";
      t "function foo(A<A<int>> $x): ?int { return null; };";
      t "class A { public static function foo<T>(): ?int { } }";
    );

    (*-----------------------------------------------------------------------*)
    (* Misc *)
    (*-----------------------------------------------------------------------*)

    (* Check that the visitor implementation correctly visit all AST 
     * subelements, even when they are deep inside the AST tree (e.g. 
     * sub-sub expressions inside parenthesis).
     *)
    "visitor" >:: (fun () ->
      let ast = Parse_php.program_of_string "echo 1+2+(3+4);" in

      let cnt = ref 0 in
      (* This is very tricky. See docs/manual/Parsing_php.pdf section 
       * 2.1.2 for a tutorial on visitors in OCaml. *)
      let hooks = { Visitor_php.default_visitor with
        Visitor_php.kexpr = (fun (k, _) e ->
          match e with
          | Sc _ -> incr cnt
          | _ -> k e
        )
      }
      in
      let visitor = Visitor_php.mk_visitor hooks in
      visitor (Program ast);
      assert_equal 4 !cnt ;
    );

    "checking column numbers" >:: (fun () ->
      
      (* See bug reported by dreiss, because the lexer had a few todos
       * regarding objects. *)
      let e = Parse_php.expr_of_string "$o->foo" in
      match e with
      | ObjGet (_v, _tok, Id name) ->
        let info = Ast.info_of_name name in
        assert_equal 4 (Parse_info.col_of_info info)
      | _ -> 
        assert_failure "not good AST"
    );

    (*-----------------------------------------------------------------------*)
    (* Sgrep *)
    (*-----------------------------------------------------------------------*)

    "parsing sgrep expressions" >:: (fun () ->
      
      let _e = Parse_php.any_of_string "debug_rlog(1)" in
      assert_bool "it should not generate an error" true;
      let _e = Parse_php.any_of_string "debug_rlog(X)" in
      assert_bool "it should not generate an error" true;
      let _e = Parse_php.any_of_string "debug_rlog(X, 0)" in
      assert_bool "it should not generate an error" true;

      (try 
        let _e = 
          Common.save_excursion Flag.show_parsing_error false (fun () ->
            Parse_php.any_of_string "debug_rlog(X, 0" 
          ) 
        in
        assert_failure "it should generate an error"
      with _exn ->
        ()
      );
    );

    "parsing sgrep patterns" >:: (fun () ->
      let any = Parse_php.any_of_string "foo();" in
      let ok = match any with Stmt2(ExprStmt( _)) -> true | _ -> false in
      assert_bool "it should be the AST of a statement" ok;
      let any = Parse_php.any_of_string "foo()" in
      let ok = match any with Expr(_) -> true | _ -> false in
      assert_bool "it should be the AST of an expression" ok;
      let any = Parse_php.any_of_string "<x:frag>x</x:frag>" in
      let ok = match any with Expr(_) -> true | _ -> false in
      assert_bool "it should be the AST of an expression" ok;

    );

  (* todo: 
   *  - ? sexp and json output
   *  - ? correctness of Ast (too many cases)
   *)
  ]
(*e: unit_parsing_php.ml *)
OCaml

Innovation. Community. Security.