]> code.delx.au - gnu-emacs/blob - lisp/progmodes/antlr-mode.el
3402282bd5d62011fc02068fec6639dfc7e6450e
[gnu-emacs] / lisp / progmodes / antlr-mode.el
1 ;;; antlr-mode.el --- major mode for ANTLR grammar files
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Author: Christoph.Wedler@sap.com
7 ;; Keywords: languages, ANTLR, code generator
8 ;; Version: 2.2c
9 ;; X-URL: http://antlr-mode.sourceforge.net/
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; The Emacs package ANTLR-Mode provides: syntax highlighting for ANTLR grammar
29 ;; files, automatic indentation, menus containing rule/token definitions and
30 ;; supported options and various other things like running ANTLR from within
31 ;; Emacs.
32
33 ;; For details, check <http://antlr-mode.sourceforge.net/> or, if you prefer
34 ;; the manual style, follow all commands mentioned in the documentation of
35 ;; `antlr-mode'. ANTLR is a LL(k)-based recognition tool which generates
36 ;; lexers, parsers and tree transformers in Java, C++ or Sather and can be
37 ;; found at <http://www.antlr.org/>.
38
39 ;; Bug fixes, bug reports, improvements, and suggestions for the newest version
40 ;; are strongly appreciated.
41
42 ;; To-do/Wish-list:
43 ;;
44 ;; * Next Version [C-c C-w]. Produce HTML document with syntax highlighted
45 ;; and hyper-links (using htmlize).
46 ;; * Next Version [C-c C-u]. Insert/update special comments: each rule lists
47 ;; all rules which use the current rule. With font-lock update.
48 ;; * Next Version. Make hiding much more customizable.
49 ;; * Planned [C-c C-j]. Jump to generated coding.
50 ;; * Planned. Further support for imenu, i.e., include entries for method
51 ;; definitions at beginning of grammar class.
52 ;; * Planned [C-c C-p]. Pack/unpack rule/subrule & options (one/multi-line).
53 ;;
54 ;; * Probably. Show rules/dependencies for ANT like for Makefile (does ANT
55 ;; support vocabularies and grammar inheritance?), I have to look at
56 ;; jde-ant.el: http://jakarta.apache.org/ant/manual/OptionalTasks/antlr.html
57 ;; * Probably. Make `indent-region' faster, especially in actions. ELP
58 ;; profiling in a class init action shows half the time is spent in
59 ;; `antlr-next-rule', the other half in `c-guess-basic-syntax'.
60 ;; * Unlikely. Sather as generated language with syntax highlighting etc/.
61 ;; Questions/problems: is sather-mode.el the standard mode for sather, is it
62 ;; still supported, what is its relationship to eiffel3.el? Requirement:
63 ;; this mode must not depend on a Sather mode.
64 ;; * Unlikely. Faster syntax highlighting: sectionize the buffer into Antlr
65 ;; and action code and run special highlighting functions on these regions.
66 ;; Problems: code size, this mode would depend on font-lock internals.
67
68 ;;; Installation:
69
70 ;; This file requires Emacs-20.3, XEmacs-20.4 or higher and package cc-mode.
71
72 ;; If antlr-mode is not part of your distribution, put this file into your
73 ;; load-path and the following into your ~/.emacs:
74 ;; (autoload 'antlr-mode "antlr-mode" nil t)
75 ;; (setq auto-mode-alist (cons '("\\.g\\'" . antlr-mode) auto-mode-alist))
76 ;; (add-hook 'speedbar-load-hook ; would be too late in antlr-mode.el
77 ;; (lambda () (speedbar-add-supported-extension ".g")))
78
79 ;; I strongly recommend to use font-lock with a support mode like fast-lock,
80 ;; lazy-lock or better jit-lock (Emacs-21.1+) / lazy-shot (XEmacs).
81
82 ;; To customize, use menu item "Antlr" -> "Customize Antlr".
83
84 ;;; Code:
85
86 (eval-when-compile
87 (require 'cl))
88
89 (require 'easymenu)
90 (require 'cc-mode)
91
92 ;; Just to get the rid of the byte compiler warning. The code for
93 ;; this function and its friends are too complex for their own good.
94 (declare-function cond-emacs-xemacs-macfn "antlr-mode" (args &optional msg))
95
96 ;; General Emacs/XEmacs-compatibility compile-time macros
97 (eval-when-compile
98 (defmacro cond-emacs-xemacs (&rest args)
99 (cond-emacs-xemacs-macfn
100 args "`cond-emacs-xemacs' must return exactly one element"))
101 (defun cond-emacs-xemacs-macfn (args &optional msg)
102 (if (atom args) args
103 (and (eq (car args) :@) (null msg) ; (:@ ...spliced...)
104 (setq args (cdr args)
105 msg "(:@ ....) must return exactly one element"))
106 (let ((ignore (if (featurep 'xemacs) :EMACS :XEMACS))
107 (mode :BOTH) code)
108 (while (consp args)
109 (if (memq (car args) '(:EMACS :XEMACS :BOTH)) (setq mode (pop args)))
110 (if (atom args)
111 (or args (error "Used selector %s without elements" mode))
112 (or (eq ignore mode)
113 (push (cond-emacs-xemacs-macfn (car args)) code))
114 (pop args)))
115 (cond (msg (if (or args (cdr code)) (error msg) (car code)))
116 ((or (null args) (eq ignore mode)) (nreverse code))
117 (t (nconc (nreverse code) args))))))
118 ;; Emacs/XEmacs-compatibility `defun': remove interactive "_" for Emacs, use
119 ;; existing functions when they are `fboundp', provide shortcuts if they are
120 ;; known to be defined in a specific Emacs branch (for short .elc)
121 (defmacro defunx (name arglist &rest definition)
122 (let ((xemacsp (featurep 'xemacs)) reuses)
123 (while (memq (car definition)
124 '(:try :emacs-and-try :xemacs-and-try))
125 (if (eq (pop definition) (if xemacsp :xemacs-and-try :emacs-and-try))
126 (setq reuses (car definition)
127 definition nil)
128 (push (pop definition) reuses)))
129 (if (and reuses (symbolp reuses))
130 `(defalias ',name ',reuses)
131 (let* ((docstring (if (stringp (car definition)) (pop definition)))
132 (spec (and (not xemacsp)
133 (eq (car-safe (car definition)) 'interactive)
134 (null (cddar definition))
135 (cadar definition))))
136 (if (and (stringp spec)
137 (not (string-equal spec ""))
138 (eq (aref spec 0) ?_))
139 (setq definition
140 (cons (if (string-equal spec "_")
141 '(interactive)
142 `(interactive ,(substring spec 1)))
143 (cdr definition))))
144 (if (null reuses)
145 `(defun ,name ,arglist ,docstring
146 ,@(cond-emacs-xemacs-macfn definition))
147 ;; no dynamic docstring in this case
148 `(eval-and-compile ; no warnings in Emacs
149 (defalias ',name
150 (cond ,@(mapcar (lambda (func) `((fboundp ',func) ',func))
151 (nreverse reuses))
152 (t ,(if definition
153 `(lambda ,arglist ,docstring
154 ,@(cond-emacs-xemacs-macfn definition))
155 'ignore))))))))))
156 (defmacro ignore-errors-x (&rest body)
157 (let ((specials '((scan-sexps . 4) (scan-lists . 5)))
158 spec nils)
159 (if (and (featurep 'xemacs)
160 (null (cdr body)) (consp (car body))
161 (setq spec (assq (caar body) specials))
162 (>= (setq nils (- (cdr spec) (length (car body)))) 0))
163 `(,@(car body) ,@(make-list nils nil) t)
164 `(ignore-errors ,@body)))))
165
166 ;; More compile-time-macros
167 (eval-when-compile
168 (defmacro save-buffer-state-x (&rest body) ; similar to EMACS/lazy-lock.el
169 (let ((modified (with-no-warnings (gensym "save-buffer-state-x-modified-"))))
170 `(let ((,modified (buffer-modified-p)))
171 (unwind-protect
172 (let ((buffer-undo-list t) (inhibit-read-only t)
173 ,@(unless (featurep 'xemacs)
174 '((inhibit-point-motion-hooks t) deactivate-mark))
175 before-change-functions after-change-functions
176 buffer-file-name buffer-file-truename)
177 ,@body)
178 (and (not ,modified) (buffer-modified-p)
179 (set-buffer-modified-p nil)))))))
180 (put 'save-buffer-state-x 'lisp-indent-function 0)
181
182 ;; get rid of byte-compile warnings
183 (eval-when-compile
184 (require 'cc-mode))
185
186 (defvar outline-level)
187 (defvar imenu-use-markers)
188 (defvar imenu-create-index-function)
189
190 ;; We cannot use `c-forward-syntactic-ws' directly since it is a macro since
191 ;; cc-mode-5.30 => antlr-mode compiled with older cc-mode would fail (macro
192 ;; call) when used with newer cc-mode. Also, antlr-mode compiled with newer
193 ;; cc-mode would fail (undefined `c-forward-sws') when used with older cc-mode.
194 ;; Additional to the `defalias' below, we must set `antlr-c-forward-sws' to
195 ;; `c-forward-syntactic-ws' when `c-forward-sws' is not defined after requiring
196 ;; cc-mode.
197 (defalias 'antlr-c-forward-sws 'c-forward-sws)
198
199
200 ;;;;##########################################################################
201 ;;;; Variables
202 ;;;;##########################################################################
203
204
205 (defgroup antlr nil
206 "Major mode for ANTLR grammar files."
207 :group 'languages
208 :link '(emacs-commentary-link "antlr-mode.el")
209 :link '(url-link "http://antlr-mode.sourceforge.net/")
210 :prefix "antlr-")
211
212 (defconst antlr-version "2.2c"
213 "ANTLR major mode version number.
214 Check <http://antlr-mode.sourceforge.net/> for the newest.")
215
216
217 ;;;===========================================================================
218 ;;; Controlling ANTLR's code generator (language option)
219 ;;;===========================================================================
220
221 (defvar antlr-language nil
222 "Major mode corresponding to ANTLR's \"language\" option.
223 Set via `antlr-language-alist'. The only useful place to change this
224 buffer-local variable yourself is in `antlr-mode-hook' or in the \"local
225 variable list\" near the end of the file, see
226 `enable-local-variables'.")
227
228 (defcustom antlr-language-alist
229 '((java-mode "Java" nil "\"Java\"" "Java")
230 (c++-mode "C++" "\"Cpp\"" "Cpp"))
231 "List of ANTLR's supported languages.
232 Each element in this list looks like
233 \(MAJOR-MODE MODELINE-STRING OPTION-VALUE...)
234
235 MAJOR-MODE, the major mode of the code in the grammar's actions, is the
236 value of `antlr-language' if the first group in the string matched by
237 REGEXP in `antlr-language-limit-n-regexp' is one of the OPTION-VALUEs.
238 An OPTION-VALUE of nil denotes the fallback element. MODELINE-STRING is
239 also displayed in the modeline next to \"Antlr\"."
240 :group 'antlr
241 :type '(repeat (group :value (java-mode "")
242 (function :tag "Major mode")
243 (string :tag "Modeline string")
244 (repeat :tag "ANTLR language option" :inline t
245 (choice (const :tag "Default" nil)
246 string )))))
247
248 (defcustom antlr-language-limit-n-regexp
249 '(8192 . "language[ \t]*=[ \t]*\\(\"?[A-Z][A-Za-z_]*\"?\\)")
250 "Used to set a reasonable value for `antlr-language'.
251 Looks like \(LIMIT \. REGEXP). Search for REGEXP from the beginning of
252 the buffer to LIMIT and use the first group in the matched string to set
253 the language according to `antlr-language-alist'."
254 :group 'antlr
255 :type '(cons (choice :tag "Limit" (const :tag "No" nil) (integer :value 0))
256 regexp))
257
258
259 ;;;===========================================================================
260 ;;; Hide/Unhide, Indent/Tabs
261 ;;;===========================================================================
262
263 (defcustom antlr-action-visibility 3
264 "Visibility of actions when command `antlr-hide-actions' is used.
265 If nil, the actions with their surrounding braces are hidden. If a
266 number, do not hide the braces, only hide the contents if its length is
267 greater than this number."
268 :group 'antlr
269 :type '(choice (const :tag "Completely hidden" nil)
270 (integer :tag "Hidden if longer than" :value 3)))
271
272 (defcustom antlr-indent-comment 'tab
273 "*Non-nil, if the indentation should touch lines in block comments.
274 If nil, no continuation line of a block comment is changed. If t, they
275 are changed according to `c-indentation-line'. When not nil and not t,
276 they are only changed by \\[antlr-indent-command]."
277 :group 'antlr
278 :type '(radio (const :tag "No" nil)
279 (const :tag "Always" t)
280 (sexp :tag "With TAB" :format "%t" :value tab)))
281
282 (defcustom antlr-tab-offset-alist
283 '((antlr-mode nil 4 nil)
284 (java-mode "antlr" 4 nil))
285 "Alist to determine whether to use ANTLR's convention for TABs.
286 Each element looks like \(MAJOR-MODE REGEXP TAB-WIDTH INDENT-TABS-MODE).
287 The first element whose MAJOR-MODE is nil or equal to `major-mode' and
288 whose REGEXP is nil or matches variable `buffer-file-name' is used to
289 set `tab-width' and `indent-tabs-mode'. This is useful to support both
290 ANTLR's and Java's indentation styles. Used by `antlr-set-tabs'."
291 :group 'antlr
292 :type '(repeat (group :value (antlr-mode nil 8 nil)
293 (choice (const :tag "All" nil)
294 (function :tag "Major mode"))
295 (choice (const :tag "All" nil) regexp)
296 (integer :tag "Tab width")
297 (boolean :tag "Indent-tabs-mode"))))
298
299 (defcustom antlr-indent-style "java"
300 "*If non-nil, cc-mode indentation style used for `antlr-mode'.
301 See `c-set-style' and for details, where the most interesting part in
302 `c-style-alist' is the value of `c-basic-offset'."
303 :group 'antlr
304 :type '(choice (const nil) regexp))
305
306 (defcustom antlr-indent-item-regexp
307 "[]}):;|&]" ; & is local ANTLR extension (SGML's and-connector)
308 "Regexp matching lines which should be indented by one TAB less.
309 See `antlr-indent-line' and command \\[antlr-indent-command]."
310 :group 'antlr
311 :type 'regexp)
312
313 (defcustom antlr-indent-at-bol-alist
314 ;; eval-when-compile not usable with defcustom...
315 '((java-mode . "\\(package\\|import\\)\\>")
316 (c++-mode . "#\\(assert\\|cpu\\|define\\|endif\\|el\\(if\\|se\\)\\|i\\(dent\\|f\\(def\\|ndef\\)?\\|mport\\|nclude\\(_next\\)?\\)\\|line\\|machine\\|pragma\\|system\\|un\\(assert\\|def\\)\\|warning\\)\\>"))
317 "Alist of regexps matching lines are indented at column 0.
318 Each element in this list looks like (MODE . REGEXP) where MODE is a
319 function and REGEXP is a regular expression.
320
321 If `antlr-language' equals to a MODE, the line starting at the first
322 non-whitespace is matched by the corresponding REGEXP, and the line is
323 part of a header action, indent the line at column 0 instead according
324 to the normal rules of `antlr-indent-line'."
325 :group 'antlr
326 :type '(repeat (cons (function :tag "Major mode") regexp)))
327
328 ;; adopt indentation to cc-engine
329 (defvar antlr-disabling-cc-syntactic-symbols
330 '(statement-block-intro
331 defun-block-intro topmost-intro statement-case-intro member-init-intro
332 arglist-intro brace-list-intro knr-argdecl-intro inher-intro
333 objc-method-intro
334 block-close defun-close class-close brace-list-close arglist-close
335 inline-close extern-lang-close namespace-close))
336
337
338 ;;;===========================================================================
339 ;;; Options: customization
340 ;;;===========================================================================
341
342 (defcustom antlr-options-use-submenus t
343 "*Non-nil, if the major mode menu should include option submenus.
344 If nil, the menu just includes a command to insert options. Otherwise,
345 it includes four submenus to insert file/grammar/rule/subrule options."
346 :group 'antlr
347 :type 'boolean)
348
349 (defcustom antlr-tool-version 20701
350 "*The version number of the Antlr tool.
351 The value is an integer of the form XYYZZ which stands for vX.YY.ZZ.
352 This variable is used to warn about non-supported options and to supply
353 version correct option values when using \\[antlr-insert-option].
354
355 Don't use a number smaller than 20600 since the stored history of
356 Antlr's options starts with v2.06.00, see `antlr-options-alists'. You
357 can make this variable buffer-local."
358 :group 'antlr
359 :type 'integer)
360
361 (defcustom antlr-options-auto-colon t
362 "*Non-nil, if `:' is inserted with a rule or subrule options section.
363 A `:' is only inserted if this value is non-nil, if a rule or subrule
364 option is inserted with \\[antlr-insert-option], if there was no rule or
365 subrule options section before, and if a `:' is not already present
366 after the section, ignoring whitespace, comments and the init action."
367 :group 'antlr
368 :type 'boolean)
369
370 (defcustom antlr-options-style nil
371 "List of symbols which determine the style of option values.
372 If a style symbol is present, the corresponding option value is put into
373 quotes, i.e., represented as a string, otherwise it is represented as an
374 identifier.
375
376 The only style symbol used in the default value of `antlr-options-alist'
377 is `language-as-string'. See also `antlr-read-value'."
378 :group 'antlr
379 :type '(repeat (symbol :tag "Style symbol")))
380
381 (defcustom antlr-options-push-mark t
382 "*Non-nil, if inserting an option should set & push mark.
383 If nil, never set mark when inserting an option with command
384 \\[antlr-insert-option]. If t, always set mark via `push-mark'. If a
385 number, only set mark if point was outside the options area before and
386 the number of lines between point and the insert position is greater
387 than this value. Otherwise, only set mark if point was outside the
388 options area before."
389 :group 'antlr
390 :type '(radio (const :tag "No" nil)
391 (const :tag "Always" t)
392 (integer :tag "Lines between" :value 10)
393 (sexp :tag "If outside options" :format "%t" :value outside)))
394
395 (defcustom antlr-options-assign-string " = "
396 "*String containing `=' to use between option name and value.
397 This string is only used if the option to insert did not exist before
398 or if there was no `=' after it. In other words, the spacing around an
399 existing `=' won't be changed when changing an option value."
400 :group 'antlr
401 :type 'string)
402
403
404 ;;;===========================================================================
405 ;;; Options: definitions
406 ;;;===========================================================================
407
408 (defvar antlr-options-headings '("file" "grammar" "rule" "subrule")
409 "Headings for the four different option kinds.
410 The standard value is (\"file\" \"grammar\" \"rule\" \"subrule\"). See
411 `antlr-options-alists'")
412
413 (defvar antlr-options-alists
414 '(;; file options ----------------------------------------------------------
415 (("language" antlr-language-option-extra
416 (20600 antlr-read-value
417 "Generated language: " language-as-string
418 (("Java") ("Cpp") ("HTML") ("Diagnostic")))
419 (20700 antlr-read-value
420 "Generated language: " language-as-string
421 (("Java") ("Cpp") ("HTML") ("Diagnostic") ("Sather"))))
422 ("mangleLiteralPrefix" nil
423 (20600 antlr-read-value
424 "Prefix for literals (default LITERAL_): " t))
425 ("namespace" antlr-c++-mode-extra
426 (20700 antlr-read-value
427 "Wrap generated C++ code in namespace: " t))
428 ("namespaceStd" antlr-c++-mode-extra
429 (20701 antlr-read-value
430 "Replace ANTLR_USE_NAMESPACE(std) by: " t))
431 ("namespaceAntlr" antlr-c++-mode-extra
432 (20701 antlr-read-value
433 "Replace ANTLR_USE_NAMESPACE(antlr) by: " t))
434 ("genHashLines" antlr-c++-mode-extra
435 (20701 antlr-read-boolean
436 "Include #line in generated C++ code? "))
437 )
438 ;; grammar options --------------------------------------------------------
439 (("k" nil
440 (20600 antlr-read-value
441 "Lookahead depth: "))
442 ("importVocab" nil
443 (20600 antlr-read-value
444 "Import vocabulary: "))
445 ("exportVocab" nil
446 (20600 antlr-read-value
447 "Export vocabulary: "))
448 ("testLiterals" nil ; lexer only
449 (20600 antlr-read-boolean
450 "Test each token against literals table? "))
451 ("defaultErrorHandler" nil ; not for lexer
452 (20600 antlr-read-boolean
453 "Generate default exception handler for each rule? "))
454 ("codeGenMakeSwitchThreshold" nil
455 (20600 antlr-read-value
456 "Min number of alternatives for 'switch': "))
457 ("codeGenBitsetTestThreshold" nil
458 (20600 antlr-read-value
459 "Min size of lookahead set for bitset test: "))
460 ("analyzerDebug" nil
461 (20600 antlr-read-boolean
462 "Display debugging info during grammar analysis? "))
463 ("codeGenDebug" nil
464 (20600 antlr-read-boolean
465 "Display debugging info during code generation? "))
466 ("buildAST" nil ; not for lexer
467 (20600 antlr-read-boolean
468 "Use automatic AST construction/transformation? "))
469 ("ASTLabelType" nil ; not for lexer
470 (20600 antlr-read-value
471 "Class of user-defined AST node: " t))
472 ("charVocabulary" nil ; lexer only
473 (20600 nil
474 "Insert character vocabulary"))
475 ("interactive" nil
476 (20600 antlr-read-boolean
477 "Generate interactive lexer/parser? "))
478 ("caseSensitive" nil ; lexer only
479 (20600 antlr-read-boolean
480 "Case significant when matching characters? "))
481 ("caseSensitiveLiterals" nil ; lexer only
482 (20600 antlr-read-boolean
483 "Case significant when testing literals table? "))
484 ("classHeaderSuffix" nil
485 (20600 nil
486 "Additional string for grammar class definition"))
487 ("filter" nil ; lexer only
488 (20600 antlr-read-boolean
489 "Skip rule (the name, true or false): "
490 antlr-grammar-tokens))
491 ("namespace" antlr-c++-mode-extra
492 (20700 antlr-read-value
493 "Wrap generated C++ code for grammar in namespace: " t))
494 ("namespaceStd" antlr-c++-mode-extra
495 (20701 antlr-read-value
496 "Replace ANTLR_USE_NAMESPACE(std) by: " t))
497 ("namespaceAntlr" antlr-c++-mode-extra
498 (20701 antlr-read-value
499 "Replace ANTLR_USE_NAMESPACE(antlr) by: " t))
500 ("genHashLines" antlr-c++-mode-extra
501 (20701 antlr-read-boolean
502 "Include #line in generated C++ code? "))
503 ;;; ("autoTokenDef" nil ; parser only
504 ;;; (80000 antlr-read-boolean ; default: true
505 ;;; "Automatically define referenced token? "))
506 ;;; ("keywordsMeltTo" nil ; parser only
507 ;;; (80000 antlr-read-value
508 ;;; "Change non-matching keywords to token type: "))
509 )
510 ;; rule options ----------------------------------------------------------
511 (("testLiterals" nil ; lexer only
512 (20600 antlr-read-boolean
513 "Test this token against literals table? "))
514 ("defaultErrorHandler" nil ; not for lexer
515 (20600 antlr-read-boolean
516 "Generate default exception handler for this rule? "))
517 ("ignore" nil ; lexer only
518 (20600 antlr-read-value
519 "In this rule, ignore tokens of type: " nil
520 antlr-grammar-tokens))
521 ("paraphrase" nil ; lexer only
522 (20600 antlr-read-value
523 "In messages, replace name of this token by: " t))
524 )
525 ;; subrule options -------------------------------------------------------
526 (("warnWhenFollowAmbig" nil
527 (20600 antlr-read-boolean
528 "Display warnings for ambiguities with FOLLOW? "))
529 ("generateAmbigWarnings" nil
530 (20600 antlr-read-boolean
531 "Display warnings for ambiguities? "))
532 ("greedy" nil
533 (20700 antlr-read-boolean
534 "Make this optional/loop subrule greedy? "))
535 ))
536 "Definitions for Antlr's options of all four different kinds.
537
538 The value looks like \(FILE GRAMMAR RULE SUBRULE) where each FILE,
539 GRAMMAR, RULE, and SUBRULE is a list of option definitions of the
540 corresponding kind, i.e., looks like \(OPTION-DEF...).
541
542 Each OPTION-DEF looks like \(OPTION-NAME EXTRA-FN VALUE-SPEC...) which
543 defines a file/grammar/rule/subrule option with name OPTION-NAME. The
544 OPTION-NAMEs are used for the creation of the \"Insert XXX Option\"
545 submenus, see `antlr-options-use-submenus', and to allow to insert the
546 option name with completion when using \\[antlr-insert-option].
547
548 If EXTRA-FN is a function, it is called at different phases of the
549 insertion with arguments \(PHASE OPTION-NAME). PHASE can have the
550 values `before-input' or `after-insertion', additional phases might be
551 defined in future versions of this mode. The phase `before-input'
552 occurs before the user is asked to insert a value. The phase
553 `after-insertion' occurs after the option value has been inserted.
554 EXTRA-FN might be called with additional arguments in future versions of
555 this mode.
556
557 Each specification VALUE-SPEC looks like \(VERSION READ-FN ARG...). The
558 last VALUE-SPEC in an OPTION-DEF whose VERSION is smaller or equal to
559 `antlr-tool-version' specifies how the user is asked for the value of
560 the option.
561
562 If READ-FN is nil, the only ARG is a string which is printed at the echo
563 area to guide the user what to insert at point. Otherwise, READ-FN is
564 called with arguments \(INIT-VALUE ARG...) to get the new value of the
565 option. INIT-VALUE is the old value of the option or nil.
566
567 The standard value contains the following functions as READ-FN:
568 `antlr-read-value' with ARGs = \(PROMPT AS-STRING TABLE) which reads a
569 general value, or `antlr-read-boolean' with ARGs = \(PROMPT TABLE) which
570 reads a boolean value or a member of TABLE. PROMPT is the prompt when
571 asking for a new value. If non-nil, TABLE is a table for completion or
572 a function evaluating to such a table. The return value is quoted if
573 AS-STRING is non-nil and is either t or a symbol which is a member of
574 `antlr-options-style'.")
575
576
577 ;;;===========================================================================
578 ;;; Run tool, create Makefile dependencies
579 ;;;===========================================================================
580
581 (defcustom antlr-tool-command "java antlr.Tool"
582 "*Command used in \\[antlr-run-tool] to run the Antlr tool.
583 This variable should include all options passed to Antlr except the
584 option \"-glib\" which is automatically suggested if necessary."
585 :group 'antlr
586 :type 'string)
587
588 (defcustom antlr-ask-about-save t
589 "*If not nil, \\[antlr-run-tool] asks which buffers to save.
590 Otherwise, it saves all modified buffers before running without asking."
591 :group 'antlr
592 :type 'boolean)
593
594 (defcustom antlr-makefile-specification
595 '("\n" ("GENS" "GENS%d" " \\\n\t") "$(ANTLR)")
596 "*Variable to specify the appearance of the generated makefile rules.
597 This variable influences the output of \\[antlr-show-makefile-rules].
598 It looks like \(RULE-SEP GEN-VAR-SPEC COMMAND).
599
600 RULE-SEP is the string to separate different makefile rules. COMMAND is
601 a string with the command which runs the Antlr tool, it should include
602 all options except the option \"-glib\" which is automatically added
603 if necessary.
604
605 If GEN-VAR-SPEC is nil, each target directly consists of a list of
606 files. If GEN-VAR-SPEC looks like \(GEN-VAR GEN-VAR-FORMAT GEN-SEP), a
607 Makefile variable is created for each rule target.
608
609 Then, GEN-VAR is a string with the name of the variable which contains
610 the file names of all makefile rules. GEN-VAR-FORMAT is a format string
611 producing the variable of each target with substitution COUNT/%d where
612 COUNT starts with 1. GEN-SEP is used to separate long variable values."
613 :group 'antlr
614 :type '(list (string :tag "Rule separator")
615 (choice
616 (const :tag "Direct targets" nil)
617 (list :tag "Variables for targets"
618 (string :tag "Variable for all targets")
619 (string :tag "Format for each target variable")
620 (string :tag "Variable separator")))
621 (string :tag "ANTLR command")))
622
623 (defvar antlr-file-formats-alist
624 '((java-mode ("%sTokenTypes.java") ("%s.java"))
625 (c++-mode ("%sTokenTypes.hpp") ("%s.cpp" "%s.hpp")))
626 "Language dependent formats which specify generated files.
627 Each element in this list looks looks like
628 \(MAJOR-MODE (VOCAB-FILE-FORMAT...) (CLASS-FILE-FORMAT...)).
629
630 The element whose MAJOR-MODE is equal to `antlr-language' is used to
631 specify the generated files which are language dependent. See variable
632 `antlr-special-file-formats' for language independent files.
633
634 VOCAB-FILE-FORMAT is a format string, it specifies with substitution
635 VOCAB/%s the generated file for each export vocabulary VOCAB.
636 CLASS-FILE-FORMAT is a format string, it specifies with substitution
637 CLASS/%s the generated file for each grammar class CLASS.")
638
639 (defvar antlr-special-file-formats '("%sTokenTypes.txt" "expanded%s.g")
640 "Language independent formats which specify generated files.
641 The value looks like \(VOCAB-FILE-FORMAT EXPANDED-GRAMMAR-FORMAT).
642
643 VOCAB-FILE-FORMAT is a format string, it specifies with substitution
644 VOCAB/%s the generated or input file for each export or import
645 vocabulary VOCAB, respectively. EXPANDED-GRAMMAR-FORMAT is a format
646 string, it specifies with substitution GRAMMAR/%s the constructed
647 grammar file if the file GRAMMAR.g contains a grammar class which
648 extends a class other than \"Lexer\", \"Parser\" or \"TreeParser\".
649
650 See variable `antlr-file-formats-alist' for language dependent
651 formats.")
652
653 (defvar antlr-unknown-file-formats '("?%s?.g" "?%s?")
654 "*Formats which specify the names of unknown files.
655 The value looks like \(SUPER-GRAMMAR-FILE-FORMAT SUPER-EVOCAB-FORMAT).
656
657 SUPER-GRAMMAR-FORMAT is a format string, it specifies with substitution
658 SUPER/%s the name of a grammar file for Antlr's option \"-glib\" if no
659 grammar file in the current directory defines the class SUPER or if it
660 is defined more than once. SUPER-EVOCAB-FORMAT is a format string, it
661 specifies with substitution SUPER/%s the name for the export vocabulary
662 of above mentioned class SUPER.")
663
664 (defvar antlr-help-unknown-file-text
665 "## The following rules contain filenames of the form
666 ## \"?SUPERCLASS?.g\" (and \"?SUPERCLASS?TokenTypes.txt\")
667 ## where SUPERCLASS is not found to be defined in any grammar file of
668 ## the current directory or is defined more than once. Please replace
669 ## these filenames by the grammar files (and their exportVocab).\n\n"
670 "String indicating the existence of unknown files in the Makefile.
671 See \\[antlr-show-makefile-rules] and `antlr-unknown-file-formats'.")
672
673 (defvar antlr-help-rules-intro
674 "The following Makefile rules define the dependencies for all (non-
675 expanded) grammars in directory \"%s\".\n
676 They are stored in the kill-ring, i.e., you can insert them with C-y
677 into your Makefile. You can also invoke M-x antlr-show-makefile-rules
678 from within a Makefile to insert them directly.\n\n\n"
679 "Introduction to use with \\[antlr-show-makefile-rules].
680 It is a format string and used with substitution DIRECTORY/%s where
681 DIRECTORY is the name of the current directory.")
682
683
684 ;;;===========================================================================
685 ;;; Menu
686 ;;;===========================================================================
687
688 (defcustom antlr-imenu-name t ; (featurep 'xemacs) ; TODO: Emacs-21 bug?
689 "*Non-nil, if a \"Index\" menu should be added to the menubar.
690 If it is a string, it is used instead \"Index\". Requires package
691 imenu."
692 :group 'antlr
693 :type '(choice (const :tag "No menu" nil)
694 (const :tag "Index menu" t)
695 (string :tag "Other menu name")))
696
697 (defvar antlr-mode-map
698 (let ((map (make-sparse-keymap)))
699 (define-key map "\t" 'antlr-indent-command)
700 (define-key map "\e\C-a" 'antlr-beginning-of-rule)
701 (define-key map "\e\C-e" 'antlr-end-of-rule)
702 (define-key map "\C-c\C-a" 'antlr-beginning-of-body)
703 (define-key map "\C-c\C-e" 'antlr-end-of-body)
704 (define-key map "\C-c\C-f" 'c-forward-into-nomenclature)
705 (define-key map "\C-c\C-b" 'c-backward-into-nomenclature)
706 (define-key map "\C-c\C-c" 'comment-region)
707 (define-key map "\C-c\C-v" 'antlr-hide-actions)
708 (define-key map "\C-c\C-r" 'antlr-run-tool)
709 (define-key map "\C-c\C-o" 'antlr-insert-option)
710 ;; I'm too lazy to define my own:
711 (define-key map "\ea" 'c-beginning-of-statement)
712 (define-key map "\ee" 'c-end-of-statement)
713 ;; electric keys:
714 (define-key map ":" 'antlr-electric-character)
715 (define-key map ";" 'antlr-electric-character)
716 (define-key map "|" 'antlr-electric-character)
717 (define-key map "&" 'antlr-electric-character)
718 (define-key map "(" 'antlr-electric-character)
719 (define-key map ")" 'antlr-electric-character)
720 (define-key map "{" 'antlr-electric-character)
721 (define-key map "}" 'antlr-electric-character)
722 map)
723 "Keymap used in `antlr-mode' buffers.")
724
725 (easy-menu-define antlr-mode-menu antlr-mode-map
726 "Major mode menu."
727 `("Antlr"
728 ,@(if (cond-emacs-xemacs
729 :EMACS (and antlr-options-use-submenus
730 (>= emacs-major-version 21))
731 :XEMACS antlr-options-use-submenus)
732 `(("Insert File Option"
733 :filter ,(lambda (x) (antlr-options-menu-filter 1 x)))
734 ("Insert Grammar Option"
735 :filter ,(lambda (x) (antlr-options-menu-filter 2 x)))
736 ("Insert Rule Option"
737 :filter ,(lambda (x) (antlr-options-menu-filter 3 x)))
738 ("Insert Subrule Option"
739 :filter ,(lambda (x) (antlr-options-menu-filter 4 x)))
740 "---")
741 '(["Insert Option" antlr-insert-option
742 :active (not buffer-read-only)]))
743 ("Forward/Backward"
744 ["Backward Rule" antlr-beginning-of-rule t]
745 ["Forward Rule" antlr-end-of-rule t]
746 ["Start of Rule Body" antlr-beginning-of-body
747 :active (antlr-inside-rule-p)]
748 ["End of Rule Body" antlr-end-of-body
749 :active (antlr-inside-rule-p)]
750 "---"
751 ["Backward Statement" c-beginning-of-statement t]
752 ["Forward Statement" c-end-of-statement t]
753 ["Backward Into Nomencl." c-backward-into-nomenclature t]
754 ["Forward Into Nomencl." c-forward-into-nomenclature t])
755 ["Indent Region" indent-region
756 :active (and (not buffer-read-only) (c-region-is-active-p))]
757 ["Comment Out Region" comment-region
758 :active (and (not buffer-read-only) (c-region-is-active-p))]
759 ["Uncomment Region"
760 (comment-region (region-beginning) (region-end) '(4))
761 :active (and (not buffer-read-only) (c-region-is-active-p))]
762 "---"
763 ["Hide Actions (incl. Args)" antlr-hide-actions t]
764 ["Hide Actions (excl. Args)" (antlr-hide-actions 2) t]
765 ["Unhide All Actions" (antlr-hide-actions 0) t]
766 "---"
767 ["Run Tool on Grammar" antlr-run-tool t]
768 ["Show Makefile Rules" antlr-show-makefile-rules t]
769 "---"
770 ["Customize Antlr" (customize-group 'antlr) t]))
771
772
773 ;;;===========================================================================
774 ;;; font-lock
775 ;;;===========================================================================
776
777 (defcustom antlr-font-lock-maximum-decoration 'inherit
778 "*The maximum decoration level for fontifying actions.
779 Value `none' means, do not fontify actions, just normal grammar code
780 according to `antlr-font-lock-additional-keywords'. Value `inherit'
781 means, use value of `font-lock-maximum-decoration'. Any other value is
782 interpreted as in `font-lock-maximum-decoration' with no level-0
783 fontification, see `antlr-font-lock-keywords-alist'.
784
785 While calculating the decoration level for actions, `major-mode' is
786 bound to `antlr-language'. For example, with value
787 \((java-mode \. 2) (c++-mode \. 0))
788 Java actions are fontified with level 2 and C++ actions are not
789 fontified at all."
790 :group 'antlr
791 :type '(choice (const :tag "None" none)
792 (const :tag "Inherit" inherit)
793 (const :tag "Default" nil)
794 (const :tag "Maximum" t)
795 (integer :tag "Level" 1)
796 (repeat :menu-tag "Mode specific" :tag "Mode specific"
797 :value ((t . t))
798 (cons :tag "Instance"
799 (radio :tag "Mode"
800 (const :tag "All" t)
801 (symbol :tag "Name"))
802 (radio :tag "Decoration"
803 (const :tag "Default" nil)
804 (const :tag "Maximum" t)
805 (integer :tag "Level" 1))))))
806
807 (defconst antlr-no-action-keywords nil
808 ;; Using nil directly won't work (would use highest level, see
809 ;; `font-lock-choose-keywords'), but a non-symbol, i.e., (list), at `car'
810 ;; would break Emacs-21.0:
811 "Empty font-lock keywords for actions.
812 Do not change the value of this constant.")
813
814 (defvar antlr-font-lock-keywords-alist
815 '((java-mode
816 antlr-no-action-keywords
817 java-font-lock-keywords-1 java-font-lock-keywords-2
818 java-font-lock-keywords-3)
819 (c++-mode
820 antlr-no-action-keywords
821 c++-font-lock-keywords-1 c++-font-lock-keywords-2
822 c++-font-lock-keywords-3))
823 "List of font-lock keywords for actions in the grammar.
824 Each element in this list looks like
825 \(MAJOR-MODE KEYWORD...)
826
827 If `antlr-language' is equal to MAJOR-MODE, the KEYWORDs are the
828 font-lock keywords according to `font-lock-defaults' used for the code
829 in the grammar's actions and semantic predicates, see
830 `antlr-font-lock-maximum-decoration'.")
831
832 (defvar antlr-default-face 'antlr-default)
833 (defface antlr-default '((t nil))
834 "Face to prevent strings from language dependent highlighting.
835 Do not change."
836 :group 'antlr)
837 ;; backward-compatibility alias
838 (put 'antlr-font-lock-default-face 'face-alias 'antlr-default)
839 (put 'antlr-font-lock-default-face 'obsolete-face "22.1")
840
841 (defvar antlr-keyword-face 'antlr-keyword)
842 (defface antlr-keyword
843 (cond-emacs-xemacs
844 '((((class color) (background light))
845 (:foreground "black" :EMACS :weight bold :XEMACS :bold t))
846 (t :inherit font-lock-keyword-face)))
847 "ANTLR keywords."
848 :group 'antlr)
849 ;; backward-compatibility alias
850 (put 'antlr-font-lock-keyword-face 'face-alias 'antlr-keyword)
851 (put 'antlr-font-lock-keyword-face 'obsolete-face "22.1")
852
853 (defvar antlr-syntax-face 'antlr-keyword)
854 (defface antlr-syntax
855 (cond-emacs-xemacs
856 '((((class color) (background light))
857 (:foreground "black" :EMACS :weight bold :XEMACS :bold t))
858 (t :inherit font-lock-constant-face)))
859 "ANTLR syntax symbols like :, |, (, ), ...."
860 :group 'antlr)
861 ;; backward-compatibility alias
862 (put 'antlr-font-lock-syntax-face 'face-alias 'antlr-syntax)
863 (put 'antlr-font-lock-syntax-face 'obsolete-face "22.1")
864
865 (defvar antlr-ruledef-face 'antlr-ruledef)
866 (defface antlr-ruledef
867 (cond-emacs-xemacs
868 '((((class color) (background light))
869 (:foreground "blue" :EMACS :weight bold :XEMACS :bold t))
870 (t :inherit font-lock-function-name-face)))
871 "ANTLR rule references (definition)."
872 :group 'antlr)
873 ;; backward-compatibility alias
874 (put 'antlr-font-lock-ruledef-face 'face-alias 'antlr-ruledef)
875 (put 'antlr-font-lock-ruledef-face 'obsolete-face "22.1")
876
877 (defvar antlr-tokendef-face 'antlr-tokendef)
878 (defface antlr-tokendef
879 (cond-emacs-xemacs
880 '((((class color) (background light))
881 (:foreground "blue" :EMACS :weight bold :XEMACS :bold t))
882 (t :inherit font-lock-function-name-face)))
883 "ANTLR token references (definition)."
884 :group 'antlr)
885 ;; backward-compatibility alias
886 (put 'antlr-font-lock-tokendef-face 'face-alias 'antlr-tokendef)
887 (put 'antlr-font-lock-tokendef-face 'obsolete-face "22.1")
888
889 (defvar antlr-ruleref-face 'antlr-ruleref)
890 (defface antlr-ruleref
891 '((((class color) (background light)) (:foreground "blue4"))
892 (t :inherit font-lock-type-face))
893 "ANTLR rule references (usage)."
894 :group 'antlr)
895 ;; backward-compatibility alias
896 (put 'antlr-font-lock-ruleref-face 'face-alias 'antlr-ruleref)
897 (put 'antlr-font-lock-ruleref-face 'obsolete-face "22.1")
898
899 (defvar antlr-tokenref-face 'antlr-tokenref)
900 (defface antlr-tokenref
901 '((((class color) (background light)) (:foreground "orange4"))
902 (t :inherit font-lock-type-face))
903 "ANTLR token references (usage)."
904 :group 'antlr)
905 ;; backward-compatibility alias
906 (put 'antlr-font-lock-tokenref-face 'face-alias 'antlr-tokenref)
907 (put 'antlr-font-lock-tokenref-face 'obsolete-face "22.1")
908
909 (defvar antlr-literal-face 'antlr-literal)
910 (defface antlr-literal
911 (cond-emacs-xemacs
912 '((((class color) (background light))
913 (:foreground "brown4" :EMACS :weight bold :XEMACS :bold t))
914 (t :inherit font-lock-string-face)))
915 "ANTLR special literal tokens.
916 It is used to highlight strings matched by the first regexp group of
917 `antlr-font-lock-literal-regexp'."
918 :group 'antlr)
919 ;; backward-compatibility alias
920 (put 'antlr-font-lock-literal-face 'face-alias 'antlr-literal)
921 (put 'antlr-font-lock-literal-face 'obsolete-face "22.1")
922
923 (defcustom antlr-font-lock-literal-regexp "\"\\(\\sw\\(\\sw\\|-\\)*\\)\""
924 "Regexp matching literals with special syntax highlighting, or nil.
925 If nil, there is no special syntax highlighting for some literals.
926 Otherwise, it should be a regular expression which must contain a regexp
927 group. The string matched by the first group is highlighted with
928 `antlr-font-lock-literal-face'."
929 :group 'antlr
930 :type '(choice (const :tag "None" nil) regexp))
931
932 (defvar antlr-class-header-regexp
933 "\\(class\\)[ \t]+\\([A-Za-z\300-\326\330-\337]\\sw*\\)[ \t]+\\(extends\\)[ \t]+\\([A-Za-z\300-\326\330-\337]\\sw*\\)[ \t]*;"
934 "Regexp matching class headers.")
935
936 (defvar antlr-font-lock-additional-keywords
937 (cond-emacs-xemacs
938 `((antlr-invalidate-context-cache)
939 ("\\$setType[ \t]*(\\([A-Za-z\300-\326\330-\337]\\sw*\\))"
940 (1 antlr-tokendef-face))
941 ("\\$\\sw+" (0 antlr-keyword-face))
942 ;; the tokens are already fontified as string/docstrings:
943 (,(lambda (limit)
944 (if antlr-font-lock-literal-regexp
945 (antlr-re-search-forward antlr-font-lock-literal-regexp limit)))
946 (1 antlr-literal-face t)
947 :XEMACS (0 nil)) ; XEmacs bug workaround
948 (,(lambda (limit)
949 (antlr-re-search-forward antlr-class-header-regexp limit))
950 (1 antlr-keyword-face)
951 (2 antlr-ruledef-face)
952 (3 antlr-keyword-face)
953 (4 (if (member (match-string 4) '("Lexer" "Parser" "TreeParser"))
954 antlr-keyword-face
955 font-lock-type-face)))
956 (,(lambda (limit)
957 (antlr-re-search-forward
958 "\\<\\(header\\|options\\|tokens\\|exception\\|catch\\|returns\\)\\>"
959 limit))
960 (1 antlr-keyword-face))
961 (,(lambda (limit)
962 (antlr-re-search-forward
963 "^\\(private\\|public\\|protected\\)\\>[ \t]*\\(\\(\\sw+[ \t]*\\(:\\)?\\)\\)?"
964 limit))
965 (1 font-lock-type-face) ; not XEmacs' java level-3 fruit salad
966 (3 (if (antlr-upcase-p (char-after (match-beginning 3)))
967 antlr-tokendef-face
968 antlr-ruledef-face) nil t)
969 (4 antlr-syntax-face nil t))
970 (,(lambda (limit)
971 (antlr-re-search-forward "^\\(\\sw+\\)[ \t]*\\(:\\)?" limit))
972 (1 (if (antlr-upcase-p (char-after (match-beginning 0)))
973 antlr-tokendef-face
974 antlr-ruledef-face) nil t)
975 (2 antlr-syntax-face nil t))
976 (,(lambda (limit)
977 ;; v:ruleref and v:"literal" is allowed...
978 (antlr-re-search-forward "\\(\\sw+\\)[ \t]*\\([=:]\\)?" limit))
979 (1 (if (match-beginning 2)
980 (if (eq (char-after (match-beginning 2)) ?=)
981 antlr-default-face
982 font-lock-variable-name-face)
983 (if (antlr-upcase-p (char-after (match-beginning 1)))
984 antlr-tokenref-face
985 antlr-ruleref-face)))
986 (2 antlr-default-face nil t))
987 (,(lambda (limit)
988 (antlr-re-search-forward "[|&:;(~]\\|)\\([*+?]\\|=>\\)?" limit))
989 (0 antlr-syntax-face))))
990 "Font-lock keywords for ANTLR's normal grammar code.
991 See `antlr-font-lock-keywords-alist' for the keywords of actions.")
992
993 (defvar antlr-font-lock-defaults
994 '(antlr-font-lock-keywords
995 nil nil ((?_ . "w") (?\( . ".") (?\) . ".")) beginning-of-defun)
996 "Font-lock defaults used for ANTLR syntax highlighting.
997 The SYNTAX-ALIST element is also used to initialize
998 `antlr-action-syntax-table'.")
999
1000
1001 ;;;===========================================================================
1002 ;;; Internal variables
1003 ;;;===========================================================================
1004
1005 (defvar antlr-mode-hook nil
1006 "Hook called by `antlr-mode'.")
1007
1008 (defvar antlr-mode-syntax-table
1009 (let ((st (make-syntax-table)))
1010 (c-populate-syntax-table st)
1011 st)
1012 "Syntax table used in `antlr-mode' buffers.
1013 If non-nil, it will be initialized in `antlr-mode'.")
1014
1015 ;; used for "in Java/C++ code" = syntactic-depth>0
1016 (defvar antlr-action-syntax-table
1017 (let ((st (copy-syntax-table antlr-mode-syntax-table))
1018 (slist (nth 3 antlr-font-lock-defaults)))
1019 (while slist
1020 (modify-syntax-entry (caar slist) (cdar slist) st)
1021 (setq slist (cdr slist)))
1022 st)
1023 "Syntax table used for ANTLR action parsing.
1024 Initialized by `antlr-mode-syntax-table', changed by SYNTAX-ALIST in
1025 `antlr-font-lock-defaults'. This table should be selected if you use
1026 `buffer-syntactic-context' and `buffer-syntactic-context-depth' in order
1027 not to confuse their context_cache.")
1028
1029 (defvar antlr-mode-abbrev-table nil
1030 "Abbreviation table used in `antlr-mode' buffers.")
1031 (define-abbrev-table 'antlr-mode-abbrev-table ())
1032
1033 (defvar antlr-slow-cache-enabling-symbol 'loudly
1034 ;; Emacs' font-lock changes buffer's tick counter, therefore this value should
1035 ;; be a parameter of a font-lock function, but not any other variable of
1036 ;; functions which call `antlr-slow-syntactic-context'.
1037 "If value is a bound symbol, cache will be used even with text changes.
1038 This is no user option. Used for `antlr-slow-syntactic-context'.")
1039
1040 (defvar antlr-slow-cache-diff-threshold 5000
1041 "Maximum distance between `point' and cache position for cache use.
1042 Used for `antlr-slow-syntactic-context'.")
1043
1044
1045 ;;;;##########################################################################
1046 ;;;; The Code
1047 ;;;;##########################################################################
1048
1049
1050
1051 ;;;===========================================================================
1052 ;;; Syntax functions -- Emacs vs XEmacs dependent, part 1
1053 ;;;===========================================================================
1054
1055 ;; From help.el (XEmacs-21.1), without `copy-syntax-table'
1056 (defmacro antlr-with-syntax-table (syntab &rest body)
1057 "Evaluate BODY with the syntax table SYNTAB."
1058 `(let ((stab (syntax-table)))
1059 (unwind-protect
1060 (progn (set-syntax-table ,syntab) ,@body)
1061 (set-syntax-table stab))))
1062 (put 'antlr-with-syntax-table 'lisp-indent-function 1)
1063 (put 'antlr-with-syntax-table 'edebug-form-spec '(form body))
1064
1065 (defunx antlr-default-directory ()
1066 :xemacs-and-try default-directory
1067 "Return `default-directory'."
1068 default-directory)
1069
1070 ;; Check Emacs-21.1 simple.el, `shell-command'.
1071 (defunx antlr-read-shell-command (prompt &optional initial-input history)
1072 :xemacs-and-try read-shell-command
1073 "Read a string from the minibuffer, using `shell-command-history'."
1074 (read-from-minibuffer prompt initial-input nil nil
1075 (or history 'shell-command-history)))
1076
1077 (defunx antlr-with-displaying-help-buffer (thunk &optional name)
1078 :xemacs-and-try with-displaying-help-buffer
1079 "Make a help buffer and call `thunk' there."
1080 (with-output-to-temp-buffer "*Help*"
1081 (save-excursion (funcall thunk))))
1082
1083
1084 ;;;===========================================================================
1085 ;;; Context cache
1086 ;;;===========================================================================
1087
1088 (defvar antlr-slow-context-cache nil "Internal.")
1089
1090 ;;;(defvar antlr-statistics-full-neg 0)
1091 ;;;(defvar antlr-statistics-full-diff 0)
1092 ;;;(defvar antlr-statistics-full-other 0)
1093 ;;;(defvar antlr-statistics-cache 0)
1094 ;;;(defvar antlr-statistics-inval 0)
1095
1096 (defunx antlr-invalidate-context-cache (&rest dummies)
1097 ;; checkdoc-params: (dummies)
1098 "Invalidate context cache for syntactical context information."
1099 :XEMACS ; XEmacs bug workaround
1100 (with-current-buffer (get-buffer-create " ANTLR XEmacs bug workaround")
1101 (buffer-syntactic-context-depth)
1102 nil)
1103 :EMACS
1104 ;;; (incf antlr-statistics-inval)
1105 (setq antlr-slow-context-cache nil))
1106
1107 (defunx antlr-syntactic-context ()
1108 "Return some syntactic context information.
1109 Return `string' if point is within a string, `block-comment' or
1110 `comment' is point is within a comment or the depth within all
1111 parenthesis-syntax delimiters at point otherwise.
1112 WARNING: this may alter `match-data'."
1113 :XEMACS
1114 (or (buffer-syntactic-context) (buffer-syntactic-context-depth))
1115 :EMACS
1116 (let ((orig (point)) diff state
1117 ;; Arg, Emacs' (buffer-modified-tick) changes with font-lock. Use
1118 ;; hack that `loudly' is bound during font-locking => cache use will
1119 ;; increase from 7% to 99.99% during font-locking.
1120 (tick (or (boundp antlr-slow-cache-enabling-symbol)
1121 (buffer-modified-tick))))
1122 (if (and (cdr antlr-slow-context-cache)
1123 (>= (setq diff (- orig (cadr antlr-slow-context-cache))) 0)
1124 (< diff antlr-slow-cache-diff-threshold)
1125 (eq (current-buffer) (caar antlr-slow-context-cache))
1126 (eq tick (cdar antlr-slow-context-cache)))
1127 ;; (setq antlr-statistics-cache (1+ antlr-statistics-cache) ...)
1128 (setq state (parse-partial-sexp (cadr antlr-slow-context-cache) orig
1129 nil nil
1130 (cddr antlr-slow-context-cache)))
1131 (if (>= orig antlr-slow-cache-diff-threshold)
1132 (beginning-of-defun)
1133 (goto-char (point-min)))
1134 ;;; (cond ((and diff (< diff 0)) (incf antlr-statistics-full-neg))
1135 ;;; ((and diff (>= diff 3000)) (incf antlr-statistics-full-diff))
1136 ;;; (t (incf antlr-statistics-full-other)))
1137 (setq state (parse-partial-sexp (point) orig)))
1138 (goto-char orig)
1139 (if antlr-slow-context-cache
1140 (setcdr antlr-slow-context-cache (cons orig state))
1141 (setq antlr-slow-context-cache
1142 (cons (cons (current-buffer) tick)
1143 (cons orig state))))
1144 (cond ((nth 3 state) 'string)
1145 ((nth 4 state) 'comment) ; block-comment? -- we don't care
1146 (t (car state)))))
1147
1148 ;;; (incf (aref antlr-statistics 2))
1149 ;;; (unless (and (eq (current-buffer)
1150 ;;; (caar antlr-slow-context-cache))
1151 ;;; (eq (buffer-modified-tick)
1152 ;;; (cdar antlr-slow-context-cache)))
1153 ;;; (incf (aref antlr-statistics 1))
1154 ;;; (setq antlr-slow-context-cache nil))
1155 ;;; (let* ((orig (point))
1156 ;;; (base (cadr antlr-slow-context-cache))
1157 ;;; (curr (cddr antlr-slow-context-cache))
1158 ;;; (state (cond ((eq orig (car curr)) (cdr curr))
1159 ;;; ((eq orig (car base)) (cdr base))))
1160 ;;; diff diff2)
1161 ;;; (unless state
1162 ;;; (incf (aref antlr-statistics 3))
1163 ;;; (when curr
1164 ;;; (if (< (setq diff (abs (- orig (car curr))))
1165 ;;; (setq diff2 (abs (- orig (car base)))))
1166 ;;; (setq state curr)
1167 ;;; (setq state base
1168 ;;; diff diff2))
1169 ;;; (if (or (>= (1+ diff) (point)) (>= diff 3000))
1170 ;;; (setq state nil))) ; start from bod/bob
1171 ;;; (if state
1172 ;;; (setq state
1173 ;;; (parse-partial-sexp (car state) orig nil nil (cdr state)))
1174 ;;; (if (>= orig 3000) (beginning-of-defun) (goto-char (point-min)))
1175 ;;; (incf (aref antlr-statistics 4))
1176 ;;; (setq cw (list orig (point) base curr))
1177 ;;; (setq state (parse-partial-sexp (point) orig)))
1178 ;;; (goto-char orig)
1179 ;;; (if antlr-slow-context-cache
1180 ;;; (setcdr (cdr antlr-slow-context-cache) (cons orig state))
1181 ;;; (setq antlr-slow-context-cache
1182 ;;; (cons (cons (current-buffer) (buffer-modified-tick))
1183 ;;; (cons (cons orig state) (cons orig state))))))
1184 ;;; (cond ((nth 3 state) 'string)
1185 ;;; ((nth 4 state) 'comment) ; block-comment? -- we don't care
1186 ;;; (t (car state)))))
1187
1188 ;;; (beginning-of-defun)
1189 ;;; (let ((state (parse-partial-sexp (point) orig)))
1190 ;;; (goto-char orig)
1191 ;;; (cond ((nth 3 state) 'string)
1192 ;;; ((nth 4 state) 'comment) ; block-comment? -- we don't care
1193 ;;; (t (car state))))))
1194
1195
1196 ;;;===========================================================================
1197 ;;; Miscellaneous functions
1198 ;;;===========================================================================
1199
1200 (defun antlr-upcase-p (char)
1201 "Non-nil, if CHAR is an uppercase character (if CHAR was a char)."
1202 ;; in XEmacs, upcase only works for ASCII
1203 (or (and (<= ?A char) (<= char ?Z))
1204 (and (<= ?\300 char) (<= char ?\337)))) ; ?\327 is no letter
1205
1206 (defun antlr-re-search-forward (regexp bound)
1207 "Search forward from point for regular expression REGEXP.
1208 Set point to the end of the occurrence found, and return point. Return
1209 nil if no occurrence was found. Do not search within comments, strings
1210 and actions/semantic predicates. BOUND bounds the search; it is a
1211 buffer position. See also the functions `match-beginning', `match-end'
1212 and `replace-match'."
1213 ;; WARNING: Should only be used with `antlr-action-syntax-table'!
1214 (let ((continue t))
1215 (while (and (re-search-forward regexp bound 'limit)
1216 (save-match-data
1217 (if (eq (antlr-syntactic-context) 0)
1218 (setq continue nil)
1219 t))))
1220 (if continue nil (point))))
1221
1222 (defun antlr-search-forward (string)
1223 "Search forward from point for STRING.
1224 Set point to the end of the occurrence found, and return point. Return
1225 nil if no occurrence was found. Do not search within comments, strings
1226 and actions/semantic predicates."
1227 ;; WARNING: Should only be used with `antlr-action-syntax-table'!
1228 (let ((continue t))
1229 (while (and (search-forward string nil 'limit)
1230 (if (eq (antlr-syntactic-context) 0) (setq continue nil) t)))
1231 (if continue nil (point))))
1232
1233 (defun antlr-search-backward (string)
1234 "Search backward from point for STRING.
1235 Set point to the beginning of the occurrence found, and return point.
1236 Return nil if no occurrence was found. Do not search within comments,
1237 strings and actions/semantic predicates."
1238 ;; WARNING: Should only be used with `antlr-action-syntax-table'!
1239 (let ((continue t))
1240 (while (and (search-backward string nil 'limit)
1241 (if (eq (antlr-syntactic-context) 0) (setq continue nil) t)))
1242 (if continue nil (point))))
1243
1244 (defsubst antlr-skip-sexps (count)
1245 "Skip the next COUNT balanced expressions and the comments after it.
1246 Return position before the comments after the last expression."
1247 (goto-char (or (ignore-errors-x (scan-sexps (point) count)) (point-max)))
1248 (prog1 (point)
1249 (antlr-c-forward-sws)))
1250
1251
1252 ;;;===========================================================================
1253 ;;; font-lock
1254 ;;;===========================================================================
1255
1256 (defun antlr-font-lock-keywords ()
1257 "Return font-lock keywords for current buffer.
1258 See `antlr-font-lock-additional-keywords', `antlr-language' and
1259 `antlr-font-lock-maximum-decoration'."
1260 (if (eq antlr-font-lock-maximum-decoration 'none)
1261 antlr-font-lock-additional-keywords
1262 (append antlr-font-lock-additional-keywords
1263 (eval (let ((major-mode antlr-language)) ; dynamic
1264 (font-lock-choose-keywords
1265 (cdr (assq antlr-language
1266 antlr-font-lock-keywords-alist))
1267 (if (eq antlr-font-lock-maximum-decoration 'inherit)
1268 font-lock-maximum-decoration
1269 antlr-font-lock-maximum-decoration)))))))
1270
1271
1272 ;;;===========================================================================
1273 ;;; imenu support
1274 ;;;===========================================================================
1275
1276 (defun antlr-grammar-tokens ()
1277 "Return alist for tokens defined in current buffer."
1278 (save-excursion (antlr-imenu-create-index-function t)))
1279
1280 (defun antlr-imenu-create-index-function (&optional tokenrefs-only)
1281 "Return imenu index-alist for ANTLR grammar files.
1282 IF TOKENREFS-ONLY is non-nil, just return alist with tokenref names."
1283 (let ((items nil)
1284 (classes nil)
1285 (continue t))
1286 ;; Using `imenu-progress-message' would require imenu for compilation, but
1287 ;; nobody is missing these messages. The generic imenu function searches
1288 ;; backward, which is slower and more likely not to work during editing.
1289 (antlr-with-syntax-table antlr-action-syntax-table
1290 (antlr-invalidate-context-cache)
1291 (goto-char (point-min))
1292 (antlr-skip-file-prelude t)
1293 (while continue
1294 (if (looking-at "{") (antlr-skip-sexps 1))
1295 (if (looking-at antlr-class-header-regexp)
1296 (or tokenrefs-only
1297 (push (cons (match-string 2)
1298 (if imenu-use-markers
1299 (copy-marker (match-beginning 2))
1300 (match-beginning 2)))
1301 classes))
1302 (if (looking-at "p\\(ublic\\|rotected\\|rivate\\)")
1303 (antlr-skip-sexps 1))
1304 (when (looking-at "\\sw+")
1305 (if tokenrefs-only
1306 (if (antlr-upcase-p (char-after (point)))
1307 (push (list (match-string 0)) items))
1308 (push (cons (match-string 0)
1309 (if imenu-use-markers
1310 (copy-marker (match-beginning 0))
1311 (match-beginning 0)))
1312 items))))
1313 (if (setq continue (antlr-search-forward ";"))
1314 (antlr-skip-exception-part t))))
1315 (if classes
1316 (cons (cons "Classes" (nreverse classes)) (nreverse items))
1317 (nreverse items))))
1318
1319
1320 ;;;===========================================================================
1321 ;;; Parse grammar files (internal functions)
1322 ;;;===========================================================================
1323
1324 (defun antlr-skip-exception-part (skip-comment)
1325 "Skip exception part of current rule, i.e., everything after `;'.
1326 This also includes the options and tokens part of a grammar class
1327 header. If SKIP-COMMENT is non-nil, also skip the comment after that
1328 part."
1329 (let ((pos (point))
1330 (class nil))
1331 (antlr-c-forward-sws)
1332 (while (looking-at "options\\>\\|tokens\\>")
1333 (setq class t)
1334 (setq pos (antlr-skip-sexps 2)))
1335 (if class
1336 ;; Problem: an action only belongs to a class def, not a normal rule.
1337 ;; But checking the current rule type is too expensive => only expect
1338 ;; an action if we have found an option or tokens part.
1339 (if (looking-at "{") (setq pos (antlr-skip-sexps 1)))
1340 (while (looking-at "exception\\>")
1341 (setq pos (antlr-skip-sexps 1))
1342 (when (looking-at "\\[")
1343 (setq pos (antlr-skip-sexps 1)))
1344 (while (looking-at "catch\\>")
1345 (setq pos (antlr-skip-sexps 3)))))
1346 (or skip-comment (goto-char pos))))
1347
1348 (defun antlr-skip-file-prelude (skip-comment)
1349 "Skip the file prelude: the header and file options.
1350 If SKIP-COMMENT is non-nil, also skip the comment after that part.
1351 Return the start position of the file prelude.
1352
1353 Hack: if SKIP-COMMENT is `header-only' only skip header and return
1354 position before the comment after the header."
1355 (let* ((pos (point))
1356 (pos0 pos))
1357 (antlr-c-forward-sws)
1358 (if skip-comment (setq pos0 (point)))
1359 (while (looking-at "header\\>[ \t]*\\(\"\\)?")
1360 (setq pos (antlr-skip-sexps (if (match-beginning 1) 3 2))))
1361 (if (eq skip-comment 'header-only) ; a hack...
1362 pos
1363 (when (looking-at "options\\>")
1364 (setq pos (antlr-skip-sexps 2)))
1365 (or skip-comment (goto-char pos))
1366 pos0)))
1367
1368 (defun antlr-next-rule (arg skip-comment)
1369 "Move forward to next end of rule. Do it ARG many times.
1370 A grammar class header and the file prelude are also considered as a
1371 rule. Negative argument ARG means move back to ARGth preceding end of
1372 rule. The behavior is not defined when ARG is zero. If SKIP-COMMENT
1373 is non-nil, move to beginning of the rule."
1374 ;; WARNING: Should only be used with `antlr-action-syntax-table'!
1375 ;; PRE: ARG<>0
1376 (let ((pos (point))
1377 (beg (point)))
1378 ;; first look whether point is in exception part
1379 (if (antlr-search-backward ";")
1380 (progn
1381 (setq beg (point))
1382 (forward-char)
1383 (antlr-skip-exception-part skip-comment))
1384 (antlr-skip-file-prelude skip-comment))
1385 (if (< arg 0)
1386 (unless (and (< (point) pos) (zerop (incf arg)))
1387 ;; if we have moved backward, we already moved one defun backward
1388 (goto-char beg) ; rewind (to ";" / point)
1389 (while (and arg (<= (incf arg) 0))
1390 (if (antlr-search-backward ";")
1391 (setq beg (point))
1392 (when (>= arg -1)
1393 ;; try file prelude:
1394 (setq pos (antlr-skip-file-prelude skip-comment))
1395 (if (zerop arg)
1396 (if (>= (point) beg)
1397 (goto-char (if (>= pos beg) (point-min) pos)))
1398 (goto-char (if (or (>= (point) beg) (= (point) pos))
1399 (point-min) pos))))
1400 (setq arg nil)))
1401 (when arg ; always found a ";"
1402 (forward-char)
1403 (antlr-skip-exception-part skip-comment)))
1404 (if (<= (point) pos) ; moved backward?
1405 (goto-char pos) ; rewind
1406 (decf arg)) ; already moved one defun forward
1407 (unless (zerop arg)
1408 (while (>= (decf arg) 0)
1409 (antlr-search-forward ";"))
1410 (antlr-skip-exception-part skip-comment)))))
1411
1412 (defun antlr-outside-rule-p ()
1413 "Non-nil if point is outside a grammar rule.
1414 Move to the beginning of the current rule if point is inside a rule."
1415 ;; WARNING: Should only be used with `antlr-action-syntax-table'!
1416 (let ((pos (point)))
1417 (antlr-next-rule -1 nil)
1418 (let ((between (or (bobp) (< (point) pos))))
1419 (antlr-c-forward-sws)
1420 (and between (> (point) pos) (goto-char pos)))))
1421
1422
1423 ;;;===========================================================================
1424 ;;; Parse grammar files (commands)
1425 ;;;===========================================================================
1426 ;; No (interactive "_") in Emacs... use `zmacs-region-stays'.
1427
1428 (defun antlr-inside-rule-p ()
1429 "Non-nil if point is inside a grammar rule.
1430 A grammar class header and the file prelude are also considered as a
1431 rule."
1432 (save-excursion
1433 (antlr-with-syntax-table antlr-action-syntax-table
1434 (not (antlr-outside-rule-p)))))
1435
1436 (defunx antlr-end-of-rule (&optional arg)
1437 "Move forward to next end of rule. Do it ARG [default: 1] many times.
1438 A grammar class header and the file prelude are also considered as a
1439 rule. Negative argument ARG means move back to ARGth preceding end of
1440 rule. If ARG is zero, run `antlr-end-of-body'."
1441 (interactive "_p")
1442 (if (zerop arg)
1443 (antlr-end-of-body)
1444 (antlr-with-syntax-table antlr-action-syntax-table
1445 (antlr-next-rule arg nil))))
1446
1447 (defunx antlr-beginning-of-rule (&optional arg)
1448 "Move backward to preceding beginning of rule. Do it ARG many times.
1449 A grammar class header and the file prelude are also considered as a
1450 rule. Negative argument ARG means move forward to ARGth next beginning
1451 of rule. If ARG is zero, run `antlr-beginning-of-body'."
1452 (interactive "_p")
1453 (if (zerop arg)
1454 (antlr-beginning-of-body)
1455 (antlr-with-syntax-table antlr-action-syntax-table
1456 (antlr-next-rule (- arg) t))))
1457
1458 (defunx antlr-end-of-body (&optional msg)
1459 "Move to position after the `;' of the current rule.
1460 A grammar class header is also considered as a rule. With optional
1461 prefix arg MSG, move to `:'."
1462 (interactive "_")
1463 (antlr-with-syntax-table antlr-action-syntax-table
1464 (let ((orig (point)))
1465 (if (antlr-outside-rule-p)
1466 (error "Outside an ANTLR rule"))
1467 (let ((bor (point)))
1468 (when (< (antlr-skip-file-prelude t) (point))
1469 ;; Yes, we are in the file prelude
1470 (goto-char orig)
1471 (error (or msg "The file prelude is without `;'")))
1472 (antlr-search-forward ";")
1473 (when msg
1474 (when (< (point)
1475 (progn (goto-char bor)
1476 (or (antlr-search-forward ":") (point-max))))
1477 (goto-char orig)
1478 (error msg))
1479 (antlr-c-forward-sws))))))
1480
1481 (defunx antlr-beginning-of-body ()
1482 "Move to the first element after the `:' of the current rule."
1483 (interactive "_")
1484 (antlr-end-of-body "Class headers and the file prelude are without `:'"))
1485
1486
1487 ;;;===========================================================================
1488 ;;; Literal normalization, Hide Actions
1489 ;;;===========================================================================
1490
1491 (defun antlr-downcase-literals (&optional transform)
1492 "Convert all literals in buffer to lower case.
1493 If non-nil, TRANSFORM is used on literals instead of `downcase-region'."
1494 (interactive)
1495 (or transform (setq transform 'downcase-region))
1496 (let ((literals 0))
1497 (save-excursion
1498 (goto-char (point-min))
1499 (antlr-with-syntax-table antlr-action-syntax-table
1500 (antlr-invalidate-context-cache)
1501 (while (antlr-re-search-forward "\"\\(\\sw\\(\\sw\\|-\\)*\\)\"" nil)
1502 (funcall transform (match-beginning 0) (match-end 0))
1503 (incf literals))))
1504 (message "Transformed %d literals" literals)))
1505
1506 (defun antlr-upcase-literals ()
1507 "Convert all literals in buffer to upper case."
1508 (interactive)
1509 (antlr-downcase-literals 'upcase-region))
1510
1511 (defun antlr-hide-actions (arg &optional silent)
1512 "Hide or unhide all actions in buffer.
1513 Hide all actions including arguments in brackets if ARG is 1 or if
1514 called interactively without prefix argument. Hide all actions
1515 excluding arguments in brackets if ARG is 2 or higher. Unhide all
1516 actions if ARG is 0 or negative. See `antlr-action-visibility'.
1517
1518 Display a message unless optional argument SILENT is non-nil."
1519 (interactive "p")
1520 (save-buffer-state-x
1521 (if (> arg 0)
1522 (let ((regexp (if (= arg 1) "[]}]" "}"))
1523 (diff (and antlr-action-visibility
1524 (+ (max antlr-action-visibility 0) 2))))
1525 (antlr-hide-actions 0 t)
1526 (save-excursion
1527 (goto-char (point-min))
1528 (antlr-with-syntax-table antlr-action-syntax-table
1529 (antlr-invalidate-context-cache)
1530 (while (antlr-re-search-forward regexp nil)
1531 (let ((beg (ignore-errors-x (scan-sexps (point) -1))))
1532 (when beg
1533 (if diff ; braces are visible
1534 (if (> (point) (+ beg diff))
1535 (add-text-properties (1+ beg) (1- (point))
1536 '(invisible t intangible t)))
1537 ;; if actions is on line(s) of its own, hide WS
1538 (and (looking-at "[ \t]*$")
1539 (save-excursion
1540 (goto-char beg)
1541 (skip-chars-backward " \t")
1542 (and (bolp) (setq beg (point))))
1543 (beginning-of-line 2)) ; beginning of next line
1544 (add-text-properties beg (point)
1545 '(invisible t intangible t))))))))
1546 (or silent
1547 (message "Hide all actions (%s arguments)...done"
1548 (if (= arg 1) "including" "excluding"))))
1549 (remove-text-properties (point-min) (point-max)
1550 '(invisible nil intangible nil))
1551 (or silent
1552 (message "Unhide all actions (including arguments)...done")))))
1553
1554
1555 ;;;===========================================================================
1556 ;;; Insert option: command
1557 ;;;===========================================================================
1558
1559 (defun antlr-insert-option (level option &optional location)
1560 "Insert file/grammar/rule/subrule option near point.
1561 LEVEL determines option kind to insert: 1=file, 2=grammar, 3=rule,
1562 4=subrule. OPTION is a string with the name of the option to insert.
1563 LOCATION can be specified for not calling `antlr-option-kind' twice.
1564
1565 Inserting an option with this command works as follows:
1566
1567 1. When called interactively, LEVEL is determined by the prefix
1568 argument or automatically deduced without prefix argument.
1569 2. Signal an error if no option of that level could be inserted, e.g.,
1570 if the buffer is read-only, the option area is outside the visible
1571 part of the buffer or a subrule/rule option should be inserted with
1572 point outside a subrule/rule.
1573 3. When called interactively, OPTION is read from the minibuffer with
1574 completion over the known options of the given LEVEL.
1575 4. Ask user for confirmation if the given OPTION does not seem to be a
1576 valid option to insert into the current file.
1577 5. Find a correct position to insert the option.
1578 6. Depending on the option, insert it the following way \(inserting an
1579 option also means inserting the option section if necessary\):
1580 - Insert the option and let user insert the value at point.
1581 - Read a value (with completion) from the minibuffer, using a
1582 previous value as initial contents, and insert option with value.
1583 7. Final action depending on the option. For example, set the language
1584 according to a newly inserted language option.
1585
1586 The name of all options with a specification for their values are stored
1587 in `antlr-options-alists'. The used specification also depends on the
1588 value of `antlr-tool-version', i.e., step 4 will warn you if you use an
1589 option that has been introduced in newer version of ANTLR, and step 5
1590 will offer completion using version-correct values.
1591
1592 If the option already exists inside the visible part of the buffer, this
1593 command can be used to change the value of that option. Otherwise, find
1594 a correct position where the option can be inserted near point.
1595
1596 The search for a correct position is as follows:
1597
1598 * If search is within an area where options can be inserted, use the
1599 position of point. Inside the options section and if point is in
1600 the middle of a option definition, skip the rest of it.
1601 * If an options section already exists, insert the options at the end.
1602 If only the beginning of the area is visible, insert at the
1603 beginning.
1604 * Otherwise, find the position where an options section can be
1605 inserted and insert a new section before any comments. If the
1606 position before the comments is not visible, insert the new section
1607 after the comments.
1608
1609 This function also inserts \"options {...}\" and the \":\" if necessary,
1610 see `antlr-options-auto-colon'. See also `antlr-options-assign-string'.
1611
1612 This command might also set the mark like \\[set-mark-command] does, see
1613 `antlr-options-push-mark'."
1614 (interactive (antlr-insert-option-interactive current-prefix-arg))
1615 (barf-if-buffer-read-only)
1616 (or location (setq location (cdr (antlr-option-kind level))))
1617 (cond ((null level)
1618 (error "Cannot deduce what kind of option to insert"))
1619 ((atom location)
1620 (error "Cannot insert any %s options around here"
1621 (elt antlr-options-headings (1- level)))))
1622 (let ((area (car location))
1623 (place (cdr location)))
1624 (cond ((null place) ; invisible
1625 (error (if area
1626 "Invisible %s options, use %s to make them visible"
1627 "Invisible area for %s options, use %s to make it visible")
1628 (elt antlr-options-headings (1- level))
1629 (substitute-command-keys "\\[widen]")))
1630 ((null area) ; without option part
1631 (antlr-insert-option-do level option nil
1632 (null (cdr place))
1633 (car place)))
1634 ((save-excursion ; with option part, option visible
1635 (goto-char (max (point-min) (car area)))
1636 (re-search-forward (concat "\\(^\\|;\\)[ \t]*\\(\\<"
1637 (regexp-quote option)
1638 "\\>\\)[ \t\n]*\\(\\(=[ \t]?\\)[ \t]*\\(\\(\\sw\\|\\s_\\)+\\|\"\\([^\n\"\\]\\|[\\][^\n]\\)*\"\\)?\\)?")
1639 ;; 2=name, 3=4+5, 4="=", 5=value
1640 (min (point-max) (cdr area))
1641 t))
1642 (antlr-insert-option-do level option
1643 (cons (or (match-beginning 5)
1644 (match-beginning 3))
1645 (match-end 5))
1646 (and (null (cdr place)) area)
1647 (or (match-beginning 5)
1648 (match-end 4)
1649 (match-end 2))))
1650 (t ; with option part, option not yet
1651 (antlr-insert-option-do level option t
1652 (and (null (cdr place)) area)
1653 (car place))))))
1654
1655 (defun antlr-insert-option-interactive (arg)
1656 "Interactive specification for `antlr-insert-option'.
1657 Return \(LEVEL OPTION LOCATION)."
1658 (barf-if-buffer-read-only)
1659 (if arg (setq arg (prefix-numeric-value arg)))
1660 (unless (memq arg '(nil 1 2 3 4))
1661 (error "Valid prefix args: no=auto, 1=file, 2=grammar, 3=rule, 4=subrule"))
1662 (let* ((kind (antlr-option-kind arg))
1663 (level (car kind)))
1664 (if (atom (cdr kind))
1665 (list level nil (cdr kind))
1666 (let* ((table (elt antlr-options-alists (1- level)))
1667 (completion-ignore-case t) ;dynamic
1668 (input (completing-read (format "Insert %s option: "
1669 (elt antlr-options-headings
1670 (1- level)))
1671 table)))
1672 (list level input (cdr kind))))))
1673
1674 (defun antlr-options-menu-filter (level menu-items)
1675 "Return items for options submenu of level LEVEL."
1676 ;; checkdoc-params: (menu-items)
1677 (let ((active (if buffer-read-only
1678 nil
1679 (consp (cdr-safe (cdr (antlr-option-kind level)))))))
1680 (mapcar (lambda (option)
1681 (vector option
1682 (list 'antlr-insert-option level option)
1683 :active active))
1684 (sort (mapcar 'car (elt antlr-options-alists (1- level)))
1685 'string-lessp))))
1686
1687
1688 ;;;===========================================================================
1689 ;;; Insert option: determine section-kind
1690 ;;;===========================================================================
1691
1692 (defun antlr-option-kind (requested)
1693 "Return level and location for option to insert near point.
1694 Call function `antlr-option-level' with argument REQUESTED. If the
1695 result is nil, return \(REQUESTED \. error). If the result has the
1696 non-nil value LEVEL, return \(LEVEL \. LOCATION) where LOCATION looks
1697 like \(AREA \. PLACE), see `antlr-option-location'."
1698 (save-excursion
1699 (save-restriction
1700 (let ((min0 (point-min)) ; before `widen'!
1701 (max0 (point-max))
1702 (orig (point))
1703 (level (antlr-option-level requested)) ; calls `widen'!
1704 pos)
1705 (cond ((null level)
1706 (setq level requested))
1707 ((eq level 1) ; file options
1708 (goto-char (point-min))
1709 (setq pos (antlr-skip-file-prelude 'header-only)))
1710 ((not (eq level 3)) ; grammar or subrule options
1711 (setq pos (point))
1712 (antlr-c-forward-sws))
1713 ((looking-at "^\\(private[ \t\n]\\|public[ \t\n]\\|protected[ \t\n]\\)?[ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)[ \t\n]*\\(!\\)?[ \t\n]*\\(\\[\\)?")
1714 ;; rule options, with complete rule header
1715 (goto-char (or (match-end 4) (match-end 3)))
1716 (setq pos (antlr-skip-sexps (if (match-end 5) 1 0)))
1717 (when (looking-at "returns[ \t\n]*\\[")
1718 (goto-char (1- (match-end 0)))
1719 (setq pos (antlr-skip-sexps 1)))))
1720 (cons level
1721 (cond ((null pos) 'error)
1722 ((looking-at "options[ \t\n]*{")
1723 (goto-char (match-end 0))
1724 (setq pos (ignore-errors-x (scan-lists (point) 1 1)))
1725 (antlr-option-location orig min0 max0
1726 (point)
1727 (if pos (1- pos) (point-max))
1728 t))
1729 (t
1730 (antlr-option-location orig min0 max0
1731 pos (point)
1732 nil))))))))
1733
1734 (defun antlr-option-level (requested)
1735 "Return level for option to insert near point.
1736 Remove any restrictions from current buffer and return level for the
1737 option to insert near point, i.e., 1, 2, 3, 4, or nil if no such option
1738 can be inserted. If REQUESTED is non-nil, it is the only possible value
1739 to return except nil. If REQUESTED is nil, return level for the nearest
1740 option kind, i.e., the highest number possible.
1741
1742 If the result is 2, point is at the beginning of the class after the
1743 class definition. If the result is 3 or 4, point is at the beginning of
1744 the rule/subrule after the init action. Otherwise, the point position
1745 is undefined."
1746 (widen)
1747 (if (eq requested 1)
1748 1
1749 (antlr-with-syntax-table antlr-action-syntax-table
1750 (antlr-invalidate-context-cache)
1751 (let* ((orig (point))
1752 (outsidep (antlr-outside-rule-p))
1753 bor depth)
1754 (if (eq (char-after) ?\{) (antlr-skip-sexps 1))
1755 (setq bor (point)) ; beginning of rule (after init action)
1756 (cond ((eq requested 2) ; grammar options required?
1757 (let (boc) ; beginning of class
1758 (goto-char (point-min))
1759 (while (and (<= (point) bor)
1760 (antlr-re-search-forward antlr-class-header-regexp
1761 nil))
1762 (if (<= (match-beginning 0) bor)
1763 (setq boc (match-end 0))))
1764 (when boc
1765 (goto-char boc)
1766 2)))
1767 ((save-excursion ; in region of file options?
1768 (goto-char (point-min))
1769 (antlr-skip-file-prelude t) ; ws/comment after: OK
1770 (< orig (point)))
1771 (and (null requested) 1))
1772 (outsidep ; outside rule not OK
1773 nil)
1774 ((looking-at antlr-class-header-regexp) ; rule = class def?
1775 (goto-char (match-end 0))
1776 (and (null requested) 2))
1777 ((eq requested 3) ; rule options required?
1778 (goto-char bor)
1779 3)
1780 ((setq depth (antlr-syntactic-grammar-depth orig bor))
1781 (if (> depth 0) ; move out of actions
1782 (goto-char (scan-lists (point) -1 depth)))
1783 (set-syntax-table antlr-mode-syntax-table)
1784 (antlr-invalidate-context-cache)
1785 (if (eq (antlr-syntactic-context) 0) ; not in subrule?
1786 (unless (eq requested 4)
1787 (goto-char bor)
1788 3)
1789 (goto-char (1+ (scan-lists (point) -1 1)))
1790 4)))))))
1791
1792 (defun antlr-option-location (orig min-vis max-vis min-area max-area withp)
1793 "Return location for the options area.
1794 ORIG is the original position of `point', MIN-VIS is `point-min' and
1795 MAX-VIS is `point-max'. If WITHP is non-nil, there exists an option
1796 specification and it starts after the brace at MIN-AREA and stops at
1797 MAX-AREA. If WITHP is nil, there is no area and the region where it
1798 could be inserted starts at MIN-AREA and stops at MAX-AREA.
1799
1800 The result has the form (AREA . PLACE). AREA is (MIN-AREA . MAX-AREA)
1801 if WITHP is non-nil, and nil otherwise. PLACE is nil if the area is
1802 invisible, (ORIG) if ORIG is inside the area, (MIN-AREA . beginning) for
1803 a visible start position and (MAX-AREA . end) for a visible end position
1804 where the beginning is preferred if WITHP is nil and the end if WITHP is
1805 non-nil."
1806 (cons (and withp (cons min-area max-area))
1807 (cond ((and (<= min-area orig) (<= orig max-area)
1808 (save-excursion
1809 (goto-char orig)
1810 (not (memq (antlr-syntactic-context)
1811 '(comment block-comment)))))
1812 ;; point in options area and not in comment
1813 (list orig))
1814 ((and (null withp) (<= min-vis min-area) (<= min-area max-vis))
1815 ;; use start of options area (only if not `withp')
1816 (cons min-area 'beginning))
1817 ((and (<= min-vis max-area) (<= max-area max-vis))
1818 ;; use end of options area
1819 (cons max-area 'end))
1820 ((and withp (<= min-vis min-area) (<= min-area max-vis))
1821 ;; use start of options area (only if `withp')
1822 (cons min-area 'beginning)))))
1823
1824 (defun antlr-syntactic-grammar-depth (pos beg)
1825 "Return syntactic context depth at POS.
1826 Move to POS and from there on to the beginning of the string or comment
1827 if POS is inside such a construct. Then, return the syntactic context
1828 depth at point if the point position is smaller than BEG.
1829 WARNING: this may alter `match-data'."
1830 (goto-char pos)
1831 (let ((context (or (antlr-syntactic-context) 0)))
1832 (while (and context (not (integerp context)))
1833 (cond ((eq context 'string)
1834 (setq context
1835 (and (search-backward "\"" nil t)
1836 (>= (point) beg)
1837 (or (antlr-syntactic-context) 0))))
1838 ((memq context '(comment block-comment))
1839 (setq context
1840 (and (re-search-backward "/[/*]" nil t)
1841 (>= (point) beg)
1842 (or (antlr-syntactic-context) 0))))))
1843 context))
1844
1845
1846 ;;;===========================================================================
1847 ;;; Insert options: do the insertion
1848 ;;;===========================================================================
1849
1850 (defun antlr-insert-option-do (level option old area pos)
1851 "Insert option into buffer at position POS.
1852 Insert option of level LEVEL and name OPTION. If OLD is non-nil, an
1853 options area is already exists. If OLD looks like \(BEG \. END), the
1854 option already exists. Then, BEG is the start position of the option
1855 value, the position of the `=' or nil, and END is the end position of
1856 the option value or nil.
1857
1858 If the original point position was outside an options area, AREA is nil.
1859 Otherwise, and if an option specification already exists, AREA is a cons
1860 cell where the two values determine the area inside the braces."
1861 (let* ((spec (cdr (assoc option (elt antlr-options-alists (1- level)))))
1862 (value (antlr-option-spec level option (cdr spec) (consp old))))
1863 (if (fboundp (car spec)) (funcall (car spec) 'before-input option))
1864 ;; set mark (unless point was inside options area before)
1865 (if (cond (area (eq antlr-options-push-mark t))
1866 ((numberp antlr-options-push-mark)
1867 (> (count-lines (min (point) pos) (max (point) pos))
1868 antlr-options-push-mark))
1869 (antlr-options-push-mark))
1870 (push-mark))
1871 ;; read option value -----------------------------------------------------
1872 (goto-char pos)
1873 (if (null value)
1874 ;; no option specification found
1875 (if (y-or-n-p (format "Insert unknown %s option %s? "
1876 (elt antlr-options-headings (1- level))
1877 option))
1878 (message "Insert value for %s option %s"
1879 (elt antlr-options-headings (1- level))
1880 option)
1881 (error "Didn't insert unknown %s option %s"
1882 (elt antlr-options-headings (1- level))
1883 option))
1884 ;; option specification found
1885 (setq value (cdr value))
1886 (if (car value)
1887 (let ((initial (and (consp old) (cdr old)
1888 (buffer-substring (car old) (cdr old)))))
1889 (setq value (apply (car value)
1890 (and initial
1891 (if (eq (aref initial 0) ?\")
1892 (read initial)
1893 initial))
1894 (cdr value))))
1895 (message "%s" (or (cadr value) ""))
1896 (setq value nil)))
1897 ;; insert value ----------------------------------------------------------
1898 (if (consp old)
1899 (antlr-insert-option-existing old value)
1900 (if (consp area)
1901 ;; Move outside string/comment if point is inside option spec
1902 (antlr-syntactic-grammar-depth (point) (car area)))
1903 (antlr-insert-option-space area old)
1904 (or old (antlr-insert-option-area level))
1905 (insert option " = ;")
1906 (backward-char)
1907 (if value (insert value)))
1908 ;; final -----------------------------------------------------------------
1909 (if (fboundp (car spec)) (funcall (car spec) 'after-insertion option))))
1910
1911 (defun antlr-option-spec (level option specs existsp)
1912 "Return version correct option value specification.
1913 Return specification for option OPTION of kind level LEVEL. SPECS
1914 should correspond to the VALUE-SPEC... in `antlr-option-alists'.
1915 EXISTSP determines whether the option already exists."
1916 (let (value)
1917 (while (and specs (>= antlr-tool-version (caar specs)))
1918 (setq value (pop specs)))
1919 (cond (value) ; found correct spec
1920 ((null specs) nil) ; didn't find any specs
1921 (existsp (car specs)) ; wrong version, but already present
1922 ((y-or-n-p (format "Insert v%s %s option %s in v%s? "
1923 (antlr-version-string (caar specs))
1924 (elt antlr-options-headings (1- level))
1925 option
1926 (antlr-version-string antlr-tool-version)))
1927 (car specs))
1928 (t
1929 (error "Didn't insert v%s %s option %s in v%s"
1930 (antlr-version-string (caar specs))
1931 (elt antlr-options-headings (1- level))
1932 option
1933 (antlr-version-string antlr-tool-version))))))
1934
1935 (defun antlr-version-string (version)
1936 "Format the Antlr version number VERSION, see `antlr-tool-version'."
1937 (let ((version100 (/ version 100)))
1938 (format "%d.%d.%d"
1939 (/ version100 100) (mod version100 100) (mod version 100))))
1940
1941
1942 ;;;===========================================================================
1943 ;;; Insert options: the details (used by `antlr-insert-option-do')
1944 ;;;===========================================================================
1945
1946 (defun antlr-insert-option-existing (old value)
1947 "Insert option value VALUE at point for existing option.
1948 For OLD, see `antlr-insert-option-do'."
1949 ;; no = => insert =
1950 (unless (car old) (insert antlr-options-assign-string))
1951 ;; with user input => insert if necessary
1952 (when value
1953 (if (cdr old) ; with value
1954 (if (string-equal value (buffer-substring (car old) (cdr old)))
1955 (goto-char (cdr old))
1956 (delete-region (car old) (cdr old))
1957 (insert value))
1958 (insert value)))
1959 (unless (looking-at "\\([^\n=;{}/'\"]\\|'\\([^\n'\\]\\|\\\\.\\)*'\\|\"\\([^\n\"\\]\\|\\\\.\\)*\"\\)*;")
1960 ;; stuff (no =, {, } or /) at point is not followed by ";"
1961 (insert ";")
1962 (backward-char)))
1963
1964 (defun antlr-insert-option-space (area old)
1965 "Find appropriate place to insert option, insert newlines/spaces.
1966 For AREA and OLD, see `antlr-insert-option-do'."
1967 (let ((orig (point))
1968 (open t))
1969 (skip-chars-backward " \t")
1970 (unless (bolp)
1971 (let ((before (char-after (1- (point)))))
1972 (goto-char orig)
1973 (and old ; with existing options area
1974 (consp area) ; if point inside existing area
1975 (not (eq before ?\;)) ; if not at beginning of option
1976 ; => skip to end of option
1977 (if (and (search-forward ";" (cdr area) t)
1978 (let ((context (antlr-syntactic-context)))
1979 (or (null context) (numberp context))))
1980 (setq orig (point))
1981 (goto-char orig)))
1982 (skip-chars-forward " \t")
1983
1984 (if (looking-at "$\\|//")
1985 ;; just comment after point => skip (+ lines w/ same col comment)
1986 (let ((same (if (> (match-end 0) (match-beginning 0))
1987 (current-column))))
1988 (beginning-of-line 2)
1989 (or (bolp) (insert "\n"))
1990 (when (and same (null area)) ; or (consp area)?
1991 (while (and (looking-at "[ \t]*\\(//\\)")
1992 (goto-char (match-beginning 1))
1993 (= (current-column) same))
1994 (beginning-of-line 2)
1995 (or (bolp) (insert "\n")))))
1996 (goto-char orig)
1997 (if (null old)
1998 (progn (insert "\n") (antlr-indent-line))
1999 (unless (eq (char-after (1- (point))) ?\ )
2000 (insert " "))
2001 (unless (eq (char-after (point)) ?\ )
2002 (insert " ")
2003 (backward-char))
2004 (setq open nil)))))
2005 (when open
2006 (beginning-of-line 1)
2007 (insert "\n")
2008 (backward-char)
2009 (antlr-indent-line))))
2010
2011 (defun antlr-insert-option-area (level)
2012 "Insert new options area for options of level LEVEL.
2013 Used by `antlr-insert-option-do'."
2014 (insert "options {\n\n}")
2015 (when (and antlr-options-auto-colon
2016 (memq level '(3 4))
2017 (save-excursion
2018 (antlr-c-forward-sws)
2019 (if (eq (char-after (point)) ?\{) (antlr-skip-sexps 1))
2020 (not (eq (char-after (point)) ?\:))))
2021 (insert "\n:")
2022 (antlr-indent-line)
2023 (end-of-line 0))
2024 (backward-char 1)
2025 (antlr-indent-line)
2026 (beginning-of-line 0)
2027 (antlr-indent-line))
2028
2029
2030 ;;;===========================================================================
2031 ;;; Insert options: in `antlr-options-alists'
2032 ;;;===========================================================================
2033
2034 (defun antlr-read-value (initial-contents prompt
2035 &optional as-string table table-x)
2036 "Read a string from the minibuffer, possibly with completion.
2037 If INITIAL-CONTENTS is non-nil, insert it in the minibuffer initially.
2038 PROMPT is a string to prompt with, normally it ends in a colon and a
2039 space. If AS-STRING is t or is a member \(comparison done with `eq') of
2040 `antlr-options-style', return printed representation of the user input,
2041 otherwise return the user input directly.
2042
2043 If TABLE or TABLE-X is non-nil, read with completion. The completion
2044 table is the resulting alist of TABLE-X concatenated with TABLE where
2045 TABLE can also be a function evaluation to an alist.
2046
2047 Used inside `antlr-options-alists'."
2048 (let* ((completion-ignore-case t) ; dynamic
2049 (table0 (and (or table table-x)
2050 (append table-x
2051 (if (functionp table) (funcall table) table))))
2052 (input (if table0
2053 (completing-read prompt table0 nil nil initial-contents)
2054 (read-from-minibuffer prompt initial-contents))))
2055 (if (and as-string
2056 (or (eq as-string t)
2057 (cdr (assq as-string antlr-options-style))))
2058 (format "%S" input)
2059 input)))
2060
2061 (defun antlr-read-boolean (initial-contents prompt &optional table)
2062 "Read a boolean value from the minibuffer, with completion.
2063 If INITIAL-CONTENTS is non-nil, insert it in the minibuffer initially.
2064 PROMPT is a string to prompt with, normally it ends in a question mark
2065 and a space. \"(true or false) \" is appended if TABLE is nil.
2066
2067 Read with completion over \"true\", \"false\" and the keys in TABLE, see
2068 also `antlr-read-value'.
2069
2070 Used inside `antlr-options-alists'."
2071 (antlr-read-value initial-contents
2072 (if table prompt (concat prompt "(true or false) "))
2073 nil
2074 table '(("false") ("true"))))
2075
2076 (defun antlr-language-option-extra (phase &rest dummies)
2077 ;; checkdoc-params: (dummies)
2078 "Change language according to the new value of the \"language\" option.
2079 Call `antlr-mode' if the new language would be different from the value
2080 of `antlr-language', keeping the value of variable `font-lock-mode'.
2081
2082 Called in PHASE `after-insertion', see `antlr-options-alists'."
2083 (when (eq phase 'after-insertion)
2084 (let ((new-language (antlr-language-option t)))
2085 (or (null new-language)
2086 (eq new-language antlr-language)
2087 (let ((font-lock (and (boundp 'font-lock-mode) font-lock-mode)))
2088 (if font-lock (font-lock-mode 0))
2089 (antlr-mode)
2090 (and font-lock (null font-lock-mode) (font-lock-mode 1)))))))
2091
2092 (defun antlr-c++-mode-extra (phase option &rest dummies)
2093 ;; checkdoc-params: (option dummies)
2094 "Warn if C++ option is used with the wrong language.
2095 Ask user \(\"y or n\"), if a C++ only option is going to be inserted but
2096 `antlr-language' has not the value `c++-mode'.
2097
2098 Called in PHASE `before-input', see `antlr-options-alists'."
2099 (and (eq phase 'before-input)
2100 (not (eq antlr-language 'c++-mode))
2101 (not (y-or-n-p (format "Insert C++ %s option? " option)))
2102 (error "Didn't insert C++ %s option with language %s"
2103 option (cadr (assq antlr-language antlr-language-alist)))))
2104
2105
2106 ;;;===========================================================================
2107 ;;; Compute dependencies
2108 ;;;===========================================================================
2109
2110 (defun antlr-file-dependencies ()
2111 "Return dependencies for grammar in current buffer.
2112 The result looks like \(FILE \(CLASSES \. SUPERS) VOCABS \. LANGUAGE)
2113 where CLASSES = ((CLASS . CLASS-EVOCAB) ...),
2114 SUPERS = ((SUPER . USE-EVOCAB-P) ...), and
2115 VOCABS = ((EVOCAB ...) . (IVOCAB ...))
2116
2117 FILE is the current buffer's file-name without directory part and
2118 LANGUAGE is the value of `antlr-language' in the current buffer. Each
2119 EVOCAB is an export vocabulary and each IVOCAB is an import vocabulary.
2120
2121 Each CLASS is a grammar class with its export vocabulary CLASS-EVOCAB.
2122 Each SUPER is a super-grammar class where USE-EVOCAB-P indicates whether
2123 its export vocabulary is used as an import vocabulary."
2124 (unless buffer-file-name
2125 (error "Grammar buffer does not visit a file"))
2126 (let (classes export-vocabs import-vocabs superclasses default-vocab)
2127 (antlr-with-syntax-table antlr-action-syntax-table
2128 (goto-char (point-min))
2129 (while (antlr-re-search-forward antlr-class-header-regexp nil)
2130 ;; parse class definition --------------------------------------------
2131 (let* ((class (match-string 2))
2132 (sclass (match-string 4))
2133 ;; export vocab defaults to class name (first grammar in file)
2134 ;; or to the export vocab of the first grammar in file:
2135 (evocab (or default-vocab class))
2136 (ivocab nil))
2137 (goto-char (match-end 0))
2138 (antlr-c-forward-sws)
2139 (while (looking-at "options\\>\\|\\(tokens\\)\\>")
2140 (if (match-beginning 1)
2141 (antlr-skip-sexps 2)
2142 (goto-char (match-end 0))
2143 (antlr-c-forward-sws)
2144 ;; parse grammar option sections -------------------------------
2145 (when (eq (char-after (point)) ?\{)
2146 (let* ((beg (1+ (point)))
2147 (end (1- (antlr-skip-sexps 1)))
2148 (cont (point)))
2149 (goto-char beg)
2150 (if (re-search-forward "\\<exportVocab[ \t]*=[ \t]*\\([A-Za-z\300-\326\330-\337]\\sw*\\)" end t)
2151 (setq evocab (match-string 1)))
2152 (goto-char beg)
2153 (if (re-search-forward "\\<importVocab[ \t]*=[ \t]*\\([A-Za-z\300-\326\330-\337]\\sw*\\)" end t)
2154 (setq ivocab (match-string 1)))
2155 (goto-char cont)))))
2156 (unless (member sclass '("Parser" "Lexer" "TreeParser"))
2157 (let ((super (assoc sclass superclasses)))
2158 (if super
2159 (or ivocab (setcdr super t))
2160 (push (cons sclass (null ivocab)) superclasses))))
2161 ;; remember class with export vocabulary:
2162 (push (cons class evocab) classes)
2163 ;; default export vocab is export vocab of first grammar in file:
2164 (or default-vocab (setq default-vocab evocab))
2165 (or (member evocab export-vocabs) (push evocab export-vocabs))
2166 (or (null ivocab)
2167 (member ivocab import-vocabs) (push ivocab import-vocabs)))))
2168 (if classes
2169 (list* (file-name-nondirectory buffer-file-name)
2170 (cons (nreverse classes) (nreverse superclasses))
2171 (cons (nreverse export-vocabs) (nreverse import-vocabs))
2172 antlr-language))))
2173
2174 (defun antlr-directory-dependencies (dirname)
2175 "Return dependencies for all grammar files in directory DIRNAME.
2176 The result looks like \((CLASS-SPEC ...) \. \(FILE-DEP ...))
2177 where CLASS-SPEC = (CLASS (FILE \. EVOCAB) ...).
2178
2179 FILE-DEP are the dependencies for each grammar file in DIRNAME, see
2180 `antlr-file-dependencies'. For each grammar class CLASS, FILE is a
2181 grammar file in which CLASS is defined and EVOCAB is the name of the
2182 export vocabulary specified in that file."
2183 (let ((grammar (directory-files dirname t "\\.g\\'")))
2184 (when grammar
2185 (let ((antlr-imenu-name nil) ; dynamic-let: no imenu
2186 (expanded-regexp
2187 (concat (format (regexp-quote
2188 (cadr antlr-special-file-formats))
2189 ".+")
2190 "\\'"))
2191 classes dependencies)
2192 (with-temp-buffer
2193 (dolist (file grammar)
2194 (when (and (file-regular-p file)
2195 (null (string-match expanded-regexp file)))
2196 (insert-file-contents file t nil nil t)
2197 (normal-mode t) ; necessary for major-mode, syntax
2198 ; table and `antlr-language'
2199 (when (derived-mode-p 'antlr-mode)
2200 (let* ((file-deps (antlr-file-dependencies))
2201 (file (car file-deps)))
2202 (when file-deps
2203 (dolist (class-def (caadr file-deps))
2204 (let ((file-evocab (cons file (cdr class-def)))
2205 (class-spec (assoc (car class-def) classes)))
2206 (if class-spec
2207 (nconc (cdr class-spec) (list file-evocab))
2208 (push (list (car class-def) file-evocab)
2209 classes))))
2210 (push file-deps dependencies)))))))
2211 (cons (nreverse classes) (nreverse dependencies))))))
2212
2213
2214 ;;;===========================================================================
2215 ;;; Compilation: run ANTLR tool
2216 ;;;===========================================================================
2217
2218 (defun antlr-superclasses-glibs (supers classes)
2219 "Compute the grammar lib option for the super grammars SUPERS.
2220 Look in CLASSES for the right grammar lib files for SUPERS. SUPERS is
2221 part SUPER in the result of `antlr-file-dependencies'. CLASSES is the
2222 part \(CLASS-SPEC ...) in the result of `antlr-directory-dependencies'.
2223
2224 The result looks like \(OPTION WITH-UNKNOWN GLIB ...). OPTION is the
2225 complete \"-glib\" option. WITH-UNKNOWN is t if there is none or more
2226 than one grammar file for at least one super grammar.
2227
2228 Each GLIB looks like \(GRAMMAR-FILE \. EVOCAB). GRAMMAR-FILE is a file
2229 in which a super-grammar is defined. EVOCAB is the value of the export
2230 vocabulary of the super-grammar or nil if it is not needed."
2231 ;; If the superclass is defined in the same file, that file will be included
2232 ;; with -glib again. This will lead to a redefinition. But defining a
2233 ;; analyzer of the same class twice in a file will lead to an error anyway...
2234 (let (glibs unknown)
2235 (while supers
2236 (let* ((super (pop supers))
2237 (sup-files (cdr (assoc (car super) classes)))
2238 (file (and sup-files (null (cdr sup-files)) (car sup-files))))
2239 (or file (setq unknown t)) ; not exactly one file
2240 (push (cons (or (car file)
2241 (format (car antlr-unknown-file-formats)
2242 (car super)))
2243 (and (cdr super)
2244 (or (cdr file)
2245 (format (cadr antlr-unknown-file-formats)
2246 (car super)))))
2247 glibs)))
2248 (cons (if glibs (concat " -glib " (mapconcat 'car glibs ";")) "")
2249 (cons unknown glibs))))
2250
2251 (defun antlr-run-tool (command file &optional saved)
2252 "Run Antlr took COMMAND on grammar FILE.
2253 When called interactively, COMMAND is read from the minibuffer and
2254 defaults to `antlr-tool-command' with a computed \"-glib\" option if
2255 necessary.
2256
2257 Save all buffers first unless optional value SAVED is non-nil. When
2258 called interactively, the buffers are always saved, see also variable
2259 `antlr-ask-about-save'."
2260 (interactive (antlr-run-tool-interactive))
2261 (or saved (save-some-buffers (not antlr-ask-about-save)))
2262 (let ((default-directory (file-name-directory file)))
2263 (compilation-start (concat command " " (file-name-nondirectory file))
2264 nil #'(lambda (mode-name) "*Antlr-Run*"))))
2265
2266 (defun antlr-run-tool-interactive ()
2267 ;; code in `interactive' is not compiled
2268 "Interactive specification for `antlr-run-tool'.
2269 Use prefix argument ARG to return \(COMMAND FILE SAVED)."
2270 (let* ((supers (cdadr (save-excursion
2271 (save-restriction
2272 (widen)
2273 (antlr-file-dependencies)))))
2274 (glibs ""))
2275 (when supers
2276 (save-some-buffers (not antlr-ask-about-save) nil)
2277 (setq glibs (car (antlr-superclasses-glibs
2278 supers
2279 (car (antlr-directory-dependencies
2280 (antlr-default-directory)))))))
2281 (list (antlr-read-shell-command "Run Antlr on current file with: "
2282 (concat antlr-tool-command glibs " "))
2283 buffer-file-name
2284 supers)))
2285
2286
2287 ;;;===========================================================================
2288 ;;; Makefile creation
2289 ;;;===========================================================================
2290
2291 (defun antlr-makefile-insert-variable (number pre post)
2292 "Insert Makefile variable numbered NUMBER according to specification.
2293 Also insert strings PRE and POST before and after the variable."
2294 (let ((spec (cadr antlr-makefile-specification)))
2295 (when spec
2296 (insert pre
2297 (if number (format (cadr spec) number) (car spec))
2298 post))))
2299
2300 (defun antlr-insert-makefile-rules (&optional in-makefile)
2301 "Insert Makefile rules in the current buffer at point.
2302 IN-MAKEFILE is non-nil, if the current buffer is the Makefile. See
2303 command `antlr-show-makefile-rules' for detail."
2304 (let* ((dirname (antlr-default-directory))
2305 (deps0 (antlr-directory-dependencies dirname))
2306 (classes (car deps0)) ; CLASS -> (FILE . EVOCAB) ...
2307 (deps (cdr deps0)) ; FILE -> (c . s) (ev . iv) . LANGUAGE
2308 (with-error nil)
2309 (gen-sep (or (caddr (cadr antlr-makefile-specification)) " "))
2310 (n (and (cdr deps) (cadr antlr-makefile-specification) 0)))
2311 (or in-makefile (set-buffer standard-output))
2312 (dolist (dep deps)
2313 (let ((supers (cdadr dep))
2314 (lang (cdr (assoc (cdddr dep) antlr-file-formats-alist))))
2315 (if n (incf n))
2316 (antlr-makefile-insert-variable n "" " =")
2317 (if supers
2318 (insert " "
2319 (format (cadr antlr-special-file-formats)
2320 (file-name-sans-extension (car dep)))))
2321 (dolist (class-def (caadr dep))
2322 (let ((sep gen-sep))
2323 (dolist (class-file (cadr lang))
2324 (insert sep (format class-file (car class-def)))
2325 (setq sep " "))))
2326 (dolist (evocab (caaddr dep))
2327 (let ((sep gen-sep))
2328 (dolist (vocab-file (cons (car antlr-special-file-formats)
2329 (car lang)))
2330 (insert sep (format vocab-file evocab))
2331 (setq sep " "))))
2332 (antlr-makefile-insert-variable n "\n$(" ")")
2333 (insert ": " (car dep))
2334 (dolist (ivocab (cdaddr dep))
2335 (insert " " (format (car antlr-special-file-formats) ivocab)))
2336 (let ((glibs (antlr-superclasses-glibs supers classes)))
2337 (if (cadr glibs) (setq with-error t))
2338 (dolist (super (cddr glibs))
2339 (insert " " (car super))
2340 (if (cdr super)
2341 (insert " " (format (car antlr-special-file-formats)
2342 (cdr super)))))
2343 (insert "\n\t"
2344 (caddr antlr-makefile-specification)
2345 (car glibs)
2346 " $<\n"
2347 (car antlr-makefile-specification)))))
2348 (if n
2349 (let ((i 0))
2350 (antlr-makefile-insert-variable nil "" " =")
2351 (while (<= (incf i) n)
2352 (antlr-makefile-insert-variable i " $(" ")"))
2353 (insert "\n" (car antlr-makefile-specification))))
2354 (if (string-equal (car antlr-makefile-specification) "\n")
2355 (backward-delete-char 1))
2356 (when with-error
2357 (goto-char (point-min))
2358 (insert antlr-help-unknown-file-text))
2359 (unless in-makefile
2360 (copy-region-as-kill (point-min) (point-max))
2361 (goto-char (point-min))
2362 (insert (format antlr-help-rules-intro dirname)))))
2363
2364 ;;;###autoload
2365 (defun antlr-show-makefile-rules ()
2366 "Show Makefile rules for all grammar files in the current directory.
2367 If the `major-mode' of the current buffer has the value `makefile-mode',
2368 the rules are directory inserted at point. Otherwise, a *Help* buffer
2369 is shown with the rules which are also put into the `kill-ring' for
2370 \\[yank].
2371
2372 This command considers import/export vocabularies and grammar
2373 inheritance and provides a value for the \"-glib\" option if necessary.
2374 Customize variable `antlr-makefile-specification' for the appearance of
2375 the rules.
2376
2377 If the file for a super-grammar cannot be determined, special file names
2378 are used according to variable `antlr-unknown-file-formats' and a
2379 commentary with value `antlr-help-unknown-file-text' is added. The
2380 *Help* buffer always starts with the text in `antlr-help-rules-intro'."
2381 (interactive)
2382 (if (null (derived-mode-p 'makefile-mode))
2383 (antlr-with-displaying-help-buffer 'antlr-insert-makefile-rules)
2384 (push-mark)
2385 (antlr-insert-makefile-rules t)))
2386
2387
2388 ;;;===========================================================================
2389 ;;; Indentation
2390 ;;;===========================================================================
2391
2392 (defun antlr-indent-line ()
2393 "Indent the current line as ANTLR grammar code.
2394 The indentation of grammar lines are calculated by `c-basic-offset',
2395 multiplied by:
2396 - the level of the paren/brace/bracket depth,
2397 - plus 0/2/1, depending on the position inside the rule: header, body,
2398 exception part,
2399 - minus 1 if `antlr-indent-item-regexp' matches the beginning of the
2400 line starting from the first non-whitespace.
2401
2402 Lines inside block comments are indented by `c-indent-line' according to
2403 `antlr-indent-comment'.
2404
2405 Lines in actions except top-level actions in a header part or an option
2406 area are indented by `c-indent-line'.
2407
2408 Lines in header actions are indented at column 0 if `antlr-language'
2409 equals to a key in `antlr-indent-at-bol-alist' and the line starting at
2410 the first non-whitespace is matched by the corresponding value.
2411
2412 For the initialization of `c-basic-offset', see `antlr-indent-style' and,
2413 to a lesser extent, `antlr-tab-offset-alist'."
2414 (save-restriction
2415 (let ((orig (point))
2416 (min0 (point-min))
2417 bol boi indent syntax cc-syntax)
2418 (widen)
2419 (beginning-of-line)
2420 (setq bol (point))
2421 (if (< bol min0)
2422 (error "Beginning of current line not visible"))
2423 (skip-chars-forward " \t")
2424 (setq boi (point))
2425 ;; check syntax at beginning of indentation ----------------------------
2426 (antlr-with-syntax-table antlr-action-syntax-table
2427 (antlr-invalidate-context-cache)
2428 (setq syntax (antlr-syntactic-context))
2429 (cond ((symbolp syntax)
2430 (setq indent nil)) ; block-comments, strings, (comments)
2431 ((progn
2432 (antlr-next-rule -1 t)
2433 (if (antlr-search-forward ":") (< boi (1- (point))) t))
2434 (setq indent 0)) ; in rule header
2435 ((if (antlr-search-forward ";") (< boi (point)) t)
2436 (setq indent 2)) ; in rule body
2437 (t
2438 (forward-char)
2439 (antlr-skip-exception-part nil)
2440 (setq indent (if (> (point) boi) 1 0))))) ; in exception part?
2441 ;; check whether to use indentation engine of cc-mode ------------------
2442 (antlr-invalidate-context-cache)
2443 (goto-char boi)
2444 (when (and indent (> syntax 0))
2445 (cond ((> syntax 1) ; block in action => use cc-mode
2446 (setq indent nil))
2447 ((and (= indent 0)
2448 (assq antlr-language antlr-indent-at-bol-alist)
2449 (looking-at (cdr (assq antlr-language
2450 antlr-indent-at-bol-alist))))
2451 (setq syntax 'bol))
2452 ((setq cc-syntax (c-guess-basic-syntax))
2453 (let ((cc cc-syntax) symbol)
2454 (while (setq symbol (pop cc))
2455 (when (cdr symbol)
2456 (or (memq (car symbol)
2457 antlr-disabling-cc-syntactic-symbols)
2458 (setq indent nil))
2459 (setq cc nil)))))))
2460 ;;; ((= indent 1) ; exception part => use cc-mode
2461 ;;; (setq indent nil))
2462 ;;; ((save-restriction ; not in option part => cc-mode
2463 ;;; (goto-char (scan-lists (point) -1 1))
2464 ;;; (skip-chars-backward " \t\n")
2465 ;;; (narrow-to-region (point-min) (point))
2466 ;;; (not (re-search-backward "\\<options\\'" nil t)))
2467 ;;; (setq indent nil)))))
2468 ;; compute the corresponding indentation and indent --------------------
2469 (if (null indent)
2470 ;; Use the indentation engine of cc-mode
2471 (progn
2472 (goto-char orig)
2473 (if (or (numberp syntax)
2474 (if (eq syntax 'string) nil (eq antlr-indent-comment t)))
2475 (c-indent-line cc-syntax)))
2476 ;; do it ourselves
2477 (goto-char boi)
2478 (unless (symbolp syntax) ; direct indentation
2479 ;;(antlr-invalidate-context-cache)
2480 (incf indent (antlr-syntactic-context))
2481 (and (> indent 0) (looking-at antlr-indent-item-regexp) (decf indent))
2482 (setq indent (* indent c-basic-offset)))
2483 ;; the usual major-mode indent stuff ---------------------------------
2484 (setq orig (- (point-max) orig))
2485 (unless (= (current-column) indent)
2486 (delete-region bol boi)
2487 (beginning-of-line)
2488 (indent-to indent))
2489 ;; If initial point was within line's indentation,
2490 ;; position after the indentation. Else stay at same point in text.
2491 (if (> (- (point-max) orig) (point))
2492 (goto-char (- (point-max) orig)))))))
2493
2494 (defun antlr-indent-command (&optional arg)
2495 "Indent the current line or insert tabs/spaces.
2496 With optional prefix argument ARG or if the previous command was this
2497 command, insert ARG tabs or spaces according to `indent-tabs-mode'.
2498 Otherwise, indent the current line with `antlr-indent-line'."
2499 (interactive "*P")
2500 (if (or arg (eq last-command 'antlr-indent-command))
2501 (insert-tab arg)
2502 (let ((antlr-indent-comment (and antlr-indent-comment t))) ; dynamic
2503 (antlr-indent-line))))
2504
2505 (defun antlr-electric-character (&optional arg)
2506 "Insert the character you type and indent the current line.
2507 Insert the character like `self-insert-command' and indent the current
2508 line as `antlr-indent-command' does. Do not indent the line if
2509
2510 * this command is called with a prefix argument ARG,
2511 * there are characters except whitespaces between point and the
2512 beginning of the line, or
2513 * point is not inside a normal grammar code, { and } are also OK in
2514 actions.
2515
2516 This command is useful for a character which has some special meaning in
2517 ANTLR's syntax and influences the auto indentation, see
2518 `antlr-indent-item-regexp'."
2519 (interactive "*P")
2520 (if (or arg
2521 (save-excursion (skip-chars-backward " \t") (not (bolp)))
2522 (antlr-with-syntax-table antlr-action-syntax-table
2523 (antlr-invalidate-context-cache)
2524 (let ((context (antlr-syntactic-context)))
2525 (not (and (numberp context)
2526 (or (zerop context)
2527 (memq last-command-event '(?\{ ?\}))))))))
2528 (self-insert-command (prefix-numeric-value arg))
2529 (self-insert-command (prefix-numeric-value arg))
2530 (antlr-indent-line)))
2531
2532
2533 ;;;===========================================================================
2534 ;;; Mode entry
2535 ;;;===========================================================================
2536
2537 (defun antlr-c-init-language-vars ()
2538 "Like `c-init-language-vars-for' when using cc-mode before v5.29."
2539 (let ((settings ; (cdr '(setq...)) will be optimized
2540 (if (eq antlr-language 'c++-mode)
2541 (cdr '(setq ;' from `c++-mode' v5.20, v5.28
2542 c-keywords (c-identifier-re c-C++-keywords)
2543 c-conditional-key c-C++-conditional-key
2544 c-comment-start-regexp c-C++-comment-start-regexp
2545 c-class-key c-C++-class-key
2546 c-extra-toplevel-key c-C++-extra-toplevel-key
2547 c-access-key c-C++-access-key
2548 c-recognize-knr-p nil
2549 c-bitfield-key c-C-bitfield-key ; v5.28
2550 ))
2551 (cdr '(setq ; from `java-mode' v5.20, v5.28
2552 c-keywords (c-identifier-re c-Java-keywords)
2553 c-conditional-key c-Java-conditional-key
2554 c-comment-start-regexp c-Java-comment-start-regexp
2555 c-class-key c-Java-class-key
2556 c-method-key nil
2557 c-baseclass-key nil
2558 c-recognize-knr-p nil
2559 c-access-key c-Java-access-key ; v5.20
2560 c-inexpr-class-key c-Java-inexpr-class-key ; v5.28
2561 )))))
2562 (while settings
2563 (when (boundp (car settings))
2564 (ignore-errors
2565 (set (car settings) (eval (cadr settings)))))
2566 (setq settings (cddr settings)))))
2567
2568 (defun antlr-language-option (search)
2569 "Find language in `antlr-language-alist' for language option.
2570 If SEARCH is non-nil, find element for language option. Otherwise, find
2571 the default language."
2572 (let ((value
2573 (and search
2574 (save-excursion
2575 (goto-char (point-min))
2576 (re-search-forward (cdr antlr-language-limit-n-regexp)
2577 (+ (point)
2578 (car antlr-language-limit-n-regexp))
2579 t))
2580 (match-string 1)))
2581 (seq antlr-language-alist)
2582 r)
2583 ;; Like (find VALUE antlr-language-alist :key 'cddr :test 'member)
2584 (while seq
2585 (setq r (pop seq))
2586 (if (member value (cddr r))
2587 (setq seq nil) ; stop
2588 (setq r nil))) ; no result yet
2589 (car r)))
2590
2591 ;;;###autoload
2592 (define-derived-mode antlr-mode prog-mode
2593 ;; FIXME: Since it uses cc-mode, it bumps into c-update-modeline's
2594 ;; limitation to mode-name being a string.
2595 ;; '("Antlr." (:eval (cadr (assq antlr-language antlr-language-alist))))
2596 "Antlr"
2597 "Major mode for editing ANTLR grammar files."
2598 :abbrev-table antlr-mode-abbrev-table
2599 (c-initialize-cc-mode) ; cc-mode is required
2600 (unless (fboundp 'c-forward-sws) ; see above
2601 (fset 'antlr-c-forward-sws 'c-forward-syntactic-ws))
2602 ;; ANTLR specific ----------------------------------------------------------
2603 (unless antlr-language
2604 (set (make-local-variable 'antlr-language)
2605 (or (antlr-language-option t) (antlr-language-option nil))))
2606 (if (stringp (cadr (assq antlr-language antlr-language-alist)))
2607 (setq mode-name
2608 (concat "Antlr."
2609 (cadr (assq antlr-language antlr-language-alist)))))
2610 ;; indentation, for the C engine -------------------------------------------
2611 (setq c-buffer-is-cc-mode antlr-language)
2612 (cond ((fboundp 'c-init-language-vars-for) ; cc-mode 5.30.5+
2613 (c-init-language-vars-for antlr-language))
2614 ((fboundp 'c-init-c-language-vars) ; cc-mode 5.30 to 5.30.4
2615 (c-init-c-language-vars) ; not perfect, but OK
2616 (setq c-recognize-knr-p nil))
2617 ((fboundp 'c-init-language-vars) ; cc-mode 5.29
2618 (let ((init-fn 'c-init-language-vars))
2619 (funcall init-fn))) ; is a function in v5.29
2620 (t ; cc-mode upto 5.28
2621 (antlr-c-init-language-vars))) ; do it myself
2622 (c-basic-common-init antlr-language (or antlr-indent-style "gnu"))
2623 (set (make-local-variable 'outline-regexp) "[^#\n\^M]")
2624 (set (make-local-variable 'outline-level) 'c-outline-level) ;TODO: define own
2625 (set (make-local-variable 'indent-line-function) 'antlr-indent-line)
2626 (set (make-local-variable 'indent-region-function) nil) ; too lazy
2627 (setq comment-start "// "
2628 comment-end ""
2629 comment-start-skip "/\\*+ *\\|// *")
2630 ;; various -----------------------------------------------------------------
2631 (set (make-local-variable 'font-lock-defaults) antlr-font-lock-defaults)
2632 (easy-menu-add antlr-mode-menu)
2633 (set (make-local-variable 'imenu-create-index-function)
2634 'antlr-imenu-create-index-function)
2635 (set (make-local-variable 'imenu-generic-expression) t) ; fool stupid test
2636 (and antlr-imenu-name ; there should be a global variable...
2637 (fboundp 'imenu-add-to-menubar)
2638 (imenu-add-to-menubar
2639 (if (stringp antlr-imenu-name) antlr-imenu-name "Index")))
2640 (antlr-set-tabs))
2641
2642 ;; A smarter version of `group-buffers-menu-by-mode-then-alphabetically' (in
2643 ;; XEmacs) could use the following property. The header of the submenu would
2644 ;; be "Antlr" instead of "Antlr.C++" or (not and!) "Antlr.Java".
2645 (put 'antlr-mode 'mode-name "Antlr")
2646
2647 ;;;###autoload
2648 (defun antlr-set-tabs ()
2649 "Use ANTLR's convention for TABs according to `antlr-tab-offset-alist'.
2650 Used in `antlr-mode'. Also a useful function in `java-mode-hook'."
2651 (if buffer-file-name
2652 (let ((alist antlr-tab-offset-alist) elem)
2653 (while alist
2654 (setq elem (pop alist))
2655 (and (or (null (car elem)) (eq (car elem) major-mode))
2656 (or (null (cadr elem))
2657 (string-match (cadr elem) buffer-file-name))
2658 (setq tab-width (caddr elem)
2659 indent-tabs-mode (cadddr elem)
2660 alist nil))))))
2661
2662 (provide 'antlr-mode)
2663
2664 ;;; Local IspellPersDict: .ispell_antlr
2665
2666 ;;; antlr-mode.el ends here