aboutsummaryrefslogtreecommitdiffstats
path: root/src/Store/Query/Parser.hs
blob: e16c926c08faeff830a6d3266560925719eda127 (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
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Store.Query.Parser () where

import Control.Exception (throw)
import Control.Monad (void)
import Data.Aeson qualified as J
import Data.Aeson.Key qualified as JK
import Data.Aeson.KeyMap qualified as JM
import Data.Char (isSpace)
import Data.Map qualified as M
import Data.String (IsString (fromString))
import Data.Text qualified as T
import Data.Vector qualified as V
import Data.Void (Void)
import Store.Exception (ParseError (ParseError))
import Store.Query.Type
import Text.Megaparsec qualified as P
import Text.Megaparsec.Char qualified as P
import Text.Megaparsec.Char.Lexer qualified as P

instance IsString Query where
  fromString =
    either (throw . ParseError . P.errorBundlePretty @String @Void) id
      . P.parse parser ""
    where
      parser = do
        void $ P.many P.space1
        P.choice
          [ do
              delete
              from
              c <- collection
              w <- P.optional do
                where_
                whereClause
              pure $ Delete c w,
            do
              insert
              vs <- objects
              into
              c <- collection
              pure $ Insert vs c,
            do
              select
              fs <- fieldSelector
              from
              c <- collection
              js <- joinClauses
              es <- embedClauses
              w <- P.optional do
                where_
                whereClause
              pure $ Select fs c js es w,
            do
              update
              c <- collection
              set
              v <- object
              w <-
                P.optional do
                  where_
                  whereClause
              pure $ Update c v w
          ]
          <* P.eof

      lexeme :: P.Parsec Void String a -> P.Parsec Void String a
      lexeme = P.lexeme $ void $ P.takeWhileP Nothing isSpace

      set = void $ lexeme (P.string "SET")
      update = void $ lexeme (P.string "UPDATE")
      into = void $ lexeme (P.string "INTO")
      insert = void $ lexeme (P.string "INSERT")
      delete = void $ lexeme (P.string "DELETE")
      and = void $ lexeme (P.string "AND")
      colon = void $ lexeme (P.string ":")
      comma = void $ lexeme (P.string ",")
      embed = void $ lexeme (P.string "EMBED")
      eq = void $ lexeme (P.string "==")
      from = void $ lexeme (P.string "FROM")
      on = void $ lexeme (P.string "ON")
      or = void $ lexeme (P.string "OR")
      select = void $ lexeme (P.string "SELECT")
      where_ = void $ lexeme (P.string "WHERE")

      collection = lexeme $ P.takeWhile1P (Just "collection") (not . isSpace)

      joinClauses = P.many joinClause

      joinClause = do
        t <- joinType
        c <- collection
        w <- P.optional do
          on
          whereClause
        pure $ JoinClause t c w

      embedClauses = P.many embedClause

      embedClause = do
        embed
        c <- collection
        w <- P.optional do
          on
          whereClause
        pure $ EmbedClause c w

      whereClause =
        P.choice
          [ P.try (And . map Where <$> P.sepBy1 comparison and),
            P.try (Or . map Where <$> P.sepBy1 comparison or),
            Where <$> comparison
          ]

      comparison = do
        a <- field
        eq
        b <- field
        pure $ Eq a b

      fieldSelector =
        lexeme $
          P.choice
            [ do
                void $ lexeme $ P.string "{"
                kvs <-
                  P.sepBy
                    ( P.choice
                        [ P.try do
                            k <-
                              lexeme $
                                P.takeWhile1P
                                  Nothing
                                  ( \c ->
                                      not (isSpace c)
                                        && c /= '.'
                                        && c /= ','
                                        && c /= ':'
                                  )
                            lexeme $ colon
                            v <- fieldSelector
                            pure (k, v),
                          do
                            f@(Field c ks) <- field
                            let k
                                  | null ks = c
                                  | otherwise = T.unpack (last ks)
                            pure (k, SelectField f)
                        ]
                    )
                    comma
                void $ lexeme $ P.string "}"
                pure $ SelectObject (M.fromList kvs),
              do
                SelectField <$> field
            ]

      objects = P.sepBy object comma

      object = lexeme do
        void $ lexeme $ P.string "{"
        kvs <-
          P.sepBy
            ( do
                k <-
                  lexeme $ do
                    void $ lexeme $ P.string "\""
                    P.takeWhile1P
                      Nothing
                      (/= '\"')
                      <* (void $ lexeme $ P.string "\"")
                lexeme $ colon
                v <- value
                pure (JK.fromString k, v)
            )
            comma
        void $ lexeme $ P.string "}"
        pure $ J.Object (JM.fromList kvs)

      value =
        P.choice
          [ object,
            array,
            string,
            number,
            bool,
            null_
          ]

      array = lexeme do
        void $ lexeme $ P.string "["
        vs <- P.sepBy value comma
        void $ lexeme $ P.string "]"
        pure $ J.Array (V.fromList vs)

      string = lexeme do
        void $ lexeme $ P.string "\""
        s <- P.takeWhileP Nothing (\c -> c /= '\"')
        void $ lexeme $ P.string "\""
        pure $ J.String (T.pack s)

      number = lexeme do
        J.Number <$> P.scientific

      bool = lexeme do
        J.Bool
          <$> P.choice
            [ const True <$> P.string "true",
              const False <$> P.string "false"
            ]

      null_ = lexeme do
        const J.Null <$> P.string "null"

      field :: P.Parsec Void String Field
      field =
        lexeme do
          Field
            <$> fieldPart
            <*> ( P.choice
                    [ do
                        void $ P.string "."
                        map T.pack <$> P.sepBy fieldPart (P.string "."),
                      do
                        pure []
                    ]
                )

      fieldPart :: P.Parsec Void String String
      fieldPart = P.takeWhile1P Nothing (\c -> not (isSpace c) && c /= ',' && c /= '.')

      joinType :: P.Parsec Void String JoinType
      joinType =
        P.choice
          [ do
              void $ lexeme (P.string "LEFT")
              void $ lexeme (P.string "JOIN")
              pure JoinLeft,
            do
              void $ lexeme (P.string "RIGHT")
              void $ lexeme (P.string "JOIN")
              pure JoinRight,
            do
              void $ lexeme (P.string "FULL")
              void $ lexeme (P.string "JOIN")
              pure JoinFull
          ]