|
| 1 | +(* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + *) |
| 7 | + |
| 8 | +open! IStd |
| 9 | + |
| 10 | +type location = {line: int; col: int} |
| 11 | + |
| 12 | +type class_kind = Class of string | Interface of string | AnonymousClass | Enum of string |
| 13 | + |
| 14 | +type class_or_interface = |
| 15 | + {location: location; kind: class_kind; inner_elements: class_or_interface list} |
| 16 | + |
| 17 | +type file_content = {package: string option; classes: class_or_interface list} |
| 18 | + |
| 19 | +type context = {prefix: string; mutable counter: int} |
| 20 | + |
| 21 | +let name_of_kind context = function |
| 22 | + | Class id | Interface id | Enum id -> |
| 23 | + id |
| 24 | + | AnonymousClass -> |
| 25 | + context.counter <- context.counter + 1 ; |
| 26 | + string_of_int context.counter |
| 27 | + |
| 28 | + |
| 29 | +let rec iter ~action_on_class_location context {location; kind; inner_elements} = |
| 30 | + let previous_prefix = context.prefix in |
| 31 | + let name = name_of_kind context kind in |
| 32 | + let context = {prefix= Printf.sprintf "%s%s$" context.prefix name; counter= 0} in |
| 33 | + let classname = previous_prefix ^ name in |
| 34 | + let col = location.col in |
| 35 | + let line = location.line in |
| 36 | + let _ = action_on_class_location ~classname ~col ~line in |
| 37 | + List.iter inner_elements ~f:(iter ~action_on_class_location context) |
| 38 | + |
| 39 | + |
| 40 | +let iter_on_declarations ~action_on_class_location {package; classes} = |
| 41 | + let prefix = Option.fold ~init:"" ~f:(fun _ s -> s ^ ".") package in |
| 42 | + let context = {prefix; counter= 0} in |
| 43 | + List.iter classes ~f:(iter ~action_on_class_location context) |
0 commit comments