Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Informatik 8
CoPaR
Commits
bd3e3a96
Commit
bd3e3a96
authored
Mar 13, 2019
by
Hans-Peter Deifel
Browse files
Add new lexer primitive to parse hexadecimal numbers
parent
349b3755
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/Copar/Parser/Lexer.hs
View file @
bd3e3a96
...
...
@@ -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 #-}
tests/Copar/Parser/LexerSpec.hs
View file @
bd3e3a96
...
...
@@ -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"
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment