Source file common.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
module IntMap = Map.Make(Int)

type capture = {
  capture_name : string option;
  capture_patterns : rule list;
}

and regex = Oniguruma.Encoding.utf8 Oniguruma.t

and match_ = {
  name : string option;
  pattern : regex;
  captures : capture IntMap.t;
}

and delim_kind = End | While

and delim = {
  delim_begin : regex;
  delim_end : string; (* Either an end or a while pattern *)
  delim_patterns : rule list;
  delim_name : string option;
  delim_content_name : string option;
  delim_begin_captures : capture IntMap.t;
  delim_end_captures : capture IntMap.t;
  delim_apply_end_pattern_last : bool;
  delim_kind : delim_kind;
}

and rule =
  | Match of match_
  | Delim of delim
  | Include_local of string
  | Include_scope of string
  | Include_self
  | Include_base

type repo_item_kind =
  | Repo_rule of rule
  | Repo_patterns of rule list

type repo_item = {
  repo_item_kind : repo_item_kind;
  repo_inner : (string, repo_item) Hashtbl.t;
}

type grammar = {
  name : string;
  scope_name : string;
  patterns : rule list;
  repository : (string, repo_item) Hashtbl.t;
}

type t = {
  by_name : (string, grammar) Hashtbl.t;
  by_scope_name : (string, grammar) Hashtbl.t;
}

type plist =
  [ `Bool of bool
  | `Data of string
  | `Date of float * float option
  | `Float of float
  | `Int of int
  | `String of string
  | `Array of plist list
  | `Dict of (string * plist) list
  ]

exception Error of string

let create () = {
  by_name = Hashtbl.create 23;
  by_scope_name = Hashtbl.create 23;
}

let add_grammar t grammar =
  Hashtbl.add t.by_name (String.lowercase_ascii grammar.name) grammar;
  Hashtbl.add t.by_scope_name grammar.scope_name grammar

let find_by_name t name =
  Hashtbl.find_opt t.by_name (String.lowercase_ascii name)

let find_by_scope_name t = Hashtbl.find_opt t.by_scope_name

let error msg = raise (Error msg)