-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseZon.nix
154 lines (133 loc) · 3.69 KB
/
parseZon.nix
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
150
151
152
153
154
lib:
let
inherit (builtins) head length listToAttrs match readFile;
inherit (lib) concatLists concatStrings escapeRegex findFirst fix foldl' last
nameValuePair;
comb = {
any = parsers: str: findFirst
(v: ! v ? error)
{ error = "failed to match any parser"; }
(map (p: p str) parsers);
apply = f: parser: str:
let v = parser str; in
if v ? error then v else
assert v ? value;
v // { value = f v.value; };
quiet = parser: str:
let v = parser str; in
if v ? error then v else
{ inherit (v) rest; };
seq = parsers: str: foldl'
(a: p:
if a ? error then a else
let v = p a.rest; in if v ? error then v else {
value = a.value ++ (if v ? value then [ v.value ] else [ ]);
rest = v.rest;
})
{ value = [ ]; rest = str; }
parsers;
seq1 = parsers: str:
comb.apply (v: assert length v == 1; head v) (comb.seq parsers) str;
maybe = parser: str:
let v = parser str; in
if ! v ? error then v else { rest = str; };
many = parser: fix (self: str:
let v = parser str; in
if v ? error then { value = [ ]; rest = str; }
else
let next = self v.rest; in {
value = (if v ? value then [ v.value ] else [ ]) ++ next.value;
inherit (next) rest;
});
};
parseRegex = rx: type: str:
let
m = match "${rx}(.*)" str;
len = length m;
in
if m == null
then { error = "failed to match ${type}"; }
else if len == 1 then { rest = last m; }
else { value = head m; rest = last m; };
parseStr = s: parseRegex (escapeRegex s) "`${s}`";
parseStrRep = s: rep: str:
let v = parseStr s str; in
if v ? error then v else (v // { value = rep; });
parseEnd = str:
if str == ""
then { rest = ""; }
else { error = "failed to match end of string"; };
parseWhitespace = comb.quiet (comb.many (comb.any [
(parseStr " ")
(parseStr "\n")
(parseRegex "//[^\n]*\n" "comment")
]));
parseZonObj = comb.any [
parseZonStruct
parseZonTuple
parseZonStr
];
parseZonAnonStructLitOf = parser: comb.apply concatLists (comb.seq [
(parseStr ".{")
(comb.many (comb.seq1 [
parseWhitespace
parser
parseWhitespace
(parseStr ",")
]))
(comb.maybe (comb.seq [
parseWhitespace
parser
]))
parseWhitespace
(parseStr "}")
]);
parseZonTuple = parseZonAnonStructLitOf parseZonObj;
parseZonIdent = comb.any [
(parseRegex "([a-zA-Z_][a-zA-Z0-9_]*)" "identifier")
(comb.seq1 [ (parseStr "@") parseZonStr ])
];
parseZonStructField = comb.apply (v: nameValuePair (head v) (last v))
(comb.seq [
(parseStr ".")
parseZonIdent
parseWhitespace
(parseStr "=")
parseWhitespace
parseZonObj
]);
parseZonStruct = comb.apply listToAttrs
(parseZonAnonStructLitOf parseZonStructField);
parseEscapeSeq = comb.seq1 [
(parseRegex "\\\\" "backslash")
(comb.any [
(parseStrRep "\\" "\\")
(parseStrRep "n" "\n")
(parseStrRep "r" "\r")
(parseStrRep "t" "\t")
(parseStrRep "'" "'")
(parseStrRep "\"" "\"")
(parseStrRep "x" (throw "unhandled escape sequence"))
(parseStrRep "u" (throw "unhandled escape sequence"))
])
];
parseZonStr = comb.apply concatStrings (comb.seq1 [
(parseStr "\"")
(comb.many (comb.any [
(parseRegex "([^\"\n\\])" "string char")
parseEscapeSeq
]))
(parseStr "\"")
]);
parseZon = comb.seq1 [
parseWhitespace
parseZonObj
parseWhitespace
parseEnd
];
in
path:
let
v = parseZon (readFile path);
in
if ! v ? error then v.value else throw "failed to parse ${path}"