rng-match.el 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. ;;; rng-match.el --- matching of RELAX NG patterns against XML events
  2. ;; Copyright (C) 2003, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: James Clark
  4. ;; Keywords: XML, RelaxNG
  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 the algorithm described in
  18. ;; http://www.thaiopensource.com/relaxng/derivative.html
  19. ;;
  20. ;; The schema to be used is contained in the variable
  21. ;; rng-current-schema. It has the form described in the file
  22. ;; rng-pttrn.el.
  23. ;;
  24. ;;; Code:
  25. (require 'rng-pttrn)
  26. (require 'rng-util)
  27. (require 'rng-dt)
  28. (defvar rng-not-allowed-ipattern nil)
  29. (defvar rng-empty-ipattern nil)
  30. (defvar rng-text-ipattern nil)
  31. (defvar rng-compile-table nil)
  32. (defvar rng-being-compiled nil
  33. "Contains a list of ref patterns currently being compiled.
  34. Used to detect invalid recursive references.")
  35. (defvar rng-ipattern-table nil)
  36. (defvar rng-last-ipattern-index nil)
  37. (defvar rng-match-state nil
  38. "An ipattern representing the current state of validation.")
  39. ;;; Inline functions
  40. (defsubst rng-update-match-state (new-state)
  41. (if (and (eq new-state rng-not-allowed-ipattern)
  42. (not (eq rng-match-state rng-not-allowed-ipattern)))
  43. nil
  44. (setq rng-match-state new-state)
  45. t))
  46. ;;; Interned patterns
  47. (eval-when-compile
  48. (defun rng-ipattern-slot-accessor-name (slot-name)
  49. (intern (concat "rng-ipattern-get-"
  50. (symbol-name slot-name))))
  51. (defun rng-ipattern-slot-setter-name (slot-name)
  52. (intern (concat "rng-ipattern-set-"
  53. (symbol-name slot-name)))))
  54. (defmacro rng-ipattern-defslot (slot-name index)
  55. `(progn
  56. (defsubst ,(rng-ipattern-slot-accessor-name slot-name) (ipattern)
  57. (aref ipattern ,index))
  58. (defsubst ,(rng-ipattern-slot-setter-name slot-name) (ipattern value)
  59. (aset ipattern ,index value))))
  60. (rng-ipattern-defslot type 0)
  61. (rng-ipattern-defslot index 1)
  62. (rng-ipattern-defslot name-class 2)
  63. (rng-ipattern-defslot datatype 2)
  64. (rng-ipattern-defslot after 2)
  65. (rng-ipattern-defslot child 3)
  66. (rng-ipattern-defslot value-object 3)
  67. (rng-ipattern-defslot nullable 4)
  68. (rng-ipattern-defslot memo-text-typed 5)
  69. (rng-ipattern-defslot memo-map-start-tag-open-deriv 6)
  70. (rng-ipattern-defslot memo-map-start-attribute-deriv 7)
  71. (rng-ipattern-defslot memo-start-tag-close-deriv 8)
  72. (rng-ipattern-defslot memo-text-only-deriv 9)
  73. (rng-ipattern-defslot memo-mixed-text-deriv 10)
  74. (rng-ipattern-defslot memo-map-data-deriv 11)
  75. (rng-ipattern-defslot memo-end-tag-deriv 12)
  76. (defconst rng-memo-map-alist-max 10)
  77. (defsubst rng-memo-map-get (key mm)
  78. "Return the value associated with KEY in memo-map MM."
  79. (let ((found (assoc key mm)))
  80. (if found
  81. (cdr found)
  82. (and mm
  83. (let ((head (car mm)))
  84. (and (hash-table-p head)
  85. (gethash key head)))))))
  86. (defun rng-memo-map-add (key value mm &optional weakness)
  87. "Associate KEY with VALUE in memo-map MM and return the new memo-map.
  88. The new memo-map may or may not be a different object from MM.
  89. Alists are better for small maps. Hash tables are better for large
  90. maps. A memo-map therefore starts off as an alist and switches to a
  91. hash table for large memo-maps. A memo-map is always a list. An empty
  92. memo-map is represented by nil. A large memo-map is represented by a
  93. list containing just a hash-table. A small memo map is represented by
  94. a list whose cdr is an alist and whose car is the number of entries in
  95. the alist. The complete memo-map can be passed to `assoc' without
  96. problems: assoc ignores any members that are not cons cells. There is
  97. therefore minimal overhead in successful lookups on small lists
  98. \(which is the most common case)."
  99. (if (null mm)
  100. (list 1 (cons key value))
  101. (let ((head (car mm)))
  102. (cond ((hash-table-p head)
  103. (puthash key value head)
  104. mm)
  105. ((>= head rng-memo-map-alist-max)
  106. (let ((ht (make-hash-table :test 'equal
  107. :weakness weakness
  108. :size (* 2 rng-memo-map-alist-max))))
  109. (setq mm (cdr mm))
  110. (while mm
  111. (setq head (car mm))
  112. (puthash (car head) (cdr head) ht)
  113. (setq mm (cdr mm)))
  114. (cons ht nil)))
  115. (t (cons (1+ head)
  116. (cons (cons key value)
  117. (cdr mm))))))))
  118. (defsubst rng-make-ipattern (type index name-class child nullable)
  119. (vector type index name-class child nullable
  120. ;; 5 memo-text-typed
  121. 'unknown
  122. ;; 6 memo-map-start-tag-open-deriv
  123. nil
  124. ;; 7 memo-map-start-attribute-deriv
  125. nil
  126. ;; 8 memo-start-tag-close-deriv
  127. nil
  128. ;; 9 memo-text-only-deriv
  129. nil
  130. ;; 10 memo-mixed-text-deriv
  131. nil
  132. ;; 11 memo-map-data-deriv
  133. nil
  134. ;; 12 memo-end-tag-deriv
  135. nil))
  136. (defun rng-ipattern-maybe-init ()
  137. (unless rng-ipattern-table
  138. (setq rng-ipattern-table (make-hash-table :test 'equal))
  139. (setq rng-last-ipattern-index -1)))
  140. (defun rng-ipattern-clear ()
  141. (when rng-ipattern-table
  142. (clrhash rng-ipattern-table))
  143. (setq rng-last-ipattern-index -1))
  144. (defsubst rng-gen-ipattern-index ()
  145. (setq rng-last-ipattern-index (1+ rng-last-ipattern-index)))
  146. (defun rng-put-ipattern (key type name-class child nullable)
  147. (let ((ipattern
  148. (rng-make-ipattern type
  149. (rng-gen-ipattern-index)
  150. name-class
  151. child
  152. nullable)))
  153. (puthash key ipattern rng-ipattern-table)
  154. ipattern))
  155. (defun rng-get-ipattern (key)
  156. (gethash key rng-ipattern-table))
  157. (or rng-not-allowed-ipattern
  158. (setq rng-not-allowed-ipattern
  159. (rng-make-ipattern 'not-allowed -3 nil nil nil)))
  160. (or rng-empty-ipattern
  161. (setq rng-empty-ipattern
  162. (rng-make-ipattern 'empty -2 nil nil t)))
  163. (or rng-text-ipattern
  164. (setq rng-text-ipattern
  165. (rng-make-ipattern 'text -1 nil nil t)))
  166. (defconst rng-const-ipatterns
  167. (list rng-not-allowed-ipattern
  168. rng-empty-ipattern
  169. rng-text-ipattern))
  170. (defun rng-intern-after (child after)
  171. (if (eq child rng-not-allowed-ipattern)
  172. rng-not-allowed-ipattern
  173. (let ((key (list 'after
  174. (rng-ipattern-get-index child)
  175. (rng-ipattern-get-index after))))
  176. (or (rng-get-ipattern key)
  177. (rng-put-ipattern key
  178. 'after
  179. after
  180. child
  181. nil)))))
  182. (defun rng-intern-attribute (name-class ipattern)
  183. (if (eq ipattern rng-not-allowed-ipattern)
  184. rng-not-allowed-ipattern
  185. (let ((key (list 'attribute
  186. name-class
  187. (rng-ipattern-get-index ipattern))))
  188. (or (rng-get-ipattern key)
  189. (rng-put-ipattern key
  190. 'attribute
  191. name-class
  192. ipattern
  193. nil)))))
  194. (defun rng-intern-data (dt matches-anything)
  195. (let ((key (list 'data dt)))
  196. (or (rng-get-ipattern key)
  197. (let ((ipattern (rng-put-ipattern key
  198. 'data
  199. dt
  200. nil
  201. matches-anything)))
  202. (rng-ipattern-set-memo-text-typed ipattern
  203. (not matches-anything))
  204. ipattern))))
  205. (defun rng-intern-data-except (dt ipattern)
  206. (let ((key (list 'data-except dt ipattern)))
  207. (or (rng-get-ipattern key)
  208. (rng-put-ipattern key
  209. 'data-except
  210. dt
  211. ipattern
  212. nil))))
  213. (defun rng-intern-value (dt obj)
  214. (let ((key (list 'value dt obj)))
  215. (or (rng-get-ipattern key)
  216. (rng-put-ipattern key
  217. 'value
  218. dt
  219. obj
  220. nil))))
  221. (defun rng-intern-one-or-more (ipattern)
  222. (or (rng-intern-one-or-more-shortcut ipattern)
  223. (let ((key (cons 'one-or-more
  224. (list (rng-ipattern-get-index ipattern)))))
  225. (or (rng-get-ipattern key)
  226. (rng-put-ipattern key
  227. 'one-or-more
  228. nil
  229. ipattern
  230. (rng-ipattern-get-nullable ipattern))))))
  231. (defun rng-intern-one-or-more-shortcut (ipattern)
  232. (cond ((eq ipattern rng-not-allowed-ipattern)
  233. rng-not-allowed-ipattern)
  234. ((eq ipattern rng-empty-ipattern)
  235. rng-empty-ipattern)
  236. ((eq (rng-ipattern-get-type ipattern) 'one-or-more)
  237. ipattern)
  238. (t nil)))
  239. (defun rng-intern-list (ipattern)
  240. (if (eq ipattern rng-not-allowed-ipattern)
  241. rng-not-allowed-ipattern
  242. (let ((key (cons 'list
  243. (list (rng-ipattern-get-index ipattern)))))
  244. (or (rng-get-ipattern key)
  245. (rng-put-ipattern key
  246. 'list
  247. nil
  248. ipattern
  249. nil)))))
  250. (defun rng-intern-group (ipatterns)
  251. "Return an ipattern for the list of group members in IPATTERNS."
  252. (or (rng-intern-group-shortcut ipatterns)
  253. (let* ((tem (rng-normalize-group-list ipatterns))
  254. (normalized (cdr tem)))
  255. (or (rng-intern-group-shortcut normalized)
  256. (let ((key (cons 'group
  257. (mapcar 'rng-ipattern-get-index normalized))))
  258. (or (rng-get-ipattern key)
  259. (rng-put-ipattern key
  260. 'group
  261. nil
  262. normalized
  263. (car tem))))))))
  264. (defun rng-intern-group-shortcut (ipatterns)
  265. "Try to shortcut interning a group list.
  266. If successful, return the interned pattern. Otherwise return nil."
  267. (while (and ipatterns
  268. (eq (car ipatterns) rng-empty-ipattern))
  269. (setq ipatterns (cdr ipatterns)))
  270. (if ipatterns
  271. (let ((ret (car ipatterns)))
  272. (if (eq ret rng-not-allowed-ipattern)
  273. rng-not-allowed-ipattern
  274. (setq ipatterns (cdr ipatterns))
  275. (while (and ipatterns ret)
  276. (let ((tem (car ipatterns)))
  277. (cond ((eq tem rng-not-allowed-ipattern)
  278. (setq ret tem)
  279. (setq ipatterns nil))
  280. ((eq tem rng-empty-ipattern)
  281. (setq ipatterns (cdr ipatterns)))
  282. (t
  283. ;; Stop here rather than continuing
  284. ;; looking for not-allowed patterns.
  285. ;; We do a complete scan elsewhere.
  286. (setq ret nil)))))
  287. ret))
  288. rng-empty-ipattern))
  289. (defun rng-normalize-group-list (ipatterns)
  290. "Normalize a list containing members of a group.
  291. Expands nested groups, removes empty members, handles notAllowed.
  292. Returns a pair whose car says whether the list is nullable and whose
  293. cdr is the normalized list."
  294. (let ((nullable t)
  295. (result nil)
  296. member)
  297. (while ipatterns
  298. (setq member (car ipatterns))
  299. (setq ipatterns (cdr ipatterns))
  300. (when nullable
  301. (setq nullable (rng-ipattern-get-nullable member)))
  302. (cond ((eq (rng-ipattern-get-type member) 'group)
  303. (setq result
  304. (nconc (reverse (rng-ipattern-get-child member))
  305. result)))
  306. ((eq member rng-not-allowed-ipattern)
  307. (setq result (list rng-not-allowed-ipattern))
  308. (setq ipatterns nil))
  309. ((not (eq member rng-empty-ipattern))
  310. (setq result (cons member result)))))
  311. (cons nullable (nreverse result))))
  312. (defun rng-intern-interleave (ipatterns)
  313. (or (rng-intern-group-shortcut ipatterns)
  314. (let* ((tem (rng-normalize-interleave-list ipatterns))
  315. (normalized (cdr tem)))
  316. (or (rng-intern-group-shortcut normalized)
  317. (let ((key (cons 'interleave
  318. (mapcar 'rng-ipattern-get-index normalized))))
  319. (or (rng-get-ipattern key)
  320. (rng-put-ipattern key
  321. 'interleave
  322. nil
  323. normalized
  324. (car tem))))))))
  325. (defun rng-normalize-interleave-list (ipatterns)
  326. "Normalize a list containing members of an interleave.
  327. Expands nested groups, removes empty members, handles notAllowed.
  328. Returns a pair whose car says whether the list is nullable and whose
  329. cdr is the normalized list."
  330. (let ((nullable t)
  331. (result nil)
  332. member)
  333. (while ipatterns
  334. (setq member (car ipatterns))
  335. (setq ipatterns (cdr ipatterns))
  336. (when nullable
  337. (setq nullable (rng-ipattern-get-nullable member)))
  338. (cond ((eq (rng-ipattern-get-type member) 'interleave)
  339. (setq result
  340. (append (rng-ipattern-get-child member)
  341. result)))
  342. ((eq member rng-not-allowed-ipattern)
  343. (setq result (list rng-not-allowed-ipattern))
  344. (setq ipatterns nil))
  345. ((not (eq member rng-empty-ipattern))
  346. (setq result (cons member result)))))
  347. (cons nullable (sort result 'rng-compare-ipattern))))
  348. ;; Would be cleaner if this didn't modify IPATTERNS.
  349. (defun rng-intern-choice (ipatterns)
  350. "Return a choice ipattern for the list of choices in IPATTERNS.
  351. May alter IPATTERNS."
  352. (or (rng-intern-choice-shortcut ipatterns)
  353. (let* ((tem (rng-normalize-choice-list ipatterns))
  354. (normalized (cdr tem)))
  355. (or (rng-intern-choice-shortcut normalized)
  356. (rng-intern-choice1 normalized (car tem))))))
  357. (defun rng-intern-optional (ipattern)
  358. (cond ((rng-ipattern-get-nullable ipattern) ipattern)
  359. ((eq ipattern rng-not-allowed-ipattern) rng-empty-ipattern)
  360. (t (rng-intern-choice1
  361. ;; This is sorted since the empty pattern
  362. ;; is before everything except not allowed.
  363. ;; It cannot have a duplicate empty pattern,
  364. ;; since it is not nullable.
  365. (cons rng-empty-ipattern
  366. (if (eq (rng-ipattern-get-type ipattern) 'choice)
  367. (rng-ipattern-get-child ipattern)
  368. (list ipattern)))
  369. t))))
  370. (defun rng-intern-choice1 (normalized nullable)
  371. (let ((key (cons 'choice
  372. (mapcar 'rng-ipattern-get-index normalized))))
  373. (or (rng-get-ipattern key)
  374. (rng-put-ipattern key
  375. 'choice
  376. nil
  377. normalized
  378. nullable))))
  379. (defun rng-intern-choice-shortcut (ipatterns)
  380. "Try to shortcut interning a choice list.
  381. If successful, return the interned pattern. Otherwise return nil."
  382. (while (and ipatterns
  383. (eq (car ipatterns)
  384. rng-not-allowed-ipattern))
  385. (setq ipatterns (cdr ipatterns)))
  386. (if ipatterns
  387. (let ((ret (car ipatterns)))
  388. (setq ipatterns (cdr ipatterns))
  389. (while (and ipatterns ret)
  390. (or (eq (car ipatterns) rng-not-allowed-ipattern)
  391. (eq (car ipatterns) ret)
  392. (setq ret nil))
  393. (setq ipatterns (cdr ipatterns)))
  394. ret)
  395. rng-not-allowed-ipattern))
  396. (defun rng-normalize-choice-list (ipatterns)
  397. "Normalize a list of choices.
  398. Expands nested choices, removes not-allowed members, sorts by index
  399. and removes duplicates. Return a pair whose car says whether the
  400. list is nullable and whose cdr is the normalized list."
  401. (let ((sorted t)
  402. (nullable nil)
  403. (head (cons nil ipatterns)))
  404. (let ((tail head)
  405. (final-tail nil)
  406. (prev-index -100)
  407. (cur ipatterns)
  408. member)
  409. ;; the cdr of tail is always cur
  410. (while cur
  411. (setq member (car cur))
  412. (or nullable
  413. (setq nullable (rng-ipattern-get-nullable member)))
  414. (cond ((eq (rng-ipattern-get-type member) 'choice)
  415. (setq final-tail
  416. (append (rng-ipattern-get-child member)
  417. final-tail))
  418. (setq cur (cdr cur))
  419. (setq sorted nil)
  420. (setcdr tail cur))
  421. ((eq member rng-not-allowed-ipattern)
  422. (setq cur (cdr cur))
  423. (setcdr tail cur))
  424. (t
  425. (if (and sorted
  426. (let ((cur-index (rng-ipattern-get-index member)))
  427. (if (>= prev-index cur-index)
  428. (or (= prev-index cur-index) ; will remove it
  429. (setq sorted nil)) ; won't remove it
  430. (setq prev-index cur-index)
  431. ;; won't remove it
  432. nil)))
  433. (progn
  434. ;; remove it
  435. (setq cur (cdr cur))
  436. (setcdr tail cur))
  437. ;; don't remove it
  438. (setq tail cur)
  439. (setq cur (cdr cur))))))
  440. (setcdr tail final-tail))
  441. (setq head (cdr head))
  442. (cons nullable
  443. (if sorted
  444. head
  445. (rng-uniquify-eq (sort head 'rng-compare-ipattern))))))
  446. (defun rng-compare-ipattern (p1 p2)
  447. (< (rng-ipattern-get-index p1)
  448. (rng-ipattern-get-index p2)))
  449. ;;; Name classes
  450. (defsubst rng-name-class-contains (nc nm)
  451. (if (consp nc)
  452. (equal nm nc)
  453. (rng-name-class-contains1 nc nm)))
  454. (defun rng-name-class-contains1 (nc nm)
  455. (let ((type (aref nc 0)))
  456. (cond ((eq type 'any-name) t)
  457. ((eq type 'any-name-except)
  458. (not (rng-name-class-contains (aref nc 1) nm)))
  459. ((eq type 'ns-name)
  460. (eq (car nm) (aref nc 1)))
  461. ((eq type 'ns-name-except)
  462. (and (eq (car nm) (aref nc 1))
  463. (not (rng-name-class-contains (aref nc 2) nm))))
  464. ((eq type 'choice)
  465. (let ((choices (aref nc 1))
  466. (ret nil))
  467. (while choices
  468. (if (rng-name-class-contains (car choices) nm)
  469. (progn
  470. (setq choices nil)
  471. (setq ret t))
  472. (setq choices (cdr choices))))
  473. ret)))))
  474. (defun rng-name-class-possible-names (nc accum)
  475. "Return a list of possible names that nameclass NC can match.
  476. Each possible name should be returned as a (NAMESPACE . LOCAL-NAME)
  477. pair, where NAMESPACE is a symbol or nil and LOCAL-NAME is a string.
  478. NAMESPACE, if nil, matches the absent namespace. ACCUM is a list of
  479. names which should be appended to the returned list. The returned
  480. list may contain duplicates."
  481. (if (consp nc)
  482. (cons nc accum)
  483. (when (eq (aref nc 0) 'choice)
  484. (let ((members (aref nc 1)) member)
  485. (while members
  486. (setq member (car members))
  487. (setq accum
  488. (if (consp member)
  489. (cons member accum)
  490. (rng-name-class-possible-names member
  491. accum)))
  492. (setq members (cdr members)))))
  493. accum))
  494. ;;; Debugging utilities
  495. (defun rng-ipattern-to-string (ipattern)
  496. (let ((type (rng-ipattern-get-type ipattern)))
  497. (cond ((eq type 'after)
  498. (concat (rng-ipattern-to-string
  499. (rng-ipattern-get-child ipattern))
  500. " </> "
  501. (rng-ipattern-to-string
  502. (rng-ipattern-get-after ipattern))))
  503. ((eq type 'element)
  504. (concat "element "
  505. (rng-name-class-to-string
  506. (rng-ipattern-get-name-class ipattern))
  507. ;; we can get cycles with elements so don't print it out
  508. " {...}"))
  509. ((eq type 'attribute)
  510. (concat "attribute "
  511. (rng-name-class-to-string
  512. (rng-ipattern-get-name-class ipattern))
  513. " { "
  514. (rng-ipattern-to-string
  515. (rng-ipattern-get-child ipattern))
  516. " } "))
  517. ((eq type 'empty) "empty")
  518. ((eq type 'text) "text")
  519. ((eq type 'not-allowed) "notAllowed")
  520. ((eq type 'one-or-more)
  521. (concat (rng-ipattern-to-string
  522. (rng-ipattern-get-child ipattern))
  523. "+"))
  524. ((eq type 'choice)
  525. (concat "("
  526. (mapconcat 'rng-ipattern-to-string
  527. (rng-ipattern-get-child ipattern)
  528. " | ")
  529. ")"))
  530. ((eq type 'group)
  531. (concat "("
  532. (mapconcat 'rng-ipattern-to-string
  533. (rng-ipattern-get-child ipattern)
  534. ", ")
  535. ")"))
  536. ((eq type 'interleave)
  537. (concat "("
  538. (mapconcat 'rng-ipattern-to-string
  539. (rng-ipattern-get-child ipattern)
  540. " & ")
  541. ")"))
  542. (t (symbol-name type)))))
  543. (defun rng-name-class-to-string (nc)
  544. (if (consp nc)
  545. (cdr nc)
  546. (let ((type (aref nc 0)))
  547. (cond ((eq type 'choice)
  548. (mapconcat 'rng-name-class-to-string
  549. (aref nc 1)
  550. "|"))
  551. (t (concat (symbol-name type) "*"))))))
  552. ;;; Compiling
  553. (defun rng-compile-maybe-init ()
  554. (unless rng-compile-table
  555. (setq rng-compile-table (make-hash-table :test 'eq))))
  556. (defun rng-compile-clear ()
  557. (when rng-compile-table
  558. (clrhash rng-compile-table)))
  559. (defun rng-compile (pattern)
  560. (or (gethash pattern rng-compile-table)
  561. (let ((ipattern (apply (get (car pattern) 'rng-compile)
  562. (cdr pattern))))
  563. (puthash pattern ipattern rng-compile-table)
  564. ipattern)))
  565. (put 'empty 'rng-compile 'rng-compile-empty)
  566. (put 'text 'rng-compile 'rng-compile-text)
  567. (put 'not-allowed 'rng-compile 'rng-compile-not-allowed)
  568. (put 'element 'rng-compile 'rng-compile-element)
  569. (put 'attribute 'rng-compile 'rng-compile-attribute)
  570. (put 'choice 'rng-compile 'rng-compile-choice)
  571. (put 'optional 'rng-compile 'rng-compile-optional)
  572. (put 'group 'rng-compile 'rng-compile-group)
  573. (put 'interleave 'rng-compile 'rng-compile-interleave)
  574. (put 'ref 'rng-compile 'rng-compile-ref)
  575. (put 'one-or-more 'rng-compile 'rng-compile-one-or-more)
  576. (put 'zero-or-more 'rng-compile 'rng-compile-zero-or-more)
  577. (put 'mixed 'rng-compile 'rng-compile-mixed)
  578. (put 'data 'rng-compile 'rng-compile-data)
  579. (put 'data-except 'rng-compile 'rng-compile-data-except)
  580. (put 'value 'rng-compile 'rng-compile-value)
  581. (put 'list 'rng-compile 'rng-compile-list)
  582. (defun rng-compile-not-allowed () rng-not-allowed-ipattern)
  583. (defun rng-compile-empty () rng-empty-ipattern)
  584. (defun rng-compile-text () rng-text-ipattern)
  585. (defun rng-compile-element (name-class pattern)
  586. ;; don't intern
  587. (rng-make-ipattern 'element
  588. (rng-gen-ipattern-index)
  589. (rng-compile-name-class name-class)
  590. pattern ; compile lazily
  591. nil))
  592. (defun rng-element-get-child (element)
  593. (let ((tem (rng-ipattern-get-child element)))
  594. (if (vectorp tem)
  595. tem
  596. (rng-ipattern-set-child element (rng-compile tem)))))
  597. (defun rng-compile-attribute (name-class pattern)
  598. (rng-intern-attribute (rng-compile-name-class name-class)
  599. (rng-compile pattern)))
  600. (defun rng-compile-ref (pattern name)
  601. (and (memq pattern rng-being-compiled)
  602. (rng-compile-error "Reference loop on symbol %s" name))
  603. (setq rng-being-compiled
  604. (cons pattern rng-being-compiled))
  605. (unwind-protect
  606. (rng-compile pattern)
  607. (setq rng-being-compiled
  608. (cdr rng-being-compiled))))
  609. (defun rng-compile-one-or-more (pattern)
  610. (rng-intern-one-or-more (rng-compile pattern)))
  611. (defun rng-compile-zero-or-more (pattern)
  612. (rng-intern-optional
  613. (rng-intern-one-or-more (rng-compile pattern))))
  614. (defun rng-compile-optional (pattern)
  615. (rng-intern-optional (rng-compile pattern)))
  616. (defun rng-compile-mixed (pattern)
  617. (rng-intern-interleave (cons rng-text-ipattern
  618. (list (rng-compile pattern)))))
  619. (defun rng-compile-list (pattern)
  620. (rng-intern-list (rng-compile pattern)))
  621. (defun rng-compile-choice (&rest patterns)
  622. (rng-intern-choice (mapcar 'rng-compile patterns)))
  623. (defun rng-compile-group (&rest patterns)
  624. (rng-intern-group (mapcar 'rng-compile patterns)))
  625. (defun rng-compile-interleave (&rest patterns)
  626. (rng-intern-interleave (mapcar 'rng-compile patterns)))
  627. (defun rng-compile-dt (name params)
  628. (let ((rng-dt-error-reporter 'rng-compile-error))
  629. (funcall (let ((uri (car name)))
  630. (or (get uri 'rng-dt-compile)
  631. (rng-compile-error "Unknown datatype library %s" uri)))
  632. (cdr name)
  633. params)))
  634. (defun rng-compile-data (name params)
  635. (let ((dt (rng-compile-dt name params)))
  636. (rng-intern-data (cdr dt) (car dt))))
  637. (defun rng-compile-data-except (name params pattern)
  638. (rng-intern-data-except (cdr (rng-compile-dt name params))
  639. (rng-compile pattern)))
  640. (defun rng-compile-value (name str context)
  641. (let* ((dt (cdr (rng-compile-dt name '())))
  642. (rng-dt-namespace-context-getter (list 'identity context))
  643. (obj (rng-dt-make-value dt str)))
  644. (if obj
  645. (rng-intern-value dt obj)
  646. (rng-compile-error "Value %s is not a valid instance of the datatype %s"
  647. str
  648. name))))
  649. (defun rng-compile-name-class (nc)
  650. (let ((type (car nc)))
  651. (cond ((eq type 'name) (nth 1 nc))
  652. ((eq type 'any-name) [any-name])
  653. ((eq type 'any-name-except)
  654. (vector 'any-name-except
  655. (rng-compile-name-class (nth 1 nc))))
  656. ((eq type 'ns-name)
  657. (vector 'ns-name (nth 1 nc)))
  658. ((eq type 'ns-name-except)
  659. (vector 'ns-name-except
  660. (nth 1 nc)
  661. (rng-compile-name-class (nth 2 nc))))
  662. ((eq type 'choice)
  663. (vector 'choice
  664. (mapcar 'rng-compile-name-class (cdr nc))))
  665. (t (error "Bad name-class type %s" type)))))
  666. ;;; Searching patterns
  667. ;; We write this non-recursively to avoid hitting max-lisp-eval-depth
  668. ;; on large schemas.
  669. (defun rng-map-element-attribute (function pattern accum &rest args)
  670. (let ((searched (make-hash-table :test 'eq))
  671. type todo patterns)
  672. (while (progn
  673. (setq type (car pattern))
  674. (cond ((memq type '(element attribute))
  675. (setq accum
  676. (apply function
  677. (cons pattern
  678. (cons accum args))))
  679. (setq pattern (nth 2 pattern)))
  680. ((eq type 'ref)
  681. (setq pattern (nth 1 pattern))
  682. (if (gethash pattern searched)
  683. (setq pattern nil)
  684. (puthash pattern t searched)))
  685. ((memq type '(choice group interleave))
  686. (setq todo (cons (cdr pattern) todo))
  687. (setq pattern nil))
  688. ((memq type '(one-or-more
  689. zero-or-more
  690. optional
  691. mixed))
  692. (setq pattern (nth 1 pattern)))
  693. (t (setq pattern nil)))
  694. (cond (pattern)
  695. (patterns
  696. (setq pattern (car patterns))
  697. (setq patterns (cdr patterns))
  698. t)
  699. (todo
  700. (setq patterns (car todo))
  701. (setq todo (cdr todo))
  702. (setq pattern (car patterns))
  703. (setq patterns (cdr patterns))
  704. t))))
  705. accum))
  706. (defun rng-find-element-content-pattern (pattern accum name)
  707. (if (and (eq (car pattern) 'element)
  708. (rng-search-name name (nth 1 pattern)))
  709. (cons (rng-compile (nth 2 pattern)) accum)
  710. accum))
  711. (defun rng-search-name (name nc)
  712. (let ((type (car nc)))
  713. (cond ((eq type 'name)
  714. (equal (cadr nc) name))
  715. ((eq type 'choice)
  716. (let ((choices (cdr nc))
  717. (found nil))
  718. (while (and choices (not found))
  719. (if (rng-search-name name (car choices))
  720. (setq found t)
  721. (setq choices (cdr choices))))
  722. found))
  723. (t nil))))
  724. (defun rng-find-name-class-uris (nc accum)
  725. (let ((type (car nc)))
  726. (cond ((eq type 'name)
  727. (rng-accum-namespace-uri (car (nth 1 nc)) accum))
  728. ((memq type '(ns-name ns-name-except))
  729. (rng-accum-namespace-uri (nth 1 nc) accum))
  730. ((eq type 'choice)
  731. (let ((choices (cdr nc)))
  732. (while choices
  733. (setq accum
  734. (rng-find-name-class-uris (car choices) accum))
  735. (setq choices (cdr choices))))
  736. accum)
  737. (t accum))))
  738. (defun rng-accum-namespace-uri (ns accum)
  739. (if (and ns (not (memq ns accum)))
  740. (cons ns accum)
  741. accum))
  742. ;;; Derivatives
  743. (defun rng-ipattern-text-typed-p (ipattern)
  744. (let ((memo (rng-ipattern-get-memo-text-typed ipattern)))
  745. (if (eq memo 'unknown)
  746. (rng-ipattern-set-memo-text-typed
  747. ipattern
  748. (rng-ipattern-compute-text-typed-p ipattern))
  749. memo)))
  750. (defun rng-ipattern-compute-text-typed-p (ipattern)
  751. (let ((type (rng-ipattern-get-type ipattern)))
  752. (cond ((eq type 'choice)
  753. (let ((cur (rng-ipattern-get-child ipattern))
  754. (ret nil))
  755. (while (and cur (not ret))
  756. (if (rng-ipattern-text-typed-p (car cur))
  757. (setq ret t)
  758. (setq cur (cdr cur))))
  759. ret))
  760. ((eq type 'group)
  761. (let ((cur (rng-ipattern-get-child ipattern))
  762. (ret nil)
  763. member)
  764. (while (and cur (not ret))
  765. (setq member (car cur))
  766. (if (rng-ipattern-text-typed-p member)
  767. (setq ret t))
  768. (setq cur
  769. (and (rng-ipattern-get-nullable member)
  770. (cdr cur))))
  771. ret))
  772. ((eq type 'after)
  773. (rng-ipattern-text-typed-p (rng-ipattern-get-child ipattern)))
  774. (t (and (memq type '(value list data data-except)) t)))))
  775. (defun rng-start-tag-open-deriv (ipattern nm)
  776. (or (rng-memo-map-get
  777. nm
  778. (rng-ipattern-get-memo-map-start-tag-open-deriv ipattern))
  779. (rng-ipattern-memo-start-tag-open-deriv
  780. ipattern
  781. nm
  782. (rng-compute-start-tag-open-deriv ipattern nm))))
  783. (defun rng-ipattern-memo-start-tag-open-deriv (ipattern nm deriv)
  784. (or (memq ipattern rng-const-ipatterns)
  785. (rng-ipattern-set-memo-map-start-tag-open-deriv
  786. ipattern
  787. (rng-memo-map-add nm
  788. deriv
  789. (rng-ipattern-get-memo-map-start-tag-open-deriv
  790. ipattern))))
  791. deriv)
  792. (defun rng-compute-start-tag-open-deriv (ipattern nm)
  793. (let ((type (rng-ipattern-get-type ipattern)))
  794. (cond ((eq type 'choice)
  795. (rng-transform-choice `(lambda (p)
  796. (rng-start-tag-open-deriv p ',nm))
  797. ipattern))
  798. ((eq type 'element)
  799. (if (rng-name-class-contains
  800. (rng-ipattern-get-name-class ipattern)
  801. nm)
  802. (rng-intern-after (rng-element-get-child ipattern)
  803. rng-empty-ipattern)
  804. rng-not-allowed-ipattern))
  805. ((eq type 'group)
  806. (rng-transform-group-nullable
  807. `(lambda (p) (rng-start-tag-open-deriv p ',nm))
  808. 'rng-cons-group-after
  809. ipattern))
  810. ((eq type 'interleave)
  811. (rng-transform-interleave-single
  812. `(lambda (p) (rng-start-tag-open-deriv p ',nm))
  813. 'rng-subst-interleave-after
  814. ipattern))
  815. ((eq type 'one-or-more)
  816. (rng-apply-after
  817. `(lambda (p)
  818. (rng-intern-group (list p ,(rng-intern-optional ipattern))))
  819. (rng-start-tag-open-deriv (rng-ipattern-get-child ipattern)
  820. nm)))
  821. ((eq type 'after)
  822. (rng-apply-after
  823. `(lambda (p)
  824. (rng-intern-after p
  825. ,(rng-ipattern-get-after ipattern)))
  826. (rng-start-tag-open-deriv (rng-ipattern-get-child ipattern)
  827. nm)))
  828. (t rng-not-allowed-ipattern))))
  829. (defun rng-start-attribute-deriv (ipattern nm)
  830. (or (rng-memo-map-get
  831. nm
  832. (rng-ipattern-get-memo-map-start-attribute-deriv ipattern))
  833. (rng-ipattern-memo-start-attribute-deriv
  834. ipattern
  835. nm
  836. (rng-compute-start-attribute-deriv ipattern nm))))
  837. (defun rng-ipattern-memo-start-attribute-deriv (ipattern nm deriv)
  838. (or (memq ipattern rng-const-ipatterns)
  839. (rng-ipattern-set-memo-map-start-attribute-deriv
  840. ipattern
  841. (rng-memo-map-add
  842. nm
  843. deriv
  844. (rng-ipattern-get-memo-map-start-attribute-deriv ipattern))))
  845. deriv)
  846. (defun rng-compute-start-attribute-deriv (ipattern nm)
  847. (let ((type (rng-ipattern-get-type ipattern)))
  848. (cond ((eq type 'choice)
  849. (rng-transform-choice `(lambda (p)
  850. (rng-start-attribute-deriv p ',nm))
  851. ipattern))
  852. ((eq type 'attribute)
  853. (if (rng-name-class-contains
  854. (rng-ipattern-get-name-class ipattern)
  855. nm)
  856. (rng-intern-after (rng-ipattern-get-child ipattern)
  857. rng-empty-ipattern)
  858. rng-not-allowed-ipattern))
  859. ((eq type 'group)
  860. (rng-transform-interleave-single
  861. `(lambda (p) (rng-start-attribute-deriv p ',nm))
  862. 'rng-subst-group-after
  863. ipattern))
  864. ((eq type 'interleave)
  865. (rng-transform-interleave-single
  866. `(lambda (p) (rng-start-attribute-deriv p ',nm))
  867. 'rng-subst-interleave-after
  868. ipattern))
  869. ((eq type 'one-or-more)
  870. (rng-apply-after
  871. `(lambda (p)
  872. (rng-intern-group (list p ,(rng-intern-optional ipattern))))
  873. (rng-start-attribute-deriv (rng-ipattern-get-child ipattern)
  874. nm)))
  875. ((eq type 'after)
  876. (rng-apply-after
  877. `(lambda (p)
  878. (rng-intern-after p ,(rng-ipattern-get-after ipattern)))
  879. (rng-start-attribute-deriv (rng-ipattern-get-child ipattern)
  880. nm)))
  881. (t rng-not-allowed-ipattern))))
  882. (defun rng-cons-group-after (x y)
  883. (rng-apply-after `(lambda (p) (rng-intern-group (cons p ',y)))
  884. x))
  885. (defun rng-subst-group-after (new old list)
  886. (rng-apply-after `(lambda (p)
  887. (rng-intern-group (rng-substq p ,old ',list)))
  888. new))
  889. (defun rng-subst-interleave-after (new old list)
  890. (rng-apply-after `(lambda (p)
  891. (rng-intern-interleave (rng-substq p ,old ',list)))
  892. new))
  893. (defun rng-apply-after (f ipattern)
  894. (let ((type (rng-ipattern-get-type ipattern)))
  895. (cond ((eq type 'after)
  896. (rng-intern-after
  897. (rng-ipattern-get-child ipattern)
  898. (funcall f
  899. (rng-ipattern-get-after ipattern))))
  900. ((eq type 'choice)
  901. (rng-transform-choice `(lambda (x) (rng-apply-after ,f x))
  902. ipattern))
  903. (t rng-not-allowed-ipattern))))
  904. (defun rng-start-tag-close-deriv (ipattern)
  905. (or (rng-ipattern-get-memo-start-tag-close-deriv ipattern)
  906. (rng-ipattern-set-memo-start-tag-close-deriv
  907. ipattern
  908. (rng-compute-start-tag-close-deriv ipattern))))
  909. (defconst rng-transform-map
  910. '((choice . rng-transform-choice)
  911. (group . rng-transform-group)
  912. (interleave . rng-transform-interleave)
  913. (one-or-more . rng-transform-one-or-more)
  914. (after . rng-transform-after-child)))
  915. (defun rng-compute-start-tag-close-deriv (ipattern)
  916. (let* ((type (rng-ipattern-get-type ipattern)))
  917. (if (eq type 'attribute)
  918. rng-not-allowed-ipattern
  919. (let ((transform (assq type rng-transform-map)))
  920. (if transform
  921. (funcall (cdr transform)
  922. 'rng-start-tag-close-deriv
  923. ipattern)
  924. ipattern)))))
  925. (defun rng-ignore-attributes-deriv (ipattern)
  926. (let* ((type (rng-ipattern-get-type ipattern)))
  927. (if (eq type 'attribute)
  928. rng-empty-ipattern
  929. (let ((transform (assq type rng-transform-map)))
  930. (if transform
  931. (funcall (cdr transform)
  932. 'rng-ignore-attributes-deriv
  933. ipattern)
  934. ipattern)))))
  935. (defun rng-text-only-deriv (ipattern)
  936. (or (rng-ipattern-get-memo-text-only-deriv ipattern)
  937. (rng-ipattern-set-memo-text-only-deriv
  938. ipattern
  939. (rng-compute-text-only-deriv ipattern))))
  940. (defun rng-compute-text-only-deriv (ipattern)
  941. (let* ((type (rng-ipattern-get-type ipattern)))
  942. (if (eq type 'element)
  943. rng-not-allowed-ipattern
  944. (let ((transform (assq type
  945. '((choice . rng-transform-choice)
  946. (group . rng-transform-group)
  947. (interleave . rng-transform-interleave)
  948. (one-or-more . rng-transform-one-or-more)
  949. (after . rng-transform-after-child)))))
  950. (if transform
  951. (funcall (cdr transform)
  952. 'rng-text-only-deriv
  953. ipattern)
  954. ipattern)))))
  955. (defun rng-mixed-text-deriv (ipattern)
  956. (or (rng-ipattern-get-memo-mixed-text-deriv ipattern)
  957. (rng-ipattern-set-memo-mixed-text-deriv
  958. ipattern
  959. (rng-compute-mixed-text-deriv ipattern))))
  960. (defun rng-compute-mixed-text-deriv (ipattern)
  961. (let ((type (rng-ipattern-get-type ipattern)))
  962. (cond ((eq type 'text) ipattern)
  963. ((eq type 'after)
  964. (rng-transform-after-child 'rng-mixed-text-deriv
  965. ipattern))
  966. ((eq type 'choice)
  967. (rng-transform-choice 'rng-mixed-text-deriv
  968. ipattern))
  969. ((eq type 'one-or-more)
  970. (rng-intern-group
  971. (list (rng-mixed-text-deriv
  972. (rng-ipattern-get-child ipattern))
  973. (rng-intern-optional ipattern))))
  974. ((eq type 'group)
  975. (rng-transform-group-nullable
  976. 'rng-mixed-text-deriv
  977. (lambda (x y) (rng-intern-group (cons x y)))
  978. ipattern))
  979. ((eq type 'interleave)
  980. (rng-transform-interleave-single
  981. 'rng-mixed-text-deriv
  982. (lambda (new old list) (rng-intern-interleave
  983. (rng-substq new old list)))
  984. ipattern))
  985. ((and (eq type 'data)
  986. (not (rng-ipattern-get-memo-text-typed ipattern)))
  987. ipattern)
  988. (t rng-not-allowed-ipattern))))
  989. (defun rng-end-tag-deriv (ipattern)
  990. (or (rng-ipattern-get-memo-end-tag-deriv ipattern)
  991. (rng-ipattern-set-memo-end-tag-deriv
  992. ipattern
  993. (rng-compute-end-tag-deriv ipattern))))
  994. (defun rng-compute-end-tag-deriv (ipattern)
  995. (let ((type (rng-ipattern-get-type ipattern)))
  996. (cond ((eq type 'choice)
  997. (rng-intern-choice
  998. (mapcar 'rng-end-tag-deriv
  999. (rng-ipattern-get-child ipattern))))
  1000. ((eq type 'after)
  1001. (if (rng-ipattern-get-nullable
  1002. (rng-ipattern-get-child ipattern))
  1003. (rng-ipattern-get-after ipattern)
  1004. rng-not-allowed-ipattern))
  1005. (t rng-not-allowed-ipattern))))
  1006. (defun rng-data-deriv (ipattern value)
  1007. (or (rng-memo-map-get value
  1008. (rng-ipattern-get-memo-map-data-deriv ipattern))
  1009. (and (rng-memo-map-get
  1010. (cons value (rng-namespace-context-get-no-trace))
  1011. (rng-ipattern-get-memo-map-data-deriv ipattern))
  1012. (rng-memo-map-get
  1013. (cons value (apply (car rng-dt-namespace-context-getter)
  1014. (cdr rng-dt-namespace-context-getter)))
  1015. (rng-ipattern-get-memo-map-data-deriv ipattern)))
  1016. (let* ((used-context (vector nil))
  1017. (rng-dt-namespace-context-getter
  1018. (cons 'rng-namespace-context-tracer
  1019. (cons used-context
  1020. rng-dt-namespace-context-getter)))
  1021. (deriv (rng-compute-data-deriv ipattern value)))
  1022. (rng-ipattern-memo-data-deriv ipattern
  1023. value
  1024. (aref used-context 0)
  1025. deriv))))
  1026. (defun rng-namespace-context-tracer (used getter &rest args)
  1027. (let ((context (apply getter args)))
  1028. (aset used 0 context)
  1029. context))
  1030. (defun rng-namespace-context-get-no-trace ()
  1031. (let ((tem rng-dt-namespace-context-getter))
  1032. (while (and tem (eq (car tem) 'rng-namespace-context-tracer))
  1033. (setq tem (cddr tem)))
  1034. (apply (car tem) (cdr tem))))
  1035. (defconst rng-memo-data-deriv-max-length 80
  1036. "Don't memoize data-derivs for values longer than this.")
  1037. (defun rng-ipattern-memo-data-deriv (ipattern value context deriv)
  1038. (or (memq ipattern rng-const-ipatterns)
  1039. (> (length value) rng-memo-data-deriv-max-length)
  1040. (rng-ipattern-set-memo-map-data-deriv
  1041. ipattern
  1042. (rng-memo-map-add (if context (cons value context) value)
  1043. deriv
  1044. (rng-ipattern-get-memo-map-data-deriv ipattern)
  1045. t)))
  1046. deriv)
  1047. (defun rng-compute-data-deriv (ipattern value)
  1048. (let ((type (rng-ipattern-get-type ipattern)))
  1049. (cond ((eq type 'text) ipattern)
  1050. ((eq type 'choice)
  1051. (rng-transform-choice `(lambda (p) (rng-data-deriv p ,value))
  1052. ipattern))
  1053. ((eq type 'group)
  1054. (rng-transform-group-nullable
  1055. `(lambda (p) (rng-data-deriv p ,value))
  1056. (lambda (x y) (rng-intern-group (cons x y)))
  1057. ipattern))
  1058. ((eq type 'one-or-more)
  1059. (rng-intern-group (list (rng-data-deriv
  1060. (rng-ipattern-get-child ipattern)
  1061. value)
  1062. (rng-intern-optional ipattern))))
  1063. ((eq type 'after)
  1064. (let ((child (rng-ipattern-get-child ipattern)))
  1065. (if (or (rng-ipattern-get-nullable
  1066. (rng-data-deriv child value))
  1067. (and (rng-ipattern-get-nullable child)
  1068. (rng-blank-p value)))
  1069. (rng-ipattern-get-after ipattern)
  1070. rng-not-allowed-ipattern)))
  1071. ((eq type 'data)
  1072. (if (rng-dt-make-value (rng-ipattern-get-datatype ipattern)
  1073. value)
  1074. rng-empty-ipattern
  1075. rng-not-allowed-ipattern))
  1076. ((eq type 'data-except)
  1077. (if (and (rng-dt-make-value (rng-ipattern-get-datatype ipattern)
  1078. value)
  1079. (not (rng-ipattern-get-nullable
  1080. (rng-data-deriv
  1081. (rng-ipattern-get-child ipattern)
  1082. value))))
  1083. rng-empty-ipattern
  1084. rng-not-allowed-ipattern))
  1085. ((eq type 'value)
  1086. (if (equal (rng-dt-make-value (rng-ipattern-get-datatype ipattern)
  1087. value)
  1088. (rng-ipattern-get-value-object ipattern))
  1089. rng-empty-ipattern
  1090. rng-not-allowed-ipattern))
  1091. ((eq type 'list)
  1092. (let ((tokens (split-string value))
  1093. (state (rng-ipattern-get-child ipattern)))
  1094. (while (and tokens
  1095. (not (eq state rng-not-allowed-ipattern)))
  1096. (setq state (rng-data-deriv state (car tokens)))
  1097. (setq tokens (cdr tokens)))
  1098. (if (rng-ipattern-get-nullable state)
  1099. rng-empty-ipattern
  1100. rng-not-allowed-ipattern)))
  1101. ;; don't think interleave can occur
  1102. ;; since we do text-only-deriv first
  1103. (t rng-not-allowed-ipattern))))
  1104. (defun rng-transform-multi (f ipattern interner)
  1105. (let* ((members (rng-ipattern-get-child ipattern))
  1106. (transformed (mapcar f members)))
  1107. (if (rng-members-eq members transformed)
  1108. ipattern
  1109. (funcall interner transformed))))
  1110. (defun rng-transform-choice (f ipattern)
  1111. (rng-transform-multi f ipattern 'rng-intern-choice))
  1112. (defun rng-transform-group (f ipattern)
  1113. (rng-transform-multi f ipattern 'rng-intern-group))
  1114. (defun rng-transform-interleave (f ipattern)
  1115. (rng-transform-multi f ipattern 'rng-intern-interleave))
  1116. (defun rng-transform-one-or-more (f ipattern)
  1117. (let* ((child (rng-ipattern-get-child ipattern))
  1118. (transformed (funcall f child)))
  1119. (if (eq child transformed)
  1120. ipattern
  1121. (rng-intern-one-or-more transformed))))
  1122. (defun rng-transform-after-child (f ipattern)
  1123. (let* ((child (rng-ipattern-get-child ipattern))
  1124. (transformed (funcall f child)))
  1125. (if (eq child transformed)
  1126. ipattern
  1127. (rng-intern-after transformed
  1128. (rng-ipattern-get-after ipattern)))))
  1129. (defun rng-transform-interleave-single (f subster ipattern)
  1130. (let ((children (rng-ipattern-get-child ipattern))
  1131. found)
  1132. (while (and children (not found))
  1133. (let* ((child (car children))
  1134. (transformed (funcall f child)))
  1135. (if (eq transformed rng-not-allowed-ipattern)
  1136. (setq children (cdr children))
  1137. (setq found
  1138. (funcall subster
  1139. transformed
  1140. child
  1141. (rng-ipattern-get-child ipattern))))))
  1142. (or found
  1143. rng-not-allowed-ipattern)))
  1144. (defun rng-transform-group-nullable (f conser ipattern)
  1145. "Given a group x1,...,xn,y1,...,yn where the xs are all
  1146. nullable and y1 isn't, return a choice
  1147. (conser f(x1) x2,...,xm,y1,...,yn)
  1148. |(conser f(x2) x3,...,xm,y1,...,yn)
  1149. |...
  1150. |(conser f(xm) y1,...,yn)
  1151. |(conser f(y1) y2,...,yn)"
  1152. (rng-intern-choice
  1153. (rng-transform-group-nullable-gen-choices
  1154. f
  1155. conser
  1156. (rng-ipattern-get-child ipattern))))
  1157. (defun rng-transform-group-nullable-gen-choices (f conser members)
  1158. (let ((head (car members))
  1159. (tail (cdr members)))
  1160. (if tail
  1161. (cons (funcall conser (funcall f head) tail)
  1162. (if (rng-ipattern-get-nullable head)
  1163. (rng-transform-group-nullable-gen-choices f conser tail)
  1164. nil))
  1165. (list (funcall f head)))))
  1166. (defun rng-members-eq (list1 list2)
  1167. (while (and list1
  1168. list2
  1169. (eq (car list1) (car list2)))
  1170. (setq list1 (cdr list1))
  1171. (setq list2 (cdr list2)))
  1172. (and (null list1) (null list2)))
  1173. (defun rng-ipattern-after (ipattern)
  1174. (let ((type (rng-ipattern-get-type ipattern)))
  1175. (cond ((eq type 'choice)
  1176. (rng-transform-choice 'rng-ipattern-after ipattern))
  1177. ((eq type 'after)
  1178. (rng-ipattern-get-after ipattern))
  1179. ((eq type 'not-allowed)
  1180. ipattern)
  1181. (t (error "Internal error in rng-ipattern-after: unexpected type %s" type)))))
  1182. (defun rng-unknown-start-tag-open-deriv (ipattern)
  1183. (rng-intern-after (rng-compile rng-any-content) ipattern))
  1184. (defun rng-ipattern-optionalize-elements (ipattern)
  1185. (let* ((type (rng-ipattern-get-type ipattern))
  1186. (transform (assq type rng-transform-map)))
  1187. (cond (transform
  1188. (funcall (cdr transform)
  1189. 'rng-ipattern-optionalize-elements
  1190. ipattern))
  1191. ((eq type 'element)
  1192. (rng-intern-optional ipattern))
  1193. (t ipattern))))
  1194. (defun rng-ipattern-empty-before-p (ipattern)
  1195. (let ((type (rng-ipattern-get-type ipattern)))
  1196. (cond ((eq type 'after)
  1197. (eq (rng-ipattern-get-child ipattern) rng-empty-ipattern))
  1198. ((eq type 'choice)
  1199. (let ((members (rng-ipattern-get-child ipattern))
  1200. (ret t))
  1201. (while (and members ret)
  1202. (or (rng-ipattern-empty-before-p (car members))
  1203. (setq ret nil))
  1204. (setq members (cdr members)))
  1205. ret))
  1206. (t nil))))
  1207. (defun rng-ipattern-possible-start-tags (ipattern accum)
  1208. (let ((type (rng-ipattern-get-type ipattern)))
  1209. (cond ((eq type 'after)
  1210. (rng-ipattern-possible-start-tags
  1211. (rng-ipattern-get-child ipattern)
  1212. accum))
  1213. ((memq type '(choice interleave))
  1214. (let ((members (rng-ipattern-get-child ipattern)))
  1215. (while members
  1216. (setq accum
  1217. (rng-ipattern-possible-start-tags (car members)
  1218. accum))
  1219. (setq members (cdr members))))
  1220. accum)
  1221. ((eq type 'group)
  1222. (let ((members (rng-ipattern-get-child ipattern)))
  1223. (while members
  1224. (setq accum
  1225. (rng-ipattern-possible-start-tags (car members)
  1226. accum))
  1227. (setq members
  1228. (and (rng-ipattern-get-nullable (car members))
  1229. (cdr members)))))
  1230. accum)
  1231. ((eq type 'element)
  1232. (if (eq (rng-element-get-child ipattern) rng-not-allowed-ipattern)
  1233. accum
  1234. (rng-name-class-possible-names
  1235. (rng-ipattern-get-name-class ipattern)
  1236. accum)))
  1237. ((eq type 'one-or-more)
  1238. (rng-ipattern-possible-start-tags
  1239. (rng-ipattern-get-child ipattern)
  1240. accum))
  1241. (t accum))))
  1242. (defun rng-ipattern-start-tag-possible-p (ipattern)
  1243. (let ((type (rng-ipattern-get-type ipattern)))
  1244. (cond ((memq type '(after one-or-more))
  1245. (rng-ipattern-start-tag-possible-p
  1246. (rng-ipattern-get-child ipattern)))
  1247. ((memq type '(choice interleave))
  1248. (let ((members (rng-ipattern-get-child ipattern))
  1249. (possible nil))
  1250. (while (and members (not possible))
  1251. (setq possible
  1252. (rng-ipattern-start-tag-possible-p (car members)))
  1253. (setq members (cdr members)))
  1254. possible))
  1255. ((eq type 'group)
  1256. (let ((members (rng-ipattern-get-child ipattern))
  1257. (possible nil))
  1258. (while (and members (not possible))
  1259. (setq possible
  1260. (rng-ipattern-start-tag-possible-p (car members)))
  1261. (setq members
  1262. (and (rng-ipattern-get-nullable (car members))
  1263. (cdr members))))
  1264. possible))
  1265. ((eq type 'element)
  1266. (not (eq (rng-element-get-child ipattern)
  1267. rng-not-allowed-ipattern)))
  1268. (t nil))))
  1269. (defun rng-ipattern-possible-attributes (ipattern accum)
  1270. (let ((type (rng-ipattern-get-type ipattern)))
  1271. (cond ((eq type 'after)
  1272. (rng-ipattern-possible-attributes (rng-ipattern-get-child ipattern)
  1273. accum))
  1274. ((memq type '(choice interleave group))
  1275. (let ((members (rng-ipattern-get-child ipattern)))
  1276. (while members
  1277. (setq accum
  1278. (rng-ipattern-possible-attributes (car members)
  1279. accum))
  1280. (setq members (cdr members))))
  1281. accum)
  1282. ((eq type 'attribute)
  1283. (rng-name-class-possible-names
  1284. (rng-ipattern-get-name-class ipattern)
  1285. accum))
  1286. ((eq type 'one-or-more)
  1287. (rng-ipattern-possible-attributes
  1288. (rng-ipattern-get-child ipattern)
  1289. accum))
  1290. (t accum))))
  1291. (defun rng-ipattern-possible-values (ipattern accum)
  1292. (let ((type (rng-ipattern-get-type ipattern)))
  1293. (cond ((eq type 'after)
  1294. (rng-ipattern-possible-values (rng-ipattern-get-child ipattern)
  1295. accum))
  1296. ((eq type 'choice)
  1297. (let ((members (rng-ipattern-get-child ipattern)))
  1298. (while members
  1299. (setq accum
  1300. (rng-ipattern-possible-values (car members)
  1301. accum))
  1302. (setq members (cdr members))))
  1303. accum)
  1304. ((eq type 'value)
  1305. (let ((value-object (rng-ipattern-get-value-object ipattern)))
  1306. (if (stringp value-object)
  1307. (cons value-object accum)
  1308. accum)))
  1309. (t accum))))
  1310. (defun rng-ipattern-required-element (ipattern)
  1311. (let ((type (rng-ipattern-get-type ipattern)))
  1312. (cond ((memq type '(after one-or-more))
  1313. (rng-ipattern-required-element (rng-ipattern-get-child ipattern)))
  1314. ((eq type 'choice)
  1315. (let* ((members (rng-ipattern-get-child ipattern))
  1316. (required (rng-ipattern-required-element (car members))))
  1317. (while (and required
  1318. (setq members (cdr members)))
  1319. (unless (equal required
  1320. (rng-ipattern-required-element (car members)))
  1321. (setq required nil)))
  1322. required))
  1323. ((eq type 'group)
  1324. (let ((members (rng-ipattern-get-child ipattern))
  1325. required)
  1326. (while (and (not (setq required
  1327. (rng-ipattern-required-element
  1328. (car members))))
  1329. (rng-ipattern-get-nullable (car members))
  1330. (setq members (cdr members))))
  1331. required))
  1332. ((eq type 'interleave)
  1333. (let ((members (rng-ipattern-get-child ipattern))
  1334. required)
  1335. (while members
  1336. (let ((tem (rng-ipattern-required-element (car members))))
  1337. (cond ((not tem)
  1338. (setq members (cdr members)))
  1339. ((not required)
  1340. (setq required tem)
  1341. (setq members (cdr members)))
  1342. ((equal required tem)
  1343. (setq members (cdr members)))
  1344. (t
  1345. (setq required nil)
  1346. (setq members nil)))))
  1347. required))
  1348. ((eq type 'element)
  1349. (let ((nc (rng-ipattern-get-name-class ipattern)))
  1350. (and (consp nc)
  1351. (not (eq (rng-element-get-child ipattern)
  1352. rng-not-allowed-ipattern))
  1353. nc))))))
  1354. (defun rng-ipattern-required-attributes (ipattern accum)
  1355. (let ((type (rng-ipattern-get-type ipattern)))
  1356. (cond ((eq type 'after)
  1357. (rng-ipattern-required-attributes (rng-ipattern-get-child ipattern)
  1358. accum))
  1359. ((memq type '(interleave group))
  1360. (let ((members (rng-ipattern-get-child ipattern)))
  1361. (while members
  1362. (setq accum
  1363. (rng-ipattern-required-attributes (car members)
  1364. accum))
  1365. (setq members (cdr members))))
  1366. accum)
  1367. ((eq type 'choice)
  1368. (let ((members (rng-ipattern-get-child ipattern))
  1369. in-all in-this new-in-all)
  1370. (setq in-all
  1371. (rng-ipattern-required-attributes (car members)
  1372. nil))
  1373. (while (and in-all (setq members (cdr members)))
  1374. (setq in-this
  1375. (rng-ipattern-required-attributes (car members) nil))
  1376. (setq new-in-all nil)
  1377. (while in-this
  1378. (when (member (car in-this) in-all)
  1379. (setq new-in-all
  1380. (cons (car in-this) new-in-all)))
  1381. (setq in-this (cdr in-this)))
  1382. (setq in-all new-in-all))
  1383. (append in-all accum)))
  1384. ((eq type 'attribute)
  1385. (let ((nc (rng-ipattern-get-name-class ipattern)))
  1386. (if (consp nc)
  1387. (cons nc accum)
  1388. accum)))
  1389. ((eq type 'one-or-more)
  1390. (rng-ipattern-required-attributes (rng-ipattern-get-child ipattern)
  1391. accum))
  1392. (t accum))))
  1393. (defun rng-compile-error (&rest args)
  1394. (signal 'rng-compile-error
  1395. (list (apply 'format args))))
  1396. (put 'rng-compile-error
  1397. 'error-conditions
  1398. '(error rng-error rng-compile-error))
  1399. (put 'rng-compile-error
  1400. 'error-message
  1401. "Incorrect schema")
  1402. ;;; External API
  1403. (defsubst rng-match-state () rng-match-state)
  1404. (defsubst rng-set-match-state (state)
  1405. (setq rng-match-state state))
  1406. (defsubst rng-match-state-equal (state)
  1407. (eq state rng-match-state))
  1408. (defun rng-schema-changed ()
  1409. (rng-ipattern-clear)
  1410. (rng-compile-clear))
  1411. (defun rng-match-init-buffer ()
  1412. (make-local-variable 'rng-compile-table)
  1413. (make-local-variable 'rng-ipattern-table)
  1414. (make-local-variable 'rng-last-ipattern-index))
  1415. (defun rng-match-start-document ()
  1416. (rng-ipattern-maybe-init)
  1417. (rng-compile-maybe-init)
  1418. (add-hook 'rng-schema-change-hook 'rng-schema-changed nil t)
  1419. (setq rng-match-state (rng-compile rng-current-schema)))
  1420. (defun rng-match-start-tag-open (name)
  1421. (rng-update-match-state (rng-start-tag-open-deriv rng-match-state
  1422. name)))
  1423. (defun rng-match-attribute-name (name)
  1424. (rng-update-match-state (rng-start-attribute-deriv rng-match-state
  1425. name)))
  1426. (defun rng-match-attribute-value (value)
  1427. (rng-update-match-state (rng-data-deriv rng-match-state
  1428. value)))
  1429. (defun rng-match-element-value (value)
  1430. (and (rng-update-match-state (rng-text-only-deriv rng-match-state))
  1431. (rng-update-match-state (rng-data-deriv rng-match-state
  1432. value))))
  1433. (defun rng-match-start-tag-close ()
  1434. (rng-update-match-state (rng-start-tag-close-deriv rng-match-state)))
  1435. (defun rng-match-mixed-text ()
  1436. (rng-update-match-state (rng-mixed-text-deriv rng-match-state)))
  1437. (defun rng-match-end-tag ()
  1438. (rng-update-match-state (rng-end-tag-deriv rng-match-state)))
  1439. (defun rng-match-after ()
  1440. (rng-update-match-state
  1441. (rng-ipattern-after rng-match-state)))
  1442. (defun rng-match-out-of-context-start-tag-open (name)
  1443. (let* ((found (rng-map-element-attribute 'rng-find-element-content-pattern
  1444. rng-current-schema
  1445. nil
  1446. name))
  1447. (content-pattern (if found
  1448. (rng-intern-choice found)
  1449. rng-not-allowed-ipattern)))
  1450. (rng-update-match-state
  1451. (rng-intern-after content-pattern rng-match-state))))
  1452. (defun rng-match-possible-namespace-uris ()
  1453. "Return a list of all the namespace URIs used in the current schema.
  1454. The absent URI is not included, so the result is always a list of symbols."
  1455. (rng-map-element-attribute (lambda (pattern accum)
  1456. (rng-find-name-class-uris (nth 1 pattern)
  1457. accum))
  1458. rng-current-schema
  1459. nil))
  1460. (defun rng-match-unknown-start-tag-open ()
  1461. (rng-update-match-state
  1462. (rng-unknown-start-tag-open-deriv rng-match-state)))
  1463. (defun rng-match-optionalize-elements ()
  1464. (rng-update-match-state
  1465. (rng-ipattern-optionalize-elements rng-match-state)))
  1466. (defun rng-match-ignore-attributes ()
  1467. (rng-update-match-state
  1468. (rng-ignore-attributes-deriv rng-match-state)))
  1469. (defun rng-match-text-typed-p ()
  1470. (rng-ipattern-text-typed-p rng-match-state))
  1471. (defun rng-match-empty-content ()
  1472. (if (rng-match-text-typed-p)
  1473. (rng-match-element-value "")
  1474. (rng-match-end-tag)))
  1475. (defun rng-match-empty-before-p ()
  1476. "Return non-nil if what can be matched before an end-tag is empty.
  1477. In other words, return non-nil if the pattern for what can be matched
  1478. for an end-tag is equivalent to empty."
  1479. (rng-ipattern-empty-before-p rng-match-state))
  1480. (defun rng-match-infer-start-tag-namespace (local-name)
  1481. (let ((ncs (rng-ipattern-possible-start-tags rng-match-state nil))
  1482. (nc nil)
  1483. (ns nil))
  1484. (while ncs
  1485. (setq nc (car ncs))
  1486. (if (and (equal (cdr nc) local-name)
  1487. (symbolp (car nc)))
  1488. (cond ((not ns)
  1489. ;; first possible namespace
  1490. (setq ns (car nc))
  1491. (setq ncs (cdr ncs)))
  1492. ((equal ns (car nc))
  1493. ;; same as first namespace
  1494. (setq ncs (cdr ncs)))
  1495. (t
  1496. ;; more than one possible namespace
  1497. (setq ns nil)
  1498. (setq ncs nil)))
  1499. (setq ncs (cdr ncs))))
  1500. ns))
  1501. (defun rng-match-nullable-p ()
  1502. (rng-ipattern-get-nullable rng-match-state))
  1503. (defun rng-match-possible-start-tag-names ()
  1504. "Return a list of possible names that would be valid for start-tags.
  1505. Each possible name is returned as a (NAMESPACE . LOCAL-NAME) pair,
  1506. where NAMESPACE is a symbol or nil (meaning the absent namespace) and
  1507. LOCAL-NAME is a string. The returned list may contain duplicates."
  1508. (rng-ipattern-possible-start-tags rng-match-state nil))
  1509. ;; This is no longer used. It might be useful so leave it in for now.
  1510. (defun rng-match-start-tag-possible-p ()
  1511. "Return non-nil if a start-tag is possible."
  1512. (rng-ipattern-start-tag-possible-p rng-match-state))
  1513. (defun rng-match-possible-attribute-names ()
  1514. "Return a list of possible names that would be valid for attributes.
  1515. See the function `rng-match-possible-start-tag-names' for
  1516. more information."
  1517. (rng-ipattern-possible-attributes rng-match-state nil))
  1518. (defun rng-match-possible-value-strings ()
  1519. "Return a list of strings that would be valid as content.
  1520. The list may contain duplicates. Typically, the list will not
  1521. be exhaustive."
  1522. (rng-ipattern-possible-values rng-match-state nil))
  1523. (defun rng-match-required-element-name ()
  1524. "Return the name of an element which must occur, or nil if none."
  1525. (rng-ipattern-required-element rng-match-state))
  1526. (defun rng-match-required-attribute-names ()
  1527. "Return a list of names of attributes which must all occur."
  1528. (rng-ipattern-required-attributes rng-match-state nil))
  1529. (defmacro rng-match-save (&rest body)
  1530. (let ((state (make-symbol "state")))
  1531. `(let ((,state rng-match-state))
  1532. (unwind-protect
  1533. (progn ,@body)
  1534. (setq rng-match-state ,state)))))
  1535. (put 'rng-match-save 'lisp-indent-function 0)
  1536. (def-edebug-spec rng-match-save t)
  1537. (defmacro rng-match-with-schema (schema &rest body)
  1538. `(let ((rng-current-schema ,schema)
  1539. rng-match-state
  1540. rng-compile-table
  1541. rng-ipattern-table
  1542. rng-last-ipattern-index)
  1543. (rng-ipattern-maybe-init)
  1544. (rng-compile-maybe-init)
  1545. (setq rng-match-state (rng-compile rng-current-schema))
  1546. ,@body))
  1547. (put 'rng-match-with-schema 'lisp-indent-function 1)
  1548. (def-edebug-spec rng-match-with-schema t)
  1549. (provide 'rng-match)
  1550. ;;; rng-match.el ends here