diff options
Diffstat (limited to 'stdlib/source/lux/control/parser.lux')
-rw-r--r-- | stdlib/source/lux/control/parser.lux | 138 |
1 files changed, 69 insertions, 69 deletions
diff --git a/stdlib/source/lux/control/parser.lux b/stdlib/source/lux/control/parser.lux index 095104f09..6ac2349ea 100644 --- a/stdlib/source/lux/control/parser.lux +++ b/stdlib/source/lux/control/parser.lux @@ -1,4 +1,4 @@ -(;module: +(.module: [lux #- not] (lux (control [functor #+ Functor] [applicative #+ Applicative] @@ -9,40 +9,40 @@ ["e" error]))) (type: #export (Parser s a) - {#;doc "A generic parser."} - (-> s (e;Error [s a]))) + {#.doc "A generic parser."} + (-> s (e.Error [s a]))) ## [Structures] (struct: #export Functor<Parser> (All [s] (Functor (Parser s))) (def: (map f ma) (function [input] (case (ma input) - (#e;Error msg) - (#e;Error msg) + (#e.Error msg) + (#e.Error msg) - (#e;Success [input' a]) - (#e;Success [input' (f a)]))))) + (#e.Success [input' a]) + (#e.Success [input' (f a)]))))) (struct: #export Applicative<Parser> (All [s] (Applicative (Parser s))) (def: functor Functor<Parser>) (def: (wrap x) (function [input] - (#e;Success [input x]))) + (#e.Success [input x]))) (def: (apply ff fa) (function [input] (case (ff input) - (#e;Success [input' f]) + (#e.Success [input' f]) (case (fa input') - (#e;Success [input'' a]) - (#e;Success [input'' (f a)]) + (#e.Success [input'' a]) + (#e.Success [input'' (f a)]) - (#e;Error msg) - (#e;Error msg)) + (#e.Error msg) + (#e.Error msg)) - (#e;Error msg) - (#e;Error msg))))) + (#e.Error msg) + (#e.Error msg))))) (struct: #export Monad<Parser> (All [s] (Monad (Parser s))) (def: applicative Applicative<Parser>) @@ -50,50 +50,50 @@ (def: (join mma) (function [input] (case (mma input) - (#e;Error msg) - (#e;Error msg) + (#e.Error msg) + (#e.Error msg) - (#e;Success [input' ma]) + (#e.Success [input' ma]) (ma input'))))) ## [Parsers] (def: #export (assert message test) - {#;doc "Fails with the given message if the test is false."} + {#.doc "Fails with the given message if the test is false."} (All [s] (-> Text Bool (Parser s Unit))) (function [input] (if test - (#e;Success [input []]) - (#e;Error message)))) + (#e.Success [input []]) + (#e.Error message)))) (def: #export (maybe p) - {#;doc "Optionality combinator."} + {#.doc "Optionality combinator."} (All [s a] (-> (Parser s a) (Parser s (Maybe a)))) (function [input] (case (p input) - (#e;Error _) (#e;Success [input #;None]) - (#e;Success [input' x]) (#e;Success [input' (#;Some x)])))) + (#e.Error _) (#e.Success [input #.None]) + (#e.Success [input' x]) (#e.Success [input' (#.Some x)])))) (def: #export (run input p) (All [s a] - (-> s (Parser s a) (e;Error [s a]))) + (-> s (Parser s a) (e.Error [s a]))) (p input)) (def: #export (some p) - {#;doc "0-or-more combinator."} + {#.doc "0-or-more combinator."} (All [s a] (-> (Parser s a) (Parser s (List a)))) (function [input] (case (p input) - (#e;Error _) (#e;Success [input (list)]) - (#e;Success [input' x]) (run input' + (#e.Error _) (#e.Success [input (list)]) + (#e.Success [input' x]) (run input' (do Monad<Parser> [xs (some p)] (wrap (list& x xs))) )))) (def: #export (many p) - {#;doc "1-or-more combinator."} + {#.doc "1-or-more combinator."} (All [s a] (-> (Parser s a) (Parser s (List a)))) (do Monad<Parser> @@ -102,7 +102,7 @@ (wrap (list& x xs)))) (def: #export (seq p1 p2) - {#;doc "Sequencing combinator."} + {#.doc "Sequencing combinator."} (All [s a b] (-> (Parser s a) (Parser s b) (Parser s [a b]))) (do Monad<Parser> @@ -111,40 +111,40 @@ (wrap [x1 x2]))) (def: #export (alt p1 p2) - {#;doc "Heterogeneous alternative combinator."} + {#.doc "Heterogeneous alternative combinator."} (All [s a b] (-> (Parser s a) (Parser s b) (Parser s (| a b)))) (function [tokens] (case (p1 tokens) - (#e;Success [tokens' x1]) (#e;Success [tokens' (+0 x1)]) - (#e;Error _) (run tokens + (#e.Success [tokens' x1]) (#e.Success [tokens' (+0 x1)]) + (#e.Error _) (run tokens (do Monad<Parser> [x2 p2] (wrap (+1 x2)))) ))) (def: #export (either pl pr) - {#;doc "Homogeneous alternative combinator."} + {#.doc "Homogeneous alternative combinator."} (All [s a] (-> (Parser s a) (Parser s a) (Parser s a))) (function [tokens] (case (pl tokens) - (#e;Error _) (pr tokens) + (#e.Error _) (pr tokens) output output ))) (def: #export (exactly n p) - {#;doc "Parse exactly N times."} + {#.doc "Parse exactly N times."} (All [s a] (-> Nat (Parser s a) (Parser s (List a)))) (if (n/> +0 n) (do Monad<Parser> [x p xs (exactly (n/dec n) p)] - (wrap (#;Cons x xs))) + (wrap (#.Cons x xs))) (:: Monad<Parser> wrap (list)))) (def: #export (at-least n p) - {#;doc "Parse at least N times."} + {#.doc "Parse at least N times."} (All [s a] (-> Nat (Parser s a) (Parser s (List a)))) (do Monad<Parser> [min (exactly n p) @@ -152,78 +152,78 @@ (wrap (list/compose min extra)))) (def: #export (at-most n p) - {#;doc "Parse at most N times."} + {#.doc "Parse at most N times."} (All [s a] (-> Nat (Parser s a) (Parser s (List a)))) (if (n/> +0 n) (function [input] (case (p input) - (#e;Error msg) - (#e;Success [input (list)]) + (#e.Error msg) + (#e.Success [input (list)]) - (#e;Success [input' x]) + (#e.Success [input' x]) (run input' (do Monad<Parser> [xs (at-most (n/dec n) p)] - (wrap (#;Cons x xs)))) + (wrap (#.Cons x xs)))) )) (:: Monad<Parser> wrap (list)))) (def: #export (between from to p) - {#;doc "Parse between N and M times."} + {#.doc "Parse between N and M times."} (All [s a] (-> Nat Nat (Parser s a) (Parser s (List a)))) (do Monad<Parser> [min-xs (exactly from p) max-xs (at-most (n/- from to) p)] - (wrap (:: list;Monad<List> join (list min-xs max-xs))))) + (wrap (:: list.Monad<List> join (list min-xs max-xs))))) (def: #export (sep-by sep p) - {#;doc "Parsers instances of 'p' that are separated by instances of 'sep'."} + {#.doc "Parsers instances of 'p' that are separated by instances of 'sep'."} (All [s a b] (-> (Parser s b) (Parser s a) (Parser s (List a)))) (do Monad<Parser> [?x (maybe p)] (case ?x - #;None - (wrap #;Nil) + #.None + (wrap #.Nil) - (#;Some x) + (#.Some x) (do @ [xs' (some (seq sep p))] - (wrap (#;Cons x (list/map product;right xs')))) + (wrap (#.Cons x (list/map product.right xs')))) ))) (def: #export (not p) (All [s a] (-> (Parser s a) (Parser s Unit))) (function [input] (case (p input) - (#e;Error msg) - (#e;Success [input []]) + (#e.Error msg) + (#e.Success [input []]) _ - (#e;Error "Expected to fail; yet succeeded.")))) + (#e.Error "Expected to fail; yet succeeded.")))) (def: #export (fail message) (All [s a] (-> Text (Parser s a))) (function [input] - (#e;Error message))) + (#e.Error message))) (def: #export (default value parser) - {#;doc "If the given parser fails, returns the default value."} + {#.doc "If the given parser fails, returns the default value."} (All [s a] (-> a (Parser s a) (Parser s a))) (function [input] (case (parser input) - (#e;Error error) - (#e;Success [input value]) + (#e.Error error) + (#e.Success [input value]) - (#e;Success [input' output]) - (#e;Success [input' output])))) + (#e.Success [input' output]) + (#e.Success [input' output])))) (def: #export remaining (All [s] (Parser s s)) (function [inputs] - (#e;Success [inputs inputs]))) + (#e.Success [inputs inputs]))) (def: #export (rec parser) - {#;doc "Combinator for recursive parser."} + {#.doc "Combinator for recursive parser."} (All [s a] (-> (-> (Parser s a) (Parser s a)) (Parser s a))) (function [inputs] (run inputs (parser (rec parser))))) @@ -249,16 +249,16 @@ (wrap output))) (def: #export (codec Codec<a,z> parser) - (All [s a z] (-> (codec;Codec a z) (Parser s a) (Parser s z))) + (All [s a z] (-> (codec.Codec a z) (Parser s a) (Parser s z))) (function [input] (case (parser input) - (#e;Error error) - (#e;Error error) + (#e.Error error) + (#e.Error error) - (#e;Success [input' to-decode]) + (#e.Success [input' to-decode]) (case (:: Codec<a,z> decode to-decode) - (#e;Error error) - (#e;Error error) + (#e.Error error) + (#e.Error error) - (#e;Success value) - (#e;Success [input' value]))))) + (#e.Success value) + (#e.Success [input' value]))))) |