1 ;;; rng-cmpct.el --- parsing of RELAX NG Compact Syntax schemas
3 ;; Copyright (C) 2003, 2007-2013 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 parses a RELAX NG Compact Syntax schema into the form
26 ;; specified in rng-pttrn.el.
28 ;; RELAX NG Compact Syntax is specified by
29 ;; http://relaxng.org/compact.html
31 ;; This file uses the prefix "rng-c-".
41 (defun rng-c-load-schema (filename)
42 "Load a schema in RELAX NG compact syntax from FILENAME.
44 (rng-c-parse-file filename
))
48 (put 'rng-c-incorrect-schema
50 '(error rng-error nxml-file-parse-error rng-c-incorrect-schema
))
52 (put 'rng-c-incorrect-schema
56 (defun rng-c-signal-incorrect-schema (filename pos message
)
57 (nxml-signal-file-parse-error filename
60 'rng-c-incorrect-schema
))
64 (defconst rng-c-keywords
84 "List of strings that are keywords in the compact syntax.")
86 (defconst rng-c-anchored-keyword-re
87 (concat "\\`\\(" (regexp-opt rng-c-keywords
) "\\)\\'")
88 "Regular expression to match a keyword in the compact syntax.")
90 (defvar rng-c-syntax-table nil
91 "Syntax table for parsing the compact syntax.")
93 (if rng-c-syntax-table
95 (setq rng-c-syntax-table
(make-syntax-table))
96 (modify-syntax-entry ?
# "<" rng-c-syntax-table
)
97 (modify-syntax-entry ?
\n ">" rng-c-syntax-table
)
98 (modify-syntax-entry ?-
"w" rng-c-syntax-table
)
99 (modify-syntax-entry ?.
"w" rng-c-syntax-table
)
100 (modify-syntax-entry ?_
"w" rng-c-syntax-table
)
101 (modify-syntax-entry ?
: "_" rng-c-syntax-table
))
103 (defconst rng-c-literal-1-re
104 "'\\(''\\([^']\\|'[^']\\|''[^']\\)*''\\|[^'\n]*\\)'"
105 "Regular expression to match a single-quoted literal.")
107 (defconst rng-c-literal-2-re
108 (replace-regexp-in-string "'" "\"" rng-c-literal-1-re
)
109 "Regular expression to match a double-quoted literal.")
111 (defconst rng-c-ncname-re
"\\w+")
113 (defconst rng-c-anchored-ncname-re
114 (concat "\\`" rng-c-ncname-re
"\\'"))
116 (defconst rng-c-token-re
117 (concat "[&|]=" "\\|"
118 "[][()|&,*+?{}~=-]" "\\|"
119 rng-c-literal-1-re
"\\|"
120 rng-c-literal-2-re
"\\|"
121 rng-c-ncname-re
"\\(:\\(\\*\\|" rng-c-ncname-re
"\\)\\)?" "\\|"
122 "\\\\" rng-c-ncname-re
"\\|"
124 "Regular expression to match a token in the compact syntax.")
126 (defun rng-c-init-buffer ()
127 (setq case-fold-search nil
) ; automatically becomes buffer-local when set
128 (set-buffer-multibyte t
)
129 (set-syntax-table rng-c-syntax-table
))
131 (defvar rng-c-current-token nil
)
132 (make-variable-buffer-local 'rng-c-current-token
)
134 (defun rng-c-advance ()
135 (cond ((looking-at rng-c-token-re
)
136 (setq rng-c-current-token
(match-string 0))
137 (goto-char (match-end 0))
138 (forward-comment (point-max)))
139 ((= (point) (point-max))
140 (setq rng-c-current-token
""))
141 (t (rng-c-error "Invalid token"))))
143 (defconst rng-c-anchored-datatype-name-re
144 (concat "\\`" rng-c-ncname-re
":" rng-c-ncname-re
"\\'"))
146 (defsubst rng-c-current-token-keyword-p
()
147 (string-match rng-c-anchored-keyword-re rng-c-current-token
))
149 (defsubst rng-c-current-token-prefixed-name-p
()
150 (string-match rng-c-anchored-datatype-name-re rng-c-current-token
))
152 (defsubst rng-c-current-token-literal-p
()
153 (string-match "\\`['\"]" rng-c-current-token
))
155 (defsubst rng-c-current-token-quoted-identifier-p
()
156 (string-match "\\`\\\\" rng-c-current-token
))
158 (defsubst rng-c-current-token-ncname-p
()
159 (string-match rng-c-anchored-ncname-re rng-c-current-token
))
161 (defsubst rng-c-current-token-ns-name-p
()
162 (let ((len (length rng-c-current-token
)))
164 (= (aref rng-c-current-token
(- len
1)) ?
*))))
168 (defvar rng-c-inherit-namespace nil
)
170 (defvar rng-c-default-namespace nil
)
172 (defvar rng-c-default-namespace-declared nil
)
174 (defvar rng-c-namespace-decls nil
175 "Alist of namespace declarations.")
177 (defconst rng-c-no-namespace nil
)
179 (defun rng-c-declare-standard-namespaces ()
180 (setq rng-c-namespace-decls
181 (cons (cons "xml" nxml-xml-namespace-uri
)
182 rng-c-namespace-decls
))
183 (when (and (not rng-c-default-namespace-declared
)
184 rng-c-inherit-namespace
)
185 (setq rng-c-default-namespace rng-c-inherit-namespace
)))
187 (defun rng-c-expand-name (prefixed-name)
188 (let ((i (string-match ":" prefixed-name
)))
189 (rng-make-name (rng-c-lookup-prefix (substring prefixed-name
192 (substring prefixed-name
(+ i
1)))))
194 (defun rng-c-lookup-prefix (prefix)
195 (let ((binding (assoc prefix rng-c-namespace-decls
)))
196 (or binding
(rng-c-error "Undefined prefix %s" prefix
))
199 (defun rng-c-unqualified-namespace (attribute)
202 rng-c-default-namespace
))
204 (defun rng-c-make-context ()
205 (cons rng-c-default-namespace rng-c-namespace-decls
))
209 (defconst rng-string-datatype
210 (rng-make-datatype rng-builtin-datatypes-uri
"string"))
212 (defconst rng-token-datatype
213 (rng-make-datatype rng-builtin-datatypes-uri
"token"))
215 (defvar rng-c-datatype-decls nil
216 "Alist of datatype declarations.
217 Contains a list of pairs (PREFIX . URI) where PREFIX is a string
218 and URI is a symbol.")
220 (defun rng-c-declare-standard-datatypes ()
221 (setq rng-c-datatype-decls
222 (cons (cons "xsd" rng-xsd-datatypes-uri
)
223 rng-c-datatype-decls
)))
225 (defun rng-c-lookup-datatype-prefix (prefix)
226 (let ((binding (assoc prefix rng-c-datatype-decls
)))
227 (or binding
(rng-c-error "Undefined prefix %s" prefix
))
230 (defun rng-c-expand-datatype (prefixed-name)
231 (let ((i (string-match ":" prefixed-name
)))
233 (rng-c-lookup-datatype-prefix (substring prefixed-name
0 i
))
234 (substring prefixed-name
(+ i
1)))))
238 (defvar rng-c-current-grammar nil
)
239 (defvar rng-c-parent-grammar nil
)
241 (defun rng-c-make-grammar ()
242 (make-hash-table :test
'equal
))
244 (defconst rng-c-about-override-slot
0)
245 (defconst rng-c-about-combine-slot
1)
247 (defun rng-c-lookup-create (name grammar
)
248 "Return a def object for NAME.
249 A def object is a pair \(ABOUT . REF) where REF is returned by
251 ABOUT is a two-element vector [OVERRIDE COMBINE].
252 COMBINE is either nil, choice or interleave.
253 OVERRIDE is either nil, require or t."
254 (let ((def (gethash name grammar
)))
258 (setq def
(cons (vector nil nil
) (rng-make-ref name
)))
259 (puthash name def grammar
)
262 (defun rng-c-make-ref (name)
263 (or rng-c-current-grammar
264 (rng-c-error "Reference not in a grammar"))
265 (cdr (rng-c-lookup-create name rng-c-current-grammar
)))
267 (defun rng-c-make-parent-ref (name)
268 (or rng-c-parent-grammar
269 (rng-c-error "Reference to non-existent parent grammar"))
270 (cdr (rng-c-lookup-create name rng-c-parent-grammar
)))
272 (defvar rng-c-overrides nil
273 "Contains a list of (NAME . DEF) pairs.")
275 (defun rng-c-merge-combine (def combine name
)
276 (let* ((about (car def
))
277 (current-combine (aref about rng-c-about-combine-slot
)))
280 (or (eq combine current-combine
)
281 (rng-c-error "Inconsistent combine for %s" name
))
282 (aset about rng-c-about-combine-slot combine
))
285 (defun rng-c-prepare-define (name combine in-include
)
286 (let* ((def (rng-c-lookup-create name rng-c-current-grammar
))
288 (overridden (aref about rng-c-about-override-slot
)))
290 (setq rng-c-overrides
(cons (cons name def
) rng-c-overrides
)))
291 (cond (overridden (and (eq overridden
'require
)
292 (aset about rng-c-about-override-slot t
))
294 (t (setq combine
(rng-c-merge-combine def combine name
))
295 (and (rng-ref-get (cdr def
))
297 (rng-c-error "Duplicate definition of %s" name
))
300 (defun rng-c-start-include (overrides)
301 (mapcar (lambda (name-def)
302 (let* ((def (cdr name-def
))
304 (save (aref about rng-c-about-override-slot
)))
305 (aset about rng-c-about-override-slot
'require
)
306 (cons save name-def
)))
309 (defun rng-c-end-include (overrides)
311 (let* ((saved (car o
))
313 (name (car name-def
))
316 (and (eq (aref about rng-c-about-override-slot
) 'require
)
317 (rng-c-error "Definition of %s in include did not override definition in included file" name
))
318 (aset about rng-c-about-override-slot saved
)))
321 (defun rng-c-define (def value
)
323 (let ((current-value (rng-ref-get (cdr def
))))
324 (rng-ref-set (cdr def
)
326 (if (eq (aref (car def
) rng-c-about-combine-slot
)
328 (rng-make-choice (list current-value value
))
329 (rng-make-interleave (list current-value value
)))
332 (defun rng-c-finish-grammar ()
333 (maphash (lambda (key def
)
334 (or (rng-ref-get (cdr def
))
335 (rng-c-error "Reference to undefined pattern %s" key
)))
336 rng-c-current-grammar
)
337 (rng-ref-get (cdr (or (gethash 'start rng-c-current-grammar
)
338 (rng-c-error "No definition of start")))))
342 (defvar rng-c-escape-positions nil
)
343 (make-variable-buffer-local 'rng-c-escape-positions
)
345 (defvar rng-c-file-name nil
)
346 (make-variable-buffer-local 'rng-c-file-name
)
348 (defvar rng-c-file-index nil
)
350 (defun rng-c-parse-file (filename &optional context
)
351 (with-current-buffer (get-buffer-create (rng-c-buffer-name context
))
354 (setq rng-c-file-name
355 (car (insert-file-contents filename
)))
356 (setq rng-c-escape-positions nil
)
357 (rng-c-process-escapes)
358 (rng-c-parse-top-level context
)))
360 (defun rng-c-buffer-name (context)
361 (concat " *RNC Input"
364 (number-to-string (setq rng-c-file-index
365 (1+ rng-c-file-index
)))
367 (setq rng-c-file-index
1)
370 (defun rng-c-process-escapes ()
371 ;; Check for any nuls, since we will use nul chars
372 ;; for internal purposes.
373 (let ((pos (search-forward "\C-@" nil t
)))
375 (rng-c-error "Nul character found (binary file?)")))
377 (while (re-search-forward "\\\\x+{\\([0-9a-fA-F]+\\)}"
380 (let* ((ch (decode-char 'ucs
(string-to-number (match-string 1) 16))))
381 (if (and ch
(> ch
0))
382 (let ((begin (match-beginning 0))
384 (delete-region begin end
)
385 ;; Represent an escaped newline by nul, so
386 ;; that we can distinguish it from a literal newline.
387 ;; We will translate it back into a real newline later.
388 (insert (if (eq ch ?
\n) 0 ch
))
389 (setq offset
(+ offset
(- end begin
1)))
390 (setq rng-c-escape-positions
391 (cons (cons (point) offset
)
392 rng-c-escape-positions
)))
393 (rng-c-error "Invalid character escape")))))
396 (defun rng-c-translate-position (pos)
397 (let ((tem rng-c-escape-positions
))
400 (setq tem
(cdr tem
)))
405 (defun rng-c-error (&rest args
)
406 (rng-c-signal-incorrect-schema rng-c-file-name
407 (rng-c-translate-position (point))
408 (apply 'format args
)))
410 (defun rng-c-parse-top-level (context)
411 (let ((rng-c-namespace-decls nil
)
412 (rng-c-default-namespace nil
)
413 (rng-c-datatype-decls nil
))
414 (goto-char (point-min))
415 (forward-comment (point-max))
418 (let ((p (if (eq context
'include
)
419 (if (rng-c-implicit-grammar-p)
420 (rng-c-parse-grammar-body "")
421 (rng-c-parse-included-grammar))
422 (if (rng-c-implicit-grammar-p)
423 (rng-c-parse-implicit-grammar)
424 (rng-c-parse-pattern)))))
425 (or (string-equal rng-c-current-token
"")
426 (rng-c-error "Unexpected characters after pattern"))
429 (defun rng-c-parse-included-grammar ()
430 (or (string-equal rng-c-current-token
"grammar")
431 (rng-c-error "Included schema is not a grammar"))
434 (rng-c-parse-grammar-body "}"))
436 (defun rng-c-implicit-grammar-p ()
437 (or (and (or (rng-c-current-token-prefixed-name-p)
438 (rng-c-current-token-quoted-identifier-p)
439 (and (rng-c-current-token-ncname-p)
440 (not (rng-c-current-token-keyword-p))))
442 (and (string-equal rng-c-current-token
"[")
443 (rng-c-parse-lead-annotation)
445 (member rng-c-current-token
'("div" "include" ""))
446 (looking-at "[|&]?=")))
448 (defun rng-c-parse-decls ()
449 (setq rng-c-default-namespace-declared nil
)
452 (assoc rng-c-current-token
453 '(("namespace" . rng-c-parse-namespace
)
454 ("datatypes" . rng-c-parse-datatypes
)
455 ("default" . rng-c-parse-default
)))))
459 (funcall (cdr binding
))
462 (rng-c-declare-standard-datatypes)
463 (rng-c-declare-standard-namespaces))
465 (defun rng-c-parse-datatypes ()
466 (let ((prefix (rng-c-parse-identifier-or-keyword)))
467 (or (not (assoc prefix rng-c-datatype-decls
))
468 (rng-c-error "Duplicate datatypes declaration for prefix %s" prefix
))
470 (setq rng-c-datatype-decls
472 (rng-make-datatypes-uri (rng-c-parse-literal)))
473 rng-c-datatype-decls
))))
475 (defun rng-c-parse-namespace ()
476 (rng-c-declare-namespace nil
477 (rng-c-parse-identifier-or-keyword)))
479 (defun rng-c-parse-default ()
480 (rng-c-expect "namespace")
481 (rng-c-declare-namespace t
482 (if (string-equal rng-c-current-token
"=")
484 (rng-c-parse-identifier-or-keyword))))
486 (defun rng-c-declare-namespace (declare-default prefix
)
488 (let ((ns (cond ((string-equal rng-c-current-token
"inherit")
490 rng-c-inherit-namespace
)
492 (nxml-make-namespace (rng-c-parse-literal))))))
494 (or (not (assoc prefix rng-c-namespace-decls
))
495 (rng-c-error "Duplicate namespace declaration for prefix %s"
497 (setq rng-c-namespace-decls
498 (cons (cons prefix ns
) rng-c-namespace-decls
)))
500 (or (not rng-c-default-namespace-declared
)
501 (rng-c-error "Duplicate default namespace declaration"))
502 (setq rng-c-default-namespace-declared t
)
503 (setq rng-c-default-namespace ns
))))
505 (defun rng-c-parse-implicit-grammar ()
506 (let* ((rng-c-parent-grammar rng-c-current-grammar
)
507 (rng-c-current-grammar (rng-c-make-grammar)))
508 (rng-c-parse-grammar-body "")
509 (rng-c-finish-grammar)))
511 (defun rng-c-parse-grammar-body (close-token &optional in-include
)
512 (while (not (string-equal rng-c-current-token close-token
))
513 (cond ((rng-c-current-token-keyword-p)
514 (let ((kw (intern rng-c-current-token
)))
515 (cond ((eq kw
'start
)
516 (rng-c-parse-define 'start in-include
))
519 (rng-c-parse-div in-include
))
522 (rng-c-error "Nested include"))
524 (rng-c-parse-include))
525 (t (rng-c-error "Invalid grammar keyword")))))
526 ((rng-c-current-token-ncname-p)
527 (if (looking-at "\\[")
528 (rng-c-parse-annotation-element)
529 (rng-c-parse-define rng-c-current-token
531 ((rng-c-current-token-quoted-identifier-p)
532 (if (looking-at "\\[")
533 (rng-c-parse-annotation-element)
534 (rng-c-parse-define (substring rng-c-current-token
1)
536 ((rng-c-current-token-prefixed-name-p)
537 (rng-c-parse-annotation-element))
538 ((string-equal rng-c-current-token
"[")
539 (rng-c-parse-lead-annotation)
540 (and (string-equal rng-c-current-token close-token
)
541 (rng-c-error "Missing annotation subject"))
542 (and (looking-at "\\[")
543 (rng-c-error "Leading annotation applied to annotation")))
544 (t (rng-c-error "Invalid grammar content"))))
545 (or (string-equal rng-c-current-token
"")
548 (defun rng-c-parse-div (in-include)
550 (rng-c-parse-grammar-body "}" in-include
))
552 (defun rng-c-parse-include ()
553 (let* ((filename (rng-c-expand-file (rng-c-parse-literal)))
554 (rng-c-inherit-namespace (rng-c-parse-opt-inherit))
556 (cond ((string-equal rng-c-current-token
"{")
558 (let ((rng-c-overrides nil
))
559 (rng-c-parse-grammar-body "}" t
)
560 (setq overrides rng-c-overrides
))
561 (setq overrides
(rng-c-start-include overrides
))
562 (rng-c-parse-file filename
'include
)
563 (rng-c-end-include overrides
))
564 (t (rng-c-parse-file filename
'include
)))))
566 (defun rng-c-parse-define (name in-include
)
568 (let ((assign (assoc rng-c-current-token
571 ("&=" . interleave
)))))
573 (rng-c-error "Expected assignment operator"))
575 (let ((ref (rng-c-prepare-define name
(cdr assign
) in-include
)))
576 (rng-c-define ref
(rng-c-parse-pattern)))))
578 (defvar rng-c-had-except nil
)
580 (defun rng-c-parse-pattern ()
581 (let* ((rng-c-had-except nil
)
582 (p (rng-c-parse-repeated))
583 (op (assoc rng-c-current-token
584 '(("|" . rng-make-choice
)
585 ("," . rng-make-group
)
586 ("&" . rng-make-interleave
)))))
589 (rng-c-error "Parentheses required around pattern using -")
590 (let* ((patterns (cons p nil
))
592 (connector rng-c-current-token
))
595 (let ((newcdr (cons (rng-c-parse-repeated) nil
)))
598 (string-equal rng-c-current-token connector
)))
599 (funcall (cdr op
) patterns
)))
602 (defun rng-c-parse-repeated ()
603 (let ((p (rng-c-parse-follow-annotations
604 (rng-c-parse-primary)))
605 (op (assoc rng-c-current-token
606 '(("*" . rng-make-zero-or-more
)
607 ("+" . rng-make-one-or-more
)
608 ("?" . rng-make-optional
)))))
611 (rng-c-error "Parentheses required around pattern using -")
612 (rng-c-parse-follow-annotations
615 (funcall (cdr op
) p
))))
618 (defun rng-c-parse-primary ()
619 "Parse a primary expression.
620 The current token must be the first token of the expression.
621 After parsing the current token should be the token following
622 the primary expression."
623 (cond ((rng-c-current-token-keyword-p)
624 (let ((parse-function (get (intern rng-c-current-token
)
627 (rng-c-error "Keyword %s does not introduce a pattern"
628 rng-c-current-token
))
630 (funcall parse-function
)))
631 ((rng-c-current-token-ncname-p)
632 (rng-c-advance-with (rng-c-make-ref rng-c-current-token
)))
633 ((string-equal rng-c-current-token
"(")
635 (let ((p (rng-c-parse-pattern)))
638 ((rng-c-current-token-prefixed-name-p)
639 (let ((name (rng-c-expand-datatype rng-c-current-token
)))
641 (rng-c-parse-data name
)))
642 ((rng-c-current-token-literal-p)
643 (rng-make-value rng-token-datatype
(rng-c-parse-literal) nil
))
644 ((rng-c-current-token-quoted-identifier-p)
646 (rng-c-make-ref (substring rng-c-current-token
1))))
647 ((string-equal rng-c-current-token
"[")
648 (rng-c-parse-lead-annotation)
649 (rng-c-parse-primary))
650 (t (rng-c-error "Invalid pattern"))))
652 (defun rng-c-parse-parent ()
653 (and (rng-c-current-token-keyword-p)
654 (rng-c-error "Keyword following parent was not quoted"
655 rng-c-current-token
))
656 (rng-c-make-parent-ref (rng-c-parse-identifier-or-keyword)))
658 (defun rng-c-parse-literal ()
659 (rng-c-fix-escaped-newlines
660 (apply 'concat
(rng-c-parse-literal-segments))))
662 (defun rng-c-parse-literal-segments ()
663 (let ((str (rng-c-parse-literal-segment)))
665 (cond ((string-equal rng-c-current-token
"~")
667 (rng-c-parse-literal-segments))
670 (defun rng-c-parse-literal-segment ()
671 (or (rng-c-current-token-literal-p)
672 (rng-c-error "Expected a literal"))
674 (let ((n (if (and (>= (length rng-c-current-token
) 6)
675 (eq (aref rng-c-current-token
0)
676 (aref rng-c-current-token
1)))
679 (substring rng-c-current-token n
(- n
)))))
681 (defun rng-c-fix-escaped-newlines (str)
684 (let ((n (string-match "\C-@" str pos
)))
687 (setq pos
(1+ n
)))))))
690 (defun rng-c-parse-identifier-or-keyword ()
691 (cond ((rng-c-current-token-ncname-p)
692 (rng-c-advance-with rng-c-current-token
))
693 ((rng-c-current-token-quoted-identifier-p)
694 (rng-c-advance-with (substring rng-c-current-token
1)))
695 (t (rng-c-error "Expected identifier or keyword"))))
697 (put 'string
'rng-c-pattern
'rng-c-parse-string
)
698 (put 'token
'rng-c-pattern
'rng-c-parse-token
)
699 (put 'element
'rng-c-pattern
'rng-c-parse-element
)
700 (put 'attribute
'rng-c-pattern
'rng-c-parse-attribute
)
701 (put 'list
'rng-c-pattern
'rng-c-parse-list
)
702 (put 'mixed
'rng-c-pattern
'rng-c-parse-mixed
)
703 (put 'text
'rng-c-pattern
'rng-c-parse-text
)
704 (put 'empty
'rng-c-pattern
'rng-c-parse-empty
)
705 (put 'notAllowed
'rng-c-pattern
'rng-c-parse-not-allowed
)
706 (put 'grammar
'rng-c-pattern
'rng-c-parse-grammar
)
707 (put 'parent
'rng-c-pattern
'rng-c-parse-parent
)
708 (put 'external
'rng-c-pattern
'rng-c-parse-external
)
710 (defun rng-c-parse-element ()
711 (let ((name-class (rng-c-parse-name-class nil
)))
713 (let ((pattern (rng-c-parse-pattern)))
715 (rng-make-element name-class pattern
))))
717 (defun rng-c-parse-attribute ()
718 (let ((name-class (rng-c-parse-name-class 'attribute
)))
720 (let ((pattern (rng-c-parse-pattern)))
722 (rng-make-attribute name-class pattern
))))
724 (defun rng-c-parse-name-class (attribute)
725 (let* ((rng-c-had-except nil
)
727 (rng-c-parse-follow-annotations
728 (rng-c-parse-primary-name-class attribute
))))
729 (if (string-equal rng-c-current-token
"|")
730 (let* ((name-classes (cons name-class nil
))
732 (or (not rng-c-had-except
)
733 (rng-c-error "Parentheses required around name-class using - operator"))
737 (cons (rng-c-parse-follow-annotations
738 (rng-c-parse-primary-name-class attribute
))
742 (string-equal rng-c-current-token
"|")))
743 (rng-make-choice-name-class name-classes
))
746 (defun rng-c-parse-primary-name-class (attribute)
747 (cond ((rng-c-current-token-ncname-p)
749 (rng-make-name-name-class
750 (rng-make-name (rng-c-unqualified-namespace attribute
)
751 rng-c-current-token
))))
752 ((rng-c-current-token-prefixed-name-p)
754 (rng-make-name-name-class
755 (rng-c-expand-name rng-c-current-token
))))
756 ((string-equal rng-c-current-token
"*")
757 (let ((except (rng-c-parse-opt-except-name-class attribute
)))
759 (rng-make-any-name-except-name-class except
)
760 (rng-make-any-name-name-class))))
761 ((rng-c-current-token-ns-name-p)
763 (rng-c-lookup-prefix (substring rng-c-current-token
766 (except (rng-c-parse-opt-except-name-class attribute
)))
768 (rng-make-ns-name-except-name-class ns except
)
769 (rng-make-ns-name-name-class ns
))))
770 ((string-equal rng-c-current-token
"(")
772 (let ((name-class (rng-c-parse-name-class attribute
)))
775 ((rng-c-current-token-quoted-identifier-p)
777 (rng-make-name-name-class
778 (rng-make-name (rng-c-unqualified-namespace attribute
)
779 (substring rng-c-current-token
1)))))
780 ((string-equal rng-c-current-token
"[")
781 (rng-c-parse-lead-annotation)
782 (rng-c-parse-primary-name-class attribute
))
783 (t (rng-c-error "Bad name class"))))
785 (defun rng-c-parse-opt-except-name-class (attribute)
787 (and (string-equal rng-c-current-token
"-")
788 (or (not rng-c-had-except
)
789 (rng-c-error "Parentheses required around name-class using - operator"))
790 (setq rng-c-had-except t
)
793 (rng-c-parse-primary-name-class attribute
))))
795 (defun rng-c-parse-mixed ()
797 (let ((pattern (rng-make-mixed (rng-c-parse-pattern))))
801 (defun rng-c-parse-list ()
803 (let ((pattern (rng-make-list (rng-c-parse-pattern))))
807 (defun rng-c-parse-text ()
810 (defun rng-c-parse-empty ()
813 (defun rng-c-parse-not-allowed ()
814 (rng-make-not-allowed))
816 (defun rng-c-parse-string ()
817 (rng-c-parse-data rng-string-datatype
))
819 (defun rng-c-parse-token ()
820 (rng-c-parse-data rng-token-datatype
))
822 (defun rng-c-parse-data (name)
823 (if (rng-c-current-token-literal-p)
825 (rng-c-parse-literal)
827 (rng-c-make-context)))
828 (let ((params (rng-c-parse-optional-params)))
829 (if (string-equal rng-c-current-token
"-")
832 (rng-c-error "Parentheses required around pattern using -")
833 (setq rng-c-had-except t
))
835 (rng-make-data-except name
837 (rng-c-parse-primary)))
838 (rng-make-data name params
)))))
840 (defun rng-c-parse-optional-params ()
841 (and (string-equal rng-c-current-token
"{")
842 (let* ((head (cons nil nil
))
845 (while (not (string-equal rng-c-current-token
"}"))
846 (and (string-equal rng-c-current-token
"[")
847 (rng-c-parse-lead-annotation))
848 (let ((name (rng-c-parse-identifier-or-keyword)))
850 (let ((newcdr (cons (cons (intern name
)
851 (rng-c-parse-literal))
854 (setq tail newcdr
))))
858 (defun rng-c-parse-external ()
859 (let* ((filename (rng-c-expand-file (rng-c-parse-literal)))
860 (rng-c-inherit-namespace (rng-c-parse-opt-inherit)))
861 (rng-c-parse-file filename
'external
)))
863 (defun rng-c-expand-file (uri)
865 (rng-uri-file-name (rng-uri-resolve uri
866 (rng-file-name-uri rng-c-file-name
)))
868 (rng-c-error (cadr err
)))))
870 (defun rng-c-parse-opt-inherit ()
871 (cond ((string-equal rng-c-current-token
"inherit")
874 (rng-c-lookup-prefix (rng-c-parse-identifier-or-keyword)))
875 (t rng-c-default-namespace
)))
877 (defun rng-c-parse-grammar ()
879 (let* ((rng-c-parent-grammar rng-c-current-grammar
)
880 (rng-c-current-grammar (rng-c-make-grammar)))
881 (rng-c-parse-grammar-body "}")
882 (rng-c-finish-grammar)))
884 (defun rng-c-parse-lead-annotation ()
885 (rng-c-parse-annotation-body)
886 (and (string-equal rng-c-current-token
"[")
887 (rng-c-error "Multiple leading annotations")))
889 (defun rng-c-parse-follow-annotations (obj)
890 (while (string-equal rng-c-current-token
">>")
892 (if (rng-c-current-token-prefixed-name-p)
894 (rng-c-parse-identifier-or-keyword))
895 (rng-c-parse-annotation-body t
))
898 (defun rng-c-parse-annotation-element ()
900 (rng-c-parse-annotation-body t
))
902 ;; XXX need stricter checking of attribute names
903 ;; XXX don't allow attributes after text
905 (defun rng-c-parse-annotation-body (&optional allow-text
)
906 "Current token is [. Parse up to matching ].
907 Current token after parse is token following ]."
908 (or (string-equal rng-c-current-token
"[")
909 (rng-c-error "Expected ["))
911 (while (not (string-equal rng-c-current-token
"]"))
912 (cond ((rng-c-current-token-literal-p)
914 (rng-c-error "Out of place text within annotation"))
915 (rng-c-parse-literal))
917 (if (rng-c-current-token-prefixed-name-p)
919 (rng-c-parse-identifier-or-keyword))
920 (cond ((string-equal rng-c-current-token
"[")
921 (rng-c-parse-annotation-body t
))
922 ((string-equal rng-c-current-token
"=")
924 (rng-c-parse-literal))
925 (t (rng-c-error "Expected = or ["))))))
928 (defun rng-c-advance-with (pattern)
932 (defun rng-c-expect (str)
933 (or (string-equal rng-c-current-token str
)
934 (rng-c-error "Expected `%s' but got `%s'" str rng-c-current-token
))