From 104130efba46a875eba566384578f8aa8593ad37 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 1 Jul 2022 18:53:58 -0400 Subject: Moved "lux/macro/code" to "lux/meta/code". --- stdlib/source/parser/lux/data/format/json.lux | 6 +- stdlib/source/parser/lux/data/text.lux | 5 +- stdlib/source/parser/lux/macro/code.lux | 226 -------------------------- stdlib/source/parser/lux/meta/code.lux | 225 +++++++++++++++++++++++++ stdlib/source/parser/lux/meta/type.lux | 7 +- 5 files changed, 235 insertions(+), 234 deletions(-) delete mode 100644 stdlib/source/parser/lux/macro/code.lux create mode 100644 stdlib/source/parser/lux/meta/code.lux (limited to 'stdlib/source/parser') diff --git a/stdlib/source/parser/lux/data/format/json.lux b/stdlib/source/parser/lux/data/format/json.lux index c78f8132c..ddb73da8f 100644 --- a/stdlib/source/parser/lux/data/format/json.lux +++ b/stdlib/source/parser/lux/data/format/json.lux @@ -14,11 +14,11 @@ ["[0]" list (.use "[1]#[0]" functor)] ["[0]" sequence] ["[0]" dictionary (.only Dictionary)]]] - [macro - ["[0]" code]] [math [number - ["[0]" frac]]]]] + ["[0]" frac]]] + [meta + ["[0]" code]]]] [\\library ["[0]" / (.only JSON)]]) diff --git a/stdlib/source/parser/lux/data/text.lux b/stdlib/source/parser/lux/data/text.lux index 3cd4076f5..a85ef474a 100644 --- a/stdlib/source/parser/lux/data/text.lux +++ b/stdlib/source/parser/lux/data/text.lux @@ -15,11 +15,12 @@ ["[0]" list (.use "[1]#[0]" mix)]]] [macro ["^" pattern] - ["[0]" code] ["[0]" template]] [math [number - ["n" nat (.use "[1]#[0]" decimal)]]]]]) + ["n" nat (.use "[1]#[0]" decimal)]]] + [meta + ["[0]" code]]]]) (type .public Offset Nat) diff --git a/stdlib/source/parser/lux/macro/code.lux b/stdlib/source/parser/lux/macro/code.lux deleted file mode 100644 index 85490e2d0..000000000 --- a/stdlib/source/parser/lux/macro/code.lux +++ /dev/null @@ -1,226 +0,0 @@ -(.require - [library - [lux (.except nat int rev local not symbol local global) - [abstract - ["[0]" monad (.only do)]] - [control - ["//" parser] - ["[0]" try (.only Try)]] - [data - ["[0]" bit] - ["[0]" text (.use "[1]#[0]" monoid)] - [collection - ["[0]" list (.use "[1]#[0]" functor)]]] - [macro - ["[0]" code (.use "[1]#[0]" equivalence)]] - [math - [number - ["[0]" nat] - ["[0]" int] - ["[0]" rev] - ["[0]" frac]]] - [meta - ["[0]" symbol]]]]) - -(def (un_paired pairs) - (All (_ a) (-> (List [a a]) (List a))) - (case pairs - {.#End} - {.#End} - - {.#Item [[x y] pairs']} - (list.partial x y (un_paired pairs')))) - -(type .public Parser - (//.Parser (List Code))) - -(def remaining_inputs - (-> (List Code) Text) - (|>> (list#each code.format) - (text.interposed " ") - (all text#composite text.new_line "Remaining input: "))) - -(def .public any - (Parser Code) - (function (_ tokens) - (case tokens - {.#End} - {try.#Failure "There are no tokens to parse!"} - - {.#Item [t tokens']} - {try.#Success [tokens' t]}))) - -(def .public next - (Parser Code) - (function (_ tokens) - (case tokens - {.#End} - {try.#Failure "There are no tokens to parse!"} - - {.#Item next _} - {try.#Success [tokens next]}))) - -(with_template [ ] - [(with_expansions [ (these {try.#Failure (all text#composite "Cannot parse " (remaining_inputs tokens))})] - (def .public - (Parser ) - (function (_ tokens) - (case tokens - {.#Item [[_ { x}] tokens']} - {try.#Success [tokens' x]} - - _ - ))) - - (def .public ( expected) - (-> (Parser Any)) - (function (_ tokens) - (case tokens - {.#Item [[_ { actual}] tokens']} - (if (at = expected actual) - {try.#Success [tokens' []]} - ) - - _ - ))))] - - [bit this_bit Bit .#Bit bit.equivalence "bit"] - [nat this_nat Nat .#Nat nat.equivalence "nat"] - [int this_int Int .#Int int.equivalence "int"] - [rev this_rev Rev .#Rev rev.equivalence "rev"] - [frac this_frac Frac .#Frac frac.equivalence "frac"] - [text this_text Text .#Text text.equivalence "text"] - [symbol this_symbol Symbol .#Symbol symbol.equivalence "symbol"] - ) - -(def .public (this code) - (-> Code (Parser Any)) - (function (_ tokens) - (case tokens - {.#Item [token tokens']} - (if (code#= code token) - {try.#Success [tokens' []]} - {try.#Failure (all text#composite "Expected a " (code.format code) " but instead got " (code.format token) - (remaining_inputs tokens))}) - - _ - {try.#Failure "There are no tokens to parse!"}))) - -(with_expansions [ (these {try.#Failure (all text#composite "Cannot parse local symbol" (remaining_inputs tokens))})] - (def .public local - (Parser Text) - (function (_ tokens) - (case tokens - {.#Item [[_ {.#Symbol ["" x]}] tokens']} - {try.#Success [tokens' x]} - - _ - ))) - - (def .public (this_local expected) - (-> Text (Parser Any)) - (function (_ tokens) - (case tokens - {.#Item [[_ {.#Symbol ["" actual]}] tokens']} - (if (at text.equivalence = expected actual) - {try.#Success [tokens' []]} - ) - - _ - )))) - -(with_expansions [ (these {try.#Failure (all text#composite "Cannot parse local symbol" (remaining_inputs tokens))})] - (def .public global - (Parser Symbol) - (function (_ tokens) - (case tokens - {.#Item [[_ {.#Symbol ["" short]}] tokens']} - - - {.#Item [[_ {.#Symbol it}] tokens']} - {try.#Success [tokens' it]} - - _ - ))) - - (def .public (this_global expected) - (-> Symbol (Parser Any)) - (function (_ tokens) - (case tokens - {.#Item [[_ {.#Symbol ["" actual]}] tokens']} - - - {.#Item [[_ {.#Symbol it}] tokens']} - (if (at symbol.equivalence = expected it) - {try.#Success [tokens' []]} - ) - - _ - )))) - -(with_template [ ] - [(def .public ( p) - (All (_ a) - (-> (Parser a) (Parser a))) - (function (_ tokens) - (case tokens - {.#Item [[_ { members}] tokens']} - (case (p members) - {try.#Success [{.#End} x]} {try.#Success [tokens' x]} - _ {try.#Failure (all text#composite "Parser was expected to fully consume " (remaining_inputs tokens))}) - - _ - {try.#Failure (all text#composite "Cannot parse " (remaining_inputs tokens))})))] - - [form .#Form "form"] - [variant .#Variant "variant"] - [tuple .#Tuple "tuple"] - ) - -(def .public end - (Parser Any) - (function (_ tokens) - (case tokens - {.#End} {try.#Success [tokens []]} - _ {try.#Failure (all text#composite "Expected list of tokens to be empty!" (remaining_inputs tokens))}))) - -(def .public end? - (Parser Bit) - (function (_ tokens) - {try.#Success [tokens (case tokens - {.#End} true - _ false)]})) - -(def .public (result parser inputs) - (All (_ a) (-> (Parser a) (List Code) (Try a))) - (case (parser inputs) - {try.#Failure error} - {try.#Failure error} - - {try.#Success [unconsumed value]} - (case unconsumed - {.#End} - {try.#Success value} - - _ - {try.#Failure (|> unconsumed - (list#each code.format) - (text.interposed ", ") - (text#composite "Unconsumed inputs: "))}))) - -(def .public (locally inputs parser) - (All (_ a) (-> (List Code) (Parser a) (Parser a))) - (function (_ real) - (do try.monad - [value (..result parser inputs)] - (in [real value])))) - -(def .public (not parser) - (All (_ a) (-> (Parser a) (Parser Code))) - (do //.monad - [sample ..next - result (//.or parser - ..any)] - (case result - {.#Left _} (//.failure (text#composite "Did NOT expect to parse code: " (code.format sample))) - {.#Right output} (in output)))) diff --git a/stdlib/source/parser/lux/meta/code.lux b/stdlib/source/parser/lux/meta/code.lux new file mode 100644 index 000000000..844c0e1c2 --- /dev/null +++ b/stdlib/source/parser/lux/meta/code.lux @@ -0,0 +1,225 @@ +(.require + [library + [lux (.except nat int rev local not symbol local global) + [abstract + ["[0]" monad (.only do)]] + [control + ["//" parser] + ["[0]" try (.only Try)]] + [data + ["[0]" bit] + ["[0]" text (.use "[1]#[0]" monoid)] + [collection + ["[0]" list (.use "[1]#[0]" functor)]]] + [math + [number + ["[0]" nat] + ["[0]" int] + ["[0]" rev] + ["[0]" frac]]] + [meta + ["[0]" symbol] + ["[0]" code (.use "[1]#[0]" equivalence)]]]]) + +(def (un_paired pairs) + (All (_ a) (-> (List [a a]) (List a))) + (case pairs + {.#End} + {.#End} + + {.#Item [[x y] pairs']} + (list.partial x y (un_paired pairs')))) + +(type .public Parser + (//.Parser (List Code))) + +(def remaining_inputs + (-> (List Code) Text) + (|>> (list#each code.format) + (text.interposed " ") + (all text#composite text.new_line "Remaining input: "))) + +(def .public any + (Parser Code) + (function (_ tokens) + (case tokens + {.#End} + {try.#Failure "There are no tokens to parse!"} + + {.#Item [t tokens']} + {try.#Success [tokens' t]}))) + +(def .public next + (Parser Code) + (function (_ tokens) + (case tokens + {.#End} + {try.#Failure "There are no tokens to parse!"} + + {.#Item next _} + {try.#Success [tokens next]}))) + +(with_template [ ] + [(with_expansions [ (these {try.#Failure (all text#composite "Cannot parse " (remaining_inputs tokens))})] + (def .public + (Parser ) + (function (_ tokens) + (case tokens + {.#Item [[_ { x}] tokens']} + {try.#Success [tokens' x]} + + _ + ))) + + (def .public ( expected) + (-> (Parser Any)) + (function (_ tokens) + (case tokens + {.#Item [[_ { actual}] tokens']} + (if (at = expected actual) + {try.#Success [tokens' []]} + ) + + _ + ))))] + + [bit this_bit Bit .#Bit bit.equivalence "bit"] + [nat this_nat Nat .#Nat nat.equivalence "nat"] + [int this_int Int .#Int int.equivalence "int"] + [rev this_rev Rev .#Rev rev.equivalence "rev"] + [frac this_frac Frac .#Frac frac.equivalence "frac"] + [text this_text Text .#Text text.equivalence "text"] + [symbol this_symbol Symbol .#Symbol symbol.equivalence "symbol"] + ) + +(def .public (this code) + (-> Code (Parser Any)) + (function (_ tokens) + (case tokens + {.#Item [token tokens']} + (if (code#= code token) + {try.#Success [tokens' []]} + {try.#Failure (all text#composite "Expected a " (code.format code) " but instead got " (code.format token) + (remaining_inputs tokens))}) + + _ + {try.#Failure "There are no tokens to parse!"}))) + +(with_expansions [ (these {try.#Failure (all text#composite "Cannot parse local symbol" (remaining_inputs tokens))})] + (def .public local + (Parser Text) + (function (_ tokens) + (case tokens + {.#Item [[_ {.#Symbol ["" x]}] tokens']} + {try.#Success [tokens' x]} + + _ + ))) + + (def .public (this_local expected) + (-> Text (Parser Any)) + (function (_ tokens) + (case tokens + {.#Item [[_ {.#Symbol ["" actual]}] tokens']} + (if (at text.equivalence = expected actual) + {try.#Success [tokens' []]} + ) + + _ + )))) + +(with_expansions [ (these {try.#Failure (all text#composite "Cannot parse local symbol" (remaining_inputs tokens))})] + (def .public global + (Parser Symbol) + (function (_ tokens) + (case tokens + {.#Item [[_ {.#Symbol ["" short]}] tokens']} + + + {.#Item [[_ {.#Symbol it}] tokens']} + {try.#Success [tokens' it]} + + _ + ))) + + (def .public (this_global expected) + (-> Symbol (Parser Any)) + (function (_ tokens) + (case tokens + {.#Item [[_ {.#Symbol ["" actual]}] tokens']} + + + {.#Item [[_ {.#Symbol it}] tokens']} + (if (at symbol.equivalence = expected it) + {try.#Success [tokens' []]} + ) + + _ + )))) + +(with_template [ ] + [(def .public ( p) + (All (_ a) + (-> (Parser a) (Parser a))) + (function (_ tokens) + (case tokens + {.#Item [[_ { members}] tokens']} + (case (p members) + {try.#Success [{.#End} x]} {try.#Success [tokens' x]} + _ {try.#Failure (all text#composite "Parser was expected to fully consume " (remaining_inputs tokens))}) + + _ + {try.#Failure (all text#composite "Cannot parse " (remaining_inputs tokens))})))] + + [form .#Form "form"] + [variant .#Variant "variant"] + [tuple .#Tuple "tuple"] + ) + +(def .public end + (Parser Any) + (function (_ tokens) + (case tokens + {.#End} {try.#Success [tokens []]} + _ {try.#Failure (all text#composite "Expected list of tokens to be empty!" (remaining_inputs tokens))}))) + +(def .public end? + (Parser Bit) + (function (_ tokens) + {try.#Success [tokens (case tokens + {.#End} true + _ false)]})) + +(def .public (result parser inputs) + (All (_ a) (-> (Parser a) (List Code) (Try a))) + (case (parser inputs) + {try.#Failure error} + {try.#Failure error} + + {try.#Success [unconsumed value]} + (case unconsumed + {.#End} + {try.#Success value} + + _ + {try.#Failure (|> unconsumed + (list#each code.format) + (text.interposed ", ") + (text#composite "Unconsumed inputs: "))}))) + +(def .public (locally inputs parser) + (All (_ a) (-> (List Code) (Parser a) (Parser a))) + (function (_ real) + (do try.monad + [value (..result parser inputs)] + (in [real value])))) + +(def .public (not parser) + (All (_ a) (-> (Parser a) (Parser Code))) + (do //.monad + [sample ..next + result (//.or parser + ..any)] + (case result + {.#Left _} (//.failure (text#composite "Did NOT expect to parse code: " (code.format sample))) + {.#Right output} (in output)))) diff --git a/stdlib/source/parser/lux/meta/type.lux b/stdlib/source/parser/lux/meta/type.lux index 7c7090474..46f89fec7 100644 --- a/stdlib/source/parser/lux/meta/type.lux +++ b/stdlib/source/parser/lux/meta/type.lux @@ -15,11 +15,12 @@ ["[0]" list (.use "[1]#[0]" functor)] ["[0]" dictionary (.only Dictionary)]]] [macro - ["^" pattern] - ["[0]" code]] + ["^" pattern]] [math [number - ["n" nat (.use "[1]#[0]" decimal)]]]]] + ["n" nat (.use "[1]#[0]" decimal)]]] + [meta + ["[0]" code]]]] [\\library ["[0]" / (.use "[1]#[0]" equivalence) ["[0]" check]]]) -- cgit v1.2.3