Debian Codegraph @

ext/sxml/adaptor.scm

raw
1 ;;;
2 ;;; Adapt SSAX to Gauche
3 ;;;
4
5 (define-module sxml.adaptor
6 (use scheme.list)
7 (use srfi.13)
8 (export ascii->char ucscode->char char-return char-tab char-newline
9 make-char-quotator assert |--| parser-error cout cerr nl
10 string-rindex pp substring?))
11 (select-module sxml.adaptor)
12
13 ;; Charcode related stuff, used in ssax.scm
14 (define ascii->char integer->char)
15 (define ucscode->char ucs->char)
16
17 (define-constant char-return #\return)
18 (define-constant char-tab #\tab)
19 (define-constant char-newline #\newline)
20
21 ;; make-char-quotator, used in sxml.to-html and sxml.tools
22 (define (make-char-quotator rules)
23 (lambda (s)
24 (with-string-io s
25 (lambda ()
26 (let loop ((ch (read-char)))
27 (cond ((eof-object? ch))
28 ((assv ch rules)
29 => (lambda (p) (display (cdr p)) (loop (read-char))))
30 (else (display ch) (loop (read-char)))))))
31 ))
32
33 ;; string-rindex is used in sxml-tools
34 (define string-rindex string-index-right)
35
36 ;; Derived from Oleg's myenv.scm -----------------------------
37
38 ; assert the truth of an expression (or of a sequence of expressions)
39 ;
40 ; syntax: assert ?expr ?expr ... [report: ?r-exp ?r-exp ...]
41 ;
42 ; If (and ?expr ?expr ...) evaluates to anything but #f, the result
43 ; is the value of that expression.
44 ; If (and ?expr ?expr ...) evaluates to #f, an error is reported.
45 ; The error message will show the failed expressions, as well
46 ; as the values of selected variables (or expressions, in general).
47 ; The user may explicitly specify the expressions whose
48 ; values are to be printed upon assertion failure -- as ?r-exp that
49 ; follow the identifier 'report:'
50 ; Typically, ?r-exp is either a variable or a string constant.
51 ; If the user specified no ?r-exp, the values of variables that are
52 ; referenced in ?expr will be printed upon the assertion failure.
53
54 (define-macro (assert expr . others)
55 ; given the list of expressions or vars,
56 ; make the list appropriate for cerr
57 (define (make-print-list prefix lst)
58 (cond
59 ((null? lst) '())
60 ((symbol? (car lst))
61 (cons #\newline
62 (cons (list 'quote (car lst))
63 (cons ": " (cons (car lst) (make-print-list #\newline (cdr lst)))))))
64 (else
65 (cons prefix (cons (car lst) (make-print-list "" (cdr lst)))))))
66
67 ; return the list of all unique "interesting"
68 ; variables in the expr. Variables that are certain
69 ; to be bound to procedures are not interesting.
70 (define (vars-of expr)
71 (let loop ((expr expr) (vars '()))
72 (cond
73 ((not (pair? expr)) vars) ; not an application -- ignore
74 ((memq (car expr)
75 '(quote let let* letrec let-values* lambda cond quasiquote
76 case define do assert))
77 vars) ; won't go there
78 (else ; ignore the head of the application
79 (let inner ((expr (cdr expr)) (vars vars))
80 (cond
81 ((null? expr) vars)
82 ((symbol? (car expr))
83 (inner (cdr expr)
84 (if (memq (car expr) vars) vars (cons (car expr) vars))))
85 (else
86 (inner (cdr expr) (loop (car expr) vars)))))))))
87
88 (cond
89 ((null? others) ; the most common case
90 `(or ,expr (begin (cerr "failed assertion: " ',expr nl "bindings"
91 ,@(make-print-list #\newline (vars-of expr)) nl)
92 (error "assertion failure"))))
93 ((eq? (car others) 'report:) ; another common case
94 `(or ,expr (begin (cerr "failed assertion: " ',expr
95 ,@(make-print-list #\newline (cdr others)) nl)
96 (error "assertion failure"))))
97 ((not (memq 'report: others))
98 `(or (and ,expr ,@others)
99 (begin (cerr "failed assertion: " '(,expr ,@others) nl "bindings"
100 ,@(make-print-list #\newline
101 (vars-of (cons 'and (cons expr others)))) nl)
102 (error "assertion failure"))))
103 (else ; report: occurs somewhere in 'others'
104 (let loop ((exprs (list expr)) (reported others))
105 (cond
106 ((eq? (car reported) 'report:)
107 `(or (and ,@(reverse exprs))
108 (begin (cerr "failed assertion: " ',(reverse exprs)
109 ,@(make-print-list #\newline (cdr reported)) nl)
110 (error "assertion failure"))))
111 (else (loop (cons (car reported) exprs) (cdr reported)))))))
112 )
113
114 ;; Macro used in sxpath.scm
115
116 ; Read-only decrement
117 (define-macro (|--| x) `(- ,x 1))
118
119 ;; Error handler called in SSAX
120 (define (parser-error port msg . args)
121 (let1 err (open-output-string)
122 (display (port-position-prefix port) err)
123 (display msg err)
124 (dolist [m args] ((if (string? m) display write) m err))
125 (newline err)
126 (error (get-output-string err))))
127
128 ;; error reporting
129
130 (define (cout . args)
131 (for-each (lambda (x)
132 (if (procedure? x) (x) (display x)))
133 args))
134
135 (define (cerr . args)
136 (for-each (lambda (x)
137 (if (procedure? x)
138 (x (current-error-port))
139 (display x (current-error-port))))
140 args))
141
142 (define-constant nl "\n")
143
144 ;; pretty-printer called in sxpathlib.scm (node-trace). it is used for
145 ;; debugging code, so for the time being we use 'write' instead.
146 ;; we might replace it once Gauche supports pretty-printer natively.
147 (define (pp arg) (write arg) (newline))
148
149 ;; small function used in txpath.scm
150 (define (substring? pat str) (string-contains str pat))