1 ;;; xmltok.el --- XML tokenization -*- lexical-binding:t -*-
3 ;; Copyright (C) 2003, 2007-2014 Free Software Foundation, Inc.
6 ;; Keywords: wp, hypermedia, languages, XML
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.")
136 (defvar xmltok-errors nil
137 "List of errors detected by `xmltok-forward' and `xmltok-forward-prolog'.
138 When `xmltok-forward' and `xmltok-forward-prolog' detect a
139 well-formedness error, they will add an entry to the beginning of this
140 list. Each entry is a vector [MESSAGE START END], where MESSAGE is a
141 string giving the error message and START and END are integers
142 indicating the position of the error.")
144 (defmacro xmltok-save
(&rest body
)
145 (declare (indent 0) (debug t
))
152 xmltok-namespace-attributes
156 (defsubst xmltok-attribute-name-start
(att)
159 (defsubst xmltok-attribute-name-colon
(att)
162 (defsubst xmltok-attribute-name-end
(att)
165 (defsubst xmltok-attribute-value-start
(att)
168 (defsubst xmltok-attribute-value-end
(att)
171 (defsubst xmltok-attribute-raw-normalized-value
(att)
172 "Return an object representing the normalized value of ATT.
173 This can be t indicating that the normalized value is the same as
174 the buffer substring from the start to the end of the value, or nil
175 indicating that the value is not well-formed or a string."
178 (defsubst xmltok-attribute-refs
(att)
179 "Return a list of the entity and character references in ATT.
180 Each member is a vector [TYPE START END] where TYPE is either char-ref
181 or entity-ref and START and END are integers giving the start and end of
182 the reference. Nested entity references are not included in the list."
185 (defun xmltok-attribute-prefix (att)
186 (let ((colon (xmltok-attribute-name-colon att
)))
188 (buffer-substring-no-properties (xmltok-attribute-name-start att
)
191 (defun xmltok-attribute-local-name (att)
192 (let ((colon (xmltok-attribute-name-colon att
)))
193 (buffer-substring-no-properties (if colon
195 (xmltok-attribute-name-start att
))
196 (xmltok-attribute-name-end att
))))
198 (defun xmltok-attribute-value (att)
199 (let ((rnv (xmltok-attribute-raw-normalized-value att
)))
203 (buffer-substring-no-properties (xmltok-attribute-value-start att
)
204 (xmltok-attribute-value-end att
))))))
206 (defun xmltok-start-tag-prefix ()
207 (and xmltok-name-colon
208 (buffer-substring-no-properties (1+ xmltok-start
)
211 (defun xmltok-start-tag-local-name ()
212 (buffer-substring-no-properties (1+ (or xmltok-name-colon
216 (defun xmltok-end-tag-prefix ()
217 (and xmltok-name-colon
218 (buffer-substring-no-properties (+ 2 xmltok-start
)
221 (defun xmltok-end-tag-local-name ()
222 (buffer-substring-no-properties (if xmltok-name-colon
223 (1+ xmltok-name-colon
)
227 (defun xmltok-start-tag-qname ()
228 (buffer-substring-no-properties (+ xmltok-start
1) xmltok-name-end
))
230 (defun xmltok-end-tag-qname ()
231 (buffer-substring-no-properties (+ xmltok-start
2) xmltok-name-end
))
233 (defsubst xmltok-make-attribute
(name-begin
239 raw-normalized-value
)
241 RAW-NORMALIZED-VALUE is nil if the value is not well-formed,
242 t if the normalized value is the string between VALUE-BEGIN
243 and VALUE-END, otherwise a STRING giving the value."
252 (defsubst xmltok-error-message
(err)
255 (defsubst xmltok-error-start
(err)
258 (defsubst xmltok-error-end
(err)
261 (defsubst xmltok-make-error
(message start end
)
262 (vector message start end
))
264 (defun xmltok-add-error (message &optional start end
)
266 (cons (xmltok-make-error message
267 (or start xmltok-start
)
271 (defun xmltok-forward ()
272 (setq xmltok-start
(point))
273 (let* ((case-fold-search nil
)
274 (space-count (skip-chars-forward " \t\r\n"))
277 (cond ((> space-count
0)
278 (setq xmltok-type
'space
))
281 (xmltok-scan-after-lt))))
283 (cond ((> space-count
0)
284 (setq xmltok-type
'space
))
287 (xmltok-scan-after-amp 'xmltok-handle-entity
))))
288 ((re-search-forward "[<&]\\|\\(]]>\\)" nil t
)
289 (cond ((not (match-beginning 1))
290 (goto-char (match-beginning 0))
291 ;; must have got a non-space char
292 (setq xmltok-type
'data
))
293 ((= (match-beginning 1) xmltok-start
)
294 (xmltok-add-error "Found `]]>' not closing a CDATA section")
295 (setq xmltok-type
'not-well-formed
))
297 (goto-char (match-beginning 0))
299 (if (= (point) (+ xmltok-start space-count
))
304 (if (> space-count
0)
308 (goto-char (point-max))
309 (setq xmltok-type
'data
)))))
311 (defun xmltok-forward-special (bound)
312 "Scan forward past the first special token starting at or after point.
313 Return nil if there is no special token that starts before BOUND.
314 CDATA sections, processing instructions and comments (and indeed
315 anything starting with < following by ? or !) count as special.
316 Return the type of the token."
317 (when (re-search-forward "<[?!]" (1+ bound
) t
)
318 (setq xmltok-start
(match-beginning 0))
319 (goto-char (1+ xmltok-start
))
320 (let ((case-fold-search nil
))
321 (xmltok-scan-after-lt))))
325 ;; A symbolic regexp is represented by a list whose CAR is the string
326 ;; containing the regexp and whose cdr is a list of symbolic names
327 ;; for the groups in the string.
329 ;; Construct a symbolic regexp from a regexp.
330 (defun xmltok-r (str)
333 ;; Concatenate zero of more regexps and symbolic regexps.
334 (defun xmltok+ (&rest args
)
337 (let ((arg (car args
)))
339 (setq strs
(cons arg strs
))
340 (setq strs
(cons (car arg
) strs
))
341 (setq names
(cons (cdr arg
) names
)))
342 (setq args
(cdr args
))))
343 (cons (apply 'concat
(nreverse strs
))
344 (apply 'append
(nreverse names
))))))
347 ;; Make a symbolic group named NAME from the regexp R.
348 ;; R may be a symbolic regexp or an ordinary regexp.
349 (defmacro xmltok-g
(name &rest r
)
350 (let ((sym (make-symbol "r")))
351 `(let ((,sym
(xmltok+ ,@r
)))
353 (cons (concat "\\(" ,sym
"\\)") (cons ',name nil
))
354 (cons (concat "\\(" (car ,sym
) "\\)") (cons ',name
(cdr ,sym
)))))))
356 (defun xmltok-p (&rest r
) (xmltok+ "\\(?:"
360 ;; Get the group index of ELEM in a LIST of symbols.
361 (defun xmltok-get-index (elem list
)
363 (error "Missing group name"))
367 (cond ((eq elem
(car list
))
372 (setq list
(cdr list
)))))
374 (error "Bad group name %s" elem
))))
376 ;; Define a macro SYM using a symbolic regexp R.
377 ;; SYM can be called in three ways:
379 ;; expands to the regexp in R
382 ;; (match-beginning N)
383 ;; where N is the group index of G in R.
387 ;; where N is the group index of G in R.
388 (defmacro xmltok-defregexp
(sym r
)
391 `(macro lambda
(action &optional group-name
)
392 (cond ((eq action
'regexp
)
394 ((or (eq action
'start
) (eq action
'beginning
))
395 (list 'match-beginning
(xmltok-get-index group-name
398 (list 'match-end
(xmltok-get-index group-name
402 (xmltok-get-index group-name
',(cdr r
))))
403 ((eq action
'string-no-properties
)
404 (list 'match-string-no-properties
405 (xmltok-get-index group-name
',(cdr r
))))
406 (t (error "Invalid action: %s" action
))))))))
413 (name-start-char "[_[:alpha:]]")
414 (name-continue-not-start-char "[-.[:digit:]]")
415 (name-continue-char "[-._[:alnum:]]")
423 (ncname (concat name-start-char name-continue-char
*))
425 (xmltok+ (xmltok-g entity-name ncname
)
426 (xmltok-g entity-ref-close
";") opt
))
428 (xmltok+ (xmltok-g decimal
"[0-9]" +)
429 (xmltok-g decimal-ref-close
";") opt
))
432 (xmltok-g hex
"[0-9a-fA-F]" +)
433 (xmltok-g hex-ref-close
";") opt
436 (xmltok+ (xmltok-g number-sign
"#")
437 open decimal-ref or hex-ref close opt
))
439 (xmltok+ open
(xmltok-g start-tag-close s
* ">")
440 or open
(xmltok-g empty-tag-slash s
* "/")
441 (xmltok-g empty-tag-close
">") opt close
442 or
(xmltok-g start-tag-s s
+)
445 (xmltok+ (xmltok-g start-tag-name
446 ncname
(xmltok-g start-tag-colon
":" ncname
) opt
)
447 start-tag-close opt
))
449 (xmltok+ (xmltok-g end-tag-slash
"/")
450 open
(xmltok-g end-tag-name
452 (xmltok-g end-tag-colon
":" ncname
) opt
)
453 (xmltok-g end-tag-close s
* ">") opt
456 (xmltok+ (xmltok-g markup-declaration
"!")
457 (xmltok-g comment-first-dash
"-"
458 (xmltok-g comment-open
"-") opt
) opt
))
461 (xmltok-g marked-section-open
"\\[")
467 (xmltok-g cdata-section-open
"\\[" ) opt
473 (processing-instruction
474 (xmltok-g processing-instruction-question question
)))
476 (xmltok-defregexp xmltok-ncname
(xmltok+ open ncname close
))
478 (xmltok-defregexp xmltok-after-amp
479 (xmltok+ entity-ref or char-ref
))
480 (xmltok-defregexp xmltok-after-lt
483 ;; cdata-section must come before comment
484 ;; because we treat <! as a comment
485 ;; and Emacs doesn't do fully greedy matching
489 or processing-instruction
))
495 (xmltok-g complex1
"[&\r\n\t][^<']*") opt
497 (lit2 (cons (replace-regexp-in-string "'" "\"" (car lit1
))
499 (literal (xmltok-g literal lit1 or lit2
))
500 (name (xmltok+ open
(xmltok-g xmlns
"xmlns") or ncname close
501 (xmltok-g colon
":" ncname
) opt
)))
502 (xmltok+ (xmltok-g name name
)
504 ;; If the literal isn't followed by what it should be,
505 ;; then the closing delimiter is probably really the
506 ;; opening delimiter of another literal, so don't
507 ;; absorb the literal in this case.
508 open s
* literal start-tag-close close opt
)))
510 xmltok-xml-declaration
511 (let* ((literal-content "[-._:a-zA-Z0-9]+")
513 (concat open
"\"" literal-content
"\""
514 or
"'" literal-content
"'" close
))
517 s
+ (xmltok-g version-name
"version")
519 s
* (xmltok-g version-value literal
)
523 s
+ (xmltok-g encoding-name
"encoding")
525 s
* (xmltok-g encoding-value literal
)
528 (concat open
"yes" or
"no" close
))
531 s
+ (xmltok-g standalone-name
"standalone")
533 s
* (xmltok-g standalone-value
534 "\"" yes-no
"\"" or
"'" yes-no
"'")
536 (xmltok+ "<" question
"xml"
543 (let* ((single-char (xmltok-g single-char
"[[|,(\"'>]"))
544 (internal-subset-close (xmltok-g internal-subset-close
546 (starts-with-close-paren
547 (xmltok-g close-paren
550 (xmltok-g close-paren-occur
"[+?]")
552 (xmltok-g close-paren-star
"\\*"))
556 "%" (xmltok-g param-entity-ref
558 (xmltok-g param-entity-ref-close
560 (starts-with-nmtoken-not-name
562 (xmltok-p name-continue-not-start-char or
":")
563 (xmltok-p name-continue-char or
":") *))
566 (xmltok-p name-continue-not-start-char or
":")
567 (xmltok-p name-continue-char or
":") *
572 (xmltok-p name-continue-char or
":") *))
574 (xmltok+ (xmltok-g ncname-nmtoken
575 ":" (xmltok-p nmtoken-after-colon
))
576 or
(xmltok-p (xmltok-g colon
":" ncname
)
577 (xmltok-g colon-name-occur
"[?+*]") opt
)
578 or
(xmltok-g ncname-occur
"[?+*]")
579 or
(xmltok-g ncname-colon
":")))
581 (xmltok-g name ncname
(xmltok-p after-ncname
) opt
))
584 "#" (xmltok-g hash-name ncname
)))
586 (xmltok-g markup-declaration
587 "!" (xmltok-p (xmltok-g comment-first-dash
"-"
588 (xmltok-g comment-open
"-") opt
)
589 or
(xmltok-g named-markup-declaration
592 (xmltok+ markup-declaration
593 or
(xmltok-g processing-instruction-question
595 or
(xmltok-g instance-start
597 (starts-with-lt (xmltok-g less-than
"<" (xmltok-p after-lt
) opt
)))
598 (xmltok+ starts-with-lt
600 or starts-with-close-paren
601 or starts-with-percent
603 or starts-with-nmtoken-not-name
605 or internal-subset-close
)))))
607 (defconst xmltok-ncname-regexp
(xmltok-ncname regexp
))
609 (defun xmltok-scan-after-lt ()
610 (cond ((not (looking-at (xmltok-after-lt regexp
)))
611 (xmltok-add-error "`<' that is not markup must be entered as `<'")
612 (setq xmltok-type
'not-well-formed
))
614 (goto-char (match-end 0))
615 (cond ((xmltok-after-lt start start-tag-close
)
616 (setq xmltok-name-end
617 (xmltok-after-lt end start-tag-name
))
618 (setq xmltok-name-colon
619 (xmltok-after-lt start start-tag-colon
))
620 (setq xmltok-attributes nil
)
621 (setq xmltok-namespace-attributes nil
)
622 (setq xmltok-type
'start-tag
))
623 ((xmltok-after-lt start end-tag-close
)
624 (setq xmltok-name-end
625 (xmltok-after-lt end end-tag-name
))
626 (setq xmltok-name-colon
627 (xmltok-after-lt start end-tag-colon
))
628 (setq xmltok-type
'end-tag
))
629 ((xmltok-after-lt start start-tag-s
)
630 (setq xmltok-name-end
631 (xmltok-after-lt end start-tag-name
))
632 (setq xmltok-name-colon
633 (xmltok-after-lt start start-tag-colon
))
634 (setq xmltok-namespace-attributes nil
)
635 (setq xmltok-attributes nil
)
636 (xmltok-scan-attributes)
638 ((xmltok-after-lt start empty-tag-close
)
639 (setq xmltok-name-end
640 (xmltok-after-lt end start-tag-name
))
641 (setq xmltok-name-colon
642 (xmltok-after-lt start start-tag-colon
))
643 (setq xmltok-attributes nil
)
644 (setq xmltok-namespace-attributes nil
)
645 (setq xmltok-type
'empty-element
))
646 ((xmltok-after-lt start cdata-section-open
)
648 (progn (search-forward "]]>" nil
'move
)
650 ((xmltok-after-lt start processing-instruction-question
)
651 (xmltok-scan-after-processing-instruction-open))
652 ((xmltok-after-lt start comment-open
)
653 (xmltok-scan-after-comment-open))
654 ((xmltok-after-lt start empty-tag-slash
)
655 (setq xmltok-name-end
656 (xmltok-after-lt end start-tag-name
))
657 (setq xmltok-name-colon
658 (xmltok-after-lt start start-tag-colon
))
659 (setq xmltok-attributes nil
)
660 (setq xmltok-namespace-attributes nil
)
661 (xmltok-add-error "Expected `/>'" (1- (point)))
662 (setq xmltok-type
'partial-empty-element
))
663 ((xmltok-after-lt start start-tag-name
)
664 (xmltok-add-error "Missing `>'"
667 (setq xmltok-name-end
668 (xmltok-after-lt end start-tag-name
))
669 (setq xmltok-name-colon
670 (xmltok-after-lt start start-tag-colon
))
671 (setq xmltok-namespace-attributes nil
)
672 (setq xmltok-attributes nil
)
673 (setq xmltok-type
'partial-start-tag
))
674 ((xmltok-after-lt start end-tag-name
)
675 (setq xmltok-name-end
(xmltok-after-lt end end-tag-name
))
676 (setq xmltok-name-colon
677 (xmltok-after-lt start end-tag-colon
))
678 (cond ((and (not xmltok-name-colon
)
679 (eq (char-after) ?
:))
680 (goto-char (1+ (point)))
681 (xmltok-add-error "Expected name following `:'"
684 (xmltok-add-error "Missing `>'"
687 (setq xmltok-type
'partial-end-tag
))
688 ((xmltok-after-lt start end-tag-slash
)
689 (xmltok-add-error "Expected name following `</'")
690 (setq xmltok-name-end nil
)
691 (setq xmltok-name-colon nil
)
692 (setq xmltok-type
'partial-end-tag
))
693 ((xmltok-after-lt start marked-section-open
)
694 (xmltok-add-error "Expected `CDATA[' after `<!['"
697 (setq xmltok-type
'not-well-formed
))
698 ((xmltok-after-lt start comment-first-dash
)
699 (xmltok-add-error "Expected `-' after `<!-'"
702 (setq xmltok-type
'not-well-formed
))
703 ((xmltok-after-lt start markup-declaration
)
704 (xmltok-add-error "Expected `[CDATA[' or `--' after `<!'"
707 (setq xmltok-type
'not-well-formed
))
709 (xmltok-add-error "Not well-formed")
710 (setq xmltok-type
'not-well-formed
))))))
712 ;; XXX This should be unified with
713 ;; xmltok-scan-prolog-after-processing-instruction-open
714 ;; XXX maybe should include rest of line (up to any <,>) in unclosed PI
715 (defun xmltok-scan-after-processing-instruction-open ()
716 (search-forward "?>" nil
'move
)
717 (cond ((not (save-excursion
718 (goto-char (+ 2 xmltok-start
))
719 (and (looking-at (xmltok-ncname regexp
))
720 (setq xmltok-name-end
(match-end 0)))))
721 (setq xmltok-name-end
(+ xmltok-start
2))
722 (xmltok-add-error "<? not followed by name"
725 ((not (or (memq (char-after xmltok-name-end
)
727 (= xmltok-name-end
(- (point) 2))))
728 (xmltok-add-error "Target not followed by whitespace"
730 (1+ xmltok-name-end
)))
731 ((and (= xmltok-name-end
(+ xmltok-start
5))
733 (goto-char (+ xmltok-start
2))
734 (let ((case-fold-search t
))
735 (looking-at "xml"))))
736 (xmltok-add-error "Processing instruction target is xml"
738 (+ xmltok-start
5))))
739 (setq xmltok-type
'processing-instruction
))
741 (defun xmltok-scan-after-comment-open ()
742 (let ((found-- (search-forward "--" nil
'move
)))
744 (cond ((or (eq (char-after) ?
>) (not found--
))
745 (goto-char (1+ (point)))
748 ;; just include the <!-- in the token
749 (goto-char (+ xmltok-start
4))
750 ;; Need do this after the goto-char because
751 ;; marked error should just apply to <!--
752 (xmltok-add-error "First following `--' not followed by `>'")
753 'not-well-formed
)))))
755 (defun xmltok-scan-attributes ()
756 (let ((recovering nil
)
757 (atts-needing-normalization nil
))
758 (while (cond ((or (looking-at (xmltok-attribute regexp
))
759 ;; use non-greedy group
760 (when (looking-at (concat "[^<>\n]+?"
761 (xmltok-attribute regexp
)))
763 (xmltok-add-error "Malformed attribute"
766 (goto-char (xmltok-attribute start
768 (skip-chars-backward "\r\n\t ")
771 (setq recovering nil
)
772 (goto-char (match-end 0))
773 (let ((att (xmltok-add-attribute)))
775 (setq atts-needing-normalization
776 (cons att atts-needing-normalization
))))
777 (cond ((xmltok-attribute start start-tag-s
) t
)
778 ((xmltok-attribute start start-tag-close
)
779 (setq xmltok-type
'start-tag
)
781 ((xmltok-attribute start empty-tag-close
)
782 (setq xmltok-type
'empty-element
)
784 ((xmltok-attribute start empty-tag-slash
)
785 (setq xmltok-type
'partial-empty-element
)
786 (xmltok-add-error "Expected `/>'"
789 ((looking-at "[ \t\r\n]*[\"']")
790 (goto-char (match-end 0))
791 (xmltok-add-error "Missing closing delimiter"
795 ((looking-at "[ \t]*\\([^ \t\r\n\"'=<>/]+\\)[ \t\r\n/>]")
796 (goto-char (match-end 1))
797 (xmltok-add-error "Attribute value not quoted"
802 (xmltok-add-error "Missing attribute value"
806 ((looking-at "[^<>\n]*/>")
807 (let ((start (point)))
808 (goto-char (match-end 0))
810 (xmltok-add-error "Malformed empty-element"
813 (setq xmltok-type
'empty-element
)
815 ((looking-at "[^<>\n]*>")
816 (let ((start (point)))
817 (goto-char (match-end 0))
819 (xmltok-add-error "Malformed start-tag"
822 (setq xmltok-type
'start-tag
)
826 (skip-chars-forward "^<>\n"))
827 (xmltok-add-error "Missing `>'"
830 (setq xmltok-type
'partial-start-tag
)
832 (while atts-needing-normalization
833 (xmltok-normalize-attribute (car atts-needing-normalization
))
834 (setq atts-needing-normalization
(cdr atts-needing-normalization
))))
835 (setq xmltok-attributes
836 (nreverse xmltok-attributes
))
837 (setq xmltok-namespace-attributes
838 (nreverse xmltok-namespace-attributes
)))
840 (defun xmltok-add-attribute ()
841 "Return the attribute if it needs normalizing, otherwise nil."
842 (let* ((needs-normalizing nil
)
844 (if (xmltok-attribute start literal
)
846 (setq needs-normalizing
847 (or (xmltok-attribute start complex1
)
848 (xmltok-attribute start complex2
)))
849 (xmltok-make-attribute (xmltok-attribute start name
)
850 (xmltok-attribute start colon
)
851 (xmltok-attribute end name
)
852 (1+ (xmltok-attribute start literal
))
853 (1- (xmltok-attribute end literal
))
854 (not needs-normalizing
)))
855 (xmltok-make-attribute (xmltok-attribute start name
)
856 (xmltok-attribute start colon
)
857 (xmltok-attribute end name
)))))
858 (if (xmltok-attribute start xmlns
)
859 (setq xmltok-namespace-attributes
860 (cons att xmltok-namespace-attributes
))
861 (setq xmltok-attributes
862 (cons att xmltok-attributes
)))
863 (and needs-normalizing
866 (defun xmltok-normalize-attribute (att)
867 (let ((end (xmltok-attribute-value-end att
))
872 (goto-char (xmltok-attribute-value-start att
))
874 (let ((n (skip-chars-forward "^\r\t\n&" end
)))
877 (cons (buffer-substring-no-properties (- (point) n
)
880 (when (< (point) end
)
881 (goto-char (1+ (point)))
882 (cond ((eq (char-before) ?\
&)
883 (let ((xmltok-start (1- (point)))
884 xmltok-type xmltok-replacement
)
885 (xmltok-scan-after-amp
887 (xmltok-handle-entity start end t
)))
888 (cond ((or (eq xmltok-type
'char-ref
)
889 (eq xmltok-type
'entity-ref
))
891 (cons (vector xmltok-type
895 (if xmltok-replacement
897 (cons xmltok-replacement
899 (setq well-formed nil
)))
900 (t (setq well-formed nil
)))))
902 (cons " " value-parts
)))))
905 (aset att
5 (apply 'concat
(nreverse value-parts
))))
906 (aset att
6 (nreverse refs
))))
908 (defun xmltok-scan-after-amp (entity-handler)
909 (cond ((not (looking-at (xmltok-after-amp regexp
)))
910 (xmltok-add-error "`&' that is not markup must be entered as `&'")
911 (setq xmltok-type
'not-well-formed
))
913 (goto-char (match-end 0))
914 (cond ((xmltok-after-amp start entity-ref-close
)
915 (funcall entity-handler
916 (xmltok-after-amp start entity-name
)
917 (xmltok-after-amp end entity-name
))
918 (setq xmltok-type
'entity-ref
))
919 ((xmltok-after-amp start decimal-ref-close
)
920 (xmltok-scan-char-ref (xmltok-after-amp start decimal
)
921 (xmltok-after-amp end decimal
)
923 ((xmltok-after-amp start hex-ref-close
)
924 (xmltok-scan-char-ref (xmltok-after-amp start hex
)
925 (xmltok-after-amp end hex
)
927 ((xmltok-after-amp start number-sign
)
928 (xmltok-add-error "Missing character number")
929 (setq xmltok-type
'not-well-formed
))
931 (xmltok-add-error "Missing closing `;'")
932 (setq xmltok-type
'not-well-formed
))))))
934 (defconst xmltok-entity-error-messages
935 '((unparsed .
"Referenced entity is unparsed")
936 (not-well-formed .
"Referenced entity is not well-formed")
937 (external nil .
"Referenced entity is external")
938 (element nil .
"Referenced entity contains <")))
940 (defun xmltok-handle-entity (start end
&optional attributep
)
941 (let* ((name (buffer-substring-no-properties start end
))
942 (name-def (assoc name xmltok-dtd
))
943 (def (cdr name-def
)))
944 (cond ((setq xmltok-replacement
(and (consp def
)
949 (unless (eq (car xmltok-dtd
) t
)
950 (xmltok-add-error "Referenced entity has not been defined"
953 ((and attributep
(consp def
))
954 (xmltok-add-error "Referenced entity contains <"
958 (let ((err (cdr (assq def xmltok-entity-error-messages
))))
960 (setq err
(if attributep
(cdr err
) (car err
))))
962 (xmltok-add-error err start end
)))))))
964 (defun xmltok-scan-char-ref (start end base
)
965 (setq xmltok-replacement
966 (let ((n (string-to-number (buffer-substring-no-properties start end
)
968 (cond ((and (integerp n
) (xmltok-valid-char-p n
))
969 (setq n
(xmltok-unicode-to-char n
))
972 (xmltok-add-error "Invalid character code" start end
)
974 (setq xmltok-type
'char-ref
))
976 (defun xmltok-char-number (start end
)
977 (let* ((base (if (eq (char-after (+ start
2)) ?x
)
981 (buffer-substring-no-properties (+ start
(if (= base
16) 3 2))
985 (xmltok-valid-char-p n
)
988 (defun xmltok-valid-char-p (n)
989 "Return non-nil if N is the Unicode code of a valid XML character."
990 (cond ((< n
#x20
) (memq n
'(#xA
#xD
#x9
)))
997 (defun xmltok-unicode-to-char (n)
998 "Return the character corresponding to Unicode scalar value N.
999 Return nil if unsupported in Emacs."
1000 (decode-char 'ucs n
))
1004 (defvar xmltok-contains-doctype nil
)
1005 (defvar xmltok-doctype-external-subset-flag nil
)
1006 (defvar xmltok-internal-subset-start nil
)
1007 (defvar xmltok-had-param-entity-ref nil
)
1008 (defvar xmltok-prolog-regions nil
)
1009 (defvar xmltok-standalone nil
1010 "Non-nil if there was an XML declaration specifying standalone=\"yes\".")
1011 (defvar xmltok-markup-declaration-doctype-flag nil
)
1013 (defconst xmltok-predefined-entity-alist
1018 ("quot" "\"" .
"\"")))
1020 (defun xmltok-forward-prolog ()
1021 "Move forward to the end of the XML prolog.
1023 Returns a list of vectors [TYPE START END] where TYPE is a symbol and
1024 START and END are integers giving the start and end of the region of
1025 that type. TYPE can be one of xml-declaration,
1026 xml-declaration-attribute-name, xml-declaration-attribute-value,
1027 comment, processing-instruction-left, processing-instruction-right,
1028 markup-declaration-open, markup-declaration-close,
1029 internal-subset-open, internal-subset-close, hash-name, keyword,
1030 literal, encoding-name.
1031 Adds to `xmltok-errors' as appropriate."
1032 (let ((case-fold-search nil
)
1035 xmltok-prolog-regions
1036 xmltok-contains-doctype
1037 xmltok-internal-subset-start
1038 xmltok-had-param-entity-ref
1040 xmltok-doctype-external-subset-flag
1041 xmltok-markup-declaration-doctype-flag
)
1042 (setq xmltok-dtd xmltok-predefined-entity-alist
)
1043 (xmltok-scan-xml-declaration)
1044 (xmltok-next-prolog-token)
1045 (while (condition-case nil
1046 (when (xmltok-parse-prolog-item)
1047 (xmltok-next-prolog-token))
1048 (xmltok-markup-declaration-parse-error
1049 (xmltok-skip-markup-declaration))))
1050 (when xmltok-internal-subset-start
1051 (xmltok-add-error "No closing ]"
1052 (1- xmltok-internal-subset-start
)
1053 xmltok-internal-subset-start
))
1054 (xmltok-parse-entities)
1055 (nreverse xmltok-prolog-regions
)))
1057 (defconst xmltok-bad-xml-decl-regexp
1058 "[ \t\r\n]*<\\?xml\\(?:[ \t\r\n]\\|\\?>\\)")
1061 (defun xmltok-get-declared-encoding-position (&optional limit
)
1062 "Return the position of the encoding in the XML declaration at point.
1063 If there is a well-formed XML declaration starting at point and it
1064 contains an encoding declaration, then return (START . END)
1065 where START and END are the positions of the start and the end
1066 of the encoding name; if there is no encoding declaration return
1067 the position where and encoding declaration could be inserted.
1068 If there is XML that is not well-formed that looks like an XML
1069 declaration, return nil. Otherwise, return t.
1070 If LIMIT is non-nil, then do not consider characters beyond LIMIT."
1071 (cond ((let ((case-fold-search nil
))
1072 (and (looking-at (xmltok-xml-declaration regexp
))
1073 (or (not limit
) (<= (match-end 0) limit
))))
1074 (let ((end (xmltok-xml-declaration end encoding-value
)))
1076 (cons (1+ (xmltok-xml-declaration start encoding-value
))
1078 (or (xmltok-xml-declaration end version-value
)
1080 ((not (let ((case-fold-search t
))
1081 (looking-at xmltok-bad-xml-decl-regexp
))))))
1083 (defun xmltok-scan-xml-declaration ()
1084 (when (looking-at (xmltok-xml-declaration regexp
))
1085 (xmltok-add-prolog-region 'xml-declaration
(point) (match-end 0))
1086 (goto-char (match-end 0))
1087 (when (xmltok-xml-declaration start version-name
)
1088 (xmltok-add-prolog-region 'xml-declaration-attribute-name
1089 (xmltok-xml-declaration start version-name
)
1090 (xmltok-xml-declaration end version-name
))
1091 (let ((start (xmltok-xml-declaration start version-value
))
1092 (end (xmltok-xml-declaration end version-value
)))
1093 (xmltok-add-prolog-region 'xml-declaration-attribute-value
1096 ;; XXX need to check encoding name
1097 ;; Should start with letter, not contain colon
1098 (when (xmltok-xml-declaration start encoding-name
)
1099 (xmltok-add-prolog-region 'xml-declaration-attribute-name
1100 (xmltok-xml-declaration start encoding-name
)
1101 (xmltok-xml-declaration end encoding-name
))
1102 (let ((start (xmltok-xml-declaration start encoding-value
))
1103 (end (xmltok-xml-declaration end encoding-value
)))
1104 (xmltok-add-prolog-region 'encoding-name
1107 (xmltok-add-prolog-region 'xml-declaration-attribute-value
1110 (when (xmltok-xml-declaration start standalone-name
)
1111 (xmltok-add-prolog-region 'xml-declaration-attribute-name
1112 (xmltok-xml-declaration start standalone-name
)
1113 (xmltok-xml-declaration end standalone-name
))
1114 (let ((start (xmltok-xml-declaration start standalone-value
))
1115 (end (xmltok-xml-declaration end standalone-value
)))
1116 (xmltok-add-prolog-region 'xml-declaration-attribute-value
1119 (setq xmltok-standalone
1120 (string= (buffer-substring-no-properties (1+ start
) (1- end
))
1124 (defconst xmltok-markup-declaration-alist
1125 '(("ELEMENT" . xmltok-parse-element-declaration
)
1126 ("ATTLIST" . xmltok-parse-attlist-declaration
)
1127 ("ENTITY" . xmltok-parse-entity-declaration
)
1128 ("NOTATION" . xmltok-parse-notation-declaration
)))
1130 (defun xmltok-parse-prolog-item ()
1131 (cond ((eq xmltok-type
'comment
)
1132 (xmltok-add-prolog-region 'comment
1136 ((eq xmltok-type
'processing-instruction
))
1137 ((eq xmltok-type
'named-markup-declaration
)
1138 (setq xmltok-markup-declaration-doctype-flag nil
)
1139 (xmltok-add-prolog-region 'markup-declaration-open
1142 (let* ((name (buffer-substring-no-properties
1145 (fun (cdr (assoc name xmltok-markup-declaration-alist
))))
1147 (unless xmltok-internal-subset-start
1149 "Declaration allowed only in internal subset"))
1151 ((string= name
"DOCTYPE")
1152 (xmltok-parse-doctype))
1154 (xmltok-add-error "Unknown markup declaration"
1156 (xmltok-next-prolog-token)
1157 (xmltok-markup-declaration-parse-error))))
1159 ((or (eq xmltok-type
'end-prolog
)
1162 ((eq xmltok-type
'internal-subset-close
)
1163 (xmltok-add-prolog-region 'internal-subset-close
1166 (xmltok-add-prolog-region 'markup-declaration-close
1169 (if xmltok-internal-subset-start
1170 (setq xmltok-internal-subset-start nil
)
1171 (xmltok-add-error "]> outside internal subset"))
1173 ((eq xmltok-type
'param-entity-ref
)
1174 (if xmltok-internal-subset-start
1175 (setq xmltok-had-param-entity-ref t
)
1176 (xmltok-add-error "Parameter entity reference outside document type declaration"))
1178 ;; If we don't do this, we can get thousands of errors when
1179 ;; a plain text file is parsed.
1180 ((not xmltok-internal-subset-start
)
1181 (when (let ((err (car xmltok-errors
)))
1183 (<= (xmltok-error-end err
) xmltok-start
)))
1184 (goto-char xmltok-start
))
1186 ((eq xmltok-type
'not-well-formed
) t
)
1188 (xmltok-add-error "Token allowed only inside markup declaration")
1191 (defun xmltok-parse-doctype ()
1192 (setq xmltok-markup-declaration-doctype-flag t
)
1193 (xmltok-next-prolog-token)
1194 (when xmltok-internal-subset-start
1195 (xmltok-add-error "DOCTYPE declaration not allowed in internal subset")
1196 (xmltok-markup-declaration-parse-error))
1197 (when xmltok-contains-doctype
1198 (xmltok-add-error "Duplicate DOCTYPE declaration")
1199 (xmltok-markup-declaration-parse-error))
1200 (setq xmltok-contains-doctype t
)
1201 (xmltok-require-token 'name
'prefixed-name
)
1202 (xmltok-require-next-token "SYSTEM" "PUBLIC" ?\
[ ?
>)
1203 (cond ((eq xmltok-type ?\
[)
1204 (setq xmltok-internal-subset-start
(point)))
1205 ((eq xmltok-type ?
>))
1207 (setq xmltok-doctype-external-subset-flag t
)
1208 (xmltok-parse-external-id)
1209 (xmltok-require-token ?\
[ ?
>)
1210 (when (eq xmltok-type ?\
[)
1211 (setq xmltok-internal-subset-start
(point))))))
1213 (defun xmltok-parse-attlist-declaration ()
1214 (xmltok-require-next-token 'prefixed-name
'name
)
1216 (xmltok-require-next-token ?
> 'name
'prefixed-name
)
1217 (if (eq xmltok-type ?
>)
1219 (xmltok-require-next-token ?\
(
1229 (cond ((eq xmltok-type ?\
()
1230 (xmltok-parse-nmtoken-group))
1231 ((string= (xmltok-current-token-string)
1233 (xmltok-require-next-token ?\
()
1234 (xmltok-parse-nmtoken-group)))
1235 (xmltok-require-next-token "#IMPLIED"
1239 (when (string= (xmltok-current-token-string) "#FIXED")
1240 (xmltok-require-next-token 'literal
))
1243 (defun xmltok-parse-nmtoken-group ()
1245 (xmltok-require-next-token 'nmtoken
'prefixed-name
'name
)
1246 (xmltok-require-next-token ?| ?\
))
1247 (eq xmltok-type ?|
))))
1249 (defun xmltok-parse-element-declaration ()
1250 (xmltok-require-next-token 'name
'prefixed-name
)
1251 (xmltok-require-next-token "EMPTY" "ANY" ?\
()
1252 (when (eq xmltok-type ?\
()
1253 (xmltok-require-next-token "#PCDATA"
1258 (cond ((eq xmltok-type
'hash-name
)
1259 (xmltok-require-next-token ?| ?\
) 'close-paren-star
)
1260 (while (eq xmltok-type ?|
)
1261 (xmltok-require-next-token 'name
'prefixed-name
)
1262 (xmltok-require-next-token 'close-paren-star ?|
)))
1263 (t (xmltok-parse-model-group))))
1264 (xmltok-require-next-token ?
>))
1266 (defun xmltok-parse-model-group ()
1267 (xmltok-parse-model-group-member)
1268 (xmltok-require-next-token ?|
1273 (when (memq xmltok-type
'(?
, ?|
))
1274 (let ((connector xmltok-type
))
1276 (xmltok-next-prolog-token)
1277 (xmltok-parse-model-group-member)
1278 (xmltok-require-next-token connector
1282 (eq xmltok-type connector
))))))
1284 (defun xmltok-parse-model-group-member ()
1285 (xmltok-require-token 'name
1289 (when (eq xmltok-type ?\
()
1290 (xmltok-next-prolog-token)
1291 (xmltok-parse-model-group)))
1293 (defun xmltok-parse-entity-declaration ()
1295 (xmltok-require-next-token 'name ?%
)
1296 (when (eq xmltok-type ?%
)
1298 (xmltok-require-next-token 'name
))
1299 (setq name
(xmltok-current-token-string))
1300 (xmltok-require-next-token 'literal
"SYSTEM" "PUBLIC")
1301 (cond ((eq xmltok-type
'literal
)
1302 (let ((replacement (xmltok-parse-entity-value)))
1304 (xmltok-define-entity name replacement
)))
1305 (xmltok-require-next-token ?
>))
1307 (xmltok-parse-external-id)
1309 (xmltok-require-token ?
>)
1310 (xmltok-require-token ?
> "NDATA")
1311 (if (eq xmltok-type ?
>)
1312 (xmltok-define-entity name
'external
)
1313 (xmltok-require-next-token 'name
)
1314 (xmltok-require-next-token ?
>)
1315 (xmltok-define-entity name
'unparsed
)))))))
1317 (defun xmltok-define-entity (name value
)
1318 (when (and (or (not xmltok-had-param-entity-ref
)
1320 (not (assoc name xmltok-dtd
)))
1322 (cons (cons name value
) xmltok-dtd
))))
1324 (defun xmltok-parse-entity-value ()
1325 (let ((lim (1- (point)))
1330 (goto-char (1+ xmltok-start
))
1331 (setq start
(point))
1333 (skip-chars-forward "^%&" lim
)
1334 (when (< (point) lim
)
1335 (goto-char (1+ (point)))
1336 (cond ((eq (char-before) ?%
)
1337 (xmltok-add-error "Parameter entity references are not allowed in the internal subset"
1340 (setq well-formed nil
))
1342 (let ((xmltok-start (1- (point)))
1343 xmltok-type xmltok-replacement
)
1344 (xmltok-scan-after-amp (lambda (_start _end
)))
1345 (cond ((eq xmltok-type
'char-ref
)
1347 (cons (buffer-substring-no-properties
1352 (cons xmltok-replacement
1354 (setq start
(point)))
1355 ((eq xmltok-type
'not-well-formed
)
1356 (setq well-formed nil
))))))
1358 (if (not well-formed
)
1361 (nreverse (cons (buffer-substring-no-properties start lim
)
1364 (defun xmltok-parse-notation-declaration ()
1365 (xmltok-require-next-token 'name
)
1366 (xmltok-require-next-token "SYSTEM" "PUBLIC")
1367 (let ((publicp (string= (xmltok-current-token-string) "PUBLIC")))
1368 (xmltok-require-next-token 'literal
)
1370 (xmltok-require-next-token 'literal ?
>)
1371 (unless (eq xmltok-type ?
>)
1372 (xmltok-require-next-token ?
>)))
1373 (t (xmltok-require-next-token ?
>)))))
1375 (defun xmltok-parse-external-id ()
1376 (xmltok-require-token "SYSTEM" "PUBLIC")
1377 (let ((publicp (string= (xmltok-current-token-string) "PUBLIC")))
1378 (xmltok-require-next-token 'literal
)
1380 (xmltok-require-next-token 'literal
)))
1381 (xmltok-next-prolog-token))
1383 (defun xmltok-require-next-token (&rest types
)
1384 (xmltok-next-prolog-token)
1385 (apply 'xmltok-require-token types
))
1387 (defun xmltok-require-token (&rest types
)
1388 ;; XXX Generate a more helpful error message
1389 (while (and (not (let ((type (car types
)))
1390 (if (stringp (car types
))
1391 (string= (xmltok-current-token-string) type
)
1392 (eq type xmltok-type
))))
1393 (setq types
(cdr types
))))
1395 (when (and xmltok-type
1396 (not (eq xmltok-type
'not-well-formed
)))
1397 (xmltok-add-error "Unexpected token"))
1398 (xmltok-markup-declaration-parse-error))
1399 (let ((region-type (xmltok-prolog-region-type (car types
))))
1401 (xmltok-add-prolog-region region-type
1405 (defun xmltok-current-token-string ()
1406 (buffer-substring-no-properties xmltok-start
(point)))
1408 (define-error 'xmltok-markup-declaration-parse-error
1409 "Syntax error in markup declaration")
1411 (defun xmltok-markup-declaration-parse-error ()
1412 (signal 'xmltok-markup-declaration-parse-error nil
))
1414 (defun xmltok-skip-markup-declaration ()
1415 (while (cond ((eq xmltok-type ?
>)
1416 (xmltok-next-prolog-token)
1418 ((and xmltok-markup-declaration-doctype-flag
1419 (eq xmltok-type ?\
[))
1420 (setq xmltok-internal-subset-start
(point))
1421 (xmltok-next-prolog-token)
1423 ((memq xmltok-type
'(nil
1425 named-markup-declaration
1427 processing-instruction
))
1429 ((and xmltok-internal-subset-start
1430 (eq xmltok-type
'internal-subset-close
))
1432 (t (xmltok-next-prolog-token) t
)))
1435 (defun xmltok-prolog-region-type (required)
1436 (cond ((cdr (assq xmltok-type
1437 '((literal . literal
)
1438 (?
> . markup-declaration-close
)
1439 (?\
[ . internal-subset-open
)
1440 (hash-name . hash-name
)))))
1441 ((and (stringp required
) (eq xmltok-type
'name
))
1444 ;; Return new token type.
1446 (defun xmltok-next-prolog-token ()
1447 (skip-chars-forward " \t\r\n")
1448 (setq xmltok-start
(point))
1449 (cond ((not (and (looking-at (xmltok-prolog regexp
))
1450 (goto-char (match-end 0))))
1451 (let ((ch (char-after)))
1453 (goto-char (1+ (point)))
1454 (xmltok-add-error "Illegal char in prolog")
1455 (setq xmltok-type
'not-well-formed
))
1456 (t (setq xmltok-type nil
)))))
1457 ((or (xmltok-prolog start ncname-occur
)
1458 (xmltok-prolog start colon-name-occur
))
1459 (setq xmltok-name-end
(1- (point)))
1460 (setq xmltok-name-colon
(xmltok-prolog start colon
))
1461 (setq xmltok-type
'name-occur
))
1462 ((xmltok-prolog start colon
)
1463 (setq xmltok-name-end
(point))
1464 (setq xmltok-name-colon
(xmltok-prolog start colon
))
1465 (unless (looking-at "[ \t\r\n>),|[%]")
1466 (xmltok-add-error "Missing space after name"))
1467 (setq xmltok-type
'prefixed-name
))
1468 ((or (xmltok-prolog start ncname-nmtoken
)
1469 (xmltok-prolog start ncname-colon
))
1470 (unless (looking-at "[ \t\r\n>),|[%]")
1471 (xmltok-add-error "Missing space after name token"))
1472 (setq xmltok-type
'nmtoken
))
1473 ((xmltok-prolog start name
)
1474 (setq xmltok-name-end
(point))
1475 (setq xmltok-name-colon nil
)
1476 (unless (looking-at "[ \t\r\n>),|[%]")
1477 (xmltok-add-error "Missing space after name"))
1478 (setq xmltok-type
'name
))
1479 ((xmltok-prolog start hash-name
)
1480 (setq xmltok-name-end
(point))
1481 (unless (looking-at "[ \t\r\n>)|%]")
1482 (xmltok-add-error "Missing space after name"))
1483 (setq xmltok-type
'hash-name
))
1484 ((xmltok-prolog start processing-instruction-question
)
1485 (xmltok-scan-prolog-after-processing-instruction-open))
1486 ((xmltok-prolog start comment-open
)
1487 ;; XXX if not-well-formed, ignore some stuff
1488 (xmltok-scan-after-comment-open))
1489 ((xmltok-prolog start named-markup-declaration
)
1490 (setq xmltok-type
'named-markup-declaration
))
1491 ((xmltok-prolog start instance-start
)
1492 (goto-char xmltok-start
)
1493 (setq xmltok-type
'end-prolog
))
1494 ((xmltok-prolog start close-paren-star
)
1495 (setq xmltok-type
'close-paren-star
))
1496 ((xmltok-prolog start close-paren-occur
)
1497 (setq xmltok-type
'close-paren-occur
))
1498 ((xmltok-prolog start close-paren
)
1499 (unless (looking-at "[ \t\r\n>,|)]")
1500 (xmltok-add-error "Missing space after )"))
1501 (setq xmltok-type ?\
)))
1502 ((xmltok-prolog start single-char
)
1503 (let ((ch (char-before)))
1504 (cond ((memq ch
'(?
\" ?
\'))
1505 (xmltok-scan-prolog-literal))
1506 (t (setq xmltok-type ch
)))))
1507 ((xmltok-prolog start percent
)
1508 (cond ((xmltok-prolog start param-entity-ref-close
)
1509 (setq xmltok-name-end
(1- (point)))
1510 (setq xmltok-type
'param-entity-ref
))
1511 ((xmltok-prolog start param-entity-ref
)
1512 (xmltok-add-error "Missing ;")
1513 (setq xmltok-name-end
(point))
1514 (setq xmltok-type
'param-entity-ref
))
1515 ((looking-at "[ \t\r\n%]")
1516 (setq xmltok-type ?%
))
1518 (xmltok-add-error "Expected name after %")
1519 (setq xmltok-type
'not-well-formed
))))
1520 ((xmltok-prolog start nmtoken
)
1521 (unless (looking-at "[ \t\r\n>),|[%]")
1522 (xmltok-add-error "Missing space after name token"))
1523 (setq xmltok-type
'nmtoken
))
1524 ((xmltok-prolog start internal-subset-close
)
1525 (setq xmltok-type
'internal-subset-close
))
1526 ((xmltok-prolog start pound
)
1527 (xmltok-add-error "Expected name after #")
1528 (setq xmltok-type
'not-well-formed
))
1529 ((xmltok-prolog start markup-declaration
)
1530 (xmltok-add-error "Expected name or -- after <!")
1531 (setq xmltok-type
'not-well-formed
))
1532 ((xmltok-prolog start comment-first-dash
)
1533 (xmltok-add-error "Expected <!--")
1534 (setq xmltok-type
'not-well-formed
))
1535 ((xmltok-prolog start less-than
)
1536 (xmltok-add-error "Incomplete markup")
1537 (setq xmltok-type
'not-well-formed
))
1538 (t (error "Unhandled token in prolog %s"
1539 (match-string-no-properties 0)))))
1541 (defun xmltok-scan-prolog-literal ()
1542 (let* ((delim (string (char-before)))
1543 (safe-end (save-excursion
1544 (skip-chars-forward (concat "^<>[]" delim
))
1546 (end (save-excursion
1547 (goto-char safe-end
)
1548 (search-forward delim nil t
))))
1549 (cond ((or (not end
)
1552 (looking-at "[ \t\r\n>%[]")))
1554 ((eq (1+ safe-end
) end
)
1556 (xmltok-add-error (format "Missing space after %s" delim
)
1558 (setq xmltok-type
'literal
)))
1560 (defun xmltok-scan-prolog-after-processing-instruction-open ()
1561 (search-forward "?>" nil
'move
)
1562 (let* ((end (point))
1565 (goto-char (+ xmltok-start
2))
1566 (and (looking-at (xmltok-ncname regexp
))
1567 (or (memq (char-after (match-end 0))
1569 (= (match-end 0) (- end
2)))
1570 (match-string-no-properties 0)))))
1572 (xmltok-add-error "\
1573 Processing instruction does not start with a name"
1575 (+ xmltok-start
3)))
1576 ((not (and (= (length target
) 3)
1577 (let ((case-fold-search t
))
1578 (string-match "xml" target
)))))
1580 (xmltok-add-error "Invalid XML declaration"
1584 (goto-char xmltok-start
)
1585 (looking-at (xmltok-xml-declaration regexp
)))
1586 (xmltok-add-error "XML declaration not at beginning of file"
1590 (xmltok-add-error "Processing instruction has target of xml"
1592 (+ xmltok-start
5))))
1593 (xmltok-add-prolog-region 'processing-instruction-left
1600 (xmltok-add-prolog-region 'processing-instruction-right
1603 (goto-char (+ xmltok-start
1606 (skip-chars-forward " \t\r\n")
1610 (setq xmltok-type
'processing-instruction
))
1612 (defun xmltok-parse-entities ()
1613 (let ((todo xmltok-dtd
))
1614 (when (and (or xmltok-had-param-entity-ref
1615 xmltok-doctype-external-subset-flag
)
1616 (not xmltok-standalone
))
1617 (setq xmltok-dtd
(cons t xmltok-dtd
)))
1619 (xmltok-parse-entity (car todo
))
1620 (setq todo
(cdr todo
)))))
1622 (defun xmltok-parse-entity (name-def)
1623 (let ((def (cdr name-def
))
1624 ;; in case its value is buffer local
1625 (xmltok-dtd xmltok-dtd
)
1628 (if (string-match "\\`[^&<\t\r\n]*\\'" def
)
1629 (setcdr name-def
(cons def def
))
1630 (setcdr name-def
'not-well-formed
) ; avoid infinite expansion loops
1631 (setq buf
(get-buffer-create
1632 (format " *Entity %s*" (car name-def
))))
1633 (with-current-buffer buf
1636 (goto-char (point-min))
1638 (xmltok-parse-entity-replacement)))
1639 (kill-buffer buf
)))))
1641 (defun xmltok-parse-entity-replacement ()
1642 (let ((def (cons "" "")))
1643 (while (let* ((start (point))
1644 (found (re-search-forward "[<&\t\r\n]\\|]]>" nil t
))
1645 (ch (and found
(char-before)))
1646 (str (buffer-substring-no-properties
1652 (xmltok-append-entity-def def
1654 (cond ((not found
) nil
)
1656 (setq def
'not-well-formed
)
1660 (setq xmltok-start
(1- (point)))
1661 (xmltok-scan-after-lt)
1663 (xmltok-append-entity-def
1665 (cond ((memq xmltok-type
1672 processing-instruction
))
1676 (cons (buffer-substring-no-properties
1680 (t 'not-well-formed
)))))
1683 (let ((xmltok-start (1- (point)))
1687 (xmltok-scan-after-amp 'xmltok-handle-nested-entity
)
1688 (cond ((eq xmltok-type
'entity-ref
)
1690 (xmltok-append-entity-def
1692 xmltok-replacement
)))
1693 ((eq xmltok-type
'char-ref
)
1695 (xmltok-append-entity-def
1697 (if xmltok-replacement
1698 (cons xmltok-replacement
1700 (and xmltok-errors
'not-well-formed
)))))
1702 (setq def
'not-well-formed
))))
1706 (xmltok-append-entity-def
1708 (cons (match-string-no-properties 0)
1713 (defun xmltok-handle-nested-entity (start end
)
1714 (let* ((name-def (assoc (buffer-substring-no-properties start end
)
1716 (def (cdr name-def
)))
1718 (xmltok-parse-entity name-def
)
1719 (setq def
(cdr name-def
)))
1720 (setq xmltok-replacement
1721 (cond ((null name-def
)
1722 (if (eq (car xmltok-dtd
) t
)
1725 ((eq def
'unparsed
) 'not-well-formed
)
1728 (defun xmltok-append-entity-def (d1 d2
)
1731 (cons (concat (car d1
) (car d2
))
1734 (concat (cdr d1
) (cdr d2
))))
1738 (let ((defs '(not-well-formed external element
)))
1739 (while (not (or (eq (car defs
) d1
)
1740 (eq (car defs
) d2
)))
1741 (setq defs
(cdr defs
)))
1744 (defun xmltok-add-prolog-region (type start end
)
1745 (setq xmltok-prolog-regions
1746 (cons (vector type start end
)
1747 xmltok-prolog-regions
)))
1749 (defun xmltok-merge-attributes ()
1750 "Return a list merging `xmltok-attributes' and `xmltok-namespace-attributes'.
1751 The members of the merged list are in order of occurrence in the
1752 document. The list may share list structure with `xmltok-attributes'
1753 and `xmltok-namespace-attributes'."
1754 (cond ((not xmltok-namespace-attributes
)
1756 ((not xmltok-attributes
)
1757 xmltok-namespace-attributes
)
1759 (let ((atts1 xmltok-attributes
)
1760 (atts2 xmltok-namespace-attributes
)
1762 (while (and atts1 atts2
)
1763 (cond ((< (xmltok-attribute-name-start (car atts1
))
1764 (xmltok-attribute-name-start (car atts2
)))
1765 (setq merged
(cons (car atts1
) merged
))
1766 (setq atts1
(cdr atts1
)))
1768 (setq merged
(cons (car atts2
) merged
))
1769 (setq atts2
(cdr atts2
)))))
1770 (setq merged
(nreverse merged
))
1771 (cond (atts1 (setq merged
(nconc merged atts1
)))
1772 (atts2 (setq merged
(nconc merged atts2
))))
1777 (defun xmltok-forward-test ()
1779 (if (xmltok-forward)
1780 (message "Scanned %s" xmltok-type
)
1781 (message "Scanned nothing")))
1783 (defun xmltok-next-prolog-token-test ()
1785 (if (xmltok-next-prolog-token)
1786 (message "Scanned %s"
1787 (if (integerp xmltok-type
)
1788 (string xmltok-type
)
1790 (message "Scanned end of file")))
1794 ;;; xmltok.el ends here