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