-
Notifications
You must be signed in to change notification settings - Fork 0
/
pp-code-0.satyh
47 lines (38 loc) · 1.06 KB
/
pp-code-0.satyh
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
% pp-code-0
@stage: 0
@import: external/satysfi-lib/parser
type meta-code-element =
| ObjectCode of string
| MetaCode of string
module MetaCode = struct
let atmark = Char.make `@`
let is-not-atmark ch = not (Char.equal ch atmark)
let non-atmark-string-parser =
open Parser in
many1 (satisfy is-not-atmark) >>= (fun chars ->
return (String.of-list chars)
)
let meta-parser =
open Parser in
let p1 =
between (char atmark) (char atmark)
non-atmark-string-parser >>= (fun s -> return (MetaCode(s)))
in
let p2 = string `@@` >> (return (ObjectCode(`@`))) in
p1 <|> p2
let object-parser =
open Parser in
non-atmark-string-parser >>= (fun s -> return (ObjectCode(s)))
let main-parser =
open Parser in
many (meta-parser <|> object-parser)
let parse s =
match main-parser |> Parser.run s with
| None ->
error `MetaCode: parse error (1)`
| Some((elems, rest)) ->
if String.is-empty rest then
elems
else
error `MetaCode: parse error (2)`
end