Commit bd3e3a96 authored by Hans-Peter Deifel's avatar Hans-Peter Deifel 🐢

Add new lexer primitive to parse hexadecimal numbers

parent 349b3755
......@@ -23,6 +23,7 @@ module Copar.Parser.Lexer
, signed
, adouble
, complex
, hex
) where
import Data.Char
......@@ -182,3 +183,10 @@ complex inner =
optionalNum = fmap (fromMaybe 0) . optional
sumOf p1 p2 = (,) <$> signed p1 <*> optionalNum (mandatorySigned p2)
{-# INLINE complex #-}
-- | Parse a hexadecimal number consisting of the prefix "0x" followed by
-- hexadecimal digits (both upper and lower case).
hex :: (MonadParser m, Integral a) => m a
hex = lexeme (string "0x" *> L.hexadecimal)
{-# INLINE hex #-}
......@@ -13,6 +13,7 @@ spec :: Spec
spec = do
spaceSpec
complexSpec
hexSpec
spaceSpec :: Spec
spaceSpec = describe "whitespace" $ do
......@@ -128,3 +129,19 @@ complexSpec = describe "complex" $ do
it "parses a sum without whitespace" $
p "-8.5-8.5i" `shouldParse` ((-8.5) :+ (-8.5))
hexSpec :: Spec
hexSpec = describe "hex" $ do
let p = parse (L.hex <* eof) ""
it "parses uppercase digits" $
p "0xDEADBEEF" `shouldParse` (0xdeadbeef :: Word)
it "parses lowercase digits" $
p "0xdeadbeef" `shouldParse` (0xdeadbeef :: Word)
it "parses mixed digits" $
p "0xdeadBEEF" `shouldParse` (0xdeadbeef :: Word)
it "required the 0x prefix" $
p `shouldFailOn` "deadbeef"
Markdown is supported
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