diff options
Diffstat (limited to 'lib/nix/types.ml')
-rw-r--r-- | lib/nix/types.ml | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/lib/nix/types.ml b/lib/nix/types.ml new file mode 100644 index 0000000..8245406 --- /dev/null +++ b/lib/nix/types.ml | |||
@@ -0,0 +1,112 @@ | |||
1 | open Core | ||
2 | |||
3 | (* Binary operators *) | ||
4 | type binary_op = | ||
5 | | Plus | ||
6 | | Minus | ||
7 | | Mult | ||
8 | | Div | ||
9 | | Gt | ||
10 | | Lt | ||
11 | | Lte | ||
12 | | Gte | ||
13 | | Eq | ||
14 | | Neq | ||
15 | | Or | ||
16 | | And | ||
17 | | Impl | ||
18 | | Merge | ||
19 | | Concat | ||
20 | [@@deriving sexp_of] | ||
21 | |||
22 | (* Unary operators *) | ||
23 | type unary_op = Negate | Not [@@deriving sexp_of] | ||
24 | |||
25 | (* The top-level expression type *) | ||
26 | type expr = | ||
27 | | BinaryOp of binary_op * expr * expr | ||
28 | | UnaryOp of unary_op * expr | ||
29 | | Cond of expr * expr * expr | ||
30 | | With of expr * expr | ||
31 | | Assert of expr * expr | ||
32 | | Test of expr * expr list | ||
33 | | SetLet of binding list | ||
34 | | Let of binding list * expr | ||
35 | | Val of value | ||
36 | | Id of id | ||
37 | | Select of expr * expr list * expr option | ||
38 | | Apply of expr * expr | ||
39 | | Aquote of expr | ||
40 | [@@deriving sexp_of] | ||
41 | |||
42 | (* Possible values *) | ||
43 | and value = | ||
44 | (* Str is a string start, followed by arbitrary number of antiquotations and | ||
45 | strings that separate them *) | ||
46 | | Str of string * (expr * string) list | ||
47 | (* IStr is an indented string, so it has the extra integer component which | ||
48 | indicates the indentation *) | ||
49 | | IStr of int * string * (expr * string) list | ||
50 | | Int of string | ||
51 | | Float of string | ||
52 | | Path of string | ||
53 | | SPath of string | ||
54 | | HPath of string | ||
55 | | Uri of string | ||
56 | | Lambda of pattern * expr | ||
57 | | List of expr list | ||
58 | | AttSet of recursivity * binding list | ||
59 | [@@deriving sexp_of] | ||
60 | |||
61 | (* Patterns in lambda definitions *) | ||
62 | and pattern = Alias of id | ParamSet of id option * param_set | ||
63 | [@@deriving sexp_of] | ||
64 | |||
65 | and param_set = param list * match_kind [@@deriving sexp_of] | ||
66 | and param = id * expr option [@@deriving sexp_of] | ||
67 | and recursivity = Rec | Nonrec | ||
68 | and match_kind = Exact | Loose | ||
69 | |||
70 | (* Bindings in attribute sets and let expressions *) | ||
71 | and binding = | ||
72 | (* The first expr should be attrpath, which is the same as in Select *) | ||
73 | | AttrPath of expr list * expr | ||
74 | | Inherit of expr option * expr list | ||
75 | [@@deriving sexp_of] | ||
76 | |||
77 | (* Identifiers *) | ||
78 | and id = string | ||
79 | |||
80 | (* Precedence levels of binary operators *) | ||
81 | let prec_of_bop = function | ||
82 | | Concat -> 5 | ||
83 | | Mult | Div -> 6 | ||
84 | | Plus | Minus -> 7 | ||
85 | | Merge -> 9 | ||
86 | | Gt | Lt | Lte | Gte -> 10 | ||
87 | | Eq | Neq -> 11 | ||
88 | | And -> 12 | ||
89 | | Or -> 13 | ||
90 | | Impl -> 14 | ||
91 | |||
92 | type assoc = Left | Right | ||
93 | |||
94 | let assoc_of_bop = function | ||
95 | | Mult | Div | Plus | Minus -> Some Left | ||
96 | | Concat | Merge | And | Or -> Some Right | ||
97 | | Gt | Lt | Lte | Gte | Eq | Neq | Impl -> None | ||
98 | |||
99 | (* Precedence levels of unary operators *) | ||
100 | let prec_of_uop = function Negate -> 3 | Not -> 8 | ||
101 | |||
102 | (* Precedence level of expressions | ||
103 | (assuming that the constituents have higher levels) *) | ||
104 | let prec_of_expr = function | ||
105 | | Val (Lambda _) -> 15 | ||
106 | | Val _ | Id _ | Aquote _ -> 0 | ||
107 | | BinaryOp (op, _, _) -> prec_of_bop op | ||
108 | | UnaryOp (op, _) -> prec_of_uop op | ||
109 | | Cond _ | With _ | Assert _ | Let _ | SetLet _ -> 15 | ||
110 | | Test _ -> 4 | ||
111 | | Select _ -> 1 | ||
112 | | Apply _ -> 2 | ||