Debian Codegraph @

psgml-dtd.el

raw
1 ;;;; psgml-dtd.el --- DTD parser for SGML-editing mode with parsing support
2 ;; $Id: psgml-dtd.el,v 2.30 2003/03/25 19:46:09 lenst Exp $
3
4 ;; Copyright (C) 1994 Lennart Staflin
5
6 ;; Author: Lennart Staflin <[email protected]>
7
8 ;; This program is free software; you can redistribute it and/or
9 ;; modify it under the terms of the GNU General Public License
10 ;; as published by the Free Software Foundation; either version 2
11 ;; of the License, or (at your option) any later version.
12 ;;
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; if not, write to the Free Software
20 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22
23 ;;;; Commentary:
24
25 ;; Part of major mode for editing the SGML document-markup language.
26
27
28 ;;;; Code:
29
30 (provide 'psgml-dtd)
31 (require 'psgml)
32 (require 'psgml-parse)
33 (eval-when-compile (require 'cl))
34
35 ;;;; Variables
36
37 ;; Variables used during doctype parsing and loading
38 (defvar sgml-used-pcdata nil
39 "True if model group built is mixed.")
40
41
42 ;;;; Constructing basic
43
44 (defun sgml-copy-moves (s1 s2)
45 "Copy all moves from S1 to S2, keeping their status."
46 (let ((l (sgml-state-opts s1)))
47 (while l
48 (sgml-add-opt-move s2
49 (sgml-move-token (car l))
50 (sgml-move-dest (car l)))
51 (setq l (cdr l)))
52 (setq l (sgml-state-reqs s1))
53 (while l
54 (sgml-add-req-move s2
55 (sgml-move-token (car l))
56 (sgml-move-dest (car l)))
57 (setq l (cdr l)))))
58
59 (defun sgml-copy-moves-to-opt (s1 s2)
60 "Copy all moves from S1 to S2 as optional moves."
61 (let ((l (sgml-state-opts s1)))
62 (while l
63 (sgml-add-opt-move s2
64 (sgml-move-token (car l))
65 (sgml-move-dest (car l)))
66 (setq l (cdr l)))
67 (setq l (sgml-state-reqs s1))
68 (while l
69 (sgml-add-opt-move s2
70 (sgml-move-token (car l))
71 (sgml-move-dest (car l)))
72 (setq l (cdr l)))))
73
74
75 (defun sgml-some-states-of (state)
76 ;; List of some states reachable from STATE, includes all final states
77 (let* ((states (list state))
78 (l states)
79 s ms m)
80 (while l
81 (setq s (car l)
82 ms (append (sgml-state-opts s) (sgml-state-reqs s)))
83 (while ms
84 (setq m (sgml-move-dest (car ms))
85 ms (cdr ms))
86 (unless (sgml-normal-state-p m)
87 (setq m (sgml-and-node-next m)))
88 (unless (memq m states)
89 (nconc states (list m))))
90 (setq l (cdr l)))
91 states))
92
93 (defmacro sgml-for-all-final-states (s dfa &rest forms)
94 "For all final states S in DFA do FORMS.
95 Syntax: var dfa-expr &body forms"
96 `(let ((L-states (sgml-some-states-of ,dfa))
97 ,s)
98 (while L-states
99 (when (sgml-state-final-p (setq ,s (car L-states)))
100 ,@forms)
101 (setq L-states (cdr L-states)))))
102
103 (put 'sgml-for-all-final-states 'lisp-indent-hook 2)
104 (put 'sgml-for-all-final-states 'edebug-form-hook '(symbolp &rest form))
105
106
107 ;;;; Optimization for the dfa building
108
109 (defsubst sgml-empty-state-p (s)
110 ;; True if S has no outgoing moves
111 (and (sgml-normal-state-p s)
112 (null (sgml-state-reqs s))
113 (null (sgml-state-opts s))) )
114
115 (defun sgml-one-final-state (s)
116 ;; Collapse all states that have no moves
117 ;; This is a safe optimization, useful for (..|..|..)
118 (sgml-debug "OPT one final: reqs %d opts %d"
119 (length (sgml-state-reqs s))
120 (length (sgml-state-opts s)))
121 (let ((final nil)
122 dest)
123 (loop for m in (append (sgml-state-reqs s)
124 (sgml-state-opts s))
125 do
126 (setq dest (sgml-move-dest m))
127 (when (sgml-empty-state-p dest)
128 (cond ((null final)
129 (setq final dest))
130 (t
131 (setf (sgml-move-dest m) final)))))))
132
133 (defun sgml-states-equal (s1 s2)
134 (and (= (length (sgml-state-opts s1))
135 (length (sgml-state-opts s2)))
136 (= (length (sgml-state-reqs s1))
137 (length (sgml-state-reqs s2)))
138 (loop for m in (sgml-state-opts s1)
139 always
140 (eq (sgml-move-dest m)
141 (sgml-move-dest (sgml-moves-lookup (sgml-move-token m)
142 (sgml-state-opts s2)))))
143 (loop for m in (sgml-state-reqs s1)
144 always
145 (eq (sgml-move-dest m)
146 (sgml-move-dest (sgml-moves-lookup (sgml-move-token m)
147 (sgml-state-reqs s2)))))))
148
149 (defun sgml-remove-redundant-states-1 (s)
150 ;; Remove states accessible from s with one move and equivalent to s,
151 ;; by changing the moves from s.
152 (sgml-debug "OPT redundant-1: reqs %d opts %d"
153 (length (sgml-state-reqs s))
154 (length (sgml-state-opts s)))
155 (let ((yes nil)
156 (no (list s))
157 (l (sgml-state-reqs s))
158 (nl (sgml-state-opts s))
159 dest)
160 (while (or l (setq l (prog1 nl (setq nl nil))))
161 (cond
162 ((not (sgml-normal-state-p (setq dest (sgml-move-dest (car l))))))
163 ((memq dest no))
164 ((memq dest yes))
165 ((sgml-states-equal s dest)
166 (progn (push dest yes))))
167 (setq l (cdr l)))
168 (setq l (sgml-state-opts s)
169 nl (sgml-state-reqs s))
170 (when yes
171 (sgml-debug "OPT redundant-1: success %s" (length yes))
172 (while (or l (setq l (prog1 nl (setq nl nil))))
173 (cond ((memq (sgml-move-dest (car l)) yes)
174 (setf (sgml-move-dest (car l)) s)))
175 (setq l (cdr l))))))
176
177
178
179 ;;;; Constructing
180
181 (defun sgml-make-opt (s1)
182 (when (sgml-state-reqs s1)
183 (setf (sgml-state-opts s1)
184 (nconc (sgml-state-opts s1)
185 (sgml-state-reqs s1)))
186 (setf (sgml-state-reqs s1) nil))
187 s1)
188
189 (defun sgml-make-* (s1)
190 (setq s1 (sgml-make-+ s1))
191 (when (sgml-state-reqs s1)
192 (sgml-make-opt s1))
193 (sgml-remove-redundant-states-1 s1)
194 s1)
195
196 (defun sgml-make-+ (s1)
197 (sgml-for-all-final-states s s1
198 (sgml-copy-moves-to-opt s1 s))
199 (sgml-remove-redundant-states-1 s1) ; optimize
200 s1)
201
202 (defun sgml-make-conc (s1 s2)
203 (let ((moves (append (sgml-state-reqs s1) (sgml-state-opts s1))))
204 (cond
205 (;; optimize the case where all moves from s1 goes to empty states
206 (loop for m in moves
207 always (sgml-empty-state-p (sgml-move-dest m)))
208 (loop for m in moves do (setf (sgml-move-dest m) s2))
209 (when (sgml-state-final-p s1)
210 (sgml-copy-moves s2 s1)))
211 (t ; general case
212 (sgml-for-all-final-states s s1
213 (sgml-copy-moves s2 s)
214 (sgml-remove-redundant-states-1 s)))))
215 s1)
216
217 (defun sgml-make-pcdata ()
218 (sgml-make-* (sgml-make-primitive-content-token sgml-pcdata-token)))
219
220 (defun sgml-reduce-comma (l) ; formerly sgml-reduce-,
221 (while (cdr l)
222 (setcar (cdr l)
223 (sgml-make-conc (car l) (cadr l)))
224 (setq l (cdr l)))
225 (car l))
226
227 (defun sgml-reduce-| (l)
228 (while (cdr l) ; apply the binary make-alt
229 (cond ((or (sgml-state-final-p (car l)) ; is result optional
230 (sgml-state-final-p (cadr l)))
231 (sgml-make-opt (car l))
232 (sgml-copy-moves-to-opt (cadr l) (car l)))
233 (t
234 (sgml-copy-moves (cadr l) (car l))))
235 (setcdr l (cddr l)))
236 (sgml-one-final-state (car l)) ; optimization
237 (car l))
238
239 (defun sgml-make-& (dfas)
240 (let ((&n (sgml-make-and-node dfas (sgml-make-state)))
241 (s (sgml-make-state))
242 (l dfas))
243 (while l ; For each si:
244 ;; For m in opts(si): add optional move from s to &n on token(m).
245 (loop for m in (sgml-state-opts (car l))
246 do (sgml-add-opt-move s (sgml-move-token m) &n))
247 ;; For m in reqs(si): add required move from s to &n on token(m).
248 (loop for m in (sgml-state-reqs (car l))
249 do (sgml-add-req-move s (sgml-move-token m) &n))
250 (setq l (cdr l)))
251 ;; Return s.
252 s))
253
254
255
256 ;(sgml-make-conc (sgml-make-primitive-content-token 'para) (sgml-make-primitive-content-token 'list))
257 ;(sgml-make-conc (sgml-make-& (list (sgml-make-primitive-content-token 'para) (sgml-make-primitive-content-token 'list))) (sgml-make-primitive-content-token 'foo))
258
259 ;(setq x (sgml-some-states-of (sgml-make-primitive-content-token 'para)))
260 ;(sgml-state-final-p (car x) )
261 ;(sgml-state-final-p (cadr x))
262
263
264 ;;;; Parse doctype: General
265
266 (defun sgml-skip-ts ()
267 ;; Skip over ts*
268 ;;70 ts = 5 s | EE | 60+ parameter entity reference
269 ;;For simplicity I use ps*
270 ;;65 ps = 5 s | EE | 60+ parameter entity reference | 92 comment
271 ;;*** some comments are accepted that shouldn't
272 (sgml-skip-ps))
273
274 (defun sgml-parse-character-reference (&optional dofunchar)
275 ;; *** Actually only numerical character references
276 ;; I don't know how to handel the function character references.
277 ;; For the shortrefs let's give them numeric values.
278 (if (if dofunchar
279 (sgml-parse-delim "CRO" (digit nmstart))
280 (sgml-parse-delim "CRO" (digit)))
281 (prog1 (if (sgml-is-delim "NULL" digit)
282 (string-to-int (sgml-check-nametoken))
283 (let ((spec (sgml-check-name)))
284 (or (cdr (assoc spec '(("RE" . 10)
285 ("RS" . 1)
286 ("TAB" . 9)
287 ("SPACE" . 32))))
288 ;; *** What to do with other names?
289 127)))
290 (or (sgml-parse-delim "REFC")
291 (sgml-parse-RE)))))
292
293 (defun sgml-parse-parameter-literal (&optional dofunchar)
294 (let* (lita ; flag if lita
295 (value ; accumulates literals value
296 "")
297 (original-buffer ; Buffer (entity) where lit started
298 (current-buffer))
299 temp)
300 (cond
301 ((or (sgml-parse-delim "LIT")
302 (setq lita (sgml-parse-delim "LITA")))
303 (while (not (and (eq (current-buffer) original-buffer)
304 (if lita
305 (sgml-parse-delim "LITA")
306 (sgml-parse-delim "LIT"))))
307 (cond ((eobp)
308 (or (sgml-pop-entity)
309 (sgml-error "Parameter literal unterminated")))
310 ((sgml-parse-parameter-entity-ref))
311 ((setq temp (sgml-parse-character-reference dofunchar))
312 (setq value
313 (concat value
314 (cond ((< temp 256)
315 (if enable-multibyte-characters
316 (setq temp (unibyte-char-to-multibyte temp)))
317 (format "%c" temp))
318 (t
319 (format "&#%d;" temp))))))
320 (t
321 (setq value
322 (concat value
323 (buffer-substring-no-properties
324 (point)
325 (progn (forward-char 1)
326 (if lita
327 (sgml-skip-upto ("LITA" "PERO" "CRO"))
328 (sgml-skip-upto ("LIT" "PERO" "CRO")))
329 (point))))))))
330 value))))
331
332 (defun sgml-check-parameter-literal ()
333 (or (sgml-parse-parameter-literal)
334 (sgml-parse-error "Parameter literal expected")))
335
336 (defsubst sgml-parse-connector ()
337 (sgml-skip-ps)
338 (cond ((sgml-parse-delim "SEQ")
339 (function sgml-reduce-comma)) ; formerly sgml-reduce-,
340 ((sgml-parse-delim "OR")
341 (function sgml-reduce-|))
342 ((sgml-parse-delim "AND")
343 (if sgml-xml-p
344 (sgml-error "XML forbids AND connector")
345 (function sgml-make-&)))))
346
347 (defun sgml-parse-name-group ()
348 "Parse a single name or a name group (general name case) .
349 Returns a list of strings or nil."
350 (let (names)
351 (cond
352 ((sgml-parse-delim "GRPO")
353 (sgml-skip-ps)
354 (setq names (sgml-parse-name-group)) ; *** Allows more than it should
355 (while (sgml-parse-connector)
356 (sgml-skip-ps)
357 (nconc names (sgml-parse-name-group)))
358 (sgml-check-delim "GRPC")
359 names)
360 ((setq names (sgml-parse-name))
361 (list names)))))
362
363 (defun sgml-check-name-group ()
364 (or (sgml-parse-name-group)
365 (sgml-parse-error "Expecting a name or a name group")))
366
367 (defun sgml-check-nametoken-group ()
368 "Parse a name token group, return a list of strings.
369 Case transformed for general names."
370 (sgml-skip-ps)
371 (let ((names nil))
372 (cond
373 ((sgml-parse-delim GRPO)
374 (while (progn
375 (sgml-skip-ps)
376 (push (sgml-general-case (sgml-check-nametoken)) names)
377 (sgml-parse-connector)))
378 (sgml-check-delim GRPC)
379 (nreverse names)) ; store in same order as declared
380 (t
381 (list (sgml-general-case (sgml-check-nametoken)))))))
382
383 (defun sgml-check-element-type ()
384 "Parse and check an element type, return list of strings."
385 ;;; 117 element type = [[30 generic identifier]]
386 ;;; | [[69 name group]]
387 ;;; | [[118 ranked element]]
388 ;;; | [[119 ranked group]]
389 (cond
390 ((sgml-parse-delim GRPO)
391 (when sgml-xml-p
392 (sgml-error "XML forbids name groups for the element type"))
393 (sgml-skip-ts)
394 (let ((names (list (sgml-check-name))))
395 (while (progn (sgml-skip-ts)
396 (sgml-parse-connector))
397 (sgml-skip-ts)
398 (nconc names (list (sgml-check-name))))
399 (sgml-check-delim GRPC)
400 ;; A ranked group will have a rank suffix here
401 (sgml-skip-ps)
402 (if (sgml-is-delim "NULL" digit)
403 (let ((suffix (sgml-parse-nametoken)))
404 (loop for n in names
405 collect (concat n suffix)))
406 names)))
407 (t ; gi/ranked element
408 (let ((name (sgml-check-name)))
409 (sgml-skip-ps)
410 (list (if (sgml-is-delim "NULL" digit)
411 (concat name (sgml-check-nametoken))
412 name))))))
413
414
415 (defun sgml-check-external (&optional pubid-ok)
416 (or (sgml-parse-external pubid-ok)
417 (sgml-parse-error "Expecting a PUBLIC or SYSTEM")))
418
419 ;;;; Parse doctype: notation
420
421 (defun sgml-declare-notation ()
422 ;;148 notation declaration = MDO, "NOTATION",
423 ;; 65 ps+, 41 notation name,
424 ;; 65 ps+, 149 notation identifier,
425 ;; 65 ps*, MDC
426 ;;41 notation name = 55 name
427 ;;149 notation identifier = 73 external identifier
428 (sgml-skip-ps)
429 (sgml-check-name)
430 (sgml-skip-ps)
431 (sgml-check-external t))
432
433
434 ;;;; Parse doctype: Element
435
436 (defun sgml-parse-opt ()
437 (sgml-skip-ps)
438 (cond ((or (sgml-parse-char ?o)
439 (sgml-parse-char ?O))
440 (if sgml-xml-p
441 (sgml-error "XML forbids omitted tag minimization.")
442 t))
443 ((sgml-parse-char ?-)
444 (if sgml-xml-p
445 (sgml-error "XML forbids omitted tag minimization")
446 nil))))
447
448 (defun sgml-parse-modifier ()
449 (cond ((sgml-parse-delim "PLUS")
450 (function sgml-make-+))
451 ((sgml-parse-delim "REP")
452 (function sgml-make-*))
453 ((sgml-parse-delim "OPT")
454 (function sgml-make-opt))))
455
456 (defun sgml-check-primitive-content-token ()
457 (sgml-make-primitive-content-token
458 (sgml-eltype-token
459 (sgml-lookup-eltype
460 (sgml-check-name)))))
461
462 (defun sgml-check-model-group ()
463 (sgml-skip-ps)
464 (let (el mod)
465 (cond
466 ((sgml-parse-delim "GRPO")
467 (let ((subs (list (sgml-check-model-group)))
468 (con1 nil)
469 (con2 nil))
470 (while (setq con2 (sgml-parse-connector))
471 (cond ((and con1
472 (not (eq con1 con2)))
473 (sgml-parse-error "Mixed connectors")))
474 (setq con1 con2)
475 (setq subs (nconc subs (list (sgml-check-model-group)))))
476 (sgml-check-delim "GRPC")
477 (setq el (if con1
478 (funcall con1 subs)
479 (car subs)))))
480 ((sgml-parse-rni "PCDATA") ; #PCDATA (FIXME: when changing case)
481 (setq sgml-used-pcdata t)
482 (setq el (sgml-make-pcdata)))
483 ((sgml-parse-delim "DTGO") ; data tag group
484 (when sgml-xml-p
485 (sgml-error "XML forbids DATATAG"))
486 (sgml-skip-ts)
487 (let ((tok (sgml-check-primitive-content-token)))
488 (sgml-skip-ts) (sgml-check-delim "SEQ")
489 (sgml-skip-ts) (sgml-check-data-tag-pattern)
490 (sgml-skip-ts) (sgml-check-delim "DTGC")
491 (setq el (sgml-make-conc tok (sgml-make-pcdata)))
492 (setq sgml-used-pcdata t)))
493 (t
494 (setq el (sgml-check-primitive-content-token))))
495 (setq mod (sgml-parse-modifier))
496 (if mod
497 (funcall mod el)
498 el)))
499
500 (defun sgml-check-data-tag-pattern ()
501 ;; 134 data tag pattern
502 ;; template | template group
503 (cond ((sgml-parse-delim GRPO)
504 (sgml-skip-ts)
505 (sgml-check-parameter-literal) ; data tag template,
506 (while (progn (sgml-skip-ts)
507 (sgml-parse-delim OR))
508 (sgml-skip-ts)
509 (sgml-check-parameter-literal)) ; data tag template
510 (sgml-skip-ts)
511 (sgml-check-delim GRPC))
512 (t
513 (sgml-check-parameter-literal))) ; data tag template
514 (sgml-skip-ts)
515 (when (sgml-parse-delim SEQ)
516 (sgml-check-parameter-literal))) ; data tag padding template
517
518 (defun sgml-check-content-model ()
519 (sgml-check-model-group))
520
521 (defun sgml-check-content ()
522 (sgml-skip-ps)
523 (cond ((sgml-is-delim GRPO)
524 (sgml-check-content-model))
525 (t
526 ;; ANY, CDATA, RCDATA or EMPTY
527 (let ((dc (intern (sgml-check-case (sgml-check-name)))))
528 (cond ((eq dc 'ANY)
529 (setq sgml-used-pcdata t))
530 ((eq dc 'CDATA)
531 (when sgml-xml-p
532 (sgml-error "XML forbids CDATA declared content")))
533 ((eq dc 'RCDATA)
534 (when sgml-xml-p
535 (sgml-error "XML forbids RCDATA declared content")))
536 ((eq dc 'EMPTY))
537 (t
538 (sgml-error "Exptected content model group or one of %s"
539 (if sgml-xml-p
540 "ANY or EMPTY"
541 "ANY, CDATA, RCDATA or EMPTY"))))
542 dc))))
543
544 (defun sgml-parse-exception (type)
545 (sgml-skip-ps)
546 (if (sgml-parse-char type)
547 (if sgml-xml-p
548 (sgml-error "XML forbids inclusion and exclusion exceptions")
549 (mapcar (function sgml-lookup-eltype)
550 (sgml-check-name-group)))))
551
552 (defun sgml-before-eltype-modification ()
553 ;;; (let ((merged (sgml-dtd-merged sgml-dtd-info)))
554 ;;; (when (and merged
555 ;;; (eq (sgml-dtd-eltypes sgml-dtd-info)
556 ;;; (sgml-dtd-eltypes (cdr merged))))
557 ;;; (setf (sgml-dtd-eltypes sgml-dtd-info)
558 ;;; (sgml-merge-eltypes (sgml-make-eltypes-table)
559 ;;; (sgml-dtd-eltypes sgml-dtd-info)))))
560 )
561
562 (defun sgml-declare-element ()
563 (let* ((names (sgml-check-element-type))
564 (stag-opt (sgml-parse-opt))
565 (etag-opt (sgml-parse-opt))
566 (sgml-used-pcdata nil)
567 (model (sgml-check-content))
568 (exclusions (sgml-parse-exception ?-))
569 (inclusions (sgml-parse-exception ?+)))
570 (sgml-before-eltype-modification)
571 (while names
572 (sgml-debug "Defining element %s" (car names))
573 (let ((et (sgml-lookup-eltype (car names))))
574 (setf (sgml-eltype-stag-optional et) stag-opt
575 (sgml-eltype-etag-optional et) etag-opt
576 (sgml-eltype-model et) model
577 (sgml-eltype-mixed et) sgml-used-pcdata
578 (sgml-eltype-excludes et) exclusions
579 (sgml-eltype-includes et) inclusions))
580 (setq names (cdr names)))
581 (sgml-lazy-message "Parsing doctype (%s elements)..."
582 (incf sgml-no-elements))))
583
584 ;;;; Parse doctype: Entity
585
586 (defun sgml-declare-entity ()
587 (let (name ; Name of entity
588 dest ; Entity table
589 (type 'text) ; Type of entity
590 (notation nil) ; Notation of entity
591 text ; Text of entity
592 extid ; External id
593 )
594 (cond
595 ((sgml-parse-delim "PERO") ; parameter entity declaration
596 (sgml-skip-ps)
597 (setq name (sgml-check-name t))
598 (setq dest (sgml-dtd-parameters sgml-dtd-info)))
599 (t ; normal entity declaration
600 (or (sgml-parse-rni "DEFAULT")
601 (setq name (sgml-check-name t)))
602 (setq dest (sgml-dtd-entities sgml-dtd-info))))
603 (sgml-skip-ps)
604 ;;105 entity text = 66 parameter literal
605 ;; | 106 data text
606 ;; | 107 bracketed text
607 ;; | 108 external entity specification
608 (setq extid (sgml-parse-external))
609 (setq text
610 (cond
611 (extid ; external entity specification =
612 ; 73 external identifier,
613 ; (65 ps+, 109+ entity type)?
614 (sgml-skip-ps)
615 (let ((tn (sgml-parse-entity-type)))
616 (setq type (or (car tn) 'text))
617 (unless (eq (cdr tn) "")
618 (setq notation (cdr tn))))
619 extid)
620 ((sgml-startnm-char-next)
621 (let ((token (intern (sgml-check-case (sgml-check-name)))))
622 (sgml-skip-ps)
623 (when (and sgml-xml-p
624 (memq token '(CDATA SDATA PI STARTTAG ENDTAG MS MD)))
625 (sgml-error "XML forbids %s entities"
626 (upcase (symbol-name token))))
627 (cond
628 ((memq token '(CDATA SDATA)) ; data text ***
629 (setq type token)
630 (sgml-check-parameter-literal))
631 ((eq token 'PI)
632 (concat "<?" (sgml-check-parameter-literal) ">"))
633 ((eq token 'STARTTAG)
634 (sgml-start-tag-of (sgml-check-parameter-literal)))
635 ((eq token 'ENDTAG)
636 (sgml-end-tag-of (sgml-check-parameter-literal)))
637 ((eq token 'MS) ; marked section
638 (concat "<![" (sgml-check-parameter-literal) "]]>"))
639 ((eq token 'MD) ; Markup declaration
640 (concat "<!" (sgml-check-parameter-literal) ">")))))
641 ((sgml-check-parameter-literal))))
642 (when dest
643 (sgml-entity-declare name dest type text notation))))
644
645
646 (defun sgml-parse-entity-type ()
647 ;;109+ entity type = "SUBDOC"
648 ;; | (("CDATA" | "NDATA" | "SDATA"),
649 ;; 65 ps+,
650 ;; 41 notation name,
651 ;; 149.2+ data attribute specification?)
652 (let ((type (sgml-parse-name))
653 (notation nil))
654 (when type
655 (setq type (intern (sgml-check-case type)))
656 (when (and sgml-xml-p (memq type '(SUBDOC CDATA SDATA)))
657 (sgml-error "XML forbids %s entities"
658 (upcase (symbol-name type))))
659 (cond ((eq type 'SUBDOC))
660 ((memq type '(CDATA NDATA SDATA))
661 (sgml-skip-ps)
662 (setq notation (sgml-parse-name))
663 ;;149.2+ data attribute specification
664 ;; = 65 ps+, DSO,
665 ;; 31 attribute specification list,
666 ;; 5 s*, DSC
667 (sgml-skip-ps)
668 (when (sgml-parse-delim DSO)
669 (sgml-parse-attribute-specification-list)
670 (sgml-parse-s)
671 (sgml-check-delim DSC)))
672 (t (sgml-error "Illegal entity type: %s" type))))
673 (cons type notation)))
674
675
676 ;;;; Parse doctype: Attlist
677
678 (defun sgml-declare-attlist ()
679 (let* ((assnot (cond ((sgml-parse-rni "NOTATION")
680 (when sgml-xml-p
681 (sgml-error "XML forbids data attribute declarations"))
682 (sgml-skip-ps)
683 t)))
684 (assel (sgml-check-name-group))
685 (attlist nil)
686 (attdef nil))
687 (when (and sgml-xml-p (> (length assel) 1))
688 (sgml-error "XML forbids name groups for an associated element type"))
689 (while (setq attdef (sgml-parse-attribute-definition))
690 (push attdef attlist))
691 (setq attlist (nreverse attlist))
692 (unless assnot
693 (sgml-before-eltype-modification)
694 (loop for elname in assel do
695 (setf (sgml-eltype-attlist (sgml-lookup-eltype elname))
696 (sgml-merge-attlists
697 (sgml-eltype-attlist
698 (sgml-lookup-eltype elname))
699 attlist))))))
700
701 (defun sgml-merge-attlists (old new)
702 (setq old (nreverse (copy-sequence old)))
703 (loop for att in new do
704 (unless (assoc (car att) old)
705 (setq old (cons att old))))
706 (nreverse old))
707
708 (defun sgml-parse-attribute-definition ()
709 (sgml-skip-ps)
710 (if (sgml-is-delim "MDC") ; End of attlist?
711 nil
712 (sgml-make-attdecl (sgml-check-name)
713 (sgml-check-declared-value)
714 (sgml-check-default-value))))
715
716 (defun sgml-check-declared-value ()
717 (sgml-skip-ps)
718 (let ((type 'name-token-group)
719 (names nil))
720 (unless (eq (following-char) ?\()
721 (setq type (intern (sgml-check-case (sgml-check-name))))
722 (sgml-validate-declared-value type)
723 (sgml-skip-ps))
724 (when (memq type '(name-token-group NOTATION))
725 (setq names (sgml-check-nametoken-group)))
726 (sgml-make-declared-value type names)))
727
728 (defun sgml-validate-declared-value (type)
729 (unless (memq type
730 '(CDATA
731 ENTITY
732 ENTITIES
733 ID
734 IDREF
735 IDREFS
736 NAME
737 NAMES
738 NMTOKEN
739 NMTOKENS
740 NOTATION
741 NUMBER
742 NUMBERS
743 NUTOKEN
744 NUTOKENS))
745 (sgml-error "Invalid attribute declared value: %s" type))
746 (when (and sgml-xml-p (memq type
747 '(NAME NAMES NUMBER NUMBERS NUTOKEN NUTOKENS)))
748 (sgml-error "XML forbids %s attributes" (upcase (symbol-name type)))))
749
750 (defun sgml-check-default-value ()
751 (sgml-skip-ps)
752 (let* ((rni (sgml-parse-rni))
753 (key (if rni (intern (sgml-check-case (sgml-check-name))))))
754 (if rni (sgml-validate-default-value-rn key))
755 (sgml-skip-ps)
756 (sgml-make-default-value
757 key
758 (if (or (not rni) (eq key 'FIXED))
759 (sgml-check-attribute-value-specification)))))
760
761 (defun sgml-validate-default-value-rn (rn)
762 (unless (memq rn '(REQUIRED FIXED CURRENT CONREF IMPLIED))
763 (sgml-error "Unknown reserved name: %s"
764 (upcase (symbol-name rn))))
765 (when (and sgml-xml-p (memq rn '(CURRENT CONREF)))
766 (sgml-error "XML forbids #%s attributes"
767 (upcase (symbol-name rn)))))
768
769
770
771 ;;;; Parse doctype: Shortref
772
773 ;;;150 short reference mapping declaration = MDO, "SHORTREF",
774 ;;; [[65 ps]]+, [[151 map name]],
775 ;;; ([[65 ps]]+, [[66 parameter literal]],
776 ;;; [[65 ps]]+, [[55 name]])+,
777 ;;; [[65 ps]]*, MDC
778
779 (defun sgml-declare-shortref ()
780 (let ((mapname (sgml-check-name))
781 mappings literal name)
782 (while (progn
783 (sgml-skip-ps)
784 (setq literal (sgml-parse-parameter-literal 'dofunchar)))
785 (sgml-skip-ps)
786 (setq name (sgml-check-name t))
787 (push (cons literal name) mappings))
788 (sgml-add-shortref-map
789 (sgml-dtd-shortmaps sgml-dtd-info)
790 mapname
791 (sgml-make-shortmap mappings))))
792
793 ;;;152 short reference use declaration = MDO, "USEMAP",
794 ;;; [[65 ps]]+, [[153 map specification]],
795 ;;; ([[65 ps]]+, [[72 associated element type]])?,
796 ;;; [[65 ps]]*, MDC
797
798 (defun sgml-do-usemap-element (mapname)
799 ;; This is called from sgml-do-usemap with the mapname
800 (sgml-before-eltype-modification)
801 (loop for e in (sgml-parse-name-group) do
802 (setf (sgml-eltype-shortmap (sgml-lookup-eltype e sgml-dtd-info))
803 (if (null mapname)
804 'empty
805 mapname))))
806
807
808 ;;;; Parse doctype
809
810 (defun sgml-check-dtd-subset ()
811 (let ((sgml-parsing-dtd t)
812 (eref sgml-current-eref))
813 (while
814 (progn
815 (setq sgml-markup-start (point))
816 (cond
817 ((and (eobp) (eq sgml-current-eref eref))
818 nil)
819 ((sgml-parse-ds))
820 ((sgml-parse-markup-declaration 'dtd))
821 ((sgml-parse-delim "MS-END")))))))
822
823
824 ;;;; Save DTD: compute translation
825
826 (defvar sgml-translate-table nil)
827
828 (defun sgml-translate-node (node)
829 (assert (not (numberp node)))
830 (let ((tp (assq node sgml-translate-table)))
831 (unless tp
832 (setq tp (cons node (length sgml-translate-table)))
833 (nconc sgml-translate-table (list tp)))
834 (cdr tp)))
835
836 (defun sgml-translate-moves (moves)
837 (while moves
838 (sgml-translate-node (sgml-move-dest (car moves)))
839 (setq moves (cdr moves))))
840
841 (defun sgml-translate-model (model)
842 (let* ((sgml-translate-table (list (cons model 0)))
843 (p sgml-translate-table))
844 (while p
845 (cond ((sgml-normal-state-p (caar p))
846 (sgml-translate-moves (sgml-state-opts (caar p)))
847 (sgml-translate-moves (sgml-state-reqs (caar p))))
848 (t
849 (sgml-translate-node (sgml-and-node-next (caar p)))))
850 (setq p (cdr p)))
851 sgml-translate-table))
852
853 ;;;; Save DTD: binary coding
854
855 (defvar sgml-code-token-numbers nil)
856 (defvar sgml-code-xlate nil)
857
858 (defsubst sgml-code-xlate (node)
859 ;;(let ((x (cdr (assq node sgml-code-xlate)))) (assert x) x)
860 (cdr (assq node sgml-code-xlate)))
861
862 (defun sgml-code-number (num)
863 (if (> num sgml-max-single-octet-number)
864 (insert (+ (lsh (- num sgml-max-single-octet-number) -8)
865 sgml-max-single-octet-number 1)
866 (logand (- num sgml-max-single-octet-number) 255))
867 (insert num)))
868
869 (defun sgml-code-token-number (token)
870 (let ((bp (assq token sgml-code-token-numbers)))
871 (unless bp
872 (setq sgml-code-token-numbers
873 (nconc sgml-code-token-numbers
874 (list (setq bp (cons token
875 (length sgml-code-token-numbers)))))))
876 (cdr bp)))
877
878 (defun sgml-code-token (token)
879 (sgml-code-number (sgml-code-token-number token)))
880
881 (defmacro sgml-code-sequence (loop-c &rest body)
882 "Produce the binary coding of a counted sequence from a list.
883 Syntax: (var seq) &body forms
884 FORMS should produce the binary coding of element in VAR."
885 (let ((var (car loop-c))
886 (seq (cadr loop-c)))
887 `(let ((seq ,seq))
888 (sgml-code-number (length seq))
889 (loop for ,var in seq
890 do ,@body))))
891
892 (put 'sgml-code-sequence 'lisp-indent-hook 1)
893 (put 'sgml-code-sequence 'edbug-forms-hook '(sexp &rest form))
894
895 (defun sgml-code-sexp (sexp)
896 (let ((standard-output (current-buffer)))
897 (prin1 sexp)
898 (terpri)))
899
900 (defun sgml-code-tokens (l)
901 (sgml-code-sequence (x l)
902 (sgml-code-token x)))
903
904 (defsubst sgml-code-move (m)
905 (sgml-code-token (sgml-move-token m))
906 (insert (sgml-code-xlate (sgml-move-dest m))))
907
908 (defun sgml-code-model (m)
909 (let ((sgml-code-xlate (sgml-translate-model m)))
910 (sgml-code-sequence (s sgml-code-xlate) ; s is (node . number)
911 (setq s (car s)) ; s is node
912 (cond
913 ((sgml-normal-state-p s)
914 (assert (and (< (length (sgml-state-opts s)) 255)
915 (< (length (sgml-state-reqs s)) 256)))
916 (sgml-code-sequence (x (sgml-state-opts s))
917 (sgml-code-move x))
918 (sgml-code-sequence (x (sgml-state-reqs s))
919 (sgml-code-move x)))
920 (t ; s is a &-node
921 (insert 255) ; Tag &-node
922 (insert (sgml-code-xlate (sgml-and-node-next s)))
923 (sgml-code-sequence (m (sgml-and-node-dfas s))
924 (sgml-code-model m)))))))
925
926 (defun sgml-code-element (et)
927 (sgml-code-sexp (sgml-eltype-all-miscdata et))
928 (cond
929 ((not (sgml-eltype-defined et))
930 (insert 128))
931 (t
932 (insert (sgml-eltype-flags et))
933 (let ((c (sgml-eltype-model et)))
934 (cond ((eq c sgml-cdata) (insert 0))
935 ((eq c sgml-rcdata) (insert 1))
936 ((eq c sgml-empty) (insert 2))
937 ((eq c sgml-any) (insert 3))
938 ((null c) (insert 4))
939 (t
940 (assert (sgml-model-group-p c))
941 (insert 128)
942 (sgml-code-model c))))
943 (sgml-code-tokens (sgml-eltype-includes et))
944 (sgml-code-tokens (sgml-eltype-excludes et)))))
945
946
947 (defun sgml-code-dtd (dtd)
948 "Produce the binary coding of the current DTD into the current buffer."
949 (sgml-code-sexp (sgml-dtd-dependencies dtd))
950 (sgml-code-sexp (sgml-dtd-parameters dtd))
951 (sgml-code-sexp (sgml-dtd-doctype dtd))
952 (let ((done 0) ; count written elements
953 tot)
954 (setq sgml-code-token-numbers nil)
955 (sgml-code-token-number sgml-pcdata-token) ; Make #PCDATA token 0
956 (sgml-map-eltypes ; Assign numbers to all tokens
957 (function (lambda (et)
958 (sgml-code-token-number (sgml-eltype-token et))))
959 dtd nil t)
960 (setq tot (length sgml-code-token-numbers))
961 ;; Produce the counted sequence of element type names
962 (sgml-code-sequence (pair (cdr sgml-code-token-numbers))
963 (sgml-code-sexp (sgml-eltype-name (car pair))))
964 ;; Produce the counted sequence of element types
965 (sgml-code-sequence (pair (cdr sgml-code-token-numbers))
966 (setq done (1+ done))
967 (sgml-code-element (car pair))
968 (sgml-lazy-message "Saving DTD %d%% done" (/ (* 100 done) tot)))
969 (sgml-code-sexp (sgml-dtd-entities dtd))
970 (sgml-code-sexp (sgml-dtd-shortmaps dtd))
971 (sgml-code-sexp (sgml-dtd-notations dtd))))
972
973
974 ;;;; Save DTD
975
976 (defun sgml-save-dtd (file)
977 "Save the parsed dtd on FILE."
978 (interactive
979 (let* ((tem (expand-file-name
980 (or sgml-default-dtd-file
981 (sgml-default-dtd-file))))
982 (dir (file-name-directory tem))
983 (nam (file-name-nondirectory tem)))
984 (list
985 (read-file-name "Save DTD in: " dir tem nil nam))))
986 (setq file (expand-file-name file))
987 (when (equal file (buffer-file-name))
988 (error "Would clobber current file"))
989 (sgml-need-dtd)
990 (sgml-push-to-entity (sgml-make-entity "#SAVE" nil ""))
991 (sgml-write-dtd sgml-dtd-info file)
992 (sgml-pop-entity)
993 (setq sgml-default-dtd-file
994 (if (equal (expand-file-name default-directory)
995 (file-name-directory file))
996 (file-name-nondirectory file)
997 file))
998 (setq sgml-loaded-dtd file))
999
1000 (defun sgml-write-dtd (dtd file)
1001 "Save the parsed DTD in FILE.
1002 Construct the binary coded DTD (bdtd) in the current buffer."
1003 (sgml-set-buffer-multibyte nil)
1004 (insert
1005 ";;; This file was created by psgml on " (current-time-string)
1006 " -*-coding:binary-*-\n"
1007 "(sgml-saved-dtd-version 7)\n")
1008 (let ((print-escape-multibyte t))
1009 (sgml-code-dtd dtd))
1010 (set 'file-type 1)
1011 (let ((coding-system-for-write 'no-conversion))
1012 (write-region (point-min) (point-max) file)))
1013
1014
1015 ;;; psgml-dtd.el ends here