Add ranges to /codepoints
This commit is contained in:
parent
944edaf445
commit
c7e378122d
@ -127,8 +127,8 @@ newtype CodepointsModel = CodepointsModel
|
||||
{ codepoints :: [(Word, Either String Char)]
|
||||
}
|
||||
|
||||
mkCodepointsModel :: [Word] -> CodepointsModel
|
||||
mkCodepointsModel = CodepointsModel . map go
|
||||
mkCodepointsModel :: [(Word, Word)] -> CodepointsModel
|
||||
mkCodepointsModel = CodepointsModel . map go . concatMap (uncurry enumFromTo)
|
||||
where
|
||||
go codepoint = (codepoint, toChar codepoint)
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module UToy.Parsers
|
||||
( parseHexBytes
|
||||
, parseCodepoints
|
||||
@ -29,20 +31,31 @@ hexBytes = hexByte `Atto.sepBy` separators
|
||||
| 'a' <= c && c <= 'f' = ord c - ord 'a' + 10
|
||||
| otherwise = error $ printf "not a hex digit: %c" c
|
||||
|
||||
parseCodepoints :: Text -> Either String [Word]
|
||||
parseCodepoints :: Text -> Either String [(Word, Word)]
|
||||
parseCodepoints = Atto.parseOnly $ codepoints <* Atto.endOfInput
|
||||
|
||||
codepoints :: Atto.Parser [Word]
|
||||
codepoints = codepoint `Atto.sepBy` separators
|
||||
where
|
||||
codepoint = Atto.choice [literal, decLiteral, hexLiteral]
|
||||
codepoints :: Atto.Parser [(Word, Word)]
|
||||
codepoints = codepointRange `Atto.sepBy` separators
|
||||
|
||||
codepointRange :: Atto.Parser (Word, Word)
|
||||
codepointRange = do
|
||||
codepoint1 <- codepoint
|
||||
codepoint2 <- Atto.choice [Atto.skip (== '-') *> codepoint, pure codepoint1]
|
||||
|
||||
pure (codepoint1, codepoint2)
|
||||
|
||||
|
||||
|
||||
codepoint :: Atto.Parser Word
|
||||
codepoint = Atto.choice [literal, decLiteral, hexLiteral, uCodepoint]
|
||||
where
|
||||
literal = Atto.decimal
|
||||
|
||||
decLiteral = Atto.char 'd' *> Atto.decimal
|
||||
|
||||
hexLiteral = Atto.char 'x' *> Atto.hexadecimal
|
||||
|
||||
uCodepoint = Atto.string "U+" *> Atto.hexadecimal
|
||||
|
||||
-- Common
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user