antlr-mode.el 107 KB
Newer Older
1
;;; antlr-mode.el --- major mode for ANTLR grammar files
2

Glenn Morris's avatar
Glenn Morris committed
3 4 5
;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
;;   2008, 2009  Free Software Foundation, Inc.

6
;; Author: Christoph.Wedler@sap.com
7
;; Keywords: languages, ANTLR, code generator
Juanma Barranquero's avatar
Juanma Barranquero committed
8 9
;; Version: (see `antlr-version' below)
;; X-URL: http://antlr-mode.sourceforge.net/
10 11 12

;; This file is part of GNU Emacs.

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 16
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
17 18 19 20 21 22 23

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
24
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
25 26 27

;;; Commentary:

Juanma Barranquero's avatar
Juanma Barranquero committed
28 29 30 31
;; The Emacs package ANTLR-Mode provides: syntax highlighting for ANTLR grammar
;; files, automatic indentation, menus containing rule/token definitions and
;; supported options and various other things like running ANTLR from within
;; Emacs.
32

Juanma Barranquero's avatar
Juanma Barranquero committed
33 34 35 36 37 38 39 40 41 42 43
;; For details, check <http://antlr-mode.sourceforge.net/> or, if you prefer
;; the manual style, follow all commands mentioned in the documentation of
;; `antlr-mode'.  ANTLR is a LL(k)-based recognition tool which generates
;; lexers, parsers and tree transformers in Java, C++ or Sather and can be
;; found at <http://www.antlr.org/>.

;; Bug fixes, bug reports, improvements, and suggestions for the newest version
;; are strongly appreciated.

;; To-do/Wish-list:
;;
44 45 46 47 48 49 50 51 52
;;  * Next Version [C-c C-w].  Produce HTML document with syntax highlighted
;;    and hyper-links (using htmlize).
;;  * Next Version [C-c C-u].  Insert/update special comments: each rule lists
;;    all rules which use the current rule.  With font-lock update.
;;  * Next Version.  Make hiding much more customizable.
;;  * Planned [C-c C-j].  Jump to generated coding.
;;  * Planned.  Further support for imenu, i.e., include entries for method
;;    definitions at beginning of grammar class.
;;  * Planned [C-c C-p].  Pack/unpack rule/subrule & options (one/multi-line).
Juanma Barranquero's avatar
Juanma Barranquero committed
53
;;
54 55 56
;;  * Probably.  Show rules/dependencies for ANT like for Makefile (does ANT
;;    support vocabularies and grammar inheritance?), I have to look at
;;    jde-ant.el: http://jakarta.apache.org/ant/manual/OptionalTasks/antlr.html
57 58 59
;;  * Probably.  Make `indent-region' faster, especially in actions.  ELP
;;    profiling in a class init action shows half the time is spent in
;;    `antlr-next-rule', the other half in `c-guess-basic-syntax'.
60 61 62 63 64 65 66
;;  * Unlikely.  Sather as generated language with syntax highlighting etc/.
;;    Questions/problems: is sather-mode.el the standard mode for sather, is it
;;    still supported, what is its relationship to eiffel3.el?  Requirement:
;;    this mode must not depend on a Sather mode.
;;  * Unlikely.  Faster syntax highlighting: sectionize the buffer into Antlr
;;    and action code and run special highlighting functions on these regions.
;;    Problems: code size, this mode would depend on font-lock internals.
67

68 69
;;; Installation:

70
;; This file requires Emacs-20.3, XEmacs-20.4 or higher and package cc-mode.
71 72 73 74 75 76 77 78

;; If antlr-mode is not part of your distribution, put this file into your
;; load-path and the following into your ~/.emacs:
;;   (autoload 'antlr-mode "antlr-mode" nil t)
;;   (setq auto-mode-alist (cons '("\\.g\\'" . antlr-mode) auto-mode-alist))
;;   (add-hook 'speedbar-load-hook  ; would be too late in antlr-mode.el
;;	       (lambda () (speedbar-add-supported-extension ".g")))

79 80 81
;; I strongly recommend to use font-lock with a support mode like fast-lock,
;; lazy-lock or better jit-lock (Emacs-21.1+) / lazy-shot (XEmacs).

82
;; To customize, use menu item "Antlr" -> "Customize Antlr".
83 84 85

;;; Code:

86 87 88
(eval-when-compile 
  (require 'cl))

Juanma Barranquero's avatar
Juanma Barranquero committed
89 90
(require 'easymenu)

91 92 93 94
;; Just to get the rid of the byte compiler warning.  The code for
;; this function and its friends are too complex for their own good.
(declare-function cond-emacs-xemacs-macfn "antlr-mode" (args &optional msg))

Juanma Barranquero's avatar
Juanma Barranquero committed
95
;; General Emacs/XEmacs-compatibility compile-time macros
96
(eval-when-compile 
Juanma Barranquero's avatar
Juanma Barranquero committed
97
  (defmacro cond-emacs-xemacs (&rest args)
98
    (cond-emacs-xemacs-macfn
Juanma Barranquero's avatar
Juanma Barranquero committed
99 100 101 102 103 104
     args "`cond-emacs-xemacs' must return exactly one element"))
  (defun cond-emacs-xemacs-macfn (args &optional msg)
    (if (atom args) args
      (and (eq (car args) :@) (null msg) ; (:@ ...spliced...)
	   (setq args (cdr args)
		 msg "(:@ ....) must return exactly one element"))
105
      (let ((ignore (if (featurep 'xemacs) :EMACS :XEMACS))
Juanma Barranquero's avatar
Juanma Barranquero committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	    (mode :BOTH) code)
	(while (consp args)
	  (if (memq (car args) '(:EMACS :XEMACS :BOTH)) (setq mode (pop args)))
	  (if (atom args)
	      (or args (error "Used selector %s without elements" mode))
	    (or (eq ignore mode)
		(push (cond-emacs-xemacs-macfn (car args)) code))
	    (pop args)))
	(cond (msg (if (or args (cdr code)) (error msg) (car code)))
	      ((or (null args) (eq ignore mode)) (nreverse code))
	      (t (nconc (nreverse code) args))))))
  ;; Emacs/XEmacs-compatibility `defun': remove interactive "_" for Emacs, use
  ;; existing functions when they are `fboundp', provide shortcuts if they are
  ;; known to be defined in a specific Emacs branch (for short .elc)
  (defmacro defunx (name arglist &rest definition)
121
    (let ((xemacsp (featurep 'xemacs)) reuses)
Juanma Barranquero's avatar
Juanma Barranquero committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
      (while (memq (car definition)
		   '(:try :emacs-and-try :xemacs-and-try))
	(if (eq (pop definition) (if xemacsp :xemacs-and-try :emacs-and-try))
	    (setq reuses (car definition)
		  definition nil)
	  (push (pop definition) reuses)))
      (if (and reuses (symbolp reuses))
	  `(defalias ',name ',reuses)
	(let* ((docstring (if (stringp (car definition)) (pop definition)))
	       (spec (and (not xemacsp)
			  (eq (car-safe (car definition)) 'interactive)
			  (null (cddar definition))
			  (cadar definition))))
	  (if (and (stringp spec)
		   (not (string-equal spec ""))
		   (eq (aref spec 0) ?_))
	      (setq definition
		    (cons (if (string-equal spec "_")
			      '(interactive)
			    `(interactive ,(substring spec 1)))
			  (cdr definition))))
	  (if (null reuses)
	      `(defun ,name ,arglist ,docstring
		 ,@(cond-emacs-xemacs-macfn definition))
	    ;; no dynamic docstring in this case
	    `(eval-and-compile		; no warnings in Emacs
	       (defalias ',name
		 (cond ,@(mapcar (lambda (func) `((fboundp ',func) ',func))
				 (nreverse reuses))
		       (t ,(if definition
			       `(lambda ,arglist ,docstring
				  ,@(cond-emacs-xemacs-macfn definition))
			     'ignore))))))))))
  (defmacro ignore-errors-x (&rest body)
    (let ((specials '((scan-sexps . 4) (scan-lists . 5)))
	  spec nils)
158
      (if (and (featurep 'xemacs)
Juanma Barranquero's avatar
Juanma Barranquero committed
159 160 161 162 163 164 165 166 167
	       (null (cdr body)) (consp (car body))
	       (setq spec (assq (caar body) specials))
	       (>= (setq nils (- (cdr spec) (length (car body)))) 0))
	  `(,@(car body) ,@(make-list nils nil) t)
	`(ignore-errors ,@body)))))

;; More compile-time-macros
(eval-when-compile
  (defmacro save-buffer-state-x (&rest body) ; similar to EMACS/lazy-lock.el
168
    (let ((modified (with-no-warnings (gensym "save-buffer-state-x-modified-"))))
Juanma Barranquero's avatar
Juanma Barranquero committed
169 170 171
      `(let ((,modified (buffer-modified-p)))
	 (unwind-protect
	     (let ((buffer-undo-list t) (inhibit-read-only t)
172
		   ,@(unless (featurep 'xemacs)
Juanma Barranquero's avatar
Juanma Barranquero committed
173 174 175 176 177 178 179 180 181
		       '((inhibit-point-motion-hooks t) deactivate-mark))
		   before-change-functions after-change-functions
		   buffer-file-name buffer-file-truename)
	       ,@body)
	   (and (not ,modified) (buffer-modified-p)
		(set-buffer-modified-p nil)))))))
(put 'save-buffer-state-x 'lisp-indent-function 0)

;; get rid of byte-compile warnings
182 183 184 185 186 187
(eval-when-compile
  (require 'cc-mode))

(defvar outline-level)
(defvar imenu-use-markers)
(defvar imenu-create-index-function)
188 189 190 191 192 193 194 195 196

;; We cannot use `c-forward-syntactic-ws' directly since it is a macro since
;; cc-mode-5.30 => antlr-mode compiled with older cc-mode would fail (macro
;; call) when used with newer cc-mode.  Also, antlr-mode compiled with newer
;; cc-mode would fail (undefined `c-forward-sws') when used with older cc-mode.
;; Additional to the `defalias' below, we must set `antlr-c-forward-sws' to
;; `c-forward-syntactic-ws' when `c-forward-sws' is not defined after requiring
;; cc-mode.
(defalias 'antlr-c-forward-sws 'c-forward-sws)
197 198 199 200 201 202 203 204 205 206 207


;;;;##########################################################################
;;;;  Variables
;;;;##########################################################################


(defgroup antlr nil
  "Major mode for ANTLR grammar files."
  :group 'languages
  :link '(emacs-commentary-link "antlr-mode.el")
Juanma Barranquero's avatar
Juanma Barranquero committed
208
  :link '(url-link "http://antlr-mode.sourceforge.net/")
209 210
  :prefix "antlr-")

211
(defconst antlr-version "2.2c"
Juanma Barranquero's avatar
Juanma Barranquero committed
212 213
  "ANTLR major mode version number.
Check <http://antlr-mode.sourceforge.net/> for the newest.")
214 215 216 217 218 219 220 221 222 223 224 225 226 227


;;;===========================================================================
;;;  Controlling ANTLR's code generator (language option)
;;;===========================================================================

(defvar antlr-language nil
  "Major mode corresponding to ANTLR's \"language\" option.
Set via `antlr-language-alist'.  The only useful place to change this
buffer-local variable yourself is in `antlr-mode-hook' or in the \"local
variable list\" near the end of the file, see
`enable-local-variables'.")

(defcustom antlr-language-alist
228 229
  '((java-mode "Java" nil "\"Java\"" "Java")
    (c++-mode "C++" "\"Cpp\"" "Cpp"))
230 231
  "List of ANTLR's supported languages.
Each element in this list looks like
232
  \(MAJOR-MODE MODELINE-STRING OPTION-VALUE...)
233 234

MAJOR-MODE, the major mode of the code in the grammar's actions, is the
235 236 237
value of `antlr-language' if the first group in the string matched by
REGEXP in `antlr-language-limit-n-regexp' is one of the OPTION-VALUEs.
An OPTION-VALUE of nil denotes the fallback element.  MODELINE-STRING is
238 239 240 241 242 243 244 245 246 247
also displayed in the modeline next to \"Antlr\"."
  :group 'antlr
  :type '(repeat (group :value (java-mode "")
			(function :tag "Major mode")
			(string :tag "Modeline string")
			(repeat :tag "ANTLR language option" :inline t
				(choice (const :tag "Default" nil)
					string )))))

(defcustom antlr-language-limit-n-regexp
248
  '(8192 . "language[ \t]*=[ \t]*\\(\"?[A-Z][A-Za-z_]*\"?\\)")
249
  "Used to set a reasonable value for `antlr-language'.
250
Looks like \(LIMIT \. REGEXP).  Search for REGEXP from the beginning of
251 252
the buffer to LIMIT and use the first group in the matched string to set
the language according to `antlr-language-alist'."
253 254 255 256 257 258
  :group 'antlr
  :type '(cons (choice :tag "Limit" (const :tag "No" nil) (integer :value 0))
	       regexp))


;;;===========================================================================
259
;;;  Hide/Unhide, Indent/Tabs
260 261
;;;===========================================================================

262 263 264 265 266
(defcustom antlr-action-visibility 3
  "Visibility of actions when command `antlr-hide-actions' is used.
If nil, the actions with their surrounding braces are hidden.  If a
number, do not hide the braces, only hide the contents if its length is
greater than this number."
267
  :group 'antlr
268 269
  :type '(choice (const :tag "Completely hidden" nil)
		 (integer :tag "Hidden if longer than" :value 3)))
270

271 272 273 274 275 276 277 278 279 280 281
(defcustom antlr-indent-comment 'tab
  "*Non-nil, if the indentation should touch lines in block comments.
If nil, no continuation line of a block comment is changed.  If t, they
are changed according to `c-indentation-line'.  When not nil and not t,
they are only changed by \\[antlr-indent-command]."
  :group 'antlr
  :type '(radio (const :tag "No" nil)
		(const :tag "Always" t)
		(sexp :tag "With TAB" :format "%t" :value tab)))

(defcustom antlr-tab-offset-alist
282 283
  '((antlr-mode nil 4 nil)
    (java-mode "antlr" 4 nil))
284
  "Alist to determine whether to use ANTLR's convention for TABs.
285
Each element looks like \(MAJOR-MODE REGEXP TAB-WIDTH INDENT-TABS-MODE).
286
The first element whose MAJOR-MODE is nil or equal to `major-mode' and
287 288
whose REGEXP is nil or matches variable `buffer-file-name' is used to
set `tab-width' and `indent-tabs-mode'.  This is useful to support both
289 290 291 292 293 294 295 296 297
ANTLR's and Java's indentation styles.  Used by `antlr-set-tabs'."
  :group 'antlr
  :type '(repeat (group :value (antlr-mode nil 8 nil)
			(choice (const :tag "All" nil)
				(function :tag "Major mode"))
			(choice (const :tag "All" nil) regexp)
			(integer :tag "Tab width")
			(boolean :tag "Indent-tabs-mode"))))

298 299
(defcustom antlr-indent-style "java"
  "*If non-nil, cc-mode indentation style used for `antlr-mode'.
Juanma Barranquero's avatar
Juanma Barranquero committed
300 301
See `c-set-style' and for details, where the most interesting part in
`c-style-alist' is the value of `c-basic-offset'."
302 303 304 305
  :group 'antlr
  :type '(choice (const nil) regexp))

(defcustom antlr-indent-item-regexp
Juanma Barranquero's avatar
Juanma Barranquero committed
306
  "[]}):;|&]" ; & is local ANTLR extension (SGML's and-connector)
307
  "Regexp matching lines which should be indented by one TAB less.
308 309 310 311 312 313
See `antlr-indent-line' and command \\[antlr-indent-command]."
  :group 'antlr
  :type 'regexp)

(defcustom antlr-indent-at-bol-alist
  ;; eval-when-compile not usable with defcustom...
Juanma Barranquero's avatar
Juanma Barranquero committed
314 315
  '((java-mode . "\\(package\\|import\\)\\>")
    (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\\)\\>"))
316 317 318 319
  "Alist of regexps matching lines are indented at column 0.
Each element in this list looks like (MODE . REGEXP) where MODE is a
function and REGEXP is a regular expression.

Juanma Barranquero's avatar
Juanma Barranquero committed
320 321
If `antlr-language' equals to a MODE, the line starting at the first
non-whitespace is matched by the corresponding REGEXP, and the line is
322
part of a header action, indent the line at column 0 instead according
Juanma Barranquero's avatar
Juanma Barranquero committed
323
to the normal rules of `antlr-indent-line'."
324 325 326
  :group 'antlr
  :type '(repeat (cons (function :tag "Major mode") regexp)))

Juanma Barranquero's avatar
Juanma Barranquero committed
327 328 329 330 331 332 333 334 335
;; adopt indentation to cc-engine
(defvar antlr-disabling-cc-syntactic-symbols
  '(statement-block-intro
    defun-block-intro topmost-intro statement-case-intro member-init-intro
    arglist-intro brace-list-intro knr-argdecl-intro inher-intro
    objc-method-intro
    block-close defun-close class-close brace-list-close arglist-close
    inline-close extern-lang-close namespace-close))

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

;;;===========================================================================
;;;  Options: customization
;;;===========================================================================

(defcustom antlr-options-use-submenus t
  "*Non-nil, if the major mode menu should include option submenus.
If nil, the menu just includes a command to insert options.  Otherwise,
it includes four submenus to insert file/grammar/rule/subrule options."
  :group 'antlr
  :type 'boolean)

(defcustom antlr-tool-version 20701
  "*The version number of the Antlr tool.
The value is an integer of the form XYYZZ which stands for vX.YY.ZZ.
This variable is used to warn about non-supported options and to supply
version correct option values when using \\[antlr-insert-option].

Don't use a number smaller than 20600 since the stored history of
Antlr's options starts with v2.06.00, see `antlr-options-alists'.  You
can make this variable buffer-local."
  :group 'antlr
  :type 'integer)

(defcustom antlr-options-auto-colon t
  "*Non-nil, if `:' is inserted with a rule or subrule options section.
A `:' is only inserted if this value is non-nil, if a rule or subrule
option is inserted with \\[antlr-insert-option], if there was no rule or
subrule options section before, and if a `:' is not already present
after the section, ignoring whitespace, comments and the init action."
  :group 'antlr
  :type 'boolean)

(defcustom antlr-options-style nil
  "List of symbols which determine the style of option values.
If a style symbol is present, the corresponding option value is put into
quotes, i.e., represented as a string, otherwise it is represented as an
identifier.

The only style symbol used in the default value of `antlr-options-alist'
is `language-as-string'.  See also `antlr-read-value'."
  :group 'antlr
  :type '(repeat (symbol :tag "Style symbol")))

(defcustom antlr-options-push-mark t
  "*Non-nil, if inserting an option should set & push mark.
If nil, never set mark when inserting an option with command
\\[antlr-insert-option].  If t, always set mark via `push-mark'.  If a
number, only set mark if point was outside the options area before and
the number of lines between point and the insert position is greater
than this value.  Otherwise, only set mark if point was outside the
options area before."
  :group 'antlr
  :type '(radio (const :tag "No" nil)
		(const :tag "Always" t)
		(integer :tag "Lines between" :value 10)
		(sexp :tag "If outside options" :format "%t" :value outside)))

(defcustom antlr-options-assign-string " = "
  "*String containing `=' to use between option name and value.
This string is only used if the option to insert did not exist before
or if there was no `=' after it.  In other words, the spacing around an
existing `=' won't be changed when changing an option value."
  :group 'antlr
  :type 'string)


;;;===========================================================================
;;;  Options: definitions
;;;===========================================================================

(defvar antlr-options-headings '("file" "grammar" "rule" "subrule")
  "Headings for the four different option kinds.
The standard value is (\"file\" \"grammar\" \"rule\" \"subrule\").  See
`antlr-options-alists'")

(defvar antlr-options-alists
  '(;; file options ----------------------------------------------------------
    (("language" antlr-language-option-extra
      (20600 antlr-read-value
	     "Generated language: " language-as-string
	     (("Java") ("Cpp") ("HTML") ("Diagnostic")))
      (20700 antlr-read-value
	     "Generated language: " language-as-string
	     (("Java") ("Cpp") ("HTML") ("Diagnostic") ("Sather"))))
     ("mangleLiteralPrefix" nil
      (20600 antlr-read-value
	     "Prefix for literals (default LITERAL_): " t))
     ("namespace" antlr-c++-mode-extra
      (20700 antlr-read-value
	     "Wrap generated C++ code in namespace: " t))
     ("namespaceStd" antlr-c++-mode-extra
      (20701 antlr-read-value
	     "Replace ANTLR_USE_NAMESPACE(std) by: " t))
     ("namespaceAntlr" antlr-c++-mode-extra
      (20701 antlr-read-value
	     "Replace ANTLR_USE_NAMESPACE(antlr) by: " t))
     ("genHashLines" antlr-c++-mode-extra
      (20701 antlr-read-boolean
	     "Include #line in generated C++ code? "))
     )
    ;; grammar options --------------------------------------------------------
    (("k" nil
      (20600 antlr-read-value
	     "Lookahead depth: "))
     ("importVocab" nil
      (20600 antlr-read-value
	     "Import vocabulary: "))
     ("exportVocab" nil
      (20600 antlr-read-value
	     "Export vocabulary: "))
     ("testLiterals" nil		; lexer only
      (20600 antlr-read-boolean
	     "Test each token against literals table? "))
     ("defaultErrorHandler" nil		; not for lexer
      (20600 antlr-read-boolean
	     "Generate default exception handler for each rule? "))
     ("codeGenMakeSwitchThreshold" nil
      (20600 antlr-read-value
	     "Min number of alternatives for 'switch': "))
     ("codeGenBitsetTestThreshold" nil
      (20600 antlr-read-value
	     "Min size of lookahead set for bitset test: "))
     ("analyzerDebug" nil
      (20600 antlr-read-boolean
	     "Display debugging info during grammar analysis? "))
     ("codeGenDebug" nil
      (20600 antlr-read-boolean
	     "Display debugging info during code generation? "))
     ("buildAST" nil			; not for lexer
      (20600 antlr-read-boolean
	     "Use automatic AST construction/transformation? "))
     ("ASTLabelType" nil		; not for lexer
      (20600 antlr-read-value
	     "Class of user-defined AST node: " t))
     ("charVocabulary" nil		; lexer only
      (20600 nil
	     "Insert character vocabulary"))
     ("interactive" nil
      (20600 antlr-read-boolean
	     "Generate interactive lexer/parser? "))
     ("caseSensitive" nil		; lexer only
      (20600 antlr-read-boolean
	     "Case significant when matching characters? "))
     ("caseSensitiveLiterals" nil	; lexer only
      (20600 antlr-read-boolean
	     "Case significant when testing literals table? "))
     ("classHeaderSuffix" nil
      (20600 nil
	     "Additional string for grammar class definition"))
     ("filter" nil			; lexer only
      (20600 antlr-read-boolean
	     "Skip rule (the name, true or false): "
	     antlr-grammar-tokens))
     ("namespace" antlr-c++-mode-extra
      (20700 antlr-read-value
	     "Wrap generated C++ code for grammar in namespace: " t))
     ("namespaceStd" antlr-c++-mode-extra
      (20701 antlr-read-value
	     "Replace ANTLR_USE_NAMESPACE(std) by: " t))
     ("namespaceAntlr" antlr-c++-mode-extra
      (20701 antlr-read-value
	     "Replace ANTLR_USE_NAMESPACE(antlr) by: " t))
     ("genHashLines" antlr-c++-mode-extra
      (20701 antlr-read-boolean
	     "Include #line in generated C++ code? "))
;;;     ("autoTokenDef" nil		; parser only
;;;      (80000 antlr-read-boolean		; default: true
;;;	     "Automatically define referenced token? "))
;;;     ("keywordsMeltTo" nil		; parser only
;;;      (80000 antlr-read-value
;;;	     "Change non-matching keywords to token type: "))
     )
    ;; rule options ----------------------------------------------------------
    (("testLiterals" nil		; lexer only
      (20600 antlr-read-boolean
	     "Test this token against literals table? "))
     ("defaultErrorHandler" nil		; not for lexer
      (20600 antlr-read-boolean
	     "Generate default exception handler for this rule? "))
     ("ignore" nil			; lexer only
      (20600 antlr-read-value
	     "In this rule, ignore tokens of type: " nil
	     antlr-grammar-tokens))
     ("paraphrase" nil			; lexer only
      (20600 antlr-read-value
	     "In messages, replace name of this token by: " t))
     )
    ;; subrule options -------------------------------------------------------
    (("warnWhenFollowAmbig" nil
      (20600 antlr-read-boolean
	     "Display warnings for ambiguities with FOLLOW? "))
     ("generateAmbigWarnings" nil
      (20600 antlr-read-boolean
	     "Display warnings for ambiguities? "))
     ("greedy" nil
      (20700 antlr-read-boolean
	     "Make this optional/loop subrule greedy? "))
     ))
  "Definitions for Antlr's options of all four different kinds.

The value looks like \(FILE GRAMMAR RULE SUBRULE) where each FILE,
GRAMMAR, RULE, and SUBRULE is a list of option definitions of the
corresponding kind, i.e., looks like \(OPTION-DEF...).

Each OPTION-DEF looks like \(OPTION-NAME EXTRA-FN VALUE-SPEC...) which
defines a file/grammar/rule/subrule option with name OPTION-NAME.  The
OPTION-NAMEs are used for the creation of the \"Insert XXX Option\"
submenus, see `antlr-options-use-submenus', and to allow to insert the
option name with completion when using \\[antlr-insert-option].

If EXTRA-FN is a function, it is called at different phases of the
insertion with arguments \(PHASE OPTION-NAME).  PHASE can have the
values `before-input' or `after-insertion', additional phases might be
defined in future versions of this mode.  The phase `before-input'
occurs before the user is asked to insert a value.  The phase
`after-insertion' occurs after the option value has been inserted.
EXTRA-FN might be called with additional arguments in future versions of
this mode.

Each specification VALUE-SPEC looks like \(VERSION READ-FN ARG...).  The
last VALUE-SPEC in an OPTION-DEF whose VERSION is smaller or equal to
`antlr-tool-version' specifies how the user is asked for the value of
the option.

If READ-FN is nil, the only ARG is a string which is printed at the echo
area to guide the user what to insert at point.  Otherwise, READ-FN is
called with arguments \(INIT-VALUE ARG...) to get the new value of the
option.  INIT-VALUE is the old value of the option or nil.

The standard value contains the following functions as READ-FN:
`antlr-read-value' with ARGs = \(PROMPT AS-STRING TABLE) which reads a
general value, or `antlr-read-boolean' with ARGs = \(PROMPT TABLE) which
reads a boolean value or a member of TABLE.  PROMPT is the prompt when
asking for a new value.  If non-nil, TABLE is a table for completion or
571
a function evaluating to such a table.  The return value is quoted if
572 573
AS-STRING is non-nil and is either t or a symbol which is a member of
`antlr-options-style'.")
574 575


576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
;;;===========================================================================
;;;  Run tool, create Makefile dependencies
;;;===========================================================================

(defcustom antlr-tool-command "java antlr.Tool"
  "*Command used in \\[antlr-run-tool] to run the Antlr tool.
This variable should include all options passed to Antlr except the
option \"-glib\" which is automatically suggested if necessary."
  :group 'antlr
  :type 'string)

(defcustom antlr-ask-about-save t
  "*If not nil, \\[antlr-run-tool] asks which buffers to save.
Otherwise, it saves all modified buffers before running without asking."
  :group 'antlr
  :type 'boolean)

(defcustom antlr-makefile-specification
  '("\n" ("GENS" "GENS%d" " \\\n\t") "$(ANTLR)")
  "*Variable to specify the appearance of the generated makefile rules.
This variable influences the output of \\[antlr-show-makefile-rules].
597
It looks like \(RULE-SEP GEN-VAR-SPEC COMMAND).
598 599 600 601 602 603 604

RULE-SEP is the string to separate different makefile rules.  COMMAND is
a string with the command which runs the Antlr tool, it should include
all options except the option \"-glib\" which is automatically added
if necessary.

If GEN-VAR-SPEC is nil, each target directly consists of a list of
605
files.  If GEN-VAR-SPEC looks like \(GEN-VAR GEN-VAR-FORMAT GEN-SEP), a
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
Makefile variable is created for each rule target.

Then, GEN-VAR is a string with the name of the variable which contains
the file names of all makefile rules.  GEN-VAR-FORMAT is a format string
producing the variable of each target with substitution COUNT/%d where
COUNT starts with 1.  GEN-SEP is used to separate long variable values."
  :group 'antlr
  :type '(list (string :tag "Rule separator")
	       (choice
		(const :tag "Direct targets" nil)
		(list :tag "Variables for targets"
		      (string :tag "Variable for all targets")
		      (string :tag "Format for each target variable")
		      (string :tag "Variable separator")))
	       (string :tag "ANTLR command")))

(defvar antlr-file-formats-alist
  '((java-mode ("%sTokenTypes.java") ("%s.java"))
    (c++-mode ("%sTokenTypes.hpp") ("%s.cpp" "%s.hpp")))
  "Language dependent formats which specify generated files.
Each element in this list looks looks like
627
  \(MAJOR-MODE (VOCAB-FILE-FORMAT...) (CLASS-FILE-FORMAT...)).
628 629 630 631 632 633 634 635 636 637 638 639

The element whose MAJOR-MODE is equal to `antlr-language' is used to
specify the generated files which are language dependent.  See variable
`antlr-special-file-formats' for language independent files.

VOCAB-FILE-FORMAT is a format string, it specifies with substitution
VOCAB/%s the generated file for each export vocabulary VOCAB.
CLASS-FILE-FORMAT is a format string, it specifies with substitution
CLASS/%s the generated file for each grammar class CLASS.")

(defvar antlr-special-file-formats '("%sTokenTypes.txt" "expanded%s.g")
  "Language independent formats which specify generated files.
640
The value looks like \(VOCAB-FILE-FORMAT EXPANDED-GRAMMAR-FORMAT).
641 642 643 644 645 646 647 648 649 650 651 652 653

VOCAB-FILE-FORMAT is a format string, it specifies with substitution
VOCAB/%s the generated or input file for each export or import
vocabulary VOCAB, respectively.  EXPANDED-GRAMMAR-FORMAT is a format
string, it specifies with substitution GRAMMAR/%s the constructed
grammar file if the file GRAMMAR.g contains a grammar class which
extends a class other than \"Lexer\", \"Parser\" or \"TreeParser\".

See variable `antlr-file-formats-alist' for language dependent
formats.")

(defvar antlr-unknown-file-formats '("?%s?.g" "?%s?")
  "*Formats which specify the names of unknown files.
654
The value looks like \(SUPER-GRAMMAR-FILE-FORMAT SUPER-EVOCAB-FORMAT).
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

SUPER-GRAMMAR-FORMAT is a format string, it specifies with substitution
SUPER/%s the name of a grammar file for Antlr's option \"-glib\" if no
grammar file in the current directory defines the class SUPER or if it
is defined more than once.  SUPER-EVOCAB-FORMAT is a format string, it
specifies with substitution SUPER/%s the name for the export vocabulary
of above mentioned class SUPER.")

(defvar antlr-help-unknown-file-text
  "## The following rules contain filenames of the form
##  \"?SUPERCLASS?.g\" (and \"?SUPERCLASS?TokenTypes.txt\")
## where SUPERCLASS is not found to be defined in any grammar file of
## the current directory or is defined more than once.  Please replace
## these filenames by the grammar files (and their exportVocab).\n\n"
  "String indicating the existence of unknown files in the Makefile.
See \\[antlr-show-makefile-rules] and `antlr-unknown-file-formats'.")

(defvar antlr-help-rules-intro
  "The following Makefile rules define the dependencies for all (non-
expanded) grammars in directory \"%s\".\n
They are stored in the kill-ring, i.e., you can insert them with C-y
into your Makefile.  You can also invoke M-x antlr-show-makefile-rules
from within a Makefile to insert them directly.\n\n\n"
  "Introduction to use with \\[antlr-show-makefile-rules].
It is a format string and used with substitution DIRECTORY/%s where
DIRECTORY is the name of the current directory.")


683 684 685 686
;;;===========================================================================
;;;  Menu
;;;===========================================================================

Juanma Barranquero's avatar
Juanma Barranquero committed
687
(defcustom antlr-imenu-name t ; (featurep 'xemacs) ; TODO: Emacs-21 bug?
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
  "*Non-nil, if a \"Index\" menu should be added to the menubar.
If it is a string, it is used instead \"Index\".  Requires package
imenu."
  :group 'antlr
  :type '(choice (const :tag "No menu" nil)
		 (const :tag "Index menu" t)
		 (string :tag "Other menu name")))

(defvar antlr-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\t" 'antlr-indent-command)
    (define-key map "\e\C-a" 'antlr-beginning-of-rule)
    (define-key map "\e\C-e" 'antlr-end-of-rule)
    (define-key map "\C-c\C-a" 'antlr-beginning-of-body)
    (define-key map "\C-c\C-e" 'antlr-end-of-body)
    (define-key map "\C-c\C-f" 'c-forward-into-nomenclature)
    (define-key map "\C-c\C-b" 'c-backward-into-nomenclature)
Gerd Moellmann's avatar
Gerd Moellmann committed
705
    (define-key map "\C-c\C-c" 'comment-region)
706
    (define-key map "\C-c\C-v" 'antlr-hide-actions)
707
    (define-key map "\C-c\C-r" 'antlr-run-tool)
708
    (define-key map "\C-c\C-o" 'antlr-insert-option)
709 710 711
    ;; I'm too lazy to define my own:
    (define-key map "\ea" 'c-beginning-of-statement)
    (define-key map "\ee" 'c-end-of-statement)
712 713 714 715 716 717 718 719 720
    ;; electric keys:
    (define-key map ":" 'antlr-electric-character)
    (define-key map ";" 'antlr-electric-character)
    (define-key map "|" 'antlr-electric-character)
    (define-key map "&" 'antlr-electric-character)
    (define-key map "(" 'antlr-electric-character)
    (define-key map ")" 'antlr-electric-character)
    (define-key map "{" 'antlr-electric-character)
    (define-key map "}" 'antlr-electric-character)
721 722 723
    map)
  "Keymap used in `antlr-mode' buffers.")

724 725 726
(easy-menu-define antlr-mode-menu antlr-mode-map
  "Major mode menu."
  `("Antlr"
Juanma Barranquero's avatar
Juanma Barranquero committed
727 728 729 730
    ,@(if (cond-emacs-xemacs
	   :EMACS (and antlr-options-use-submenus
		       (>= emacs-major-version 21))
	   :XEMACS antlr-options-use-submenus)
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	  `(("Insert File Option"
	     :filter ,(lambda (x) (antlr-options-menu-filter 1 x)))
	    ("Insert Grammar Option"
	     :filter ,(lambda (x) (antlr-options-menu-filter 2 x)))
	    ("Insert Rule Option"
	     :filter ,(lambda (x) (antlr-options-menu-filter 3 x)))
	    ("Insert Subrule Option"
	     :filter ,(lambda (x) (antlr-options-menu-filter 4 x)))
	    "---")
	'(["Insert Option" antlr-insert-option
	   :active (not buffer-read-only)]))
    ("Forward/Backward"
     ["Backward Rule" antlr-beginning-of-rule t]
     ["Forward Rule" antlr-end-of-rule t]
     ["Start of Rule Body" antlr-beginning-of-body
      :active (antlr-inside-rule-p)]
     ["End of Rule Body" antlr-end-of-body
      :active (antlr-inside-rule-p)]
     "---"
     ["Backward Statement" c-beginning-of-statement t]
     ["Forward Statement" c-end-of-statement t]
     ["Backward Into Nomencl." c-backward-into-nomenclature t]
     ["Forward Into Nomencl." c-forward-into-nomenclature t])
    ["Indent Region" indent-region
     :active (and (not buffer-read-only) (c-region-is-active-p))]
    ["Comment Out Region" comment-region
     :active (and (not buffer-read-only) (c-region-is-active-p))]
    ["Uncomment Region"
     (comment-region (region-beginning) (region-end) '(4))
     :active (and (not buffer-read-only) (c-region-is-active-p))]
    "---"
    ["Hide Actions (incl. Args)" antlr-hide-actions t]
    ["Hide Actions (excl. Args)" (antlr-hide-actions 2) t]
    ["Unhide All Actions" (antlr-hide-actions 0) t]
    "---"
    ["Run Tool on Grammar" antlr-run-tool t]
    ["Show Makefile Rules" antlr-show-makefile-rules t]
    "---"
    ["Customize Antlr" (customize-group 'antlr) t]))
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785


;;;===========================================================================
;;;  font-lock
;;;===========================================================================

(defcustom antlr-font-lock-maximum-decoration 'inherit
  "*The maximum decoration level for fontifying actions.
Value `none' means, do not fontify actions, just normal grammar code
according to `antlr-font-lock-additional-keywords'.  Value `inherit'
means, use value of `font-lock-maximum-decoration'.  Any other value is
interpreted as in `font-lock-maximum-decoration' with no level-0
fontification, see `antlr-font-lock-keywords-alist'.

While calculating the decoration level for actions, `major-mode' is
bound to `antlr-language'.  For example, with value
786
  \((java-mode \. 2) (c++-mode \. 0))
787 788
Java actions are fontified with level 2 and C++ actions are not
fontified at all."
789
  :group 'antlr
790 791 792 793 794 795
  :type '(choice (const :tag "None" none)
		 (const :tag "Inherit" inherit)
		 (const :tag "Default" nil)
		 (const :tag "Maximum" t)
		 (integer :tag "Level" 1)
		 (repeat :menu-tag "Mode specific" :tag "Mode specific"
796 797 798
			 :value ((t . t))
			 (cons :tag "Instance"
			       (radio :tag "Mode"
799 800
				      (const :tag "All" t)
				      (symbol :tag "Name"))
801
			       (radio :tag "Decoration"
802 803 804
				      (const :tag "Default" nil)
				      (const :tag "Maximum" t)
				      (integer :tag "Level" 1))))))
805

806 807 808 809 810 811 812
(defconst antlr-no-action-keywords nil
  ;; Using nil directly won't work (would use highest level, see
  ;; `font-lock-choose-keywords'), but a non-symbol, i.e., (list), at `car'
  ;; would break Emacs-21.0:
  "Empty font-lock keywords for actions.
Do not change the value of this constant.")

813 814
(defvar antlr-font-lock-keywords-alist
  '((java-mode
815
     antlr-no-action-keywords
816 817 818
     java-font-lock-keywords-1 java-font-lock-keywords-2
     java-font-lock-keywords-3)
    (c++-mode
819
     antlr-no-action-keywords
820 821 822 823
     c++-font-lock-keywords-1 c++-font-lock-keywords-2
     c++-font-lock-keywords-3))
  "List of font-lock keywords for actions in the grammar.
Each element in this list looks like
824
  \(MAJOR-MODE KEYWORD...)
825 826 827 828 829 830

If `antlr-language' is equal to MAJOR-MODE, the KEYWORDs are the
font-lock keywords according to `font-lock-defaults' used for the code
in the grammar's actions and semantic predicates, see
`antlr-font-lock-maximum-decoration'.")

831
(defvar antlr-default-face 'antlr-default)
832
(defface antlr-default '((t nil))
833 834 835
  "Face to prevent strings from language dependent highlighting.
Do not change."
  :group 'antlr)
836 837
;; backward-compatibility alias
(put 'antlr-font-lock-default-face 'face-alias 'antlr-default)
838

839 840
(defvar antlr-keyword-face 'antlr-keyword)
(defface antlr-keyword
Juanma Barranquero's avatar
Juanma Barranquero committed
841 842
  (cond-emacs-xemacs
   '((((class color) (background light))
Glenn Morris's avatar
Glenn Morris committed
843 844
      (:foreground "black" :EMACS :weight bold :XEMACS :bold t))
     (t :inherit font-lock-keyword-face)))
845 846
  "ANTLR keywords."
  :group 'antlr)
847 848
;; backward-compatibility alias
(put 'antlr-font-lock-keyword-face 'face-alias 'antlr-keyword)
849

850 851
(defvar antlr-syntax-face 'antlr-keyword)
(defface antlr-syntax
Juanma Barranquero's avatar
Juanma Barranquero committed
852 853
  (cond-emacs-xemacs
   '((((class color) (background light))
Glenn Morris's avatar
Glenn Morris committed
854 855
      (:foreground "black" :EMACS :weight bold :XEMACS :bold t))
     (t :inherit font-lock-constant-face)))
856 857
  "ANTLR syntax symbols like :, |, (, ), ...."
  :group 'antlr)
858 859
;; backward-compatibility alias
(put 'antlr-font-lock-syntax-face 'face-alias 'antlr-syntax)
860

861 862
(defvar antlr-ruledef-face 'antlr-ruledef)
(defface antlr-ruledef
Juanma Barranquero's avatar
Juanma Barranquero committed
863 864
  (cond-emacs-xemacs
   '((((class color) (background light))
Glenn Morris's avatar
Glenn Morris committed
865 866
      (:foreground "blue" :EMACS :weight bold :XEMACS :bold t))
     (t :inherit font-lock-function-name-face)))
867 868
  "ANTLR rule references (definition)."
  :group 'antlr)
869 870
;; backward-compatibility alias
(put 'antlr-font-lock-ruledef-face 'face-alias 'antlr-ruledef)
871

872 873
(defvar antlr-tokendef-face 'antlr-tokendef)
(defface antlr-tokendef
Juanma Barranquero's avatar
Juanma Barranquero committed
874 875
  (cond-emacs-xemacs
   '((((class color) (background light))
Glenn Morris's avatar
Glenn Morris committed
876 877
      (:foreground "blue" :EMACS :weight bold :XEMACS :bold t))
     (t :inherit font-lock-function-name-face)))
878 879
  "ANTLR token references (definition)."
  :group 'antlr)
880 881
;; backward-compatibility alias
(put 'antlr-font-lock-tokendef-face 'face-alias 'antlr-tokendef)
882

883 884
(defvar antlr-ruleref-face 'antlr-ruleref)
(defface antlr-ruleref
Glenn Morris's avatar
Glenn Morris committed
885 886
  '((((class color) (background light)) (:foreground "blue4"))
    (t :inherit font-lock-type-face))
887 888
  "ANTLR rule references (usage)."
  :group 'antlr)
889 890
;; backward-compatibility alias
(put 'antlr-font-lock-ruleref-face 'face-alias 'antlr-ruleref)
891

892 893
(defvar antlr-tokenref-face 'antlr-tokenref)
(defface antlr-tokenref
Glenn Morris's avatar
Glenn Morris committed
894 895
  '((((class color) (background light)) (:foreground "orange4"))
    (t :inherit font-lock-type-face))
896 897
  "ANTLR token references (usage)."
  :group 'antlr)
898 899
;; backward-compatibility alias
(put 'antlr-font-lock-tokenref-face 'face-alias 'antlr-tokenref)
900

901 902
(defvar antlr-literal-face 'antlr-literal)
(defface antlr-literal
Juanma Barranquero's avatar
Juanma Barranquero committed
903 904
  (cond-emacs-xemacs
   '((((class color) (background light))
Glenn Morris's avatar
Glenn Morris committed
905 906
      (:foreground "brown4" :EMACS :weight bold :XEMACS :bold t))
     (t :inherit font-lock-string-face)))
907 908 909
  "ANTLR special literal tokens.
It is used to highlight strings matched by the first regexp group of
`antlr-font-lock-literal-regexp'."
910
  :group 'antlr)
911 912
;; backward-compatibility alias
(put 'antlr-font-lock-literal-face 'face-alias 'antlr-literal)
913

914 915 916 917 918 919 920 921 922 923 924 925 926
(defcustom antlr-font-lock-literal-regexp "\"\\(\\sw\\(\\sw\\|-\\)*\\)\""
  "Regexp matching literals with special syntax highlighting, or nil.
If nil, there is no special syntax highlighting for some literals.
Otherwise, it should be a regular expression which must contain a regexp
group.  The string matched by the first group is highlighted with
`antlr-font-lock-literal-face'."
  :group 'antlr
  :type '(choice (const :tag "None" nil) regexp))

(defvar antlr-class-header-regexp
  "\\(class\\)[ \t]+\\([A-Za-z\300-\326\330-\337]\\sw*\\)[ \t]+\\(extends\\)[ \t]+\\([A-Za-z\300-\326\330-\337]\\sw*\\)[ \t]*;"
  "Regexp matching class headers.")

927
(defvar antlr-font-lock-additional-keywords
Juanma Barranquero's avatar
Juanma Barranquero committed
928 929 930
  (cond-emacs-xemacs
   `((antlr-invalidate-context-cache)
     ("\\$setType[ \t]*(\\([A-Za-z\300-\326\330-\337]\\sw*\\))"
931
      (1 antlr-tokendef-face))
932
     ("\\$\\sw+" (0 antlr-keyword-face))
Juanma Barranquero's avatar
Juanma Barranquero committed
933 934
     ;; the tokens are already fontified as string/docstrings:
     (,(lambda (limit)
935
	 (if antlr-font-lock-literal-regexp
Juanma Barranquero's avatar
Juanma Barranquero committed
936
	     (antlr-re-search-forward antlr-font-lock-literal-regexp limit)))
937
      (1 antlr-literal-face t)
Juanma Barranquero's avatar
Juanma Barranquero committed
938 939 940
      :XEMACS (0 nil))			; XEmacs bug workaround
     (,(lambda (limit)
	 (antlr-re-search-forward antlr-class-header-regexp limit))
941 942 943
      (1 antlr-keyword-face)
      (2 antlr-ruledef-face)
      (3 antlr-keyword-face)
Juanma Barranquero's avatar
Juanma Barranquero committed
944
      (4 (if (member (match-string 4) '("Lexer" "Parser" "TreeParser"))
945 946
	     antlr-keyword-face
	   type-face)))
Juanma Barranquero's avatar
Juanma Barranquero committed
947 948 949 950
     (,(lambda (limit)
	 (antlr-re-search-forward
	  "\\<\\(header\\|options\\|tokens\\|exception\\|catch\\|returns\\)\\>"
	  limit))
951
     (1 antlr-keyword-face))
Juanma Barranquero's avatar
Juanma Barranquero committed
952 953 954 955
     (,(lambda (limit)
	 (antlr-re-search-forward
	  "^\\(private\\|public\\|protected\\)\\>[ \t]*\\(\\(\\sw+[ \t]*\\(:\\)?\\)\\)?"
	  limit))
956 957
     (1 font-lock-type-face)		; not XEmacs' java level-3 fruit salad
     (3 (if (antlr-upcase-p (char-after (match-beginning 3)))
958 959 960
	    antlr-tokendef-face
	  antlr-ruledef-face) nil t)
     (4 antlr-syntax-face nil t))
Juanma Barranquero's avatar
Juanma Barranquero committed
961 962
     (,(lambda (limit)
	 (antlr-re-search-forward "^\\(\\sw+\\)[ \t]*\\(:\\)?" limit))
963
     (1 (if (antlr-upcase-p (char-after (match-beginning 0)))
964 965 966
	    antlr-tokendef-face
	  antlr-ruledef-face) nil t)
     (2 antlr-syntax-face nil t))
Juanma Barranquero's avatar
Juanma Barranquero committed
967 968 969
     (,(lambda (limit)
	 ;; v:ruleref and v:"literal" is allowed...
	 (antlr-re-search-forward "\\(\\sw+\\)[ \t]*\\([=:]\\)?" limit))
970 971
     (1 (if (match-beginning 2)
	    (if (eq (char-after (match-beginning 2)) ?=)
972 973
		antlr-default-face
	      font-lock-variable-name-face)
974
	  (if (antlr-upcase-p (char-after (match-beginning 1)))
975 976 977
	      antlr-tokenref-face
	    antlr-ruleref-face)))
     (2 antlr-default-face nil t))
Juanma Barranquero's avatar
Juanma Barranquero committed
978 979
     (,(lambda (limit)
	 (antlr-re-search-forward "[|&:;(~]\\|)\\([*+?]\\|=>\\)?" limit))
980
     (0 antlr-syntax-face))))
981 982 983 984 985 986
  "Font-lock keywords for ANTLR's normal grammar code.
See `antlr-font-lock-keywords-alist' for the keywords of actions.")

(defvar antlr-font-lock-defaults
  '(antlr-font-lock-keywords
    nil nil ((?_ . "w") (?\( . ".") (?\) . ".")) beginning-of-defun)
987
  "Font-lock defaults used for ANTLR syntax highlighting.
988 989 990 991 992 993 994 995 996 997 998
The SYNTAX-ALIST element is also used to initialize
`antlr-action-syntax-table'.")


;;;===========================================================================
;;;  Internal variables
;;;===========================================================================

(defvar antlr-mode-hook nil
  "Hook called by `antlr-mode'.")

999 1000 1001 1002
(defvar antlr-mode-syntax-table nil
  "Syntax table used in `antlr-mode' buffers.
If non-nil, it will be initialized in `antlr-mode'.")

1003 1004 1005
;; used for "in Java/C++ code" = syntactic-depth>0
(defvar antlr-action-syntax-table nil
  "Syntax table used for ANTLR action parsing.
1006 1007 1008 1009
Initialized by `antlr-mode-syntax-table', changed by SYNTAX-ALIST in
`antlr-font-lock-defaults'.  This table should be selected if you use
`buffer-syntactic-context' and `buffer-syntactic-context-depth' in order
not to confuse their context_cache.")
1010 1011 1012 1013 1014

(defvar antlr-mode-abbrev-table nil
  "Abbreviation table used in `antlr-mode' buffers.")
(define-abbrev-table 'antlr-mode-abbrev-table ())

Juanma Barranquero's avatar
Juanma Barranquero committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
(defvar antlr-slow-cache-enabling-symbol 'loudly
;; Emacs' font-lock changes buffer's tick counter, therefore this value should
;; be a parameter of a font-lock function, but not any other variable of
;; functions which call `antlr-slow-syntactic-context'.
  "If value is a bound symbol, cache will be used even with text changes.
This is no user option.  Used for `antlr-slow-syntactic-context'.")

(defvar antlr-slow-cache-diff-threshold 5000
  "Maximum distance between `point' and cache position for cache use.
Used for `antlr-slow-syntactic-context'.")
1025 1026 1027 1028 1029 1030 1031


;;;;##########################################################################
;;;;  The Code
;;;;##########################################################################


1032

1033
;;;===========================================================================
Juanma Barranquero's avatar
Juanma Barranquero committed
1034
;;;  Syntax functions -- Emacs vs XEmacs dependent, part 1
1035 1036
;;;===========================================================================

1037
;; From help.el (XEmacs-21.1), without `copy-syntax-table'
1038
(defmacro antlr-with-syntax-table (syntab &rest body)
1039
  "Evaluate BODY with the syntax table SYNTAB."
1040 1041
  `(let ((stab (syntax-table)))
     (unwind-protect
1042
	 (progn (set-syntax-table ,syntab) ,@body)
1043 1044 1045 1046
       (set-syntax-table stab))))
(put 'antlr-with-syntax-table 'lisp-indent-function 1)
(put 'antlr-with-syntax-table 'edebug-form-spec '(form body))

Juanma Barranquero's avatar
Juanma Barranquero committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
(defunx antlr-default-directory ()
  :xemacs-and-try default-directory
  "Return `default-directory'."
  default-directory)

;; Check Emacs-21.1 simple.el, `shell-command'.
(defunx antlr-read-shell-command (prompt &optional initial-input history)
  :xemacs-and-try read-shell-command
  "Read a string from the minibuffer, using `shell-command-history'."
  (read-from-minibuffer prompt initial-input nil nil
			(or history 'shell-command-history)))

(defunx antlr-with-displaying-help-buffer (thunk &optional name)
  :xemacs-and-try with-displaying-help-buffer
  "Make a help buffer and call `thunk' there."
  (with-output-to-temp-buffer "*Help*"
    (save-excursion (funcall thunk))))


;;;===========================================================================
;;;  Context cache
;;;===========================================================================

(defvar antlr-slow-context-cache nil "Internal.")

;;;(defvar antlr-statistics-full-neg 0)
;;;(defvar antlr-statistics-full-diff 0)
;;;(defvar antlr-statistics-full-other 0)
;;;(defvar antlr-statistics-cache 0)
;;;(defvar antlr-statistics-inval 0)

(defunx antlr-invalidate-context-cache (&rest dummies)
1079
;; checkdoc-params: (dummies)
Juanma Barranquero's avatar
Juanma Barranquero committed
1080 1081
  "Invalidate context cache for syntactical context information."
  :XEMACS				; XEmacs bug workaround
1082 1083
  (save-excursion
    (set-buffer (get-buffer-create " ANTLR XEmacs bug workaround"))
Juanma Barranquero's avatar
Juanma Barranquero committed
1084 1085 1086 1087 1088
    (buffer-syntactic-context-depth)
    nil)
  :EMACS
;;;  (incf antlr-statistics-inval)
  (setq antlr-slow-context-cache nil))
1089

Juanma Barranquero's avatar
Juanma Barranquero committed
1090
(defunx antlr-syntactic-context ()
1091 1092 1093 1094 1095
  "Return some syntactic context information.
Return `string' if point is within a string, `block-comment' or
`comment' is point is within a comment or the depth within all
parenthesis-syntax delimiters at point otherwise.
WARNING: this may alter `match-data'."
Juanma Barranquero's avatar
Juanma Barranquero committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
  :XEMACS
  (or (buffer-syntactic-context) (buffer-syntactic-context-depth))
  :EMACS
  (let ((orig (point)) diff state
	;; Arg, Emacs' (buffer-modified-tick) changes with font-lock.  Use
	;; hack that `loudly' is bound during font-locking => cache use will
	;; increase from 7% to 99.99% during font-locking.
	(tick (or (boundp antlr-slow-cache-enabling-symbol)
		  (buffer-modified-tick))))
    (if (and (cdr antlr-slow-context-cache)
	     (>= (setq diff (- orig (cadr antlr-slow-context-cache))) 0)
	     (< diff antlr-slow-cache-diff-threshold)
	     (eq (current-buffer) (caar antlr-slow-context-cache))
	     (eq tick (cdar antlr-slow-context-cache)))
	;; (setq antlr-statistics-cache (1+ antlr-statistics-cache) ...)
	(setq state (parse-partial-sexp (cadr antlr-slow-context-cache) orig
					nil nil
					(cddr antlr-slow-context-cache)))
      (if (>= orig antlr-slow-cache-diff-threshold)
	  (beginning-of-defun)
	(goto-char (point-min)))
;;;      (cond ((and diff (< diff 0)) (incf antlr-statistics-full-neg))
;;;	    ((and diff (>= diff 3000)) (incf antlr-statistics-full-diff))
;;;	    (t (incf antlr-statistics-full-other)))
      (setq state (parse-partial-sexp (point) orig)))
    (goto-char orig)
    (if antlr-slow-context-cache
	(setcdr antlr-slow-context-cache (cons orig state))
      (setq antlr-slow-context-cache
	    (cons (cons (current-buffer) tick)
		  (cons orig state))))
    (cond ((nth 3 state) 'string)
	  ((nth 4 state) 'comment)	; block-comment? -- we don't care
	  (t (car state)))))

;;;  (incf (aref antlr-statistics 2))
;;;  (unless (and (eq (current-buffer)
;;;		   (caar antlr-slow-context-cache))
;;;	       (eq (buffer-modified-tick)
;;;		   (cdar antlr-slow-context-cache)))
;;;    (incf (aref antlr-statistics 1))
;;;    (setq antlr-slow-context-cache nil))
;;;  (let* ((orig (point))
;;;	 (base (cadr antlr-slow-context-cache))
;;;	 (curr (cddr antlr-slow-context-cache))
;;;	 (state (cond ((eq orig (car curr)) (cdr curr))
;;;		      ((eq orig (car base)) (cdr base))))
;;;	 diff diff2)
;;;    (unless state
;;;      (incf (aref antlr-statistics 3))
;;;      (when curr
;;;	(if (< (setq diff  (abs (- orig (car curr))))
;;;	       (setq diff2 (abs (- orig (car base)))))
;;;	    (setq state curr)
;;;	  (setq state base
;;;		diff  diff2))
;;;	(if (or (>= (1+ diff) (point)) (>= diff 3000))
;;;	    (setq state nil)))		; start from bod/bob
;;;      (if state
;;;	  (setq state
;;;		(parse-partial-sexp (car state) orig nil nil (cdr state)))
;;;	(if (>= orig 3000) (beginning-of-defun) (goto-char (point-min)))
;;;	(incf (aref antlr-statistics 4))
;;;	(setq cw (list orig (point) base curr))
;;;	(setq state (parse-partial-sexp (point) orig)))
;;;      (goto-char orig)
;;;      (if antlr-slow-context-cache
;;;	  (setcdr (cdr antlr-slow-context-cache) (cons orig state))
;;;	(setq antlr-slow-context-cache
;;;	      (cons (cons (current-buffer) (buffer-modified-tick))
;;;		    (cons (cons orig state) (cons orig state))))))
;;;    (cond ((nth 3 state) 'string)
;;;	  ((nth 4 state) 'comment)	; block-comment? -- we don't care
;;;	  (t (car state)))))

;;;    (beginning-of-defun)
;;;    (let ((state (parse-partial-sexp (point) orig)))
;;;      (goto-char orig)
;;;      (cond ((nth 3 state) 'string)
;;;	    ((nth 4 state) 'comment)	; block-comment? -- we don't care
;;;	    (t (car state))))))
1177 1178 1179


;;;===========================================================================
Juanma Barranquero's avatar
Juanma Barranquero committed
1180
;;;  Miscellaneous functions
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
;;;===========================================================================

(defun antlr-upcase-p (char)
  "Non-nil, if CHAR is an uppercase character (if CHAR was a char)."
  ;; in XEmacs, upcase only works for ASCII
  (or (and (<= ?A char) (<= char ?Z))
      (and (<= ?\300 char) (<= char ?\337)))) ; ?\327 is no letter

(defun antlr-re-search-forward (regexp bound)
  "Search forward from point for regular expression REGEXP.
Set point to the end of the occurrence found, and return point.  Return
Gerd Moellmann's avatar
Gerd Moellmann committed
1192
nil if no occurrence was found.  Do not search within comments, strings
1193 1194 1195 1196 1197 1198 1199
and actions/semantic predicates.  BOUND bounds the search; it is a
buffer position.  See also the functions `match-beginning', `match-end'
and `replace-match'."
  ;; WARNING: Should only be used with `antlr-action-syntax-table'!
  (let ((continue t))
    (while (and (re-search-forward regexp bound 'limit)
		(save-match-data
1200 1201 1202
		  (if (eq (antlr-syntactic-context) 0)
		      (setq continue nil)
		    t))))
1203 1204 1205 1206 1207
    (if continue nil (point))))

(defun antlr-search-forward (string)
  "Search forward from point for STRING.
Set point to the end of the occurrence found, and return point.  Return
Gerd Moellmann's avatar
Gerd Moellmann committed
1208
nil if no occurrence was found.  Do not search within comments, strings
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
and actions/semantic predicates."
  ;; WARNING: Should only be used with `antlr-action-syntax-table'!
  (let ((continue t))
    (while (and (search-forward string nil 'limit)
		(if (eq (antlr-syntactic-context) 0) (setq continue nil) t)))
    (if continue nil (point))))

(defun antlr-search-backward (string)
  "Search backward from point for STRING.
Set point to the beginning of the occurrence found, and return point.
Gerd Moellmann's avatar
Gerd Moellmann committed
1219
Return nil if no occurrence was found.  Do not search within comments,
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
strings and actions/semantic predicates."
  ;; WARNING: Should only be used with `antlr-action-syntax-table'!
  (let ((continue t))
    (while (and (search-backward string nil 'limit)
		(if (eq (antlr-syntactic-context) 0) (setq continue nil) t)))
    (if continue nil (point))))

(defsubst antlr-skip-sexps (count)
  "Skip the next COUNT balanced expressions and the comments after it.
Return position before the comments after the last expression."
Juanma Barranquero's avatar
Juanma Barranquero committed
1230
  (goto-char (or (ignore-errors-x (scan-sexps (point) count)) (point-max)))
1231
  (prog1 (point)
1232
    (antlr-c-forward-sws)))
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258


;;;===========================================================================
;;;  font-lock
;;;===========================================================================

(defun antlr-font-lock-keywords ()
  "Return font-lock keywords for current buffer.
See `antlr-font-lock-additional-keywords', `antlr-language' and
`antlr-font-lock-maximum-decoration'."
  (if (eq antlr-font-lock-maximum-decoration 'none)
      antlr-font-lock-additional-keywords
    (append antlr-font-lock-additional-keywords
	    (eval (let ((major-mode antlr-language)) ; dynamic
			(font-lock-choose-keywords
			 (cdr (assq antlr-language
				    antlr-font-lock-keywords-alist))
			 (if (eq antlr-font-lock-maximum-decoration 'inherit)
			     font-lock-maximum-decoration
			   antlr-font-lock-maximum-decoration)))))))


;;;===========================================================================
;;;  imenu support
;;;===========================================================================

1259 1260 1261 1262 1263 1264 1265
(defun antlr-grammar-tokens ()
  "Return alist for tokens defined in current buffer."
  (save-excursion (antlr-imenu-create-index-function t)))

(defun antlr-imenu-create-index-function (&optional tokenrefs-only)
  "Return imenu index-alist for ANTLR grammar files.
IF TOKENREFS-ONLY is non-nil, just return alist with tokenref names."
1266 1267
  (let ((items nil)
	(classes nil)
Juanma Barranquero's avatar
Juanma Barranquero committed
1268 1269 1270 1271
	(continue t))
    ;; Using `imenu-progress-message' would require imenu for compilation, but
    ;; nobody is missing these messages.  The generic imenu function searches
    ;; backward, which is slower and more likely not to work during editing.
1272
    (antlr-with-syntax-table antlr-action-syntax-table
Juanma Barranquero's avatar
Juanma Barranquero committed
1273 1274 1275 1276
      (antlr-invalidate-context-cache)
      (goto-char (point-min))
      (antlr-skip-file-prelude t)
      (while continue
1277
	(if (looking-at "{") (antlr-skip-sexps 1))
1278 1279 1280 1281 1282 1283 1284
	(if (looking-at antlr-class-header-regexp)
	    (or tokenrefs-only
		(push (cons (match-string 2)
			    (if imenu-use-markers
				(copy-marker (match-beginning 2))
			      (match-beginning 2)))
		      classes))
1285 1286 1287
	  (if (looking-at "p\\(ublic\\|rotected\\|rivate\\)")
	      (antlr-skip-sexps 1))
	  (when (looking-at "\\sw+")
1288 1289 1290 1291 1292 1293 1294
	    (if tokenrefs-only
		(if (antlr-upcase-p (char-after (point)))
		    (push (list (match-string 0)) items))
	      (push (cons (match-string 0)
			  (if imenu-use-markers
			      (copy-marker (match-beginning 0))
			    (match-beginning 0)))
Juanma Barranquero's avatar
Juanma Barranquero committed
1295 1296 1297 1298 1299 1300
		    items))))
	(if (setq continue (antlr-search-forward ";"))
	    (antlr-skip-exception-part t))))
    (if classes
	(cons (cons "Classes" (nreverse classes)) (nreverse items))
      (nreverse items))))
1301