1 ;;; xmltok.el --- XML tokenization
3 ;; Copyright (C) 2003, 2007-2013 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.")
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
)
151 xmltok-namespace-attributes
155 (put 'xmltok-save
'lisp-indent-function
0)
156 (def-edebug-spec xmltok-save t
)
158 (defsubst xmltok-attribute-name-start
(att)
161 (defsubst xmltok-attribute-name-colon
(att)
164 (defsubst xmltok-attribute-name-end
(att)
167 (defsubst xmltok-attribute-value-start
(att)
170 (defsubst xmltok-attribute-value-end
(att)
173 (defsubst xmltok-attribute-raw-normalized-value
(att)
174 "Return an object representing the normalized value of ATT.
175 This can be t indicating that the normalized value is the same as
176 the buffer substring from the start to the end of the value, or nil
177 indicating that the value is not well-formed or a string."
180 (defsubst xmltok-attribute-refs
(att)
181 "Return a list of the entity and character references in ATT.
182 Each member is a vector [TYPE START END] where TYPE is either char-ref
183 or entity-ref and START and END are integers giving the start and end of
184 the reference. Nested entity references are not included in the list."
187 (defun xmltok-attribute-prefix (att)
188 (let ((colon (xmltok-attribute-name-colon att
)))
190 (buffer-substring-no-properties (xmltok-attribute-name-start att
)
193 (defun xmltok-attribute-local-name (att)
194 (let ((colon (xmltok-attribute-name-colon att
)))
195 (buffer-substring-no-properties (if colon
197 (xmltok-attribute-name-start att
))
198 (xmltok-attribute-name-end att
))))
200 (defun xmltok-attribute-value (att)
201 (let ((rnv (xmltok-attribute-raw-normalized-value att
)))
205 (buffer-substring-no-properties (xmltok-attribute-value-start att
)
206 (xmltok-attribute-value-end att
))))))
208 (defun xmltok-start-tag-prefix ()
209 (and xmltok-name-colon
210 (buffer-substring-no-properties (1+ xmltok-start
)
213 (defun xmltok-start-tag-local-name ()
214 (buffer-substring-no-properties (1+ (or xmltok-name-colon
218 (defun xmltok-end-tag-prefix ()
219 (and xmltok-name-colon
220 (buffer-substring-no-properties (+ 2 xmltok-start
)
223 (defun xmltok-end-tag-local-name ()
224 (buffer-substring-no-properties (if xmltok-name-colon
225 (1+ xmltok-name-colon
)
229 (defun xmltok-start-tag-qname ()
230 (buffer-substring-no-properties (+ xmltok-start
1) xmltok-name-end
))
232 (defun xmltok-end-tag-qname ()
233 (buffer-substring-no-properties (+ xmltok-start
2) xmltok-name-end
))
235 (defsubst xmltok-make-attribute
(name-begin
241 raw-normalized-value
)
243 RAW-NORMALIZED-VALUE is nil if the value is not well-formed,
244 t if the normalized value is the string between VALUE-BEGIN
245 and VALUE-END, otherwise a STRING giving the value."
254 (defsubst xmltok-error-message
(err)
257 (defsubst xmltok-error-start
(err)
260 (defsubst xmltok-error-end
(err)
263 (defsubst xmltok-make-error
(message start end
)
264 (vector message start end
))
266 (defun xmltok-add-error (message &optional start end
)
268 (cons (xmltok-make-error message
269 (or start xmltok-start
)
273 (defun xmltok-forward ()
274 (setq xmltok-start
(point))
275 (let* ((case-fold-search nil
)
276 (space-count (skip-chars-forward " \t\r\n"))
279 (cond ((> space-count
0)
280 (setq xmltok-type
'space
))
283 (xmltok-scan-after-lt))))
285 (cond ((> space-count
0)
286 (setq xmltok-type
'space
))
289 (xmltok-scan-after-amp 'xmltok-handle-entity
))))
290 ((re-search-forward "[<&]\\|\\(]]>\\)" nil t
)
291 (cond ((not (match-beginning 1))
292 (goto-char (match-beginning 0))
293 ;; must have got a non-space char
294 (setq xmltok-type
'data
))
295 ((= (match-beginning 1) xmltok-start
)
296 (xmltok-add-error "Found `]]>' not closing a CDATA section")
297 (setq xmltok-type
'not-well-formed
))
299 (goto-char (match-beginning 0))
301 (if (= (point) (+ xmltok-start space-count
))
306 (if (> space-count
0)
310 (goto-char (point-max))
311 (setq xmltok-type
'data
)))))
313 (defun xmltok-forward-special (bound)
314 "Scan forward past the first special token starting at or after point.
315 Return nil if there is no special token that starts before BOUND.
316 CDATA sections, processing instructions and comments (and indeed
317 anything starting with < following by ? or !) count as special.
318 Return the type of the token."
319 (when (re-search-forward "<[?!]" (1+ bound
) t
)
320 (setq xmltok-start
(match-beginning 0))
321 (goto-char (1+ xmltok-start
))
322 (let ((case-fold-search nil
))
323 (xmltok-scan-after-lt))))
327 ;; A symbolic regexp is represented by a list whose CAR is the string
328 ;; containing the regexp and whose cdr is a list of symbolic names
329 ;; for the groups in the string.
331 ;; Construct a symbolic regexp from a regexp.
332 (defun xmltok-r (str)
335 ;; Concatenate zero of more regexps and symbolic regexps.
336 (defun xmltok+ (&rest args
)
339 (let ((arg (car args
)))
341 (setq strs
(cons arg strs
))
342 (setq strs
(cons (car arg
) strs
))
343 (setq names
(cons (cdr arg
) names
)))
344 (setq args
(cdr args
))))
345 (cons (apply 'concat
(nreverse strs
))
346 (apply 'append
(nreverse names
))))))
349 ;; Make a symbolic group named NAME from the regexp R.
350 ;; R may be a symbolic regexp or an ordinary regexp.
351 (defmacro xmltok-g
(name &rest r
)
352 (let ((sym (make-symbol "r")))
353 `(let ((,sym
(xmltok+ ,@r
)))
355 (cons (concat "\\(" ,sym
"\\)") (cons ',name nil
))
356 (cons (concat "\\(" (car ,sym
) "\\)") (cons ',name
(cdr ,sym
)))))))
358 (defun xmltok-p (&rest r
) (xmltok+ "\\(?:"
362 ;; Get the group index of ELEM in a LIST of symbols.
363 (defun xmltok-get-index (elem list
)
365 (error "Missing group name"))
369 (cond ((eq elem
(car list
))
374 (setq list
(cdr list
)))))
376 (error "Bad group name %s" elem
))))
378 ;; Define a macro SYM using a symbolic regexp R.
379 ;; SYM can be called in three ways:
381 ;; expands to the regexp in R
384 ;; (match-beginning N)
385 ;; where N is the group index of G in R.
389 ;; where N is the group index of G in R.
390 (defmacro xmltok-defregexp
(sym r
)
393 `(macro lambda
(action &optional group-name
)
394 (cond ((eq action
'regexp
)
396 ((or (eq action
'start
) (eq action
'beginning
))
397 (list 'match-beginning
(xmltok-get-index group-name
400 (list 'match-end
(xmltok-get-index group-name
404 (xmltok-get-index group-name
',(cdr r
))))
405 ((eq action
'string-no-properties
)
406 (list 'match-string-no-properties
407 (xmltok-get-index group-name
',(cdr r
))))
408 (t (error "Invalid action: %s" action
))))))))
416 (name-start-char "[_[:alpha:]]")
417 (name-continue-not-start-char "[-.[:digit:]]")
418 (name-continue-char "[-._[:alnum:]]")
426 (ncname (concat name-start-char name-continue-char
*))
428 (xmltok+ (xmltok-g entity-name ncname
)
429 (xmltok-g entity-ref-close
";") opt
))
431 (xmltok+ (xmltok-g decimal
"[0-9]" +)
432 (xmltok-g decimal-ref-close
";") opt
))
435 (xmltok-g hex
"[0-9a-fA-F]" +)
436 (xmltok-g hex-ref-close
";") opt
439 (xmltok+ (xmltok-g number-sign
"#")
440 open decimal-ref or hex-ref close opt
))
442 (xmltok+ open
(xmltok-g start-tag-close s
* ">")
443 or open
(xmltok-g empty-tag-slash s
* "/")
444 (xmltok-g empty-tag-close
">") opt close
445 or
(xmltok-g start-tag-s s
+)
448 (xmltok+ (xmltok-g start-tag-name
449 ncname
(xmltok-g start-tag-colon
":" ncname
) opt
)
450 start-tag-close opt
))
452 (xmltok+ (xmltok-g end-tag-slash
"/")
453 open
(xmltok-g end-tag-name
455 (xmltok-g end-tag-colon
":" ncname
) opt
)
456 (xmltok-g end-tag-close s
* ">") opt
459 (xmltok+ (xmltok-g markup-declaration
"!")
460 (xmltok-g comment-first-dash
"-"
461 (xmltok-g comment-open
"-") opt
) opt
))
464 (xmltok-g marked-section-open
"\\[")
470 (xmltok-g cdata-section-open
"\\[" ) opt
476 (processing-instruction
477 (xmltok-g processing-instruction-question question
)))
479 (xmltok-defregexp xmltok-ncname
(xmltok+ open ncname close
))
481 (xmltok-defregexp xmltok-after-amp
482 (xmltok+ entity-ref or char-ref
))
483 (xmltok-defregexp xmltok-after-lt
486 ;; cdata-section must come before comment
487 ;; because we treat <! as a comment
488 ;; and Emacs doesn't do fully greedy matching
492 or processing-instruction
))
498 (xmltok-g complex1
"[&\r\n\t][^<']*") opt
500 (lit2 (cons (replace-regexp-in-string "'" "\"" (car lit1
))
502 (literal (xmltok-g literal lit1 or lit2
))
503 (name (xmltok+ open
(xmltok-g xmlns
"xmlns") or ncname close
504 (xmltok-g colon
":" ncname
) opt
)))
505 (xmltok+ (xmltok-g name name
)
507 ;; If the literal isn't followed by what it should be,
508 ;; then the closing delimiter is probably really the
509 ;; opening delimiter of another literal, so don't
510 ;; absorb the literal in this case.
511 open s
* literal start-tag-close close opt
)))
513 xmltok-xml-declaration
514 (let* ((literal-content "[-._:a-zA-Z0-9]+")
516 (concat open
"\"" literal-content
"\""
517 or
"'" literal-content
"'" close
))
520 s
+ (xmltok-g version-name
"version")
522 s
* (xmltok-g version-value literal
)
526 s
+ (xmltok-g encoding-name
"encoding")
528 s
* (xmltok-g encoding-value literal
)
531 (concat open
"yes" or
"no" close
))
534 s
+ (xmltok-g standalone-name
"standalone")
536 s
* (xmltok-g standalone-value
537 "\"" yes-no
"\"" or
"'" yes-no
"'")
539 (xmltok+ "<" question
"xml"
546 (let* ((single-char (xmltok-g single-char
"[[|,(\"'>]"))
547 (internal-subset-close (xmltok-g internal-subset-close
549 (starts-with-close-paren
550 (xmltok-g close-paren
553 (xmltok-g close-paren-occur
"[+?]")
555 (xmltok-g close-paren-star
"\\*"))
559 "%" (xmltok-g param-entity-ref
561 (xmltok-g param-entity-ref-close
563 (starts-with-nmtoken-not-name
565 (xmltok-p name-continue-not-start-char or
":")
566 (xmltok-p name-continue-char or
":") *))
569 (xmltok-p name-continue-not-start-char or
":")
570 (xmltok-p name-continue-char or
":") *
575 (xmltok-p name-continue-char or
":") *))
577 (xmltok+ (xmltok-g ncname-nmtoken
578 ":" (xmltok-p nmtoken-after-colon
))
579 or
(xmltok-p (xmltok-g colon
":" ncname
)
580 (xmltok-g colon-name-occur
"[?+*]") opt
)
581 or
(xmltok-g ncname-occur
"[?+*]")
582 or
(xmltok-g ncname-colon
":")))
584 (xmltok-g name ncname
(xmltok-p after-ncname
) opt
))
587 "#" (xmltok-g hash-name ncname
)))
589 (xmltok-g markup-declaration
590 "!" (xmltok-p (xmltok-g comment-first-dash
"-"
591 (xmltok-g comment-open
"-") opt
)
592 or
(xmltok-g named-markup-declaration
595 (xmltok+ markup-declaration
596 or
(xmltok-g processing-instruction-question
598 or
(xmltok-g instance-start
600 (starts-with-lt (xmltok-g less-than
"<" (xmltok-p after-lt
) opt
)))
601 (xmltok+ starts-with-lt
603 or starts-with-close-paren
604 or starts-with-percent
606 or starts-with-nmtoken-not-name
608 or internal-subset-close
)))))
610 (defconst xmltok-ncname-regexp
(xmltok-ncname regexp
))
612 (defun xmltok-scan-after-lt ()
613 (cond ((not (looking-at (xmltok-after-lt regexp
)))
614 (xmltok-add-error "`<' that is not markup must be entered as `<'")
615 (setq xmltok-type
'not-well-formed
))
617 (goto-char (match-end 0))
618 (cond ((xmltok-after-lt start start-tag-close
)
619 (setq xmltok-name-end
620 (xmltok-after-lt end start-tag-name
))
621 (setq xmltok-name-colon
622 (xmltok-after-lt start start-tag-colon
))
623 (setq xmltok-attributes nil
)
624 (setq xmltok-namespace-attributes nil
)
625 (setq xmltok-type
'start-tag
))
626 ((xmltok-after-lt start end-tag-close
)
627 (setq xmltok-name-end
628 (xmltok-after-lt end end-tag-name
))
629 (setq xmltok-name-colon
630 (xmltok-after-lt start end-tag-colon
))
631 (setq xmltok-type
'end-tag
))
632 ((xmltok-after-lt start start-tag-s
)
633 (setq xmltok-name-end
634 (xmltok-after-lt end start-tag-name
))
635 (setq xmltok-name-colon
636 (xmltok-after-lt start start-tag-colon
))
637 (setq xmltok-namespace-attributes nil
)
638 (setq xmltok-attributes nil
)
639 (xmltok-scan-attributes)
641 ((xmltok-after-lt start empty-tag-close
)
642 (setq xmltok-name-end
643 (xmltok-after-lt end start-tag-name
))
644 (setq xmltok-name-colon
645 (xmltok-after-lt start start-tag-colon
))
646 (setq xmltok-attributes nil
)
647 (setq xmltok-namespace-attributes nil
)
648 (setq xmltok-type
'empty-element
))
649 ((xmltok-after-lt start cdata-section-open
)
651 (progn (search-forward "]]>" nil
'move
)
653 ((xmltok-after-lt start processing-instruction-question
)
654 (xmltok-scan-after-processing-instruction-open))
655 ((xmltok-after-lt start comment-open
)
656 (xmltok-scan-after-comment-open))
657 ((xmltok-after-lt start empty-tag-slash
)
658 (setq xmltok-name-end
659 (xmltok-after-lt end start-tag-name
))
660 (setq xmltok-name-colon
661 (xmltok-after-lt start start-tag-colon
))
662 (setq xmltok-attributes nil
)
663 (setq xmltok-namespace-attributes nil
)
664 (xmltok-add-error "Expected `/>'" (1- (point)))
665 (setq xmltok-type
'partial-empty-element
))
666 ((xmltok-after-lt start start-tag-name
)
667 (xmltok-add-error "Missing `>'"
670 (setq xmltok-name-end
671 (xmltok-after-lt end start-tag-name
))
672 (setq xmltok-name-colon
673 (xmltok-after-lt start start-tag-colon
))
674 (setq xmltok-namespace-attributes nil
)
675 (setq xmltok-attributes nil
)
676 (setq xmltok-type
'partial-start-tag
))
677 ((xmltok-after-lt start end-tag-name
)
678 (setq xmltok-name-end
(xmltok-after-lt end end-tag-name
))
679 (setq xmltok-name-colon
680 (xmltok-after-lt start end-tag-colon
))
681 (cond ((and (not xmltok-name-colon
)
682 (eq (char-after) ?
:))
683 (goto-char (1+ (point)))
684 (xmltok-add-error "Expected name following `:'"
687 (xmltok-add-error "Missing `>'"
690 (setq xmltok-type
'partial-end-tag
))
691 ((xmltok-after-lt start end-tag-slash
)
692 (xmltok-add-error "Expected name following `</'")
693 (setq xmltok-name-end nil
)
694 (setq xmltok-name-colon nil
)
695 (setq xmltok-type
'partial-end-tag
))
696 ((xmltok-after-lt start marked-section-open
)
697 (xmltok-add-error "Expected `CDATA[' after `<!['"
700 (setq xmltok-type
'not-well-formed
))
701 ((xmltok-after-lt start comment-first-dash
)
702 (xmltok-add-error "Expected `-' after `<!-'"
705 (setq xmltok-type
'not-well-formed
))
706 ((xmltok-after-lt start markup-declaration
)
707 (xmltok-add-error "Expected `[CDATA[' or `--' after `<!'"
710 (setq xmltok-type
'not-well-formed
))
712 (xmltok-add-error "Not well-formed")
713 (setq xmltok-type
'not-well-formed
))))))
715 ;; XXX This should be unified with
716 ;; xmltok-scan-prolog-after-processing-instruction-open
717 ;; XXX maybe should include rest of line (up to any <,>) in unclosed PI
718 (defun xmltok-scan-after-processing-instruction-open ()
719 (search-forward "?>" nil
'move
)
720 (cond ((not (save-excursion
721 (goto-char (+ 2 xmltok-start
))
722 (and (looking-at (xmltok-ncname regexp
))
723 (setq xmltok-name-end
(match-end 0)))))
724 (setq xmltok-name-end
(+ xmltok-start
2))
725 (xmltok-add-error "<? not followed by name"
728 ((not (or (memq (char-after xmltok-name-end
)
730 (= xmltok-name-end
(- (point) 2))))
731 (xmltok-add-error "Target not followed by whitespace"
733 (1+ xmltok-name-end
)))
734 ((and (= xmltok-name-end
(+ xmltok-start
5))
736 (goto-char (+ xmltok-start
2))
737 (let ((case-fold-search t
))
738 (looking-at "xml"))))
739 (xmltok-add-error "Processing instruction target is xml"
741 (+ xmltok-start
5))))
742 (setq xmltok-type
'processing-instruction
))
744 (defun xmltok-scan-after-comment-open ()
745 (let ((found-- (search-forward "--" nil
'move
)))
747 (cond ((or (eq (char-after) ?
>) (not found--
))
748 (goto-char (1+ (point)))
751 ;; just include the <!-- in the token
752 (goto-char (+ xmltok-start
4))
753 ;; Need do this after the goto-char because
754 ;; marked error should just apply to <!--
755 (xmltok-add-error "First following `--' not followed by `>'")
756 'not-well-formed
)))))
758 (defun xmltok-scan-attributes ()
759 (let ((recovering nil
)
760 (atts-needing-normalization nil
))
761 (while (cond ((or (looking-at (xmltok-attribute regexp
))
762 ;; use non-greedy group
763 (when (looking-at (concat "[^<>\n]+?"
764 (xmltok-attribute regexp
)))
766 (xmltok-add-error "Malformed attribute"
769 (goto-char (xmltok-attribute start
771 (skip-chars-backward "\r\n\t ")
774 (setq recovering nil
)
775 (goto-char (match-end 0))
776 (let ((att (xmltok-add-attribute)))
778 (setq atts-needing-normalization
779 (cons att atts-needing-normalization
))))
780 (cond ((xmltok-attribute start start-tag-s
) t
)
781 ((xmltok-attribute start start-tag-close
)
782 (setq xmltok-type
'start-tag
)
784 ((xmltok-attribute start empty-tag-close
)
785 (setq xmltok-type
'empty-element
)
787 ((xmltok-attribute start empty-tag-slash
)
788 (setq xmltok-type
'partial-empty-element
)
789 (xmltok-add-error "Expected `/>'"
792 ((looking-at "[ \t\r\n]*[\"']")
793 (goto-char (match-end 0))
794 (xmltok-add-error "Missing closing delimiter"
798 ((looking-at "[ \t]*\\([^ \t\r\n\"'=<>/]+\\)[ \t\r\n/>]")
799 (goto-char (match-end 1))
800 (xmltok-add-error "Attribute value not quoted"
805 (xmltok-add-error "Missing attribute value"
809 ((looking-at "[^<>\n]*/>")
810 (let ((start (point)))
811 (goto-char (match-end 0))
813 (xmltok-add-error "Malformed empty-element"
816 (setq xmltok-type
'empty-element
)
818 ((looking-at "[^<>\n]*>")
819 (let ((start (point)))
820 (goto-char (match-end 0))
822 (xmltok-add-error "Malformed start-tag"
825 (setq xmltok-type
'start-tag
)
829 (skip-chars-forward "^<>\n"))
830 (xmltok-add-error "Missing `>'"
833 (setq xmltok-type
'partial-start-tag
)
835 (while atts-needing-normalization
836 (xmltok-normalize-attribute (car atts-needing-normalization
))
837 (setq atts-needing-normalization
(cdr atts-needing-normalization
))))
838 (setq xmltok-attributes
839 (nreverse xmltok-attributes
))
840 (setq xmltok-namespace-attributes
841 (nreverse xmltok-namespace-attributes
)))
843 (defun xmltok-add-attribute ()
844 "Return the attribute if it needs normalizing, otherwise nil."
845 (let* ((needs-normalizing nil
)
847 (if (xmltok-attribute start literal
)
849 (setq needs-normalizing
850 (or (xmltok-attribute start complex1
)
851 (xmltok-attribute start complex2
)))
852 (xmltok-make-attribute (xmltok-attribute start name
)
853 (xmltok-attribute start colon
)
854 (xmltok-attribute end name
)
855 (1+ (xmltok-attribute start literal
))
856 (1- (xmltok-attribute end literal
))
857 (not needs-normalizing
)))
858 (xmltok-make-attribute (xmltok-attribute start name
)
859 (xmltok-attribute start colon
)
860 (xmltok-attribute end name
)))))
861 (if (xmltok-attribute start xmlns
)
862 (setq xmltok-namespace-attributes
863 (cons att xmltok-namespace-attributes
))
864 (setq xmltok-attributes
865 (cons att xmltok-attributes
)))
866 (and needs-normalizing
869 (defun xmltok-normalize-attribute (att)
870 (let ((end (xmltok-attribute-value-end att
))
875 (goto-char (xmltok-attribute-value-start att
))
877 (let ((n (skip-chars-forward "^\r\t\n&" end
)))
880 (cons (buffer-substring-no-properties (- (point) n
)
883 (when (< (point) end
)
884 (goto-char (1+ (point)))
885 (cond ((eq (char-before) ?\
&)
886 (let ((xmltok-start (1- (point)))
887 xmltok-type xmltok-replacement
)
888 (xmltok-scan-after-amp
890 (xmltok-handle-entity start end t
)))
891 (cond ((or (eq xmltok-type
'char-ref
)
892 (eq xmltok-type
'entity-ref
))
894 (cons (vector xmltok-type
898 (if xmltok-replacement
900 (cons xmltok-replacement
902 (setq well-formed nil
)))
903 (t (setq well-formed nil
)))))
905 (cons " " value-parts
)))))
908 (aset att
5 (apply 'concat
(nreverse value-parts
))))
909 (aset att
6 (nreverse refs
))))
911 (defun xmltok-scan-after-amp (entity-handler)
912 (cond ((not (looking-at (xmltok-after-amp regexp
)))
913 (xmltok-add-error "`&' that is not markup must be entered as `&'")
914 (setq xmltok-type
'not-well-formed
))
916 (goto-char (match-end 0))
917 (cond ((xmltok-after-amp start entity-ref-close
)
918 (funcall entity-handler
919 (xmltok-after-amp start entity-name
)
920 (xmltok-after-amp end entity-name
))
921 (setq xmltok-type
'entity-ref
))
922 ((xmltok-after-amp start decimal-ref-close
)
923 (xmltok-scan-char-ref (xmltok-after-amp start decimal
)
924 (xmltok-after-amp end decimal
)
926 ((xmltok-after-amp start hex-ref-close
)
927 (xmltok-scan-char-ref (xmltok-after-amp start hex
)
928 (xmltok-after-amp end hex
)
930 ((xmltok-after-amp start number-sign
)
931 (xmltok-add-error "Missing character number")
932 (setq xmltok-type
'not-well-formed
))
934 (xmltok-add-error "Missing closing `;'")
935 (setq xmltok-type
'not-well-formed
))))))
937 (defconst xmltok-entity-error-messages
938 '((unparsed .
"Referenced entity is unparsed")
939 (not-well-formed .
"Referenced entity is not well-formed")
940 (external nil .
"Referenced entity is external")
941 (element nil .
"Referenced entity contains <")))
943 (defun xmltok-handle-entity (start end
&optional attributep
)
944 (let* ((name (buffer-substring-no-properties start end
))
945 (name-def (assoc name xmltok-dtd
))
946 (def (cdr name-def
)))
947 (cond ((setq xmltok-replacement
(and (consp def
)
952 (unless (eq (car xmltok-dtd
) t
)
953 (xmltok-add-error "Referenced entity has not been defined"
956 ((and attributep
(consp def
))
957 (xmltok-add-error "Referenced entity contains <"
961 (let ((err (cdr (assq def xmltok-entity-error-messages
))))
963 (setq err
(if attributep
(cdr err
) (car err
))))
965 (xmltok-add-error err start end
)))))))
967 (defun xmltok-scan-char-ref (start end base
)
968 (setq xmltok-replacement
969 (let ((n (string-to-number (buffer-substring-no-properties start end
)
971 (cond ((and (integerp n
) (xmltok-valid-char-p n
))
972 (setq n
(xmltok-unicode-to-char n
))
975 (xmltok-add-error "Invalid character code" start end
)
977 (setq xmltok-type
'char-ref
))
979 (defun xmltok-char-number (start end
)
980 (let* ((base (if (eq (char-after (+ start
2)) ?x
)
984 (buffer-substring-no-properties (+ start
(if (= base
16) 3 2))
988 (xmltok-valid-char-p n
)
991 (defun xmltok-unclosed-reparse-p (change-start
997 (let ((len-1 (1- (length delimiter
))))
998 (goto-char (max start
(- change-start len-1
)))
999 (search-forward delimiter
(min end
(+ change-end len-1
)) t
)))
1001 ;; Handles a <!-- with the next -- not followed by >
1003 (defun xmltok-semi-closed-reparse-p (change-start
1010 (or (<= (- end delimiter-length
) change-end
)
1011 (xmltok-unclosed-reparse-p change-start
1018 (defun xmltok-valid-char-p (n)
1019 "Return non-nil if N is the Unicode code of a valid XML character."
1020 (cond ((< n
#x20
) (memq n
'(#xA
#xD
#x9
)))
1024 (t (and (> n
#xFFFF
)
1027 (defun xmltok-unicode-to-char (n)
1028 "Return the character corresponding to Unicode scalar value N.
1029 Return nil if unsupported in Emacs."
1030 (decode-char 'ucs n
))
1034 (defvar xmltok-contains-doctype nil
)
1035 (defvar xmltok-doctype-external-subset-flag nil
)
1036 (defvar xmltok-internal-subset-start nil
)
1037 (defvar xmltok-had-param-entity-ref nil
)
1038 (defvar xmltok-prolog-regions nil
)
1039 (defvar xmltok-standalone nil
1040 "Non-nil if there was an XML declaration specifying standalone=\"yes\".")
1041 (defvar xmltok-markup-declaration-doctype-flag nil
)
1043 (defconst xmltok-predefined-entity-alist
1048 ("quot" "\"" .
"\"")))
1050 (defun xmltok-forward-prolog ()
1051 "Move forward to the end of the XML prolog.
1053 Returns a list of vectors [TYPE START END] where TYPE is a symbol and
1054 START and END are integers giving the start and end of the region of
1055 that type. TYPE can be one of xml-declaration,
1056 xml-declaration-attribute-name, xml-declaration-attribute-value,
1057 comment, processing-instruction-left, processing-instruction-right,
1058 markup-declaration-open, markup-declaration-close,
1059 internal-subset-open, internal-subset-close, hash-name, keyword,
1060 literal, encoding-name.
1061 Adds to `xmltok-errors' as appropriate."
1062 (let ((case-fold-search nil
)
1065 xmltok-prolog-regions
1066 xmltok-contains-doctype
1067 xmltok-internal-subset-start
1068 xmltok-had-param-entity-ref
1070 xmltok-doctype-external-subset-flag
1071 xmltok-markup-declaration-doctype-flag
)
1072 (setq xmltok-dtd xmltok-predefined-entity-alist
)
1073 (xmltok-scan-xml-declaration)
1074 (xmltok-next-prolog-token)
1075 (while (condition-case err
1076 (when (xmltok-parse-prolog-item)
1077 (xmltok-next-prolog-token))
1078 (xmltok-markup-declaration-parse-error
1079 (xmltok-skip-markup-declaration))))
1080 (when xmltok-internal-subset-start
1081 (xmltok-add-error "No closing ]"
1082 (1- xmltok-internal-subset-start
)
1083 xmltok-internal-subset-start
))
1084 (xmltok-parse-entities)
1085 (nreverse xmltok-prolog-regions
)))
1087 (defconst xmltok-bad-xml-decl-regexp
1088 "[ \t\r\n]*<\\?xml\\(?:[ \t\r\n]\\|\\?>\\)")
1091 (defun xmltok-get-declared-encoding-position (&optional limit
)
1092 "Return the position of the encoding in the XML declaration at point.
1093 If there is a well-formed XML declaration starting at point and it
1094 contains an encoding declaration, then return (START . END)
1095 where START and END are the positions of the start and the end
1096 of the encoding name; if there is no encoding declaration return
1097 the position where and encoding declaration could be inserted.
1098 If there is XML that is not well-formed that looks like an XML
1099 declaration, return nil. Otherwise, return t.
1100 If LIMIT is non-nil, then do not consider characters beyond LIMIT."
1101 (cond ((let ((case-fold-search nil
))
1102 (and (looking-at (xmltok-xml-declaration regexp
))
1103 (or (not limit
) (<= (match-end 0) limit
))))
1104 (let ((end (xmltok-xml-declaration end encoding-value
)))
1106 (cons (1+ (xmltok-xml-declaration start encoding-value
))
1108 (or (xmltok-xml-declaration end version-value
)
1110 ((not (let ((case-fold-search t
))
1111 (looking-at xmltok-bad-xml-decl-regexp
))))))
1113 (defun xmltok-scan-xml-declaration ()
1114 (when (looking-at (xmltok-xml-declaration regexp
))
1115 (xmltok-add-prolog-region 'xml-declaration
(point) (match-end 0))
1116 (goto-char (match-end 0))
1117 (when (xmltok-xml-declaration start version-name
)
1118 (xmltok-add-prolog-region 'xml-declaration-attribute-name
1119 (xmltok-xml-declaration start version-name
)
1120 (xmltok-xml-declaration end version-name
))
1121 (let ((start (xmltok-xml-declaration start version-value
))
1122 (end (xmltok-xml-declaration end version-value
)))
1123 (xmltok-add-prolog-region 'xml-declaration-attribute-value
1126 ;; XXX need to check encoding name
1127 ;; Should start with letter, not contain colon
1128 (when (xmltok-xml-declaration start encoding-name
)
1129 (xmltok-add-prolog-region 'xml-declaration-attribute-name
1130 (xmltok-xml-declaration start encoding-name
)
1131 (xmltok-xml-declaration end encoding-name
))
1132 (let ((start (xmltok-xml-declaration start encoding-value
))
1133 (end (xmltok-xml-declaration end encoding-value
)))
1134 (xmltok-add-prolog-region 'encoding-name
1137 (xmltok-add-prolog-region 'xml-declaration-attribute-value
1140 (when (xmltok-xml-declaration start standalone-name
)
1141 (xmltok-add-prolog-region 'xml-declaration-attribute-name
1142 (xmltok-xml-declaration start standalone-name
)
1143 (xmltok-xml-declaration end standalone-name
))
1144 (let ((start (xmltok-xml-declaration start standalone-value
))
1145 (end (xmltok-xml-declaration end standalone-value
)))
1146 (xmltok-add-prolog-region 'xml-declaration-attribute-value
1149 (setq xmltok-standalone
1150 (string= (buffer-substring-no-properties (1+ start
) (1- end
))
1154 (defconst xmltok-markup-declaration-alist
1155 '(("ELEMENT" . xmltok-parse-element-declaration
)
1156 ("ATTLIST" . xmltok-parse-attlist-declaration
)
1157 ("ENTITY" . xmltok-parse-entity-declaration
)
1158 ("NOTATION" . xmltok-parse-notation-declaration
)))
1160 (defun xmltok-parse-prolog-item ()
1161 (cond ((eq xmltok-type
'comment
)
1162 (xmltok-add-prolog-region 'comment
1166 ((eq xmltok-type
'processing-instruction
))
1167 ((eq xmltok-type
'named-markup-declaration
)
1168 (setq xmltok-markup-declaration-doctype-flag nil
)
1169 (xmltok-add-prolog-region 'markup-declaration-open
1172 (let* ((name (buffer-substring-no-properties
1175 (fun (cdr (assoc name xmltok-markup-declaration-alist
))))
1177 (unless xmltok-internal-subset-start
1179 "Declaration allowed only in internal subset"))
1181 ((string= name
"DOCTYPE")
1182 (xmltok-parse-doctype))
1184 (xmltok-add-error "Unknown markup declaration"
1186 (xmltok-next-prolog-token)
1187 (xmltok-markup-declaration-parse-error))))
1189 ((or (eq xmltok-type
'end-prolog
)
1192 ((eq xmltok-type
'internal-subset-close
)
1193 (xmltok-add-prolog-region 'internal-subset-close
1196 (xmltok-add-prolog-region 'markup-declaration-close
1199 (if xmltok-internal-subset-start
1200 (setq xmltok-internal-subset-start nil
)
1201 (xmltok-add-error "]> outside internal subset"))
1203 ((eq xmltok-type
'param-entity-ref
)
1204 (if xmltok-internal-subset-start
1205 (setq xmltok-had-param-entity-ref t
)
1206 (xmltok-add-error "Parameter entity reference outside document type declaration"))
1208 ;; If we don't do this, we can get thousands of errors when
1209 ;; a plain text file is parsed.
1210 ((not xmltok-internal-subset-start
)
1211 (when (let ((err (car xmltok-errors
)))
1213 (<= (xmltok-error-end err
) xmltok-start
)))
1214 (goto-char xmltok-start
))
1216 ((eq xmltok-type
'not-well-formed
) t
)
1218 (xmltok-add-error "Token allowed only inside markup declaration")
1221 (defun xmltok-parse-doctype ()
1222 (setq xmltok-markup-declaration-doctype-flag t
)
1223 (xmltok-next-prolog-token)
1224 (when xmltok-internal-subset-start
1225 (xmltok-add-error "DOCTYPE declaration not allowed in internal subset")
1226 (xmltok-markup-declaration-parse-error))
1227 (when xmltok-contains-doctype
1228 (xmltok-add-error "Duplicate DOCTYPE declaration")
1229 (xmltok-markup-declaration-parse-error))
1230 (setq xmltok-contains-doctype t
)
1231 (xmltok-require-token 'name
'prefixed-name
)
1232 (xmltok-require-next-token "SYSTEM" "PUBLIC" ?\
[ ?
>)
1233 (cond ((eq xmltok-type ?\
[)
1234 (setq xmltok-internal-subset-start
(point)))
1235 ((eq xmltok-type ?
>))
1237 (setq xmltok-doctype-external-subset-flag t
)
1238 (xmltok-parse-external-id)
1239 (xmltok-require-token ?\
[ ?
>)
1240 (when (eq xmltok-type ?\
[)
1241 (setq xmltok-internal-subset-start
(point))))))
1243 (defun xmltok-parse-attlist-declaration ()
1244 (xmltok-require-next-token 'prefixed-name
'name
)
1246 (xmltok-require-next-token ?
> 'name
'prefixed-name
)
1247 (if (eq xmltok-type ?
>)
1249 (xmltok-require-next-token ?\
(
1259 (cond ((eq xmltok-type ?\
()
1260 (xmltok-parse-nmtoken-group))
1261 ((string= (xmltok-current-token-string)
1263 (xmltok-require-next-token ?\
()
1264 (xmltok-parse-nmtoken-group)))
1265 (xmltok-require-next-token "#IMPLIED"
1269 (when (string= (xmltok-current-token-string) "#FIXED")
1270 (xmltok-require-next-token 'literal
))
1273 (defun xmltok-parse-nmtoken-group ()
1275 (xmltok-require-next-token 'nmtoken
'prefixed-name
'name
)
1276 (xmltok-require-next-token ?| ?\
))
1277 (eq xmltok-type ?|
))))
1279 (defun xmltok-parse-element-declaration ()
1280 (xmltok-require-next-token 'name
'prefixed-name
)
1281 (xmltok-require-next-token "EMPTY" "ANY" ?\
()
1282 (when (eq xmltok-type ?\
()
1283 (xmltok-require-next-token "#PCDATA"
1288 (cond ((eq xmltok-type
'hash-name
)
1289 (xmltok-require-next-token ?| ?\
) 'close-paren-star
)
1290 (while (eq xmltok-type ?|
)
1291 (xmltok-require-next-token 'name
'prefixed-name
)
1292 (xmltok-require-next-token 'close-paren-star ?|
)))
1293 (t (xmltok-parse-model-group))))
1294 (xmltok-require-next-token ?
>))
1296 (defun xmltok-parse-model-group ()
1297 (xmltok-parse-model-group-member)
1298 (xmltok-require-next-token ?|
1303 (when (memq xmltok-type
'(?
, ?|
))
1304 (let ((connector xmltok-type
))
1306 (xmltok-next-prolog-token)
1307 (xmltok-parse-model-group-member)
1308 (xmltok-require-next-token connector
1312 (eq xmltok-type connector
))))))
1314 (defun xmltok-parse-model-group-member ()
1315 (xmltok-require-token 'name
1319 (when (eq xmltok-type ?\
()
1320 (xmltok-next-prolog-token)
1321 (xmltok-parse-model-group)))
1323 (defun xmltok-parse-entity-declaration ()
1325 (xmltok-require-next-token 'name ?%
)
1326 (when (eq xmltok-type ?%
)
1328 (xmltok-require-next-token 'name
))
1329 (setq name
(xmltok-current-token-string))
1330 (xmltok-require-next-token 'literal
"SYSTEM" "PUBLIC")
1331 (cond ((eq xmltok-type
'literal
)
1332 (let ((replacement (xmltok-parse-entity-value)))
1334 (xmltok-define-entity name replacement
)))
1335 (xmltok-require-next-token ?
>))
1337 (xmltok-parse-external-id)
1339 (xmltok-require-token ?
>)
1340 (xmltok-require-token ?
> "NDATA")
1341 (if (eq xmltok-type ?
>)
1342 (xmltok-define-entity name
'external
)
1343 (xmltok-require-next-token 'name
)
1344 (xmltok-require-next-token ?
>)
1345 (xmltok-define-entity name
'unparsed
)))))))
1347 (defun xmltok-define-entity (name value
)
1348 (when (and (or (not xmltok-had-param-entity-ref
)
1350 (not (assoc name xmltok-dtd
)))
1352 (cons (cons name value
) xmltok-dtd
))))
1354 (defun xmltok-parse-entity-value ()
1355 (let ((lim (1- (point)))
1360 (goto-char (1+ xmltok-start
))
1361 (setq start
(point))
1363 (skip-chars-forward "^%&" lim
)
1364 (when (< (point) lim
)
1365 (goto-char (1+ (point)))
1366 (cond ((eq (char-before) ?%
)
1367 (xmltok-add-error "Parameter entity references are not allowed in the internal subset"
1370 (setq well-formed nil
))
1372 (let ((xmltok-start (1- (point)))
1373 xmltok-type xmltok-replacement
)
1374 (xmltok-scan-after-amp (lambda (start end
)))
1375 (cond ((eq xmltok-type
'char-ref
)
1377 (cons (buffer-substring-no-properties
1382 (cons xmltok-replacement
1384 (setq start
(point)))
1385 ((eq xmltok-type
'not-well-formed
)
1386 (setq well-formed nil
))))))
1388 (if (not well-formed
)
1391 (nreverse (cons (buffer-substring-no-properties start lim
)
1394 (defun xmltok-parse-notation-declaration ()
1395 (xmltok-require-next-token 'name
)
1396 (xmltok-require-next-token "SYSTEM" "PUBLIC")
1397 (let ((publicp (string= (xmltok-current-token-string) "PUBLIC")))
1398 (xmltok-require-next-token 'literal
)
1400 (xmltok-require-next-token 'literal ?
>)
1401 (unless (eq xmltok-type ?
>)
1402 (xmltok-require-next-token ?
>)))
1403 (t (xmltok-require-next-token ?
>)))))
1405 (defun xmltok-parse-external-id ()
1406 (xmltok-require-token "SYSTEM" "PUBLIC")
1407 (let ((publicp (string= (xmltok-current-token-string) "PUBLIC")))
1408 (xmltok-require-next-token 'literal
)
1410 (xmltok-require-next-token 'literal
)))
1411 (xmltok-next-prolog-token))
1413 (defun xmltok-require-next-token (&rest types
)
1414 (xmltok-next-prolog-token)
1415 (apply 'xmltok-require-token types
))
1417 (defun xmltok-require-token (&rest types
)
1418 ;; XXX Generate a more helpful error message
1419 (while (and (not (let ((type (car types
)))
1420 (if (stringp (car types
))
1421 (string= (xmltok-current-token-string) type
)
1422 (eq type xmltok-type
))))
1423 (setq types
(cdr types
))))
1425 (when (and xmltok-type
1426 (not (eq xmltok-type
'not-well-formed
)))
1427 (xmltok-add-error "Unexpected token"))
1428 (xmltok-markup-declaration-parse-error))
1429 (let ((region-type (xmltok-prolog-region-type (car types
))))
1431 (xmltok-add-prolog-region region-type
1435 (defun xmltok-current-token-string ()
1436 (buffer-substring-no-properties xmltok-start
(point)))
1438 (define-error 'xmltok-markup-declaration-parse-error
1439 "Syntax error in markup declaration")
1441 (defun xmltok-markup-declaration-parse-error ()
1442 (signal 'xmltok-markup-declaration-parse-error nil
))
1444 (defun xmltok-skip-markup-declaration ()
1445 (while (cond ((eq xmltok-type ?
>)
1446 (xmltok-next-prolog-token)
1448 ((and xmltok-markup-declaration-doctype-flag
1449 (eq xmltok-type ?\
[))
1450 (setq xmltok-internal-subset-start
(point))
1451 (xmltok-next-prolog-token)
1453 ((memq xmltok-type
'(nil
1455 named-markup-declaration
1457 processing-instruction
))
1459 ((and xmltok-internal-subset-start
1460 (eq xmltok-type
'internal-subset-close
))
1462 (t (xmltok-next-prolog-token) t
)))
1465 (defun xmltok-prolog-region-type (required)
1466 (cond ((cdr (assq xmltok-type
1467 '((literal . literal
)
1468 (?
> . markup-declaration-close
)
1469 (?\
[ . internal-subset-open
)
1470 (hash-name . hash-name
)))))
1471 ((and (stringp required
) (eq xmltok-type
'name
))
1474 ;; Return new token type.
1476 (defun xmltok-next-prolog-token ()
1477 (skip-chars-forward " \t\r\n")
1478 (setq xmltok-start
(point))
1479 (cond ((not (and (looking-at (xmltok-prolog regexp
))
1480 (goto-char (match-end 0))))
1481 (let ((ch (char-after)))
1483 (goto-char (1+ (point)))
1484 (xmltok-add-error "Illegal char in prolog")
1485 (setq xmltok-type
'not-well-formed
))
1486 (t (setq xmltok-type nil
)))))
1487 ((or (xmltok-prolog start ncname-occur
)
1488 (xmltok-prolog start colon-name-occur
))
1489 (setq xmltok-name-end
(1- (point)))
1490 (setq xmltok-name-colon
(xmltok-prolog start colon
))
1491 (setq xmltok-type
'name-occur
))
1492 ((xmltok-prolog start colon
)
1493 (setq xmltok-name-end
(point))
1494 (setq xmltok-name-colon
(xmltok-prolog start colon
))
1495 (unless (looking-at "[ \t\r\n>),|[%]")
1496 (xmltok-add-error "Missing space after name"))
1497 (setq xmltok-type
'prefixed-name
))
1498 ((or (xmltok-prolog start ncname-nmtoken
)
1499 (xmltok-prolog start ncname-colon
))
1500 (unless (looking-at "[ \t\r\n>),|[%]")
1501 (xmltok-add-error "Missing space after name token"))
1502 (setq xmltok-type
'nmtoken
))
1503 ((xmltok-prolog start name
)
1504 (setq xmltok-name-end
(point))
1505 (setq xmltok-name-colon nil
)
1506 (unless (looking-at "[ \t\r\n>),|[%]")
1507 (xmltok-add-error "Missing space after name"))
1508 (setq xmltok-type
'name
))
1509 ((xmltok-prolog start hash-name
)
1510 (setq xmltok-name-end
(point))
1511 (unless (looking-at "[ \t\r\n>)|%]")
1512 (xmltok-add-error "Missing space after name"))
1513 (setq xmltok-type
'hash-name
))
1514 ((xmltok-prolog start processing-instruction-question
)
1515 (xmltok-scan-prolog-after-processing-instruction-open))
1516 ((xmltok-prolog start comment-open
)
1517 ;; XXX if not-well-formed, ignore some stuff
1518 (xmltok-scan-after-comment-open))
1519 ((xmltok-prolog start named-markup-declaration
)
1520 (setq xmltok-type
'named-markup-declaration
))
1521 ((xmltok-prolog start instance-start
)
1522 (goto-char xmltok-start
)
1523 (setq xmltok-type
'end-prolog
))
1524 ((xmltok-prolog start close-paren-star
)
1525 (setq xmltok-type
'close-paren-star
))
1526 ((xmltok-prolog start close-paren-occur
)
1527 (setq xmltok-type
'close-paren-occur
))
1528 ((xmltok-prolog start close-paren
)
1529 (unless (looking-at "[ \t\r\n>,|)]")
1530 (xmltok-add-error "Missing space after )"))
1531 (setq xmltok-type ?\
)))
1532 ((xmltok-prolog start single-char
)
1533 (let ((ch (char-before)))
1534 (cond ((memq ch
'(?
\" ?
\'))
1535 (xmltok-scan-prolog-literal))
1536 (t (setq xmltok-type ch
)))))
1537 ((xmltok-prolog start percent
)
1538 (cond ((xmltok-prolog start param-entity-ref-close
)
1539 (setq xmltok-name-end
(1- (point)))
1540 (setq xmltok-type
'param-entity-ref
))
1541 ((xmltok-prolog start param-entity-ref
)
1542 (xmltok-add-error "Missing ;")
1543 (setq xmltok-name-end
(point))
1544 (setq xmltok-type
'param-entity-ref
))
1545 ((looking-at "[ \t\r\n%]")
1546 (setq xmltok-type ?%
))
1548 (xmltok-add-error "Expected name after %")
1549 (setq xmltok-type
'not-well-formed
))))
1550 ((xmltok-prolog start nmtoken
)
1551 (unless (looking-at "[ \t\r\n>),|[%]")
1552 (xmltok-add-error "Missing space after name token"))
1553 (setq xmltok-type
'nmtoken
))
1554 ((xmltok-prolog start internal-subset-close
)
1555 (setq xmltok-type
'internal-subset-close
))
1556 ((xmltok-prolog start pound
)
1557 (xmltok-add-error "Expected name after #")
1558 (setq xmltok-type
'not-well-formed
))
1559 ((xmltok-prolog start markup-declaration
)
1560 (xmltok-add-error "Expected name or -- after <!")
1561 (setq xmltok-type
'not-well-formed
))
1562 ((xmltok-prolog start comment-first-dash
)
1563 (xmltok-add-error "Expected <!--")
1564 (setq xmltok-type
'not-well-formed
))
1565 ((xmltok-prolog start less-than
)
1566 (xmltok-add-error "Incomplete markup")
1567 (setq xmltok-type
'not-well-formed
))
1568 (t (error "Unhandled token in prolog %s"
1569 (match-string-no-properties 0)))))
1571 (defun xmltok-scan-prolog-literal ()
1572 (let* ((delim (string (char-before)))
1573 (safe-end (save-excursion
1574 (skip-chars-forward (concat "^<>[]" delim
))
1576 (end (save-excursion
1577 (goto-char safe-end
)
1578 (search-forward delim nil t
))))
1579 (cond ((or (not end
)
1582 (looking-at "[ \t\r\n>%[]")))
1584 ((eq (1+ safe-end
) end
)
1586 (xmltok-add-error (format "Missing space after %s" delim
)
1588 (setq xmltok-type
'literal
)))
1590 (defun xmltok-scan-prolog-after-processing-instruction-open ()
1591 (search-forward "?>" nil
'move
)
1592 (let* ((end (point))
1595 (goto-char (+ xmltok-start
2))
1596 (and (looking-at (xmltok-ncname regexp
))
1597 (or (memq (char-after (match-end 0))
1599 (= (match-end 0) (- end
2)))
1600 (match-string-no-properties 0)))))
1602 (xmltok-add-error "\
1603 Processing instruction does not start with a name"
1605 (+ xmltok-start
3)))
1606 ((not (and (= (length target
) 3)
1607 (let ((case-fold-search t
))
1608 (string-match "xml" target
)))))
1610 (xmltok-add-error "Invalid XML declaration"
1614 (goto-char xmltok-start
)
1615 (looking-at (xmltok-xml-declaration regexp
)))
1616 (xmltok-add-error "XML declaration not at beginning of file"
1620 (xmltok-add-error "Processing instruction has target of xml"
1622 (+ xmltok-start
5))))
1623 (xmltok-add-prolog-region 'processing-instruction-left
1630 (xmltok-add-prolog-region 'processing-instruction-right
1633 (goto-char (+ xmltok-start
1636 (skip-chars-forward " \t\r\n")
1640 (setq xmltok-type
'processing-instruction
))
1642 (defun xmltok-parse-entities ()
1643 (let ((todo xmltok-dtd
))
1644 (when (and (or xmltok-had-param-entity-ref
1645 xmltok-doctype-external-subset-flag
)
1646 (not xmltok-standalone
))
1647 (setq xmltok-dtd
(cons t xmltok-dtd
)))
1649 (xmltok-parse-entity (car todo
))
1650 (setq todo
(cdr todo
)))))
1652 (defun xmltok-parse-entity (name-def)
1653 (let ((def (cdr name-def
))
1654 ;; in case its value is buffer local
1655 (xmltok-dtd xmltok-dtd
)
1658 (if (string-match "\\`[^&<\t\r\n]*\\'" def
)
1659 (setcdr name-def
(cons def def
))
1660 (setcdr name-def
'not-well-formed
) ; avoid infinite expansion loops
1661 (setq buf
(get-buffer-create
1662 (format " *Entity %s*" (car name-def
))))
1663 (with-current-buffer buf
1666 (goto-char (point-min))
1668 (xmltok-parse-entity-replacement)))
1669 (kill-buffer buf
)))))
1671 (defun xmltok-parse-entity-replacement ()
1672 (let ((def (cons "" "")))
1673 (while (let* ((start (point))
1674 (found (re-search-forward "[<&\t\r\n]\\|]]>" nil t
))
1675 (ch (and found
(char-before)))
1676 (str (buffer-substring-no-properties
1682 (xmltok-append-entity-def def
1684 (cond ((not found
) nil
)
1686 (setq def
'not-well-formed
)
1690 (setq xmltok-start
(1- (point)))
1691 (xmltok-scan-after-lt)
1693 (xmltok-append-entity-def
1695 (cond ((memq xmltok-type
1702 processing-instruction
))
1706 (cons (buffer-substring-no-properties
1710 (t 'not-well-formed
)))))
1713 (let ((xmltok-start (1- (point)))
1717 (xmltok-scan-after-amp 'xmltok-handle-nested-entity
)
1718 (cond ((eq xmltok-type
'entity-ref
)
1720 (xmltok-append-entity-def
1722 xmltok-replacement
)))
1723 ((eq xmltok-type
'char-ref
)
1725 (xmltok-append-entity-def
1727 (if xmltok-replacement
1728 (cons xmltok-replacement
1730 (and xmltok-errors
'not-well-formed
)))))
1732 (setq def
'not-well-formed
))))
1736 (xmltok-append-entity-def
1738 (cons (match-string-no-properties 0)
1743 (defun xmltok-handle-nested-entity (start end
)
1744 (let* ((name-def (assoc (buffer-substring-no-properties start end
)
1746 (def (cdr name-def
)))
1748 (xmltok-parse-entity name-def
)
1749 (setq def
(cdr name-def
)))
1750 (setq xmltok-replacement
1751 (cond ((null name-def
)
1752 (if (eq (car xmltok-dtd
) t
)
1755 ((eq def
'unparsed
) 'not-well-formed
)
1758 (defun xmltok-append-entity-def (d1 d2
)
1761 (cons (concat (car d1
) (car d2
))
1764 (concat (cdr d1
) (cdr d2
))))
1768 (let ((defs '(not-well-formed external element
)))
1769 (while (not (or (eq (car defs
) d1
)
1770 (eq (car defs
) d2
)))
1771 (setq defs
(cdr defs
)))
1774 (defun xmltok-add-prolog-region (type start end
)
1775 (setq xmltok-prolog-regions
1776 (cons (vector type start end
)
1777 xmltok-prolog-regions
)))
1779 (defun xmltok-merge-attributes ()
1780 "Return a list merging `xmltok-attributes' and `xmltok-namespace-attributes'.
1781 The members of the merged list are in order of occurrence in the
1782 document. The list may share list structure with `xmltok-attributes'
1783 and `xmltok-namespace-attributes'."
1784 (cond ((not xmltok-namespace-attributes
)
1786 ((not xmltok-attributes
)
1787 xmltok-namespace-attributes
)
1789 (let ((atts1 xmltok-attributes
)
1790 (atts2 xmltok-namespace-attributes
)
1792 (while (and atts1 atts2
)
1793 (cond ((< (xmltok-attribute-name-start (car atts1
))
1794 (xmltok-attribute-name-start (car atts2
)))
1795 (setq merged
(cons (car atts1
) merged
))
1796 (setq atts1
(cdr atts1
)))
1798 (setq merged
(cons (car atts2
) merged
))
1799 (setq atts2
(cdr atts2
)))))
1800 (setq merged
(nreverse merged
))
1801 (cond (atts1 (setq merged
(nconc merged atts1
)))
1802 (atts2 (setq merged
(nconc merged atts2
))))
1807 (defun xmltok-forward-test ()
1809 (if (xmltok-forward)
1810 (message "Scanned %s" xmltok-type
)
1811 (message "Scanned nothing")))
1813 (defun xmltok-next-prolog-token-test ()
1815 (if (xmltok-next-prolog-token)
1816 (message "Scanned %s"
1817 (if (integerp xmltok-type
)
1818 (string xmltok-type
)
1820 (message "Scanned end of file")))
1824 ;;; xmltok.el ends here