diff options
Diffstat (limited to 'stdlib/source/lux/control/parser')
-rw-r--r-- | stdlib/source/lux/control/parser/code.lux | 70 | ||||
-rw-r--r-- | stdlib/source/lux/control/parser/synthesis.lux | 40 |
2 files changed, 62 insertions, 48 deletions
diff --git a/stdlib/source/lux/control/parser/code.lux b/stdlib/source/lux/control/parser/code.lux index 56cbe5bc2..1e1287467 100644 --- a/stdlib/source/lux/control/parser/code.lux +++ b/stdlib/source/lux/control/parser/code.lux @@ -41,44 +41,42 @@ #.Nil (#error.Failure "There are no tokens to parse!") (#.Cons [t tokens']) (#error.Success [tokens' t])))) -(template [<get-name> <type> <tag> <eq> <desc>] - [(def: #export <get-name> - {#.doc (code.text ($_ text@compose "Parses the next " <desc> " input Code."))} - (Parser <type>) - (function (_ tokens) - (case tokens - (#.Cons [[_ (<tag> x)] tokens']) - (#error.Success [tokens' x]) - - _ - (#error.Failure ($_ text@compose "Cannot parse " <desc> (remaining-inputs tokens))))))] - - [ bit Bit #.Bit bit.equivalence "bit"] - [ nat Nat #.Nat nat.equivalence "nat"] - [ int Int #.Int int.equivalence "int"] - [ rev Rev #.Rev rev.equivalence "rev"] - [ frac Frac #.Frac frac.equivalence "frac"] - [ text Text #.Text text.equivalence "text"] - [identifier Name #.Identifier name.equivalence "identifier"] - [ tag Name #.Tag name.equivalence "tag"] +(template [<query> <assertion> <type> <tag> <eq> <desc>] + [(with-expansions [<error> (as-is (#error.Failure ($_ text@compose "Cannot parse " <desc> (remaining-inputs tokens))))] + (def: #export <query> + {#.doc (code.text ($_ text@compose "Parses the next " <desc> " input."))} + (Parser <type>) + (function (_ tokens) + (case tokens + (#.Cons [[_ (<tag> x)] tokens']) + (#error.Success [tokens' x]) + + _ + <error>))) + + (def: #export (<assertion> expected) + (-> <type> (Parser Any)) + (function (_ tokens) + (case tokens + (#.Cons [[_ (<tag> actual)] tokens']) + (if (:: <eq> = expected actual) + (#error.Success [tokens' []]) + <error>) + + _ + <error>))))] + + [bit bit! Bit #.Bit bit.equivalence "bit"] + [nat nat! Nat #.Nat nat.equivalence "nat"] + [int int! Int #.Int int.equivalence "int"] + [rev rev! Rev #.Rev rev.equivalence "rev"] + [frac frac! Frac #.Frac frac.equivalence "frac"] + [text text! Text #.Text text.equivalence "text"] + [identifier identifier! Name #.Identifier name.equivalence "identifier"] + [tag tag! Name #.Tag name.equivalence "tag"] ) -(def: #export (this? ast) - {#.doc "Asks if the given Code is the next input."} - (-> Code (Parser Bit)) - (function (_ tokens) - (case tokens - (#.Cons [token tokens']) - (let [is-it? (code@= ast token) - remaining (if is-it? - tokens' - tokens)] - (#error.Success [remaining is-it?])) - - _ - (#error.Success [tokens #0])))) - -(def: #export (this ast) +(def: #export (this! ast) {#.doc "Ensures the given Code is the next input."} (-> Code (Parser Any)) (function (_ tokens) diff --git a/stdlib/source/lux/control/parser/synthesis.lux b/stdlib/source/lux/control/parser/synthesis.lux index f08159848..c36c61601 100644 --- a/stdlib/source/lux/control/parser/synthesis.lux +++ b/stdlib/source/lux/control/parser/synthesis.lux @@ -6,7 +6,13 @@ ["." exception (#+ exception:)]] [data ["." error (#+ Error)] - [text + ["." bit] + ["." name] + [number + ["." i64] + ["." frac] + ["." nat]] + ["." text format]] [tool [compiler @@ -56,8 +62,8 @@ (#.Cons [head tail]) (#error.Success [tail head])))) -(template [<name> <tag> <type>] - [(def: #export <name> +(template [<query> <assertion> <tag> <type> <eq>] + [(def: #export <query> (Parser <type>) (.function (_ input) (case input @@ -65,17 +71,27 @@ (#error.Success [input' x]) _ + (exception.throw ..cannot-parse input)))) + + (def: #export (<assertion> expected) + (-> <type> (Parser Any)) + (.function (_ input) + (case input + (^ (list& (<tag> actual) input')) + (if (:: <eq> = expected actual) + (#error.Success [input' []]) + (exception.throw ..cannot-parse input)) + + _ (exception.throw ..cannot-parse input))))] - [bit /.bit Bit] - [i64 /.i64 (I64 Any)] - [f64 /.f64 Frac] - [text /.text Text] - [variant /.variant (Variant Synthesis)] - [local /.variable/local Nat] - [foreign /.variable/foreign Nat] - [constant /.constant Name] - [abstraction /.function/abstraction Abstraction] + [bit bit! /.bit Bit bit.equivalence] + [i64 i64! /.i64 (I64 Any) i64.equivalence] + [f64 f64! /.f64 Frac frac.equivalence] + [text text! /.text Text text.equivalence] + [local local! /.variable/local Nat nat.equivalence] + [foreign foreign! /.variable/foreign Nat nat.equivalence] + [constant constant! /.constant Name name.equivalence] ) (def: #export (tuple parser) |