nxml-rap.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. ;;; nxml-rap.el --- low-level support for random access parsing for nXML mode
  2. ;; Copyright (C) 2003-2004, 2007-2015 Free Software Foundation, Inc.
  3. ;; Author: James Clark
  4. ;; Keywords: wp, hypermedia, languages, XML
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This uses xmltok.el to do XML parsing. The fundamental problem is
  18. ;; how to handle changes. We don't want to maintain a complete parse
  19. ;; tree. We also don't want to reparse from the start of the document
  20. ;; on every keystroke. However, it is not possible in general to
  21. ;; parse an XML document correctly starting at a random point in the
  22. ;; middle. The main problems are comments, CDATA sections and
  23. ;; processing instructions: these can all contain things that are
  24. ;; indistinguishable from elements. Literals in the prolog are also a
  25. ;; problem. Attribute value literals are not a problem because
  26. ;; attribute value literals cannot contain less-than signs.
  27. ;;
  28. ;; Our strategy is to keep track of just the problematic things.
  29. ;; Specifically, we keep track of all comments, CDATA sections and
  30. ;; processing instructions in the instance. We do this by marking all
  31. ;; except the first character of these with a non-nil nxml-inside text
  32. ;; property. The value of the nxml-inside property is comment,
  33. ;; cdata-section or processing-instruction. The first character does
  34. ;; not have the nxml-inside property so we can find the beginning of
  35. ;; the construct by looking for a change in a text property value
  36. ;; (Emacs provides primitives for this). We use text properties
  37. ;; rather than overlays, since the implementation of overlays doesn't
  38. ;; look like it scales to large numbers of overlays in a buffer.
  39. ;;
  40. ;; We don't in fact track all these constructs, but only track them in
  41. ;; some initial part of the instance. The variable `nxml-scan-end'
  42. ;; contains the limit of where we have scanned up to for them.
  43. ;;
  44. ;; Thus to parse some random point in the file we first ensure that we
  45. ;; have scanned up to that point. Then we search backwards for a
  46. ;; <. Then we check whether the < has an nxml-inside property. If it
  47. ;; does we go backwards to first character that does not have an
  48. ;; nxml-inside property (this character must be a <). Then we start
  49. ;; parsing forward from the < we have found.
  50. ;;
  51. ;; The prolog has to be parsed specially, so we also keep track of the
  52. ;; end of the prolog in `nxml-prolog-end'. The prolog is reparsed on
  53. ;; every change to the prolog. This won't work well if people try to
  54. ;; edit huge internal subsets. Hopefully that will be rare.
  55. ;;
  56. ;; We keep track of the changes by adding to the buffer's
  57. ;; after-change-functions hook. Scanning is also done as a
  58. ;; prerequisite to fontification by adding to fontification-functions
  59. ;; (in the same way as jit-lock). This means that scanning for these
  60. ;; constructs had better be quick. Fortunately it is. Firstly, the
  61. ;; typical proportion of comments, CDATA sections and processing
  62. ;; instructions is small relative to other things. Secondly, to scan
  63. ;; we just search for the regexp <[!?].
  64. ;;; Code:
  65. (require 'xmltok)
  66. (require 'nxml-util)
  67. (defvar nxml-prolog-end nil
  68. "Integer giving position following end of the prolog.")
  69. (make-variable-buffer-local 'nxml-prolog-end)
  70. (defvar nxml-scan-end nil
  71. "Marker giving position up to which we have scanned.
  72. nxml-scan-end must be >= nxml-prolog-end. Furthermore, nxml-scan-end
  73. must not be an inside position in the following sense. A position is
  74. inside if the following character is a part of, but not the first
  75. character of, a CDATA section, comment or processing instruction.
  76. Furthermore all positions >= nxml-prolog-end and < nxml-scan-end that
  77. are inside positions must have a non-nil `nxml-inside' property whose
  78. value is a symbol specifying what it is inside. Any characters with a
  79. non-nil `fontified' property must have position < nxml-scan-end and
  80. the correct face. Dependent regions must also be established for any
  81. unclosed constructs starting before nxml-scan-end.
  82. There must be no `nxml-inside' properties after nxml-scan-end.")
  83. (make-variable-buffer-local 'nxml-scan-end)
  84. (defsubst nxml-get-inside (pos)
  85. (get-text-property pos 'nxml-inside))
  86. (defsubst nxml-clear-inside (start end)
  87. (nxml-debug-clear-inside start end)
  88. (remove-text-properties start end '(nxml-inside nil)))
  89. (defsubst nxml-set-inside (start end type)
  90. (nxml-debug-set-inside start end)
  91. (put-text-property start end 'nxml-inside type))
  92. (defun nxml-inside-end (pos)
  93. "Return the end of the inside region containing POS.
  94. Return nil if the character at POS is not inside."
  95. (if (nxml-get-inside pos)
  96. (or (next-single-property-change pos 'nxml-inside)
  97. (point-max))
  98. nil))
  99. (defun nxml-inside-start (pos)
  100. "Return the start of the inside region containing POS.
  101. Return nil if the character at POS is not inside."
  102. (if (nxml-get-inside pos)
  103. (or (previous-single-property-change (1+ pos) 'nxml-inside)
  104. (point-min))
  105. nil))
  106. ;;; Change management
  107. (defun nxml-scan-after-change (start end)
  108. "Restore `nxml-scan-end' invariants after a change.
  109. The change happened between START and END.
  110. Return position after which lexical state is unchanged.
  111. END must be > `nxml-prolog-end'. START must be outside
  112. any “inside” regions and at the beginning of a token."
  113. (if (>= start nxml-scan-end)
  114. nxml-scan-end
  115. (let ((inside-remove-start start)
  116. xmltok-errors)
  117. (while (or (when (xmltok-forward-special (min end nxml-scan-end))
  118. (when (memq xmltok-type
  119. '(comment
  120. cdata-section
  121. processing-instruction))
  122. (nxml-clear-inside inside-remove-start
  123. (1+ xmltok-start))
  124. (nxml-set-inside (1+ xmltok-start)
  125. (point)
  126. xmltok-type)
  127. (setq inside-remove-start (point)))
  128. (if (< (point) (min end nxml-scan-end))
  129. t
  130. (setq end (point))
  131. nil))
  132. ;; The end of the change was inside but is now outside.
  133. ;; Imagine something really weird like
  134. ;; <![CDATA[foo <!-- bar ]]> <![CDATA[ stuff --> <!-- ]]> -->
  135. ;; and suppose we deleted "<![CDATA[f"
  136. (let ((inside-end (nxml-inside-end end)))
  137. (when inside-end
  138. (setq end inside-end)
  139. t))))
  140. (nxml-clear-inside inside-remove-start end))
  141. (when (> end nxml-scan-end)
  142. (set-marker nxml-scan-end end))
  143. end))
  144. ;; n-s-p only called from nxml-mode.el, where this variable is defined.
  145. (defvar nxml-prolog-regions)
  146. (defun nxml-scan-prolog ()
  147. (goto-char (point-min))
  148. (let (xmltok-dtd
  149. xmltok-errors)
  150. (setq nxml-prolog-regions (xmltok-forward-prolog))
  151. (setq nxml-prolog-end (point))
  152. (nxml-clear-inside (point-min) nxml-prolog-end))
  153. (when (< nxml-scan-end nxml-prolog-end)
  154. (set-marker nxml-scan-end nxml-prolog-end)))
  155. ;;; Random access parsing
  156. (defun nxml-token-after ()
  157. "Return the position after the token containing the char after point.
  158. Sets up the variables `xmltok-type', `xmltok-start',
  159. `xmltok-name-end', `xmltok-name-colon', `xmltok-attributes',
  160. `xmltok-namespace-attributes' in the same was as does
  161. `xmltok-forward'. The prolog will be treated as a single token with
  162. type `prolog'."
  163. (let ((pos (point)))
  164. (if (< pos nxml-prolog-end)
  165. (progn
  166. (setq xmltok-type 'prolog
  167. xmltok-start (point-min))
  168. (min nxml-prolog-end (point-max)))
  169. (nxml-ensure-scan-up-to-date)
  170. (if (nxml-get-inside pos)
  171. (save-excursion
  172. (nxml-move-outside-backwards)
  173. (xmltok-forward)
  174. (point))
  175. (save-excursion
  176. (if (or (eq (char-after) ?<)
  177. (search-backward "<"
  178. (max (point-min) nxml-prolog-end)
  179. t))
  180. (nxml-move-outside-backwards)
  181. (goto-char (if (<= (point-min) nxml-prolog-end)
  182. nxml-prolog-end
  183. (or (nxml-inside-end (point-min))
  184. (point-min)))))
  185. (while (and (nxml-tokenize-forward)
  186. (<= (point) pos)))
  187. (point))))))
  188. (defun nxml-token-before ()
  189. "Return the position after the token containing the char before point.
  190. Sets variables like `nxml-token-after'."
  191. (if (/= (point-min) (point))
  192. (save-excursion
  193. (goto-char (1- (point)))
  194. (nxml-token-after))
  195. (setq xmltok-start (point))
  196. (setq xmltok-type nil)
  197. (point)))
  198. (defun nxml-tokenize-forward ()
  199. (let (xmltok-errors)
  200. (when (and (xmltok-forward)
  201. (> (point) nxml-scan-end))
  202. (cond ((memq xmltok-type '(comment
  203. cdata-section
  204. processing-instruction))
  205. (with-silent-modifications
  206. (nxml-set-inside (1+ xmltok-start) (point) xmltok-type))))
  207. (set-marker nxml-scan-end (point)))
  208. xmltok-type))
  209. (defun nxml-move-tag-backwards (bound)
  210. "Move point backwards outside any “inside” regions or tags.
  211. Point will not move past `nxml-prolog-end'.
  212. Point will either be at BOUND or a `<' character starting a tag
  213. outside any “inside” regions.
  214. As a precondition, point must be >= BOUND."
  215. (nxml-move-outside-backwards)
  216. (when (not (equal (char-after) ?<))
  217. (if (search-backward "<" bound t)
  218. (progn
  219. (nxml-move-outside-backwards)
  220. (when (not (equal (char-after) ?<))
  221. (search-backward "<" bound t)))
  222. (goto-char bound))))
  223. (defun nxml-move-outside-backwards ()
  224. "Move point to first character of the containing special thing.
  225. Leave point unmoved if it is not inside anything special."
  226. (let ((start (nxml-inside-start (point))))
  227. (when start
  228. (goto-char (1- start))
  229. (when (nxml-get-inside (point))
  230. (error "Char before inside-start at %s had nxml-inside property %s"
  231. (point)
  232. (nxml-get-inside (point)))))))
  233. (defun nxml-ensure-scan-up-to-date ()
  234. (let ((pos (point)))
  235. (when (< nxml-scan-end pos)
  236. (save-excursion
  237. (goto-char nxml-scan-end)
  238. (let (xmltok-errors)
  239. (while (when (xmltok-forward-special pos)
  240. (when (memq xmltok-type
  241. '(comment
  242. processing-instruction
  243. cdata-section))
  244. (with-silent-modifications
  245. (nxml-set-inside (1+ xmltok-start)
  246. (point)
  247. xmltok-type)))
  248. (if (< (point) pos)
  249. t
  250. (setq pos (point))
  251. nil)))
  252. (set-marker nxml-scan-end pos))))))
  253. ;;; Element scanning
  254. (defun nxml-scan-element-forward (from &optional up)
  255. "Scan forward from FROM over a single balanced element.
  256. Point must be between tokens. Return the position of the end of
  257. the tag that ends the element. `xmltok-start' will contain the
  258. position of the start of the tag. If UP is non-nil, then scan
  259. past end-tag of element containing point. If no element is
  260. found, return nil. If a well-formedness error prevents scanning,
  261. signal an `nxml-scan-error'. Point is not moved."
  262. (let ((open-tags (and up t))
  263. found)
  264. (save-excursion
  265. (goto-char from)
  266. (while (cond ((not (nxml-tokenize-forward))
  267. (when (consp open-tags)
  268. (nxml-scan-error (cadr open-tags)
  269. "Start-tag has no end-tag"))
  270. nil)
  271. ((eq xmltok-type 'start-tag)
  272. (setq open-tags
  273. (cons (xmltok-start-tag-qname)
  274. (cons xmltok-start
  275. open-tags)))
  276. t)
  277. ((eq xmltok-type 'end-tag)
  278. (cond ((not open-tags) nil)
  279. ((not (consp open-tags)) (setq found (point)) nil)
  280. ((not (string= (car open-tags)
  281. (xmltok-end-tag-qname)))
  282. (nxml-scan-error (+ 2 xmltok-start)
  283. "Mismatched end-tag; \
  284. expected `%s'"
  285. (car open-tags)))
  286. ((setq open-tags (cddr open-tags)) t)
  287. (t (setq found (point)) nil)))
  288. ((memq xmltok-type '(empty-element
  289. partial-empty-element))
  290. (if open-tags
  291. t
  292. (setq found (point))
  293. nil))
  294. ((eq xmltok-type 'partial-end-tag)
  295. (cond ((not open-tags) nil)
  296. ((not (consp open-tags)) (setq found (point)) nil)
  297. ((setq open-tags (cddr open-tags)) t)
  298. (t (setq found (point)) nil)))
  299. ((eq xmltok-type 'partial-start-tag)
  300. (nxml-scan-error xmltok-start
  301. "Missing `>'"))
  302. (t t))))
  303. found))
  304. (defun nxml-scan-element-backward (from &optional up bound)
  305. "Scan backward from FROM over a single balanced element.
  306. Point must be between tokens. Return the position of the end of
  307. the tag that starts the element. `xmltok-start' will contain the
  308. position of the start of the tag. If UP is non-nil, then scan
  309. past start-tag of element containing point. If BOUND is non-nil,
  310. then don't scan back past BOUND. If no element is found, return
  311. nil. If a well-formedness error prevents scanning, signal an
  312. `nxml-scan-error'. Point is not moved."
  313. (let ((open-tags (and up t))
  314. token-end found)
  315. (save-excursion
  316. (goto-char from)
  317. (while (cond ((or (< (point) nxml-prolog-end)
  318. (not (search-backward "<"
  319. (max (or bound 0)
  320. nxml-prolog-end)
  321. t)))
  322. (when (and (consp open-tags) (not bound))
  323. (nxml-scan-error (cadr open-tags)
  324. "End-tag has no start-tag"))
  325. nil)
  326. ((progn
  327. (nxml-move-outside-backwards)
  328. (save-excursion
  329. (nxml-tokenize-forward)
  330. (setq token-end (point)))
  331. (eq xmltok-type 'end-tag))
  332. (setq open-tags
  333. (cons (xmltok-end-tag-qname)
  334. (cons xmltok-start open-tags)))
  335. t)
  336. ((eq xmltok-type 'start-tag)
  337. (cond ((not open-tags) nil)
  338. ((not (consp open-tags))
  339. (setq found token-end)
  340. nil)
  341. ((and (car open-tags)
  342. (not (string= (car open-tags)
  343. (xmltok-start-tag-qname))))
  344. (nxml-scan-error (1+ xmltok-start)
  345. "Mismatched start-tag; \
  346. expected `%s'"
  347. (car open-tags)))
  348. ((setq open-tags (cddr open-tags)) t)
  349. (t (setq found token-end) nil)))
  350. ((memq xmltok-type '(empty-element
  351. partial-empty-element))
  352. (if open-tags
  353. t
  354. (setq found token-end)
  355. nil))
  356. ((eq xmltok-type 'partial-end-tag)
  357. (setq open-tags
  358. (cons nil (cons xmltok-start open-tags)))
  359. t)
  360. ((eq xmltok-type 'partial-start-tag)
  361. ;; if we have only a partial-start-tag
  362. ;; then it's unlikely that there's a matching
  363. ;; end-tag, so it's probably not helpful
  364. ;; to treat it as a complete start-tag
  365. (nxml-scan-error xmltok-start
  366. "Missing `>'"))
  367. (t t))))
  368. found))
  369. (defun nxml-scan-error (&rest args)
  370. (signal 'nxml-scan-error args))
  371. (define-error 'nxml-scan-error
  372. "Scan over element that is not well-formed" 'nxml-error)
  373. (provide 'nxml-rap)
  374. ;;; nxml-rap.el ends here