Skip to content

Commit d02e52a

Browse files
committed
generalize StringLike to StreamLike
1 parent f9388a1 commit d02e52a

File tree

2 files changed

+94
-53
lines changed

2 files changed

+94
-53
lines changed

src/Text/Parsing/Parser/Pos.purs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Text.Parsing.Parser.Pos where
33
import Prelude
44
import Data.Foldable (foldl)
55
import Data.Newtype (wrap)
6-
import Data.String (split)
6+
import Data.String (toCharArray)
77

88
-- | `Position` represents the position of the parser in the input.
99
-- |
@@ -27,10 +27,11 @@ initialPos = Position { line: 1, column: 1 }
2727

2828
-- | Updates a `Position` by adding the columns and lines in `String`.
2929
updatePosString :: Position -> String -> Position
30-
updatePosString pos' str = foldl updatePosChar pos' (split (wrap "") str)
31-
where
32-
updatePosChar (Position pos) c = case c of
33-
"\n" -> Position { line: pos.line + 1, column: 1 }
34-
"\r" -> Position { line: pos.line + 1, column: 1 }
35-
"\t" -> Position { line: pos.line, column: pos.column + 8 - ((pos.column - 1) `mod` 8) }
36-
_ -> Position { line: pos.line, column: pos.column + 1 }
30+
updatePosString pos' str = foldl updatePosChar pos' (toCharArray str)
31+
32+
updatePosChar :: Position -> Char -> Position
33+
updatePosChar (Position pos) c = case c of
34+
'\n' -> Position { line: pos.line + 1, column: 1 }
35+
'\r' -> Position { line: pos.line + 1, column: 1 }
36+
'\t' -> Position { line: pos.line, column: pos.column + 8 - ((pos.column - 1) `mod` 8) }
37+
_ -> Position { line: pos.line, column: pos.column + 1 }

src/Text/Parsing/Parser/String.purs

Lines changed: 85 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,129 @@
22

33
module Text.Parsing.Parser.String where
44

5+
6+
import Control.Monad.Rec.Class (tailRecM3, Step(..))
57
import Data.String as S
68
import Control.Monad.State (modify, gets)
7-
import Data.Array (many)
8-
import Data.Foldable (elem, notElem)
9+
import Data.Array (many, toUnfoldable)
10+
import Data.Foldable (elem, notElem, foldMap)
11+
import Data.Unfoldable (class Unfoldable)
12+
import Data.List as L
913
import Data.Maybe (Maybe(..))
10-
import Data.Newtype (wrap)
11-
import Data.String (Pattern, fromCharArray, length, singleton)
14+
import Data.Either (Either(..))
15+
import Data.Monoid (class Monoid)
1216
import Text.Parsing.Parser (ParseState(..), ParserT, fail)
1317
import Text.Parsing.Parser.Combinators (try, (<?>))
14-
import Text.Parsing.Parser.Pos (updatePosString)
18+
import Text.Parsing.Parser.Pos (Position, updatePosString, updatePosChar)
1519
import Prelude hiding (between)
20+
import Data.Foldable (foldl)
21+
22+
-- | A newtype used in cases where there is a prefix string to droped.
23+
newtype Prefix f = Prefix f
24+
25+
derive instance eqPrefix :: Eq f => Eq (Prefix f)
26+
derive instance ordPrefix :: Ord f => Ord (Prefix f)
27+
-- derive instance newtypePrefix :: Newtype Prefix _
28+
29+
instance showPrefix :: Show f => Show (Prefix f) where
30+
show (Prefix s) = "(Prefix " <> show s <> ")"
1631

