/[emacs]/emacs/lisp/progmodes/antlr-mode.el
ViewVC logotype

Diff of /emacs/lisp/progmodes/antlr-mode.el

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.16 by miles, Mon Sep 1 15:45:34 2003 UTC revision 1.17 by rms, Mon Oct 20 23:30:05 2003 UTC
# Line 3  Line 3 
3  ;; Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.  ;; Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4  ;;  ;;
5  ;; Author: Christoph.Wedler@sap.com  ;; Author: Christoph.Wedler@sap.com
6  ;; Keywords: languages  ;; Keywords: languages, ANTLR, code generator
7  ;; Version: (see `antlr-version' below)  ;; Version: (see `antlr-version' below)
8  ;; X-URL: http://antlr-mode.sourceforge.net/  ;; X-URL: http://antlr-mode.sourceforge.net/
9    
# Line 55  Line 55 
55  ;;  * Probably.  Show rules/dependencies for ANT like for Makefile (does ANT  ;;  * Probably.  Show rules/dependencies for ANT like for Makefile (does ANT
56  ;;    support vocabularies and grammar inheritance?), I have to look at  ;;    support vocabularies and grammar inheritance?), I have to look at
57  ;;    jde-ant.el: http://jakarta.apache.org/ant/manual/OptionalTasks/antlr.html  ;;    jde-ant.el: http://jakarta.apache.org/ant/manual/OptionalTasks/antlr.html
58    ;;  * Probably.  Make `indent-region' faster, especially in actions.  ELP
59    ;;    profiling in a class init action shows half the time is spent in
60    ;;    `antlr-next-rule', the other half in `c-guess-basic-syntax'.
61  ;;  * Unlikely.  Sather as generated language with syntax highlighting etc/.  ;;  * Unlikely.  Sather as generated language with syntax highlighting etc/.
62  ;;    Questions/problems: is sather-mode.el the standard mode for sather, is it  ;;    Questions/problems: is sather-mode.el the standard mode for sather, is it
63  ;;    still supported, what is its relationship to eiffel3.el?  Requirement:  ;;    still supported, what is its relationship to eiffel3.el?  Requirement:
# Line 173  Line 176 
176    
177  ;; get rid of byte-compile warnings  ;; get rid of byte-compile warnings
178  (eval-when-compile                      ; required and optional libraries  (eval-when-compile                      ; required and optional libraries
179    (ignore-errors (require 'cc-mode))    (require 'cc-mode)
180    (ignore-errors (require 'font-lock))    (ignore-errors (require 'font-lock))
181    (ignore-errors (require 'compile))    (ignore-errors (require 'compile))
182      ;;(ignore-errors (defun c-init-language-vars))) dangerous on Emacs!
183      ;;(ignore-errors (defun c-init-c-language-vars))) dangerous on Emacs!
184      ;;(ignore-errors (defun c-basic-common-init))   dangerous on Emacs!
185    (defvar outline-level) (defvar imenu-use-markers)    (defvar outline-level) (defvar imenu-use-markers)
186    (defvar imenu-create-index-function)    (defvar imenu-create-index-function))
187    (ignore-errors (defun c-init-language-vars)))  
188    ;; We cannot use `c-forward-syntactic-ws' directly since it is a macro since
189    ;; cc-mode-5.30 => antlr-mode compiled with older cc-mode would fail (macro
190    ;; call) when used with newer cc-mode.  Also, antlr-mode compiled with newer
191    ;; cc-mode would fail (undefined `c-forward-sws') when used with older cc-mode.
192    ;; Additional to the `defalias' below, we must set `antlr-c-forward-sws' to
193    ;; `c-forward-syntactic-ws' when `c-forward-sws' is not defined after requiring
194    ;; cc-mode.
195    (defalias 'antlr-c-forward-sws 'c-forward-sws)
196    
197    
198  ;;;;##########################################################################  ;;;;##########################################################################
# Line 193  Line 207 
207    :link '(url-link "http://antlr-mode.sourceforge.net/")    :link '(url-link "http://antlr-mode.sourceforge.net/")
208    :prefix "antlr-")    :prefix "antlr-")
209    
210  (defconst antlr-version "2.2b"  (defconst antlr-version "2.2c"
211    "ANTLR major mode version number.    "ANTLR major mode version number.
212  Check <http://antlr-mode.sourceforge.net/> for the newest.")  Check <http://antlr-mode.sourceforge.net/> for the newest.")
213    
# Line 1190  strings and actions/semantic predicates. Line 1204  strings and actions/semantic predicates.
1204  Return position before the comments after the last expression."  Return position before the comments after the last expression."
1205    (goto-char (or (ignore-errors-x (scan-sexps (point) count)) (point-max)))    (goto-char (or (ignore-errors-x (scan-sexps (point) count)) (point-max)))
1206    (prog1 (point)    (prog1 (point)
1207      (c-forward-syntactic-ws)))      (antlr-c-forward-sws)))
1208    
1209    
1210  ;;;===========================================================================  ;;;===========================================================================
# Line 1272  header.  If SKIP-COMMENT is non-nil, als Line 1286  header.  If SKIP-COMMENT is non-nil, als
1286  part."  part."
1287    (let ((pos (point))    (let ((pos (point))
1288          (class nil))          (class nil))
1289      (c-forward-syntactic-ws)      (antlr-c-forward-sws)
1290      (while (looking-at "options\\>\\|tokens\\>")      (while (looking-at "options\\>\\|tokens\\>")
1291        (setq class t)        (setq class t)
1292        (setq pos (antlr-skip-sexps 2)))        (setq pos (antlr-skip-sexps 2)))
# Line 1298  Hack: if SKIP-COMMENT is `header-only' o Line 1312  Hack: if SKIP-COMMENT is `header-only' o
1312  position before the comment after the header."  position before the comment after the header."
1313    (let* ((pos (point))    (let* ((pos (point))
1314           (pos0 pos))           (pos0 pos))
1315      (c-forward-syntactic-ws)      (antlr-c-forward-sws)
1316      (if skip-comment (setq pos0 (point)))      (if skip-comment (setq pos0 (point)))
1317      (while (looking-at "header\\>[ \t]*\\(\"\\)?")      (while (looking-at "header\\>[ \t]*\\(\"\\)?")
1318        (setq pos (antlr-skip-sexps (if (match-beginning 1) 3 2))))        (setq pos (antlr-skip-sexps (if (match-beginning 1) 3 2))))
# Line 1360  Move to the beginning of the current rul Line 1374  Move to the beginning of the current rul
1374    (let ((pos (point)))    (let ((pos (point)))
1375      (antlr-next-rule -1 nil)      (antlr-next-rule -1 nil)
1376      (let ((between (or (bobp) (< (point) pos))))      (let ((between (or (bobp) (< (point) pos))))
1377        (c-forward-syntactic-ws)        (antlr-c-forward-sws)
1378        (and between (> (point) pos) (goto-char pos)))))        (and between (> (point) pos) (goto-char pos)))))
1379    
1380    
# Line 1420  prefix arg MSG, move to `:'." Line 1434  prefix arg MSG, move to `:'."
1434                            (or (antlr-search-forward ":") (point-max))))                            (or (antlr-search-forward ":") (point-max))))
1435              (goto-char orig)              (goto-char orig)
1436              (error msg))              (error msg))
1437            (c-forward-syntactic-ws))))))            (antlr-c-forward-sws))))))
1438    
1439  (defunx antlr-beginning-of-body ()  (defunx antlr-beginning-of-body ()
1440    "Move to the first element after the `:' of the current rule."    "Move to the first element after the `:' of the current rule."
# Line 1598  This command might also set the mark lik Line 1612  This command might also set the mark lik
1612    
1613  (defun antlr-insert-option-interactive (arg)  (defun antlr-insert-option-interactive (arg)
1614    "Interactive specification for `antlr-insert-option'.    "Interactive specification for `antlr-insert-option'.
1615  Use prefix argument ARG to return \(LEVEL OPTION LOCATION)."  Return \(LEVEL OPTION LOCATION)."
1616    (barf-if-buffer-read-only)    (barf-if-buffer-read-only)
1617    (if arg (setq arg (prefix-numeric-value arg)))    (if arg (setq arg (prefix-numeric-value arg)))
1618    (unless (memq arg '(nil 1 2 3 4))    (unless (memq arg '(nil 1 2 3 4))
# Line 1627  Use prefix argument ARG to return \(LEVE Line 1641  Use prefix argument ARG to return \(LEVE
1641                        :active active))                        :active active))
1642              (sort (mapcar 'car (elt antlr-options-alists (1- level)))              (sort (mapcar 'car (elt antlr-options-alists (1- level)))
1643                    'string-lessp))))                    'string-lessp))))
1644        
1645    
1646  ;;;===========================================================================  ;;;===========================================================================
1647  ;;;  Insert option: determine section-kind  ;;;  Insert option: determine section-kind
# Line 1653  like \(AREA \. PLACE), see `antlr-option Line 1667  like \(AREA \. PLACE), see `antlr-option
1667                 (setq pos (antlr-skip-file-prelude 'header-only)))                 (setq pos (antlr-skip-file-prelude 'header-only)))
1668                ((not (eq level 3))       ; grammar or subrule options                ((not (eq level 3))       ; grammar or subrule options
1669                 (setq pos (point))                 (setq pos (point))
1670                 (c-forward-syntactic-ws))                 (antlr-c-forward-sws))
1671                ((looking-at "^\\(private[ \t\n]\\|public[ \t\n]\\|protected[ \t\n]\\)?[ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)[ \t\n]*\\(!\\)?[ \t\n]*\\(\\[\\)?")                ((looking-at "^\\(private[ \t\n]\\|public[ \t\n]\\|protected[ \t\n]\\)?[ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)[ \t\n]*\\(!\\)?[ \t\n]*\\(\\[\\)?")
1672                 ;; rule options, with complete rule header                 ;; rule options, with complete rule header
1673                 (goto-char (or (match-end 4) (match-end 3)))                 (goto-char (or (match-end 4) (match-end 3)))
# Line 1904  For OLD, see `antlr-insert-option-do'." Line 1918  For OLD, see `antlr-insert-option-do'."
1918      ;; stuff (no =, {, } or /) at point is not followed by ";"      ;; stuff (no =, {, } or /) at point is not followed by ";"
1919      (insert ";")      (insert ";")
1920      (backward-char)))      (backward-char)))
1921            
1922  (defun antlr-insert-option-space (area old)  (defun antlr-insert-option-space (area old)
1923    "Find appropriate place to insert option, insert newlines/spaces.    "Find appropriate place to insert option, insert newlines/spaces.
1924  For AREA and OLD, see `antlr-insert-option-do'."  For AREA and OLD, see `antlr-insert-option-do'."
# Line 1924  For AREA and OLD, see `antlr-insert-opti Line 1938  For AREA and OLD, see `antlr-insert-opti
1938                   (setq orig (point))                   (setq orig (point))
1939                 (goto-char orig)))                 (goto-char orig)))
1940          (skip-chars-forward " \t")          (skip-chars-forward " \t")
1941            
1942          (if (looking-at "$\\|//")          (if (looking-at "$\\|//")
1943              ;; just comment after point => skip (+ lines w/ same col comment)              ;; just comment after point => skip (+ lines w/ same col comment)
1944              (let ((same (if (> (match-end 0) (match-beginning 0))              (let ((same (if (> (match-end 0) (match-beginning 0))
# Line 1959  Used by `antlr-insert-option-do'." Line 1973  Used by `antlr-insert-option-do'."
1973    (when (and antlr-options-auto-colon    (when (and antlr-options-auto-colon
1974               (memq level '(3 4))               (memq level '(3 4))
1975               (save-excursion               (save-excursion
1976                 (c-forward-syntactic-ws)                 (antlr-c-forward-sws)
1977                 (if (eq (char-after (point)) ?\{) (antlr-skip-sexps 1))                 (if (eq (char-after (point)) ?\{) (antlr-skip-sexps 1))
1978                 (not (eq (char-after (point)) ?\:))))                 (not (eq (char-after (point)) ?\:))))
1979      (insert "\n:")      (insert "\n:")
# Line 2079  its export vocabulary is used as an impo Line 2093  its export vocabulary is used as an impo
2093                 (evocab (or default-vocab class))                 (evocab (or default-vocab class))
2094                 (ivocab nil))                 (ivocab nil))
2095            (goto-char (match-end 0))            (goto-char (match-end 0))
2096            (c-forward-syntactic-ws)            (antlr-c-forward-sws)
2097            (while (looking-at "options\\>\\|\\(tokens\\)\\>")            (while (looking-at "options\\>\\|\\(tokens\\)\\>")
2098              (if (match-beginning 1)              (if (match-beginning 1)
2099                  (antlr-skip-sexps 2)                  (antlr-skip-sexps 2)
2100                (goto-char (match-end 0))                (goto-char (match-end 0))
2101                (c-forward-syntactic-ws)                (antlr-c-forward-sws)
2102                ;; parse grammar option sections -------------------------------                ;; parse grammar option sections -------------------------------
2103                (when (eq (char-after (point)) ?\{)                (when (eq (char-after (point)) ?\{)
2104                  (let* ((beg (1+ (point)))                  (let* ((beg (1+ (point)))
# Line 2341  commentary with value `antlr-help-unknow Line 2355  commentary with value `antlr-help-unknow
2355    
2356  (defun antlr-indent-line ()  (defun antlr-indent-line ()
2357    "Indent the current line as ANTLR grammar code.    "Indent the current line as ANTLR grammar code.
2358  The indentation of non-comment lines are calculated by `c-basic-offset',  The indentation of grammar lines are calculated by `c-basic-offset',
2359  multiplied by:  multiplied by:
2360   - the level of the paren/brace/bracket depth,   - the level of the paren/brace/bracket depth,
2361   - plus 0/2/1, depending on the position inside the rule: header, body,   - plus 0/2/1, depending on the position inside the rule: header, body,
# Line 2484  ANTLR's syntax and influences the auto i Line 2498  ANTLR's syntax and influences the auto i
2498  ;;;  Mode entry  ;;;  Mode entry
2499  ;;;===========================================================================  ;;;===========================================================================
2500    
2501    (defun antlr-c-init-language-vars ()
2502      "Like `c-init-language-vars-for' when using cc-mode before v5.29."
2503      (let ((settings                       ; (cdr '(setq...)) will be optimized
2504             (if (eq antlr-language 'c++-mode)
2505                 (cdr '(setq                ;' from `c++-mode' v5.20, v5.28
2506                        c-keywords (c-identifier-re c-C++-keywords)
2507                        c-conditional-key c-C++-conditional-key
2508                        c-comment-start-regexp c-C++-comment-start-regexp
2509                        c-class-key c-C++-class-key
2510                        c-extra-toplevel-key c-C++-extra-toplevel-key
2511                        c-access-key c-C++-access-key
2512                        c-recognize-knr-p nil
2513                        c-bitfield-key c-C-bitfield-key ; v5.28
2514                        ))
2515               (cdr '(setq                  ; from `java-mode' v5.20, v5.28
2516                      c-keywords (c-identifier-re c-Java-keywords)
2517                      c-conditional-key c-Java-conditional-key
2518                      c-comment-start-regexp c-Java-comment-start-regexp
2519                      c-class-key c-Java-class-key
2520                      c-method-key nil
2521                      c-baseclass-key nil
2522                      c-recognize-knr-p nil
2523                      c-access-key c-Java-access-key ; v5.20
2524                      c-inexpr-class-key c-Java-inexpr-class-key ; v5.28
2525                      )))))
2526        (while settings
2527          (when (boundp (car settings))
2528            (ignore-errors
2529              (set (car settings) (eval (cadr settings)))))
2530          (setq settings (cddr settings)))))
2531    
2532  (defun antlr-c-common-init ()  (defun antlr-c-common-init ()
2533    "Like `c-common-init' except menu, auto-hungry and c-style stuff."    "Like `c-basic-common-init' when using cc-mode before v5.30."
2534    ;; X/Emacs 20 only    ;; X/Emacs 20 only
2535    (make-local-variable 'paragraph-start)    (make-local-variable 'paragraph-start)
2536    (make-local-variable 'paragraph-separate)    (make-local-variable 'paragraph-separate)
2537    (make-local-variable 'paragraph-ignore-fill-prefix)    (make-local-variable 'paragraph-ignore-fill-prefix)
2538    (make-local-variable 'require-final-newline)    (make-local-variable 'require-final-newline)
2539    (make-local-variable 'parse-sexp-ignore-comments)    (make-local-variable 'parse-sexp-ignore-comments)
   (make-local-variable 'indent-line-function)  
   (make-local-variable 'indent-region-function)  
2540    (make-local-variable 'comment-start)    (make-local-variable 'comment-start)
   (make-local-variable 'comment-end)  
   (make-local-variable 'comment-column)  
   (make-local-variable 'comment-start-skip)  
2541    (make-local-variable 'comment-multi-line)    (make-local-variable 'comment-multi-line)
2542    (make-local-variable 'outline-regexp)    (make-local-variable 'outline-regexp)
2543    (make-local-variable 'outline-level)    (make-local-variable 'outline-level)
# Line 2515  ANTLR's syntax and influences the auto i Line 2555  ANTLR's syntax and influences the auto i
2555    (setq paragraph-start (concat page-delimiter "\\|$")    (setq paragraph-start (concat page-delimiter "\\|$")
2556          paragraph-separate paragraph-start          paragraph-separate paragraph-start
2557          paragraph-ignore-fill-prefix t          paragraph-ignore-fill-prefix t
         require-final-newline t  
2558          parse-sexp-ignore-comments t          parse-sexp-ignore-comments t
         indent-line-function 'c-indent-line  
         indent-region-function 'c-indent-region  
         outline-regexp "[^#\n\^M]"  
         outline-level 'c-outline-level  
2559          comment-column 32          comment-column 32
         comment-start-skip "/\\*+ *\\|// *"  
2560          comment-multi-line nil          comment-multi-line nil
2561          comment-line-break-function 'c-comment-line-break-function          comment-line-break-function 'c-comment-line-break-function
2562          adaptive-fill-regexp nil          adaptive-fill-regexp nil
# Line 2563  the default language." Line 2597  the default language."
2597          (setq r nil)))                  ; no result yet          (setq r nil)))                  ; no result yet
2598      (car r)))      (car r)))
2599    
   
2600  ;;;###autoload  ;;;###autoload
2601  (defun antlr-mode ()  (defun antlr-mode ()
2602    "Major mode for editing ANTLR grammar files.    "Major mode for editing ANTLR grammar files.
2603  \\{antlr-mode-map}"  \\{antlr-mode-map}"
2604    (interactive)    (interactive)
   (c-initialize-cc-mode)                ; required when depending on cc-mode  
2605    (kill-all-local-variables)    (kill-all-local-variables)
2606      (c-initialize-cc-mode)                ; cc-mode is required
2607      (unless (fboundp 'c-forward-sws)      ; see above
2608        (fset 'antlr-c-forward-sws 'c-forward-syntactic-ws))
2609    ;; ANTLR specific ----------------------------------------------------------    ;; ANTLR specific ----------------------------------------------------------
2610    (setq major-mode 'antlr-mode    (setq major-mode 'antlr-mode
2611          mode-name "Antlr")          mode-name "Antlr")
# Line 2597  the default language." Line 2632  the default language."
2632              (concat "Antlr."              (concat "Antlr."
2633                      (cadr (assq antlr-language antlr-language-alist)))))                      (cadr (assq antlr-language antlr-language-alist)))))
2634    ;; indentation, for the C engine -------------------------------------------    ;; indentation, for the C engine -------------------------------------------
2635    (antlr-c-common-init)    (setq c-buffer-is-cc-mode antlr-language)
2636      (cond ((fboundp 'c-init-language-vars-for) ; cc-mode 5.30.5+
2637             (c-init-language-vars-for antlr-language))
2638            ((fboundp 'c-init-c-language-vars) ; cc-mode 5.30 to 5.30.4
2639             (c-init-c-language-vars)       ; not perfect, but OK
2640             (setq c-recognize-knr-p nil))
2641            ((fboundp 'c-init-language-vars) ; cc-mode 5.29
2642             (let ((init-fn 'c-init-language-vars))
2643               (funcall init-fn)))          ; is a function in v5.29
2644            (t                              ; cc-mode upto 5.28
2645             (antlr-c-init-language-vars))) ; do it myself
2646      (cond ((fboundp 'c-basic-common-init) ; cc-mode 5.30+
2647             (c-basic-common-init antlr-language (or antlr-indent-style "gnu")))
2648            (t
2649             (antlr-c-common-init)))
2650      (make-local-variable 'outline-regexp)
2651      (make-local-variable 'outline-level)
2652      (make-local-variable 'require-final-newline)
2653      (make-local-variable 'indent-line-function)
2654      (make-local-variable 'indent-region-function)
2655      (setq outline-regexp "[^#\n\^M]"
2656            outline-level 'c-outline-level) ; TODO: define own
2657      (setq require-final-newline t)
2658    (setq indent-line-function 'antlr-indent-line    (setq indent-line-function 'antlr-indent-line
2659          indent-region-function nil)     ; too lazy          indent-region-function nil)     ; too lazy
   (setq c-buffer-is-cc-mode antlr-language)  
   (if (fboundp 'c-init-language-vars)  
       (c-init-language-vars)            ; cc-mode >= v5.29  
     (let ((settings                     ; (cdr '(setq...)) will be optimized  
            (if (eq antlr-language 'c++-mode)  
                (cdr '(setq              ;' from `c++-mode' v5.20, v5.28  
                       c-keywords (c-identifier-re c-C++-keywords)  
                       c-conditional-key c-C++-conditional-key  
                       c-comment-start-regexp c-C++-comment-start-regexp  
                       c-class-key c-C++-class-key  
                       c-extra-toplevel-key c-C++-extra-toplevel-key  
                       c-access-key c-C++-access-key  
                       c-recognize-knr-p nil  
                       c-bitfield-key c-C-bitfield-key ; v5.28  
                       ))  
              (cdr '(setq                ; from `java-mode' v5.20, v5.28  
                     c-keywords (c-identifier-re c-Java-keywords)  
                     c-conditional-key c-Java-conditional-key  
                     c-comment-start-regexp c-Java-comment-start-regexp  
                     c-class-key c-Java-class-key  
                     c-method-key nil  
                     c-baseclass-key nil  
                     c-recognize-knr-p nil  
                     c-access-key c-Java-access-key ; v5.20  
                     c-inexpr-class-key c-Java-inexpr-class-key ; v5.28  
                     )))))  
       (while settings  
         (when (boundp (car settings))  
           (ignore-errors  
             (set (car settings) (eval (cadr settings)))))  
         (setq settings (cddr settings)))))  
2660    (setq comment-start "// "    (setq comment-start "// "
2661          comment-end "")          comment-end ""
2662            comment-start-skip "/\\*+ *\\|// *")
2663    ;; various -----------------------------------------------------------------    ;; various -----------------------------------------------------------------
2664    (make-local-variable 'font-lock-defaults)    (make-local-variable 'font-lock-defaults)
2665    (setq font-lock-defaults antlr-font-lock-defaults)    (setq font-lock-defaults antlr-font-lock-defaults)

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.17

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26