From 84b6e91a4e48dc1649ae9ef8b4eee39a6864555f Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 21 Oct 2015 21:45:08 -0400 Subject: Gracefully handle parse failures. * syntax-highlight.scm (highlight): Add remainder of code to result as a plain string if parsing fails. --- syntax-highlight.scm | 10 ++++++++-- 1 file 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 -- cgit v1.2.3