|
| 1 | +-- | Primitive parsers for strings, parsing based on code units. |
| 2 | +-- | |
| 3 | +-- | These functions will be much faster than the `CodePoints` alternatives, but |
| 4 | +-- | will behave incorrectly when dealing with Unicode characters that consist |
| 5 | +-- | of multiple code units. |
| 6 | +module Text.Parsing.StringParser.CodeUnits |
| 7 | + ( eof |
| 8 | + , anyChar |
| 9 | + , anyDigit |
| 10 | + , string |
| 11 | + , satisfy |
| 12 | + , char |
| 13 | + , whiteSpace |
| 14 | + , skipSpaces |
| 15 | + , oneOf |
| 16 | + , noneOf |
| 17 | + , lowerCaseChar |
| 18 | + , upperCaseChar |
| 19 | + , anyLetter |
| 20 | + , alphaNum |
| 21 | + , regex |
| 22 | + ) where |
| 23 | + |
| 24 | +import Prelude |
| 25 | + |
| 26 | +import Control.Alt ((<|>)) |
| 27 | +import Data.Array ((..)) |
| 28 | +import Data.Array.NonEmpty as NEA |
| 29 | +import Data.Char (toCharCode) |
| 30 | +import Data.Either (Either(..)) |
| 31 | +import Data.Foldable (class Foldable, foldMap, elem, notElem) |
| 32 | +import Data.Maybe (Maybe(..)) |
| 33 | +import Data.String.CodeUnits (charAt, singleton) |
| 34 | +import Data.String.CodeUnits as SCU |
| 35 | +import Data.String.Pattern (Pattern(..)) |
| 36 | +import Data.String.Regex as Regex |
| 37 | +import Data.String.Regex.Flags (noFlags) |
| 38 | +import Text.Parsing.StringParser (Parser(..), ParseError(..), try, fail) |
| 39 | +import Text.Parsing.StringParser.Combinators (many, (<?>)) |
| 40 | + |
| 41 | +-- | Match the end of the file. |
| 42 | +eof :: Parser Unit |
| 43 | +eof = Parser \s -> |
| 44 | + case s of |
| 45 | + { str, pos } | pos < SCU.length str -> Left { pos, error: ParseError "Expected EOF" } |
| 46 | + _ -> Right { result: unit, suffix: s } |
| 47 | + |
| 48 | +-- | Match any character. |
| 49 | +anyChar :: Parser Char |
| 50 | +anyChar = Parser \{ str, pos } -> |
| 51 | + case charAt pos str of |
| 52 | + Just chr -> Right { result: chr, suffix: { str, pos: pos + 1 } } |
| 53 | + Nothing -> Left { pos, error: ParseError "Unexpected EOF" } |
| 54 | + |
| 55 | +-- | Match any digit. |
| 56 | +anyDigit :: Parser Char |
| 57 | +anyDigit = try do |
| 58 | + c <- anyChar |
| 59 | + if c >= '0' && c <= '9' |
| 60 | + then pure c |
| 61 | + else fail $ "Character " <> show c <> " is not a digit" |
| 62 | + |
| 63 | +-- | Match the specified string. |
| 64 | +string :: String -> Parser String |
| 65 | +string nt = Parser \s -> |
| 66 | + case s of |
| 67 | + { str, pos } | SCU.indexOf' (Pattern nt) pos str == Just pos -> Right { result: nt, suffix: { str, pos: pos + SCU.length nt } } |
| 68 | + { pos } -> Left { pos, error: ParseError ("Expected '" <> nt <> "'.") } |
| 69 | + |
| 70 | +-- | Match a character satisfying the given predicate. |
| 71 | +satisfy :: (Char -> Boolean) -> Parser Char |
| 72 | +satisfy f = try do |
| 73 | + c <- anyChar |
| 74 | + if f c |
| 75 | + then pure c |
| 76 | + else fail $ "Character " <> show c <> " did not satisfy predicate" |
| 77 | + |
| 78 | +-- | Match the specified character. |
| 79 | +char :: Char -> Parser Char |
| 80 | +char c = satisfy (_ == c) <?> "Could not match character " <> show c |
| 81 | + |
| 82 | +-- | Match many whitespace characters. |
| 83 | +whiteSpace :: Parser String |
| 84 | +whiteSpace = do |
| 85 | + cs <- many (satisfy \ c -> c == '\n' || c == '\r' || c == ' ' || c == '\t') |
| 86 | + pure (foldMap singleton cs) |
| 87 | + |
| 88 | +-- | Skip many whitespace characters. |
| 89 | +skipSpaces :: Parser Unit |
| 90 | +skipSpaces = void whiteSpace |
| 91 | + |
| 92 | +-- | Match one of the characters in the foldable structure. |
| 93 | +oneOf :: forall f. Foldable f => f Char -> Parser Char |
| 94 | +oneOf = satisfy <<< flip elem |
| 95 | + |
| 96 | +-- | Match any character not in the foldable structure. |
| 97 | +noneOf :: forall f. Foldable f => f Char -> Parser Char |
| 98 | +noneOf = satisfy <<< flip notElem |
| 99 | + |
| 100 | +-- | Match any lower case character. |
| 101 | +lowerCaseChar :: Parser Char |
| 102 | +lowerCaseChar = try do |
| 103 | + c <- anyChar |
| 104 | + if toCharCode c `elem` (97 .. 122) |
| 105 | + then pure c |
| 106 | + else fail $ "Expected a lower case character but found " <> show c |
| 107 | + |
| 108 | +-- | Match any upper case character. |
| 109 | +upperCaseChar :: Parser Char |
| 110 | +upperCaseChar = try do |
| 111 | + c <- anyChar |
| 112 | + if toCharCode c `elem` (65 .. 90) |
| 113 | + then pure c |
| 114 | + else fail $ "Expected an upper case character but found " <> show c |
| 115 | + |
| 116 | +-- | Match any letter. |
| 117 | +anyLetter :: Parser Char |
| 118 | +anyLetter = lowerCaseChar <|> upperCaseChar <?> "Expected a letter" |
| 119 | + |
| 120 | +-- | Match a letter or a number. |
| 121 | +alphaNum :: Parser Char |
| 122 | +alphaNum = anyLetter <|> anyDigit <?> "Expected a letter or a number" |
| 123 | + |
| 124 | +-- | match the regular expression |
| 125 | +regex :: String -> Parser String |
| 126 | +regex pat = |
| 127 | + case Regex.regex pattern noFlags of |
| 128 | + Left _ -> |
| 129 | + fail $ "Text.Parsing.StringParser.String.regex': illegal regex " <> pat |
| 130 | + Right r -> |
| 131 | + matchRegex r |
| 132 | + where |
| 133 | + -- ensure the pattern only matches the current position in the parse |
| 134 | + pattern = |
| 135 | + case SCU.stripPrefix (Pattern "^") pat of |
| 136 | + Nothing -> |
| 137 | + "^" <> pat |
| 138 | + _ -> |
| 139 | + pat |
| 140 | + matchRegex :: Regex.Regex -> Parser String |
| 141 | + matchRegex r = |
| 142 | + Parser \{ str, pos } -> |
| 143 | + let |
| 144 | + remainder = SCU.drop pos str |
| 145 | + in |
| 146 | + case NEA.head <$> Regex.match r remainder of |
| 147 | + Just (Just matched) -> |
| 148 | + Right { result: matched, suffix: { str, pos: pos + SCU.length matched } } |
| 149 | + _ -> |
| 150 | + Left { pos, error: ParseError "no match" } |
0 commit comments