1 ;;; Copyright (c) 2007 David Lichteblau. All rights reserved.
3 ;;; Redistribution and use in source and binary forms, with or without
4 ;;; modification, are permitted provided that the following conditions
7 ;;; * Redistributions of source code must retain the above copyright
8 ;;; notice, this list of conditions and the following disclaimer.
10 ;;; * Redistributions in binary form must reproduce the above
11 ;;; copyright notice, this list of conditions and the following
12 ;;; disclaimer in the documentation and/or other materials
13 ;;; provided with the distribution.
15 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
16 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 (in-package :cxml-rng
)
30 (declaim (optimize (debug 2)))
35 (define-condition rng-error
(simple-error)
36 ((line-number :initarg
:line-number
:accessor rng-error-line-number
)
37 (column-number :initarg
:column-number
:accessor rng-error-column-number
)
38 (system-id :initarg
:system-id
:accessor rng-error-system-id
))
40 "The class of all validation errors.
41 @see-slot{rng-error-line-number}
42 @see-slot{rng-error-column-number}
43 @see-slot{rng-error-system-id}"))
45 (setf (documentation 'rng-error-line-number
'function
)
46 "@arg[instance]{an instance of @class{rng-error}}
47 @return{an integer, or nil}
48 Return the line number reported by the parser when the Relax NG error
49 was detected, or NIL if not available.")
51 (setf (documentation 'rng-error-column-number
'function
)
52 "@arg[instance]{an instance of @class{rng-error}}
53 @return{an integer, or nil}
54 Return the column number reported by the parser when the Relax NG error
55 was detected, or NIL if not available.")
57 (setf (documentation 'rng-error-system-id
'function
)
58 "@arg[instance]{an instance of @class{rng-error}}
59 @return{a puri:uri, or nil}
60 Return the System ID of the document being parsed when the Relax NG
61 error was detected, or NIL if not available.")
63 (defun rng-error (source fmt
&rest args
)
65 (let ((s (make-string-output-stream)))
66 (apply #'format s fmt args
)
67 (multiple-value-bind (line-number column-number system-id
)
71 (values (klacks:current-line-number source
)
72 (klacks:current-column-number source
)
73 (klacks:current-system-id source
)))
75 (values (sax:line-number source
)
76 (sax:column-number source
)
77 (sax:system-id source
))))
78 (when (or line-number column-number system-id
)
79 (format s
"~& [ Error at line ~D, column ~D in ~S ]"
85 :format-arguments
(list (get-output-stream-string s
))
86 :line-number line-number
87 :column-number column-number
88 :system-id system-id
))))
93 (defvar *datatype-library
*)
94 (defvar *namespace-uri
*)
96 (defvar *entity-resolver
*)
97 (defvar *external-href-stack
*)
98 (defvar *include-uri-stack
*)
99 (defvar *include-body-p
* nil
)
105 (:constructor make-schema
(start definitions
)))
106 "An instance of this class represents a Relax NG grammar that has
107 been parsed and simplified.
108 @see-slot{schema-start}
109 @see-constructor{parse-schema}
111 @see{serialize-schema} "
112 (start (missing) :type pattern
)
113 (definitions (missing) :type list
)
114 (interned-start nil
:type
(or null pattern
))
115 (registratur nil
:type
(or null hash-table
)))
117 (setf (documentation 'schema-start
'function
)
118 "@arg[instance]{an instance of @class{schema}}
119 @return{the start pattern, an instance of @class{pattern}}
120 Reader function for the grammar's start pattern, from which all
121 of the grammar's patters are reachable.")
123 (defmethod print-object ((object schema
) stream
)
124 (print-unreadable-object (object stream
:type t
:identity t
)))
126 (defun invoke-with-klacks-handler (fn source
)
131 (cxml:xml-parse-error
(c)
132 (rng-error source
"Cannot parse schema: ~A" c
)))))
134 (defvar *validate-grammar
* t
)
135 (defparameter *relax-ng-grammar
* nil
)
137 (defun make-validating-source (input)
138 (let ((upstream (cxml:make-source input
)))
139 (if *validate-grammar
*
140 (klacks:make-tapping-source upstream
141 (make-validator *relax-ng-grammar
*))
144 (defun parse-schema (input &key entity-resolver
)
145 "@arg[input]{a string, pathname, stream, or xstream}
146 @arg[entity-resolver]{a function of two arguments, or NIL}
147 @return{a parsed @class{schema}}
148 @short{This function parses a Relax NG schema file in XML syntax}
149 and returns a parsed representation of that schema.
151 @code{input} can be any stream designator as understood by
152 @code{cxml:make-source}.
154 Note that namestrings are not valid arguments,
155 because they would be interpreted as XML source code. Use pathnames
158 @code{entity-resolver} can be passed as a function of two arguments.
159 It is invoked for every entity referenced by the
160 document with the entity's Public ID (a rod) and System ID (an
161 URI object) as arguments. The function may either return
162 nil, CXML will then try to resolve the entity as usual.
163 Alternatively it may return a Common Lisp stream specialized on
164 @code{(unsigned-byte 8)} which will be used instead.
167 @see{make-validator}"
168 (when *validate-grammar
*
169 (unless *relax-ng-grammar
*
170 (setf *relax-ng-grammar
*
171 (let* ((*validate-grammar
* nil
)
172 (d (slot-value (asdf:find-system
:cxml-rng
)
173 'asdf
::relative-pathname
)))
174 (parse-schema (merge-pathnames "rng.rng" d
))
176 (parse-compact (merge-pathnames "rng.rnc" d
))))))
177 (klacks:with-open-source
(source (make-validating-source input
))
178 (invoke-with-klacks-handler
180 (klacks:find-event source
:start-element
)
181 (let* ((*datatype-library
* "")
183 (*entity-resolver
* entity-resolver
)
184 (*external-href-stack
* '())
185 (*include-uri-stack
* '())
186 (*grammar
* (make-grammar nil
))
187 (start (p/pattern source
)))
189 (rng-error nil
"empty grammar"))
190 (setf (grammar-start *grammar
*)
191 (make-definition :name
:start
:child start
))
192 (check-pattern-definitions source
*grammar
*)
193 (check-recursion start
0)
194 (multiple-value-bind (new-start defns
)
195 (finalize-definitions start
)
196 (setf start
(fold-not-allowed new-start
))
198 (setf (defn-child defn
) (fold-not-allowed (defn-child defn
))))
199 (setf start
(fold-empty start
))
201 (setf (defn-child defn
) (fold-empty (defn-child defn
)))))
202 (multiple-value-bind (new-start defns
)
203 (finalize-definitions start
)
204 (check-start-restrictions new-start
)
206 (check-restrictions (defn-child defn
)))
207 (make-schema new-start defns
))))
211 ;;;; pattern structures
214 "@short{The superclass of all patterns.}
215 Instances of this class represent elements of the simplified syntax
218 Patterns are documented for introspective purposes and are not meant to
219 be modified by user code.
221 The start pattern of a schema is available through @fun{schema-start}.
224 (nullable :uninitialized
))
226 (defmethod print-object :around
((object pattern
) stream
)
228 (let ((*print-circle
* t
))
230 (print-unreadable-object (object stream
:type t
:identity t
))))
232 (defstruct (%parent
(:include pattern
) (:conc-name
"PATTERN-"))
235 (defstruct (%named-pattern
(:include %parent
) (:conc-name
"PATTERN-"))
238 (setf (documentation 'pattern-name
'function
)
239 "@arg[instance]{an instance of @class{pattern}}
240 @return{a @class{name-class}}
241 @short{Returns the @code{pattern}'s name class.}
243 This slot describes the name allowed for the current element or
249 (setf (documentation 'pattern-child
'function
)
250 "@arg[instance]{an instance of @class{pattern}}
251 @return{an instance of @class{pattern}}
252 @short{Returns the pattern's sub-pattern.}
254 (Elements in the full Relax NG syntax allow more than one child
255 pattern, but simplification normalizes the representation so that
256 any such element has exactly one child.)
264 (defstruct (element (:include %named-pattern
))
265 "@short{This pattern specifies that an element of a certain name class
268 Its child pattern describes the attributes and child nodes
270 @see-slot{pattern-name}
271 @see-slot{pattern-child}")
273 (defstruct (attribute (:include %named-pattern
))
274 "@short{This pattern specifies that an attribute of a certain name class
277 Its child pattern describes the type of the attribute's
279 @see-slot{pattern-name}
280 @see-slot{pattern-child}")
282 (defstruct (%combination
(:include pattern
) (:conc-name
"PATTERN-"))
285 (setf (documentation 'pattern-a
'function
)
286 "@arg[instance]{an instance of @class{pattern}}
287 @return{an instance of @class{pattern}}
288 @short{Returns the first of two sub-patterns the pattern instance has.}
290 (Elements in the full Relax NG syntax allow more than two child
291 patterns, but simplification normalizes the representation so that
292 any such element has exactly two children.)
299 (setf (documentation 'pattern-b
'function
)
300 "@arg[instance]{an instance of @class{pattern}}
301 @return{an instance of @class{pattern}}
302 @short{Returns the second of two sub-patterns the pattern instance has.}
304 (Elements in the full Relax NG syntax allow more than two child
305 patterns, but simplification normalizes the representation so that
306 any such element has exactly two children.)
314 (:include %combination
)
315 (:constructor make-group
(a b
)))
316 "@short{This pattern specifies that two subpatterns are
317 required at the current position in a specific order.}
320 @see-slot{pattern-b}")
321 (defstruct (interleave
322 (:include %combination
)
323 (:constructor make-interleave
(a b
)))
324 "@short{This pattern specifies that two possible subpatterns are
325 allowed to occur in any order at the current position.}
328 @see-slot{pattern-b}")
330 (:include %combination
)
331 (:constructor make-choice
(a b
)))
332 "@short{This pattern specifies that one of two possible subpatterns are
333 allowed at the current position, given as its children.}
336 @see-slot{pattern-b}")
338 (:include %combination
)
339 (:constructor make-after
(a b
))))
341 (defstruct (one-or-more
343 (:constructor make-one-or-more
(child)))
344 "@short{This pattern specifies that its subpattern is
345 allowed to occur at the current position one or more times.}
347 @see-slot{pattern-child}")
348 (defstruct (list-pattern
350 (:constructor make-list-pattern
(child)))
351 "@short{This pattern specifies that a subpatterns is allowed multiple
352 times a the current position, with whitespace as a separator.}
354 @see-slot{pattern-child}")
358 (:conc-name
"PATTERN-")
359 (:constructor make-ref
(target)))
360 "@short{This pattern references another part of the pattern graph.}
362 @code{ref} is the only pattern to introduce shared structure and
363 circularity into the pattern graph, by referring to elements defined
366 (@code{ref} pattern in the full Relax NG syntax can be used to refer
367 to any pattern definition in the grammar. Simplification normalizes
368 the schema so that ref patterns only refer to definitions which have
369 an @code{element} as their child.)
371 @see-slot{pattern-element}"
375 (defun pattern-element (ref)
376 "@arg[ref]{an instance of @class{ref}}
377 @return{an instance of @class{element}}
378 @short{Returns the ref pattern's target.}
380 @code{ref} is the only pattern to introduce shared structure and
381 circularity into the pattern graph, by referring to elements defined
384 (@code{ref} pattern in the full Relax NG syntax can be used to refer
385 to any pattern definition in the grammar. Simplification normalizes
386 the schema so that ref patterns only refer to definitions which have
387 an @code{element} as their child.)"
388 (defn-child (pattern-target ref
)))
390 (defstruct (%leaf
(:include pattern
)))
392 (defstruct (empty (:include %leaf
))
393 "@short{This pattern specifies that nothing more is expected at the current
396 (defstruct (text (:include %leaf
))
397 "@short{This pattern specifies that text is expected here.}")
399 (defstruct (%typed-pattern
(:include %leaf
) (:conc-name
"PATTERN-"))
402 (setf (documentation 'pattern-type
'function
)
403 "@arg[instance]{an instance of @class{pattern}}
404 @return{a @class{cxml-types:data-type}}
405 @short{Returns the data type expected at this position.}
407 This type has already been parsed into an object. Its name and
408 the URI of its library can be queried from that object.
412 @see{cxml-types:type-name}
413 @see{cxml-types:type-library}")
415 (defstruct (value (:include %typed-pattern
) (:conc-name
"PATTERN-"))
416 "@short{This pattern specifies that a specific value is expected as text
419 The value expected is @code{pattern-value}, parsed from
420 @code{pattern-string} using @code{pattern-type}.
422 @see-slot{pattern-type}
423 @see-slot{pattern-value}
424 @see-slot{pattern-string}"
429 (setf (documentation 'pattern-string
'function
)
430 "@arg[instance]{an instance of @class{value}}
432 @short{Returns the string expected at this position.}
434 This string is the lexical representation expected, not parsed into
435 a value object yet. The parsed object is available as
440 (setf (documentation 'pattern-value
'function
)
441 "@arg[instance]{an instance of @class{value}}
442 @return{an object as returned by @fun{cxml-types:parse}}
443 @short{Returns the value expected at this position.}
445 This object is the result of parsing @fun{pattern-string} using
446 @fun{pattern-type}.")
448 (defstruct (data (:include %typed-pattern
) (:conc-name
"PATTERN-"))
449 "@short{This pattern specifies that text of a specific data type is
452 The data type instance stored in the @code{pattern-type} slot takes into
453 account additional paramaters, which can be retrieved using
454 @code{pattern-params} in their original form.
456 @see-slot{pattern-type}
457 @see-slot{pattern-params}
458 @see-slot{pattern-except}"
462 (setf (documentation 'pattern-except
'function
)
463 "@arg[instance]{an instance of @class{data}}
464 @return{a @class{pattern}, or @code{nil}}
465 @short{Returns the @code{data} instance's @code{except} pattern.}
467 In addition to a data type, @code{data} can specify that certain
468 values are @em{not} permitted. They are described using a pattern.
470 If this slot is @code{nil}, no exception is defined.")
472 (setf (documentation 'pattern-params
'function
)
473 "@arg[instance]{an instance of @class{data}}
474 @return{a list of @fun{cxml-types:param}}
475 @short{The data type parameters for this data pattern.}
477 (With the XSD type library, these are known as restricting facets.)")
479 (defstruct (not-allowed (:include %leaf
))
480 "@short{This pattern specifies that the part of the schema reached at
481 this point is not valid.}")
486 (defstruct (grammar (:constructor make-grammar
(parent)))
489 (definitions (make-hash-table :test
'equal
)))
491 ;; Clark calls this structure "RefPattern"
492 (defstruct (definition (:conc-name
"DEFN-"))
503 (error "missing arg"))
505 (defstruct name-class
506 "@short{The abstract superclass of all name-related classes.}
508 Name classes represent sets of permissible names for an element or
511 Names are pairs of namespace URI and local-name.
516 (defstruct (any-name (:include name-class
)
517 (:constructor make-any-name
(except)))
518 "@short{This name class allows any name.}
520 Exceptions are given as @code{any-name-except}.
522 @see-slot{any-name-except}"
523 (except (missing) :type
(or null name-class
)))
525 (setf (documentation 'any-name-except
'function
)
526 "@arg[instance]{an instance of @class{any-name}}
527 @return{a @class{name-class} or @code{nil}}
529 Return the name class @em{not} allowed by this @code{any-name},
530 or @code{nil} if there is no such exception.")
532 (defstruct (name (:include name-class
)
533 (:constructor make-name
(uri lname
)))
534 "@short{This name class allows only a specific name.}
536 A specific namespace URI and local name are expected.
539 @see-slot{name-lname}"
540 (uri (missing) :type string
)
541 (lname (missing) :type string
))
543 (setf (documentation 'name-uri
'function
)
544 "@arg[instance]{an instance of @class{name}}
546 Return the expected namespace URI.")
548 (setf (documentation 'name-lname
'function
)
549 "@arg[instance]{an instance of @class{name}}
551 Return the expected local name.")
553 (defstruct (ns-name (:include name-class
)
554 (:constructor make-ns-name
(uri except
)))
555 "@short{This name class allows all names in a specific namespace}, with
558 A specific namespace URI is expected.
560 Exceptions are given as @code{ns-name-except}.
562 @see-slot{ns-name-uri}
563 @see-slot{ns-name-except}"
564 (uri (missing) :type string
)
565 (except (missing) :type
(or null name-class
)))
567 (setf (documentation 'ns-name-uri
'function
)
568 "@arg[instance]{an instance of @class{ns-name}}
570 Return the expected namespace URI.")
572 (setf (documentation 'ns-name-except
'function
)
573 "@arg[instance]{an instance of @class{ns-name}}
574 @return{a @class{name-class} or @code{nil}}
576 Return the name class @em{not} allowed by this @code{ns-name},
577 or @code{nil} if there is no such exception.")
579 (defstruct (name-class-choice (:include name-class
)
580 (:constructor make-name-class-choice
(a b
)))
581 "@short{This name class represents the union of two other name classes.}
583 @see-slot{name-class-choice-a}
584 @see-slot{name-class-choice-b}"
585 (a (missing) :type name-class
)
586 (b (missing) :type name-class
))
588 (setf (documentation 'name-class-choice-a
'function
)
589 "@arg[instance]{an instance of @class{name-class-choice}}
590 @return{a @class{name-class}}
591 Returns the 'first' of two name classes that are allowed.
592 @see{name-class-choice-b}")
594 (setf (documentation 'name-class-choice-b
'function
)
595 "@arg[instance]{an instance of @class{name-class-choice}}
596 @return{a @class{name-class}}
597 Returns the 'second' of two name classes that are allowed.
598 @see{name-class-choice-a}")
600 (defun simplify-nc-choice (values)
601 (zip #'make-name-class-choice values
))
606 (defvar *rng-namespace
* "http://relaxng.org/ns/structure/1.0")
608 (defun skip-foreign* (source)
610 (case (klacks:peek-next source
)
611 (:start-element
(skip-foreign source
))
612 (:end-element
(return)))))
614 (defun skip-to-native (source)
616 (case (klacks:peek source
)
618 (when (equal (klacks:current-uri source
) *rng-namespace
*)
620 (klacks:serialize-element source nil
))
621 (:end-element
(return)))
622 (klacks:consume source
)))
624 (defun consume-and-skip-to-native (source)
625 (klacks:consume source
)
626 (skip-to-native source
))
628 (defun skip-foreign (source)
629 (when (equal (klacks:current-uri source
) *rng-namespace
*)
631 "invalid schema: ~A not allowed here"
632 (klacks:current-lname source
)))
633 (klacks:serialize-element source nil
))
635 (defun attribute (lname attrs
)
637 (let ((a (sax:find-attribute-ns
"" lname attrs
)))
639 (sax:attribute-value a
)
642 (defparameter *whitespace
*
643 (format nil
"~C~C~C~C"
649 (defun ntc (lname source-or-attrs
)
650 ;; used for (n)ame, (t)ype, and (c)ombine, this also strings whitespace
652 (if (listp source-or-attrs
)
654 (klacks:list-attributes source-or-attrs
)))
655 (a (sax:find-attribute-ns
"" lname attrs
)))
657 (string-trim *whitespace
* (sax:attribute-value a
))
660 (defmacro with-library-and-ns
(attrs &body body
)
661 `(invoke-with-library-and-ns (lambda () ,@body
) ,attrs
))
663 (defun invoke-with-library-and-ns (fn attrs
)
664 (let* ((dl (attribute "datatypeLibrary" attrs
))
665 (ns (attribute "ns" attrs
))
666 (*datatype-library
* (if dl
(escape-uri dl
) *datatype-library
*))
667 (*namespace-uri
* (or ns
*namespace-uri
*))
669 ;; FIXME: Ganz boese gehackt -- gerade so, dass wir die Relax NG
670 ;; Test-Suite bestehen.
672 (not (zerop (length *datatype-library
*)))
673 ;; scheme pruefen, und es muss was folgen
674 (or (not (cl-ppcre:all-matches
675 "^[a-zA-Z][a-zA-Z0-9+.-]*:.+"
677 ;; keine kaputten %te, keine #
678 (cl-ppcre:all-matches
679 "(%$|%.$|%[^0-9A-Fa-f][^0-9A-Fa-f]|#)"
680 *datatype-library
*)))
681 (rng-error nil
"malformed datatypeLibrary: ~A" *datatype-library
*))
684 (defun p/pattern
(source)
685 (let* ((lname (klacks:current-lname source
))
686 (attrs (klacks:list-attributes source
)))
687 (with-library-and-ns attrs
688 (case (find-symbol lname
:keyword
)
689 (:|element|
(p/element source
(ntc "name" attrs
)))
690 (:|attribute|
(p/attribute source
(ntc "name" attrs
)))
691 (:|group|
(p/combination
#'groupify source
))
692 (:|interleave|
(p/combination
#'interleave-ify source
))
693 (:|choice|
(p/combination
#'choice-ify source
))
694 (:|optional|
(p/optional source
))
695 (:|zeroOrMore|
(p/zero-or-more source
))
696 (:|oneOrMore|
(p/one-or-more source
))
697 (:|list|
(p/list source
))
698 (:|mixed|
(p/mixed source
))
699 (:|ref|
(p/ref source
))
700 (:|parentRef|
(p/parent-ref source
))
701 (:|empty|
(p/empty source
))
702 (:|text|
(p/text source
))
703 (:|value|
(p/value source
))
704 (:|data|
(p/data source
))
705 (:|notAllowed|
(p/not-allowed source
))
706 (:|externalRef|
(p/external-ref source
))
707 (:|grammar|
(p/grammar source
))
708 (t (skip-foreign source
))))))
710 (defun p/pattern
+ (source)
711 (let ((children nil
))
713 (case (klacks:peek source
)
715 (let ((p (p/pattern source
))) (when p
(push p children
))))
719 (klacks:consume source
))))
721 (rng-error source
"empty element"))
722 (nreverse children
)))
724 (defun p/pattern?
(source)
727 (skip-to-native source
)
728 (case (klacks:peek source
)
731 (rng-error source
"at most one pattern expected here"))
732 (setf result
(p/pattern source
)))
736 (klacks:consume source
))))
739 (defun p/element
(source name
)
740 (klacks:expecting-element
(source "element")
741 (let ((elt (make-element)))
742 (consume-and-skip-to-native source
)
744 (setf (pattern-name elt
) (destructure-name source name
))
745 (setf (pattern-name elt
) (p/name-class source
)))
746 (skip-to-native source
)
747 (setf (pattern-child elt
) (groupify (p/pattern
+ source
)))
748 (make-ref (make-definition :name
(gensym "ANONYMOUS") :child elt
)))))
750 (defvar *attribute-namespace-p
* nil
)
752 (defun p/attribute
(source name
)
753 (klacks:expecting-element
(source "attribute")
754 (let ((result (make-attribute)))
755 (consume-and-skip-to-native source
)
757 (setf (pattern-name result
)
758 (let ((*namespace-uri
* (or *ns
* ""))
759 (*attribute-namespace-p
* t
))
760 (destructure-name source name
)))
761 (setf (pattern-name result
)
762 (let ((*attribute-namespace-p
* t
))
763 (p/name-class source
))))
764 (skip-to-native source
)
765 (setf (pattern-child result
)
766 (or (p/pattern? source
) (make-text)))
769 (defun p/combination
(zipper source
)
770 (klacks:expecting-element
(source)
771 (consume-and-skip-to-native source
)
772 (funcall zipper
(p/pattern
+ source
))))
774 (defun p/one-or-more
(source)
775 (klacks:expecting-element
(source "oneOrMore")
776 (consume-and-skip-to-native source
)
777 (let ((children (p/pattern
+ source
)))
778 (make-one-or-more (groupify children
)))))
780 (defun p/zero-or-more
(source)
781 (klacks:expecting-element
(source "zeroOrMore")
782 (consume-and-skip-to-native source
)
783 (let ((children (p/pattern
+ source
)))
784 (make-choice (make-one-or-more (groupify children
))
787 (defun p/optional
(source)
788 (klacks:expecting-element
(source "optional")
789 (consume-and-skip-to-native source
)
790 (let ((children (p/pattern
+ source
)))
791 (make-choice (groupify children
) (make-empty)))))
793 (defun p/list
(source)
794 (klacks:expecting-element
(source "list")
795 (consume-and-skip-to-native source
)
796 (let ((children (p/pattern
+ source
)))
797 (make-list-pattern (groupify children
)))))
799 (defun p/mixed
(source)
800 (klacks:expecting-element
(source "mixed")
801 (consume-and-skip-to-native source
)
802 (let ((children (p/pattern
+ source
)))
803 (make-interleave (groupify children
) (make-text)))))
805 (defun p/ref
(source)
806 (klacks:expecting-element
(source "ref")
808 (let* ((name (ntc "name" source
))
810 (or (find-definition name
)
811 (setf (find-definition name
)
812 (make-definition :name name
:child nil
)))))
813 (make-ref pdefinition
))
814 (skip-foreign* source
))))
816 (defun p/parent-ref
(source)
817 (klacks:expecting-element
(source "parentRef")
819 (let* ((name (ntc "name" source
))
820 (grammar (grammar-parent *grammar
*))
822 (or (find-definition name grammar
)
823 (setf (find-definition name grammar
)
824 (make-definition :name name
:child nil
)))))
825 (make-ref pdefinition
))
826 (skip-foreign* source
))))
828 (defun p/empty
(source)
829 (klacks:expecting-element
(source "empty")
830 (skip-foreign* source
)
833 (defun p/text
(source)
834 (klacks:expecting-element
(source "text")
835 (skip-foreign* source
)
838 (defun consume-and-parse-characters (source)
842 (multiple-value-bind (key data
) (klacks:peek-next source
)
845 (setf tmp
(concatenate 'string tmp data
)))
846 (:end-element
(return)))))
849 (defun p/value
(source)
850 (klacks:expecting-element
(source "value")
851 (let* ((type (ntc "type" source
))
852 (string (consume-and-parse-characters source
))
854 (dl *datatype-library
*))
859 (cxml-types:find-type
(and dl
(find-symbol dl
:keyword
))
862 (vc (cxml-types:make-klacks-validation-context source
)))
864 (rng-error source
"type not found: ~A/~A" type dl
))
865 (make-value :string string
866 :value
(cxml-types:parse data-type string vc
)
870 (defun p/data
(source)
871 (klacks:expecting-element
(source "data")
872 (let* ((type (ntc "type" source
))
876 (multiple-value-bind (key uri lname
)
877 (klacks:peek-next source
)
881 (case (find-symbol lname
:keyword
)
882 (:|param|
(push (p/param source
) params
))
884 (setf except
(p/except-pattern source
))
885 (skip-to-native source
)
887 (t (skip-foreign source
))))
890 (setf params
(nreverse params
))
891 (let* ((dl *datatype-library
*)
892 (data-type (cxml-types:find-type
893 (and dl
(find-symbol dl
:keyword
))
897 (rng-error source
"type not found: ~A/~A" type dl
))
898 (when (eq data-type
:error
)
899 (rng-error source
"params not valid for type: ~A/~A/~A"
906 (defun p/param
(source)
907 (klacks:expecting-element
(source "param")
908 (let ((name (ntc "name" source
))
909 (string (consume-and-parse-characters source
)))
910 (cxml-types:make-param name string
))))
912 (defun p/except-pattern
(source)
913 (klacks:expecting-element
(source "except")
914 (with-library-and-ns (klacks:list-attributes source
)
915 (klacks:consume source
)
916 (choice-ify (p/pattern
+ source
)))))
918 (defun p/not-allowed
(source)
919 (klacks:expecting-element
(source "notAllowed")
920 (consume-and-skip-to-native source
)
923 (defun safe-parse-uri (source str
&optional base
)
924 (when (zerop (length str
))
925 (rng-error source
"missing URI"))
926 (let* ((compactp (rnc-uri-p str
))
927 (str (if compactp
(follow-rnc-uri str
) str
))
931 (puri:merge-uris str base
)
932 (puri:parse-uri str
))
933 (puri:uri-parse-error
()
934 (rng-error source
"invalid URI: ~A" str
)))))
935 (when (and (eq (puri:uri-scheme uri
) :file
)
936 (puri:uri-fragment uri
))
937 (rng-error source
"Forbidden fragment in URI: ~A" str
))
938 (values uri compactp
)))
940 (defun named-string-xstream (str uri
)
941 (let ((xstream (cxml::string-
>xstream str
)))
942 (setf (cxml::xstream-name xstream
)
943 (cxml::make-stream-name
944 :entity-name
"main document"
949 (defun xstream-open-schema (uri compactp
)
951 (named-string-xstream
953 ;; fixme: Hier waere es schon, mit *entity-resolver* arbeiten
954 ;; zu koennen, aber der liefert binaere Streams.
955 (open (cxml::uri-to-pathname uri
)
956 :element-type
'character
959 (cxml::xstream-open-extid
* *entity-resolver
* nil uri
)))
961 (defun p/external-ref
(source)
962 (klacks:expecting-element
(source "externalRef")
964 (escape-uri (attribute "href" (klacks:list-attributes source
))))
965 (base (klacks:current-xml-base source
)))
966 (multiple-value-bind (uri compactp
)
967 (safe-parse-uri source href base
)
968 (when (find uri
*include-uri-stack
* :test
#'puri
:uri
=)
969 (rng-error source
"looping include"))
971 (let* ((*include-uri-stack
* (cons uri
*include-uri-stack
*))
972 (xstream (xstream-open-schema uri compactp
)))
973 (klacks:with-open-source
974 (source (make-validating-source xstream
))
975 (invoke-with-klacks-handler
977 (klacks:find-event source
:start-element
)
978 (let ((*datatype-library
* ""))
981 (skip-foreign* source
))))))
983 (defun p/grammar
(source &optional grammar
)
984 (klacks:expecting-element
(source "grammar")
985 (consume-and-skip-to-native source
)
986 (let ((*grammar
* (or grammar
(make-grammar *grammar
*)))
988 (process-grammar-content* source
)
989 (unless (or includep
(grammar-start *grammar
*))
990 (rng-error source
"no <start> in grammar"))
992 (check-pattern-definitions source
*grammar
*)
993 (defn-child (grammar-start *grammar
*))))))
995 (defvar *include-start
*)
996 (defvar *include-definitions
*)
998 (defun process-grammar-content* (source &key disallow-include
)
1000 (multiple-value-bind (key uri lname
) (klacks:peek source
)
1004 (klacks:consume source
))
1006 (with-library-and-ns (klacks:list-attributes source
)
1007 (case (find-symbol lname
:keyword
)
1009 (process-start source
))
1010 (:|define|
(process-define source
))
1011 (:|div|
(process-div source
))
1013 (when disallow-include
1014 (rng-error source
"nested include not permitted"))
1015 (process-include source
))
1017 (skip-foreign source
)))))
1021 (defun process-start (source)
1022 (klacks:expecting-element
(source "start")
1023 (let* ((combine0 (ntc "combine" source
))
1026 (find-symbol (string-upcase combine0
) :keyword
)))
1029 (consume-and-skip-to-native source
)
1030 (p/pattern source
)))
1031 (pdefinition (grammar-start *grammar
*)))
1032 (skip-foreign* source
)
1033 ;; fixme: shared code with process-define
1035 (setf pdefinition
(make-definition :name
:start
:child nil
))
1036 (setf (grammar-start *grammar
*) pdefinition
))
1037 (when *include-body-p
*
1038 (setf *include-start
* pdefinition
))
1040 ((defn-child pdefinition
)
1041 (ecase (defn-redefinition pdefinition
)
1042 (:not-being-redefined
1044 (defn-combine-method pdefinition
)
1046 (defn-combine-method pdefinition
))))
1047 (rng-error source
"conflicting combine values for <start>"))
1049 (when (defn-head-p pdefinition
)
1050 (rng-error source
"multiple definitions for <start>"))
1051 (setf (defn-head-p pdefinition
) t
))
1052 (unless (defn-combine-method pdefinition
)
1053 (setf (defn-combine-method pdefinition
) combine
))
1054 (setf (defn-child pdefinition
)
1055 (case (defn-combine-method pdefinition
)
1057 (make-choice (defn-child pdefinition
) child
))
1059 (make-interleave (defn-child pdefinition
) child
)))))
1060 (:being-redefined-and-no-original
1061 (setf (defn-redefinition pdefinition
)
1062 :being-redefined-and-original
))
1063 (:being-redefined-and-original
)))
1065 (setf (defn-child pdefinition
) child
)
1066 (setf (defn-combine-method pdefinition
) combine
)
1067 (setf (defn-head-p pdefinition
) (null combine
))
1068 (setf (defn-redefinition pdefinition
) :not-being-redefined
))))))
1070 (defun zip (constructor children
)
1073 (rng-error nil
"empty choice?"))
1074 ((null (cdr children
))
1077 (destructuring-bind (a b
&rest rest
)
1079 (zip constructor
(cons (funcall constructor a b
) rest
))))))
1081 (defun choice-ify (children) (zip #'make-choice children
))
1082 (defun groupify (children) (zip #'make-group children
))
1083 (defun interleave-ify (children) (zip #'make-interleave children
))
1085 (defun find-definition (name &optional
(grammar *grammar
*))
1086 (gethash name
(grammar-definitions grammar
)))
1088 (defun (setf find-definition
) (newval name
&optional
(grammar *grammar
*))
1089 (setf (gethash name
(grammar-definitions grammar
)) newval
))
1091 (defun process-define (source)
1092 (klacks:expecting-element
(source "define")
1093 (let* ((name (ntc "name" source
))
1094 (combine0 (ntc "combine" source
))
1095 (combine (when combine0
1096 (find-symbol (string-upcase combine0
) :keyword
)))
1099 (consume-and-skip-to-native source
)
1100 (p/pattern
+ source
))))
1101 (pdefinition (find-definition name
)))
1103 (setf pdefinition
(make-definition :name name
:child nil
))
1104 (setf (find-definition name
) pdefinition
))
1105 (when *include-body-p
*
1106 (push pdefinition
*include-definitions
*))
1108 ((defn-child pdefinition
)
1109 (case (defn-redefinition pdefinition
)
1110 (:not-being-redefined
1112 (defn-combine-method pdefinition
)
1114 (defn-combine-method pdefinition
))))
1115 (rng-error source
"conflicting combine values for ~A" name
))
1117 (when (defn-head-p pdefinition
)
1118 (rng-error source
"multiple definitions for ~A" name
))
1119 (setf (defn-head-p pdefinition
) t
))
1120 (unless (defn-combine-method pdefinition
)
1121 (setf (defn-combine-method pdefinition
) combine
))
1122 (setf (defn-child pdefinition
)
1123 (case (defn-combine-method pdefinition
)
1125 (make-choice (defn-child pdefinition
) child
))
1127 (make-interleave (defn-child pdefinition
) child
)))))
1128 (:being-redefined-and-no-original
1129 (setf (defn-redefinition pdefinition
)
1130 :being-redefined-and-original
))
1131 (:being-redefined-and-original
)))
1133 (setf (defn-child pdefinition
) child
)
1134 (setf (defn-combine-method pdefinition
) combine
)
1135 (setf (defn-head-p pdefinition
) (null combine
))
1136 (setf (defn-redefinition pdefinition
) :not-being-redefined
))))))
1138 (defun process-div (source)
1139 (klacks:expecting-element
(source "div")
1140 (consume-and-skip-to-native source
)
1141 (process-grammar-content* source
)))
1143 (defun reset-definition-for-include (defn)
1144 (setf (defn-combine-method defn
) nil
)
1145 (setf (defn-redefinition defn
) :being-redefined-and-no-original
)
1146 (setf (defn-head-p defn
) nil
))
1148 (defun restore-definition (defn original
)
1149 (setf (defn-combine-method defn
) (defn-combine-method original
))
1150 (setf (defn-redefinition defn
) (defn-redefinition original
))
1151 (setf (defn-head-p defn
) (defn-head-p original
)))
1153 (defun process-include (source)
1154 (klacks:expecting-element
(source "include")
1156 (escape-uri (attribute "href" (klacks:list-attributes source
))))
1157 (base (klacks:current-xml-base source
))
1158 (*include-start
* nil
)
1159 (*include-definitions
* '()))
1160 (multiple-value-bind (uri compactp
)
1161 (safe-parse-uri source href base
)
1162 (consume-and-skip-to-native source
)
1163 (let ((*include-body-p
* t
))
1164 (process-grammar-content* source
:disallow-include t
))
1166 (when *include-start
*
1168 (copy-structure *include-start
*)
1169 (reset-definition-for-include *include-start
*))))
1172 for defn in
*include-definitions
*
1175 (copy-structure defn
)
1176 (reset-definition-for-include defn
)))))
1177 (when (find uri
*include-uri-stack
* :test
#'puri
:uri
=)
1178 (rng-error source
"looping include"))
1179 (let* ((*include-uri-stack
* (cons uri
*include-uri-stack
*))
1180 (xstream (xstream-open-schema uri compactp
)))
1181 (klacks:with-open-source
(source (make-validating-source xstream
))
1182 (invoke-with-klacks-handler
1184 (klacks:find-event source
:start-element
)
1185 (let ((*datatype-library
* ""))
1186 (p/grammar source
*grammar
*)))
1189 (when (eq (defn-redefinition *include-start
*)
1190 :being-redefined-and-no-original
)
1191 (rng-error source
"start not found in redefinition of grammar"))
1192 (restore-definition *include-start
* tmp-start
))
1193 (dolist (copy tmp-defns
)
1194 (let ((defn (gethash (defn-name copy
)
1195 (grammar-definitions *grammar
*))))
1196 (when (eq (defn-redefinition defn
)
1197 :being-redefined-and-no-original
)
1198 (rng-error source
"redefinition not found in grammar"))
1199 (restore-definition defn copy
)))
1202 (defun check-pattern-definitions (source grammar
)
1203 (when (and (grammar-start grammar
)
1204 (eq (defn-redefinition (grammar-start grammar
))
1205 :being-redefined-and-no-original
))
1206 (rng-error source
"start not found in redefinition of grammar"))
1207 (loop for defn being each hash-value in
(grammar-definitions grammar
) do
1208 (when (eq (defn-redefinition defn
) :being-redefined-and-no-original
)
1209 (rng-error source
"redefinition not found in grammar"))
1210 (unless (defn-child defn
)
1211 (rng-error source
"unresolved reference to ~A" (defn-name defn
)))))
1213 (defvar *any-name-allowed-p
* t
)
1214 (defvar *ns-name-allowed-p
* t
)
1216 (defun destructure-name (source qname
)
1217 (multiple-value-bind (uri lname
)
1218 (klacks:decode-qname qname source
)
1219 (setf uri
(or uri
*namespace-uri
*))
1220 (when (and *attribute-namespace-p
*
1221 (or (and (equal lname
"xmlns") (equal uri
""))
1222 (equal uri
"http://www.w3.org/2000/xmlns")))
1223 (rng-error source
"namespace attribute not permitted"))
1224 (make-name uri lname
)))
1226 (defun p/name-class
(source)
1227 (klacks:expecting-element
(source)
1228 (with-library-and-ns (klacks:list-attributes source
)
1229 (case (find-symbol (klacks:current-lname source
) :keyword
)
1231 (let ((qname (string-trim *whitespace
*
1232 (consume-and-parse-characters source
))))
1233 (destructure-name source qname
)))
1235 (unless *any-name-allowed-p
*
1236 (rng-error source
"anyname not permitted in except"))
1237 (klacks:consume source
)
1239 (let ((*any-name-allowed-p
* nil
))
1240 (make-any-name (p/except-name-class? source
)))
1241 (skip-to-native source
)))
1243 (unless *ns-name-allowed-p
*
1244 (rng-error source
"nsname not permitted in except"))
1245 (let ((uri *namespace-uri
*)
1246 (*any-name-allowed-p
* nil
)
1247 (*ns-name-allowed-p
* nil
))
1248 (when (and *attribute-namespace-p
*
1249 (equal uri
"http://www.w3.org/2000/xmlns"))
1250 (rng-error source
"namespace attribute not permitted"))
1251 (klacks:consume source
)
1253 (make-ns-name uri
(p/except-name-class? source
))
1254 (skip-to-native source
))))
1256 (klacks:consume source
)
1257 (simplify-nc-choice (p/name-class
* source
)))
1259 (rng-error source
"invalid child in except"))))))
1261 (defun p/name-class
* (source)
1262 (let ((results nil
))
1264 (skip-to-native source
)
1265 (case (klacks:peek source
)
1267 (klacks:consume source
))
1269 (push (p/name-class source
) results
))
1272 (nreverse results
)))
1274 (defun p/except-name-class?
(source)
1275 (skip-to-native source
)
1276 (multiple-value-bind (key uri lname
)
1277 (klacks:peek source
)
1279 (if (and (eq key
:start-element
)
1280 (string= (find-symbol lname
:keyword
) "except"))
1281 (p/except-name-class source
)
1284 (defun p/except-name-class
(source)
1285 (klacks:expecting-element
(source "except")
1286 (with-library-and-ns (klacks:list-attributes source
)
1287 (klacks:consume source
)
1288 (let ((x (p/name-class
* source
)))
1290 (simplify-nc-choice x
)
1293 (defun escape-uri (string)
1294 (with-output-to-string (out)
1295 (loop for c across
(cxml::rod-to-utf8-string string
) do
1296 (let ((code (char-code c
)))
1297 ;; http://www.w3.org/TR/xlink/#link-locators
1298 (if (or (>= code
127) (<= code
32) (find c
"<>\"{}|\\^`"))
1299 (format out
"%~2,'0X" code
)
1300 (write-char c out
))))))
1305 (defvar *definitions-to-names
*)
1306 (defvar *seen-names
*)
1308 (defun serialization-name (defn)
1309 (or (gethash defn
*definitions-to-names
*)
1310 (setf (gethash defn
*definitions-to-names
*)
1311 (let ((name (if (gethash (defn-name defn
) *seen-names
*)
1314 (hash-table-count *seen-names
*))
1316 (setf (gethash name
*seen-names
*) defn
)
1319 (defun serialize-schema (schema sink
)
1320 "@arg[schema]{a Relax NG @class{schema}}
1321 @arg[sink]{a SAX handler}
1322 @return{the result of @code{sax:end-document}}
1323 @short{This function serializes a parsed Relax NG back into XML syntax.}
1325 Note that the schema represented in memory has gone through simplification
1326 as is textually different from the original XML document.
1329 (cxml:with-xml-output sink
1330 (let ((*definitions-to-names
* (make-hash-table))
1331 (*seen-names
* (make-hash-table :test
'equal
)))
1332 (cxml:with-element
"grammar"
1333 (cxml:with-element
"start"
1334 (serialize-pattern (schema-start schema
)))
1335 (loop for defn being each hash-key in
*definitions-to-names
* do
1336 (serialize-definition defn
))))))
1338 (defun serialize-pattern (pattern)
1341 (cxml:with-element
"element"
1342 (serialize-name (pattern-name pattern
))
1343 (serialize-pattern (pattern-child pattern
))))
1345 (cxml:with-element
"attribute"
1346 (serialize-name (pattern-name pattern
))
1347 (serialize-pattern (pattern-child pattern
))))
1352 (interleave "interleave")
1354 (serialize-pattern (pattern-a pattern
))
1355 (serialize-pattern (pattern-b pattern
))))
1357 (cxml:with-element
"oneOrMore"
1358 (serialize-pattern (pattern-child pattern
))))
1360 (cxml:with-element
"list"
1361 (serialize-pattern (pattern-child pattern
))))
1363 (cxml:with-element
"ref"
1364 (cxml:attribute
"name" (serialization-name (pattern-target pattern
)))))
1366 (cxml:with-element
"empty"))
1368 (cxml:with-element
"notAllowed"))
1370 (cxml:with-element
"text"))
1372 (cxml:with-element
"value"
1373 (let ((type (pattern-type pattern
)))
1374 (cxml:attribute
"datatype-library"
1375 (symbol-name (cxml-types:type-library type
)))
1376 (cxml:attribute
"type" (cxml-types:type-name type
)))
1377 (cxml:attribute
"ns" (pattern-ns pattern
))
1378 (cxml:text
(pattern-string pattern
))))
1380 (cxml:with-element
"value"
1381 (let ((type (pattern-type pattern
)))
1382 (cxml:attribute
"datatype-library"
1383 (symbol-name (cxml-types:type-library type
)))
1384 (cxml:attribute
"type" (cxml-types:type-name type
)))
1385 (dolist (param (pattern-params pattern
))
1386 (cxml:with-element
"param"
1387 (cxml:attribute
"name" (cxml-types:param-name param
))
1388 (cxml:text
(cxml-types:param-value param
))))
1389 (when (pattern-except pattern
)
1390 (cxml:with-element
"except"
1391 (serialize-pattern (pattern-except pattern
))))))))
1393 (defun serialize-definition (defn)
1394 (cxml:with-element
"define"
1395 (cxml:attribute
"name" (serialization-name defn
))
1396 (serialize-pattern (defn-child defn
))))
1398 (defun serialize-name (name)
1401 (cxml:with-element
"name"
1402 (cxml:attribute
"ns" (name-uri name
))
1403 (cxml:text
(name-lname name
))))
1405 (cxml:with-element
"anyName"
1406 (when (any-name-except name
)
1407 (serialize-except-name (any-name-except name
)))))
1409 (cxml:with-element
"anyName"
1410 (cxml:attribute
"ns" (ns-name-uri name
))
1411 (when (ns-name-except name
)
1412 (serialize-except-name (ns-name-except name
)))))
1414 (cxml:with-element
"choice"
1415 (serialize-name (name-class-choice-a name
))
1416 (serialize-name (name-class-choice-b name
))))))
1418 (defun serialize-except-name (spec)
1419 (cxml:with-element
"except"
1420 (serialize-name spec
)))
1426 ;;; Foreign attributes and elements are removed implicitly while parsing.
1429 ;;; All character data is discarded while parsing (which can only be
1430 ;;; whitespace after validation).
1432 ;;; Whitespace in name, type, and combine attributes is stripped while
1433 ;;; parsing. Ditto for <name/>.
1435 ;;; 4.3. datatypeLibrary attribute
1436 ;;; Escaping is done by p/pattern.
1437 ;;; Attribute value defaulting is done using *datatype-library*; only
1438 ;;; p/data and p/value record the computed value.
1440 ;;; 4.4. type attribute of value element
1441 ;;; Done by p/value.
1443 ;;; 4.5. href attribute
1444 ;;; Escaping is done by process-include and p/external-ref.
1446 ;;; FIXME: Mime-type handling should be the job of the entity resolver,
1447 ;;; but that requires xstream hacking.
1449 ;;; 4.6. externalRef element
1450 ;;; Done by p/external-ref.
1452 ;;; 4.7. include element
1453 ;;; Done by process-include.
1455 ;;; 4.8. name attribute of element and attribute elements
1456 ;;; `name' is stored as a slot, not a child. Done by p/element and
1459 ;;; 4.9. ns attribute
1460 ;;; done by p/name-class, p/value, p/element, p/attribute
1463 ;;; done by p/name-class
1465 ;;; 4.11. div element
1466 ;;; Legen wir gar nicht erst an.
1468 ;;; 4.12. 4.13 4.14 4.15
1473 ;;; -- ausser der sache mit den datentypen
1475 ;;; 4.17, 4.18, 4.19
1476 ;;; Ueber die Grammar-und Definition Objekte, wie von James Clark
1479 ;;; Dabei werden keine Umbenennungen vorgenommen, weil Referenzierung
1480 ;;; durch Aufbei der Graphenstruktur zwischen ref und Definition
1481 ;;; erfolgt und Namen dann bereits aufgeloest sind. Wir benennen
1482 ;;; dafuer beim Serialisieren um.
1484 (defmethod check-recursion ((pattern element
) depth
)
1485 (check-recursion (pattern-child pattern
) (1+ depth
)))
1487 (defmethod check-recursion ((pattern ref
) depth
)
1488 (when (eql (pattern-crdepth pattern
) depth
)
1489 (rng-error nil
"infinite recursion in ~A"
1490 (defn-name (pattern-target pattern
))))
1491 (when (null (pattern-crdepth pattern
))
1492 (setf (pattern-crdepth pattern
) depth
)
1493 (check-recursion (defn-child (pattern-target pattern
)) depth
)
1494 (setf (pattern-crdepth pattern
) t
)))
1496 (defmethod check-recursion ((pattern %parent
) depth
)
1497 (check-recursion (pattern-child pattern
) depth
))
1499 (defmethod check-recursion ((pattern %combination
) depth
)
1500 (check-recursion (pattern-a pattern
) depth
)
1501 (check-recursion (pattern-b pattern
) depth
))
1503 (defmethod check-recursion ((pattern %leaf
) depth
)
1504 (declare (ignore depth
)))
1506 (defmethod check-recursion ((pattern data
) depth
)
1507 (when (pattern-except pattern
)
1508 (check-recursion (pattern-except pattern
) depth
)))
1515 (defmethod fold-not-allowed ((pattern element
))
1516 (setf (pattern-child pattern
) (fold-not-allowed (pattern-child pattern
)))
1519 (defmethod fold-not-allowed ((pattern %parent
))
1520 (setf (pattern-child pattern
) (fold-not-allowed (pattern-child pattern
)))
1521 (if (typep (pattern-child pattern
) 'not-allowed
)
1522 (pattern-child pattern
)
1527 (defmethod fold-not-allowed ((pattern %combination
))
1528 (setf (pattern-a pattern
) (fold-not-allowed (pattern-a pattern
)))
1529 (setf (pattern-b pattern
) (fold-not-allowed (pattern-b pattern
)))
1532 (defmethod fold-not-allowed ((pattern group
))
1535 ;; remove if any child is not allowed
1536 ((typep (pattern-a pattern
) 'not-allowed
) (pattern-a pattern
))
1537 ((typep (pattern-b pattern
) 'not-allowed
) (pattern-b pattern
))
1540 (defmethod fold-not-allowed ((pattern interleave
))
1543 ;; remove if any child is not allowed
1544 ((typep (pattern-a pattern
) 'not-allowed
) (pattern-a pattern
))
1545 ((typep (pattern-b pattern
) 'not-allowed
) (pattern-b pattern
))
1548 (defmethod fold-not-allowed ((pattern choice
))
1551 ;; if any child is not allowed, choose the other
1552 ((typep (pattern-a pattern
) 'not-allowed
) (pattern-b pattern
))
1553 ((typep (pattern-b pattern
) 'not-allowed
) (pattern-a pattern
))
1558 (defmethod fold-not-allowed ((pattern %leaf
))
1561 (defmethod fold-not-allowed ((pattern data
))
1562 (when (pattern-except pattern
)
1563 (setf (pattern-except pattern
) (fold-not-allowed (pattern-except pattern
)))
1564 (when (typep (pattern-except pattern
) 'not-allowed
)
1565 (setf (pattern-except pattern
) nil
)))
1570 (defmethod fold-not-allowed ((pattern ref
))
1578 (defmethod fold-empty ((pattern one-or-more
))
1580 (if (typep (pattern-child pattern
) 'empty
)
1581 (pattern-child pattern
)
1584 (defmethod fold-empty ((pattern %parent
))
1585 (setf (pattern-child pattern
) (fold-empty (pattern-child pattern
)))
1590 (defmethod fold-empty ((pattern %combination
))
1591 (setf (pattern-a pattern
) (fold-empty (pattern-a pattern
)))
1592 (setf (pattern-b pattern
) (fold-empty (pattern-b pattern
)))
1595 (defmethod fold-empty ((pattern group
))
1598 ;; if any child is empty, choose the other
1599 ((typep (pattern-a pattern
) 'empty
) (pattern-b pattern
))
1600 ((typep (pattern-b pattern
) 'empty
) (pattern-a pattern
))
1603 (defmethod fold-empty ((pattern interleave
))
1606 ;; if any child is empty, choose the other
1607 ((typep (pattern-a pattern
) 'empty
) (pattern-b pattern
))
1608 ((typep (pattern-b pattern
) 'empty
) (pattern-a pattern
))
1611 (defmethod fold-empty ((pattern choice
))
1613 (if (typep (pattern-b pattern
) 'empty
)
1615 ((typep (pattern-a pattern
) 'empty
)
1616 (pattern-a pattern
))
1618 (rotatef (pattern-a pattern
) (pattern-b pattern
))
1624 (defmethod fold-empty ((pattern %leaf
))
1627 (defmethod fold-empty ((pattern data
))
1628 (when (pattern-except pattern
)
1629 (setf (pattern-except pattern
) (fold-empty (pattern-except pattern
))))
1634 (defmethod fold-empty ((pattern ref
))
1638 ;;;; name class overlap
1640 ;;; fixme: memorize this stuff?
1642 (defparameter !uri
(string (code-char 1)))
1643 (defparameter !lname
"")
1645 (defun classes-overlap-p (nc1 nc2
)
1646 (flet ((both-contain (x)
1647 (and (contains nc1
(car x
) (cdr x
))
1648 (contains nc2
(car x
) (cdr x
)))))
1649 (or (some #'both-contain
(representatives nc1
))
1650 (some #'both-contain
(representatives nc2
)))))
1652 (defmethod representatives ((nc any-name
))
1653 (cons (cons !uri
!lname
)
1654 (if (any-name-except nc
)
1655 (representatives (any-name-except nc
))
1658 (defmethod representatives ((nc ns-name
))
1659 (cons (cons (ns-name-uri nc
) !lname
)
1660 (if (ns-name-except nc
)
1661 (representatives (ns-name-except nc
))
1664 (defmethod representatives ((nc name
))
1665 (list (cons (name-uri nc
) (name-lname nc
))))
1667 (defmethod representatives ((nc name-class-choice
))
1668 (nconc (representatives (name-class-choice-a nc
))
1669 (representatives (name-class-choice-b nc
))))
1674 (defun finalize-definitions (pattern)
1675 (let ((defns (make-hash-table)))
1676 (labels ((recurse (p)
1679 (let ((target (pattern-target p
)))
1680 (unless (gethash target defns
)
1681 (setf (gethash target defns
) t
)
1682 (setf (defn-child target
) (recurse (defn-child target
))))
1683 (if (typep (defn-child target
) 'element
)
1685 (copy-pattern-tree (defn-child target
)))))
1689 (when (pattern-except p
)
1690 (setf (pattern-except p
) (recurse (pattern-except p
)))))
1692 (setf (pattern-child p
) (recurse (pattern-child p
))))
1694 (setf (pattern-a p
) (recurse (pattern-a p
)))
1695 (setf (pattern-b p
) (recurse (pattern-b p
))))
1701 for defn being each hash-key in defns
1704 (defun copy-pattern-tree (pattern)
1705 (labels ((recurse (p)
1706 (let ((q (copy-structure p
)))
1709 (when (pattern-except p
)
1710 (setf (pattern-except q
) (recurse (pattern-except p
)))))
1712 (setf (pattern-child q
) (recurse (pattern-child p
))))
1714 (setf (pattern-a q
) (recurse (pattern-a p
)))
1715 (setf (pattern-b q
) (recurse (pattern-b p
))))
1720 (defparameter *in-attribute-p
* nil
)
1721 (defparameter *in-one-or-more-p
* nil
)
1722 (defparameter *in-one-or-more
//group-or-interleave-p
* nil
)
1723 (defparameter *in-list-p
* nil
)
1724 (defparameter *in-data-except-p
* nil
)
1725 (defparameter *in-start-p
* nil
)
1727 (defun check-start-restrictions (pattern)
1728 (let ((*in-start-p
* t
))
1729 (check-restrictions pattern
)))
1731 (defun content-type-max (a b
)
1740 (defun groupable-max (a b
)
1741 (if (or (eq a
:empty
)
1743 (and (eq a
:complex
)
1745 (content-type-max a b
)
1748 (defun assert-name-class-finite (nc)
1750 ((or any-name ns-name
)
1751 (rng-error nil
"infinite attribute name class outside of one-or-more"))
1754 (assert-name-class-finite (name-class-choice-a nc
))
1755 (assert-name-class-finite (name-class-choice-b nc
)))))
1757 (defmethod check-restrictions ((pattern attribute
))
1758 (when *in-attribute-p
*
1759 (rng-error nil
"nested attribute not allowed"))
1760 (when *in-one-or-more
//group-or-interleave-p
*
1761 (rng-error nil
"attribute not allowed in oneOrMore//group, oneOrMore//interleave"))
1763 (rng-error nil
"attribute in list not allowed"))
1764 (when *in-data-except-p
*
1765 (rng-error nil
"attribute in data/except not allowed"))
1767 (rng-error nil
"attribute in start not allowed"))
1768 (let ((*in-attribute-p
* t
))
1769 (unless *in-one-or-more-p
*
1770 (assert-name-class-finite (pattern-name pattern
)))
1771 (values (if (check-restrictions (pattern-child pattern
))
1774 (list (pattern-name pattern
))
1777 (defmethod check-restrictions ((pattern ref
))
1778 (when *in-attribute-p
*
1779 (rng-error nil
"ref in attribute not allowed"))
1781 (rng-error nil
"ref in list not allowed"))
1782 (when *in-data-except-p
*
1783 (rng-error nil
"ref in data/except not allowed"))
1786 (list (pattern-name (defn-child (pattern-target pattern
))))
1789 (defmethod check-restrictions ((pattern one-or-more
))
1790 (when *in-data-except-p
*
1791 (rng-error nil
"oneOrMore in data/except not allowed"))
1793 (rng-error nil
"one-or-more in start not allowed"))
1794 (let* ((*in-one-or-more-p
* t
))
1795 (multiple-value-bind (x a e textp
)
1796 (check-restrictions (pattern-child pattern
))
1797 (values (groupable-max x x
) a e textp
))))
1799 (defmethod check-restrictions ((pattern group
))
1800 (when *in-data-except-p
*
1801 (rng-error nil
"group in data/except not allowed"))
1803 (rng-error nil
"group in start not allowed"))
1804 (let ((*in-one-or-more
//group-or-interleave-p
*
1805 *in-one-or-more-p
*))
1806 (multiple-value-bind (x a e tp
) (check-restrictions (pattern-a pattern
))
1807 (multiple-value-bind (y b f tq
) (check-restrictions (pattern-b pattern
))
1810 (when (classes-overlap-p nc1 nc2
)
1811 (rng-error nil
"attribute name overlap in group: ~A ~A"
1813 (values (groupable-max x y
)
1818 (defmethod check-restrictions ((pattern interleave
))
1820 (rng-error nil
"interleave in list not allowed"))
1821 (when *in-data-except-p
*
1822 (rng-error nil
"interleave in data/except not allowed"))
1824 (rng-error nil
"interleave in start not allowed"))
1825 (let ((*in-one-or-more
//group-or-interleave-p
*
1826 *in-one-or-more-p
*))
1827 (multiple-value-bind (x a e tp
) (check-restrictions (pattern-a pattern
))
1828 (multiple-value-bind (y b f tq
) (check-restrictions (pattern-b pattern
))
1831 (when (classes-overlap-p nc1 nc2
)
1832 (rng-error nil
"attribute name overlap in interleave: ~A ~A"
1836 (when (classes-overlap-p nc1 nc2
)
1837 (rng-error nil
"element name overlap in interleave: ~A ~A"
1840 (rng-error nil
"multiple text permitted by interleave"))
1841 (values (groupable-max x y
)
1846 (defmethod check-restrictions ((pattern choice
))
1847 (multiple-value-bind (x a e tp
) (check-restrictions (pattern-a pattern
))
1848 (multiple-value-bind (y b f tq
) (check-restrictions (pattern-b pattern
))
1849 (values (content-type-max x y
)
1854 (defmethod check-restrictions ((pattern list-pattern
))
1856 (rng-error nil
"nested list not allowed"))
1857 (when *in-data-except-p
*
1858 (rng-error nil
"list in data/except not allowed"))
1859 (let ((*in-list-p
* t
))
1860 (check-restrictions (pattern-child pattern
)))
1862 (rng-error nil
"list in start not allowed"))
1865 (defmethod check-restrictions ((pattern text
))
1867 (rng-error nil
"text in list not allowed"))
1868 (when *in-data-except-p
*
1869 (rng-error nil
"text in data/except not allowed"))
1871 (rng-error nil
"text in start not allowed"))
1872 (values :complex nil nil t
))
1874 (defmethod check-restrictions ((pattern data
))
1876 (rng-error nil
"data in start not allowed"))
1877 (when (pattern-except pattern
)
1878 (let ((*in-data-except-p
* t
))
1879 (check-restrictions (pattern-except pattern
))))
1882 (defmethod check-restrictions ((pattern value
))
1884 (rng-error nil
"value in start not allowed"))
1887 (defmethod check-restrictions ((pattern empty
))
1888 (when *in-data-except-p
*
1889 (rng-error nil
"empty in data/except not allowed"))
1891 (rng-error nil
"empty in start not allowed"))
1894 (defmethod check-restrictions ((pattern element
))
1895 (unless (check-restrictions (pattern-child pattern
))
1896 (rng-error nil
"restrictions on string sequences violated")))
1898 (defmethod check-restrictions ((pattern not-allowed
))