@@ -6,6 +6,7 @@ module Language.PureScript.Parser.Lexer
66 , Token()
77 , TokenParser()
88 , lex
9+ , lex'
910 , anyToken
1011 , token
1112 , match
@@ -69,6 +70,8 @@ import Control.Monad (void, guard)
6970
7071import Data.Char (isSpace, isAscii, isSymbol, isAlphaNum)
7172import Data.Functor.Identity
73+import Data.Text (Text)
74+import qualified Data.Text as T
7275
7376import Language.PureScript.Comments
7477import Language.PureScript.Parser.State
@@ -154,8 +157,13 @@ data PositionedToken = PositionedToken
154157instance Show PositionedToken where
155158 show = prettyPrintToken . ptToken
156159
160+type Lexer u a = P.Parsec Text u a
161+
157162lex :: FilePath -> String -> Either P.ParseError [PositionedToken]
158−lex f s = updatePositions <$> P.parse parseTokens f s
163+lex fp = lex' fp . T.pack
164+
165+lex' :: FilePath -> Text -> Either P.ParseError [PositionedToken]
166+lex' f s = updatePositions <$> P.parse parseTokens f s
159167
160168updatePositions :: [PositionedToken] -> [PositionedToken]
161169updatePositions [] = []
@@ -163,22 +171,22 @@ updatePositions (x:xs) = x : zipWith update (x:xs) xs
163171 where
164172 update PositionedToken { ptEndPos = pos } pt = pt { ptPrevEndPos = Just pos }
165173
166−parseTokens :: P.Parsec String u [PositionedToken]
174+parseTokens :: Lexer u [PositionedToken]
167175parseTokens = whitespace *> P.many parsePositionedToken <* P.skipMany parseComment <* P.eof
168176
169−whitespace :: P.Parsec String u ()
177+whitespace :: Lexer u ()
170178whitespace = P.skipMany (P.satisfy isSpace)
171179
172−parseComment :: P.Parsec String u Comment
180+parseComment :: Lexer u Comment
173181parseComment = (BlockComment <$> blockComment <|> LineComment <$> lineComment) <* whitespace
174182 where
175− blockComment :: P.Parsec String u String
183+ blockComment :: Lexer u String
176184 blockComment = P.try $ P.string "{-" *> P.manyTill P.anyChar (P.try (P.string "-}"))
177185
178− lineComment :: P.Parsec String u String
186+ lineComment :: Lexer u String
179187 lineComment = P.try $ P.string "--" *> P.manyTill P.anyChar (P.try (void (P.char '\n') <|> P.eof))
180188
181−parsePositionedToken :: P.Parsec String u PositionedToken
189+parsePositionedToken :: Lexer u PositionedToken
182190parsePositionedToken = P.try $ do
183191 comments <- P.many parseComment
184192 pos <- P.getPosition
@@ -187,7 +195,7 @@ parsePositionedToken = P.try $ do
187195 whitespace
188196 return $ PositionedToken pos pos' Nothing tok comments
189197
190−parseToken :: P.Parsec String u Token
198+parseToken :: Lexer u Token
191199parseToken = P.choice
192200 [ P.try $ P.string "<-" *> P.notFollowedBy symbolChar *> pure LArrow
193201 , P.try $ P.string "←" *> P.notFollowedBy symbolChar *> pure LArrow
@@ -226,34 +234,34 @@ parseToken = P.choice
226234 ]
227235
228236 where
229− parseLName :: P.Parsec String u String
237+ parseLName :: Lexer u String
230238 parseLName = (:) <$> identStart <*> P.many identLetter
231239
232− parseUName :: P.Parsec String u String
240+ parseUName :: Lexer u String
233241 parseUName = (:) <$> P.upper <*> P.many identLetter
234242
235− parseSymbol :: P.Parsec String u String
243+ parseSymbol :: Lexer u String
236244 parseSymbol = P.many1 symbolChar
237245
238− identStart :: P.Parsec String u Char
246+ identStart :: Lexer u Char
239247 identStart = P.lower <|> P.oneOf "_"
240248
241− identLetter :: P.Parsec String u Char
249+ identLetter :: Lexer u Char
242250 identLetter = P.alphaNum <|> P.oneOf "_'"
243251
244− symbolChar :: P.Parsec String u Char
252+ symbolChar :: Lexer u Char
245253 symbolChar = P.satisfy isSymbolChar
246254
247− parseCharLiteral :: P.Parsec String u Char
255+ parseCharLiteral :: Lexer u Char
248256 parseCharLiteral = PT.charLiteral tokenParser
249257
250− parseStringLiteral :: P.Parsec String u String
258+ parseStringLiteral :: Lexer u String
251259 parseStringLiteral = blockString <|> PT.stringLiteral tokenParser
252260 where
253261 delimiter = P.try (P.string "\"\"\"")
254262 blockString = delimiter >> P.manyTill P.anyChar delimiter
255263
256− parseNumber :: P.Parsec String u (Either Integer Double)
264+ parseNumber :: Lexer u (Either Integer Double)
257265 parseNumber = (consumeLeadingZero >> P.parserZero) <|>
258266 (Right <$> P.try (PT.float tokenParser) <|>
259267 Left <$> P.try (PT.natural tokenParser))
@@ -267,7 +275,7 @@ parseToken = P.choice
267275-- |
268276-- We use Text.Parsec.Token to implement the string and number lexemes
269277--
270−langDef :: PT.GenLanguageDef String u Identity
278+langDef :: PT.GenLanguageDef Text u Identity
271279langDef = PT.LanguageDef
272280 { PT.reservedNames = []
273281 , PT.reservedOpNames = []
@@ -285,7 +293,7 @@ langDef = PT.LanguageDef
285293-- |
286294-- A token parser based on the language definition
287295--
288−tokenParser :: PT.GenTokenParser String u Identity
296+tokenParser :: PT.GenTokenParser Text u Identity
289297tokenParser = PT.makeTokenParser langDef
290298
291299type TokenParser a = P.Parsec [PositionedToken] ParseState a