on master help sign in
Text.Lexer: hexDigit, hexDigits, hexLit
55762f6Michael Morgan
libs/contrib/Text/Lexer.idr+18−3
@@ -144,12 +144,12 @@ takeToken lex str
144 = do i <- scan lex 0 str -- i must be > 0 if successful
144 = do i <- scan lex 0 str -- i must be > 0 if successful
145 pure (substr 0 i (getString str), strTail i str)
145 pure (substr 0 i (getString str), strTail i str)
146
146
147−||| Recognise a digit 0-9
147+||| Recognise a single digit 0-9
148export
148export
149digit : Lexer
149digit : Lexer
150digit = pred isDigit
150digit = pred isDigit
151
151
152−||| Recognise one or more digits 0-9
152+||| Recognise one or more digits
153export
153export
154digits : Lexer
154digits : Lexer
155digits = some digit
155digits = some digit
@@ -162,7 +162,17 @@ exact str with (unpack str)
162 exact str | (x :: xs)
162 exact str | (x :: xs)
163 = foldl SeqEmpty (is x) (map is xs)
163 = foldl SeqEmpty (is x) (map is xs)
164
164
165+||| Recognise a single hexidecimal digit
166+export
167+hexDigit : Lexer
168+hexDigit = digit <|> oneOf "abcdefABCDEF"
169+
170+||| Recognise one or more hexidecimal digits
171+export
172+hexDigits : Lexer
173+hexDigits = some hexDigit
174+
165−||| Recognise an alpha character
175+||| Recognise a single alpha character
166export
176export
167alpha : Lexer
177alpha : Lexer
168alpha = pred isAlpha
178alpha = pred isAlpha
@@ -247,6 +257,11 @@ export
247intLit : Lexer
257intLit : Lexer
248intLit = opt (is '-') <+> digits
258intLit = opt (is '-') <+> digits
249
259
260+||| Recognise a hexidecimal literal, prefixed by "0x" or "0X"
261+export
262+hexLit : Lexer
263+hexLit = is '0' <+> oneOf "xX" <+> hexDigits
264+
250||| A mapping from lexers to the tokens they produce.
265||| A mapping from lexers to the tokens they produce.
251||| This is a list of pairs `(Lexer, String -> tokenType)`
266||| This is a list of pairs `(Lexer, String -> tokenType)`
252||| For each Lexer in the list, if a substring in the input matches, run
267||| For each Lexer in the list, if a substring in the input matches, run