aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/test/test/luxc/lang/parser.lux
blob: c70bdaecea36e2daf64341917108abc5c1dda699 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
(;module:
  lux
  (lux [io]
       (control [monad #+ do])
       (data [number]
             ["e" error]
             [text]
             (text format
                   ["l" lexer])
             (coll [list]))
       ["r" math/random "r/" Monad<Random>]
       (meta [code])
       test)
  (luxc (lang ["&" parser])))

(def: default-cursor
  Cursor
  {#;module ""
   #;line   +0
   #;column +0})

(def: ident-part^
  (r;Random Text)
  (do r;Monad<Random>
    [#let [digits "0123456789"
           delimiters "()[]{}#;\""
           space "\t\v \n\r\f"
           invalid-range (format digits delimiters space)
           char-gen (|> r;nat
                        (r;filter (function [sample]
                                    (not (text;contains? (text;from-code sample)
                                                         invalid-range)))))]
     size (|> r;nat (:: @ map (|>. (n.% +20) (n.max +1))))]
    (r;text' char-gen size)))

(def: ident^
  (r;Random Ident)
  (r;seq ident-part^ ident-part^))

(def: code^
  (r;Random Code)
  (let [numeric^ (: (r;Random Code)
                    ($_ r;either
                        (|> r;bool (r/map (|>. #;Bool [default-cursor])))
                        (|> r;nat (r/map (|>. #;Nat [default-cursor])))
                        (|> r;int (r/map (|>. #;Int [default-cursor])))
                        (|> r;deg (r/map (|>. #;Deg [default-cursor])))
                        (|> r;frac (r/map (|>. #;Frac [default-cursor])))))
        textual^ (: (r;Random Code)
                    ($_ r;either
                        (do r;Monad<Random>
                          [size (|> r;nat (r/map (n.% +20)))]
                          (|> (r;text size) (r/map (|>. #;Text [default-cursor]))))
                        (|> ident^ (r/map (|>. #;Symbol [default-cursor])))
                        (|> ident^ (r/map (|>. #;Tag [default-cursor])))))
        simple^ (: (r;Random Code)
                   ($_ r;either
                       numeric^
                       textual^))]
    (r;rec
     (function [code^]
       (let [multi^ (do r;Monad<Random>
                      [size (|> r;nat (r/map (n.% +3)))]
                      (r;list size code^))
             composite^ (: (r;Random Code)
                           ($_ r;either
                               (|> multi^ (r/map (|>. #;Form [default-cursor])))
                               (|> multi^ (r/map (|>. #;Tuple [default-cursor])))
                               (do r;Monad<Random>
                                 [size (|> r;nat (r/map (n.% +3)))]
                                 (|> (r;list size (r;seq code^ code^))
                                     (r/map (|>. #;Record [default-cursor]))))))]
         (r;either simple^
                   composite^))))))

(context: "Lux code parser."
  (<| (times +100)
      (do @
        [sample code^
         other code^]
        ($_ seq
            (test "Can parse Lux code."
                  (case (&;parse [default-cursor +0 (code;to-text sample)])
                    (#e;Error error)
                    false

                    (#e;Success [_ parsed])
                    (:: code;Eq<Code> = parsed sample)))
            (test "Can parse Lux multiple code nodes."
                  (case (&;parse [default-cursor +0 (format (code;to-text sample) " "
                                                            (code;to-text other))])
                    (#e;Error error)
                    false

                    (#e;Success [remaining =sample])
                    (case (&;parse remaining)
                      (#e;Error error)
                      false

                      (#e;Success [_ =other])
                      (and (:: code;Eq<Code> = sample =sample)
                           (:: code;Eq<Code> = other =other)))))
            ))))

(def: nat-to-frac
  (-> Nat Frac)
  (|>. nat-to-int int-to-frac))

(context: "Frac special syntax."
  (<| (times +100)
      (do @
        [numerator (|> r;nat (:: @ map (|>. (n.% +100) nat-to-frac)))
         denominator (|> r;nat (:: @ map (|>. (n.% +100) (n.max +1) nat-to-frac)))
         signed? r;bool
         #let [expected (|> numerator (f./ denominator) (f.* (if signed? -1.0 1.0)))]]
        (test "Can parse frac ratio syntax."
              (case (&;parse [default-cursor +0
                              (format (if signed? "-" "")
                                      (%i (frac-to-int numerator))
                                      "/"
                                      (%i (frac-to-int denominator)))])
                (#e;Success [_ [_ (#;Frac actual)]])
                (f.= expected actual)

                _
                false)
              ))))

(context: "Nat special syntax."
  (<| (times +100)
      (do @
        [expected (|> r;nat (:: @ map (n.% +1_000)))]
        (test "Can parse nat char syntax."
              (case (&;parse [default-cursor +0
                              (format "#" (%t (text;from-code expected)) "")])
                (#e;Success [_ [_ (#;Nat actual)]])
                (n.= expected actual)

                _
                false)
              ))))

(def: comment-text^
  (r;Random Text)
  (let [char-gen (|> r;nat (r;filter (function [value]
                                       (not (or (text;space? value)
                                                (n.= (char "#") value)
                                                (n.= (char "(") value)
                                                (n.= (char ")") value))))))]
    (do r;Monad<Random>
      [size (|> r;nat (r/map (n.% +20)))]
      (r;text' char-gen size))))

(def: comment^
  (r;Random Text)
  (r;either (do r;Monad<Random>
              [comment comment-text^]
              (wrap (format "## " comment "\n")))
            (r;rec (function [nested^]
                     (do r;Monad<Random>
                       [comment (r;either comment-text^
                                          nested^)]
                       (wrap (format "#( " comment " )#")))))))

(context: "Multi-line text & comments."
  (<| (times +100)
      (do @
        [#let [char-gen (|> r;nat (r;filter (function [value]
                                              (not (or (text;space? value)
                                                       (n.= (char "\"") value))))))]
         x char-gen
         y char-gen
         z char-gen
         offset-size (|> r;nat (r/map (|>. (n.% +10) (n.max +1))))
         #let [offset (text;join-with "" (list;repeat offset-size " "))]
         sample code^
         comment comment^
         unbalanced-comment comment-text^]
        ($_ seq
            (test "Will reject invalid multi-line text."
                  (let [bad-match (format (text;from-code x) "\n"
                                          (text;from-code y) "\n"
                                          (text;from-code z))]
                    (case (&;parse [default-cursor +0
                                    (format "\"" bad-match "\"")])
                      (#e;Error error)
                      true

                      (#e;Success [_ parsed])
                      false)))
            (test "Will accept valid multi-line text"
                  (let [good-input (format (text;from-code x) "\n"
                                           offset (text;from-code y) "\n"
                                           offset (text;from-code z))
                        good-output (format (text;from-code x) "\n"
                                            (text;from-code y) "\n"
                                            (text;from-code z))]
                    (case (&;parse [(|> default-cursor (update@ #;column (n.+ (n.dec offset-size))))
                                    +0
                                    (format "\"" good-input "\"")])
                      (#e;Error error)
                      false

                      (#e;Success [_ parsed])
                      (:: code;Eq<Code> =
                          parsed
                          (code;text good-output)))))
            (test "Can handle comments."
                  (case (&;parse [default-cursor +0
                                  (format comment (code;to-text sample))])
                    (#e;Error error)
                    false

                    (#e;Success [_ parsed])
                    (:: code;Eq<Code> = parsed sample)))
            (test "Will reject unbalanced multi-line comments."
                  (and (case (&;parse [default-cursor +0
                                       (format "#(" "#(" unbalanced-comment ")#"
                                               (code;to-text sample))])
                         (#e;Error error)
                         true

                         (#e;Success [_ parsed])
                         false)
                       (case (&;parse [default-cursor +0
                                       (format "#(" unbalanced-comment ")#" ")#"
                                               (code;to-text sample))])
                         (#e;Error error)
                         true

                         (#e;Success [_ parsed])
                         false)))
            ))))