blob: 87b661bb8d73fc93f0e7338b11e310ada0028464 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
;;; guile-syntax-highlight --- General-purpose syntax highlighter
;;; Copyright © 2015 David Thompson <davet@gnu.org>
;;;
;;; Guile-syntax-highlight is free software; you can redistribute it
;;; and/or modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 3 of the License, or (at your option) any later version.
;;;
;;; Guile-syntax-highlight is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the implied
;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;;; See the GNU Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with guile-syntax-highlight. If not, see
;;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Syntax highlighting for Scheme.
;;
;;; Code:
(define-module (syntax-highlight xml)
#:use-module (ice-9 match)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-11)
#:use-module (srfi srfi-41)
#:use-module (syntax-highlight parsers)
#:export (xml-highlighter))
(define (flatten+compact highlights)
(define tagged?
(match-lambda
(((? symbol?) _) #t)
(_ #f)))
(let loop ((highlights highlights)
(result '()))
(match highlights
(() (reverse result))
(("" . tail)
(loop tail result))
(((or (? string? head) (? tagged? head)) . tail)
(loop tail (cons head result)))
((head . tail)
(loop tail (append (reverse (flatten+compact head)) result))))))
(define parse-comment
(tagged-parser 'comment (parse-delimited "<!--" #:until "-->")))
(define parse-xml-symbol
(parse-char-set
(char-set-union char-set:letter+digit
(char-set #\. #\- #\_ #\:))))
(define parse-element-name
(tagged-parser 'element parse-xml-symbol))
(define parse-whitespace-maybe
(parse-maybe parse-whitespace))
(define parse-attribute
(parse-each (tagged-parser 'attribute parse-xml-symbol)
parse-whitespace-maybe
(parse-string "=")
parse-whitespace-maybe
(tagged-parser 'string (parse-delimited "\""))))
(define parse-open-tag
(parse-each (tagged-parser 'open (parse-any (parse-string "<?")
(parse-string "<")))
parse-element-name
(parse-many
(parse-any (parse-each
parse-whitespace
parse-attribute)
parse-whitespace))
(tagged-parser 'close (parse-any (parse-string ">")
(parse-string "/>")
(parse-string "?>")))))
(define parse-close-tag
(parse-each (tagged-parser 'open (parse-string "</"))
parse-element-name
(tagged-parser 'close (parse-string ">"))))
(define char-set:not-whitespace
(char-set-complement char-set:whitespace))
(define parse-tag
(parse-each (parse-string "<")
(parse-char-set
(char-set-delete char-set:not-whitespace #\>))
(parse-string ">")))
(define parse-entity
(tagged-parser 'entity (parse-delimited "&" #:until ";")))
(define parse-text
(parse-char-set
(char-set-difference char-set:full (char-set #\<))))
(define xml-highlighter
(parse-map flatten+compact
(parse-many
(parse-any parse-comment
parse-close-tag
parse-open-tag
parse-entity
parse-text))))
|