aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/parser/code.lux
blob: 554ad21e9e3766180636e6bd1398e8d97fd61985 (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
(.using
 [library
  [lux {"-" nat int rev local not symbol local global}
   [abstract
    ["[0]" monad {"+" do}]]
   [control
    ["[0]" try {"+" Try}]]
   [data
    ["[0]" bit]
    ["[0]" text ("[1]#[0]" monoid)]
    [collection
     ["[0]" list ("[1]#[0]" functor)]]]
   [macro
    ["[0]" code ("[1]#[0]" equivalence)]]
   [math
    [number
     ["[0]" nat]
     ["[0]" int]
     ["[0]" rev]
     ["[0]" frac]]]
   [meta
    ["[0]" symbol]]]]
 ["[0]" //])

(def: (un_paired pairs)
  (All (_ a) (-> (List [a a]) (List a)))
  (case pairs
    {.#End}                 {.#End}
    {.#Item [[x y] pairs']} (list& x y (un_paired pairs'))))

(type: .public Parser
  (//.Parser (List Code)))

(def: remaining_inputs
  (-> (List Code) Text)
  (|>> (list#each code.format)
       (text.interposed " ")
       ($_ text#composite text.new_line "Remaining input: ")))

(def: .public any
  (Parser Code)
  (function (_ tokens)
    (case tokens
      {.#End}
      {try.#Failure "There are no tokens to parse!"}
      
      {.#Item [t tokens']}
      {try.#Success [tokens' t]})))

(def: .public next
  (Parser Code)
  (function (_ tokens)
    (case tokens
      {.#End}
      {try.#Failure "There are no tokens to parse!"}
      
      {.#Item next _}
      {try.#Success [tokens next]})))

(template [<query> <check> <type> <tag> <eq> <desc>]
  [(with_expansions [<failure> (these {try.#Failure ($_ text#composite "Cannot parse " <desc> (remaining_inputs tokens))})]
     (def: .public <query>
       (Parser <type>)
       (function (_ tokens)
         (case tokens
           {.#Item [[_ {<tag> x}] tokens']}
           {try.#Success [tokens' x]}

           _
           <failure>)))

     (def: .public (<check> expected)
       (-> <type> (Parser Any))
       (function (_ tokens)
         (case tokens
           {.#Item [[_ {<tag> actual}] tokens']}
           (if (# <eq> = expected actual)
             {try.#Success [tokens' []]}
             <failure>)

           _
           <failure>))))]

  [bit    this_bit    Bit    .#Bit    bit.equivalence    "bit"]
  [nat    this_nat    Nat    .#Nat    nat.equivalence    "nat"]
  [int    this_int    Int    .#Int    int.equivalence    "int"]
  [rev    this_rev    Rev    .#Rev    rev.equivalence    "rev"]
  [frac   this_frac   Frac   .#Frac   frac.equivalence   "frac"]
  [text   this_text   Text   .#Text   text.equivalence   "text"]
  [symbol this_symbol Symbol .#Symbol symbol.equivalence "symbol"]
  )

(def: .public (this code)
  (-> Code (Parser Any))
  (function (_ tokens)
    (case tokens
      {.#Item [token tokens']}
      (if (code#= code token)
        {try.#Success [tokens' []]}
        {try.#Failure ($_ text#composite "Expected a " (code.format code) " but instead got " (code.format token)
                          (remaining_inputs tokens))})

      _
      {try.#Failure "There are no tokens to parse!"})))

(with_expansions [<failure> (these {try.#Failure ($_ text#composite "Cannot parse local symbol" (remaining_inputs tokens))})]
  (def: .public local
    (Parser Text)
    (function (_ tokens)
      (case tokens
        {.#Item [[_ {.#Symbol ["" x]}] tokens']}
        {try.#Success [tokens' x]}

        _
        <failure>)))

  (def: .public (this_local expected)
    (-> Text (Parser Any))
    (function (_ tokens)
      (case tokens
        {.#Item [[_ {.#Symbol ["" actual]}] tokens']}
        (if (# text.equivalence = expected actual)
          {try.#Success [tokens' []]}
          <failure>)

        _
        <failure>))))

(with_expansions [<failure> (these {try.#Failure ($_ text#composite "Cannot parse local symbol" (remaining_inputs tokens))})]
  (def: .public global
    (Parser Symbol)
    (function (_ tokens)
      (case tokens
        {.#Item [[_ {.#Symbol ["" short]}] tokens']}
        <failure>
        
        {.#Item [[_ {.#Symbol it}] tokens']}
        {try.#Success [tokens' it]}

        _
        <failure>)))

  (def: .public (this_global expected)
    (-> Symbol (Parser Any))
    (function (_ tokens)
      (case tokens
        {.#Item [[_ {.#Symbol ["" actual]}] tokens']}
        <failure>
        
        {.#Item [[_ {.#Symbol it}] tokens']}
        (if (# symbol.equivalence = expected it)
          {try.#Success [tokens' []]}
          <failure>)

        _
        <failure>))))

(template [<name> <tag> <desc>]
  [(def: .public (<name> p)
     (All (_ a)
       (-> (Parser a) (Parser a)))
     (function (_ tokens)
       (case tokens
         {.#Item [[_ {<tag> members}] tokens']}
         (case (p members)
           {try.#Success [{.#End} x]} {try.#Success [tokens' x]}
           _                          {try.#Failure ($_ text#composite "Parser was expected to fully consume " <desc> (remaining_inputs tokens))})

         _
         {try.#Failure ($_ text#composite "Cannot parse " <desc> (remaining_inputs tokens))})))]

  [form .#Form "form"]
  [variant .#Variant "variant"]
  [tuple .#Tuple "tuple"]
  )

(def: .public end
  (Parser Any)
  (function (_ tokens)
    (case tokens
      {.#End} {try.#Success [tokens []]}
      _       {try.#Failure ($_ text#composite "Expected list of tokens to be empty!" (remaining_inputs tokens))})))

(def: .public end?
  (Parser Bit)
  (function (_ tokens)
    {try.#Success [tokens (case tokens
                            {.#End} true
                            _       false)]}))

(def: .public (result parser inputs)
  (All (_ a) (-> (Parser a) (List Code) (Try a)))
  (case (parser inputs)
    {try.#Failure error}
    {try.#Failure error}

    {try.#Success [unconsumed value]}
    (case unconsumed
      {.#End}
      {try.#Success value}

      _
      {try.#Failure (|> unconsumed
                        (list#each code.format)
                        (text.interposed ", ")
                        (text#composite "Unconsumed inputs: "))})))

(def: .public (locally inputs parser)
  (All (_ a) (-> (List Code) (Parser a) (Parser a)))
  (function (_ real)
    (do try.monad
      [value (..result parser inputs)]
      (in [real value]))))

(def: .public (not parser)
  (All (_ a) (-> (Parser a) (Parser Code)))
  (do //.monad
    [sample ..next
     result (//.or parser
                   ..any)]
    (case result
      {.#Left _} (//.failure (text#composite "Did NOT expect to parse code: " (code.format sample)))
      {.#Right output} (in output))))