summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2015-10-21 21:45:08 -0400
committerDavid Thompson <dthompson2@worcester.edu>2015-10-21 21:45:08 -0400
commit84b6e91a4e48dc1649ae9ef8b4eee39a6864555f (patch)
tree58a5f49229f4f0624be1f5bbf9688b043f1c7023
parent47903e9188cb7483a11506358def912a2cc1b2d2 (diff)
Gracefully handle parse failures.
* syntax-highlight.scm (highlight): Add remainder of code to result as a plain string if parsing fails.
-rw-r--r--syntax-highlight.scm10
1 files changed, 8 insertions, 2 deletions
diff --git a/syntax-highlight.scm b/syntax-highlight.scm
index 6a15dab..091eed2 100644
--- a/syntax-highlight.scm
+++ b/syntax-highlight.scm
@@ -36,7 +36,7 @@
STREAM may be an open port, string, or SRFI-41 character stream. If
STREAM is not specified, characters are read from the current input
port."
- (let-values (((result stream)
+ (let-values (((result remaining)
(highlighter (cond
((port? stream)
(port->stream stream))
@@ -46,7 +46,13 @@ port."
stream)
(else
(error "Cannot convert to stream: " stream))))))
- result))
+ ;; If there's a remainder, it means that parsing failed. We want
+ ;; to preserve *all* of the input text, even if it is invalid.
+ ;; So, we take the stuff that could be parsed and tack on the
+ ;; stuff that wasn't.
+ (if (stream-null? remaining)
+ result
+ (append result (list (stream->string remaining))))))
(define (highlights->sxml highlights)
"Convert HIGHLIGHTS, a list of syntax highlighting expressions, into