aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/test/test/luxc/parser.lux
blob: c90812cc87363a9ec0771570d45b95becaa90f08 (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
(;module:
  lux
  (lux [io]
       (control monad)
       (data [text "T/" Eq<Text>]
             (text format
                   ["l" lexer])
             [number]
             ["R" result]
             (coll [list]))
       ["r" math/random "r/" Monad<Random>]
       (macro [code])
       test)
  (luxc ["&" 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;real (r/map (|>. #;Real [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."
  [sample code^]
  (test "Can parse Lux code."
        (case (&;parse [default-cursor (code;to-text sample)])
          (#R;Error error)
          false

          (#R;Success [_ parsed])
          (:: code;Eq<Code> = parsed sample))
        ))

(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."
  [#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
                              (format "\"" bad-match "\"")])
                (#R;Error error)
                true

                (#R;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))))
                              (format "\"" good-input "\"")])
                (#R;Error error)
                false

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

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

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

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