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
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
(* Yoann Padioleau
 *
 * Copyright (C) 2013 Facebook
 *
 * 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

module PI = Parse_info

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* A few helpers function to build Ast_fuzzy tree from a list of tokens.
 * It factorizes the language-independent part of those AST fuzzy builder.
 *)

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

type 'tok hooks = {
  kind: 'tok -> Parse_info.token_kind;
  tokf: 'tok -> Parse_info.info;
}

exception Unclosed of string * Parse_info.info (* starting point *)

(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)
let char_of_token_kind = function
 | PI.RAngle -> '>'
 | PI.RBracket -> ']'
 | PI.RBrace -> '}'
 | _ -> raise (Impossible)

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

(*
 * less: I should also factorize with Parse_cpp.parse_fuzzy. 
 * put here also generic parts of  token_views_of_xxx?
 * 
 * less: check that it's consistent with the indentation? 
 * less: more fault tolerance? if col == 0 and { then reset?
 *)

let mk_trees h xs =

 (* filter comment tokens *)
  let xs = xs +> Common.exclude (fun t ->
      let kind = h.kind t in
      match kind with
      | PI.Esthet _ | PI.Eof -> true
      | _ -> false
  )
  in

  let rec consume x xs =
    match x with
    | tok when h.kind tok = PI.LBrace -> 
        let body, closing, rest = look_close PI.RBrace x [] xs in
        Ast_fuzzy.Braces (h.tokf x, body, h.tokf closing), rest
    | tok when h.kind tok = PI.LBracket -> 
        let body, closing, rest = look_close PI.RBracket x [] xs in
        Ast_fuzzy.Bracket (h.tokf x, body, h.tokf closing), rest
    | tok when h.kind tok = PI.LAngle -> 
        let body, closing, rest = look_close PI.RAngle x [] xs in
        Ast_fuzzy.Angle (h.tokf x, body, h.tokf closing), rest
    | tok when h.kind tok = PI.LPar ->
        let body, closing, rest = look_close_paren x [] xs in
        let body' = split_comma body in
        Ast_fuzzy.Parens (h.tokf x, body', h.tokf closing), rest
    | tok -> 
      Ast_fuzzy.Tok (PI.str_of_info (h.tokf tok), h.tokf x), xs
(*
    (match Ast.str_of_info (tokext tok) with
    | "..." -> Ast_fuzzy.Dots (tokext tok)
    | s when Ast_fuzzy.is_metavar s -> Ast_fuzzy.Metavar (s, tokext tok)
    | s -> Ast_fuzzy.Tok (s, tokext tok)
*)
  
  and aux xs =
  match xs with
  | [] -> []
  | x::xs ->
      let x', xs' = consume x xs in
      x'::aux xs'

  and look_close close_kind tok_start accbody xs = 
    match xs with
    | [] -> 
        raise (Unclosed (spf "look_close '%c'"
                         (char_of_token_kind close_kind),
                         h.tokf tok_start))

    | x::xs -> 
        (match x with
        | tok when h.kind tok = close_kind -> 
          List.rev accbody, x, xs
        | _ -> let (x', xs') = consume x xs in
               look_close close_kind tok_start (x'::accbody) xs'
        )

  (* todo? diff with look_close PI.RPar ? *)
  and look_close_paren tok_start accbody xs =
    match xs with
    | [] -> 
        raise (Unclosed ("look_close_paren", h.tokf tok_start))
    | x::xs -> 
        (match x with
        | tok when h.kind tok = PI.RPar -> 
            List.rev accbody, x, xs
        | _ -> 
            let (x', xs') = consume x xs in
            look_close_paren tok_start (x'::accbody) xs'
        )

  and split_comma xs =
     let rec aux acc xs =
       match xs with
       | [] ->
         if null acc
         then []
         else [Left (acc +> List.rev)]
       | x::xs ->
         (match x with
         | Ast_fuzzy.Tok (",", info) ->
           let before = acc +> List.rev in
           if null before
           then aux [] xs
           else (Left before)::(Right (info))::aux [] xs
         | _ ->
           aux (x::acc) xs
         )
     in
     aux [] xs
  in
  aux xs
OCaml

Innovation. Community. Security.