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

Juanma Barranquero's avatar
Juanma Barranquero committed
3
;; Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 5
;;
;; Author: Christoph.Wedler@sap.com
6
;; Keywords: languages, ANTLR, code generator
Juanma Barranquero's avatar
Juanma Barranquero committed
7 8
;; Version: (see `antlr-version' below)
;; X-URL: http://antlr-mode.sourceforge.net/
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; 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
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

Juanma Barranquero's avatar
Juanma Barranquero committed
29 30 31 32
;; 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.
33

Juanma Barranquero's avatar
Juanma Barranquero committed
34 35 36 37 38 39 40 41 42 43 44
;; 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:
;;
45 46 47 48 49 50 51 52 53
;;  * 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
54
;;
55 56 57
;;  * 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
58 59 60
;;  * 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'.
61 62 63 64 65 66 67
;;  * 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.
68

69 70
;;; Installation:

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

;; 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")))

80 81 82
;; 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).

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

;;; Code:

(provide 'antlr-mode)
Juanma Barranquero's avatar
Juanma Barranquero committed
88 89 90 91 92 93
(require 'easymenu)

;; General Emacs/XEmacs-compatibility compile-time macros
(eval-when-compile
  (require 'cl)
  (defmacro cond-emacs-xemacs (&rest args)
94
    (cond-emacs-xemacs-macfn
Juanma Barranquero's avatar
Juanma Barranquero committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
     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"))
      (let ((ignore (if (string-match "XEmacs" emacs-version) :EMACS :XEMACS))
	    (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)
    (let ((xemacsp (string-match "XEmacs" emacs-version)) reuses)
      (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)
      (if (and (string-match "XEmacs" emacs-version)
	       (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
164
    (let ((modified (with-no-warnings (gensym "save-buffer-state-x-modified-"))))
Juanma Barranquero's avatar
Juanma Barranquero committed
165 166 167 168 169 170 171 172 173 174 175 176 177
      `(let ((,modified (buffer-modified-p)))
	 (unwind-protect
	     (let ((buffer-undo-list t) (inhibit-read-only t)
		   ,@(unless (string-match "XEmacs" emacs-version)
		       '((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
178
(eval-when-compile			; required and optional libraries
179
  (require 'cc-mode)
Juanma Barranquero's avatar
Juanma Barranquero committed
180 181
  (ignore-errors (require 'font-lock))
  (ignore-errors (require 'compile))
182 183 184
  ;;(ignore-errors (defun c-init-language-vars))) dangerous on Emacs!
  ;;(ignore-errors (defun c-init-c-language-vars))) dangerous on Emacs!
  ;;(ignore-errors (defun c-basic-common-init))   dangerous on Emacs!
185
  (defvar outline-level) (defvar imenu-use-markers)
186 187 188 189 190 191 192 193 194 195
  (defvar imenu-create-index-function))

;; 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)
196 197 198 199 200 201 202 203 204 205 206


;;;;##########################################################################
;;;;  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
207
  :link '(url-link "http://antlr-mode.sourceforge.net/")
208 209
  :prefix "antlr-")

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


;;;===========================================================================
;;;  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
227 228
  '((java-mode "Java" nil "\"Java\"" "Java")
    (c++-mode "C++" "\"Cpp\"" "Cpp"))
229 230
  "List of ANTLR's supported languages.
Each element in this list looks like
231
  \(MAJOR-MODE MODELINE-STRING OPTION-VALUE...)
232 233

MAJOR-MODE, the major mode of the code in the grammar's actions, is the
234 235 236
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
237 238 239 240 241 242 243 244 245 246
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
247
  '(8192 . "language[ \t]*=[ \t]*\\(\"?[A-Z][A-Za-z_]*\"?\\)")
248
  "Used to set a reasonable value for `antlr-language'.
249
Looks like \(LIMIT \. REGEXP).  Search for REGEXP from the beginning of
250 251
the buffer to LIMIT and use the first group in the matched string to set
the language according to `antlr-language-alist'."
252 253 254 255 256 257
  :group 'antlr
  :type '(cons (choice :tag "Limit" (const :tag "No" nil) (integer :value 0))
	       regexp))


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

261 262 263 264 265
(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."
266
  :group 'antlr
267 268
  :type '(choice (const :tag "Completely hidden" nil)
		 (integer :tag "Hidden if longer than" :value 3)))
269

270 271 272 273 274 275 276 277 278 279 280
(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
281 282
  '((antlr-mode nil 4 nil)
    (java-mode "antlr" 4 nil))
283
  "Alist to determine whether to use ANTLR's convention for TABs.
284
Each element looks like \(MAJOR-MODE REGEXP TAB-WIDTH INDENT-TABS-MODE).
285
The first element whose MAJOR-MODE is nil or equal to `major-mode' and
286 287
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
288 289 290 291 292 293 294 295 296
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"))))

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

(defcustom antlr-indent-item-regexp
Juanma Barranquero's avatar
Juanma Barranquero committed
305
  "[]}):;|&]" ; & is local ANTLR extension (SGML's and-connector)
306
  "Regexp matching lines which should be indented by one TAB less.
307 308 309 310 311 312
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
313 314
  '((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\\)\\>"))
315 316 317 318
  "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
319 320 321 322
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
part of an header action, indent the line at column 0 instead according
to the normal rules of `antlr-indent-line'."
323 324 325
  :group 'antlr
  :type '(repeat (cons (function :tag "Major mode") regexp)))

Juanma Barranquero's avatar
Juanma Barranquero committed
326 327 328 329 330 331 332 333 334
;; 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))

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

;;;===========================================================================
;;;  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
a function evaluating to such a table.  The return value is quoted iff
AS-STRING is non-nil and is either t or a symbol which is a member of
`antlr-options-style'.")
573 574


575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
;;;===========================================================================
;;;  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].
596
It looks like \(RULE-SEP GEN-VAR-SPEC COMMAND).
597 598 599 600 601 602 603

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
604
files.  If GEN-VAR-SPEC looks like \(GEN-VAR GEN-VAR-FORMAT GEN-SEP), a
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
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
626
  \(MAJOR-MODE (VOCAB-FILE-FORMAT...) (CLASS-FILE-FORMAT...)).
627 628 629 630 631 632 633 634 635 636 637 638

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.
639
The value looks like \(VOCAB-FILE-FORMAT EXPANDED-GRAMMAR-FORMAT).
640 641 642 643 644 645 646 647 648 649 650 651 652

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.
653
The value looks like \(SUPER-GRAMMAR-FILE-FORMAT SUPER-EVOCAB-FORMAT).
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

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.")


682 683 684 685
;;;===========================================================================
;;;  Menu
;;;===========================================================================

Juanma Barranquero's avatar
Juanma Barranquero committed
686
(defcustom antlr-imenu-name t ; (featurep 'xemacs) ; TODO: Emacs-21 bug?
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
  "*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
704
    (define-key map "\C-c\C-c" 'comment-region)
705
    (define-key map "\C-c\C-v" 'antlr-hide-actions)
706
    (define-key map "\C-c\C-r" 'antlr-run-tool)
707
    (define-key map "\C-c\C-o" 'antlr-insert-option)
708 709 710
    ;; I'm too lazy to define my own:
    (define-key map "\ea" 'c-beginning-of-statement)
    (define-key map "\ee" 'c-end-of-statement)
711 712 713 714 715 716 717 718 719
    ;; 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)
720 721 722
    map)
  "Keymap used in `antlr-mode' buffers.")

723 724 725
(easy-menu-define antlr-mode-menu antlr-mode-map
  "Major mode menu."
  `("Antlr"
Juanma Barranquero's avatar
Juanma Barranquero committed
726 727 728 729
    ,@(if (cond-emacs-xemacs
	   :EMACS (and antlr-options-use-submenus
		       (>= emacs-major-version 21))
	   :XEMACS antlr-options-use-submenus)
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
	  `(("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]))
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784


;;;===========================================================================
;;;  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
785
  \((java-mode \. 2) (c++-mode \. 0))
786 787
Java actions are fontified with level 2 and C++ actions are not
fontified at all."
788 789 790 791 792 793
  :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"
794 795 796
			 :value ((t . t))
			 (cons :tag "Instance"
			       (radio :tag "Mode"
797 798
				      (const :tag "All" t)
				      (symbol :tag "Name"))
799
			       (radio :tag "Decoration"
800 801 802
				      (const :tag "Default" nil)
				      (const :tag "Maximum" t)
				      (integer :tag "Level" 1))))))
803

804 805 806 807 808 809 810
(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.")

811 812
(defvar antlr-font-lock-keywords-alist
  '((java-mode
813
     antlr-no-action-keywords
814 815 816
     java-font-lock-keywords-1 java-font-lock-keywords-2
     java-font-lock-keywords-3)
    (c++-mode
817
     antlr-no-action-keywords
818 819 820 821
     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
822
  \(MAJOR-MODE KEYWORD...)
823 824 825 826 827 828

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'.")

829 830 831 832 833 834
(defvar antlr-font-lock-default-face 'antlr-font-lock-default-face)
(defface antlr-font-lock-default-face nil
  "Face to prevent strings from language dependent highlighting.
Do not change."
  :group 'antlr)

835 836
(defvar antlr-font-lock-keyword-face 'antlr-font-lock-keyword-face)
(defface antlr-font-lock-keyword-face
Juanma Barranquero's avatar
Juanma Barranquero committed
837 838 839
  (cond-emacs-xemacs
   '((((class color) (background light))
      (:foreground "black" :EMACS :weight bold :XEMACS :bold t))))
840 841 842
  "ANTLR keywords."
  :group 'antlr)

843 844
(defvar antlr-font-lock-syntax-face 'antlr-font-lock-keyword-face)
(defface antlr-font-lock-syntax-face
Juanma Barranquero's avatar
Juanma Barranquero committed
845 846 847
  (cond-emacs-xemacs
   '((((class color) (background light))
      (:foreground "black" :EMACS :weight bold :XEMACS :bold t))))
848 849 850
  "ANTLR syntax symbols like :, |, (, ), ...."
  :group 'antlr)

851 852
(defvar antlr-font-lock-ruledef-face 'antlr-font-lock-ruledef-face)
(defface antlr-font-lock-ruledef-face
Juanma Barranquero's avatar
Juanma Barranquero committed
853 854 855
  (cond-emacs-xemacs
   '((((class color) (background light))
      (:foreground "blue" :EMACS :weight bold :XEMACS :bold t))))
856 857 858 859 860
  "ANTLR rule references (definition)."
  :group 'antlr)

(defvar antlr-font-lock-tokendef-face 'antlr-font-lock-tokendef-face)
(defface antlr-font-lock-tokendef-face
Juanma Barranquero's avatar
Juanma Barranquero committed
861 862 863
  (cond-emacs-xemacs
   '((((class color) (background light))
      (:foreground "blue" :EMACS :weight bold :XEMACS :bold t))))
864 865 866 867 868 869 870 871 872 873 874
  "ANTLR token references (definition)."
  :group 'antlr)

(defvar antlr-font-lock-ruleref-face 'antlr-font-lock-ruleref-face)
(defface antlr-font-lock-ruleref-face
  '((((class color) (background light)) (:foreground "blue4")))
  "ANTLR rule references (usage)."
  :group 'antlr)

(defvar antlr-font-lock-tokenref-face 'antlr-font-lock-tokenref-face)
(defface antlr-font-lock-tokenref-face
875
  '((((class color) (background light)) (:foreground "orange4")))
876 877 878 879 880
  "ANTLR token references (usage)."
  :group 'antlr)

(defvar antlr-font-lock-literal-face 'antlr-font-lock-literal-face)
(defface antlr-font-lock-literal-face
Juanma Barranquero's avatar
Juanma Barranquero committed
881 882 883
  (cond-emacs-xemacs
   '((((class color) (background light))
      (:foreground "brown4" :EMACS :weight bold :XEMACS :bold t))))
884 885 886
  "ANTLR special literal tokens.
It is used to highlight strings matched by the first regexp group of
`antlr-font-lock-literal-regexp'."
887 888
  :group 'antlr)

889 890 891 892 893 894 895 896 897 898 899 900 901
(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.")

902
(defvar antlr-font-lock-additional-keywords
Juanma Barranquero's avatar
Juanma Barranquero committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
  (cond-emacs-xemacs
   `((antlr-invalidate-context-cache)
     ("\\$setType[ \t]*(\\([A-Za-z\300-\326\330-\337]\\sw*\\))"
      (1 antlr-font-lock-tokendef-face))
     ("\\$\\sw+" (0 font-lock-keyword-face))
     ;; the tokens are already fontified as string/docstrings:
     (,(lambda (limit)
	 (if antlr-font-lock-literal-regexp
	     (antlr-re-search-forward antlr-font-lock-literal-regexp limit)))
      (1 antlr-font-lock-literal-face t)
      :XEMACS (0 nil))			; XEmacs bug workaround
     (,(lambda (limit)
	 (antlr-re-search-forward antlr-class-header-regexp limit))
      (1 antlr-font-lock-keyword-face)
      (2 antlr-font-lock-ruledef-face)
      (3 antlr-font-lock-keyword-face)
      (4 (if (member (match-string 4) '("Lexer" "Parser" "TreeParser"))
	     'antlr-font-lock-keyword-face
	   'font-lock-type-face)))
     (,(lambda (limit)
	 (antlr-re-search-forward
	  "\\<\\(header\\|options\\|tokens\\|exception\\|catch\\|returns\\)\\>"
	  limit))
926
     (1 antlr-font-lock-keyword-face))
Juanma Barranquero's avatar
Juanma Barranquero committed
927 928 929 930
     (,(lambda (limit)
	 (antlr-re-search-forward
	  "^\\(private\\|public\\|protected\\)\\>[ \t]*\\(\\(\\sw+[ \t]*\\(:\\)?\\)\\)?"
	  limit))
931 932 933
     (1 font-lock-type-face)		; not XEmacs' java level-3 fruit salad
     (3 (if (antlr-upcase-p (char-after (match-beginning 3)))
	    'antlr-font-lock-tokendef-face
934 935
	  'antlr-font-lock-ruledef-face) nil t)
     (4 antlr-font-lock-syntax-face nil t))
Juanma Barranquero's avatar
Juanma Barranquero committed
936 937
     (,(lambda (limit)
	 (antlr-re-search-forward "^\\(\\sw+\\)[ \t]*\\(:\\)?" limit))
938
     (1 (if (antlr-upcase-p (char-after (match-beginning 0)))
939
	    'antlr-font-lock-tokendef-face
940 941
	  'antlr-font-lock-ruledef-face) nil t)
     (2 antlr-font-lock-syntax-face nil t))
Juanma Barranquero's avatar
Juanma Barranquero committed
942 943 944
     (,(lambda (limit)
	 ;; v:ruleref and v:"literal" is allowed...
	 (antlr-re-search-forward "\\(\\sw+\\)[ \t]*\\([=:]\\)?" limit))
945 946 947 948 949 950 951 952
     (1 (if (match-beginning 2)
	    (if (eq (char-after (match-beginning 2)) ?=)
		'antlr-font-lock-default-face
	      'font-lock-variable-name-face)
	  (if (antlr-upcase-p (char-after (match-beginning 1)))
	      'antlr-font-lock-tokenref-face
	    'antlr-font-lock-ruleref-face)))
     (2 antlr-font-lock-default-face nil t))
Juanma Barranquero's avatar
Juanma Barranquero committed
953 954 955
     (,(lambda (limit)
	 (antlr-re-search-forward "[|&:;(~]\\|)\\([*+?]\\|=>\\)?" limit))
     (0 'antlr-font-lock-syntax-face))))
956 957 958 959 960 961
  "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)
962
  "Font-lock defaults used for ANTLR syntax highlighting.
963 964 965 966 967 968 969 970 971 972 973
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'.")

974 975 976 977
(defvar antlr-mode-syntax-table nil
  "Syntax table used in `antlr-mode' buffers.
If non-nil, it will be initialized in `antlr-mode'.")

978 979 980
;; used for "in Java/C++ code" = syntactic-depth>0
(defvar antlr-action-syntax-table nil
  "Syntax table used for ANTLR action parsing.
981 982 983 984
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.")
985 986 987 988 989

(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
990 991 992 993 994 995 996 997 998 999
(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'.")
1000 1001 1002 1003 1004 1005 1006


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


1007

1008
;;;===========================================================================
Juanma Barranquero's avatar
Juanma Barranquero committed
1009
;;;  Syntax functions -- Emacs vs XEmacs dependent, part 1
1010 1011
;;;===========================================================================

1012
;; From help.el (XEmacs-21.1), without `copy-syntax-table'
1013
(defmacro antlr-with-syntax-table (syntab &rest body)
1014
  "Evaluate BODY with the syntax table SYNTAB."
1015 1016
  `(let ((stab (syntax-table)))
     (unwind-protect
1017
	 (progn (set-syntax-table ,syntab) ,@body)
1018 1019 1020 1021
       (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
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
(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)
1054
;; checkdoc-params: (dummies)
Juanma Barranquero's avatar
Juanma Barranquero committed
1055 1056
  "Invalidate context cache for syntactical context information."
  :XEMACS				; XEmacs bug workaround
1057 1058
  (save-excursion
    (set-buffer (get-buffer-create " ANTLR XEmacs bug workaround"))
Juanma Barranquero's avatar
Juanma Barranquero committed
1059 1060 1061 1062 1063
    (buffer-syntactic-context-depth)
    nil)
  :EMACS
;;;  (incf antlr-statistics-inval)
  (setq antlr-slow-context-cache nil))
1064

Juanma Barranquero's avatar
Juanma Barranquero committed
1065
(defunx antlr-syntactic-context ()
1066 1067 1068 1069 1070
  "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
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 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
  :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))))))
1152 1153 1154


;;;===========================================================================
Juanma Barranquero's avatar
Juanma Barranquero committed
1155
;;;  Miscellaneous functions
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
;;;===========================================================================

(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
1167
nil if no occurrence was found.  Do not search within comments, strings
1168 1169 1170 1171 1172 1173 1174
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
1175 1176 1177
		  (if (eq (antlr-syntactic-context) 0)
		      (setq continue nil)
		    t))))
1178 1179 1180 1181 1182
    (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
1183
nil if no occurrence was found.  Do not search within comments, strings
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
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
1194
Return nil if no occurrence was found.  Do not search within comments,
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
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
1205
  (goto-char (or (ignore-errors-x (scan-sexps (point) count)) (point-max)))
1206
  (prog1 (point)
1207
    (antlr-c-forward-sws)))
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233


;;;===========================================================================
;;;  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
;;;===========================================================================

1234 1235 1236 1237 1238 1239 1240
(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."
1241 1242
  (let ((items nil)
	(classes nil)
Juanma Barranquero's avatar
Juanma Barranquero committed
1243 1244 1245 1246
	(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.
1247
    (antlr-with-syntax-table antlr-action-syntax-table
Juanma Barranquero's avatar
Juanma Barranquero committed
1248 1249 1250 1251
      (antlr-invalidate-context-cache)
      (goto-char (point-min))
      (antlr-skip-file-prelude t)
      (while continue
1252
	(if (looking-at "{") (antlr-skip-sexps 1))
1253 1254 1255 1256 1257 1258 1259
	(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))
1260 1261 1262
	  (if (looking-at "p\\(ublic\\|rotected\\|rivate\\)")
	      (antlr-skip-sexps 1))
	  (when (looking-at "\\sw+")
1263 1264 1265 1266 1267 1268 1269
	    (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
1270 1271 1272 1273 1274 1275
		    items))))
	(if (setq continue (antlr-search-forward ";"))
	    (antlr-skip-exception-part t))))
    (if classes
	(cons (cons "Classes" (nreverse classes)) (nreverse items))
      (nreverse items))))
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288


;;;===========================================================================
;;;  Parse grammar files (internal functions)
;;;===========================================================================

(defun antlr-skip-exception-part (skip-comment)
  "Skip exception part of current rule, i.e., everything after `;'.
This also includes the options and tokens part of a grammar class
header.  If SKIP-COMMENT is non-nil, also skip the comment after that
part."
  (let ((pos (point))
	(class nil))
1289
    (antlr-c-forward-sws)
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
    (while (looking-at "options\\>\\|tokens\\>")
      (setq class t)
      (setq pos (antlr-skip-sexps 2)))
    (if class
	;; Problem: an action only belongs to a class def, not a normal rule.
	;; But checking the current rule type is too expensive => only expect
	;; an action if we have found an option or tokens part.
	(if (looking-at "{") (setq pos (antlr-skip-sexps 1)))
      (while (looking-at "exception\\>")
	(setq pos (antlr-skip-sexps 1))
1300 1301
	(when (looking-at "\\[")
	  (setq pos (antlr-skip-sexps 1)))
1302 1303 1304 1305 1306 1307
	(while (looking-at "catch\\>")
	  (setq pos (antlr-skip-sexps 3)))))
    (or skip-comment (goto-char pos))))

(defun antlr-skip-file-prelude (skip-comment)
  "Skip the file prelude: the header and file options.
1308
If SKIP-COMMENT is non-nil, also skip the comment after that part.
1309 1310 1311 1312
Return the start position of the file prelude.

Hack: if SKIP-COMMENT is `header-only' only skip header and return
position before the comment after the header."
1313 1314
  (let* ((pos (point))
	 (pos0 pos))
1315
    (antlr-c-forward-sws)
1316
    (if skip-comment (setq pos0 (point)))
1317 1318
    (while (looking-at "header\\>[ \t]*\\(\"\\)?")
      (setq pos (antlr-skip-sexps (if (match-beginning 1) 3 2))))
1319 1320 1321 1322 1323 1324
    (if (eq skip-comment 'header-only)	; a hack...
	pos
      (when (looking-at "options\\>")
	(setq pos (antlr-skip-sexps 2)))
      (or skip-comment (goto-char pos))
      pos0)))
1325 1326 1327 1328 1329

(defun antlr-next-rule (arg skip-comment)
  "Move forward to next end of rule.  Do it ARG many times.
A grammar class header and the file prelude are also considered as a
rule.  Negative argument ARG means move back to ARGth preceding end of
Gerd Moellmann's avatar
Gerd Moellmann committed
1330
rule.  The behavior is not defined when ARG is zero.  If SKIP-COMMENT
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
is non-nil, move to beginning of the rule."
  ;; WARNING: Should only be used with `antlr-action-syntax-table'!
  ;; PRE: ARG<>0
  (let ((pos (point))
	(beg (point)))
    ;; first look whether point is in exception part
    (if (antlr-search-backward ";")
	(progn
	  (setq beg (point))
	  (forward-char)
	  (antlr-skip-exception-part skip-comment))
      (antlr-skip-file-prelude skip-comment))
    (if (< arg 0)
	(unless (and (< (point) pos) (zerop (incf arg)))
	  ;; if we have moved backward, we already moved one defun backward
	  (goto-char beg)		; rewind (to ";" / point)
	  (while (and arg (<= (incf arg) 0))
	    (if (antlr-search-backward ";")
		(setq beg (point))
	      (when (>= arg -1)
		;; try file prelude:
		(setq pos (antlr-skip-file-prelude skip-comment))
		(if (zerop arg)
		    (if (>= (point) beg)
			(goto-char (if (>= pos beg) (point-min) pos)))
		  (goto-char (if (or (>= (point) beg) (= (point) pos))
				 (point-min) pos))))
	      (setq arg nil)))
	  (when arg			; always found a ";"
	    (forward-char)
	    (antlr-skip-exception-part skip-comment)))
      (if (<= (point) pos)		; moved backward?
	  (goto-char pos)		; rewind
	(decf arg))			; already moved one defun forward
      (unless (zerop arg)
	(while (>= (decf arg) 0)
	  (antlr-search-forward ";"))
	(antlr-skip-exception-part skip-comment)))))

(defun antlr-outside-rule-p ()
  "Non-nil if point is outside a grammar rule.
Move to the beginning of the current rule if point is inside a rule."
  ;; WARNING: Should only be used with `antlr-action-syntax-table'!
  (let ((pos (point)))
    (antlr-next-rule -1 nil)
    (let ((between (or (bobp) (< (point) pos))))
1377
      (antlr-c-forward-sws)
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
      (and between (> (point) pos) (goto-char pos)))))


;;;===========================================================================
;;;  Parse grammar files (commands)
;;;===========================================================================
;; No (interactive "_") in Emacs... use `zmacs-region-stays'.

(defun antlr-inside-rule-p ()
  "Non-nil if point is inside a grammar rule.
A grammar class header and the file prelude are also considered as a
rule."
  (save-excursion
    (antlr-with-syntax-table antlr-action-syntax-table
      (not (antlr-outside-rule-p)))))

Juanma Barranquero's avatar
Juanma Barranquero committed
1394
(defunx antlr-end-of-rule (&optional arg)
1395 1396 1397 1398
  "Move forward to next end of rule.  Do it ARG [default: 1] many times.
A grammar class header and the file prelude are also considered as a
rule.  Negative argument ARG means move back to ARGth preceding end of
rule.  If ARG is zero, run `antlr-end-of-body'."
Juanma Barranquero's avatar
Juanma Barranquero committed
1399
  (interactive "_p")
1400 1401 1402
  (if (zerop arg)
      (antlr-end-of-body)
    (antlr-with-syntax-table antlr-action-syntax-table
Juanma Barranquero's avatar
Juanma Barranquero committed
1403
      (antlr-next-rule arg nil))))
1404

Juanma Barranquero's avatar
Juanma Barranquero committed
1405
(defunx antlr-beginning-of-rule (&optional arg)
1406 1407 1408 1409
  "Move backward to preceding beginning of rule.  Do it ARG many times.
A grammar class header and the file prelude are also considered as a
rule.  Negative argument ARG means move forward to ARGth next beginning
of rule.  If ARG is zero, run `antlr-beginning-of-body'."
Juanma Barranquero's avatar
Juanma Barranquero committed
1410
  (interactive "_p")
1411 1412 1413
  (if (zerop arg)
      (antlr-beginning-of-body)
    (antlr-with-syntax-table antlr-action-syntax-table