aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/target/jvm/type/parser.lux
blob: 2ad5b09a2cc02608d4327c807fdb06845ad75586 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
(.require
 [library
  [lux (.except Type Declaration int char parameter)
   [abstract
    [monad (.only do)]]
   [control
    ["<>" parser (.use "[1]#[0]" monad)]
    ["[0]" try]
    ["[0]" function]]
   [data
    ["[0]" product]
    ["[0]" text
     ["%" \\format (.only format)]
     ["<[1]>" \\parser (.only Parser)]]
    [collection
     ["[0]" list]]]]]
 ["[0]" // (.only Type)
  [category (.only Void Value Return Method Primitive Object Class Array Var Parameter Declaration)]
  ["[1][0]" signature]
  ["[1][0]" descriptor]
  [//
   [encoding
    ["[1][0]" name (.only External)]]]])

(with_template [<category> <name> <signature> <type>]
  [(def .public <name>
     (Parser (Type <category>))
     (<>.after (<text>.this (//signature.signature <signature>))
               (<>#in <type>)))]

  [Void void //signature.void //.void]
  [Primitive boolean //signature.boolean //.boolean]
  [Primitive byte //signature.byte //.byte]
  [Primitive short //signature.short //.short]
  [Primitive int //signature.int //.int]
  [Primitive long //signature.long //.long]
  [Primitive float //signature.float //.float]
  [Primitive double //signature.double //.double]
  [Primitive char //signature.char //.char]
  [Parameter wildcard //signature.wildcard //.wildcard]
  )

(def .public primitive
  (Parser (Type Primitive))
  (all <>.either
       ..boolean
       ..byte
       ..short
       ..int
       ..long
       ..float
       ..double
       ..char
       ))

(def var/head
  (format "abcdefghijklmnopqrstuvwxyz"
          "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
          "_"))

(def var/tail
  (format var/head
          "0123456789$"))

(def class/set
  (format var/tail //name.internal_separator))

(with_template [<type> <name> <head> <tail> <adapter>]
  [(def .public <name>
     (Parser <type>)
     (of <>.functor each <adapter>
         (<text>.slice (<text>.and! (<text>.one_of! <head>)
                                    (<text>.some! (<text>.one_of! <tail>))))))]

  [External class_name class/set  class/set  (|>> //name.internal //name.external)]
  [Text     var_name   var/head   var/tail   function.identity]
  )

(def .public var'
  (Parser Text)
  (|> ..var_name
      (<>.after (<text>.this //signature.var_prefix))
      (<>.before (<text>.this //descriptor.class_suffix))))

(def .public var
  (Parser (Type Var))
  (<>#each //.var ..var'))

(def .public var?
  (-> (Type Value) (Maybe Text))
  (|>> //.signature
       //signature.signature
       (<text>.result ..var')
       try.maybe))

(def .public name
  (-> (Type Var) Text)
  (|>> //.signature
       //signature.signature
       (<text>.result ..var')
       try.trusted))

(with_template [<name> <prefix> <constructor>]
  [(def <name>
     (-> (Parser (Type Parameter)) (Parser (Type Parameter)))
     (|>> (<>.after (<text>.this <prefix>))
          (<>#each <constructor>)))]

  [lower //signature.lower_prefix //.lower]
  [upper //signature.upper_prefix //.upper]
  )

(def (class'' parameter)
  (-> (Parser (Type Parameter)) (Parser [External (List (Type Parameter))]))
  (|> (do <>.monad
        [name ..class_name
         parameters (|> (<>.some parameter)
                        (<>.after (<text>.this //signature.parameters_start))
                        (<>.before (<text>.this //signature.parameters_end))
                        (<>.else (list)))]
        (in [name parameters]))
      (<>.after (<text>.this //descriptor.class_prefix))
      (<>.before (<text>.this //descriptor.class_suffix))))

(def class'
  (-> (Parser (Type Parameter)) (Parser (Type Class)))
  (|>> ..class''
       (of <>.monad each (product.uncurried //.class))))

(def .public array'
  (-> (Parser (Type Value)) (Parser (Type Array)))
  (|>> (<>.after (<text>.this //descriptor.array_prefix))
       (<>#each //.array)))

(def (parameter' value)
  (-> (Parser (Type Value)) (Parser (Type Parameter)))
  (<>.rec
   (function (_ parameter)
     (let [class (..class' parameter)]
       (all <>.either
            ..var
            ..wildcard
            (..lower parameter)
            (..upper parameter)
            (..array' value)
            class
            )))))

(def .public value
  (Parser (Type Value))
  (<>.rec
   (function (_ value)
     (all <>.either
          ..primitive
          (..parameter' value)
          ))))

(def .public parameter
  (Parser (Type Parameter))
  (..parameter' ..value))

(def .public class
  (Parser (Type Class))
  (..class' ..parameter))

(with_template [<name> <prefix> <constructor>]
  [(def .public <name>
     (-> (Type Value) (Maybe (Type Parameter)))
     (|>> //.signature
          //signature.signature
          (<text>.result (<>.after (<text>.this <prefix>) ..parameter))
          try.maybe))]

  [lower? //signature.lower_prefix //.lower]
  [upper? //signature.upper_prefix //.upper]
  )

(def .public read_class
  (-> (Type Class) [External (List (Type Parameter))])
  (|>> //.signature
       //signature.signature
       (<text>.result (..class'' ..parameter))
       try.trusted))

(def .public array
  (Parser (Type Array))
  (..array' ..value))

(def .public object
  (Parser (Type Object))
  (all <>.either
       ..class
       ..array))

(def inputs
  (|> (<>.some ..value)
      (<>.after (<text>.this //signature.arguments_start))
      (<>.before (<text>.this //signature.arguments_end))))

(def .public return
  (Parser (Type Return))
  (<>.either ..void
             ..value))

(def exception
  (Parser (Type Class))
  (|> ..class
      (<>.after (<text>.this //signature.exception_prefix))))

(def .public var_declaration
  (Parser [(Type Var) (Type Class)])
  (do <>.monad
    [name ..var_name
     _ (<text>.this //signature.format_type_parameter_infix)
     type ..class]
    (in [(//.var name) type])))

(def .public method
  (-> (Type Method)
      [(List (Type Var))
       (List (Type Value))
       (Type Return)
       (List (Type Class))])
  (let [parser (is (Parser [(List (Type Var))
                            (List (Type Value))
                            (Type Return)
                            (List (Type Class))])
                   (all <>.and
                        (|> (<>.some (<>#each product.left ..var_declaration))
                            (<>.after (<text>.this //signature.parameters_start))
                            (<>.before (<text>.this //signature.parameters_end))
                            (<>.else (list)))
                        ..inputs
                        ..return
                        (<>.some ..exception)))]
    (|>> //.signature
         //signature.signature
         (<text>.result parser)
         try.trusted)))

(with_template [<name> <category> <parser>]
  [(def .public <name>
     (-> (Type Value) (Maybe <category>))
     (|>> //.signature
          //signature.signature
          (<text>.result <parser>)
          try.maybe))]

  [array? (Type Value)
   (do <>.monad
     [_ (<text>.this //descriptor.array_prefix)]
     ..value)]
  [class? [External (List (Type Parameter))]
   (..class'' ..parameter)]

  [primitive? (Type Primitive) ..primitive]
  [wildcard? (Type Parameter) ..wildcard]
  [parameter? (Type Parameter) ..parameter]
  [object? (Type Object) ..object]
  )

(def .public declaration'
  (Parser [External (List (Type Var))])
  (|> (<>.and ..class_name
              (|> (<>.some ..var)
                  (<>.after (<text>.this //signature.parameters_start))
                  (<>.before (<text>.this //signature.parameters_end))
                  (<>.else (list))))
      (<>.after (<text>.this //descriptor.class_prefix))
      (<>.before (<text>.this //descriptor.class_suffix))))

(def .public declaration
  (-> (Type Declaration) [External (List (Type Var))])
  (|>> //.signature
       //signature.signature
       (<text>.result ..declaration')
       try.trusted))