kahl

R⁷RS Scheme parser combinator library for decoding BARE messages

git clone https://git.8pit.net/kahl.git

 1(test-group "optional<type>"
 2  (test-parse 'nothing (parse-optional parse-u8) #u8(0))
 3  (test-parse 23 (parse-optional parse-u8) #u8(1 23))
 4  (test-parse-error "invalid option value"
 5                    (parse-optional parse-u8) #u8(23 42)))
 6
 7(test-group "map[type A]type B"
 8  (test-parse '((35 2) (5 6))
 9              (parse-map parse-u8 parse-u8)
10              #u8(#x2 #x23 #x2 #x5 #x6))
11  (test-parse '()
12              (parse-map parse-u16 parse-u32)
13              #u8(#x0))
14
15  (test-parse-error "kons failed"
16                    (parse-map parse-u8 parse-u8)
17                    #u8(#x2 #xac #xab #xac #xab)))
18
19(test-group "[length]type"
20  (test-parse '(#x23 #x42)
21              (parse-list parse-u8 2)
22              #u8(#x23 #x42))
23
24  ;; length of fixed-length array must be at least 1
25  (test-error (parse-list parse-u8 0)))
26
27(test-group "[]type"
28  (test-parse '(#x05 #x15)
29              (parse-list parse-u8)
30              #u8(2 #x05 #x15))
31  (test-parse '(#x4223)
32              (parse-list parse-u16)
33              #u8(#x1 #x23 #x42)))
34
35(test-group "union"
36  (test-parse #t
37              (parse-union
38                (vector
39                  parse-bool ;; type id → 0
40                  parse-u16  ;; type id → 1
41                ))
42              #u8(#x00 #x01))
43
44  (test-parse #x1312
45              (parse-union
46                (vector
47                  parse-bool ;; type id → 0
48                  parse-u16  ;; type id → 1
49                ))
50              #u8(#x01 #x12 #x13))
51
52  (test-parse-error
53    "unexpected tag in tagged union"
54    (parse-union
55      (vector parse-u8 parse-u8))
56    #u8(#x02 #x23 #x42))
57
58  ;; unions must have at least one type
59  (test-error (parse-union '())))
60
61(test-group "struct"
62  (test-parse '#(#t #xff #f)
63              (parse-struct
64                parse-bool
65                parse-u8
66                parse-bool)
67              #u8(#x01 #xff #x00))
68
69  (test-parse '#("" "")
70              (parse-struct
71                parse-string
72                parse-string)
73              #u8(#x0 #x0))
74
75  ;; Structs MUST have at least one field.
76  (test-error (parse-struct '()))
77  (test-error (parse-struct)))