Commit 4ae76e76 authored by Hans-Peter Deifel's avatar Hans-Peter Deifel
Browse files

Inline lexer combinators

This already improves the parser benchmark by a factor of two.
parent c815cc81
......@@ -17,6 +17,8 @@ module MA.Parser.Lexer
, signed
) where
import Data.Char
import Data.Text (Text)
import qualified Data.Text as T
import Text.Megaparsec
......@@ -27,51 +29,70 @@ import MA.Parser.Types
spaceConsumer :: MonadParser m => m ()
spaceConsumer = L.space space1 (L.skipLineComment "#") empty
{-# INLINE spaceConsumer #-}
lexeme :: MonadParser m => m a -> m a
lexeme = L.lexeme spaceConsumer
{-# INLINE lexeme #-}
symbol :: MonadParser m => Text -> m Text
symbol = L.symbol spaceConsumer
{-# INLINE symbol #-}
braces :: MonadParser m => m a -> m a
braces = between (symbol "{") (symbol "}")
{-# INLINE braces #-}
parens :: MonadParser m => m a -> m a
parens = between (symbol "(") (symbol ")")
{-# INLINE parens #-}
angles :: MonadParser m => m a -> m a
angles = between (symbol "<") (symbol ">")
{-# INLINE angles #-}
brackets :: MonadParser m => m a -> m a
brackets = between (symbol "[") (symbol "]")
{-# INLINE brackets #-}
semicolon :: MonadParser m => m Text
semicolon = symbol ";"
{-# INLINE semicolon #-}
comma :: MonadParser m => m Text
comma = symbol ","
{-# INLINE comma #-}
colon :: MonadParser m => m Text
colon = symbol ":"
{-# INLINE colon #-}
dot :: MonadParser m => m Text
dot = symbol "."
{-# INLINE dot #-}
name :: MonadParser m => m Text
name = lexeme (T.pack <$> ((:) <$> nameChar1 <*> many nameChar) <?> "name")
name = lexeme ((T.cons <$> nameChar1 <*> restName) <?> "name")
{-# INLINE name #-}
nameChar1 :: MonadParser m => m Char
nameChar1 = letterChar <|> char '_'
{-# INLINE nameChar1 #-}
nameChar :: MonadParser m => m Char
nameChar = nameChar1 <|> digitChar
restName :: MonadParser m => m Text
restName = takeWhileP Nothing isNameChar
where
isNameChar c = c == '_' || isLetter c || isDigit c
{-# INLINE restName #-}
decimal :: (MonadParser m, Integral a) => m a
decimal = lexeme L.decimal
{-# INLINE decimal #-}
float :: (MonadParser m, RealFloat a) => m a
float = lexeme L.float
{-# INLINE float #-}
signed :: (MonadParser m, Num a) => m a -> m a
signed = L.signed spaceConsumer
{-# INLINE signed #-}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment