1 ;;; xmltok.el --- XML tokenization
3 ;; Copyright (C) 2003, 2007, 2008 Free Software Foundation, Inc.
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 implements an XML 1.0 parser. It also implements the XML
26 ;; Namespaces Recommendation. It is designed to be conforming, but it
27 ;; works a bit differently from a normal XML parser. An XML document
28 ;; consists of the prolog and an instance. The prolog is parsed as a
29 ;; single unit using `xmltok-forward-prolog'. The instance is
30 ;; considered as a sequence of tokens, where a token is something like
31 ;; a start-tag, a comment, a chunk of data or a CDATA section. The
32 ;; tokenization of the instance is stateless: the tokenization of one
33 ;; part of the instance does not depend on tokenization of the
34 ;; preceding part of the instance. This allows the instance to be
35 ;; parsed incrementally. The main entry point is `xmltok-forward':
36 ;; this can be called at any point in the instance provided it is
37 ;; between tokens. The other entry point is `xmltok-forward-special'
38 ;; which skips over tokens other comments, processing instructions or
39 ;; CDATA sections (i.e. the constructs in an instance that can contain
40 ;; less than signs that don't start a token).
42 ;; This is a non-validating XML 1.0 processor. It does not resolve
43 ;; parameter entities (including the external DTD subset) and it does
44 ;; not resolve external general entities.
46 ;; It is non-conformant by design in the following respects.
48 ;; 1. It expects the client to detect aspects of well-formedness that
49 ;; are not internal to a single token, specifically checking that
50 ;; end-tags match start-tags and that the instance contains exactly
53 ;; 2. It expects the client to detect duplicate attributes. Detection
54 ;; of duplicate attributes after expansion of namespace prefixes
55 ;; requires the namespace processing state. Detection of duplicate
56 ;; attributes before expansion of namespace prefixes does not, but is
57 ;; redundant given that the client will do detection of duplicate
58 ;; attributes after expansion of namespace prefixes.
60 ;; 3. It allows the client to recover from well-formedness errors.
61 ;; This is essential for use in applications where the document is
62 ;; being parsed during the editing process.
64 ;; 4. It does not support documents that do not conform to the lexical
65 ;; requirements of the XML Namespaces Recommendation (e.g. a document
66 ;; with a colon in an entity name).
68 ;; There are also a number of things that have not yet been
69 ;; implemented that make it non-conformant.
71 ;; 1. It does not implement default attributes. ATTLIST declarations
72 ;; are parsed, but no checking is done on the content of attribute
73 ;; value literals specifying default attribute values, and default
74 ;; attribute values are not reported to the client.
76 ;; 2. It does not implement internal entities containing elements. If
77 ;; an internal entity is referenced and parsing its replacement text
78 ;; yields one or more tags, then it will skip the reference and
79 ;; report this to the client.
81 ;; 3. It does not check the syntax of public identifiers in the DTD.
83 ;; 4. It allows some non-ASCII characters in certain situations where
84 ;; it should not. For example, it only enforces XML 1.0's
85 ;; restrictions on name characters strictly for ASCII characters. The
86 ;; problem here is XML's character model is based squarely on Unicode,
87 ;; whereas Emacs's is not (as of version 21). It is not clear what
88 ;; the right thing to do is.
92 (defvar xmltok-type nil)
93 (defvar xmltok-start nil)
94 (defvar xmltok-name-colon nil)
95 (defvar xmltok-name-end nil)
96 (defvar xmltok-replacement nil
97 "String containing replacement for a character or entity reference.")
99 (defvar xmltok-attributes nil
100 "List containing attributes of last scanned element.
101 Each member of the list is a vector representing an attribute, which
102 can be accessed using the functions `xmltok-attribute-name-start',
103 `xmltok-attribute-name-colon', `xmltok-attribute-name-end',
104 `xmltok-attribute-value-start', `xmltok-attribute-value-end',
105 `xmltok-attribute-raw-normalized-value', `xmltok-attribute-refs'.")
107 (defvar xmltok-namespace-attributes nil
108 "List containing namespace declarations of last scanned element.
109 List has same format as `xmltok-attributes'.")
111 (defvar xmltok-dtd nil
112 "Information about the DTD used by `xmltok-forward'.
113 `xmltok-forward-prolog' sets this up.
115 It consists of an alist of general entity names vs definitions. The
116 first member of the alist is t if references to entities not in the
117 alist are well-formed \(e.g. because there's an external subset that
120 Each general entity name is a string. The definition is either nil,
121 a symbol, a string, a cons cell. If the definition is nil, then it
122 means that it's an internal entity but the result of parsing it is
123 unknown. If it is a symbol, then the symbol is either `unparsed',
124 meaning the entity is an unparsed entity, `external', meaning the
125 entity is or references an external entity, `element', meaning the
126 entity includes one or more elements, or `not-well-formed', meaning
127 the replacement text is not well-formed. If the definition is a
128 string, then the replacement text of the entity is that string; this
129 happens only during the parsing of the prolog. If the definition is
130 a cons cell \(ER . AR), then ER specifies the string that results
131 from referencing the entity in element content and AR is either nil,
132 meaning the replacement text included a <, or a string which is the
133 normalized attribute value.")
135 (defvar xmltok-dependent-regions nil
136 "List of descriptors of regions that a parsed token depends on.
138 A token depends on a region if the region occurs after the token and a
139 change in the region may require the token to be reparsed. This only
140 happens with markup that is not well-formed. For example, if a <?
141 occurs without a matching ?>, then the <? is returned as a
142 not-well-formed token. However, this token is dependent on region
143 from the end of the token to the end of the buffer: if this ever
144 contains ?> then the buffer must be reparsed from the <?.
146 A region descriptor is a list (FUN START END ARG ...), where FUN is a
147 function to be called when the region changes, START and END are
148 integers giving the start and end of the region, and ARG... are
149 additional arguments to be passed to FUN. FUN will be called with 5
150 arguments followed by the additional arguments if any: the position of
151 the start of the changed area in the region, the position of the end
152 of the changed area in the region, the length of the changed area
153 before the change, the position of the start of the region, the
154 position of the end of the region. FUN must return non-nil if the
155 region needs reparsing. FUN will be called in a `save-excursion'
156 with match-data saved.
158 `xmltok-forward', `xmltok-forward-special' and `xmltok-forward-prolog'
159 may add entries to the beginning of this list, but will not clear it.
160 `xmltok-forward' and `xmltok-forward-special' will only add entries
161 when returning tokens of type not-well-formed.")
163 (defvar xmltok-errors nil
164 "List of errors detected by `xmltok-forward' and `xmltok-forward-prolog'.
165 When `xmltok-forward' and `xmltok-forward-prolog' detect a
166 well-formedness error, they will add an entry to the beginning of this
167 list. Each entry is a vector [MESSAGE START END], where MESSAGE is a
168 string giving the error message and START and END are integers
169 indicating the position of the error.")
171 (defmacro xmltok-save (&rest body)
178 xmltok-namespace-attributes
179 xmltok-dependent-regions
183 (put 'xmltok-save 'lisp-indent-function 0)
184 (def-edebug-spec xmltok-save t)
186 (defsubst xmltok-attribute-name-start (att)
189 (defsubst xmltok-attribute-name-colon (att)
192 (defsubst xmltok-attribute-name-end (att)
195 (defsubst xmltok-attribute-value-start (att)
198 (defsubst xmltok-attribute-value-end (att)
201 (defsubst xmltok-attribute-raw-normalized-value (att)
202 "Return an object representing the normalized value of ATT.
203 This can be t indicating that the normalized value is the same as
204 the buffer substring from the start to the end of the value, or nil
205 indicating that the value is not well-formed or a string."
208 (defsubst xmltok-attribute-refs (att)
209 "Return a list of the entity and character references in ATT.
210 Each member is a vector [TYPE START END] where TYPE is either char-ref
211 or entity-ref and START and END are integers giving the start and end of
212 the reference. Nested entity references are not included in the list."
215 (defun xmltok-attribute-prefix (att)
216 (let ((colon (xmltok-attribute-name-colon att)))
218 (buffer-substring-no-properties (xmltok-attribute-name-start att)
221 (defun xmltok-attribute-local-name (att)
222 (let ((colon (xmltok-attribute-name-colon att)))
223 (buffer-substring-no-properties (if colon
225 (xmltok-attribute-name-start att))
226 (xmltok-attribute-name-end att))))
228 (defun xmltok-attribute-value (att)
229 (let ((rnv (xmltok-attribute-raw-normalized-value att)))
233 (buffer-substring-no-properties (xmltok-attribute-value-start att)
234 (xmltok-attribute-value-end att))))))
236 (defun xmltok-start-tag-prefix ()
237 (and xmltok-name-colon
238 (buffer-substring-no-properties (1+ xmltok-start)
241 (defun xmltok-start-tag-local-name ()
242 (buffer-substring-no-properties (1+ (or xmltok-name-colon
246 (defun xmltok-end-tag-prefix ()
247 (and xmltok-name-colon
248 (buffer-substring-no-properties (+ 2 xmltok-start)
251 (defun xmltok-end-tag-local-name ()
252 (buffer-substring-no-properties (if xmltok-name-colon
253 (1+ xmltok-name-colon)
257 (defun xmltok-start-tag-qname ()
258 (buffer-substring-no-properties (+ xmltok-start 1) xmltok-name-end))
260 (defun xmltok-end-tag-qname ()
261 (buffer-substring-no-properties (+ xmltok-start 2) xmltok-name-end))
263 (defsubst xmltok-make-attribute (name-begin
269 raw-normalized-value)
271 RAW-NORMALIZED-VALUE is nil if the value is not well-formed,
272 t if the normalized value is the string between VALUE-BEGIN
273 and VALUE-END, otherwise a STRING giving the value."
282 (defsubst xmltok-error-message (err)
285 (defsubst xmltok-error-start (err)
288 (defsubst xmltok-error-end (err)
291 (defsubst xmltok-make-error (message start end)
292 (vector message start end))
294 (defun xmltok-add-error (message &optional start end)
296 (cons (xmltok-make-error message
297 (or start xmltok-start)
301 (defun xmltok-add-dependent (fun &optional start end &rest args)
302 (setq xmltok-dependent-regions
304 (cons (or start xmltok-start)
305 (cons (or end (point-max))
307 xmltok-dependent-regions)))
309 (defun xmltok-forward ()
310 (setq xmltok-start (point))
311 (let* ((case-fold-search nil)
312 (space-count (skip-chars-forward " \t\r\n"))
315 (cond ((> space-count 0)
316 (setq xmltok-type 'space))
318 (goto-char (1+ (point)))
319 (xmltok-scan-after-lt))))
321 (cond ((> space-count 0)
322 (setq xmltok-type 'space))
324 (goto-char (1+ (point)))
325 (xmltok-scan-after-amp
327 (xmltok-handle-entity start end))))))
328 ((re-search-forward "[<&]\\|\\(]]>\\)" nil t)
329 (cond ((not (match-beginning 1))
330 (goto-char (match-beginning 0))
331 ;; must have got a non-space char
332 (setq xmltok-type 'data))
333 ((= (match-beginning 1) xmltok-start)
334 (xmltok-add-error "Found `]]>' not closing a CDATA section")
335 (setq xmltok-type 'not-well-formed))
337 (goto-char (match-beginning 0))
339 (if (= (point) (+ xmltok-start space-count))
344 (if (> space-count 0)
348 (goto-char (point-max))
349 (setq xmltok-type 'data)))))
351 (defun xmltok-forward-special (bound)
352 "Scan forward past the first special token starting at or after point.
353 Return nil if there is no special token that starts before BOUND.
354 CDATA sections, processing instructions and comments (and indeed
355 anything starting with < following by ? or !) count as special.
356 Return the type of the token."
357 (when (re-search-forward "<[?!]" (1+ bound) t)
358 (setq xmltok-start (match-beginning 0))
359 (goto-char (1+ xmltok-start))
360 (let ((case-fold-search nil))
361 (xmltok-scan-after-lt))))
365 ;; A symbolic regexp is represented by a list whose CAR is the string
366 ;; containing the regexp and whose cdr is a list of symbolic names
367 ;; for the groups in the string.
369 ;; Construct a symbolic regexp from a regexp.
370 (defun xmltok-r (str)
373 ;; Concatenate zero of more regexps and symbolic regexps.
374 (defun xmltok+ (&rest args)
377 (let ((arg (car args)))
379 (setq strs (cons arg strs))
380 (setq strs (cons (car arg) strs))
381 (setq names (cons (cdr arg) names)))
382 (setq args (cdr args))))
383 (cons (apply 'concat (nreverse strs))
384 (apply 'append (nreverse names))))))
387 ;; Make a symbolic group named NAME from the regexp R.
388 ;; R may be a symbolic regexp or an ordinary regexp.
389 (defmacro xmltok-g (name &rest r)
390 (let ((sym (make-symbol "r")))
391 `(let ((,sym (xmltok+ ,@r)))
393 (cons (concat "\\(" ,sym "\\)") (cons ',name nil))
394 (cons (concat "\\(" (car ,sym) "\\)") (cons ',name (cdr ,sym)))))))
396 (defun xmltok-p (&rest r) (xmltok+ "\\(?:"
400 ;; Get the group index of ELEM in a LIST of symbols.
401 (defun xmltok-get-index (elem list)
403 (error "Missing group name"))
407 (cond ((eq elem (car list))
412 (setq list (cdr list)))))
414 (error "Bad group name %s" elem))))
416 ;; Define a macro SYM using a symbolic regexp R.
417 ;; SYM can be called in three ways:
419 ;; expands to the regexp in R
422 ;; (match-beginning N)
423 ;; where N is the group index of G in R.
427 ;; where N is the group index of G in R.
428 (defmacro xmltok-defregexp (sym r)
431 `(macro lambda (action &optional group-name)
432 (cond ((eq action 'regexp)
434 ((or (eq action 'start) (eq action 'beginning))
435 (list 'match-beginning (xmltok-get-index group-name
438 (list 'match-end (xmltok-get-index group-name
442 (xmltok-get-index group-name ',(cdr r))))
443 ((eq action 'string-no-properties)
444 (list 'match-string-no-properties
445 (xmltok-get-index group-name ',(cdr r))))
446 (t (error "Invalid action: %s" action))))))))
454 (name-start-char "[_[:alpha:]]")
455 (name-continue-not-start-char "[-.[:digit:]]")
456 (name-continue-char "[-._[:alnum:]]")
464 (ncname (concat name-start-char name-continue-char *))
466 (xmltok+ (xmltok-g entity-name ncname)
467 (xmltok-g entity-ref-close ";") opt))
469 (xmltok+ (xmltok-g decimal "[0-9]" +)
470 (xmltok-g decimal-ref-close ";") opt))
473 (xmltok-g hex "[0-9a-fA-F]" +)
474 (xmltok-g hex-ref-close ";") opt
477 (xmltok+ (xmltok-g number-sign "#")
478 open decimal-ref or hex-ref close opt))
480 (xmltok+ open (xmltok-g start-tag-close s* ">")
481 or open (xmltok-g empty-tag-slash s* "/")
482 (xmltok-g empty-tag-close ">") opt close
483 or (xmltok-g start-tag-s s+)
486 (xmltok+ (xmltok-g start-tag-name
487 ncname (xmltok-g start-tag-colon ":" ncname) opt)
488 start-tag-close opt))
490 (xmltok+ (xmltok-g end-tag-slash "/")
491 open (xmltok-g end-tag-name
493 (xmltok-g end-tag-colon ":" ncname) opt)
494 (xmltok-g end-tag-close s* ">") opt
497 (xmltok+ (xmltok-g markup-declaration "!")
498 (xmltok-g comment-first-dash "-"
499 (xmltok-g comment-open "-") opt) opt))
502 (xmltok-g marked-section-open "\\[")
508 (xmltok-g cdata-section-open "\\[" ) opt
514 (processing-instruction
515 (xmltok-g processing-instruction-question question)))
517 (xmltok-defregexp xmltok-ncname (xmltok+ open ncname close))
519 (xmltok-defregexp xmltok-after-amp
520 (xmltok+ entity-ref or char-ref))
521 (xmltok-defregexp xmltok-after-lt
524 ;; cdata-section must come before comment
525 ;; because we treat <! as a comment
526 ;; and Emacs doesn't do fully greedy matching
530 or processing-instruction))
536 (xmltok-g complex1 "[&\r\n\t][^<']*") opt
538 (lit2 (cons (replace-regexp-in-string "'" "\"" (car lit1))
540 (literal (xmltok-g literal lit1 or lit2))
541 (name (xmltok+ open (xmltok-g xmlns "xmlns") or ncname close
542 (xmltok-g colon ":" ncname) opt)))
543 (xmltok+ (xmltok-g name name)
545 ;; If the literal isn't followed by what it should be,
546 ;; then the closing delimiter is probably really the
547 ;; opening delimiter of another literal, so don't
548 ;; absorb the literal in this case.
549 open s* literal start-tag-close close opt)))
551 xmltok-xml-declaration
552 (let* ((literal-content "[-._:a-zA-Z0-9]+")
554 (concat open "\"" literal-content "\""
555 or "'" literal-content "'" close))
558 s+ (xmltok-g version-name "version")
560 s* (xmltok-g version-value literal)
564 s+ (xmltok-g encoding-name "encoding")
566 s* (xmltok-g encoding-value literal)
569 (concat open "yes" or "no" close))
572 s+ (xmltok-g standalone-name "standalone")
574 s* (xmltok-g standalone-value
575 "\"" yes-no "\"" or "'" yes-no "'")
577 (xmltok+ "<" question "xml"
584 (let* ((single-char (xmltok-g single-char "[[|,(\"'>]"))
585 (internal-subset-close (xmltok-g internal-subset-close
587 (starts-with-close-paren
588 (xmltok-g close-paren
591 (xmltok-g close-paren-occur "[+?]")
593 (xmltok-g close-paren-star "\\*"))
597 "%" (xmltok-g param-entity-ref
599 (xmltok-g param-entity-ref-close
601 (starts-with-nmtoken-not-name
603 (xmltok-p name-continue-not-start-char or ":")
604 (xmltok-p name-continue-char or ":") *))
607 (xmltok-p name-continue-not-start-char or ":")
608 (xmltok-p name-continue-char or ":") *
613 (xmltok-p name-continue-char or ":") *))
615 (xmltok+ (xmltok-g ncname-nmtoken
616 ":" (xmltok-p nmtoken-after-colon))
617 or (xmltok-p (xmltok-g colon ":" ncname)
618 (xmltok-g colon-name-occur "[?+*]") opt)
619 or (xmltok-g ncname-occur "[?+*]")
620 or (xmltok-g ncname-colon ":")))
622 (xmltok-g name ncname (xmltok-p after-ncname) opt))
625 "#" (xmltok-g hash-name ncname)))
627 (xmltok-g markup-declaration
628 "!" (xmltok-p (xmltok-g comment-first-dash "-"
629 (xmltok-g comment-open "-") opt)
630 or (xmltok-g named-markup-declaration
633 (xmltok+ markup-declaration
634 or (xmltok-g processing-instruction-question
636 or (xmltok-g instance-start
638 (starts-with-lt (xmltok-g less-than "<" (xmltok-p after-lt) opt)))
639 (xmltok+ starts-with-lt
641 or starts-with-close-paren
642 or starts-with-percent
644 or starts-with-nmtoken-not-name
646 or internal-subset-close)))))
648 (defconst xmltok-ncname-regexp (xmltok-ncname regexp))
650 (defun xmltok-scan-after-lt ()
651 (cond ((not (looking-at (xmltok-after-lt regexp)))
652 (xmltok-add-error "`<' that is not markup must be entered as `<'")
653 (setq xmltok-type 'not-well-formed))
655 (goto-char (match-end 0))
656 (cond ((xmltok-after-lt start start-tag-close)
657 (setq xmltok-name-end
658 (xmltok-after-lt end start-tag-name))
659 (setq xmltok-name-colon
660 (xmltok-after-lt start start-tag-colon))
661 (setq xmltok-attributes nil)
662 (setq xmltok-namespace-attributes nil)
663 (setq xmltok-type 'start-tag))
664 ((xmltok-after-lt start end-tag-close)
665 (setq xmltok-name-end
666 (xmltok-after-lt end end-tag-name))
667 (setq xmltok-name-colon
668 (xmltok-after-lt start end-tag-colon))
669 (setq xmltok-type 'end-tag))
670 ((xmltok-after-lt start start-tag-s)
671 (setq xmltok-name-end
672 (xmltok-after-lt end start-tag-name))
673 (setq xmltok-name-colon
674 (xmltok-after-lt start start-tag-colon))
675 (setq xmltok-namespace-attributes nil)
676 (setq xmltok-attributes nil)
677 (xmltok-scan-attributes)
679 ((xmltok-after-lt start empty-tag-close)
680 (setq xmltok-name-end
681 (xmltok-after-lt end start-tag-name))
682 (setq xmltok-name-colon
683 (xmltok-after-lt start start-tag-colon))
684 (setq xmltok-attributes nil)
685 (setq xmltok-namespace-attributes nil)
686 (setq xmltok-type 'empty-element))
687 ((xmltok-after-lt start cdata-section-open)
689 (if (search-forward "]]>" nil t)
691 (xmltok-add-error "No closing ]]>")
692 (xmltok-add-dependent 'xmltok-unclosed-reparse-p
697 ((xmltok-after-lt start processing-instruction-question)
698 (xmltok-scan-after-processing-instruction-open))
699 ((xmltok-after-lt start comment-open)
700 (xmltok-scan-after-comment-open))
701 ((xmltok-after-lt start empty-tag-slash)
702 (setq xmltok-name-end
703 (xmltok-after-lt end start-tag-name))
704 (setq xmltok-name-colon
705 (xmltok-after-lt start start-tag-colon))
706 (setq xmltok-attributes nil)
707 (setq xmltok-namespace-attributes nil)
708 (xmltok-add-error "Expected `/>'" (1- (point)))
709 (setq xmltok-type 'partial-empty-element))
710 ((xmltok-after-lt start start-tag-name)
711 (xmltok-add-error "Missing `>'"
714 (setq xmltok-name-end
715 (xmltok-after-lt end start-tag-name))
716 (setq xmltok-name-colon
717 (xmltok-after-lt start start-tag-colon))
718 (setq xmltok-namespace-attributes nil)
719 (setq xmltok-attributes nil)
720 (setq xmltok-type 'partial-start-tag))
721 ((xmltok-after-lt start end-tag-name)
722 (setq xmltok-name-end (xmltok-after-lt end end-tag-name))
723 (setq xmltok-name-colon
724 (xmltok-after-lt start end-tag-colon))
725 (cond ((and (not xmltok-name-colon)
726 (eq (char-after) ?:))
727 (goto-char (1+ (point)))
728 (xmltok-add-error "Expected name following `:'"
731 (xmltok-add-error "Missing `>'"
734 (setq xmltok-type 'partial-end-tag))
735 ((xmltok-after-lt start end-tag-slash)
736 (xmltok-add-error "Expected name following `</'")
737 (setq xmltok-name-end nil)
738 (setq xmltok-name-colon nil)
739 (setq xmltok-type 'partial-end-tag))
740 ((xmltok-after-lt start marked-section-open)
741 (xmltok-add-error "Expected `CDATA[' after `<!['"
744 (setq xmltok-type 'not-well-formed))
745 ((xmltok-after-lt start comment-first-dash)
746 (xmltok-add-error "Expected `-' after `<!-'"
749 (setq xmltok-type 'not-well-formed))
750 ((xmltok-after-lt start markup-declaration)
751 (xmltok-add-error "Expected `[CDATA[' or `--' after `<!'"
754 (setq xmltok-type 'not-well-formed))
756 (xmltok-add-error "Not well-formed")
757 (setq xmltok-type 'not-well-formed))))))
759 ;; XXX This should be unified with
760 ;; xmltok-scan-prolog-after-processing-instruction-open
761 ;; XXX maybe should include rest of line (up to any <,>) in unclosed PI
762 (defun xmltok-scan-after-processing-instruction-open ()
763 (cond ((not (search-forward "?>" nil t))
764 (xmltok-add-error "No closing ?>"
767 (xmltok-add-dependent 'xmltok-unclosed-reparse-p
771 (setq xmltok-type 'not-well-formed))
773 (cond ((not (save-excursion
774 (goto-char (+ 2 xmltok-start))
775 (and (looking-at (xmltok-ncname regexp))
776 (setq xmltok-name-end (match-end 0)))))
777 (setq xmltok-name-end (+ xmltok-start 2))
778 (xmltok-add-error "<? not followed by name"
781 ((not (or (memq (char-after xmltok-name-end)
783 (= xmltok-name-end (- (point) 2))))
784 (xmltok-add-error "Target not followed by whitespace"
786 (1+ xmltok-name-end)))
787 ((and (= xmltok-name-end (+ xmltok-start 5))
789 (goto-char (+ xmltok-start 2))
790 (let ((case-fold-search t))
791 (looking-at "xml"))))
792 (xmltok-add-error "Processing instruction target is xml"
794 (+ xmltok-start 5))))
795 (setq xmltok-type 'processing-instruction))))
797 (defun xmltok-scan-after-comment-open ()
799 (cond ((not (search-forward "--" nil t))
800 (xmltok-add-error "No closing -->")
801 (xmltok-add-dependent 'xmltok-unclosed-reparse-p
806 ;; in comments in XML
809 ((eq (char-after) ?>)
810 (goto-char (1+ (point)))
813 (xmltok-add-dependent
814 'xmltok-semi-closed-reparse-p
819 ;; just include the <!-- in the token
820 (goto-char (+ xmltok-start 4))
821 ;; Need do this after the goto-char because
822 ;; marked error should just apply to <!--
823 (xmltok-add-error "First following `--' not followed by `>'")
826 (defun xmltok-scan-attributes ()
827 (let ((recovering nil)
828 (atts-needing-normalization nil))
829 (while (cond ((or (looking-at (xmltok-attribute regexp))
830 ;; use non-greedy group
831 (when (looking-at (concat "[^<>\n]+?"
832 (xmltok-attribute regexp)))
834 (xmltok-add-error "Malformed attribute"
837 (goto-char (xmltok-attribute start
839 (skip-chars-backward "\r\n\t ")
842 (setq recovering nil)
843 (goto-char (match-end 0))
844 (let ((att (xmltok-add-attribute)))
846 (setq atts-needing-normalization
847 (cons att atts-needing-normalization))))
848 (cond ((xmltok-attribute start start-tag-s) t)
849 ((xmltok-attribute start start-tag-close)
850 (setq xmltok-type 'start-tag)
852 ((xmltok-attribute start empty-tag-close)
853 (setq xmltok-type 'empty-element)
855 ((xmltok-attribute start empty-tag-slash)
856 (setq xmltok-type 'partial-empty-element)
857 (xmltok-add-error "Expected `/>'"
860 ((looking-at "[ \t\r\n]*[\"']")
861 (goto-char (match-end 0))
862 (xmltok-add-error "Missing closing delimiter"
866 ((looking-at "[ \t]*\\([^ \t\r\n\"'=<>/]+\\)[ \t\r\n/>]")
867 (goto-char (match-end 1))
868 (xmltok-add-error "Attribute value not quoted"
873 (xmltok-add-error "Missing attribute value"
877 ((looking-at "[^<>\n]*/>")
878 (let ((start (point)))
879 (goto-char (match-end 0))
881 (xmltok-add-error "Malformed empty-element"
884 (setq xmltok-type 'empty-element)
886 ((looking-at "[^<>\n]*>")
887 (let ((start (point)))
888 (goto-char (match-end 0))
890 (xmltok-add-error "Malformed start-tag"
893 (setq xmltok-type 'start-tag)
897 (skip-chars-forward "^<>\n"))
898 (xmltok-add-error "Missing `>'"
901 (setq xmltok-type 'partial-start-tag)
903 (while atts-needing-normalization
904 (xmltok-normalize-attribute (car atts-needing-normalization))
905 (setq atts-needing-normalization (cdr atts-needing-normalization))))
906 (setq xmltok-attributes
907 (nreverse xmltok-attributes))
908 (setq xmltok-namespace-attributes
909 (nreverse xmltok-namespace-attributes)))
911 (defun xmltok-add-attribute ()
912 "Return the attribute if it needs normalizing, otherwise nil."
913 (let* ((needs-normalizing nil)
915 (if (xmltok-attribute start literal)
917 (setq needs-normalizing
918 (or (xmltok-attribute start complex1)
919 (xmltok-attribute start complex2)))
920 (xmltok-make-attribute (xmltok-attribute start name)
921 (xmltok-attribute start colon)
922 (xmltok-attribute end name)
923 (1+ (xmltok-attribute start literal))
924 (1- (xmltok-attribute end literal))
925 (not needs-normalizing)))
926 (xmltok-make-attribute (xmltok-attribute start name)
927 (xmltok-attribute start colon)
928 (xmltok-attribute end name)))))
929 (if (xmltok-attribute start xmlns)
930 (setq xmltok-namespace-attributes
931 (cons att xmltok-namespace-attributes))
932 (setq xmltok-attributes
933 (cons att xmltok-attributes)))
934 (and needs-normalizing
937 (defun xmltok-normalize-attribute (att)
938 (let ((end (xmltok-attribute-value-end att))
943 (goto-char (xmltok-attribute-value-start att))
945 (let ((n (skip-chars-forward "^\r\t\n&" end)))
948 (cons (buffer-substring-no-properties (- (point) n)
951 (when (< (point) end)
952 (goto-char (1+ (point)))
953 (cond ((eq (char-before) ?\&)
954 (let ((xmltok-start (1- (point)))
955 xmltok-type xmltok-replacement)
956 (xmltok-scan-after-amp
958 (xmltok-handle-entity start end t)))
959 (cond ((or (eq xmltok-type 'char-ref)
960 (eq xmltok-type 'entity-ref))
962 (cons (vector xmltok-type
966 (if xmltok-replacement
968 (cons xmltok-replacement
970 (setq well-formed nil)))
971 (t (setq well-formed nil)))))
973 (cons " " value-parts)))))
976 (aset att 5 (apply 'concat (nreverse value-parts))))
977 (aset att 6 (nreverse refs))))
979 (defun xmltok-scan-after-amp (entity-handler)
980 (cond ((not (looking-at (xmltok-after-amp regexp)))
981 (xmltok-add-error "`&' that is not markup must be entered as `&'")
982 (setq xmltok-type 'not-well-formed))
984 (goto-char (match-end 0))
985 (cond ((xmltok-after-amp start entity-ref-close)
986 (funcall entity-handler
987 (xmltok-after-amp start entity-name)
988 (xmltok-after-amp end entity-name))
989 (setq xmltok-type 'entity-ref))
990 ((xmltok-after-amp start decimal-ref-close)
991 (xmltok-scan-char-ref (xmltok-after-amp start decimal)
992 (xmltok-after-amp end decimal)
994 ((xmltok-after-amp start hex-ref-close)
995 (xmltok-scan-char-ref (xmltok-after-amp start hex)
996 (xmltok-after-amp end hex)
998 ((xmltok-after-amp start number-sign)
999 (xmltok-add-error "Missing character number")
1000 (setq xmltok-type 'not-well-formed))
1002 (xmltok-add-error "Missing closing `;'")
1003 (setq xmltok-type 'not-well-formed))))))
1005 (defconst xmltok-entity-error-messages
1006 '((unparsed . "Referenced entity is unparsed")
1007 (not-well-formed . "Referenced entity is not well-formed")
1008 (external nil . "Referenced entity is external")
1009 (element nil . "Referenced entity contains <")))
1011 (defun xmltok-handle-entity (start end &optional attributep)
1012 (let* ((name (buffer-substring-no-properties start end))
1013 (name-def (assoc name xmltok-dtd))
1014 (def (cdr name-def)))
1015 (cond ((setq xmltok-replacement (and (consp def)
1020 (unless (eq (car xmltok-dtd) t)
1021 (xmltok-add-error "Referenced entity has not been defined"
1024 ((and attributep (consp def))
1025 (xmltok-add-error "Referenced entity contains <"
1029 (let ((err (cdr (assq def xmltok-entity-error-messages))))
1031 (setq err (if attributep (cdr err) (car err))))
1033 (xmltok-add-error err start end)))))))
1035 (defun xmltok-scan-char-ref (start end base)
1036 (setq xmltok-replacement
1037 (let ((n (string-to-number (buffer-substring-no-properties start end)
1039 (cond ((and (integerp n) (xmltok-valid-char-p n))
1040 (setq n (xmltok-unicode-to-char n))
1043 (xmltok-add-error "Invalid character code" start end)
1045 (setq xmltok-type 'char-ref))
1047 (defun xmltok-char-number (start end)
1048 (let* ((base (if (eq (char-after (+ start 2)) ?x)
1051 (n (string-to-number
1052 (buffer-substring-no-properties (+ start (if (= base 16) 3 2))
1056 (xmltok-valid-char-p n)
1059 (defun xmltok-unclosed-reparse-p (change-start
1065 (let ((len-1 (1- (length delimiter))))
1066 (goto-char (max start (- change-start len-1)))
1067 (search-forward delimiter (min end (+ change-end len-1)) t)))
1069 ;; Handles a <!-- with the next -- not followed by >
1071 (defun xmltok-semi-closed-reparse-p (change-start
1078 (or (<= (- end delimiter-length) change-end)
1079 (xmltok-unclosed-reparse-p change-start
1086 (defun xmltok-valid-char-p (n)
1087 "Return non-nil if N is the Unicode code of a valid XML character."
1088 (cond ((< n #x20) (memq n '(#xA #xD #x9)))
1092 (t (and (> n #xFFFF)
1095 (defun xmltok-unicode-to-char (n)
1096 "Return the character corresponding to Unicode scalar value N.
1097 Return nil if unsupported in Emacs."
1098 (decode-char 'ucs n))
1102 (defvar xmltok-contains-doctype nil)
1103 (defvar xmltok-doctype-external-subset-flag nil)
1104 (defvar xmltok-internal-subset-start nil)
1105 (defvar xmltok-had-param-entity-ref nil)
1106 (defvar xmltok-prolog-regions nil)
1107 (defvar xmltok-standalone nil
1108 "Non-nil if there was an XML declaration specifying standalone=\"yes\".")
1109 (defvar xmltok-markup-declaration-doctype-flag nil)
1111 (defconst xmltok-predefined-entity-alist
1116 ("quot" "\"" . "\"")))
1118 (defun xmltok-forward-prolog ()
1119 "Move forward to the end of the XML prolog.
1121 Returns a list of vectors [TYPE START END] where TYPE is a symbol and
1122 START and END are integers giving the start and end of the region of
1123 that type. TYPE can be one of xml-declaration,
1124 xml-declaration-attribute-name, xml-declaration-attribute-value,
1125 comment, processing-instruction-left, processing-instruction-right,
1126 markup-declaration-open, markup-declaration-close,
1127 internal-subset-open, internal-subset-close, hash-name, keyword,
1128 literal, encoding-name.
1129 Adds to `xmltok-errors' and `xmltok-dependent-regions' as appropriate."
1130 (let ((case-fold-search nil)
1133 xmltok-prolog-regions
1134 xmltok-contains-doctype
1135 xmltok-internal-subset-start
1136 xmltok-had-param-entity-ref
1138 xmltok-doctype-external-subset-flag
1139 xmltok-markup-declaration-doctype-flag)
1140 (setq xmltok-dtd xmltok-predefined-entity-alist)
1141 (xmltok-scan-xml-declaration)
1142 (xmltok-next-prolog-token)
1143 (while (condition-case err
1144 (when (xmltok-parse-prolog-item)
1145 (xmltok-next-prolog-token))
1146 (xmltok-markup-declaration-parse-error
1147 (xmltok-skip-markup-declaration))))
1148 (when xmltok-internal-subset-start
1149 (xmltok-add-error "No closing ]"
1150 (1- xmltok-internal-subset-start)
1151 xmltok-internal-subset-start))
1152 (xmltok-parse-entities)
1153 ;; XXX prune dependent-regions for those entirely in prolog
1154 (nreverse xmltok-prolog-regions)))
1156 (defconst xmltok-bad-xml-decl-regexp
1157 "[ \t\r\n]*<\\?xml\\(?:[ \t\r\n]\\|\\?>\\)")
1160 (defun xmltok-get-declared-encoding-position (&optional limit)
1161 "Return the position of the encoding in the XML declaration at point.
1162 If there is a well-formed XML declaration starting at point and it
1163 contains an encoding declaration, then return (START . END)
1164 where START and END are the positions of the start and the end
1165 of the encoding name; if there is no encoding declaration return
1166 the position where and encoding declaration could be inserted.
1167 If there is XML that is not well-formed that looks like an XML
1168 declaration, return nil. Otherwise, return t.
1169 If LIMIT is non-nil, then do not consider characters beyond LIMIT."
1170 (cond ((let ((case-fold-search nil))
1171 (and (looking-at (xmltok-xml-declaration regexp))
1172 (or (not limit) (<= (match-end 0) limit))))
1173 (let ((end (xmltok-xml-declaration end encoding-value)))
1175 (cons (1+ (xmltok-xml-declaration start encoding-value))
1177 (or (xmltok-xml-declaration end version-value)
1179 ((not (let ((case-fold-search t))
1180 (looking-at xmltok-bad-xml-decl-regexp))))))
1182 (defun xmltok-scan-xml-declaration ()
1183 (when (looking-at (xmltok-xml-declaration regexp))
1184 (xmltok-add-prolog-region 'xml-declaration (point) (match-end 0))
1185 (goto-char (match-end 0))
1186 (when (xmltok-xml-declaration start version-name)
1187 (xmltok-add-prolog-region 'xml-declaration-attribute-name
1188 (xmltok-xml-declaration start version-name)
1189 (xmltok-xml-declaration end version-name))
1190 (let ((start (xmltok-xml-declaration start version-value))
1191 (end (xmltok-xml-declaration end version-value)))
1192 (xmltok-add-prolog-region 'xml-declaration-attribute-value
1195 ;; XXX need to check encoding name
1196 ;; Should start with letter, not contain colon
1197 (when (xmltok-xml-declaration start encoding-name)
1198 (xmltok-add-prolog-region 'xml-declaration-attribute-name
1199 (xmltok-xml-declaration start encoding-name)
1200 (xmltok-xml-declaration end encoding-name))
1201 (let ((start (xmltok-xml-declaration start encoding-value))
1202 (end (xmltok-xml-declaration end encoding-value)))
1203 (xmltok-add-prolog-region 'encoding-name
1206 (xmltok-add-prolog-region 'xml-declaration-attribute-value
1209 (when (xmltok-xml-declaration start standalone-name)
1210 (xmltok-add-prolog-region 'xml-declaration-attribute-name
1211 (xmltok-xml-declaration start standalone-name)
1212 (xmltok-xml-declaration end standalone-name))
1213 (let ((start (xmltok-xml-declaration start standalone-value))
1214 (end (xmltok-xml-declaration end standalone-value)))
1215 (xmltok-add-prolog-region 'xml-declaration-attribute-value
1218 (setq xmltok-standalone
1219 (string= (buffer-substring-no-properties (1+ start) (1- end))
1223 (defconst xmltok-markup-declaration-alist
1224 '(("ELEMENT" . xmltok-parse-element-declaration)
1225 ("ATTLIST" . xmltok-parse-attlist-declaration)
1226 ("ENTITY" . xmltok-parse-entity-declaration)
1227 ("NOTATION" . xmltok-parse-notation-declaration)))
1229 (defun xmltok-parse-prolog-item ()
1230 (cond ((eq xmltok-type 'comment)
1231 (xmltok-add-prolog-region 'comment
1235 ((eq xmltok-type 'processing-instruction))
1236 ((eq xmltok-type 'named-markup-declaration)
1237 (setq xmltok-markup-declaration-doctype-flag nil)
1238 (xmltok-add-prolog-region 'markup-declaration-open
1241 (let* ((name (buffer-substring-no-properties
1244 (fun (cdr (assoc name xmltok-markup-declaration-alist))))
1246 (unless xmltok-internal-subset-start
1248 "Declaration allowed only in internal subset"))
1250 ((string= name "DOCTYPE")
1251 (xmltok-parse-doctype))
1253 (xmltok-add-error "Unknown markup declaration"
1255 (xmltok-next-prolog-token)
1256 (xmltok-markup-declaration-parse-error))))
1258 ((or (eq xmltok-type 'end-prolog)
1261 ((eq xmltok-type 'internal-subset-close)
1262 (xmltok-add-prolog-region 'internal-subset-close
1265 (xmltok-add-prolog-region 'markup-declaration-close
1268 (if xmltok-internal-subset-start
1269 (setq xmltok-internal-subset-start nil)
1270 (xmltok-add-error "]> outside internal subset"))
1272 ((eq xmltok-type 'param-entity-ref)
1273 (if xmltok-internal-subset-start
1274 (setq xmltok-had-param-entity-ref t)
1275 (xmltok-add-error "Parameter entity reference outside document type declaration"))
1277 ;; If we don't do this, we can get thousands of errors when
1278 ;; a plain text file is parsed.
1279 ((not xmltok-internal-subset-start)
1280 (when (let ((err (car xmltok-errors)))
1282 (<= (xmltok-error-end err) xmltok-start)))
1283 (goto-char xmltok-start))
1285 ((eq xmltok-type 'not-well-formed) t)
1287 (xmltok-add-error "Token allowed only inside markup declaration")
1290 (defun xmltok-parse-doctype ()
1291 (setq xmltok-markup-declaration-doctype-flag t)
1292 (xmltok-next-prolog-token)
1293 (when xmltok-internal-subset-start
1294 (xmltok-add-error "DOCTYPE declaration not allowed in internal subset")
1295 (xmltok-markup-declaration-parse-error))
1296 (when xmltok-contains-doctype
1297 (xmltok-add-error "Duplicate DOCTYPE declaration")
1298 (xmltok-markup-declaration-parse-error))
1299 (setq xmltok-contains-doctype t)
1300 (xmltok-require-token 'name 'prefixed-name)
1301 (xmltok-require-next-token "SYSTEM" "PUBLIC" ?\[ ?>)
1302 (cond ((eq xmltok-type ?\[)
1303 (setq xmltok-internal-subset-start (point)))
1304 ((eq xmltok-type ?>))
1306 (setq xmltok-doctype-external-subset-flag t)
1307 (xmltok-parse-external-id)
1308 (xmltok-require-token ?\[ ?>)
1309 (when (eq xmltok-type ?\[)
1310 (setq xmltok-internal-subset-start (point))))))
1312 (defun xmltok-parse-attlist-declaration ()
1313 (xmltok-require-next-token 'prefixed-name 'name)
1315 (xmltok-require-next-token ?> 'name 'prefixed-name)
1316 (if (eq xmltok-type ?>)
1318 (xmltok-require-next-token ?\(
1328 (cond ((eq xmltok-type ?\()
1329 (xmltok-parse-nmtoken-group))
1330 ((string= (xmltok-current-token-string)
1332 (xmltok-require-next-token ?\()
1333 (xmltok-parse-nmtoken-group)))
1334 (xmltok-require-next-token "#IMPLIED"
1338 (when (string= (xmltok-current-token-string) "#FIXED")
1339 (xmltok-require-next-token 'literal))
1342 (defun xmltok-parse-nmtoken-group ()
1344 (xmltok-require-next-token 'nmtoken 'prefixed-name 'name)
1345 (xmltok-require-next-token ?| ?\))
1346 (eq xmltok-type ?|))))
1348 (defun xmltok-parse-element-declaration ()
1349 (xmltok-require-next-token 'name 'prefixed-name)
1350 (xmltok-require-next-token "EMPTY" "ANY" ?\()
1351 (when (eq xmltok-type ?\()
1352 (xmltok-require-next-token "#PCDATA"
1357 (cond ((eq xmltok-type 'hash-name)
1358 (xmltok-require-next-token ?| ?\) 'close-paren-star)
1359 (while (eq xmltok-type ?|)
1360 (xmltok-require-next-token 'name 'prefixed-name)
1361 (xmltok-require-next-token 'close-paren-star ?|)))
1362 (t (xmltok-parse-model-group))))
1363 (xmltok-require-next-token ?>))
1365 (defun xmltok-parse-model-group ()
1366 (xmltok-parse-model-group-member)
1367 (xmltok-require-next-token ?|
1372 (when (memq xmltok-type '(?, ?|))
1373 (let ((connector xmltok-type))
1375 (xmltok-next-prolog-token)
1376 (xmltok-parse-model-group-member)
1377 (xmltok-require-next-token connector
1381 (eq xmltok-type connector))))))
1383 (defun xmltok-parse-model-group-member ()
1384 (xmltok-require-token 'name
1388 (when (eq xmltok-type ?\()
1389 (xmltok-next-prolog-token)
1390 (xmltok-parse-model-group)))
1392 (defun xmltok-parse-entity-declaration ()
1394 (xmltok-require-next-token 'name ?%)
1395 (when (eq xmltok-type ?%)
1397 (xmltok-require-next-token 'name))
1398 (setq name (xmltok-current-token-string))
1399 (xmltok-require-next-token 'literal "SYSTEM" "PUBLIC")
1400 (cond ((eq xmltok-type 'literal)
1401 (let ((replacement (xmltok-parse-entity-value)))
1403 (xmltok-define-entity name replacement)))
1404 (xmltok-require-next-token ?>))
1406 (xmltok-parse-external-id)
1408 (xmltok-require-token ?>)
1409 (xmltok-require-token ?> "NDATA")
1410 (if (eq xmltok-type ?>)
1411 (xmltok-define-entity name 'external)
1412 (xmltok-require-next-token 'name)
1413 (xmltok-require-next-token ?>)
1414 (xmltok-define-entity name 'unparsed)))))))
1416 (defun xmltok-define-entity (name value)
1417 (when (and (or (not xmltok-had-param-entity-ref)
1419 (not (assoc name xmltok-dtd)))
1421 (cons (cons name value) xmltok-dtd))))
1423 (defun xmltok-parse-entity-value ()
1424 (let ((lim (1- (point)))
1429 (goto-char (1+ xmltok-start))
1430 (setq start (point))
1432 (skip-chars-forward "^%&" lim)
1433 (when (< (point) lim)
1434 (goto-char (1+ (point)))
1435 (cond ((eq (char-before) ?%)
1436 (xmltok-add-error "Parameter entity references are not allowed in the internal subset"
1439 (setq well-formed nil))
1441 (let ((xmltok-start (1- (point)))
1442 xmltok-type xmltok-replacement)
1443 (xmltok-scan-after-amp (lambda (start end)))
1444 (cond ((eq xmltok-type 'char-ref)
1446 (cons (buffer-substring-no-properties
1451 (cons xmltok-replacement
1453 (setq start (point)))
1454 ((eq xmltok-type 'not-well-formed)
1455 (setq well-formed nil))))))
1457 (if (not well-formed)
1460 (nreverse (cons (buffer-substring-no-properties start lim)
1463 (defun xmltok-parse-notation-declaration ()
1464 (xmltok-require-next-token 'name)
1465 (xmltok-require-next-token "SYSTEM" "PUBLIC")
1466 (let ((publicp (string= (xmltok-current-token-string) "PUBLIC")))
1467 (xmltok-require-next-token 'literal)
1469 (xmltok-require-next-token 'literal ?>)
1470 (unless (eq xmltok-type ?>)
1471 (xmltok-require-next-token ?>)))
1472 (t (xmltok-require-next-token ?>)))))
1474 (defun xmltok-parse-external-id ()
1475 (xmltok-require-token "SYSTEM" "PUBLIC")
1476 (let ((publicp (string= (xmltok-current-token-string) "PUBLIC")))
1477 (xmltok-require-next-token 'literal)
1479 (xmltok-require-next-token 'literal)))
1480 (xmltok-next-prolog-token))
1482 (defun xmltok-require-next-token (&rest types)
1483 (xmltok-next-prolog-token)
1484 (apply 'xmltok-require-token types))
1486 (defun xmltok-require-token (&rest types)
1487 ;; XXX Generate a more helpful error message
1488 (while (and (not (let ((type (car types)))
1489 (if (stringp (car types))
1490 (string= (xmltok-current-token-string) type)
1491 (eq type xmltok-type))))
1492 (setq types (cdr types))))
1494 (when (and xmltok-type
1495 (not (eq xmltok-type 'not-well-formed)))
1496 (xmltok-add-error "Unexpected token"))
1497 (xmltok-markup-declaration-parse-error))
1498 (let ((region-type (xmltok-prolog-region-type (car types))))
1500 (xmltok-add-prolog-region region-type
1504 (defun xmltok-current-token-string ()
1505 (buffer-substring-no-properties xmltok-start (point)))
1507 (put 'xmltok-markup-declaration-parse-error
1509 '(error xmltok-markup-declaration-parse-error))
1511 (put 'xmltok-markup-declaration-parse-error
1513 "Syntax error in markup declaration")
1515 (defun xmltok-markup-declaration-parse-error ()
1516 (signal 'xmltok-markup-declaration-parse-error nil))
1518 (defun xmltok-skip-markup-declaration ()
1519 (while (cond ((eq xmltok-type ?>)
1520 (xmltok-next-prolog-token)
1522 ((and xmltok-markup-declaration-doctype-flag
1523 (eq xmltok-type ?\[))
1524 (setq xmltok-internal-subset-start (point))
1525 (xmltok-next-prolog-token)
1527 ((memq xmltok-type '(nil
1529 named-markup-declaration
1531 processing-instruction))
1533 ((and xmltok-internal-subset-start
1534 (eq xmltok-type 'internal-subset-close))
1536 (t (xmltok-next-prolog-token) t)))
1539 (defun xmltok-prolog-region-type (required)
1540 (cond ((cdr (assq xmltok-type
1541 '((literal . literal)
1542 (?> . markup-declaration-close)
1543 (?\[ . internal-subset-open)
1544 (hash-name . hash-name)))))
1545 ((and (stringp required) (eq xmltok-type 'name))
1548 ;; Return new token type.
1550 (defun xmltok-next-prolog-token ()
1551 (skip-chars-forward " \t\r\n")
1552 (setq xmltok-start (point))
1553 (cond ((not (and (looking-at (xmltok-prolog regexp))
1554 (goto-char (match-end 0))))
1555 (let ((ch (char-after)))
1557 (goto-char (1+ (point)))
1558 (xmltok-add-error "Illegal char in prolog")
1559 (setq xmltok-type 'not-well-formed))
1560 (t (setq xmltok-type nil)))))
1561 ((or (xmltok-prolog start ncname-occur)
1562 (xmltok-prolog start colon-name-occur))
1563 (setq xmltok-name-end (1- (point)))
1564 (setq xmltok-name-colon (xmltok-prolog start colon))
1565 (setq xmltok-type 'name-occur))
1566 ((xmltok-prolog start colon)
1567 (setq xmltok-name-end (point))
1568 (setq xmltok-name-colon (xmltok-prolog start colon))
1569 (unless (looking-at "[ \t\r\n>),|[%]")
1570 (xmltok-add-error "Missing space after name"))
1571 (setq xmltok-type 'prefixed-name))
1572 ((or (xmltok-prolog start ncname-nmtoken)
1573 (xmltok-prolog start ncname-colon))
1574 (unless (looking-at "[ \t\r\n>),|[%]")
1575 (xmltok-add-error "Missing space after name token"))
1576 (setq xmltok-type 'nmtoken))
1577 ((xmltok-prolog start name)
1578 (setq xmltok-name-end (point))
1579 (setq xmltok-name-colon nil)
1580 (unless (looking-at "[ \t\r\n>),|[%]")
1581 (xmltok-add-error "Missing space after name"))
1582 (setq xmltok-type 'name))
1583 ((xmltok-prolog start hash-name)
1584 (setq xmltok-name-end (point))
1585 (unless (looking-at "[ \t\r\n>)|%]")
1586 (xmltok-add-error "Missing space after name"))
1587 (setq xmltok-type 'hash-name))
1588 ((xmltok-prolog start processing-instruction-question)
1589 (xmltok-scan-prolog-after-processing-instruction-open))
1590 ((xmltok-prolog start comment-open)
1591 ;; XXX if not-well-formed, ignore some stuff
1592 (xmltok-scan-after-comment-open))
1593 ((xmltok-prolog start named-markup-declaration)
1594 (setq xmltok-type 'named-markup-declaration))
1595 ((xmltok-prolog start instance-start)
1596 (goto-char xmltok-start)
1597 (setq xmltok-type 'end-prolog))
1598 ((xmltok-prolog start close-paren-star)
1599 (setq xmltok-type 'close-paren-star))
1600 ((xmltok-prolog start close-paren-occur)
1601 (setq xmltok-type 'close-paren-occur))
1602 ((xmltok-prolog start close-paren)
1603 (unless (looking-at "[ \t\r\n>,|)]")
1604 (xmltok-add-error "Missing space after )"))
1605 (setq xmltok-type ?\)))
1606 ((xmltok-prolog start single-char)
1607 (let ((ch (char-before)))
1608 (cond ((memq ch '(?\" ?\'))
1609 (xmltok-scan-prolog-literal))
1610 (t (setq xmltok-type ch)))))
1611 ((xmltok-prolog start percent)
1612 (cond ((xmltok-prolog start param-entity-ref-close)
1613 (setq xmltok-name-end (1- (point)))
1614 (setq xmltok-type 'param-entity-ref))
1615 ((xmltok-prolog start param-entity-ref)
1616 (xmltok-add-error "Missing ;")
1617 (setq xmltok-name-end (point))
1618 (setq xmltok-type 'param-entity-ref))
1619 ((looking-at "[ \t\r\n%]")
1620 (setq xmltok-type ?%))
1622 (xmltok-add-error "Expected name after %")
1623 (setq xmltok-type 'not-well-formed))))
1624 ((xmltok-prolog start nmtoken)
1625 (unless (looking-at "[ \t\r\n>),|[%]")
1626 (xmltok-add-error "Missing space after name token"))
1627 (setq xmltok-type 'nmtoken))
1628 ((xmltok-prolog start internal-subset-close)
1629 (setq xmltok-type 'internal-subset-close))
1630 ((xmltok-prolog start pound)
1631 (xmltok-add-error "Expected name after #")
1632 (setq xmltok-type 'not-well-formed))
1633 ((xmltok-prolog start markup-declaration)
1634 (xmltok-add-error "Expected name or -- after <!")
1635 (setq xmltok-type 'not-well-formed))
1636 ((xmltok-prolog start comment-first-dash)
1637 (xmltok-add-error "Expected <!--")
1638 (setq xmltok-type 'not-well-formed))
1639 ((xmltok-prolog start less-than)
1640 (xmltok-add-error "Incomplete markup")
1641 (setq xmltok-type 'not-well-formed))
1642 (t (error "Unhandled token in prolog %s"
1643 (match-string-no-properties 0)))))
1645 (defun xmltok-scan-prolog-literal ()
1646 (let* ((delim (string (char-before)))
1647 (safe-end (save-excursion
1648 (skip-chars-forward (concat "^<>[]" delim))
1650 (end (save-excursion
1651 (goto-char safe-end)
1652 (search-forward delim nil t))))
1653 (or (cond ((not end)
1654 (xmltok-add-dependent 'xmltok-unclosed-reparse-p
1661 (looking-at "[ \t\r\n>%[]"))
1663 (setq xmltok-type 'literal))
1664 ((eq (1+ safe-end) end)
1666 (xmltok-add-error (format "Missing space after %s" delim)
1668 (setq xmltok-type 'literal))
1670 (xmltok-add-dependent 'xmltok-semi-closed-reparse-p
1677 (xmltok-add-error (format "Missing closing %s" delim))
1678 (goto-char safe-end)
1679 (skip-chars-backward " \t\r\n")
1680 (setq xmltok-type 'not-well-formed)))))
1682 (defun xmltok-scan-prolog-after-processing-instruction-open ()
1683 (cond ((not (search-forward "?>" nil t))
1684 (xmltok-add-error "No closing ?>"
1687 (xmltok-add-dependent 'xmltok-unclosed-reparse-p
1691 (setq xmltok-type 'not-well-formed))
1693 (let* ((end (point))
1696 (goto-char (+ xmltok-start 2))
1697 (and (looking-at (xmltok-ncname regexp))
1698 (or (memq (char-after (match-end 0))
1700 (= (match-end 0) (- end 2)))
1701 (match-string-no-properties 0)))))
1703 (xmltok-add-error "\
1704 Processing instruction does not start with a name"
1706 (+ xmltok-start 3)))
1707 ((not (and (= (length target) 3)
1708 (let ((case-fold-search t))
1709 (string-match "xml" target)))))
1711 (xmltok-add-error "Invalid XML declaration"
1715 (goto-char xmltok-start)
1716 (looking-at (xmltok-xml-declaration regexp)))
1717 (xmltok-add-error "XML declaration not at beginning of file"
1721 (xmltok-add-error "Processing instruction has target of xml"
1723 (+ xmltok-start 5))))
1724 (xmltok-add-prolog-region 'processing-instruction-left
1731 (xmltok-add-prolog-region 'processing-instruction-right
1734 (goto-char (+ xmltok-start
1737 (skip-chars-forward " \t\r\n")
1741 (setq xmltok-type 'processing-instruction))))
1743 (defun xmltok-parse-entities ()
1744 (let ((todo xmltok-dtd))
1745 (when (and (or xmltok-had-param-entity-ref
1746 xmltok-doctype-external-subset-flag)
1747 (not xmltok-standalone))
1748 (setq xmltok-dtd (cons t xmltok-dtd)))
1750 (xmltok-parse-entity (car todo))
1751 (setq todo (cdr todo)))))
1753 (defun xmltok-parse-entity (name-def)
1754 (let ((def (cdr name-def))
1755 ;; in case its value is buffer local
1756 (xmltok-dtd xmltok-dtd)
1759 (if (string-match "\\`[^&<\t\r\n]*\\'" def)
1760 (setcdr name-def (cons def def))
1761 (setcdr name-def 'not-well-formed) ; avoid infinite expansion loops
1762 (setq buf (get-buffer-create
1763 (format " *Entity %s*" (car name-def))))
1768 (goto-char (point-min))
1770 (xmltok-parse-entity-replacement)))
1771 (kill-buffer buf)))))
1773 (defun xmltok-parse-entity-replacement ()
1774 (let ((def (cons "" "")))
1775 (while (let* ((start (point))
1776 (found (re-search-forward "[<&\t\r\n]\\|]]>" nil t))
1777 (ch (and found (char-before)))
1778 (str (buffer-substring-no-properties
1784 (xmltok-append-entity-def def
1786 (cond ((not found) nil)
1788 (setq def 'not-well-formed)
1792 (setq xmltok-start (1- (point)))
1793 (xmltok-scan-after-lt)
1795 (xmltok-append-entity-def
1797 (cond ((memq xmltok-type
1804 processing-instruction))
1808 (cons (buffer-substring-no-properties
1812 (t 'not-well-formed)))))
1815 (let ((xmltok-start (1- (point)))
1819 (xmltok-scan-after-amp 'xmltok-handle-nested-entity)
1820 (cond ((eq xmltok-type 'entity-ref)
1822 (xmltok-append-entity-def
1824 xmltok-replacement)))
1825 ((eq xmltok-type 'char-ref)
1827 (xmltok-append-entity-def
1829 (if xmltok-replacement
1830 (cons xmltok-replacement
1832 (and xmltok-errors 'not-well-formed)))))
1834 (setq def 'not-well-formed))))
1838 (xmltok-append-entity-def
1840 (cons (match-string-no-properties 0)
1845 (defun xmltok-handle-nested-entity (start end)
1846 (let* ((name-def (assoc (buffer-substring-no-properties start end)
1848 (def (cdr name-def)))
1850 (xmltok-parse-entity name-def)
1851 (setq def (cdr name-def)))
1852 (setq xmltok-replacement
1853 (cond ((null name-def)
1854 (if (eq (car xmltok-dtd) t)
1857 ((eq def 'unparsed) 'not-well-formed)
1860 (defun xmltok-append-entity-def (d1 d2)
1863 (cons (concat (car d1) (car d2))
1866 (concat (cdr d1) (cdr d2))))
1870 (let ((defs '(not-well-formed external element)))
1871 (while (not (or (eq (car defs) d1)
1872 (eq (car defs) d2)))
1873 (setq defs (cdr defs)))
1876 (defun xmltok-add-prolog-region (type start end)
1877 (setq xmltok-prolog-regions
1878 (cons (vector type start end)
1879 xmltok-prolog-regions)))
1881 (defun xmltok-merge-attributes ()
1882 "Return a list merging `xmltok-attributes' and `xmltok-namespace-attributes'.
1883 The members of the merged list are in order of occurrence in the
1884 document. The list may share list structure with `xmltok-attributes'
1885 and `xmltok-namespace-attributes'."
1886 (cond ((not xmltok-namespace-attributes)
1888 ((not xmltok-attributes)
1889 xmltok-namespace-attributes)
1891 (let ((atts1 xmltok-attributes)
1892 (atts2 xmltok-namespace-attributes)
1894 (while (and atts1 atts2)
1895 (cond ((< (xmltok-attribute-name-start (car atts1))
1896 (xmltok-attribute-name-start (car atts2)))
1897 (setq merged (cons (car atts1) merged))
1898 (setq atts1 (cdr atts1)))
1900 (setq merged (cons (car atts2) merged))
1901 (setq atts2 (cdr atts2)))))
1902 (setq merged (nreverse merged))
1903 (cond (atts1 (setq merged (nconc merged atts1)))
1904 (atts2 (setq merged (nconc merged atts2))))
1909 (defun xmltok-forward-test ()
1911 (if (xmltok-forward)
1912 (message "Scanned %s" xmltok-type)
1913 (message "Scanned nothing")))
1915 (defun xmltok-next-prolog-token-test ()
1917 (if (xmltok-next-prolog-token)
1918 (message "Scanned %s"
1919 (if (integerp xmltok-type)
1920 (string xmltok-type)
1922 (message "Scanned end of file")))
1926 ;; arch-tag: 747e5f3a-6fc3-4f8d-bd96-89f05aa99f5e
1927 ;;; xmltok.el ends here