1732
-- | This class exists to abstract over streams which support the string-like
1833
-- | operations which this modules needs.
19-
class StringLike s where
20-
drop :: Int -> s -> s
21-
indexOf :: Pattern -> s -> Maybe Int
22-
null :: s -> Boolean
23-
uncons :: s -> Maybe { head :: Char, tail :: s }
24-
25-
instance stringLikeString :: StringLike String where
26-
uncons = S.uncons
27-
drop = S.drop
28-
indexOf = S.indexOf
29-
null = S.null
30-
31-
-- | Match end-of-file.
32-
eof :: forall s m. StringLike s => Monad m => ParserT s m Unit
34+
-- |
35+
-- | Instances must satisfy the following laws:
36+
-- |
37+
type Single f c = { head :: c, singleton :: f }
38+
39+
class HasUpdatePosition a where
40+
updatePos :: Position -> a -> Position
41+
42+
instance stringHasUpdatePosition :: HasUpdatePosition String where
43+
updatePos = updatePosString
44+
45+
instance charHasUpdatePosition :: HasUpdatePosition Char where
46+
updatePos = updatePosChar
47+
48+
class StreamLike f c | f -> c where
49+
uncons :: f -> Maybe { head :: c, tail :: f, updatePos :: (Position -> Position) }
50+
drop :: Prefix f -> f -> Maybe { rest :: f, updatePos :: (Position -> Position) }
51+
52+
instance stringLikeString :: StreamLike String Char where
53+
uncons f = S.uncons f <#> \({ head, tail}) ->
54+
{ head: head, updatePos: (_ `updatePos` head), tail}
55+
drop (Prefix p) s = S.stripPrefix (S.Pattern p) s <#> \rest ->
56+
{ rest: rest, updatePos: (_ `updatePos` p)}
57+
58+
instance listcharLikeString :: (Eq a, HasUpdatePosition a) => StreamLike (L.List a) a where
59+
uncons f = L.uncons f <#> \({ head, tail}) ->
60+
{ head: head, updatePos: (_ `updatePos` head), tail}
61+
drop (Prefix p') s' = case (tailRecM3 go p' s' id) of -- no MonadRec for Maybe
62+
Right a -> pure a
63+
_ -> Nothing
64+
where
65+
go prefix input updatePos' = case prefix, input of
66+
(L.Cons p ps), (L.Cons i is) | p == i -> pure $ Loop
67+
({ a: ps, b: is, c: updatePos' >>> (_ `updatePos` p) })
68+
(L.Nil), is -> pure $ Done
69+
({ rest: is, updatePos: updatePos' })
70+
_, _ -> Left unit
71+
72+
eof :: forall f c m. StreamLike f c => Monad m => ParserT f m Unit
3373
eof = do
3474
input <- gets \(ParseState input _ _) -> input
35-
unless (null input) (fail "Expected EOF")
75+
case uncons input of
76+
Nothing -> pure unit
77+
_ -> fail "Expected EOF"
3678

3779
-- | Match the specified string.
38-
string :: forall s m. StringLike s => Monad m => String -> ParserT s m String
80+
string :: forall f c m. StreamLike f c => Show f => Monad m => f -> ParserT f m f
3981
string str = do
4082
input <- gets \(ParseState input _ _) -> input
41-
case indexOf (wrap str) input of
42-
Just 0 -> do
83+
case drop (Prefix str) input of
84+
Just {rest, updatePos} -> do
4385
modify \(ParseState _ position _) ->
44-
ParseState (drop (length str) input)
45-
(updatePosString position str)
46-
true
86+
ParseState rest (updatePos position) true
4787
pure str
4888
_ -> fail ("Expected " <> show str)
4989

5090
-- | Match any character.
51-
anyChar :: forall s m. StringLike s => Monad m => ParserT s m Char
91+
anyChar :: forall f c m. StreamLike f c => Monad m => ParserT f m c
5292
anyChar = do
5393
input <- gets \(ParseState input _ _) -> input
5494
case uncons input of
5595
Nothing -> fail "Unexpected EOF"
56-
Just { head, tail } -> do
96+
Just ({ head, updatePos, tail }) -> do
5797
modify \(ParseState _ position _) ->
58-
ParseState tail
59-
(updatePosString position (singleton head))
60-
true
98+
ParseState tail (updatePos position) true
6199
pure head
62100

63101
-- | Match a character satisfying the specified predicate.
64-
satisfy :: forall s m. StringLike s => Monad m => (Char -> Boolean) -> ParserT s m Char
102+
satisfy :: forall f c m. StreamLike f c => Show c => Monad m => (c -> Boolean) -> ParserT f m c
65103
satisfy f = try do
66104
c <- anyChar
67105
if f c then pure c
68-
else fail $ "Character '" <> singleton c <> "' did not satisfy predicate"
106+
else fail $ "Character " <> show c <> " did not satisfy predicate"
69107

70108
-- | Match the specified character
71-
char :: forall s m. StringLike s => Monad m => Char -> ParserT s m Char
109+
char :: forall f c m. StreamLike f c => Eq c => Show c => Monad m => c -> ParserT f m c
72110
char c = satisfy (_ == c) <?> ("Expected " <> show c)
73111

74-
-- | Match a whitespace character.
75-
whiteSpace :: forall s m. StringLike s => Monad m => ParserT s m String
76-
whiteSpace = do
77-
cs <- many $ satisfy \c -> c == '\n' || c == '\r' || c == ' ' || c == '\t'
78-
pure $ fromCharArray cs
112+
-- | Match many whitespace characters.
113+
whiteSpace :: forall f m g. StreamLike f Char => Unfoldable g => Monoid f => Monad m => ParserT f m (g Char)
114+
whiteSpace = map toUnfoldable whiteSpace'
115+
116+
-- | Match a whitespace characters but returns them as Array.
117+
whiteSpace' :: forall f m. StreamLike f Char => Monad m => ParserT f m (Array Char)
118+
whiteSpace' = many $ satisfy \c -> c == '\n' || c == '\r' || c == ' ' || c == '\t'
79119

80120
-- | Skip whitespace characters.
81-
skipSpaces :: forall s m. StringLike s => Monad m => ParserT s m Unit
82-
skipSpaces = void whiteSpace
121+
skipSpaces :: forall f m. StreamLike f Char => Monad m => ParserT f m Unit
122+
skipSpaces = void whiteSpace'
83123

84124
-- | Match one of the characters in the array.
85-
oneOf :: forall s m. StringLike s => Monad m => Array Char -> ParserT s m Char
86-
oneOf ss = satisfy (flip elem ss) <?> ("Expected one of " <> show ss)
125+
oneOf :: forall f c m. StreamLike f c => Show c => Eq c => Monad m => Array c -> ParserT f m c
126+
oneOf ss = satisfy (flip elem ss) <?> ("one of " <> show ss)
87127

88128
-- | Match any character not in the array.
89-
noneOf :: forall s m. StringLike s => Monad m => Array Char -> ParserT s m Char
90-
noneOf ss = satisfy (flip notElem ss) <?> ("Expected none of " <> show ss)
129+
noneOf :: forall f c m. StreamLike f c => Show c => Eq c => Monad m => Array c -> ParserT f m c
130+
noneOf ss = satisfy (flip notElem ss) <?> ("none of " <> show ss)

0 commit comments

Comments
 (0)