4 (declaim (optimize (debug 2)))
9 (define-condition rng-error
(simple-error) ())
11 (defun rng-error (source fmt
&rest args
)
12 (let ((s (make-string-output-stream)))
13 (apply #'format s fmt args
)
15 (format s
"~& [ Error at line ~D, column ~D in ~S ]"
16 (klacks:current-line-number source
)
17 (klacks:current-column-number source
)
18 (klacks:current-system-id source
)))
21 :format-arguments
(list (get-output-stream-string s
)))))
26 (defvar *datatype-library
*)
27 (defvar *namespace-uri
*)
28 (defvar *entity-resolver
*)
29 (defvar *external-href-stack
*)
30 (defvar *include-uri-stack
*)
34 (defun invoke-with-klacks-handler (fn source
)
39 (cxml:xml-parse-error
(c)
40 (rng-error source
"Cannot parse schema: ~A" c
)))))
42 (defun parse-relax-ng (input &key entity-resolver
)
43 (klacks:with-open-source
(source (cxml:make-source input
))
44 (invoke-with-klacks-handler
46 (klacks:find-event source
:start-element
)
47 (let ((*datatype-library
* "")
49 (*entity-resolver
* entity-resolver
)
50 (*external-href-stack
* '())
51 (*include-uri-stack
* '()))
56 ;;;; pattern structures
60 (defstruct (%combination
(:include pattern
) (:conc-name
"PATTERN-"))
63 (defstruct (%named-pattern
(:include pattern
) (:conc-name
"PATTERN-"))
66 (defstruct (element (:include %named-pattern
) (:conc-name
"PATTERN-"))
69 (defstruct (attribute (:include %named-pattern
) (:conc-name
"PATTERN-"))
72 (defstruct (group (:include %combination
) (:conc-name
"PATTERN-")))
73 (defstruct (interleave (:include %combination
) (:conc-name
"PATTERN-")))
74 (defstruct (choice (:include %combination
) (:conc-name
"PATTERN-")))
75 (defstruct (optional (:include %combination
) (:conc-name
"PATTERN-")))
76 (defstruct (zero-or-more (:include %combination
) (:conc-name
"PATTERN-")))
77 (defstruct (one-or-more (:include %combination
) (:conc-name
"PATTERN-")))
78 (defstruct (list-pattern (:include %combination
) (:conc-name
"PATTERN-")))
79 (defstruct (mixed (:include %combination
) (:conc-name
"PATTERN-")))
81 (defstruct (ref (:include %named-pattern
) (:conc-name
"PATTERN-")))
83 (defstruct (parent-ref (:include %named-pattern
) (:conc-name
"PATTERN-")))
85 (defstruct (empty (:include pattern
) (:conc-name
"PATTERN-")))
86 (defstruct (text (:include pattern
) (:conc-name
"PATTERN-")))
88 (defstruct (%typed-pattern
(:include pattern
) (:conc-name
"PATTERN-"))
92 (defstruct (value (:include %typed-pattern
) (:conc-name
"PATTERN-"))
96 (defstruct (data (:include %typed-pattern
) (:conc-name
"PATTERN-"))
100 (defstruct (not-allowed (:include pattern
) (:conc-name
"PATTERN-")))
102 (defstruct (grammar (:include pattern
) (:conc-name
"PATTERN-"))
124 (defvar *rng-namespace
* "http://relaxng.org/ns/structure/1.0")
126 (defun skip-foreign* (source)
128 (case (klacks:peek-next source
)
129 (:start-element
(skip-foreign source
))
130 (:end-element
(return)))))
132 (defun skip-to-native (source)
134 (case (klacks:peek source
)
136 (when (equal (klacks:current-uri source
) *rng-namespace
*)
138 (klacks:serialize-element source nil
))
139 (:end-element
(return)))
140 (klacks:consume source
)))
142 (defun consume-and-skip-to-native (source)
143 (klacks:consume source
)
144 (skip-to-native source
))
146 (defun skip-foreign (source)
147 (when (equal (klacks:current-uri source
) *rng-namespace
*)
149 "invalid schema: ~A not allowed here"
150 (klacks:current-lname source
)))
151 (klacks:serialize-element source nil
))
153 (defun attribute (lname attrs
)
154 (let ((a (sax:find-attribute-ns
"" lname attrs
)))
156 (sax:attribute-value a
)
166 (defun ntc (lname source-or-attrs
)
167 ;; used for (n)ame, (t)ype, and (c)ombine, this also strings whitespace
169 (if (listp source-or-attrs
)
171 (klacks:list-attributes source-or-attrs
)))
172 (a (sax:find-attribute-ns
"" lname attrs
)))
174 (string-trim *whitespace
* (sax:attribute-value a
))
177 (defmacro with-library-and-ns
(attrs &body body
)
178 `(invoke-with-library-and-ns (lambda () ,@body
) ,attrs
))
180 (defun invoke-with-library-and-ns (fn attrs
)
181 (let* ((dl (attribute "datatypeLibrary" attrs
))
182 (ns (attribute "ns" attrs
))
183 (*datatype-library
* (if dl
(escape-uri dl
) *datatype-library
*))
184 (*namespace-uri
* (or ns
*namespace-uri
*)))
187 (defun p/pattern
(source)
188 (let* ((lname (klacks:current-lname source
))
189 (attrs (klacks:list-attributes source
)))
190 (with-library-and-ns attrs
191 (case (find-symbol lname
:keyword
)
192 (:|element|
(p/element source
(ntc "name" attrs
)))
193 (:|attribute|
(p/attribute source
(ntc "name" attrs
)))
194 (:|group|
(p/combination
#'make-group source
))
195 (:|interleave|
(p/combination
#'make-interleave source
))
196 (:|choice|
(p/combination
#'make-choice source
))
197 (:|optional|
(p/combination
#'make-optional source
))
198 (:|zeroOrMore|
(p/combination
#'make-zero-or-more source
))
199 (:|oneOrMore|
(p/combination
#'make-one-or-more source
))
200 (:|list|
(p/combination
#'make-list-pattern source
))
201 (:|mixed|
(p/combination
#'make-mixed source
))
202 (:|ref|
(p/ref source
))
203 (:|parentRef|
(p/parent-ref source
))
204 (:|empty|
(p/empty source
))
205 (:|text|
(p/text source
))
206 (:|value|
(p/value source
))
207 (:|data|
(p/data source
))
208 (:|notAllowed|
(p/not-allowed source
))
209 (:|externalRef|
(p/external-ref source
))
210 (:|grammar|
(p/grammar source
))
211 (t (skip-foreign source
))))))
213 (defun p/pattern
+ (source)
214 (let ((children nil
))
216 (case (klacks:peek source
)
218 (let ((p (p/pattern source
))) (when p
(push p children
))))
222 (klacks:consume source
))))
224 (rng-error source
"empty element"))
225 (nreverse children
)))
227 (defun p/pattern?
(source)
230 (skip-to-native source
)
231 (case (klacks:peek source
)
234 (rng-error source
"at most one pattern expected here"))
235 (setf result
(p/pattern source
)))
239 (klacks:consume source
))))
242 (defun p/element
(source name
)
243 (klacks:expecting-element
(source "element")
244 (let ((result (make-element)))
245 (consume-and-skip-to-native source
)
247 (setf (pattern-name result
)
248 (list :name name
:uri
*namespace-uri
*))
249 (setf (pattern-name result
) (p/name-class source
)))
250 (skip-to-native source
)
251 (setf (pattern-children result
) (p/pattern
+ source
))
254 (defun p/attribute
(source name
)
255 (klacks:expecting-element
(source "attribute")
256 (let ((result (make-attribute)))
257 (consume-and-skip-to-native source
)
259 (setf (pattern-name result
)
260 (list :name name
:uri
""))
261 (setf (pattern-name result
) (p/name-class source
)))
262 (skip-to-native source
)
263 (setf (pattern-child result
) (p/pattern? source
))
266 (defun p/combination
(constructor source
)
267 (klacks:expecting-element
(source)
268 (consume-and-skip-to-native source
)
269 (let ((possibilities (p/pattern
+ source
)))
270 (funcall constructor
:possibilities possibilities
))))
272 (defun p/ref
(source)
273 (klacks:expecting-element
(source "ref")
275 (make-ref :name
(ntc "name" source
))
276 (skip-foreign* source
))))
278 (defun p/parent-ref
(source)
279 (klacks:expecting-element
(source "parentRef")
281 (make-parent-ref :name
(ntc "name" source
))
282 (skip-foreign* source
))))
284 (defun p/empty
(source)
285 (klacks:expecting-element
(source "empty")
286 (skip-foreign* source
)
289 (defun p/text
(source)
290 (klacks:expecting-element
(source "text")
291 (skip-foreign* source
)
294 (defun consume-and-parse-characters (source)
298 (multiple-value-bind (key data
) (klacks:peek-next source
)
301 (setf tmp
(concatenate 'string tmp data
)))
302 (:end-element
(return)))))
305 (defun p/value
(source)
306 (klacks:expecting-element
(source "value")
307 (let* ((type (ntc "type" source
))
308 (string (consume-and-parse-characters source
))
310 (dl *datatype-library
*))
314 (make-value :string string
:type type
:ns ns
:datatype-library dl
))))
316 (defun p/data
(source)
317 (klacks:expecting-element
(source "data")
318 (let* ((type (ntc "type" source
))
319 (result (make-data :type type
320 :datatype-library
*datatype-library
*
324 (multiple-value-bind (key uri lname
)
325 (klacks:peek-next source
)
329 (case (find-symbol lname
:keyword
)
330 (:|param|
(push (p/param source
) params
))
332 (setf (pattern-except result
) (p/except-pattern source
))
333 (skip-to-native source
)
335 (t (skip-foreign source
))))
338 (setf (pattern-params result
) (nreverse params
))
341 (defun p/param
(source)
342 (klacks:expecting-element
(source "param")
343 (let ((name (ntc "name" source
))
344 (string (consume-and-parse-characters source
)))
345 (make-param :name name
:string string
))))
347 (defun p/except-pattern
(source)
348 (klacks:expecting-element
(source "except")
349 (with-library-and-ns (klacks:list-attributes source
)
350 (klacks:consume source
)
351 (p/pattern
+ source
))))
353 (defun p/not-allowed
(source)
354 (klacks:expecting-element
(source "notAllowed")
355 (consume-and-skip-to-native source
)
358 (defun safe-parse-uri (source str
&optional base
)
359 (when (zerop (length str
))
360 (rng-error source
"missing URI"))
363 (puri:merge-uris str base
)
364 (puri:parse-uri str
))
365 (puri:uri-parse-error
()
366 (rng-error source
"invalid URI: ~A" str
))))
368 (defun p/external-ref
(source)
369 (klacks:expecting-element
(source "externalRef")
371 (escape-uri (attribute "href" (klacks:list-attributes source
))))
372 (base (klacks:current-xml-base source
))
373 (uri (safe-parse-uri source href base
)))
374 (when (find uri
*include-uri-stack
* :test
#'puri
:uri
=)
375 (rng-error source
"looping include"))
377 (let* ((*include-uri-stack
* (cons uri
*include-uri-stack
*))
379 (cxml::xstream-open-extid
* *entity-resolver
* nil uri
)))
380 (klacks:with-open-source
(source (cxml:make-source xstream
))
381 (invoke-with-klacks-handler
383 (klacks:find-event source
:start-element
)
384 (let ((*datatype-library
* ""))
387 (skip-foreign* source
)))))
389 (defun p/grammar
(source)
390 (klacks:expecting-element
(source "grammar")
391 (consume-and-skip-to-native source
)
392 (make-grammar :content
(p/grammar-content
* source
))))
394 (defun p/grammar-content
* (source &key disallow-include
)
398 (multiple-value-bind (key uri lname
) (klacks:peek source
)
402 (with-library-and-ns (klacks:list-attributes source
)
403 (case (find-symbol lname
:keyword
)
404 (:|start|
(list (p/start source
)))
405 (:|define|
(list (p/define source
)))
406 (:|div|
(p/div source
))
408 (when disallow-include
409 (rng-error source
"nested include not permitted"))
412 (skip-foreign source
)
416 (klacks:consume source
))))
418 (defun p/start
(source)
419 (klacks:expecting-element
(source "start")
420 (let ((combine (ntc "combine" source
))
423 (consume-and-skip-to-native source
)
424 (p/pattern source
))))
425 (skip-foreign* source
)
426 (make-start :combine
(find-symbol (string-upcase combine
) :keyword
)
429 (defun p/define
(source)
430 (klacks:expecting-element
(source "define")
431 (let ((name (ntc "name" source
))
432 (combine (ntc "combine" source
))
434 (consume-and-skip-to-native source
)
435 (p/pattern
+ source
))))
436 (make-define :name name
437 :combine
(find-symbol (string-upcase combine
) :keyword
)
438 :children children
))))
440 (defun p/div
(source)
441 (klacks:expecting-element
(source "div")
442 (consume-and-skip-to-native source
)
443 (p/grammar-content
* source
)))
445 (defun p/include
(source)
446 (klacks:expecting-element
(source "include")
448 (escape-uri (attribute "href" (klacks:list-attributes source
))))
449 (base (klacks:current-xml-base source
))
450 (uri (safe-parse-uri source href base
))
453 (consume-and-skip-to-native source
)
454 (p/grammar-content
* source
:disallow-include t
))))
455 (when (find uri
*include-uri-stack
* :test
#'puri
:uri
=)
456 (rng-error source
"looping include"))
457 (let* ((*include-uri-stack
* (cons uri
*include-uri-stack
*))
458 (xstream (cxml::xstream-open-extid
* *entity-resolver
* nil uri
))
460 (klacks:with-open-source
(source (cxml:make-source xstream
))
461 (invoke-with-klacks-handler
463 (klacks:find-event source
:start-element
)
464 (let ((*datatype-library
* ""))
467 (grammar-content (pattern-content grammar
)))
469 (simplify-include source grammar-content include-content
)
472 (defun simplify-include/map
(fn l
)
473 (remove nil
(mapcar fn l
)))
475 (defun simplify-include/start
(source grammar-content include-content
)
476 (let ((startp (some (lambda (x) (typep x
'start
)) include-content
)))
480 (remove-if (lambda (x)
481 (when (typep x
'start
)
486 (rng-error source
"expected start in grammar"))))
489 (defun simplify-include/define
(source grammar-content include-content
)
491 (dolist (x include-content
)
492 (when (typep x
'define
)
493 (push (cons x nil
) defines
)))
495 (remove-if (lambda (x)
496 (when (typep x
'define
)
497 (let ((cons (find (define-name x
)
500 (define-name (car y
)))
506 (loop for
(define . okp
) in defines do
508 (rng-error source
"expected matching ~A in grammar" define
))))))
510 (defun simplify-include (source grammar-content include-content
)
511 (simplify-include/define
513 (simplify-include/start source grammar-content include-content
)
516 (defun p/name-class
(source)
517 (klacks:expecting-element
(source)
518 (with-library-and-ns (klacks:list-attributes source
)
519 (case (find-symbol (klacks:current-lname source
) :keyword
)
521 (let ((qname (string-trim *whitespace
*
522 (consume-and-parse-characters source
))))
523 (multiple-value-bind (uri lname
)
524 (klacks:decode-qname qname source
)
525 (list :name lname
:uri
(or uri
*namespace-uri
*)))))
527 (klacks:consume source
)
529 (cons :any
(p/except-name-class? source
))
530 (skip-to-native source
)))
532 (let ((uri *namespace-uri
*))
533 (klacks:consume source
)
535 (list :nsname
(p/except-name-class? source
) :uri uri
)
536 (skip-to-native source
))))
538 (klacks:consume source
)
539 (cons :choice
(p/name-class
* source
)))
541 (rng-error source
"invalid child in except"))))))
543 (defun p/name-class
* (source)
546 (skip-to-native source
)
547 (case (klacks:peek source
)
548 (:start-element
(push (p/name-class source
) results
))
549 (:end-element
(return)))
550 (klacks:consume source
))
553 (defun p/except-name-class?
(source)
554 (skip-to-native source
)
555 (multiple-value-bind (key uri lname
)
558 (if (and (eq key
:start-element
)
559 (string= (find-symbol lname
:keyword
) "except"))
560 (p/except-name-class source
)
563 (defun p/except-name-class
(source)
564 (klacks:expecting-element
(source "except")
565 (with-library-and-ns (klacks:list-attributes source
)
566 (klacks:consume source
)
567 (cons :except
(p/name-class
* source
)))))
569 (defun escape-uri (string)
570 (with-output-to-string (out)
571 (loop for c across
(cxml::rod-to-utf8-string string
) do
572 (let ((code (char-code c
)))
573 ;; http://www.w3.org/TR/xlink/#link-locators
574 (if (or (>= code
127) (<= code
32) (find c
"<>\"{}|\\^`"))
575 (format out
"%~2,'0X" code
)
576 (write-char c out
))))))
582 ;;; Foreign attributes and elements are removed implicitly while parsing.
585 ;;; All character data is discarded while parsing (which can only be
586 ;;; whitespace after validation).
588 ;;; Whitespace in name, type, and combine attributes is stripped while
589 ;;; parsing. Ditto for <name/>.
591 ;;; 4.3. datatypeLibrary attribute
592 ;;; Escaping is done by p/pattern.
593 ;;; Attribute value defaulting is done using *datatype-library*; only
594 ;;; p/data and p/value record the computed value.
596 ;;; 4.4. type attribute of value element
599 ;;; 4.5. href attribute
600 ;;; Escaping is done by p/include and p/external-ref.
602 ;;; FIXME: Mime-type handling should be the job of the entity resolver,
603 ;;; but that requires xstream hacking.
605 ;;; 4.6. externalRef element
606 ;;; Done by p/external-ref.
608 ;;; 4.7. include element
609 ;;; Done by p/include.
611 ;;; 4.8. name attribute of element and attribute elements
612 ;;; `name' is stored as a slot, not a child. Done by p/element and
615 ;;; 4.9. ns attribute
616 ;;; done by p/name-class, p/value, p/element, p/attribute
619 ;;; done by p/name-class
621 ;;; 4.11. div element
622 ;;; Legen wir gar nicht erst an.
626 (defun run-tests (&optional
(p "/home/david/src/lisp/cxml-rng/spec-split/*"))
627 (dribble "/home/david/src/lisp/cxml-rng/TEST" :if-exists
:rename-and-delete
)
630 (dolist (d (directory p
))
631 (let ((name (car (last (pathname-directory d
)))))
632 (when (parse-integer name
:junk-allowed t
)
636 (format t
"Passed ~D/~D tests.~%" pass total
))
639 (defun run-test (n &optional
(p "/home/david/src/lisp/cxml-rng/spec-split/"))
640 (test1 (merge-pathnames (format nil
"~3,'0D/" n
) p
)))
642 (defun parse-test (n &optional
(p "/home/david/src/lisp/cxml-rng/spec-split/"))
644 (d (merge-pathnames (format nil
"~3,'0D/" n
) p
))
645 (i (merge-pathnames "i.rng" d
))
646 (c (merge-pathnames "c.rng" d
))
647 (rng (if (probe-file c
) c i
)))
648 (format t
"~A: " (car (last (pathname-directory d
))))
650 (parse-relax-ng rng
)))
653 (let* ((i (merge-pathnames "i.rng" d
))
654 (c (merge-pathnames "c.rng" d
)))
655 (format t
"~A: " (car (last (pathname-directory d
))))
663 (format t
" FAIL: ~A~%" c
)
668 (format t
" FAIL: didn't detect invalid schema~%")
671 (format t
" PASS: ~S~%" (type-of c
))
674 (format t
" FAIL: incorrect condition type: ~A~%" c
)