diff options
Diffstat (limited to 'stdlib/source/library/lux/control/parser.lux')
-rw-r--r-- | stdlib/source/library/lux/control/parser.lux | 127 |
1 files changed, 87 insertions, 40 deletions
diff --git a/stdlib/source/library/lux/control/parser.lux b/stdlib/source/library/lux/control/parser.lux index c3c87c59f..9f2083573 100644 --- a/stdlib/source/library/lux/control/parser.lux +++ b/stdlib/source/library/lux/control/parser.lux @@ -13,11 +13,13 @@ [collection ["[0]" list (.use "[1]#[0]" functor monoid)]]]]]) -(type .public (Parser s a) - (-> s (Try [s a]))) +(type .public (Parser state of) + (-> state + (Try [state of]))) (def .public functor - (All (_ s) (Functor (Parser s))) + (All (_ state) + (Functor (Parser state))) (implementation (def (each f ma) (function (_ input) @@ -29,7 +31,8 @@ {try.#Failure msg}))))) (def .public apply - (All (_ s) (Apply (Parser s))) + (All (_ state) + (Apply (Parser state))) (implementation (def functor ..functor) @@ -48,7 +51,8 @@ {try.#Failure msg}))))) (def .public monad - (All (_ s) (Monad (Parser s))) + (All (_ state) + (Monad (Parser state))) (implementation (def functor ..functor) @@ -66,15 +70,18 @@ {try.#Failure msg}))))) (def .public (assertion message test) - (All (_ s) (-> Text Bit (Parser s Any))) + (All (_ state) + (-> Text Bit + (Parser state Any))) (function (_ input) (if test {try.#Success [input []]} {try.#Failure message}))) (def .public (maybe parser) - (All (_ s a) - (-> (Parser s a) (Parser s (Maybe a)))) + (All (_ state of) + (-> (Parser state of) + (Parser state (Maybe of)))) (function (_ input) (when (parser input) {try.#Success [input' x]} @@ -84,20 +91,23 @@ {try.#Success [input {.#None}]}))) (def .public (result parser input) - (All (_ s a) - (-> (Parser s a) s (Try [s a]))) + (All (_ state of) + (-> (Parser state of) state + (Try [state of]))) (parser input)) -(def .public (and first second) - (All (_ s a b) - (-> (Parser s a) (Parser s b) (Parser s [a b]))) +(def .public (and left right) + (All (_ state left right) + (-> (Parser state left) (Parser state right) + (Parser state (And left right)))) (do [! ..monad] - [head first] - (of ! each (|>> [head]) second))) + [head left] + (of ! each (|>> [head]) right))) (def .public (or left right) - (All (_ s a b) - (-> (Parser s a) (Parser s b) (Parser s (Or a b)))) + (All (_ state left right) + (-> (Parser state left) (Parser state right) + (Parser state (Or left right)))) (function (_ tokens) (when (left tokens) {try.#Success [tokens' output]} @@ -112,8 +122,9 @@ {try.#Failure error})))) (def .public (either this that) - (All (_ s a) - (-> (Parser s a) (Parser s a) (Parser s a))) + (All (_ state of) + (-> (Parser state of) (Parser state of) + (Parser state of))) (function (_ tokens) (when (this tokens) {try.#Failure _} @@ -123,8 +134,9 @@ success))) (def .public (some parser) - (All (_ s a) - (-> (Parser s a) (Parser s (List a)))) + (All (_ state of) + (-> (Parser state of) + (Parser state (List of)))) (function (_ input) (when (parser input) {try.#Success [input' head]} @@ -136,14 +148,17 @@ {try.#Success [input (list)]}))) (def .public (many parser) - (All (_ s a) - (-> (Parser s a) (Parser s (List a)))) + (All (_ state of) + (-> (Parser state of) + (Parser state (List of)))) (|> (..some parser) (..and parser) (of ..monad each (|>> {.#Item})))) (def .public (exactly amount parser) - (All (_ s a) (-> Nat (Parser s a) (Parser s (List a)))) + (All (_ state of) + (-> Nat (Parser state of) + (Parser state (List of)))) (when amount 0 (of ..monad in (list)) _ (do [! ..monad] @@ -153,13 +168,17 @@ (of ! each (|>> {.#Item x})))))) (def .public (at_least amount parser) - (All (_ s a) (-> Nat (Parser s a) (Parser s (List a)))) + (All (_ state of) + (-> Nat (Parser state of) + (Parser state (List of)))) (do [! ..monad] [minimum (..exactly amount parser)] (of ! each (list#composite minimum) (..some parser)))) (def .public (at_most amount parser) - (All (_ s a) (-> Nat (Parser s a) (Parser s (List a)))) + (All (_ state of) + (-> Nat (Parser state of) + (Parser state (List of)))) (when amount 0 (of ..monad in (list)) _ (function (_ input) @@ -173,7 +192,9 @@ {try.#Success [input (list)]})))) (def .public (between minimum additional parser) - (All (_ s a) (-> Nat Nat (Parser s a) (Parser s (List a)))) + (All (_ state of) + (-> Nat Nat (Parser state of) + (Parser state (List of)))) (do [! ..monad] [minimum (..exactly minimum parser)] (when additional @@ -182,7 +203,9 @@ (..at_most additional parser))))) (def .public (separated_by separator parser) - (All (_ s a b) (-> (Parser s b) (Parser s a) (Parser s (List a)))) + (All (_ state separator of) + (-> (Parser state separator) (Parser state of) + (Parser state (List of)))) (do [! ..monad] [?x (..maybe parser)] (when ?x @@ -196,7 +219,9 @@ (in {.#End})))) (def .public (not parser) - (All (_ s a) (-> (Parser s a) (Parser s Any))) + (All (_ state of) + (-> (Parser state of) + (Parser state Any))) (function (_ input) (when (parser input) {try.#Failure msg} @@ -206,7 +231,9 @@ {try.#Failure "Expected to fail; yet succeeded."}))) (def .public (failure message) - (All (_ s a) (-> Text (Parser s a))) + (All (_ state of) + (-> Text + (Parser state of))) (function (_ input) {try.#Failure message})) @@ -223,7 +250,9 @@ {try.#Failure error}))) (def .public (else value parser) - (All (_ s a) (-> a (Parser s a) (Parser s a))) + (All (_ state of) + (-> of (Parser state of) + (Parser state of))) (function (_ input) (when (parser input) {try.#Failure error} @@ -233,37 +262,49 @@ success))) (def .public remaining - (All (_ s) (Parser s s)) + (All (_ state) + (Parser state state)) (function (_ inputs) {try.#Success [inputs inputs]})) (def .public (rec parser) - (All (_ s a) (-> (-> (Parser s a) (Parser s a)) (Parser s a))) + (All (_ state of) + (-> (-> (Parser state of) + (Parser state of)) + (Parser state of))) (function (_ inputs) (..result (parser (rec parser)) inputs))) (def .public (after param subject) - (All (_ s _ a) (-> (Parser s _) (Parser s a) (Parser s a))) + (All (_ state _ of) + (-> (Parser state _) (Parser state of) + (Parser state of))) (do ..monad [_ param] subject)) (def .public (before param subject) - (All (_ s _ a) (-> (Parser s _) (Parser s a) (Parser s a))) + (All (_ state _ of) + (-> (Parser state _) (Parser state of) + (Parser state of))) (do ..monad [output subject _ param] (in output))) (def .public (only test parser) - (All (_ s a) (-> (-> a Bit) (Parser s a) (Parser s a))) + (All (_ state of) + (-> (-> of Bit) (Parser state of) + (Parser state of))) (do ..monad [output parser _ (..assertion "Constraint failed." (test output))] (in output))) (def .public (parses? parser) - (All (_ s a) (-> (Parser s a) (Parser s Bit))) + (All (_ state of) + (-> (Parser state of) + (Parser state Bit))) (function (_ input) (when (parser input) {try.#Success [input' _]} @@ -273,7 +314,9 @@ {try.#Success [input false]}))) (def .public (parses parser) - (All (_ s a) (-> (Parser s a) (Parser s Any))) + (All (_ state of) + (-> (Parser state of) + (Parser state Any))) (function (_ input) (when (parser input) {try.#Success [input' _]} @@ -283,7 +326,9 @@ {try.#Failure error}))) (def .public (speculative parser) - (All (_ s a) (-> (Parser s a) (Parser s a))) + (All (_ state of) + (-> (Parser state of) + (Parser state of))) (function (_ input) (when (parser input) {try.#Success [input' output]} @@ -293,7 +338,9 @@ failure))) (def .public (codec codec parser) - (All (_ s a z) (-> (Codec a z) (Parser s a) (Parser s z))) + (All (_ state medium of) + (-> (Codec medium of) (Parser state medium) + (Parser state of))) (function (_ input) (when (parser input) {try.#Success [input' to_decode]} |