@@ -8,12 +8,11 @@ import Prelude
88
99import Control.Alt ((<|>))
1010import Data.Array as A
11- import Data.Char as Ch
1211import Data.Either (Either )
1312import Data.HugeInt as HI
1413import Data.HugeNum as HN
1514import Data.Json.Extended.Signature.Parse as EJP
16- import Data.Maybe ( isJust )
15+ import Data.Set as Set
1716import Data.String as S
1817import SqlSquared.Utils ((∘))
1918import Text.Parsing.Parser as P
@@ -94,8 +93,8 @@ op = map Op $ PC.choice
9493 , PS .string " %"
9594 ]
9695
97- keywords ∷ Array String
98- keywords =
96+ keywords ∷ Set.Set String
97+ keywords = Set .fromFoldable
9998 [ " where"
10099 , " when"
101100 , " values"
@@ -152,8 +151,8 @@ keywords =
152151digits ∷ Array Char
153152digits = [' 0' ,' 1' ,' 2' ,' 3' ,' 4' ,' 5' ,' 6' ,' 7' ,' 8' ,' 9' ]
154153
155- ident ∷ ∀ m . Monad m ⇒ P.ParserT String m Token
156- ident = map Identifier $ PC .try quotedIdent <|> PC .try notQuotedIdent
154+ identOrKeyword ∷ ∀ m . Monad m ⇒ P.ParserT String m Token
155+ identOrKeyword = PC .try quotedIdent <|> notQuotedIdentOrKeyword
157156
158157oneLineComment ∷ ∀ m . Monad m ⇒ P.ParserT String m Token
159158oneLineComment =
@@ -178,25 +177,27 @@ multiLineComment = do
178177 _ →
179178 collectBeforeComment $ acc <> S .fromCharArray [ c ]
180179
181- quotedIdent ∷ ∀ m . Monad m ⇒ P.ParserT String m String
180+ quotedIdent ∷ ∀ m . Monad m ⇒ P.ParserT String m Token
182181quotedIdent =
183- PC .between (PS .string " `" ) (PS .string " `" )
184- $ map S .fromCharArray
185- $ A .some identChar
182+ map Identifier
183+ $ PC .between (PS .string " `" ) (PS .string " `" )
184+ $ map S .fromCharArray
185+ $ A .some identChar
186186 where
187187 identChar = PC .try identEscape <|> identLetter
188188 identLetter = PS .satisfy (not ∘ eq ' `' )
189189 identEscape = PS .string " \\ `" $> ' `'
190190
191- notQuotedIdent ∷ ∀ m . Monad m ⇒ P.ParserT String m String
192- notQuotedIdent = do
191+ notQuotedIdentOrKeyword ∷ ∀ m . Monad m ⇒ P.ParserT String m Token
192+ notQuotedIdentOrKeyword = do
193193 first ← PT .letter
194194 other ← A .many (PT .alphaNum <|> PS .char ' _' )
195195 let
196196 str = S .fromCharArray $ A .cons first other
197- if isJust $ A .elemIndex (S .toLower str) keywords
198- then P .fail " unexpected keyword"
199- else pure str
197+ low = S .toLower str
198+ pure if Set .member low keywords
199+ then Kw low
200+ else Identifier str
200201
201202stringLit ∷ ∀ m . Monad m ⇒ P.ParserT String m Token
202203stringLit = Lit ∘ String <$> EJP .parseStringLiteral
@@ -207,26 +208,14 @@ numLit = Lit ∘ Decimal <$> EJP.parseDecimalLiteral
207208intLit ∷ ∀ m . Monad m ⇒ P.ParserT String m Token
208209intLit = Lit ∘ Integer <$> EJP .parseHugeIntLiteral
209210
210- keyword ∷ ∀ m . Monad m ⇒ P.ParserT String m Token
211- keyword = map Kw $ PC .choice $ map (PC .try ∘ parseKeyWord) keywords
212-
213- parseKeyWord ∷ ∀ m . Monad m ⇒ String → P.ParserT String m String
214- parseKeyWord s =
215- map S .fromCharArray $ A .foldM foldFn [ ] $ S .toCharArray s
216- where
217- foldFn acc ch = do
218- c ← PC .try $ PS .oneOf [ Ch .toUpper ch, Ch .toLower ch ]
219- pure $ A .snoc acc c
220-
221211tokens ∷ ∀ m . Monad m ⇒ P.ParserT String m (Array Token )
222212tokens = do
223213 PS .skipSpaces
224214 A .some $ PC .choice
225215 [ skipped oneLineComment
226216 , skipped multiLineComment
227217 , skipped op
228- , skipped keyword
229- , skipped ident
218+ , skipped identOrKeyword
230219 , skipped numLit
231220 , skipped intLit
232221 , skipped stringLit
0 commit comments