package pfff

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

Source file parse_fuzzy.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
(* Yoann Padioleau
 *
 * Copyright (C) 2019 r2c
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation, with the
 * special exception on linking described in file license.txt.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
 * license.txt for more details.
 *)
open Common

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* Wrappers around many languages to transform them in a fuzzy AST
 * (see ast_fuzzy.ml)
 *)

(*****************************************************************************)
(* Entry point *)
(*****************************************************************************)

(* This is similar to what I did for OPA. This is also similar
 * to what I do for parsing hacks for C++, but this fuzzy AST can be useful
 * on its own, e.g. for a not too bad sgrep/spatch.
 *)

let parse_and_tokens_with_lang lang file =
  match lang with
  | Lang_fuzzy.PHP ->
     let toks = Parse_php.tokens file |> (fun toks ->
            if !Flag_parsing.sgrep_mode
            then Common.exclude Token_helpers_php.is_eof toks
            else toks)
     in
     let hooks = { Lib_ast_fuzzy.
       tokf = Token_helpers_php.info_of_tok;
       kind = Token_helpers_php.token_kind_of_tok;
     } in
     Lib_ast_fuzzy.mk_trees hooks toks, Lib_ast_fuzzy.mk_tokens hooks toks

  | Lang_fuzzy.ML ->
    let toks = Parse_ml.tokens file  |> (fun toks ->
            if !Flag_parsing.sgrep_mode
            then Common.exclude Token_helpers_ml.is_eof toks
            else toks)
    in
    let hooks = { Lib_ast_fuzzy.
     tokf = Token_helpers_ml.info_of_tok;
     kind = Token_helpers_ml.token_kind_of_tok;
    } in
    Lib_ast_fuzzy.mk_trees hooks toks, Lib_ast_fuzzy.mk_tokens hooks toks
  | Lang_fuzzy.Skip ->
    let toks = Parse_skip.tokens file  |> (fun toks ->
            if !Flag_parsing.sgrep_mode
            then Common.exclude Token_helpers_skip.is_eof toks
            else toks)
    in
    let hooks = { Lib_ast_fuzzy.
     tokf = Token_helpers_skip.info_of_tok;
     kind = Token_helpers_skip.token_kind_of_tok;
    } in
    Lib_ast_fuzzy.mk_trees hooks toks, Lib_ast_fuzzy.mk_tokens hooks toks
  | Lang_fuzzy.Java ->
    let toks = Parse_java.tokens file  |> (fun toks ->
            if !Flag_parsing.sgrep_mode
            then Common.exclude Token_helpers_java.is_eof toks
            else toks)
    in
    let hooks = { Lib_ast_fuzzy.
     tokf = Token_helpers_java.info_of_tok;
     kind = Token_helpers_java.token_kind_of_tok;
    } in
    Lib_ast_fuzzy.mk_trees hooks toks, Lib_ast_fuzzy.mk_tokens hooks toks
  | Lang_fuzzy.Javascript ->
    let toks = Parse_js.tokens file  |> (fun toks ->
            if !Flag_parsing.sgrep_mode
            then Common.exclude Token_helpers_js.is_eof toks
            else toks)
    in
    let hooks = { Lib_ast_fuzzy.
     tokf = Token_helpers_js.info_of_tok;
     kind = Token_helpers_js.token_kind_of_tok;
    } in
    Lib_ast_fuzzy.mk_trees hooks toks, Lib_ast_fuzzy.mk_tokens hooks toks
  | Lang_fuzzy.Cpp ->
    Common.save_excursion Flag_parsing.verbose_lexing false (fun () ->
      Parse_cpp.parse_fuzzy file
    )

let parse_with_lang lang file =
  parse_and_tokens_with_lang lang file |> fst

let parse file =
  match Lang_fuzzy.lang_of_filename_opt file with
  | Some lang -> parse_with_lang lang file
  | None -> failwith (spf "unsupported file for fuzzy AST: %s" file)

let parse_pattern lang str =
  Common.save_excursion Flag_parsing.sgrep_mode true (fun () ->
   Common2.with_tmp_file ~str ~ext:"xx" (fun tmpfile ->
    parse_with_lang lang tmpfile
  ))
OCaml

Innovation. Community. Security.