Skip to content

Commit 348fdae

Browse files
authored
Merge pull request #46 from rintcius/fix-codepoint-anychar
Fix CodePoint.anyChar parser
2 parents b720043 + 326cd30 commit 348fdae

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Text/Parsing/StringParser/CodePoints.purs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ import Prelude
2626
import Control.Alt ((<|>))
2727
import Data.Array ((..))
2828
import Data.Array.NonEmpty as NEA
29-
import Data.Char (toCharCode)
29+
import Data.Char (fromCharCode, toCharCode)
3030
import Data.Either (Either(..))
31+
import Data.Enum (fromEnum)
3132
import Data.Foldable (class Foldable, foldMap, elem, notElem)
3233
import Data.Maybe (Maybe(..))
33-
import Data.String.CodePoints (drop, length, indexOf', stripPrefix)
34-
import Data.String.CodeUnits (charAt, singleton)
34+
import Data.String.CodePoints (codePointAt, drop, indexOf', length, stripPrefix)
35+
import Data.String.CodeUnits (singleton)
3536
import Data.String.Pattern (Pattern(..))
3637
import Data.String.Regex as Regex
3738
import Data.String.Regex.Flags (noFlags)
@@ -48,9 +49,13 @@ eof = Parser \s ->
4849
-- | Match any character.
4950
anyChar :: Parser Char
5051
anyChar = Parser \{ str, pos } ->
51-
case charAt pos str of
52-
Just chr -> Right { result: chr, suffix: { str, pos: pos + 1 } }
52+
case codePointAt pos str of
53+
Just cp -> case toChar cp of
54+
Just chr -> Right { result: chr, suffix: { str, pos: pos + 1 } }
55+
Nothing -> Left { pos, error: ParseError $ "CodePoint " <> show cp <> " is not a character" }
5356
Nothing -> Left { pos, error: ParseError "Unexpected EOF" }
57+
where
58+
toChar = fromCharCode <<< fromEnum
5459

5560
-- | Match any digit.
5661
anyDigit :: Parser Char

test/CodePoints.purs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Test.Assert (assert', assert)
1717
import Text.Parsing.StringParser (Parser, runParser, try)
1818
import Text.Parsing.StringParser.Combinators (many1, endBy1, sepBy1, optionMaybe, many, manyTill, many1Till, chainl, fix, between)
1919
import Text.Parsing.StringParser.Expr (Assoc(..), Operator(..), buildExprParser)
20-
import Text.Parsing.StringParser.CodePoints (anyDigit, eof, string, anyChar, regex)
20+
import Text.Parsing.StringParser.CodePoints (anyDigit, char, eof, string, anyChar, regex)
2121

2222
parens :: forall a. Parser a -> Parser a
2323
parens = between (string "(") (string ")")
@@ -97,3 +97,6 @@ testCodePoints = do
9797
assert $ canParse (many1Till (string "a") (string "and")) $ (fold <<< take 10000 $ repeat "a") <> "and"
9898
-- check correct order
9999
assert $ expectResult (NonEmptyList ('a' :| 'b':'c':Nil)) (many1Till anyChar (string "d")) "abcd"
100+
assert $ expectResult "\x458CA" (string "\x458CA" <* char ']' <* eof ) "\x458CA]"
101+
assert $ expectResult "\x458CA" (string "\x458CA" <* string ")" <* eof ) "\x458CA)"
102+
assert $ expectResult '\xEEE2' (char '\xEEE2' <* eof ) "\xEEE2"

0 commit comments

Comments
 (0)