aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/parser/code.lux
blob: 5ea2247d666bf6c4233a06ce5efb8c794e74b2e5 (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
(.module:
  [lux (#- nat int rev)
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." try (#+ Try)]]
   [data
    ["." bit]
    ["." text ("#@." monoid)]
    ["." name]
    [number
     ["." nat]
     ["." int]
     ["." rev]
     ["." frac]]
    [collection
     ["." list ("#@." functor)]]]
   [macro
    ["." code ("#@." equivalence)]]]
  ["." //])

(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'))))

(type: #export Parser
  {#.doc "A Lux syntax parser."}
  (//.Parser (List Code)))

(def: (remaining-inputs asts)
  (-> (List Code) Text)
  ($_ text@compose text.new-line "Remaining input: "
      (|> asts (list@map code.to-text) (list.interpose " ") (text.join-with ""))))

(def: #export any
  {#.doc "Just returns the next input without applying any logic."}
  (Parser Code)
  (function (_ tokens)
    (case tokens
      #.Nil                (#try.Failure "There are no tokens to parse!")
      (#.Cons [t tokens']) (#try.Success [tokens' t]))))

(template [<query> <assertion> <type> <tag> <eq> <desc>]
  [(with-expansions [<error> (as-is (#try.Failure ($_ text@compose "Cannot parse " <desc> (remaining-inputs tokens))))]
     (def: #export <query>
       {#.doc (code.text ($_ text@compose "Parses the next " <desc> " input."))}
       (Parser <type>)
       (function (_ tokens)
         (case tokens
           (#.Cons [[_ (<tag> x)] tokens'])
           (#try.Success [tokens' x])

           _
           <error>)))

     (def: #export (<assertion> expected)
       (-> <type> (Parser Any))
       (function (_ tokens)
         (case tokens
           (#.Cons [[_ (<tag> actual)] tokens'])
           (if (:: <eq> = expected actual)
             (#try.Success [tokens' []])
             <error>)

           _
           <error>))))]

  [bit        bit!        Bit  #.Bit        bit.equivalence  "bit"]
  [nat        nat!        Nat  #.Nat        nat.equivalence  "nat"]
  [int        int!        Int  #.Int        int.equivalence  "int"]
  [rev        rev!        Rev  #.Rev        rev.equivalence  "rev"]
  [frac       frac!       Frac #.Frac       frac.equivalence "frac"]
  [text       text!       Text #.Text       text.equivalence "text"]
  [identifier identifier! Name #.Identifier name.equivalence "identifier"]
  [tag        tag!        Name #.Tag        name.equivalence "tag"]
  )

(def: #export (this! ast)
  {#.doc "Ensures the given Code is the next input."}
  (-> Code (Parser Any))
  (function (_ tokens)
    (case tokens
      (#.Cons [token tokens'])
      (if (code@= ast token)
        (#try.Success [tokens' []])
        (#try.Failure ($_ text@compose "Expected a " (code.to-text ast) " but instead got " (code.to-text token)
                          (remaining-inputs tokens))))

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

(template [<name> <tag> <desc>]
  [(def: #export <name>
     {#.doc (code.text ($_ text@compose "Parse a local " <desc> " (a " <desc> " that has no module prefix)."))}
     (Parser Text)
     (function (_ tokens)
       (case tokens
         (#.Cons [[_ (<tag> ["" x])] tokens'])
         (#try.Success [tokens' x])

         _
         (#try.Failure ($_ text@compose "Cannot parse local " <desc> (remaining-inputs tokens))))))]

  [local-identifier #.Identifier "identifier"]
  [   local-tag     #.Tag        "tag"]
  )

(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]
       (-> (Parser a) (Parser a)))
     (function (_ tokens)
       (case tokens
         (#.Cons [[_ (<tag> members)] tokens'])
         (case (p members)
           (#try.Success [#.Nil x]) (#try.Success [tokens' x])
           _                          (#try.Failure ($_ text@compose "Parser was expected to fully consume " <desc> (remaining-inputs tokens))))

         _
         (#try.Failure ($_ 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]
    (-> (Parser a) (Parser a)))
  (function (_ tokens)
    (case tokens
      (#.Cons [[_ (#.Record pairs)] tokens'])
      (case (p (join-pairs pairs))
        (#try.Success [#.Nil x]) (#try.Success [tokens' x])
        _                          (#try.Failure ($_ text@compose "Parser was expected to fully consume record" (remaining-inputs tokens))))

      _
      (#try.Failure ($_ text@compose "Cannot parse record" (remaining-inputs tokens))))))

(def: #export end!
  {#.doc "Ensures there are no more inputs."}
  (Parser Any)
  (function (_ tokens)
    (case tokens
      #.Nil (#try.Success [tokens []])
      _     (#try.Failure ($_ text@compose "Expected list of tokens to be empty!" (remaining-inputs tokens))))))

(def: #export end?
  {#.doc "Checks whether there are no more inputs."}
  (Parser Bit)
  (function (_ tokens)
    (case tokens
      #.Nil (#try.Success [tokens #1])
      _     (#try.Success [tokens #0]))))

(def: #export (run syntax inputs)
  (All [a] (-> (Parser a) (List Code) (Try a)))
  (case (syntax inputs)
    (#try.Failure error)
    (#try.Failure error)

    (#try.Success [unconsumed value])
    (case unconsumed
      #.Nil
      (#try.Success value)

      _
      (#try.Failure (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) (Parser a) (Parser a)))
  (function (_ real)
    (do try.monad
      [value (run syntax inputs)]
      (wrap [real value]))))