forked from GitHub/gf-core
slight optimization in GF.Grammar.Lexer
This commit is contained in:
@@ -234,23 +234,23 @@ data AlexInput = AI {-# UNPACK #-} !Posn -- current position,
|
|||||||
{-# UNPACK #-} !BS.ByteString -- current input string
|
{-# UNPACK #-} !BS.ByteString -- current input string
|
||||||
|
|
||||||
data ParseResult a
|
data ParseResult a
|
||||||
= POk AlexInput a
|
= POk a
|
||||||
| PFailed Posn -- The position of the error
|
| PFailed Posn -- The position of the error
|
||||||
String -- The error message
|
String -- The error message
|
||||||
|
|
||||||
newtype P a = P { unP :: AlexInput -> ParseResult a }
|
newtype P a = P { unP :: AlexInput -> ParseResult a }
|
||||||
|
|
||||||
instance Monad P where
|
instance Monad P where
|
||||||
return a = a `seq` (P $ \s -> POk s a)
|
return a = a `seq` (P $ \s -> POk a)
|
||||||
(P m) >>= k = P $ \ s -> case m s of
|
(P m) >>= k = P $ \ s -> case m s of
|
||||||
POk s1 a -> unP (k a) s1
|
POk a -> unP (k a) s
|
||||||
PFailed posn err -> PFailed posn err
|
PFailed posn err -> PFailed posn err
|
||||||
fail msg = P $ \(AI posn _ _) -> PFailed posn msg
|
fail msg = P $ \(AI posn _ _) -> PFailed posn msg
|
||||||
|
|
||||||
runP :: P a -> BS.ByteString -> Either (Posn,String) a
|
runP :: P a -> BS.ByteString -> Either (Posn,String) a
|
||||||
runP (P f) txt =
|
runP (P f) txt =
|
||||||
case f (AI (Pn 1 0) ' ' txt) of
|
case f (AI (Pn 1 0) ' ' txt) of
|
||||||
POk _ x -> Right x
|
POk x -> Right x
|
||||||
PFailed pos msg -> Left (pos,msg)
|
PFailed pos msg -> Left (pos,msg)
|
||||||
|
|
||||||
failLoc :: Posn -> String -> P a
|
failLoc :: Posn -> String -> P a
|
||||||
@@ -267,6 +267,6 @@ lexer cont = P go
|
|||||||
AlexToken inp' len act -> unP (cont (act pos (BS.take len str))) inp'
|
AlexToken inp' len act -> unP (cont (act pos (BS.take len str))) inp'
|
||||||
|
|
||||||
getPosn :: P Posn
|
getPosn :: P Posn
|
||||||
getPosn = P $ \inp@(AI pos _ _) -> POk inp pos
|
getPosn = P $ \inp@(AI pos _ _) -> POk pos
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user