(.module: [lux #* [abstract [codec (#+ Codec)]] [data ["." error (#+ Error)] ["." text]]] ["." / #_ ["#." nat] ["#." int] ["#." rev] ["#." frac]]) (macro: (encoding-doc tokens state) (case tokens (^ (list [cursor (#.Text encoding)] example-1 example-2)) (let [encoding ($_ "lux text concat" "Given syntax for a " encoding " number, generates a Nat, an Int, a Rev or a Frac.") commas "Allows for the presence of commas among the digits." description [cursor (#.Text ($_ "lux text concat" encoding " " commas))]] (#error.Success [state (list (` (doc (~ description) (~ example-1) (~ example-2))))])) _ (#error.Failure "Wrong syntax for 'encoding-doc'."))) (def: (comma-prefixed? number) (-> Text Bit) (case ("lux text index" 0 "," number) (#.Some 0) #1 _ #0)) (def: clean-commas (-> Text Text) (text.replace-all "," "")) (template [ ] [(macro: #export ( tokens state) {#.doc } (case tokens (#.Cons [meta (#.Text repr')] #.Nil) (if (comma-prefixed? repr') (#error.Failure ) (let [repr (clean-commas repr')] (case (:: decode repr) (#error.Success value) (#error.Success [state (list [meta (#.Nat value)])]) (^multi (#error.Failure _) [(:: decode repr) (#error.Success value)]) (#error.Success [state (list [meta (#.Int value)])]) (^multi (#error.Failure _) [(:: decode repr) (#error.Success value)]) (#error.Success [state (list [meta (#.Rev value)])]) (^multi (#error.Failure _) [(:: decode repr) (#error.Success value)]) (#error.Success [state (list [meta (#.Frac value)])]) _ (#error.Failure )))) _ (#error.Failure )))] [bin /nat.binary /int.binary /rev.binary /frac.binary "Invalid binary syntax." (encoding-doc "binary" (bin "+11001001") (bin "+11,00,10,01"))] [oct /nat.octal /int.octal /rev.octal /frac.octal "Invalid octal syntax." (encoding-doc "octal" (oct "+615243") (oct "+615,243"))] [hex /nat.hex /int.hex /rev.hex /frac.hex "Invalid hexadecimal syntax." (encoding-doc "hexadecimal" (hex "deadBEEF") (hex "dead,BEEF"))] )