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