aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/text/lexer.lux
blob: ae3c4859fba1d4b95d43e9e69c8c190cd6939466 (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
(;module:
  [lux #- not]
  (lux (control functor
                applicative
                [monad #+ do Monad]
                ["p" parser])
       (data [text "text/" Monoid<Text>]
             [product]
             [maybe]
             ["R" result]
             (coll [list "L/" Functor<List>]))))

(type: Offset Nat)

(def: start-offset Offset +0)

(type: #export Lexer
  (p;Parser [Offset Text]))

(def: (remaining offset tape)
  (-> Offset Text Text)
  (|> tape (text;split offset) assume product;right))

(def: cannot-lex-error Text "Cannot lex from empty text.")

(def: (unconsumed-input-error offset tape)
  (-> Offset Text Text)
  ($_ text/append "Unconsumed input: " (remaining offset tape)))

(def: #export (run input lexer)
  (All [a] (-> Text (Lexer a) (R;Result a)))
  (case (lexer [start-offset input])
    (#R;Error msg)
    (#R;Error msg)
    
    (#R;Success [[end-offset _] output])
    (if (n.= end-offset (text;size input))
      (#R;Success output)
      (#R;Error (unconsumed-input-error end-offset input)))
    ))

(def: #export any
  {#;doc "Just returns the next character without applying any logic."}
  (Lexer Text)
  (function [[offset tape]]
    (case (text;nth offset tape)
      (#;Some output)
      (#R;Success [[(n.inc offset) tape] (text;from-code output)])

      _
      (#R;Error cannot-lex-error))
    ))

(def: #export (not p)
  {#;doc "Produce a character if the lexer fails."}
  (All [a] (-> (Lexer a) (Lexer Text)))
  (function [input]
    (case (p input)
      (#R;Error msg)
      (any input)
      
      _
      (#R;Error "Expected to fail; yet succeeded."))))

(def: #export (this reference)
  {#;doc "Lex a text if it matches the given sample."}
  (-> Text (Lexer Unit))
  (function [[offset tape]]
    (case (text;index-of reference offset tape)
      (^multi (#;Some where) (n.= offset where))
      (#R;Success [[(n.+ (text;size reference) offset) tape] []])

      _
      (#R;Error ($_ text/append "Could not match: " (text;encode reference) " @ " tape)))))

(def: #export (this? reference)
  {#;doc "Lex a text if it matches the given sample."}
  (-> Text (Lexer Bool))
  (function [(^@ input [offset tape])]
    (case (text;index-of reference offset tape)
      (^multi (#;Some where) (n.= offset where))
      (#R;Success [[(n.+ (text;size reference) offset) tape] true])

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

(def: #export end
  {#;doc "Ensure the lexer's input is empty."}
  (Lexer Unit)
  (function [(^@ input [offset tape])]
    (if (n.= offset (text;size tape))
      (#R;Success [input []])
      (#R;Error (unconsumed-input-error offset tape)))))

(def: #export end?
  {#;doc "Ask if the lexer's input is empty."}
  (Lexer Bool)
  (function [(^@ input [offset tape])]
    (#R;Success [input (n.= offset (text;size tape))])))

(def: #export peek
  {#;doc "Lex the next character (without consuming it from the input)."}
  (Lexer Text)
  (function [(^@ input [offset tape])]
    (case (text;nth offset tape)
      (#;Some output)
      (#R;Success [input (text;from-code output)])

      _
      (#R;Error cannot-lex-error))
    ))

(def: #export get-input
  {#;doc "Get all of the remaining input (without consuming it)."}
  (Lexer Text)
  (function [(^@ input [offset tape])]
    (#R;Success [input (remaining offset tape)])))

(def: #export (range bottom top)
  {#;doc "Only lex characters within a range."}
  (-> Nat Nat (Lexer Text))
  (do p;Monad<Parser>
    [char any
     #let [char' (assume (text;nth +0 char))]
     _ (p;assert ($_ text/append "Character is not within range: " (text;from-code bottom) "-" (text;from-code top))
                 (and (n.>= bottom char')
                      (n.<= top char')))]
    (wrap char)))

(do-template [<name> <bottom> <top> <desc>]
  [(def: #export <name>
     {#;doc (#;TextA ($_ text/append "Only lex " <desc> " characters."))}
     (Lexer Text)
     (range (char <bottom>) (char <top>)))]

  [upper   "A" "Z" "uppercase"]
  [lower   "a" "z" "lowercase"]
  [decimal "0" "9" "decimal"]
  [octal   "0" "7" "octal"]
  )

(def: #export alpha
  {#;doc "Only lex alphabetic characters."}
  (Lexer Text)
  (p;either lower upper))

(def: #export alpha-num
  {#;doc "Only lex alphanumeric characters."}
  (Lexer Text)
  (p;either alpha decimal))

(def: #export hexadecimal
  {#;doc "Only lex hexadecimal digits."}
  (Lexer Text)
  ($_ p;either
      decimal
      (range (char "a") (char "f"))
      (range (char "A") (char "F"))))

(def: #export (one-of options)
  {#;doc "Only lex characters that are part of a piece of text."}
  (-> Text (Lexer Text))
  (function [[offset tape]]
    (case (text;nth offset tape)
      (#;Some output)
      (let [output (text;from-code output)]
        (if (text;contains? output options)
          (#R;Success [[(n.inc offset) tape] output])
          (#R;Error ($_ text/append "Character (" output ") is not one of: " options))))

      _
      (#R;Error cannot-lex-error))))

(def: #export (none-of options)
  {#;doc "Only lex characters that are not part of a piece of text."}
  (-> Text (Lexer Text))
  (function [[offset tape]]
    (case (text;nth offset tape)
      (#;Some output)
      (let [output (text;from-code output)]
        (if (;not (text;contains? output options))
          (#R;Success [[(n.inc offset) tape] output])
          (#R;Error ($_ text/append "Character (" output ") is one of: " options))))

      _
      (#R;Error cannot-lex-error))))

(def: #export (satisfies p)
  {#;doc "Only lex characters that satisfy a predicate."}
  (-> (-> Nat Bool) (Lexer Text))
  (function [[offset tape]]
    (case (text;nth offset tape)
      (#;Some output)
      (if (p output)
        (#R;Success [[(n.inc offset) tape] (text;from-code output)])
        (#R;Error ($_ text/append "Character does not satisfy predicate: " (text;from-code output))))

      _
      (#R;Error cannot-lex-error))))

(def: #export space
  {#;doc "Only lex white-space."}
  (Lexer Text)
  (satisfies text;space?))

(def: #export (seq left right)
  (-> (Lexer Text) (Lexer Text) (Lexer Text))
  (do p;Monad<Parser>
    [=left left
     =right right]
    (wrap ($_ text/append =left =right))))

(do-template [<name> <base> <doc>]
  [(def: #export (<name> p)
     {#;doc <doc>}
     (-> (Lexer Text) (Lexer Text))
     (|> p <base> (:: p;Monad<Parser> map text;concat)))]

  [some p;some "Lex some characters as a single continuous text."]
  [many p;many "Lex many characters as a single continuous text."]
  )

(do-template [<name> <base> <doc>]
  [(def: #export (<name> n p)
     {#;doc <doc>}
     (-> Nat (Lexer Text) (Lexer Text))
     (do p;Monad<Parser>
       []
       (|> p (<base> n) (:: @ map text;concat))))]

  [exactly  p;exactly  "Lex exactly N characters."]
  [at-most  p;at-most  "Lex at most N characters."]
  [at-least p;at-least "Lex at least N characters."]
  )

(def: #export (between from to p)
  {#;doc "Lex between N and M characters."}
  (-> Nat Nat (Lexer Text) (Lexer Text))
  (|> p (p;between from to) (:: p;Monad<Parser> map text;concat)))

(def: #export (enclosed [start end] lexer)
  (All [a] (-> [Text Text] (Lexer a) (Lexer a)))
  (|> lexer
      (p;before (this end))
      (p;after (this start))))

(def: #export (local local-input lexer)
  {#;doc "Run a lexer with the given input, instead of the real one."}
  (All [a] (-> Text (Lexer a) (Lexer a)))
  (function [real-input]
    (case (run local-input lexer)
      (#R;Error error)
      (#R;Error error)

      (#R;Success value)
      (#R;Success [real-input value]))))