Created
August 30, 2015 23:39
-
-
Save TheSeamau5/60ad2369faf5d1f66ed8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type alias Parser s a b = | |
{ static : StaticParser s | |
, dynamic : DynamicParser s a b | |
} | |
type alias StaticParser s = | |
{ flag : Bool | |
, startingChars : List s | |
} | |
type alias DynamicParser s a b = (a, List s) -> (b, List s) | |
arr : (a -> b) -> Parser s a b | |
arr f = | |
Parser (StaticParser True []) (\(a,s) -> (f a, s)) | |
map2 : (a -> b -> c) -> Parser s x a -> Parser s x b -> Parser s x c | |
map2 op f g = | |
(f &&& g) >>> arr (uncurry op) | |
andMap : Parser s x (a -> b) -> Parser s x a -> Parser s x b | |
andMap = | |
map2 (<|) | |
first : Parser s a b -> Parser s (a, c) (b, c) | |
first {static, dynamic} = | |
Parser static <| | |
\((a, c), s) -> | |
case dynamic (a,s) of | |
(b, s') -> | |
((b, c), s') | |
second : Parser s a b -> Parser s (c, a) (c, b) | |
second f = | |
arr swap >>> first f >>> arr swap | |
(***) : Parser s a c -> Parser s b d -> Parser s (a, b) (c, d) | |
(***) f g = | |
first f >>> second g | |
(&&&) : Parser s a b -> Parser s a c -> Parser s a (b, c) | |
(&&&) f g = | |
arr (\b -> (b,b)) >>> (f *** g) | |
(>>>) : Parser s a b -> Parser s b c -> Parser s a c | |
(>>>) parserAB parserBC = | |
let | |
static = | |
StaticParser | |
(parserAB.static.flag && parserBC.static.flag) | |
(parserAB.static.startingChars `union` (if parserAB.static.flag then parserBC.static.startingChars else [])) | |
dynamic = | |
parserAB.dynamic >> parserBC.dynamic | |
in | |
Parser static dynamic | |
---------------------------------- | |
swap : (a, b) -> (b, a) | |
swap (a, b) = (b, a) | |
union : List a -> List a -> List a | |
union list1 list2 = | |
case list2 of | |
[] -> | |
list1 | |
x :: xs -> | |
if List.member x list1 | |
then | |
union list1 xs | |
else | |
union (list1 ++ [x]) xs | |
------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment