mim-syntax.el 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ;; Syntax checker for Mim (MDL).
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3. ;; Principal author K. Shane Hartman
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is distributed in the hope that it will be useful,
  6. ;; but WITHOUT ANY WARRANTY. No author or distributor
  7. ;; accepts responsibility to anyone for the consequences of using it
  8. ;; or for whether it serves any particular purpose or works at all,
  9. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  10. ;; License for full details.
  11. ;; Everyone is granted permission to copy, modify and redistribute
  12. ;; GNU Emacs, but only under the conditions described in the
  13. ;; GNU Emacs General Public License. A copy of this license is
  14. ;; supposed to have been given to you along with GNU Emacs so you
  15. ;; can know your rights and responsibilities. It should be in a
  16. ;; file named COPYING. Among other things, the copyright notice
  17. ;; and this notice must be preserved on all copies.
  18. (require 'mim-mode)
  19. (defun slow-syntax-check-mim ()
  20. "Check Mim syntax slowly.
  21. Points out the context of the error, if the syntax is incorrect."
  22. (interactive)
  23. (message "checking syntax...")
  24. (let ((stop (point-max)) point-stack current last-bracket whoops last-point)
  25. (save-excursion
  26. (goto-char (point-min))
  27. (while (and (not whoops)
  28. (re-search-forward "\\s(\\|\\s)\\|\"\\|[\\]" stop t))
  29. (setq current (preceding-char))
  30. (cond ((= current ?\")
  31. (condition-case nil
  32. (progn (re-search-forward "[^\\]\"")
  33. (setq current nil))
  34. (error (setq whoops (point)))))
  35. ((= current ?\\)
  36. (condition-case nil (forward-char 1) (error nil)))
  37. ((= (char-syntax current) ?\))
  38. (if (or (not last-bracket)
  39. (not (= (logand (lsh (aref (syntax-table) last-bracket) -8)
  40. ?\177)
  41. current)))
  42. (setq whoops (point))
  43. (setq last-point (car point-stack))
  44. (setq last-bracket (if last-point (char-after (1- last-point))))
  45. (setq point-stack (cdr point-stack))))
  46. (t
  47. (if last-point (setq point-stack (cons last-point point-stack)))
  48. (setq last-point (point))
  49. (setq last-bracket current)))))
  50. (cond ((not (or whoops last-point))
  51. (message "Syntax correct"))
  52. (whoops
  53. (goto-char whoops)
  54. (cond ((equal current ?\")
  55. (error "Unterminated string"))
  56. ((not last-point)
  57. (error "Extraneous %s" (char-to-string current)))
  58. (t
  59. (error "Mismatched %s with %s"
  60. (save-excursion
  61. (setq whoops (1- (point)))
  62. (goto-char (1- last-point))
  63. (buffer-substring (point)
  64. (min (progn (end-of-line) (point))
  65. whoops)))
  66. (char-to-string current)))))
  67. (t
  68. (goto-char last-point)
  69. (error "Unmatched %s" (char-to-string last-bracket))))))
  70. (defun fast-syntax-check-mim ()
  71. "Checks Mim syntax quickly.
  72. Answers correct or incorrect, cannot point out the error context."
  73. (interactive)
  74. (save-excursion
  75. (goto-char (point-min))
  76. (let (state)
  77. (while (and (not (eobp))
  78. (equal (car (setq state (parse-partial-sexp (point) (point-max) 0)))
  79. 0)))
  80. (if (equal (car state) 0)
  81. (message "Syntax correct")
  82. (error "Syntax incorrect")))))