From 0776142d3d789ac371792bfe67f905d9cb7d6294 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 17 Jan 2017 19:44:16 -0400 Subject: - Added some extra text lexers. --- stdlib/source/lux/lexer.lux | 63 ++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 24 deletions(-) (limited to 'stdlib/source') diff --git a/stdlib/source/lux/lexer.lux b/stdlib/source/lux/lexer.lux index bc5bea5f0..787cbb0c5 100644 --- a/stdlib/source/lux/lexer.lux +++ b/stdlib/source/lux/lexer.lux @@ -9,15 +9,13 @@ applicative monad codec) - (data [text "Text/" Eq] - text/format + (data [text "Text/" Eq Monoid] [number "Int/" Codec] [product] [char "Char/" Ord] maybe ["E" error #- fail] - (struct [list "" Functor])) - host)) + (struct [list "" Functor])))) ## [Types] (type: #export (Lexer a) @@ -247,7 +245,7 @@ (case (text;split (text;size test) input) #;None (#E;Error "") (#;Some [_ input']) (#E;Success [input' test])) - (#E;Error (format "Invalid match: " test " @ " (:: text;Codec encode input)))) + (#E;Error ($_ Text/append "Invalid match: " test " @ " (:: text;Codec encode input)))) )) (def: #export (sep-by sep lexer) @@ -271,7 +269,7 @@ (lambda [input] (case input "" (#E;Success [input []]) - _ (#E;Error (format "The text input has not been fully consumed @ " (:: text;Codec encode input))) + _ (#E;Error ($_ Text/append "The text input has not been fully consumed @ " (:: text;Codec encode input))) ))) (def: #export peek @@ -294,8 +292,7 @@ [(#;Some char') (#;Some [_ input'])] (if (Char/= test char') (#E;Success [input' test]) - (#E;Error (format "Expected " (:: char;Codec encode test) " @ " (:: text;Codec encode input) - " " (Int/encode (c2l test))" " (Int/encode (c2l [char']))))) + (#E;Error ($_ Text/append "Expected " (:: char;Codec encode test) " @ " (:: text;Codec encode input)))) _ (#E;Error "Can't parse character from empty text.")) @@ -313,14 +310,14 @@ (do Monad [input get-input char any - _ (assert (format "Character is not within range: " (:: char;Codec encode bottom) "-" (:: char;Codec encode top) " @ " (:: text;Codec encode input)) + _ (assert ($_ Text/append "Character is not within range: " (:: char;Codec encode bottom) "-" (:: char;Codec encode top) " @ " (:: text;Codec encode input)) (and (Char/>= bottom char) (Char/<= top char)))] (wrap char))) (do-template [ ] [(def: #export - {#;doc (#;TextM (format "Only lex " " characters."))} + {#;doc (#;TextM ($_ Text/append "Only lex " " characters."))} (Lexer Char) (char-range ))] @@ -361,7 +358,7 @@ _ (#E;Error "")) - (#E;Error (format "Character (" init ") is not one of: " options " @ " (:: text;Codec encode input)))) + (#E;Error ($_ Text/append "Character (" init ") is not one of: " options " @ " (:: text;Codec encode input)))) _ (#E;Error "Can't parse character from empty text.")))) @@ -379,7 +376,7 @@ _ (#E;Error "")) - (#E;Error (format "Character (" init ") is one of: " options " @ " (:: text;Codec encode input)))) + (#E;Error ($_ Text/append "Character (" init ") is one of: " options " @ " (:: text;Codec encode input)))) _ (#E;Error "Can't parse character from empty text.")))) @@ -396,7 +393,7 @@ (#;Some [input' output]) (if (p output) (#E;Success [input' output]) - (#E;Error (format "Character does not satisfy predicate: " (:: text;Codec encode input)))) + (#E;Error ($_ Text/append "Character does not satisfy predicate: " (:: text;Codec encode input)))) _ (#E;Error "Can't parse character from empty text.")))) @@ -406,18 +403,36 @@ (Lexer Char) (satisfies char;space?)) -(def: #export (some' p) - {#;doc "Lex some characters as a single continuous text."} - (-> (Lexer Char) (Lexer Text)) - (do Monad - [cs (some p)] - (wrap (text;concat (map char;as-text cs))))) +(do-template [ ] + [(def: #export ( p) + {#;doc } + (-> (Lexer Char) (Lexer Text)) + (do Monad + [cs ( p)] + (wrap (text;concat (map char;as-text cs)))))] + + [some' some "Lex some characters as a single continuous text."] + [many' many "Lex many characters as a single continuous text."] + ) + +(do-template [ ] + [(def: #export ( n p) + {#;doc } + (-> Nat (Lexer Char) (Lexer Text)) + (do Monad + [cs ( n p)] + (wrap (text;concat (map char;as-text cs)))))] + + [exactly' exactly "Lex exactly N characters."] + [at-most' at-most "Lex at most N characters."] + [at-least' at-least "Lex at least N characters."] + ) -(def: #export (many' p) - {#;doc "Lex many characters as a single continuous text."} - (-> (Lexer Char) (Lexer Text)) +(def: #export (between' from to p) + {#;doc "Lex between N and M characters."} + (-> Nat Nat (Lexer Char) (Lexer Text)) (do Monad - [cs (many p)] + [cs (between from to p)] (wrap (text;concat (map char;as-text cs))))) (def: #export end? @@ -489,4 +504,4 @@ (#E;Success [unconsumed value]) (if (Text/= "" unconsumed) (#E;Success [real-input value]) - (#E;Error (format "Unconsumed input: " unconsumed)))))) + (#E;Error ($_ Text/append "Unconsumed input: " unconsumed)))))) -- cgit v1.2.3