diff options
Diffstat (limited to 'stdlib/source/library/lux/control/parser.lux')
-rw-r--r-- | stdlib/source/library/lux/control/parser.lux | 238 |
1 files changed, 133 insertions, 105 deletions
diff --git a/stdlib/source/library/lux/control/parser.lux b/stdlib/source/library/lux/control/parser.lux index 207e486de..cd25b8bbf 100644 --- a/stdlib/source/library/lux/control/parser.lux +++ b/stdlib/source/library/lux/control/parser.lux @@ -99,31 +99,6 @@ (Try [state of]))) (parser input)) -(def .public (and left right) - (All (_ state left right) - (-> (Parser state left) (Parser state right) - (Parser state (And left right)))) - (do [! ..monad] - [head left] - (of ! each (|>> [head]) right))) - -(def .public (or left right) - (All (_ state left right) - (-> (Parser state left) (Parser state right) - (Parser state (Or left right)))) - (function (_ tokens) - (when (left tokens) - {try.#Success [tokens' output]} - {try.#Success [tokens' {0 #0 output}]} - - {try.#Failure _} - (when (right tokens) - {try.#Success [tokens' output]} - {try.#Success [tokens' {0 #1 output}]} - - {try.#Failure error} - {try.#Failure error})))) - (def .public (either this that) (All (_ state of) (-> (Parser state of) (Parser state of) @@ -136,27 +111,137 @@ success success))) -(def .public (some parser) - (All (_ state of) - (-> (Parser state of) - (Parser state (List of)))) - (function (_ input) - (when (parser input) - {try.#Success [input' head]} - (..result (of ..monad each (|>> (list.partial head)) - (some parser)) - input') - - {try.#Failure _} - {try.#Success [input (list)]}))) - -(def .public (many parser) - (All (_ state of) - (-> (Parser state of) - (Parser state (List of)))) - (|> (..some parser) - (..and parser) - (of ..monad each (|>> {.#Item})))) +(with_expansions [<failure> {try.#Failure error} + <handle_failure!> (these <failure> + <failure>)] + (def .public (and left right) + (All (_ state left right) + (-> (Parser state left) (Parser state right) + (Parser state (And left right)))) + (function (_ state) + (when (left state) + {try.#Success [state left]} + (when (right state) + {try.#Success [state right]} + {try.#Success [state [left right]]} + + + <handle_failure!>) + + <handle_failure!>))) + + (def .public (or left right) + (All (_ state left right) + (-> (Parser state left) (Parser state right) + (Parser state (Or left right)))) + (function (_ tokens) + (when (left tokens) + {try.#Success [tokens' output]} + {try.#Success [tokens' {0 #0 output}]} + + {try.#Failure _} + (when (right tokens) + {try.#Success [tokens' output]} + {try.#Success [tokens' {0 #1 output}]} + + <handle_failure!>)))) + + (def .public (some it) + (All (_ state of) + (-> (Parser state of) + (Parser state (List of)))) + (function (_ state) + (loop (next [state state + output (list)]) + (when (it state) + {try.#Success [state head]} + (next state (list.partial head output)) + + {try.#Failure _} + {try.#Success [state (list.reversed output)]})))) + + (def .public (many parser) + (All (_ state of) + (-> (Parser state of) + (Parser state (List of)))) + (function (_ state) + (when (parser state) + {try.#Success [state head]} + (when (..some parser state) + {try.#Success [state tail]} + {try.#Success [state {.#Item head tail}]} + + <handle_failure!>) + + <handle_failure!>))) + + (def .public (after parameter it) + (All (_ state _ of) + (-> (Parser state _) (Parser state of) + (Parser state of))) + (function (_ state) + (when (parameter state) + {try.#Success [state _]} + (when (it state) + <handle_failure!> + + success + success) + + <handle_failure!>))) + + (def .public (before parameter it) + (All (_ state _ of) + (-> (Parser state _) (Parser state of) + (Parser state of))) + (function (_ state) + (when (it state) + {try.#Success [state it]} + (when (parameter state) + {try.#Success [state _]} + {try.#Success [state it]} + + <handle_failure!>) + + <handle_failure!>))) + + (def .public (of_try operation) + (All (_ state of) + (-> (Try of) + (Parser state of))) + (function (_ input) + (when operation + {try.#Success output} + {try.#Success [input output]} + + <handle_failure!>))) + + (def .public (parses parser) + (All (_ state of) + (-> (Parser state of) + (Parser state Any))) + (function (_ input) + (when (parser input) + {try.#Success [input' _]} + {try.#Success [input' []]} + + <handle_failure!>))) + + (def .public (codec codec parser) + (All (_ state medium of) + (-> (Codec medium of) (Parser state medium) + (Parser state of))) + (function (_ input) + (when (parser input) + {try.#Success [input' to_decode]} + (when (of codec decoded to_decode) + {try.#Success value} + {try.#Success [input' value]} + + <handle_failure!>) + + <handle_failure!>))) + ) (def .public (exactly amount parser) (All (_ state of) @@ -240,18 +325,6 @@ (function (_ input) {try.#Failure message})) -(def .public (of_try operation) - (All (_ state of) - (-> (Try of) - (Parser state of))) - (function (_ input) - (when operation - {try.#Success output} - {try.#Success [input output]} - - {try.#Failure error} - {try.#Failure error}))) - (def .public (else value parser) (All (_ state of) (-> of (Parser state of) @@ -276,24 +349,8 @@ (Parser state of)) (Parser state of))) (function (_ inputs) - (..result (parser (rec parser)) inputs))) - -(def .public (after param subject) - (All (_ state _ of) - (-> (Parser state _) (Parser state of) - (Parser state of))) - (do ..monad - [_ param] - subject)) - -(def .public (before param subject) - (All (_ state _ of) - (-> (Parser state _) (Parser state of) - (Parser state of))) - (do ..monad - [output subject - _ param] - (in output))) + (..result (parser (rec parser)) + inputs))) (def .public (only test parser) (All (_ state of) @@ -316,18 +373,6 @@ {try.#Failure error} {try.#Success [input false]}))) -(def .public (parses parser) - (All (_ state of) - (-> (Parser state of) - (Parser state Any))) - (function (_ input) - (when (parser input) - {try.#Success [input' _]} - {try.#Success [input' []]} - - {try.#Failure error} - {try.#Failure error}))) - (def .public (speculative parser) (All (_ state of) (-> (Parser state of) @@ -339,20 +384,3 @@ failure failure))) - -(def .public (codec codec parser) - (All (_ state medium of) - (-> (Codec medium of) (Parser state medium) - (Parser state of))) - (function (_ input) - (when (parser input) - {try.#Success [input' to_decode]} - (when (of codec decoded to_decode) - {try.#Success value} - {try.#Success [input' value]} - - {try.#Failure error} - {try.#Failure error}) - - {try.#Failure error} - {try.#Failure error}))) |