Fix address and number parser

* The order in the address parser was wrong

* The signed number *optionally* required a sign. Due to this every
  address was relative. Now the `signedNumber` *required* a sign and the
  previous behaviour was renamed to `optionallySignedNumber`.
This commit is contained in:
I-Al-Istannen 2020-03-31 12:03:52 +02:00
parent f96f088eba
commit 5bfd2e4357

View file

@ -218,12 +218,15 @@ number =
(chunk "0x" *> hexadecimal) <|> (chunk "0x" *> hexadecimal) <|>
decimal decimal
optionallySignedNumber :: (Num a) => Parser a
optionallySignedNumber = signed (pure ()) number -- do not allow any space
signedNumber :: (Num a) => Parser a signedNumber :: (Num a) => Parser a
signedNumber = signed empty number signedNumber = lookAhead (char '+' <|> char '-') *> optionallySignedNumber
boundedNumber :: (Bounded n, Num n) => Parser n boundedNumber :: (Bounded n, Num n) => Parser n
boundedNumber = do boundedNumber = do
n <- signedNumber :: Parser Integer n <- optionallySignedNumber :: Parser Integer
when (n < minVal || n > maxVal) $ fail $ when (n < minVal || n > maxVal) $ fail $
"invalid range: " ++ "invalid range: " ++
show n ++ " is not between " ++ show n ++ " is not between " ++
@ -235,8 +238,8 @@ boundedNumber = do
address :: Parser (Address Span) address :: Parser (Address Span)
address = address =
fmap (uncurry AddressAbsolute) (withSpan boundedNumber) <|> fmap (uncurry AddressRelative) (withSpan signedNumber) <|>
fmap (uncurry AddressRelative) (withSpan signedNumber) fmap (uncurry AddressAbsolute) (withSpan boundedNumber)
location :: Parser (Location Span) location :: Parser (Location Span)
location = (LocationAddress <$> address) <|> (LocationLabel <$> name) location = (LocationAddress <$> address) <|> (LocationLabel <$> name)