aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/text/lexer.lux
diff options
context:
space:
mode:
authorEduardo Julian2018-12-04 19:10:43 -0400
committerEduardo Julian2018-12-04 19:10:43 -0400
commit73120f5cc97224c2a5c961b4aa881738cc78e2af (patch)
treec45bc39a93856633cc9bb7da9d7d7245e652c231 /stdlib/source/lux/data/text/lexer.lux
parentccdac3e7ae689cfe9f8fe2211527ec37023a2a34 (diff)
Some refactoring.
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/data/text/lexer.lux20
1 files changed, 11 insertions, 9 deletions
diff --git a/stdlib/source/lux/data/text/lexer.lux b/stdlib/source/lux/data/text/lexer.lux
index 21aba8360..45a88bdf3 100644
--- a/stdlib/source/lux/data/text/lexer.lux
+++ b/stdlib/source/lux/data/text/lexer.lux
@@ -2,11 +2,13 @@
[lux (#- or and not)
[control
[monad (#+ do Monad)]
- ["p" parser]]
+ ["p" parser]
+ ["ex" exception (#+ exception:)]]
[data
["." product]
["." maybe]
["e" error]
+ [number ("nat/." Codec<Text,Nat>)]
[collection
["." list ("list/." Fold<List>)]]]
[macro
@@ -24,15 +26,16 @@
{#basis Offset
#distance Offset})
+(def: cannot-lex-error Text "Cannot lex from empty text.")
+
(def: (remaining offset tape)
(-> Offset Text Text)
(|> tape (//.split offset) maybe.assume product.right))
-(def: cannot-lex-error Text "Cannot lex from empty text.")
-
-(def: (unconsumed-input-error offset tape)
- (-> Offset Text Text)
- ($_ text/compose "Unconsumed input: " (remaining offset tape)))
+(exception: #export (unconsumed-input {offset Offset} {tape Text})
+ (ex.report ["Offset" (nat/encode offset)]
+ ["Input size" (nat/encode (//.size tape))]
+ ["Remaining input" (remaining offset tape)]))
(def: #export (run input lexer)
(All [a] (-> Text (Lexer a) (e.Error a)))
@@ -43,8 +46,7 @@
(#e.Success [[end-offset _] output])
(if (n/= end-offset (//.size input))
(#e.Success output)
- (#e.Error (unconsumed-input-error end-offset input)))
- ))
+ (ex.throw unconsumed-input [end-offset input]))))
(def: #export offset
(Lexer Offset)
@@ -130,7 +132,7 @@
(function (_ (^@ input [offset tape]))
(if (n/= offset (//.size tape))
(#e.Success [input []])
- (#e.Error (unconsumed-input-error offset tape)))))
+ (ex.throw unconsumed-input [offset tape]))))
(def: #export end?
{#.doc "Ask if the lexer's input is empty."}