1 ;;; rng-match.el --- matching of RELAX NG patterns against XML events
3 ;; Copyright (C) 2003, 2007, 2008 Free Software Foundation, Inc.
6 ;; Keywords: XML, RelaxNG
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This uses the algorithm described in
26 ;; http://www.thaiopensource.com/relaxng/derivative.html
28 ;; The schema to be used is contained in the variable
29 ;; rng-current-schema. It has the form described in the file
38 (defvar rng-not-allowed-ipattern nil
)
39 (defvar rng-empty-ipattern nil
)
40 (defvar rng-text-ipattern nil
)
42 (defvar rng-compile-table nil
)
44 (defvar rng-being-compiled nil
45 "Contains a list of ref patterns currently being compiled.
46 Used to detect illegal recursive references.")
48 (defvar rng-ipattern-table nil
)
50 (defvar rng-last-ipattern-index nil
)
52 (defvar rng-match-state nil
53 "An ipattern representing the current state of validation.")
57 (defsubst rng-update-match-state
(new-state)
58 (if (and (eq new-state rng-not-allowed-ipattern
)
59 (not (eq rng-match-state rng-not-allowed-ipattern
)))
61 (setq rng-match-state new-state
)
67 (defun rng-ipattern-slot-accessor-name (slot-name)
68 (intern (concat "rng-ipattern-get-"
69 (symbol-name slot-name
))))
71 (defun rng-ipattern-slot-setter-name (slot-name)
72 (intern (concat "rng-ipattern-set-"
73 (symbol-name slot-name
)))))
75 (defmacro rng-ipattern-defslot
(slot-name index
)
77 (defsubst ,(rng-ipattern-slot-accessor-name slot-name
) (ipattern)
78 (aref ipattern
,index
))
79 (defsubst ,(rng-ipattern-slot-setter-name slot-name
) (ipattern value
)
80 (aset ipattern
,index value
))))
82 (rng-ipattern-defslot type
0)
83 (rng-ipattern-defslot index
1)
84 (rng-ipattern-defslot name-class
2)
85 (rng-ipattern-defslot datatype
2)
86 (rng-ipattern-defslot after
2)
87 (rng-ipattern-defslot child
3)
88 (rng-ipattern-defslot value-object
3)
89 (rng-ipattern-defslot nullable
4)
90 (rng-ipattern-defslot memo-text-typed
5)
91 (rng-ipattern-defslot memo-map-start-tag-open-deriv
6)
92 (rng-ipattern-defslot memo-map-start-attribute-deriv
7)
93 (rng-ipattern-defslot memo-start-tag-close-deriv
8)
94 (rng-ipattern-defslot memo-text-only-deriv
9)
95 (rng-ipattern-defslot memo-mixed-text-deriv
10)
96 (rng-ipattern-defslot memo-map-data-deriv
11)
97 (rng-ipattern-defslot memo-end-tag-deriv
12)
99 (defconst rng-memo-map-alist-max
10)
101 (defsubst rng-memo-map-get
(key mm
)
102 "Return the value associated with KEY in memo-map MM."
103 (let ((found (assoc key mm
)))
107 (let ((head (car mm
)))
108 (and (hash-table-p head
)
109 (gethash key head
)))))))
111 (defun rng-memo-map-add (key value mm
&optional weakness
)
112 "Associate KEY with VALUE in memo-map MM and return the new memo-map.
113 The new memo-map may or may not be a different object from MM.
115 Alists are better for small maps. Hash tables are better for large
116 maps. A memo-map therefore starts off as an alist and switches to a
117 hash table for large memo-maps. A memo-map is always a list. An empty
118 memo-map is represented by nil. A large memo-map is represented by a
119 list containing just a hash-table. A small memo map is represented by
120 a list whose cdr is an alist and whose car is the number of entries in
121 the alist. The complete memo-map can be passed to assoc without
122 problems: assoc ignores any members that are not cons cells. There is
123 therefore minimal overhead in successful lookups on small lists
124 \(which is the most common case)."
126 (list 1 (cons key value
))
127 (let ((head (car mm
)))
128 (cond ((hash-table-p head
)
129 (puthash key value head
)
131 ((>= head rng-memo-map-alist-max
)
132 (let ((ht (make-hash-table :test
'equal
134 :size
(* 2 rng-memo-map-alist-max
))))
138 (puthash (car head
) (cdr head
) ht
)
142 (cons (cons key value
)
145 (defsubst rng-make-ipattern
(type index name-class child nullable
)
146 (vector type index name-class child nullable
149 ;; 6 memo-map-start-tag-open-deriv
151 ;; 7 memo-map-start-attribute-deriv
153 ;; 8 memo-start-tag-close-deriv
155 ;; 9 memo-text-only-deriv
157 ;; 10 memo-mixed-text-deriv
159 ;; 11 memo-map-data-deriv
161 ;; 12 memo-end-tag-deriv
164 (defun rng-ipattern-maybe-init ()
165 (unless rng-ipattern-table
166 (setq rng-ipattern-table
(make-hash-table :test
'equal
))
167 (setq rng-last-ipattern-index -
1)))
169 (defun rng-ipattern-clear ()
170 (when rng-ipattern-table
171 (clrhash rng-ipattern-table
))
172 (setq rng-last-ipattern-index -
1))
174 (defsubst rng-gen-ipattern-index
()
175 (setq rng-last-ipattern-index
(1+ rng-last-ipattern-index
)))
177 (defun rng-put-ipattern (key type name-class child nullable
)
179 (rng-make-ipattern type
180 (rng-gen-ipattern-index)
184 (puthash key ipattern rng-ipattern-table
)
187 (defun rng-get-ipattern (key)
188 (gethash key rng-ipattern-table
))
190 (or rng-not-allowed-ipattern
191 (setq rng-not-allowed-ipattern
192 (rng-make-ipattern 'not-allowed -
3 nil nil nil
)))
194 (or rng-empty-ipattern
195 (setq rng-empty-ipattern
196 (rng-make-ipattern 'empty -
2 nil nil t
)))
198 (or rng-text-ipattern
199 (setq rng-text-ipattern
200 (rng-make-ipattern 'text -
1 nil nil t
)))
202 (defconst rng-const-ipatterns
203 (list rng-not-allowed-ipattern
207 (defun rng-intern-after (child after
)
208 (if (eq child rng-not-allowed-ipattern
)
209 rng-not-allowed-ipattern
210 (let ((key (list 'after
211 (rng-ipattern-get-index child
)
212 (rng-ipattern-get-index after
))))
213 (or (rng-get-ipattern key
)
214 (rng-put-ipattern key
220 (defun rng-intern-attribute (name-class ipattern
)
221 (if (eq ipattern rng-not-allowed-ipattern
)
222 rng-not-allowed-ipattern
223 (let ((key (list 'attribute
225 (rng-ipattern-get-index ipattern
))))
226 (or (rng-get-ipattern key
)
227 (rng-put-ipattern key
233 (defun rng-intern-data (dt matches-anything
)
234 (let ((key (list 'data dt
)))
235 (or (rng-get-ipattern key
)
236 (let ((ipattern (rng-put-ipattern key
241 (rng-ipattern-set-memo-text-typed ipattern
242 (not matches-anything
))
245 (defun rng-intern-data-except (dt ipattern
)
246 (let ((key (list 'data-except dt ipattern
)))
247 (or (rng-get-ipattern key
)
248 (rng-put-ipattern key
254 (defun rng-intern-value (dt obj
)
255 (let ((key (list 'value dt obj
)))
256 (or (rng-get-ipattern key
)
257 (rng-put-ipattern key
263 (defun rng-intern-one-or-more (ipattern)
264 (or (rng-intern-one-or-more-shortcut ipattern
)
265 (let ((key (cons 'one-or-more
266 (list (rng-ipattern-get-index ipattern
)))))
267 (or (rng-get-ipattern key
)
268 (rng-put-ipattern key
272 (rng-ipattern-get-nullable ipattern
))))))
274 (defun rng-intern-one-or-more-shortcut (ipattern)
275 (cond ((eq ipattern rng-not-allowed-ipattern
)
276 rng-not-allowed-ipattern
)
277 ((eq ipattern rng-empty-ipattern
)
279 ((eq (rng-ipattern-get-type ipattern
) 'one-or-more
)
283 (defun rng-intern-list (ipattern)
284 (if (eq ipattern rng-not-allowed-ipattern
)
285 rng-not-allowed-ipattern
286 (let ((key (cons 'list
287 (list (rng-ipattern-get-index ipattern
)))))
288 (or (rng-get-ipattern key
)
289 (rng-put-ipattern key
295 (defun rng-intern-group (ipatterns)
296 "Return a ipattern for the list of group members in IPATTERNS."
297 (or (rng-intern-group-shortcut ipatterns
)
298 (let* ((tem (rng-normalize-group-list ipatterns
))
299 (normalized (cdr tem
)))
300 (or (rng-intern-group-shortcut normalized
)
301 (let ((key (cons 'group
302 (mapcar 'rng-ipattern-get-index normalized
))))
303 (or (rng-get-ipattern key
)
304 (rng-put-ipattern key
310 (defun rng-intern-group-shortcut (ipatterns)
311 "Try to shortcut interning a group list. If successful, return the
312 interned pattern. Otherwise return nil."
313 (while (and ipatterns
314 (eq (car ipatterns
) rng-empty-ipattern
))
315 (setq ipatterns
(cdr ipatterns
)))
317 (let ((ret (car ipatterns
)))
318 (if (eq ret rng-not-allowed-ipattern
)
319 rng-not-allowed-ipattern
320 (setq ipatterns
(cdr ipatterns
))
321 (while (and ipatterns ret
)
322 (let ((tem (car ipatterns
)))
323 (cond ((eq tem rng-not-allowed-ipattern
)
325 (setq ipatterns nil
))
326 ((eq tem rng-empty-ipattern
)
327 (setq ipatterns
(cdr ipatterns
)))
329 ;; Stop here rather than continuing
330 ;; looking for not-allowed patterns.
331 ;; We do a complete scan elsewhere.
336 (defun rng-normalize-group-list (ipatterns)
337 "Normalize a list containing members of a group.
338 Expands nested groups, removes empty members, handles notAllowed.
339 Returns a pair whose car says whether the list is nullable and whose
340 cdr is the normalized list."
345 (setq member
(car ipatterns
))
346 (setq ipatterns
(cdr ipatterns
))
348 (setq nullable
(rng-ipattern-get-nullable member
)))
349 (cond ((eq (rng-ipattern-get-type member
) 'group
)
351 (nconc (reverse (rng-ipattern-get-child member
))
353 ((eq member rng-not-allowed-ipattern
)
354 (setq result
(list rng-not-allowed-ipattern
))
355 (setq ipatterns nil
))
356 ((not (eq member rng-empty-ipattern
))
357 (setq result
(cons member result
)))))
358 (cons nullable
(nreverse result
))))
360 (defun rng-intern-interleave (ipatterns)
361 (or (rng-intern-group-shortcut ipatterns
)
362 (let* ((tem (rng-normalize-interleave-list ipatterns
))
363 (normalized (cdr tem
)))
364 (or (rng-intern-group-shortcut normalized
)
365 (let ((key (cons 'interleave
366 (mapcar 'rng-ipattern-get-index normalized
))))
367 (or (rng-get-ipattern key
)
368 (rng-put-ipattern key
374 (defun rng-normalize-interleave-list (ipatterns)
375 "Normalize a list containing members of an interleave.
376 Expands nested groups, removes empty members, handles notAllowed.
377 Returns a pair whose car says whether the list is nullable and whose
378 cdr is the normalized list."
383 (setq member
(car ipatterns
))
384 (setq ipatterns
(cdr ipatterns
))
386 (setq nullable
(rng-ipattern-get-nullable member
)))
387 (cond ((eq (rng-ipattern-get-type member
) 'interleave
)
389 (append (rng-ipattern-get-child member
)
391 ((eq member rng-not-allowed-ipattern
)
392 (setq result
(list rng-not-allowed-ipattern
))
393 (setq ipatterns nil
))
394 ((not (eq member rng-empty-ipattern
))
395 (setq result
(cons member result
)))))
396 (cons nullable
(sort result
'rng-compare-ipattern
))))
398 ;; Would be cleaner if this didn't modify IPATTERNS.
400 (defun rng-intern-choice (ipatterns)
401 "Return a choice ipattern for the list of choices in IPATTERNS.
402 May alter IPATTERNS."
403 (or (rng-intern-choice-shortcut ipatterns
)
404 (let* ((tem (rng-normalize-choice-list ipatterns
))
405 (normalized (cdr tem
)))
406 (or (rng-intern-choice-shortcut normalized
)
407 (rng-intern-choice1 normalized
(car tem
))))))
409 (defun rng-intern-optional (ipattern)
410 (cond ((rng-ipattern-get-nullable ipattern
) ipattern
)
411 ((eq ipattern rng-not-allowed-ipattern
) rng-empty-ipattern
)
412 (t (rng-intern-choice1
413 ;; This is sorted since the empty pattern
414 ;; is before everything except not allowed.
415 ;; It cannot have a duplicate empty pattern,
416 ;; since it is not nullable.
417 (cons rng-empty-ipattern
418 (if (eq (rng-ipattern-get-type ipattern
) 'choice
)
419 (rng-ipattern-get-child ipattern
)
424 (defun rng-intern-choice1 (normalized nullable
)
425 (let ((key (cons 'choice
426 (mapcar 'rng-ipattern-get-index normalized
))))
427 (or (rng-get-ipattern key
)
428 (rng-put-ipattern key
434 (defun rng-intern-choice-shortcut (ipatterns)
435 "Try to shortcut interning a choice list. If successful, return the
436 interned pattern. Otherwise return nil."
437 (while (and ipatterns
439 rng-not-allowed-ipattern
))
440 (setq ipatterns
(cdr ipatterns
)))
442 (let ((ret (car ipatterns
)))
443 (setq ipatterns
(cdr ipatterns
))
444 (while (and ipatterns ret
)
445 (or (eq (car ipatterns
) rng-not-allowed-ipattern
)
446 (eq (car ipatterns
) ret
)
448 (setq ipatterns
(cdr ipatterns
)))
450 rng-not-allowed-ipattern
))
452 (defun rng-normalize-choice-list (ipatterns)
453 "Normalize a list of choices, expanding nested choices, removing
454 not-allowed members, sorting by index and removing duplicates. Return
455 a pair whose car says whether the list is nullable and whose cdr is
456 the normalized list."
459 (head (cons nil ipatterns
)))
465 ;; the cdr of tail is always cur
467 (setq member
(car cur
))
469 (setq nullable
(rng-ipattern-get-nullable member
)))
470 (cond ((eq (rng-ipattern-get-type member
) 'choice
)
472 (append (rng-ipattern-get-child member
)
477 ((eq member rng-not-allowed-ipattern
)
482 (let ((cur-index (rng-ipattern-get-index member
)))
483 (if (>= prev-index cur-index
)
484 (or (= prev-index cur-index
) ; will remove it
485 (setq sorted nil
)) ; won't remove it
486 (setq prev-index cur-index
)
495 (setq cur
(cdr cur
))))))
496 (setcdr tail final-tail
))
497 (setq head
(cdr head
))
501 (rng-uniquify-eq (sort head
'rng-compare-ipattern
))))))
503 (defun rng-compare-ipattern (p1 p2
)
504 (< (rng-ipattern-get-index p1
)
505 (rng-ipattern-get-index p2
)))
509 (defsubst rng-name-class-contains
(nc nm
)
512 (rng-name-class-contains1 nc nm
)))
514 (defun rng-name-class-contains1 (nc nm
)
515 (let ((type (aref nc
0)))
516 (cond ((eq type
'any-name
) t
)
517 ((eq type
'any-name-except
)
518 (not (rng-name-class-contains (aref nc
1) nm
)))
520 (eq (car nm
) (aref nc
1)))
521 ((eq type
'ns-name-except
)
522 (and (eq (car nm
) (aref nc
1))
523 (not (rng-name-class-contains (aref nc
2) nm
))))
525 (let ((choices (aref nc
1))
528 (if (rng-name-class-contains (car choices
) nm
)
532 (setq choices
(cdr choices
))))
535 (defun rng-name-class-possible-names (nc accum
)
536 "Return a list of possible names that nameclass NC can match.
538 Each possible name should be returned as a (NAMESPACE . LOCAL-NAME)
539 pair, where NAMESPACE is a symbol or nil and LOCAL-NAME is a string.
540 nil for NAMESPACE matches the absent namespace. ACCUM is a list of
541 names which should be appended to the returned list. The returned list
542 may contain duplicates."
545 (when (eq (aref nc
0) 'choice
)
546 (let ((members (aref nc
1)) member
)
548 (setq member
(car members
))
552 (rng-name-class-possible-names member
554 (setq members
(cdr members
)))))
557 ;;; Debugging utilities
559 (defun rng-ipattern-to-string (ipattern)
560 (let ((type (rng-ipattern-get-type ipattern
)))
561 (cond ((eq type
'after
)
562 (concat (rng-ipattern-to-string
563 (rng-ipattern-get-child ipattern
))
565 (rng-ipattern-to-string
566 (rng-ipattern-get-after ipattern
))))
569 (rng-name-class-to-string
570 (rng-ipattern-get-name-class ipattern
))
571 ;; we can get cycles with elements so don't print it out
573 ((eq type
'attribute
)
575 (rng-name-class-to-string
576 (rng-ipattern-get-name-class ipattern
))
578 (rng-ipattern-to-string
579 (rng-ipattern-get-child ipattern
))
581 ((eq type
'empty
) "empty")
582 ((eq type
'text
) "text")
583 ((eq type
'not-allowed
) "notAllowed")
584 ((eq type
'one-or-more
)
585 (concat (rng-ipattern-to-string
586 (rng-ipattern-get-child ipattern
))
590 (mapconcat 'rng-ipattern-to-string
591 (rng-ipattern-get-child ipattern
)
596 (mapconcat 'rng-ipattern-to-string
597 (rng-ipattern-get-child ipattern
)
600 ((eq type
'interleave
)
602 (mapconcat 'rng-ipattern-to-string
603 (rng-ipattern-get-child ipattern
)
606 (t (symbol-name type
)))))
608 (defun rng-name-class-to-string (nc)
611 (let ((type (aref nc
0)))
612 (cond ((eq type
'choice
)
613 (mapconcat 'rng-name-class-to-string
616 (t (concat (symbol-name type
) "*"))))))
621 (defun rng-compile-maybe-init ()
622 (unless rng-compile-table
623 (setq rng-compile-table
(make-hash-table :test
'eq
))))
625 (defun rng-compile-clear ()
626 (when rng-compile-table
627 (clrhash rng-compile-table
)))
629 (defun rng-compile (pattern)
630 (or (gethash pattern rng-compile-table
)
631 (let ((ipattern (apply (get (car pattern
) 'rng-compile
)
633 (puthash pattern ipattern rng-compile-table
)
636 (put 'empty
'rng-compile
'rng-compile-empty
)
637 (put 'text
'rng-compile
'rng-compile-text
)
638 (put 'not-allowed
'rng-compile
'rng-compile-not-allowed
)
639 (put 'element
'rng-compile
'rng-compile-element
)
640 (put 'attribute
'rng-compile
'rng-compile-attribute
)
641 (put 'choice
'rng-compile
'rng-compile-choice
)
642 (put 'optional
'rng-compile
'rng-compile-optional
)
643 (put 'group
'rng-compile
'rng-compile-group
)
644 (put 'interleave
'rng-compile
'rng-compile-interleave
)
645 (put 'ref
'rng-compile
'rng-compile-ref
)
646 (put 'one-or-more
'rng-compile
'rng-compile-one-or-more
)
647 (put 'zero-or-more
'rng-compile
'rng-compile-zero-or-more
)
648 (put 'mixed
'rng-compile
'rng-compile-mixed
)
649 (put 'data
'rng-compile
'rng-compile-data
)
650 (put 'data-except
'rng-compile
'rng-compile-data-except
)
651 (put 'value
'rng-compile
'rng-compile-value
)
652 (put 'list
'rng-compile
'rng-compile-list
)
654 (defun rng-compile-not-allowed () rng-not-allowed-ipattern
)
655 (defun rng-compile-empty () rng-empty-ipattern
)
656 (defun rng-compile-text () rng-text-ipattern
)
658 (defun rng-compile-element (name-class pattern
)
660 (rng-make-ipattern 'element
661 (rng-gen-ipattern-index)
662 (rng-compile-name-class name-class
)
663 pattern
; compile lazily
666 (defun rng-element-get-child (element)
667 (let ((tem (rng-ipattern-get-child element
)))
670 (rng-ipattern-set-child element
(rng-compile tem
)))))
672 (defun rng-compile-attribute (name-class pattern
)
673 (rng-intern-attribute (rng-compile-name-class name-class
)
674 (rng-compile pattern
)))
676 (defun rng-compile-ref (pattern name
)
677 (and (memq pattern rng-being-compiled
)
678 (rng-compile-error "Reference loop on symbol %s" name
))
679 (setq rng-being-compiled
680 (cons pattern rng-being-compiled
))
682 (rng-compile pattern
)
683 (setq rng-being-compiled
684 (cdr rng-being-compiled
))))
686 (defun rng-compile-one-or-more (pattern)
687 (rng-intern-one-or-more (rng-compile pattern
)))
689 (defun rng-compile-zero-or-more (pattern)
691 (rng-intern-one-or-more (rng-compile pattern
))))
693 (defun rng-compile-optional (pattern)
694 (rng-intern-optional (rng-compile pattern
)))
696 (defun rng-compile-mixed (pattern)
697 (rng-intern-interleave (cons rng-text-ipattern
698 (list (rng-compile pattern
)))))
700 (defun rng-compile-list (pattern)
701 (rng-intern-list (rng-compile pattern
)))
703 (defun rng-compile-choice (&rest patterns
)
704 (rng-intern-choice (mapcar 'rng-compile patterns
)))
706 (defun rng-compile-group (&rest patterns
)
707 (rng-intern-group (mapcar 'rng-compile patterns
)))
709 (defun rng-compile-interleave (&rest patterns
)
710 (rng-intern-interleave (mapcar 'rng-compile patterns
)))
712 (defun rng-compile-dt (name params
)
713 (let ((rng-dt-error-reporter 'rng-compile-error
))
714 (funcall (let ((uri (car name
)))
715 (or (get uri
'rng-dt-compile
)
716 (rng-compile-error "Unknown datatype library %s" uri
)))
720 (defun rng-compile-data (name params
)
721 (let ((dt (rng-compile-dt name params
)))
722 (rng-intern-data (cdr dt
) (car dt
))))
724 (defun rng-compile-data-except (name params pattern
)
725 (rng-intern-data-except (cdr (rng-compile-dt name params
))
726 (rng-compile pattern
)))
728 (defun rng-compile-value (name str context
)
729 (let* ((dt (cdr (rng-compile-dt name
'())))
730 (rng-dt-namespace-context-getter (list 'identity context
))
731 (obj (rng-dt-make-value dt str
)))
733 (rng-intern-value dt obj
)
734 (rng-compile-error "Value %s is not a valid instance of the datatype %s"
738 (defun rng-compile-name-class (nc)
739 (let ((type (car nc
)))
740 (cond ((eq type
'name
) (nth 1 nc
))
741 ((eq type
'any-name
) [any-name
])
742 ((eq type
'any-name-except
)
743 (vector 'any-name-except
744 (rng-compile-name-class (nth 1 nc
))))
746 (vector 'ns-name
(nth 1 nc
)))
747 ((eq type
'ns-name-except
)
748 (vector 'ns-name-except
750 (rng-compile-name-class (nth 2 nc
))))
753 (mapcar 'rng-compile-name-class
(cdr nc
))))
754 (t (error "Bad name-class type %s" type
)))))
756 ;;; Searching patterns
758 ;; We write this non-recursively to avoid hitting max-lisp-eval-depth
761 (defun rng-map-element-attribute (function pattern accum
&rest args
)
762 (let ((searched (make-hash-table :test
'eq
))
765 (setq type
(car pattern
))
766 (cond ((memq type
'(element attribute
))
771 (setq pattern
(nth 2 pattern
)))
773 (setq pattern
(nth 1 pattern
))
774 (if (gethash pattern searched
)
776 (puthash pattern t searched
)))
777 ((memq type
'(choice group interleave
))
778 (setq todo
(cons (cdr pattern
) todo
))
780 ((memq type
'(one-or-more
784 (setq pattern
(nth 1 pattern
)))
785 (t (setq pattern nil
)))
788 (setq pattern
(car patterns
))
789 (setq patterns
(cdr patterns
))
792 (setq patterns
(car todo
))
793 (setq todo
(cdr todo
))
794 (setq pattern
(car patterns
))
795 (setq patterns
(cdr patterns
))
799 (defun rng-find-element-content-pattern (pattern accum name
)
800 (if (and (eq (car pattern
) 'element
)
801 (rng-search-name name
(nth 1 pattern
)))
802 (cons (rng-compile (nth 2 pattern
)) accum
)
805 (defun rng-search-name (name nc
)
806 (let ((type (car nc
)))
807 (cond ((eq type
'name
)
808 (equal (cadr nc
) name
))
810 (let ((choices (cdr nc
))
812 (while (and choices
(not found
))
813 (if (rng-search-name name
(car choices
))
815 (setq choices
(cdr choices
))))
819 (defun rng-find-name-class-uris (nc accum
)
820 (let ((type (car nc
)))
821 (cond ((eq type
'name
)
822 (rng-accum-namespace-uri (car (nth 1 nc
)) accum
))
823 ((memq type
'(ns-name ns-name-except
))
824 (rng-accum-namespace-uri (nth 1 nc
) accum
))
826 (let ((choices (cdr nc
)))
829 (rng-find-name-class-uris (car choices
) accum
))
830 (setq choices
(cdr choices
))))
834 (defun rng-accum-namespace-uri (ns accum
)
835 (if (and ns
(not (memq ns accum
)))
841 (defun rng-ipattern-text-typed-p (ipattern)
842 (let ((memo (rng-ipattern-get-memo-text-typed ipattern
)))
843 (if (eq memo
'unknown
)
844 (rng-ipattern-set-memo-text-typed
846 (rng-ipattern-compute-text-typed-p ipattern
))
849 (defun rng-ipattern-compute-text-typed-p (ipattern)
850 (let ((type (rng-ipattern-get-type ipattern
)))
851 (cond ((eq type
'choice
)
852 (let ((cur (rng-ipattern-get-child ipattern
))
854 (while (and cur
(not ret
))
855 (if (rng-ipattern-text-typed-p (car cur
))
857 (setq cur
(cdr cur
))))
860 (let ((cur (rng-ipattern-get-child ipattern
))
863 (while (and cur
(not ret
))
864 (setq member
(car cur
))
865 (if (rng-ipattern-text-typed-p member
)
868 (and (rng-ipattern-get-nullable member
)
872 (rng-ipattern-text-typed-p (rng-ipattern-get-child ipattern
)))
873 (t (and (memq type
'(value list data data-except
)) t
)))))
875 (defun rng-start-tag-open-deriv (ipattern nm
)
876 (or (rng-memo-map-get
878 (rng-ipattern-get-memo-map-start-tag-open-deriv ipattern
))
879 (rng-ipattern-memo-start-tag-open-deriv
882 (rng-compute-start-tag-open-deriv ipattern nm
))))
884 (defun rng-ipattern-memo-start-tag-open-deriv (ipattern nm deriv
)
885 (or (memq ipattern rng-const-ipatterns
)
886 (rng-ipattern-set-memo-map-start-tag-open-deriv
890 (rng-ipattern-get-memo-map-start-tag-open-deriv
894 (defun rng-compute-start-tag-open-deriv (ipattern nm
)
895 (let ((type (rng-ipattern-get-type ipattern
)))
896 (cond ((eq type
'choice
)
897 (rng-transform-choice `(lambda (p)
898 (rng-start-tag-open-deriv p
',nm
))
901 (if (rng-name-class-contains
902 (rng-ipattern-get-name-class ipattern
)
904 (rng-intern-after (rng-element-get-child ipattern
)
906 rng-not-allowed-ipattern
))
908 (rng-transform-group-nullable
909 `(lambda (p) (rng-start-tag-open-deriv p
',nm
))
910 'rng-cons-group-after
912 ((eq type
'interleave
)
913 (rng-transform-interleave-single
914 `(lambda (p) (rng-start-tag-open-deriv p
',nm
))
915 'rng-subst-interleave-after
917 ((eq type
'one-or-more
)
920 (rng-intern-group (list p
,(rng-intern-optional ipattern
))))
921 (rng-start-tag-open-deriv (rng-ipattern-get-child ipattern
)
927 ,(rng-ipattern-get-after ipattern
)))
928 (rng-start-tag-open-deriv (rng-ipattern-get-child ipattern
)
930 (t rng-not-allowed-ipattern
))))
932 (defun rng-start-attribute-deriv (ipattern nm
)
933 (or (rng-memo-map-get
935 (rng-ipattern-get-memo-map-start-attribute-deriv ipattern
))
936 (rng-ipattern-memo-start-attribute-deriv
939 (rng-compute-start-attribute-deriv ipattern nm
))))
941 (defun rng-ipattern-memo-start-attribute-deriv (ipattern nm deriv
)
942 (or (memq ipattern rng-const-ipatterns
)
943 (rng-ipattern-set-memo-map-start-attribute-deriv
948 (rng-ipattern-get-memo-map-start-attribute-deriv ipattern
))))
951 (defun rng-compute-start-attribute-deriv (ipattern nm
)
952 (let ((type (rng-ipattern-get-type ipattern
)))
953 (cond ((eq type
'choice
)
954 (rng-transform-choice `(lambda (p)
955 (rng-start-attribute-deriv p
',nm
))
957 ((eq type
'attribute
)
958 (if (rng-name-class-contains
959 (rng-ipattern-get-name-class ipattern
)
961 (rng-intern-after (rng-ipattern-get-child ipattern
)
963 rng-not-allowed-ipattern
))
965 (rng-transform-interleave-single
966 `(lambda (p) (rng-start-attribute-deriv p
',nm
))
967 'rng-subst-group-after
969 ((eq type
'interleave
)
970 (rng-transform-interleave-single
971 `(lambda (p) (rng-start-attribute-deriv p
',nm
))
972 'rng-subst-interleave-after
974 ((eq type
'one-or-more
)
977 (rng-intern-group (list p
,(rng-intern-optional ipattern
))))
978 (rng-start-attribute-deriv (rng-ipattern-get-child ipattern
)
983 (rng-intern-after p
,(rng-ipattern-get-after ipattern
)))
984 (rng-start-attribute-deriv (rng-ipattern-get-child ipattern
)
986 (t rng-not-allowed-ipattern
))))
988 (defun rng-cons-group-after (x y
)
989 (rng-apply-after `(lambda (p) (rng-intern-group (cons p
',y
)))
992 (defun rng-subst-group-after (new old list
)
993 (rng-apply-after `(lambda (p)
994 (rng-intern-group (rng-substq p
,old
',list
)))
997 (defun rng-subst-interleave-after (new old list
)
998 (rng-apply-after `(lambda (p)
999 (rng-intern-interleave (rng-substq p
,old
',list
)))
1002 (defun rng-apply-after (f ipattern
)
1003 (let ((type (rng-ipattern-get-type ipattern
)))
1004 (cond ((eq type
'after
)
1006 (rng-ipattern-get-child ipattern
)
1008 (rng-ipattern-get-after ipattern
))))
1010 (rng-transform-choice `(lambda (x) (rng-apply-after ,f x
))
1012 (t rng-not-allowed-ipattern
))))
1014 (defun rng-start-tag-close-deriv (ipattern)
1015 (or (rng-ipattern-get-memo-start-tag-close-deriv ipattern
)
1016 (rng-ipattern-set-memo-start-tag-close-deriv
1018 (rng-compute-start-tag-close-deriv ipattern
))))
1020 (defconst rng-transform-map
1021 '((choice . rng-transform-choice
)
1022 (group . rng-transform-group
)
1023 (interleave . rng-transform-interleave
)
1024 (one-or-more . rng-transform-one-or-more
)
1025 (after . rng-transform-after-child
)))
1027 (defun rng-compute-start-tag-close-deriv (ipattern)
1028 (let* ((type (rng-ipattern-get-type ipattern
)))
1029 (if (eq type
'attribute
)
1030 rng-not-allowed-ipattern
1031 (let ((transform (assq type rng-transform-map
)))
1033 (funcall (cdr transform
)
1034 'rng-start-tag-close-deriv
1038 (defun rng-ignore-attributes-deriv (ipattern)
1039 (let* ((type (rng-ipattern-get-type ipattern
)))
1040 (if (eq type
'attribute
)
1042 (let ((transform (assq type rng-transform-map
)))
1044 (funcall (cdr transform
)
1045 'rng-ignore-attributes-deriv
1049 (defun rng-text-only-deriv (ipattern)
1050 (or (rng-ipattern-get-memo-text-only-deriv ipattern
)
1051 (rng-ipattern-set-memo-text-only-deriv
1053 (rng-compute-text-only-deriv ipattern
))))
1055 (defun rng-compute-text-only-deriv (ipattern)
1056 (let* ((type (rng-ipattern-get-type ipattern
)))
1057 (if (eq type
'element
)
1058 rng-not-allowed-ipattern
1059 (let ((transform (assq type
1060 '((choice . rng-transform-choice
)
1061 (group . rng-transform-group
)
1062 (interleave . rng-transform-interleave
)
1063 (one-or-more . rng-transform-one-or-more
)
1064 (after . rng-transform-after-child
)))))
1066 (funcall (cdr transform
)
1067 'rng-text-only-deriv
1071 (defun rng-mixed-text-deriv (ipattern)
1072 (or (rng-ipattern-get-memo-mixed-text-deriv ipattern
)
1073 (rng-ipattern-set-memo-mixed-text-deriv
1075 (rng-compute-mixed-text-deriv ipattern
))))
1077 (defun rng-compute-mixed-text-deriv (ipattern)
1078 (let ((type (rng-ipattern-get-type ipattern
)))
1079 (cond ((eq type
'text
) ipattern
)
1081 (rng-transform-after-child 'rng-mixed-text-deriv
1084 (rng-transform-choice 'rng-mixed-text-deriv
1086 ((eq type
'one-or-more
)
1088 (list (rng-mixed-text-deriv
1089 (rng-ipattern-get-child ipattern
))
1090 (rng-intern-optional ipattern
))))
1092 (rng-transform-group-nullable
1093 'rng-mixed-text-deriv
1094 (lambda (x y
) (rng-intern-group (cons x y
)))
1096 ((eq type
'interleave
)
1097 (rng-transform-interleave-single
1098 'rng-mixed-text-deriv
1099 (lambda (new old list
) (rng-intern-interleave
1100 (rng-substq new old list
)))
1102 ((and (eq type
'data
)
1103 (not (rng-ipattern-get-memo-text-typed ipattern
)))
1105 (t rng-not-allowed-ipattern
))))
1107 (defun rng-end-tag-deriv (ipattern)
1108 (or (rng-ipattern-get-memo-end-tag-deriv ipattern
)
1109 (rng-ipattern-set-memo-end-tag-deriv
1111 (rng-compute-end-tag-deriv ipattern
))))
1113 (defun rng-compute-end-tag-deriv (ipattern)
1114 (let ((type (rng-ipattern-get-type ipattern
)))
1115 (cond ((eq type
'choice
)
1117 (mapcar 'rng-end-tag-deriv
1118 (rng-ipattern-get-child ipattern
))))
1120 (if (rng-ipattern-get-nullable
1121 (rng-ipattern-get-child ipattern
))
1122 (rng-ipattern-get-after ipattern
)
1123 rng-not-allowed-ipattern
))
1124 (t rng-not-allowed-ipattern
))))
1126 (defun rng-data-deriv (ipattern value
)
1127 (or (rng-memo-map-get value
1128 (rng-ipattern-get-memo-map-data-deriv ipattern
))
1129 (and (rng-memo-map-get
1130 (cons value
(rng-namespace-context-get-no-trace))
1131 (rng-ipattern-get-memo-map-data-deriv ipattern
))
1133 (cons value
(apply (car rng-dt-namespace-context-getter
)
1134 (cdr rng-dt-namespace-context-getter
)))
1135 (rng-ipattern-get-memo-map-data-deriv ipattern
)))
1136 (let* ((used-context (vector nil
))
1137 (rng-dt-namespace-context-getter
1138 (cons 'rng-namespace-context-tracer
1140 rng-dt-namespace-context-getter
)))
1141 (deriv (rng-compute-data-deriv ipattern value
)))
1142 (rng-ipattern-memo-data-deriv ipattern
1144 (aref used-context
0)
1147 (defun rng-namespace-context-tracer (used getter
&rest args
)
1148 (let ((context (apply getter args
)))
1149 (aset used
0 context
)
1152 (defun rng-namespace-context-get-no-trace ()
1153 (let ((tem rng-dt-namespace-context-getter
))
1154 (while (and tem
(eq (car tem
) 'rng-namespace-context-tracer
))
1155 (setq tem
(cddr tem
)))
1156 (apply (car tem
) (cdr tem
))))
1158 (defconst rng-memo-data-deriv-max-length
80
1159 "Don't memoize data-derivs for values longer than this.")
1161 (defun rng-ipattern-memo-data-deriv (ipattern value context deriv
)
1162 (or (memq ipattern rng-const-ipatterns
)
1163 (> (length value
) rng-memo-data-deriv-max-length
)
1164 (rng-ipattern-set-memo-map-data-deriv
1166 (rng-memo-map-add (if context
(cons value context
) value
)
1168 (rng-ipattern-get-memo-map-data-deriv ipattern
)
1172 (defun rng-compute-data-deriv (ipattern value
)
1173 (let ((type (rng-ipattern-get-type ipattern
)))
1174 (cond ((eq type
'text
) ipattern
)
1176 (rng-transform-choice `(lambda (p) (rng-data-deriv p
,value
))
1179 (rng-transform-group-nullable
1180 `(lambda (p) (rng-data-deriv p
,value
))
1181 (lambda (x y
) (rng-intern-group (cons x y
)))
1183 ((eq type
'one-or-more
)
1184 (rng-intern-group (list (rng-data-deriv
1185 (rng-ipattern-get-child ipattern
)
1187 (rng-intern-optional ipattern
))))
1189 (let ((child (rng-ipattern-get-child ipattern
)))
1190 (if (or (rng-ipattern-get-nullable
1191 (rng-data-deriv child value
))
1192 (and (rng-ipattern-get-nullable child
)
1193 (rng-blank-p value
)))
1194 (rng-ipattern-get-after ipattern
)
1195 rng-not-allowed-ipattern
)))
1197 (if (rng-dt-make-value (rng-ipattern-get-datatype ipattern
)
1200 rng-not-allowed-ipattern
))
1201 ((eq type
'data-except
)
1202 (if (and (rng-dt-make-value (rng-ipattern-get-datatype ipattern
)
1204 (not (rng-ipattern-get-nullable
1206 (rng-ipattern-get-child ipattern
)
1209 rng-not-allowed-ipattern
))
1211 (if (equal (rng-dt-make-value (rng-ipattern-get-datatype ipattern
)
1213 (rng-ipattern-get-value-object ipattern
))
1215 rng-not-allowed-ipattern
))
1217 (let ((tokens (split-string value
))
1218 (state (rng-ipattern-get-child ipattern
)))
1220 (not (eq state rng-not-allowed-ipattern
)))
1221 (setq state
(rng-data-deriv state
(car tokens
)))
1222 (setq tokens
(cdr tokens
)))
1223 (if (rng-ipattern-get-nullable state
)
1225 rng-not-allowed-ipattern
)))
1226 ;; don't think interleave can occur
1227 ;; since we do text-only-deriv first
1228 (t rng-not-allowed-ipattern
))))
1230 (defun rng-transform-multi (f ipattern interner
)
1231 (let* ((members (rng-ipattern-get-child ipattern
))
1232 (transformed (mapcar f members
)))
1233 (if (rng-members-eq members transformed
)
1235 (funcall interner transformed
))))
1237 (defun rng-transform-choice (f ipattern
)
1238 (rng-transform-multi f ipattern
'rng-intern-choice
))
1240 (defun rng-transform-group (f ipattern
)
1241 (rng-transform-multi f ipattern
'rng-intern-group
))
1243 (defun rng-transform-interleave (f ipattern
)
1244 (rng-transform-multi f ipattern
'rng-intern-interleave
))
1246 (defun rng-transform-one-or-more (f ipattern
)
1247 (let* ((child (rng-ipattern-get-child ipattern
))
1248 (transformed (funcall f child
)))
1249 (if (eq child transformed
)
1251 (rng-intern-one-or-more transformed
))))
1253 (defun rng-transform-after-child (f ipattern
)
1254 (let* ((child (rng-ipattern-get-child ipattern
))
1255 (transformed (funcall f child
)))
1256 (if (eq child transformed
)
1258 (rng-intern-after transformed
1259 (rng-ipattern-get-after ipattern
)))))
1261 (defun rng-transform-interleave-single (f subster ipattern
)
1262 (let ((children (rng-ipattern-get-child ipattern
))
1264 (while (and children
(not found
))
1265 (let* ((child (car children
))
1266 (transformed (funcall f child
)))
1267 (if (eq transformed rng-not-allowed-ipattern
)
1268 (setq children
(cdr children
))
1273 (rng-ipattern-get-child ipattern
))))))
1275 rng-not-allowed-ipattern
)))
1277 (defun rng-transform-group-nullable (f conser ipattern
)
1278 "Given a group x1,...,xn,y1,...,yn where the xs are all
1279 nullable and y1 isn't, return a choice
1280 (conser f(x1) x2,...,xm,y1,...,yn)
1281 |(conser f(x2) x3,...,xm,y1,...,yn)
1283 |(conser f(xm) y1,...,yn)
1284 |(conser f(y1) y2,...,yn)"
1286 (rng-transform-group-nullable-gen-choices
1289 (rng-ipattern-get-child ipattern
))))
1291 (defun rng-transform-group-nullable-gen-choices (f conser members
)
1292 (let ((head (car members
))
1293 (tail (cdr members
)))
1295 (cons (funcall conser
(funcall f head
) tail
)
1296 (if (rng-ipattern-get-nullable head
)
1297 (rng-transform-group-nullable-gen-choices f conser tail
)
1299 (list (funcall f head
)))))
1301 (defun rng-members-eq (list1 list2
)
1304 (eq (car list1
) (car list2
)))
1305 (setq list1
(cdr list1
))
1306 (setq list2
(cdr list2
)))
1307 (and (null list1
) (null list2
)))
1310 (defun rng-ipattern-after (ipattern)
1311 (let ((type (rng-ipattern-get-type ipattern
)))
1312 (cond ((eq type
'choice
)
1313 (rng-transform-choice 'rng-ipattern-after ipattern
))
1315 (rng-ipattern-get-after ipattern
))
1316 ((eq type
'not-allowed
)
1318 (t (error "Internal error in rng-ipattern-after: unexpected type %s" type
)))))
1320 (defun rng-unknown-start-tag-open-deriv (ipattern)
1321 (rng-intern-after (rng-compile rng-any-content
) ipattern
))
1323 (defun rng-ipattern-optionalize-elements (ipattern)
1324 (let* ((type (rng-ipattern-get-type ipattern
))
1325 (transform (assq type rng-transform-map
)))
1327 (funcall (cdr transform
)
1328 'rng-ipattern-optionalize-elements
1331 (rng-intern-optional ipattern
))
1334 (defun rng-ipattern-empty-before-p (ipattern)
1335 (let ((type (rng-ipattern-get-type ipattern
)))
1336 (cond ((eq type
'after
)
1337 (eq (rng-ipattern-get-child ipattern
) rng-empty-ipattern
))
1339 (let ((members (rng-ipattern-get-child ipattern
))
1341 (while (and members ret
)
1342 (or (rng-ipattern-empty-before-p (car members
))
1344 (setq members
(cdr members
)))
1348 (defun rng-ipattern-possible-start-tags (ipattern accum
)
1349 (let ((type (rng-ipattern-get-type ipattern
)))
1350 (cond ((eq type
'after
)
1351 (rng-ipattern-possible-start-tags
1352 (rng-ipattern-get-child ipattern
)
1354 ((memq type
'(choice interleave
))
1355 (let ((members (rng-ipattern-get-child ipattern
)))
1358 (rng-ipattern-possible-start-tags (car members
)
1360 (setq members
(cdr members
))))
1363 (let ((members (rng-ipattern-get-child ipattern
)))
1366 (rng-ipattern-possible-start-tags (car members
)
1369 (and (rng-ipattern-get-nullable (car members
))
1373 (if (eq (rng-element-get-child ipattern
) rng-not-allowed-ipattern
)
1375 (rng-name-class-possible-names
1376 (rng-ipattern-get-name-class ipattern
)
1378 ((eq type
'one-or-more
)
1379 (rng-ipattern-possible-start-tags
1380 (rng-ipattern-get-child ipattern
)
1384 (defun rng-ipattern-start-tag-possible-p (ipattern)
1385 (let ((type (rng-ipattern-get-type ipattern
)))
1386 (cond ((memq type
'(after one-or-more
))
1387 (rng-ipattern-start-tag-possible-p
1388 (rng-ipattern-get-child ipattern
)))
1389 ((memq type
'(choice interleave
))
1390 (let ((members (rng-ipattern-get-child ipattern
))
1392 (while (and members
(not possible
))
1394 (rng-ipattern-start-tag-possible-p (car members
)))
1395 (setq members
(cdr members
)))
1398 (let ((members (rng-ipattern-get-child ipattern
))
1400 (while (and members
(not possible
))
1402 (rng-ipattern-start-tag-possible-p (car members
)))
1404 (and (rng-ipattern-get-nullable (car members
))
1408 (not (eq (rng-element-get-child ipattern
)
1409 rng-not-allowed-ipattern
)))
1412 (defun rng-ipattern-possible-attributes (ipattern accum
)
1413 (let ((type (rng-ipattern-get-type ipattern
)))
1414 (cond ((eq type
'after
)
1415 (rng-ipattern-possible-attributes (rng-ipattern-get-child ipattern
)
1417 ((memq type
'(choice interleave group
))
1418 (let ((members (rng-ipattern-get-child ipattern
)))
1421 (rng-ipattern-possible-attributes (car members
)
1423 (setq members
(cdr members
))))
1425 ((eq type
'attribute
)
1426 (rng-name-class-possible-names
1427 (rng-ipattern-get-name-class ipattern
)
1429 ((eq type
'one-or-more
)
1430 (rng-ipattern-possible-attributes
1431 (rng-ipattern-get-child ipattern
)
1435 (defun rng-ipattern-possible-values (ipattern accum
)
1436 (let ((type (rng-ipattern-get-type ipattern
)))
1437 (cond ((eq type
'after
)
1438 (rng-ipattern-possible-values (rng-ipattern-get-child ipattern
)
1441 (let ((members (rng-ipattern-get-child ipattern
)))
1444 (rng-ipattern-possible-values (car members
)
1446 (setq members
(cdr members
))))
1449 (let ((value-object (rng-ipattern-get-value-object ipattern
)))
1450 (if (stringp value-object
)
1451 (cons value-object accum
)
1455 (defun rng-ipattern-required-element (ipattern)
1456 (let ((type (rng-ipattern-get-type ipattern
)))
1457 (cond ((memq type
'(after one-or-more
))
1458 (rng-ipattern-required-element (rng-ipattern-get-child ipattern
)))
1460 (let* ((members (rng-ipattern-get-child ipattern
))
1461 (required (rng-ipattern-required-element (car members
))))
1462 (while (and required
1463 (setq members
(cdr members
)))
1464 (unless (equal required
1465 (rng-ipattern-required-element (car members
)))
1466 (setq required nil
)))
1469 (let ((members (rng-ipattern-get-child ipattern
))
1471 (while (and (not (setq required
1472 (rng-ipattern-required-element
1474 (rng-ipattern-get-nullable (car members
))
1475 (setq members
(cdr members
))))
1477 ((eq type
'interleave
)
1478 (let ((members (rng-ipattern-get-child ipattern
))
1481 (let ((tem (rng-ipattern-required-element (car members
))))
1483 (setq members
(cdr members
)))
1486 (setq members
(cdr members
)))
1487 ((equal required tem
)
1488 (setq members
(cdr members
)))
1491 (setq members nil
)))))
1494 (let ((nc (rng-ipattern-get-name-class ipattern
)))
1496 (not (eq (rng-element-get-child ipattern
)
1497 rng-not-allowed-ipattern
))
1500 (defun rng-ipattern-required-attributes (ipattern accum
)
1501 (let ((type (rng-ipattern-get-type ipattern
)))
1502 (cond ((eq type
'after
)
1503 (rng-ipattern-required-attributes (rng-ipattern-get-child ipattern
)
1505 ((memq type
'(interleave group
))
1506 (let ((members (rng-ipattern-get-child ipattern
)))
1509 (rng-ipattern-required-attributes (car members
)
1511 (setq members
(cdr members
))))
1514 (let ((members (rng-ipattern-get-child ipattern
))
1515 in-all in-this new-in-all
)
1517 (rng-ipattern-required-attributes (car members
)
1519 (while (and in-all
(setq members
(cdr members
)))
1521 (rng-ipattern-required-attributes (car members
) nil
))
1522 (setq new-in-all nil
)
1524 (when (member (car in-this
) in-all
)
1526 (cons (car in-this
) new-in-all
)))
1527 (setq in-this
(cdr in-this
)))
1528 (setq in-all new-in-all
))
1529 (append in-all accum
)))
1530 ((eq type
'attribute
)
1531 (let ((nc (rng-ipattern-get-name-class ipattern
)))
1535 ((eq type
'one-or-more
)
1536 (rng-ipattern-required-attributes (rng-ipattern-get-child ipattern
)
1540 (defun rng-compile-error (&rest args
)
1541 (signal 'rng-compile-error
1542 (list (apply 'format args
))))
1544 (put 'rng-compile-error
1546 '(error rng-error rng-compile-error
))
1548 (put 'rng-compile-error
1555 (defsubst rng-match-state
() rng-match-state
)
1557 (defsubst rng-set-match-state
(state)
1558 (setq rng-match-state state
))
1560 (defsubst rng-match-state-equal
(state)
1561 (eq state rng-match-state
))
1563 (defun rng-schema-changed ()
1564 (rng-ipattern-clear)
1565 (rng-compile-clear))
1567 (defun rng-match-init-buffer ()
1568 (make-local-variable 'rng-compile-table
)
1569 (make-local-variable 'rng-ipattern-table
)
1570 (make-local-variable 'rng-last-ipattern-index
))
1572 (defun rng-match-start-document ()
1573 (rng-ipattern-maybe-init)
1574 (rng-compile-maybe-init)
1575 (add-hook 'rng-schema-change-hook
'rng-schema-changed nil t
)
1576 (setq rng-match-state
(rng-compile rng-current-schema
)))
1578 (defun rng-match-start-tag-open (name)
1579 (rng-update-match-state (rng-start-tag-open-deriv rng-match-state
1582 (defun rng-match-attribute-name (name)
1583 (rng-update-match-state (rng-start-attribute-deriv rng-match-state
1586 (defun rng-match-attribute-value (value)
1587 (rng-update-match-state (rng-data-deriv rng-match-state
1590 (defun rng-match-element-value (value)
1591 (and (rng-update-match-state (rng-text-only-deriv rng-match-state
))
1592 (rng-update-match-state (rng-data-deriv rng-match-state
1595 (defun rng-match-start-tag-close ()
1596 (rng-update-match-state (rng-start-tag-close-deriv rng-match-state
)))
1598 (defun rng-match-mixed-text ()
1599 (rng-update-match-state (rng-mixed-text-deriv rng-match-state
)))
1601 (defun rng-match-end-tag ()
1602 (rng-update-match-state (rng-end-tag-deriv rng-match-state
)))
1604 (defun rng-match-after ()
1605 (rng-update-match-state
1606 (rng-ipattern-after rng-match-state
)))
1608 (defun rng-match-out-of-context-start-tag-open (name)
1609 (let* ((found (rng-map-element-attribute 'rng-find-element-content-pattern
1613 (content-pattern (if found
1614 (rng-intern-choice found
)
1615 rng-not-allowed-ipattern
)))
1616 (rng-update-match-state
1617 (rng-intern-after content-pattern rng-match-state
))))
1619 (defun rng-match-possible-namespace-uris ()
1620 "Return a list of all the namespace URIs used in the current schema.
1621 The absent URI is not included, so the result is always list of symbols."
1622 (rng-map-element-attribute (lambda (pattern accum
)
1623 (rng-find-name-class-uris (nth 1 pattern
)
1628 (defun rng-match-unknown-start-tag-open ()
1629 (rng-update-match-state
1630 (rng-unknown-start-tag-open-deriv rng-match-state
)))
1632 (defun rng-match-optionalize-elements ()
1633 (rng-update-match-state
1634 (rng-ipattern-optionalize-elements rng-match-state
)))
1636 (defun rng-match-ignore-attributes ()
1637 (rng-update-match-state
1638 (rng-ignore-attributes-deriv rng-match-state
)))
1640 (defun rng-match-text-typed-p ()
1641 (rng-ipattern-text-typed-p rng-match-state
))
1643 (defun rng-match-empty-content ()
1644 (if (rng-match-text-typed-p)
1645 (rng-match-element-value "")
1646 (rng-match-end-tag)))
1648 (defun rng-match-empty-before-p ()
1649 "Return non-nil if what can be matched before an end-tag is empty.
1650 In other words, return non-nil if the pattern for what can be matched
1651 for an end-tag is equivalent to empty."
1652 (rng-ipattern-empty-before-p rng-match-state
))
1654 (defun rng-match-infer-start-tag-namespace (local-name)
1655 (let ((ncs (rng-ipattern-possible-start-tags rng-match-state nil
))
1660 (if (and (equal (cdr nc
) local-name
)
1663 ;; first possible namespace
1665 (setq ncs
(cdr ncs
)))
1666 ((equal ns
(car nc
))
1667 ;; same as first namespace
1668 (setq ncs
(cdr ncs
)))
1670 ;; more than one possible namespace
1673 (setq ncs
(cdr ncs
))))
1676 (defun rng-match-nullable-p ()
1677 (rng-ipattern-get-nullable rng-match-state
))
1679 (defun rng-match-possible-start-tag-names ()
1680 "Return a list of possible names that would be valid for start-tags.
1682 Each possible name is returned as a (NAMESPACE . LOCAL-NAME) pair,
1683 where NAMESPACE is a symbol or nil (meaning the absent namespace) and
1684 LOCAL-NAME is a string. The returned list may contain duplicates."
1685 (rng-ipattern-possible-start-tags rng-match-state nil
))
1687 ;; This is no longer used. It might be useful so leave it in for now.
1688 (defun rng-match-start-tag-possible-p ()
1689 "Return non-nil if a start-tag is possible."
1690 (rng-ipattern-start-tag-possible-p rng-match-state
))
1692 (defun rng-match-possible-attribute-names ()
1693 "Return a list of possible names that would be valid for attributes.
1695 See the function `rng-match-possible-start-tag-names' for
1697 (rng-ipattern-possible-attributes rng-match-state nil
))
1699 (defun rng-match-possible-value-strings ()
1700 "Return a list of strings that would be valid as content.
1701 The list may contain duplicates. Typically, the list will not
1703 (rng-ipattern-possible-values rng-match-state nil
))
1705 (defun rng-match-required-element-name ()
1706 "Return the name of an element which must occur, or nil if none."
1707 (rng-ipattern-required-element rng-match-state
))
1709 (defun rng-match-required-attribute-names ()
1710 "Return a list of names of attributes which must all occur."
1711 (rng-ipattern-required-attributes rng-match-state nil
))
1713 (defmacro rng-match-save
(&rest body
)
1714 (let ((state (make-symbol "state")))
1715 `(let ((,state rng-match-state
))
1718 (setq rng-match-state
,state
)))))
1720 (put 'rng-match-save
'lisp-indent-function
0)
1721 (def-edebug-spec rng-match-save t
)
1723 (defmacro rng-match-with-schema
(schema &rest body
)
1724 `(let ((rng-current-schema ,schema
)
1728 rng-last-ipattern-index
)
1729 (rng-ipattern-maybe-init)
1730 (rng-compile-maybe-init)
1731 (setq rng-match-state
(rng-compile rng-current-schema
))
1734 (put 'rng-match-with-schema
'lisp-indent-function
1)
1735 (def-edebug-spec rng-match-with-schema t
)
1737 (provide 'rng-match
)
1739 ;; arch-tag: c8c50733-edcf-49fb-85e2-0aac8749b7f8
1740 ;;; rng-match.el ends here