blob: d7621eba49127108cbbea47e6304e2473f8d9a3b (
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
|
(.module:
[lux #- nat int deg]
(lux [macro #+ with-gensyms]
(control [monad #+ do Monad]
[equality #+ Eq]
["p" parser])
(data [bool]
[number]
[text "text/" Monoid<Text>]
[ident]
(coll [list "list/" Functor<List>])
[product]
[maybe]
["e" error]))
(// [code "code/" Eq<Code>]))
## [Utils]
(def: (join-pairs pairs)
(All [a] (-> (List [a a]) (List a)))
(case pairs
#.Nil #.Nil
(#.Cons [[x y] pairs']) (list& x y (join-pairs pairs'))))
## [Types]
(type: #export Syntax
{#.doc "A Lux syntax parser."}
(p.Parser (List Code)))
## [Utils]
(def: (remaining-inputs asts)
(-> (List Code) Text)
($_ text/compose "\nRemaining input: "
(|> asts (list/map code.to-text) (list.interpose " ") (text.join-with ""))))
## [Syntaxs]
(def: #export any
{#.doc "Just returns the next input without applying any logic."}
(Syntax Code)
(function (_ tokens)
(case tokens
#.Nil (#e.Error "There are no tokens to parse!")
(#.Cons [t tokens']) (#e.Success [tokens' t]))))
(do-template [<get-name> <type> <tag> <eq> <desc>]
[(def: #export <get-name>
{#.doc (code.text ($_ text/compose "Parses the next " <desc> " input Code."))}
(Syntax <type>)
(function (_ tokens)
(case tokens
(#.Cons [[_ (<tag> x)] tokens'])
(#e.Success [tokens' x])
_
(#e.Error ($_ text/compose "Cannot parse " <desc> (remaining-inputs tokens))))))]
[ bool Bool #.Bool bool.Eq<Bool> "bool"]
[ nat Nat #.Nat number.Eq<Nat> "nat"]
[ int Int #.Int number.Eq<Int> "int"]
[ deg Deg #.Deg number.Eq<Deg> "deg"]
[ frac Frac #.Frac number.Eq<Frac> "frac"]
[ text Text #.Text text.Eq<Text> "text"]
[symbol Ident #.Symbol ident.Eq<Ident> "symbol"]
[ tag Ident #.Tag ident.Eq<Ident> "tag"]
)
(def: #export (this? ast)
{#.doc "Asks if the given Code is the next input."}
(-> Code (Syntax Bool))
(function (_ tokens)
(case tokens
(#.Cons [token tokens'])
(let [is-it? (code/= ast token)
remaining (if is-it?
tokens'
tokens)]
(#e.Success [remaining is-it?]))
_
(#e.Success [tokens false]))))
(def: #export (this ast)
{#.doc "Ensures the given Code is the next input."}
(-> Code (Syntax Top))
(function (_ tokens)
(case tokens
(#.Cons [token tokens'])
(if (code/= ast token)
(#e.Success [tokens' []])
(#e.Error ($_ text/compose "Expected a " (code.to-text ast) " but instead got " (code.to-text token)
(remaining-inputs tokens))))
_
(#e.Error "There are no tokens to parse!"))))
(do-template [<name> <tag> <desc>]
[(def: #export <name>
{#.doc (code.text ($_ text/compose "Parse a local " <desc> " (a " <desc> " that has no module prefix)."))}
(Syntax Text)
(function (_ tokens)
(case tokens
(#.Cons [[_ (<tag> ["" x])] tokens'])
(#e.Success [tokens' x])
_
(#e.Error ($_ text/compose "Cannot parse local " <desc> (remaining-inputs tokens))))))]
[local-symbol #.Symbol "symbol"]
[ local-tag #.Tag "tag"]
)
(do-template [<name> <tag> <desc>]
[(def: #export (<name> p)
{#.doc (code.text ($_ text/compose "Parse inside the contents of a " <desc> " as if they were the input Codes."))}
(All [a]
(-> (Syntax a) (Syntax a)))
(function (_ tokens)
(case tokens
(#.Cons [[_ (<tag> members)] tokens'])
(case (p members)
(#e.Success [#.Nil x]) (#e.Success [tokens' x])
_ (#e.Error ($_ text/compose "Syntax was expected to fully consume " <desc> (remaining-inputs tokens))))
_
(#e.Error ($_ text/compose "Cannot parse " <desc> (remaining-inputs tokens))))))]
[ form #.Form "form"]
[tuple #.Tuple "tuple"]
)
(def: #export (record p)
{#.doc (code.text ($_ text/compose "Parse inside the contents of a record as if they were the input Codes."))}
(All [a]
(-> (Syntax a) (Syntax a)))
(function (_ tokens)
(case tokens
(#.Cons [[_ (#.Record pairs)] tokens'])
(case (p (join-pairs pairs))
(#e.Success [#.Nil x]) (#e.Success [tokens' x])
_ (#e.Error ($_ text/compose "Syntax was expected to fully consume record" (remaining-inputs tokens))))
_
(#e.Error ($_ text/compose "Cannot parse record" (remaining-inputs tokens))))))
(def: #export end!
{#.doc "Ensures there are no more inputs."}
(Syntax Top)
(function (_ tokens)
(case tokens
#.Nil (#e.Success [tokens []])
_ (#e.Error ($_ text/compose "Expected list of tokens to be empty!" (remaining-inputs tokens))))))
(def: #export end?
{#.doc "Checks whether there are no more inputs."}
(Syntax Bool)
(function (_ tokens)
(case tokens
#.Nil (#e.Success [tokens true])
_ (#e.Success [tokens false]))))
(def: #export (on compiler action)
{#.doc "Run a Lux operation as if it was a Syntax parser."}
(All [a] (-> Lux (Meta a) (Syntax a)))
(function (_ input)
(case (macro.run compiler action)
(#e.Error error)
(#e.Error error)
(#e.Success value)
(#e.Success [input value])
)))
(def: #export (run inputs syntax)
(All [a] (-> (List Code) (Syntax a) (e.Error a)))
(case (syntax inputs)
(#e.Error error)
(#e.Error error)
(#e.Success [unconsumed value])
(case unconsumed
#.Nil
(#e.Success value)
_
(#e.Error (text/compose "Unconsumed inputs: "
(|> (list/map code.to-text unconsumed)
(text.join-with ", ")))))))
(def: #export (local inputs syntax)
{#.doc "Run a syntax parser with the given list of inputs, instead of the real ones."}
(All [a] (-> (List Code) (Syntax a) (Syntax a)))
(function (_ real)
(do e.Monad<Error>
[value (run inputs syntax)]
(wrap [real value]))))
## [Syntax]
(macro: #export (syntax: tokens)
{#.doc (doc "A more advanced way to define macros than \"macro:\"."
"The inputs to the macro can be parsed in complex ways through the use of syntax parsers."
"The macro body is also (implicitly) run in the Monad<Meta>, to save some typing."
"Also, the compiler state can be accessed through the *compiler* binding."
(syntax: #export (object {#let [imports (class-imports *compiler*)]}
{#let [class-vars (list)]}
{super (opt (super-class-decl^ imports class-vars))}
{interfaces (tuple (some (super-class-decl^ imports class-vars)))}
{constructor-args (constructor-args^ imports class-vars)}
{methods (some (overriden-method-def^ imports))})
(let [def-code ($_ text/compose "anon-class:"
(spaced (list (super-class-decl$ (maybe.default object-super-class super))
(with-brackets (spaced (list/map super-class-decl$ interfaces)))
(with-brackets (spaced (list/map constructor-arg$ constructor-args)))
(with-brackets (spaced (list/map (method-def$ id) methods))))))]
(wrap (list (` ((~ (code.text def-code)))))))))}
(let [[exported? tokens] (: [Bool (List Code)]
(case tokens
(^ (list& [_ (#.Tag ["" "export"])] tokens'))
[true tokens']
_
[false tokens]))
?parts (: (Maybe [Text (List Code) Code Code])
(case tokens
(^ (list [_ (#.Form (list& [_ (#.Symbol ["" name])] args))]
body))
(#.Some name args (` {}) body)
(^ (list [_ (#.Form (list& [_ (#.Symbol ["" name])] args))]
meta-data
body))
(#.Some name args meta-data body)
_
#.None))]
(case ?parts
(#.Some [name args meta body])
(with-gensyms [g!text/join-with g!tokens g!body g!error]
(do macro.Monad<Meta>
[vars+parsers (monad.map @
(: (-> Code (Meta [Code Code]))
(function (_ arg)
(case arg
(^ [_ (#.Record (list [var parser]))])
(wrap [var parser])
[_ (#.Symbol var-name)]
(wrap [(code.symbol var-name) (` any)])
_
(macro.fail "Syntax pattern expects records or symbols."))))
args)
#let [g!state (code.symbol ["" "*compiler*"])
error-msg (code.text (text/compose "Wrong syntax for " name))
export-ast (: (List Code)
(if exported?
(list (' #export))
(list)))]]
(wrap (list (` (macro: (~+ export-ast) ((~ (code.symbol ["" name])) (~ g!tokens) (~ g!state))
(~ meta)
("lux case" (..run (~ g!tokens)
(: (..Syntax (Meta (List Code)))
((~! do) (~! p.Monad<Parser>)
[(~+ (join-pairs vars+parsers))]
((~' wrap) ((~! do) (~! macro.Monad<Meta>)
[]
(~ body))))))
{(#e.Success (~ g!body))
((~ g!body) (~ g!state))
(#e.Error (~ g!error))
(let [(~ g!text/join-with) (~! text.join-with)]
(#e.Error ((~ g!text/join-with) ": " (list (~ error-msg) (~ g!error)))))})))))))
_
(macro.fail "Wrong syntax for syntax:"))))
|