Commit | Line | Data |
---|---|---|
a2095e2e CY |
1 | ;;; semantic/grammar.el --- Major mode framework for Semantic grammars |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 2002-2005, 2007-2013 Free Software Foundation, Inc. |
a2095e2e CY |
4 | |
5 | ;; Author: David Ponce <david@dponce.com> | |
6 | ;; Maintainer: David Ponce <david@dponce.com> | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation, either version 3 of the License, or | |
13 | ;; (at your option) any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
22 | ||
23 | ;;; Commentary: | |
24 | ;; | |
25 | ;; Major mode framework for editing Semantic's input grammar files. | |
26 | ||
27 | ;;; History: | |
28 | ;; | |
29 | ||
30 | ;;; Code: | |
31 | ||
32 | (require 'semantic) | |
62a81506 | 33 | (require 'semantic/wisent) |
a2095e2e CY |
34 | (require 'semantic/ctxt) |
35 | (require 'semantic/format) | |
f9b697dd GM |
36 | ;; FIXME this is a generated file, but we need to load this file to |
37 | ;; generate it! | |
a2095e2e CY |
38 | (require 'semantic/grammar-wy) |
39 | (require 'semantic/idle) | |
3f2a848d | 40 | (require 'help-fns) |
62a81506 | 41 | |
a2095e2e CY |
42 | (declare-function semantic-momentary-highlight-tag "semantic/decorate") |
43 | (declare-function semantic-analyze-context "semantic/analyze") | |
44 | (declare-function semantic-analyze-tags-of-class-list | |
45 | "semantic/analyze/complete") | |
46 | ||
a2095e2e CY |
47 | (eval-when-compile |
48 | (require 'eldoc) | |
49 | (require 'semantic/edit) | |
62a81506 CY |
50 | (require 'semantic/find) |
51 | (require 'semantic/db)) | |
a2095e2e | 52 | |
f20def1f | 53 | (declare-function semantic-grammar-wy--install-parser "semantic/grammar-wy") |
e8cc7880 | 54 | |
a2095e2e CY |
55 | \f |
56 | ;;;; | |
57 | ;;;; Set up lexer | |
58 | ;;;; | |
59 | ||
60 | (defconst semantic-grammar-lex-c-char-re "'\\s\\?.'" | |
61 | "Regexp matching C-like character literals.") | |
62 | ||
63 | ;; Most of the analyzers are auto-generated from the grammar, but the | |
64 | ;; following which need special handling code. | |
65 | ;; | |
66 | (define-lex-regex-analyzer semantic-grammar-lex-prologue | |
67 | "Detect and create a prologue token." | |
68 | "\\<%{" | |
69 | ;; Zing to the end of this brace block. | |
70 | (semantic-lex-push-token | |
71 | (semantic-lex-token | |
72 | 'PROLOGUE (point) | |
73 | (save-excursion | |
74 | (semantic-lex-unterminated-syntax-protection 'PROLOGUE | |
75 | (forward-char) | |
76 | (forward-sexp 1) | |
77 | (point)))))) | |
78 | ||
79 | (defsubst semantic-grammar-epilogue-start () | |
80 | "Return the start position of the grammar epilogue." | |
81 | (save-excursion | |
82 | (goto-char (point-min)) | |
83 | (if (re-search-forward "^\\s-*\\<%%\\>\\s-*$" nil t 2) | |
84 | (match-beginning 0) | |
85 | (1+ (point-max))))) | |
86 | ||
87 | (define-lex-regex-analyzer semantic-grammar-lex-epilogue | |
88 | "Detect and create an epilogue or percent-percent token." | |
89 | "\\<%%\\>" | |
90 | (let ((start (match-beginning 0)) | |
91 | (end (match-end 0)) | |
92 | (class 'PERCENT_PERCENT)) | |
93 | (when (>= start (semantic-grammar-epilogue-start)) | |
94 | (setq class 'EPILOGUE | |
95 | end (point-max))) | |
96 | (semantic-lex-push-token | |
97 | (semantic-lex-token class start end)))) | |
98 | ||
99 | (define-lex semantic-grammar-lexer | |
100 | "Lexical analyzer that handles Semantic grammar buffers. | |
101 | It ignores whitespaces, newlines and comments." | |
102 | semantic-lex-ignore-newline | |
103 | semantic-lex-ignore-whitespace | |
104 | ;; Must detect prologue/epilogue before other symbols/keywords! | |
105 | semantic-grammar-lex-prologue | |
106 | semantic-grammar-lex-epilogue | |
107 | semantic-grammar-wy--<keyword>-keyword-analyzer | |
108 | semantic-grammar-wy--<symbol>-regexp-analyzer | |
109 | semantic-grammar-wy--<char>-regexp-analyzer | |
110 | semantic-grammar-wy--<string>-sexp-analyzer | |
111 | ;; Must detect comments after strings because `comment-start-skip' | |
112 | ;; regexp match semicolons inside strings! | |
113 | semantic-lex-ignore-comments | |
114 | ;; Must detect prefixed list before punctuation because prefix chars | |
e4920bc9 | 115 | ;; are also punctuation! |
a2095e2e | 116 | semantic-grammar-wy--<qlist>-sexp-analyzer |
e4920bc9 PE |
117 | ;; Must detect punctuation after comments because the semicolon can |
118 | ;; be punctuation or a comment start! | |
a2095e2e CY |
119 | semantic-grammar-wy--<punctuation>-string-analyzer |
120 | semantic-grammar-wy--<block>-block-analyzer | |
121 | semantic-grammar-wy--<sexp>-sexp-analyzer) | |
122 | ||
123 | ;;; Test the lexer | |
124 | ;; | |
125 | (defun semantic-grammar-lex-buffer () | |
126 | "Run `semantic-grammar-lex' on current buffer." | |
127 | (interactive) | |
128 | (semantic-lex-init) | |
129 | (setq semantic-lex-analyzer 'semantic-grammar-lexer) | |
130 | (let ((token-stream | |
131 | (semantic-lex (point-min) (point-max)))) | |
132 | (with-current-buffer (get-buffer-create "*semantic-grammar-lex*") | |
133 | (erase-buffer) | |
134 | (pp token-stream (current-buffer)) | |
135 | (goto-char (point-min)) | |
136 | (pop-to-buffer (current-buffer))))) | |
137 | \f | |
138 | ;;;; | |
139 | ;;;; Semantic action expansion | |
140 | ;;;; | |
141 | ||
142 | (defun semantic-grammar-ASSOC (&rest args) | |
143 | "Return expansion of built-in ASSOC expression. | |
144 | ARGS are ASSOC's key value list." | |
145 | (let ((key t)) | |
146 | `(semantic-tag-make-assoc-list | |
147 | ,@(mapcar #'(lambda (i) | |
148 | (prog1 | |
149 | (if key | |
150 | (list 'quote i) | |
151 | i) | |
152 | (setq key (not key)))) | |
153 | args)))) | |
154 | ||
155 | (defsubst semantic-grammar-quote-p (sym) | |
156 | "Return non-nil if SYM is bound to the `quote' function." | |
157 | (condition-case nil | |
158 | (eq (indirect-function sym) | |
159 | (indirect-function 'quote)) | |
160 | (error nil))) | |
161 | ||
162 | (defsubst semantic-grammar-backquote-p (sym) | |
163 | "Return non-nil if SYM is bound to the `backquote' function." | |
164 | (condition-case nil | |
165 | (eq (indirect-function sym) | |
166 | (indirect-function 'backquote)) | |
167 | (error nil))) | |
168 | \f | |
169 | ;;;; | |
170 | ;;;; API to access grammar tags | |
171 | ;;;; | |
172 | ||
173 | (define-mode-local-override semantic-tag-components | |
174 | semantic-grammar-mode (tag) | |
175 | "Return the children of tag TAG." | |
176 | (semantic-tag-get-attribute tag :children)) | |
177 | ||
178 | (defun semantic-grammar-first-tag-name (class) | |
179 | "Return the name of the first tag of class CLASS found. | |
180 | Warn if other tags of class CLASS exist." | |
181 | (let* ((tags (semantic-find-tags-by-class | |
182 | class (current-buffer)))) | |
183 | (if tags | |
184 | (prog1 | |
185 | (semantic-tag-name (car tags)) | |
186 | (if (cdr tags) | |
187 | (message "*** Ignore all but first declared %s" | |
188 | class)))))) | |
189 | ||
190 | (defun semantic-grammar-tag-symbols (class) | |
191 | "Return the list of symbols defined in tags of class CLASS. | |
192 | That is tag names plus names defined in tag attribute `:rest'." | |
193 | (let* ((tags (semantic-find-tags-by-class | |
194 | class (current-buffer)))) | |
195 | (apply 'append | |
196 | (mapcar | |
197 | #'(lambda (tag) | |
198 | (mapcar | |
199 | 'intern | |
200 | (cons (semantic-tag-name tag) | |
201 | (semantic-tag-get-attribute tag :rest)))) | |
202 | tags)))) | |
203 | ||
204 | (defsubst semantic-grammar-item-text (item) | |
205 | "Return the readable string form of ITEM." | |
206 | (if (string-match semantic-grammar-lex-c-char-re item) | |
207 | (concat "?" (substring item 1 -1)) | |
208 | item)) | |
209 | ||
210 | (defsubst semantic-grammar-item-value (item) | |
211 | "Return symbol or character value of ITEM string." | |
212 | (if (string-match semantic-grammar-lex-c-char-re item) | |
213 | (let ((c (read (concat "?" (substring item 1 -1))))) | |
214 | (if (featurep 'xemacs) | |
215 | ;; Handle characters as integers in XEmacs like in GNU Emacs. | |
216 | (char-int c) | |
217 | c)) | |
218 | (intern item))) | |
219 | ||
220 | (defun semantic-grammar-prologue () | |
221 | "Return grammar prologue code as a string value." | |
222 | (let ((tag (semantic-find-first-tag-by-name | |
223 | "prologue" | |
224 | (semantic-find-tags-by-class 'code (current-buffer))))) | |
225 | (if tag | |
226 | (save-excursion | |
227 | (concat | |
228 | (buffer-substring | |
229 | (progn | |
230 | (goto-char (semantic-tag-start tag)) | |
231 | (skip-chars-forward "%{\r\n\t ") | |
232 | (point)) | |
233 | (progn | |
234 | (goto-char (semantic-tag-end tag)) | |
235 | (skip-chars-backward "\r\n\t %}") | |
236 | (point))) | |
237 | "\n")) | |
238 | ""))) | |
239 | ||
240 | (defun semantic-grammar-epilogue () | |
241 | "Return grammar epilogue code as a string value." | |
242 | (let ((tag (semantic-find-first-tag-by-name | |
243 | "epilogue" | |
244 | (semantic-find-tags-by-class 'code (current-buffer))))) | |
245 | (if tag | |
246 | (save-excursion | |
247 | (concat | |
248 | (buffer-substring | |
249 | (progn | |
250 | (goto-char (semantic-tag-start tag)) | |
251 | (skip-chars-forward "%\r\n\t ") | |
252 | (point)) | |
253 | (progn | |
254 | (goto-char (semantic-tag-end tag)) | |
255 | (skip-chars-backward "\r\n\t") | |
256 | ;; If a grammar footer is found, skip it. | |
257 | (re-search-backward "^;;;\\s-+\\S-+\\s-+ends here" | |
9b026d9f | 258 | (point-at-bol) t) |
a2095e2e CY |
259 | (skip-chars-backward "\r\n\t") |
260 | (point))) | |
261 | "\n")) | |
262 | ""))) | |
263 | ||
264 | (defsubst semantic-grammar-buffer-file (&optional buffer) | |
265 | "Return name of file sans directory BUFFER is visiting. | |
266 | No argument or nil as argument means use the current buffer." | |
267 | (file-name-nondirectory (buffer-file-name buffer))) | |
268 | ||
269 | (defun semantic-grammar-package () | |
270 | "Return the %package value as a string. | |
271 | If there is no %package statement in the grammar, return a default | |
272 | package name derived from the grammar file name. For example, the | |
273 | default package name for the grammar file foo.wy is foo-wy, and for | |
274 | foo.by it is foo-by." | |
275 | (or (semantic-grammar-first-tag-name 'package) | |
276 | (let* ((file (semantic-grammar-buffer-file)) | |
277 | (ext (file-name-extension file)) | |
278 | (i (string-match (format "\\([.]\\)%s\\'" ext) file))) | |
279 | (concat (substring file 0 i) "-" ext)))) | |
280 | ||
281 | (defsubst semantic-grammar-languagemode () | |
282 | "Return the %languagemode value as a list of symbols or nil." | |
283 | (semantic-grammar-tag-symbols 'languagemode)) | |
284 | ||
285 | (defsubst semantic-grammar-start () | |
286 | "Return the %start value as a list of symbols or nil." | |
287 | (semantic-grammar-tag-symbols 'start)) | |
288 | ||
289 | (defsubst semantic-grammar-scopestart () | |
290 | "Return the %scopestart value as a symbol or nil." | |
291 | (intern (or (semantic-grammar-first-tag-name 'scopestart) "nil"))) | |
292 | ||
293 | (defsubst semantic-grammar-quotemode () | |
294 | "Return the %quotemode value as a symbol or nil." | |
295 | (intern (or (semantic-grammar-first-tag-name 'quotemode) "nil"))) | |
296 | ||
297 | (defsubst semantic-grammar-keywords () | |
298 | "Return the language keywords. | |
299 | That is an alist of (VALUE . TOKEN) where VALUE is the string value of | |
300 | the keyword and TOKEN is the terminal symbol identifying the keyword." | |
301 | (mapcar | |
302 | #'(lambda (key) | |
303 | (cons (semantic-tag-get-attribute key :value) | |
304 | (intern (semantic-tag-name key)))) | |
305 | (semantic-find-tags-by-class 'keyword (current-buffer)))) | |
306 | ||
307 | (defun semantic-grammar-keyword-properties (keywords) | |
308 | "Return the list of KEYWORDS properties." | |
309 | (let ((puts (semantic-find-tags-by-class | |
310 | 'put (current-buffer))) | |
311 | put keys key plist assoc pkey pval props) | |
312 | (while puts | |
313 | (setq put (car puts) | |
314 | puts (cdr puts) | |
315 | keys (mapcar | |
316 | 'intern | |
317 | (cons (semantic-tag-name put) | |
318 | (semantic-tag-get-attribute put :rest)))) | |
319 | (while keys | |
320 | (setq key (car keys) | |
321 | keys (cdr keys) | |
322 | assoc (rassq key keywords)) | |
323 | (if (null assoc) | |
324 | nil ;;(message "*** %%put to undefined keyword %s ignored" key) | |
325 | (setq key (car assoc) | |
326 | plist (semantic-tag-get-attribute put :value)) | |
327 | (while plist | |
328 | (setq pkey (intern (caar plist)) | |
329 | pval (read (cdar plist)) | |
330 | props (cons (list key pkey pval) props) | |
331 | plist (cdr plist)))))) | |
332 | props)) | |
333 | ||
334 | (defun semantic-grammar-tokens () | |
335 | "Return defined lexical tokens. | |
336 | That is an alist (TYPE . DEFS) where type is a %token <type> symbol | |
337 | and DEFS is an alist of (TOKEN . VALUE). TOKEN is the terminal symbol | |
338 | identifying the token and VALUE is the string value of the token or | |
339 | nil." | |
340 | (let (tags alist assoc tag type term names value) | |
341 | ||
342 | ;; Check for <type> in %left, %right & %nonassoc declarations | |
343 | (setq tags (semantic-find-tags-by-class | |
344 | 'assoc (current-buffer))) | |
345 | (while tags | |
346 | (setq tag (car tags) | |
347 | tags (cdr tags)) | |
348 | (when (setq type (semantic-tag-type tag)) | |
349 | (setq names (semantic-tag-get-attribute tag :value) | |
350 | assoc (assoc type alist)) | |
351 | (or assoc (setq assoc (list type) | |
352 | alist (cons assoc alist))) | |
353 | (while names | |
354 | (setq term (car names) | |
355 | names (cdr names)) | |
356 | (or (string-match semantic-grammar-lex-c-char-re term) | |
357 | (setcdr assoc (cons (list (intern term)) | |
358 | (cdr assoc))))))) | |
359 | ||
360 | ;; Then process %token declarations so they can override any | |
361 | ;; previous specifications | |
362 | (setq tags (semantic-find-tags-by-class | |
363 | 'token (current-buffer))) | |
364 | (while tags | |
365 | (setq tag (car tags) | |
366 | tags (cdr tags)) | |
367 | (setq names (cons (semantic-tag-name tag) | |
368 | (semantic-tag-get-attribute tag :rest)) | |
369 | type (or (semantic-tag-type tag) "<no-type>") | |
370 | value (semantic-tag-get-attribute tag :value) | |
371 | assoc (assoc type alist)) | |
372 | (or assoc (setq assoc (list type) | |
373 | alist (cons assoc alist))) | |
374 | (while names | |
375 | (setq term (intern (car names)) | |
376 | names (cdr names)) | |
377 | (setcdr assoc (cons (cons term value) (cdr assoc))))) | |
378 | alist)) | |
379 | ||
380 | (defun semantic-grammar-token-%type-properties (&optional props) | |
381 | "Return properties set by %type statements. | |
382 | This declare a new type if necessary. | |
383 | If optional argument PROPS is non-nil, it is an existing list of | |
384 | properties where to add new properties." | |
385 | (let (type) | |
386 | (dolist (tag (semantic-find-tags-by-class 'type (current-buffer))) | |
387 | (setq type (semantic-tag-name tag)) | |
388 | ;; Indicate to auto-generate the analyzer for this type | |
389 | (push (list type :declared t) props) | |
390 | (dolist (e (semantic-tag-get-attribute tag :value)) | |
391 | (push (list type (intern (car e)) (read (or (cdr e) "nil"))) | |
392 | props))) | |
393 | props)) | |
394 | ||
395 | (defun semantic-grammar-token-%put-properties (tokens) | |
396 | "For types found in TOKENS, return properties set by %put statements." | |
397 | (let (found props) | |
398 | (dolist (put (semantic-find-tags-by-class 'put (current-buffer))) | |
399 | (dolist (type (cons (semantic-tag-name put) | |
400 | (semantic-tag-get-attribute put :rest))) | |
401 | (setq found (assoc type tokens)) | |
402 | (if (null found) | |
403 | nil ;; %put <type> ignored, no token defined | |
404 | (setq type (car found)) | |
405 | (dolist (e (semantic-tag-get-attribute put :value)) | |
406 | (push (list type (intern (car e)) (read (or (cdr e) "nil"))) | |
407 | props))))) | |
408 | props)) | |
409 | ||
410 | (defsubst semantic-grammar-token-properties (tokens) | |
411 | "Return properties of declared types. | |
412 | Types are explicitly declared by %type statements. Types found in | |
413 | TOKENS are those declared implicitly by %token statements. | |
414 | Properties can be set by %put and %type statements. | |
415 | Properties set by %type statements take precedence over those set by | |
416 | %put statements." | |
417 | (let ((props (semantic-grammar-token-%put-properties tokens))) | |
418 | (semantic-grammar-token-%type-properties props))) | |
419 | ||
420 | (defun semantic-grammar-use-macros () | |
421 | "Return macro definitions from %use-macros statements. | |
422 | Also load the specified macro libraries." | |
423 | (let (lib defs) | |
424 | (dolist (tag (semantic-find-tags-by-class 'macro (current-buffer))) | |
425 | (setq lib (intern (semantic-tag-type tag))) | |
426 | (condition-case nil | |
427 | ;;(load lib) ;; Be sure to use the latest macro library. | |
428 | (require lib) | |
429 | (error nil)) | |
430 | (dolist (mac (semantic-tag-get-attribute tag :value)) | |
431 | (push (cons (intern mac) | |
432 | (intern (format "%s-%s" lib mac))) | |
433 | defs))) | |
434 | (nreverse defs))) | |
435 | ||
436 | (defvar semantic-grammar-macros nil | |
437 | "List of associations (MACRO-NAME . EXPANDER).") | |
438 | (make-variable-buffer-local 'semantic-grammar-macros) | |
439 | ||
440 | (defun semantic-grammar-macros () | |
441 | "Build and return the alist of defined macros." | |
442 | (append | |
443 | ;; Definitions found in tags. | |
444 | (semantic-grammar-use-macros) | |
445 | ;; Other pre-installed definitions. | |
446 | semantic-grammar-macros)) | |
447 | \f | |
448 | ;;;; | |
449 | ;;;; Overloaded functions that build parser data. | |
450 | ;;;; | |
451 | ||
452 | ;;; Keyword table builder | |
453 | ;; | |
454 | (defun semantic-grammar-keywordtable-builder-default () | |
455 | "Return the default value of the keyword table." | |
456 | (let ((keywords (semantic-grammar-keywords))) | |
457 | `(semantic-lex-make-keyword-table | |
458 | ',keywords | |
459 | ',(semantic-grammar-keyword-properties keywords)))) | |
460 | ||
461 | (define-overloadable-function semantic-grammar-keywordtable-builder () | |
5a5fa834 | 462 | "Return the keyword table value.") |
a2095e2e CY |
463 | |
464 | ;;; Token table builder | |
465 | ;; | |
466 | (defun semantic-grammar-tokentable-builder-default () | |
467 | "Return the default value of the table of lexical tokens." | |
468 | (let ((tokens (semantic-grammar-tokens))) | |
469 | `(semantic-lex-make-type-table | |
470 | ',tokens | |
471 | ',(semantic-grammar-token-properties tokens)))) | |
472 | ||
473 | (define-overloadable-function semantic-grammar-tokentable-builder () | |
474 | "Return the value of the table of lexical tokens.") | |
475 | ||
476 | ;;; Parser table builder | |
477 | ;; | |
478 | (defun semantic-grammar-parsetable-builder-default () | |
479 | "Return the default value of the parse table." | |
480 | (error "`semantic-grammar-parsetable-builder' not defined")) | |
481 | ||
482 | (define-overloadable-function semantic-grammar-parsetable-builder () | |
483 | "Return the parser table value.") | |
484 | ||
485 | ;;; Parser setup code builder | |
486 | ;; | |
487 | (defun semantic-grammar-setupcode-builder-default () | |
488 | "Return the default value of the setup code form." | |
489 | (error "`semantic-grammar-setupcode-builder' not defined")) | |
490 | ||
491 | (define-overloadable-function semantic-grammar-setupcode-builder () | |
492 | "Return the parser setup code form.") | |
493 | \f | |
494 | ;;;; | |
495 | ;;;; Lisp code generation | |
496 | ;;;; | |
497 | (defvar semantic--grammar-input-buffer nil) | |
498 | (defvar semantic--grammar-output-buffer nil) | |
62a81506 CY |
499 | (defvar semantic--grammar-package nil) |
500 | (defvar semantic--grammar-provide nil) | |
a2095e2e CY |
501 | |
502 | (defsubst semantic-grammar-keywordtable () | |
503 | "Return the variable name of the keyword table." | |
62a81506 | 504 | (concat semantic--grammar-package |
a2095e2e CY |
505 | "--keyword-table")) |
506 | ||
507 | (defsubst semantic-grammar-tokentable () | |
508 | "Return the variable name of the token table." | |
62a81506 | 509 | (concat semantic--grammar-package |
a2095e2e CY |
510 | "--token-table")) |
511 | ||
512 | (defsubst semantic-grammar-parsetable () | |
513 | "Return the variable name of the parse table." | |
62a81506 | 514 | (concat semantic--grammar-package |
a2095e2e CY |
515 | "--parse-table")) |
516 | ||
517 | (defsubst semantic-grammar-setupfunction () | |
518 | "Return the name of the parser setup function." | |
62a81506 | 519 | (concat semantic--grammar-package |
a2095e2e CY |
520 | "--install-parser")) |
521 | ||
522 | (defmacro semantic-grammar-as-string (object) | |
523 | "Return OBJECT as a string value." | |
524 | `(if (stringp ,object) | |
525 | ,object | |
526 | ;;(require 'pp) | |
527 | (pp-to-string ,object))) | |
528 | ||
529 | (defun semantic-grammar-insert-defconst (name value docstring) | |
530 | "Insert declaration of constant NAME with VALUE and DOCSTRING." | |
531 | (let ((start (point))) | |
532 | (insert (format "(defconst %s\n%s%S)\n\n" name value docstring)) | |
533 | (save-excursion | |
534 | (goto-char start) | |
535 | (indent-sexp)))) | |
536 | ||
537 | (defun semantic-grammar-insert-defun (name body docstring) | |
538 | "Insert declaration of function NAME with BODY and DOCSTRING." | |
539 | (let ((start (point))) | |
540 | (insert (format "(defun %s ()\n%S\n%s)\n\n" name docstring body)) | |
541 | (save-excursion | |
542 | (goto-char start) | |
543 | (indent-sexp)))) | |
544 | ||
545 | (defun semantic-grammar-insert-define (define) | |
546 | "Insert the declaration specified by DEFINE expression. | |
547 | Typically a DEFINE expression should look like this: | |
548 | ||
549 | \(define-thing name docstring expression1 ...)" | |
550 | ;;(require 'pp) | |
551 | (let ((start (point))) | |
552 | (insert (format "(%S %S" (car define) (nth 1 define))) | |
553 | (dolist (item (nthcdr 2 define)) | |
554 | (insert "\n") | |
555 | (delete-blank-lines) | |
556 | (pp item (current-buffer))) | |
557 | (insert ")\n\n") | |
558 | (save-excursion | |
559 | (goto-char start) | |
560 | (indent-sexp)))) | |
561 | ||
562 | (defconst semantic-grammar-header-template | |
563 | '("\ | |
564 | ;;; " file " --- Generated parser support file | |
565 | ||
566 | " copy " | |
567 | ||
568 | ;; Author: " user-full-name " <" user-mail-address "> | |
569 | ;; Created: " date " | |
570 | ;; Keywords: syntax | |
571 | ;; X-RCS: " vcid " | |
572 | ||
573 | ;; This file is not part of GNU Emacs. | |
d40a7570 | 574 | |
a2095e2e CY |
575 | ;; This program is free software; you can redistribute it and/or |
576 | ;; modify it under the terms of the GNU General Public License as | |
d40a7570 GM |
577 | ;; published by the Free Software Foundation, either version 3 of |
578 | ;; the License, or (at your option) any later version. | |
579 | ||
a2095e2e CY |
580 | ;; This software is distributed in the hope that it will be useful, |
581 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
582 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
583 | ;; General Public License for more details. | |
584 | ;; | |
585 | ;; You should have received a copy of the GNU General Public License | |
d40a7570 | 586 | ;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
a2095e2e CY |
587 | |
588 | ;;; Commentary: | |
589 | ;; | |
590 | ;; PLEASE DO NOT MANUALLY EDIT THIS FILE! It is automatically | |
591 | ;; generated from the grammar file " gram ". | |
592 | ||
593 | ;;; History: | |
594 | ;; | |
595 | ||
596 | ;;; Code: | |
62a81506 CY |
597 | |
598 | (require 'semantic/lex) | |
599 | (eval-when-compile (require 'semantic/bovine)) | |
a2095e2e CY |
600 | ") |
601 | "Generated header template. | |
602 | The symbols in the template are local variables in | |
603 | `semantic-grammar-header'") | |
604 | ||
605 | (defconst semantic-grammar-footer-template | |
606 | '("\ | |
607 | ||
608 | \(provide '" libr ") | |
609 | ||
f9b697dd GM |
610 | ;; Local Variables: |
611 | ;; version-control: never | |
612 | ;; no-update-autoloads: t | |
613 | ;; End: | |
614 | ||
a2095e2e CY |
615 | ;;; " file " ends here |
616 | ") | |
617 | "Generated footer template. | |
618 | The symbols in the list are local variables in | |
619 | `semantic-grammar-footer'.") | |
620 | ||
621 | (defun semantic-grammar-copyright-line () | |
622 | "Return the grammar copyright line, or nil if not found." | |
623 | (save-excursion | |
624 | (goto-char (point-min)) | |
625 | (when (re-search-forward "^;;+[ \t]+Copyright (C) .*$" | |
626 | ;; Search only in the four top lines | |
627 | (save-excursion (forward-line 4) (point)) | |
628 | t) | |
629 | (match-string 0)))) | |
630 | ||
631 | (defun semantic-grammar-header () | |
632 | "Return text of a generated standard header." | |
633 | (let ((file (semantic-grammar-buffer-file | |
634 | semantic--grammar-output-buffer)) | |
635 | (gram (semantic-grammar-buffer-file)) | |
636 | (date (format-time-string "%Y-%m-%d %T%z")) | |
637 | (vcid (concat "$" "Id" "$")) ;; Avoid expansion | |
638 | ;; Try to get the copyright from the input grammar, or | |
639 | ;; generate a new one if not found. | |
640 | (copy (or (semantic-grammar-copyright-line) | |
641 | (concat (format-time-string ";; Copyright (C) %Y ") | |
642 | user-full-name))) | |
643 | (out "")) | |
644 | (dolist (S semantic-grammar-header-template) | |
645 | (cond ((stringp S) | |
646 | (setq out (concat out S))) | |
647 | ((symbolp S) | |
648 | (setq out (concat out (symbol-value S)))))) | |
649 | out)) | |
650 | ||
651 | (defun semantic-grammar-footer () | |
652 | "Return text of a generated standard footer." | |
653 | (let* ((file (semantic-grammar-buffer-file | |
654 | semantic--grammar-output-buffer)) | |
62a81506 CY |
655 | (libr (or semantic--grammar-provide |
656 | semantic--grammar-package)) | |
a2095e2e CY |
657 | (out "")) |
658 | (dolist (S semantic-grammar-footer-template) | |
659 | (cond ((stringp S) | |
660 | (setq out (concat out S))) | |
661 | ((symbolp S) | |
662 | (setq out (concat out (symbol-value S)))))) | |
663 | out)) | |
664 | ||
665 | (defun semantic-grammar-token-data () | |
666 | "Return the string value of the table of lexical tokens." | |
667 | (semantic-grammar-as-string | |
668 | (semantic-grammar-tokentable-builder))) | |
669 | ||
670 | (defun semantic-grammar-keyword-data () | |
671 | "Return the string value of the table of keywords." | |
672 | (semantic-grammar-as-string | |
673 | (semantic-grammar-keywordtable-builder))) | |
674 | ||
675 | (defun semantic-grammar-parser-data () | |
676 | "Return the parser table as a string value." | |
677 | (semantic-grammar-as-string | |
678 | (semantic-grammar-parsetable-builder))) | |
679 | ||
680 | (defun semantic-grammar-setup-data () | |
681 | "Return the parser setup code form as a string value." | |
682 | (semantic-grammar-as-string | |
683 | (semantic-grammar-setupcode-builder))) | |
684 | \f | |
685 | ;;; Generation of lexical analyzers. | |
686 | ;; | |
687 | (defvar semantic-grammar--lex-block-specs) | |
688 | ||
689 | (defsubst semantic-grammar--lex-delim-spec (block-spec) | |
690 | "Return delimiters specification from BLOCK-SPEC." | |
691 | (condition-case nil | |
692 | (let* ((standard-input (cdr block-spec)) | |
693 | (delim-spec (read))) | |
694 | (if (and (consp delim-spec) | |
695 | (car delim-spec) (symbolp (car delim-spec)) | |
696 | (cadr delim-spec) (symbolp (cadr delim-spec))) | |
697 | delim-spec | |
547776f9 | 698 | (error "Invalid delimiter"))) |
a2095e2e CY |
699 | (error |
700 | (error "Invalid delimiters specification %s in block token %s" | |
701 | (cdr block-spec) (car block-spec))))) | |
702 | ||
703 | (defun semantic-grammar--lex-block-specs () | |
704 | "Compute lexical block specifications for the current buffer. | |
705 | Block definitions are read from the current table of lexical types." | |
706 | (cond | |
707 | ;; Block specifications have been parsed and are invalid. | |
708 | ((eq semantic-grammar--lex-block-specs 'error) | |
709 | nil | |
710 | ) | |
711 | ;; Parse block specifications. | |
712 | ((null semantic-grammar--lex-block-specs) | |
713 | (condition-case err | |
714 | (let* ((blocks (cdr (semantic-lex-type-value "block" t))) | |
715 | (open-delims (cdr (semantic-lex-type-value "open-paren" t))) | |
716 | (close-delims (cdr (semantic-lex-type-value "close-paren" t))) | |
717 | olist clist block-spec delim-spec open-spec close-spec) | |
718 | (dolist (block-spec blocks) | |
719 | (setq delim-spec (semantic-grammar--lex-delim-spec block-spec) | |
720 | open-spec (assq (car delim-spec) open-delims) | |
721 | close-spec (assq (cadr delim-spec) close-delims)) | |
722 | (or open-spec | |
723 | (error "Missing open-paren token %s required by block %s" | |
724 | (car delim-spec) (car block-spec))) | |
725 | (or close-spec | |
726 | (error "Missing close-paren token %s required by block %s" | |
727 | (cdr delim-spec) (car block-spec))) | |
728 | ;; build alist ((OPEN-DELIM OPEN-SYM BLOCK-SYM) ...) | |
729 | (push (list (cdr open-spec) (car open-spec) (car block-spec)) | |
730 | olist) | |
731 | ;; build alist ((CLOSE-DELIM CLOSE-SYM) ...) | |
732 | (push (list (cdr close-spec) (car close-spec)) | |
733 | clist)) | |
734 | (setq semantic-grammar--lex-block-specs (cons olist clist))) | |
735 | (error | |
736 | (setq semantic-grammar--lex-block-specs 'error) | |
737 | (message "%s" (error-message-string err)) | |
738 | nil)) | |
739 | ) | |
740 | ;; Block specifications already parsed. | |
741 | (t | |
742 | semantic-grammar--lex-block-specs))) | |
743 | ||
744 | (defsubst semantic-grammar-quoted-form (exp) | |
745 | "Return a quoted form of EXP if it isn't a self evaluating form." | |
746 | (if (and (not (null exp)) | |
747 | (or (listp exp) (symbolp exp))) | |
748 | (list 'quote exp) | |
749 | exp)) | |
750 | ||
751 | (defun semantic-grammar-insert-defanalyzer (type) | |
752 | "Insert declaration of the lexical analyzer defined with TYPE." | |
753 | (let* ((type-name (symbol-name type)) | |
754 | (type-value (symbol-value type)) | |
755 | (syntax (get type 'syntax)) | |
756 | (declared (get type :declared)) | |
757 | spec mtype prefix name doc) | |
758 | ;; Generate an analyzer if the corresponding type has been | |
759 | ;; explicitly declared in a %type statement, and if at least the | |
760 | ;; syntax property has been provided. | |
761 | (when (and declared syntax) | |
62a81506 | 762 | (setq prefix semantic--grammar-package |
a2095e2e CY |
763 | mtype (or (get type 'matchdatatype) 'regexp) |
764 | name (intern (format "%s--<%s>-%s-analyzer" prefix type mtype)) | |
765 | doc (format "%s analyzer for <%s> tokens." mtype type)) | |
766 | (cond | |
767 | ;; Regexp match analyzer | |
768 | ((eq mtype 'regexp) | |
769 | (semantic-grammar-insert-define | |
770 | `(define-lex-regex-type-analyzer ,name | |
771 | ,doc ,syntax | |
772 | ,(semantic-grammar-quoted-form (cdr type-value)) | |
773 | ',(or (car type-value) (intern type-name)))) | |
774 | ) | |
775 | ;; String compare analyzer | |
776 | ((eq mtype 'string) | |
777 | (semantic-grammar-insert-define | |
778 | `(define-lex-string-type-analyzer ,name | |
779 | ,doc ,syntax | |
780 | ,(semantic-grammar-quoted-form (cdr type-value)) | |
781 | ',(or (car type-value) (intern type-name)))) | |
782 | ) | |
783 | ;; Block analyzer | |
784 | ((and (eq mtype 'block) | |
785 | (setq spec (semantic-grammar--lex-block-specs))) | |
786 | (semantic-grammar-insert-define | |
787 | `(define-lex-block-type-analyzer ,name | |
788 | ,doc ,syntax | |
789 | ,(semantic-grammar-quoted-form spec))) | |
790 | ) | |
791 | ;; Sexp analyzer | |
792 | ((eq mtype 'sexp) | |
793 | (semantic-grammar-insert-define | |
794 | `(define-lex-sexp-type-analyzer ,name | |
795 | ,doc ,syntax | |
796 | ',(or (car type-value) (intern type-name)))) | |
797 | ) | |
798 | ;; keyword analyzer | |
799 | ((eq mtype 'keyword) | |
800 | (semantic-grammar-insert-define | |
801 | `(define-lex-keyword-type-analyzer ,name | |
802 | ,doc ,syntax)) | |
803 | ) | |
804 | )) | |
805 | )) | |
806 | ||
807 | (defun semantic-grammar-insert-defanalyzers () | |
808 | "Insert declarations of lexical analyzers." | |
809 | (let (tokens props) | |
810 | (with-current-buffer semantic--grammar-input-buffer | |
811 | (setq tokens (semantic-grammar-tokens) | |
812 | props (semantic-grammar-token-properties tokens))) | |
a2095e2e CY |
813 | (let ((semantic-lex-types-obarray |
814 | (semantic-lex-make-type-table tokens props)) | |
815 | semantic-grammar--lex-block-specs) | |
816 | (mapatoms 'semantic-grammar-insert-defanalyzer | |
817 | semantic-lex-types-obarray)))) | |
818 | \f | |
819 | ;;; Generation of the grammar support file. | |
820 | ;; | |
821 | (defcustom semantic-grammar-file-regexp "\\.[wb]y$" | |
822 | "Regexp which matches grammar source files." | |
823 | :group 'semantic | |
824 | :type 'regexp) | |
825 | ||
826 | (defsubst semantic-grammar-noninteractive () | |
827 | "Return non-nil if running without interactive terminal." | |
828 | (if (featurep 'xemacs) | |
829 | (noninteractive) | |
830 | noninteractive)) | |
831 | ||
f9b697dd | 832 | (defun semantic-grammar-create-package (&optional force uptodate) |
a2095e2e | 833 | "Create package Lisp code from grammar in current buffer. |
f9b697dd GM |
834 | If the Lisp code seems up to date, do nothing (if UPTODATE |
835 | is non-nil, return nil in such cases). | |
a2095e2e CY |
836 | If optional argument FORCE is non-nil, unconditionally re-generate the |
837 | Lisp code." | |
838 | (interactive "P") | |
3f2a848d DE |
839 | (unless (semantic-active-p) |
840 | (error "You have to activate semantic-mode to create a package.")) | |
a2095e2e CY |
841 | (setq force (or force current-prefix-arg)) |
842 | (semantic-fetch-tags) | |
843 | (let* ( | |
844 | ;; Values of the following local variables are obtained from | |
845 | ;; the grammar parsed tree in current buffer, that is before | |
846 | ;; switching to the output file. | |
62a81506 CY |
847 | (semantic--grammar-package (semantic-grammar-package)) |
848 | (semantic--grammar-provide (semantic-grammar-first-tag-name 'provide)) | |
849 | (output (concat (or semantic--grammar-provide | |
850 | semantic--grammar-package) ".el")) | |
a2095e2e | 851 | (semantic--grammar-input-buffer (current-buffer)) |
62a81506 CY |
852 | (semantic--grammar-output-buffer |
853 | (find-file-noselect | |
854 | (file-name-nondirectory output))) | |
a2095e2e CY |
855 | (header (semantic-grammar-header)) |
856 | (prologue (semantic-grammar-prologue)) | |
857 | (epilogue (semantic-grammar-epilogue)) | |
858 | (footer (semantic-grammar-footer)) | |
859 | ) | |
860 | (if (and (not force) | |
861 | (not (buffer-modified-p)) | |
862 | (file-newer-than-file-p | |
863 | (buffer-file-name semantic--grammar-output-buffer) | |
864 | (buffer-file-name semantic--grammar-input-buffer))) | |
f9b697dd GM |
865 | (progn |
866 | (message "Package `%s' is up to date." semantic--grammar-package) | |
867 | ;; It would be better if this were always the case, IMO, | |
868 | ;; but the (unspecified) return value of this function is | |
869 | ;; assumed to be non-nil in some places, it seems. | |
870 | (if uptodate (setq output nil))) | |
a2095e2e CY |
871 | ;; Create the package |
872 | (set-buffer semantic--grammar-output-buffer) | |
873 | ;; Use Unix EOLs, so that the file is portable to all platforms. | |
874 | (setq buffer-file-coding-system 'raw-text-unix) | |
875 | (erase-buffer) | |
1b3b87df | 876 | (unless (derived-mode-p 'emacs-lisp-mode) |
a2095e2e CY |
877 | (emacs-lisp-mode)) |
878 | ||
879 | ;;;; Header + Prologue | |
880 | ||
881 | (insert header | |
882 | "\f\n;;; Prologue\n;;\n" | |
883 | prologue | |
884 | ) | |
885 | ;; Evaluate the prologue now, because it might provide definition | |
886 | ;; of grammar macro expanders. | |
887 | (eval-region (point-min) (point)) | |
888 | ||
889 | (save-excursion | |
890 | ||
891 | ;;;; Declarations | |
892 | ||
893 | (insert "\f\n;;; Declarations\n;;\n") | |
894 | ||
895 | ;; `eval-defun' is not necessary to reset `defconst' values. | |
896 | (semantic-grammar-insert-defconst | |
897 | (semantic-grammar-keywordtable) | |
898 | (with-current-buffer semantic--grammar-input-buffer | |
899 | (semantic-grammar-keyword-data)) | |
900 | "Table of language keywords.") | |
901 | ||
902 | (semantic-grammar-insert-defconst | |
903 | (semantic-grammar-tokentable) | |
904 | (with-current-buffer semantic--grammar-input-buffer | |
905 | (semantic-grammar-token-data)) | |
906 | "Table of lexical tokens.") | |
907 | ||
908 | (semantic-grammar-insert-defconst | |
909 | (semantic-grammar-parsetable) | |
910 | (with-current-buffer semantic--grammar-input-buffer | |
911 | (semantic-grammar-parser-data)) | |
912 | "Parser table.") | |
913 | ||
914 | (semantic-grammar-insert-defun | |
915 | (semantic-grammar-setupfunction) | |
916 | (with-current-buffer semantic--grammar-input-buffer | |
917 | (semantic-grammar-setup-data)) | |
918 | "Setup the Semantic Parser.") | |
919 | ||
920 | ;;;; Analyzers | |
921 | (insert "\f\n;;; Analyzers\n;;\n") | |
922 | ||
923 | (semantic-grammar-insert-defanalyzers) | |
924 | ||
925 | ;;;; Epilogue & Footer | |
926 | ||
927 | (insert "\f\n;;; Epilogue\n;;\n" | |
928 | epilogue | |
929 | footer | |
930 | ) | |
931 | ||
932 | ) | |
933 | ||
934 | (save-buffer 16) | |
935 | ||
936 | ;; If running in batch mode, there is nothing more to do. | |
937 | ;; Save the generated file and quit. | |
938 | (if (semantic-grammar-noninteractive) | |
939 | (let ((version-control t) | |
940 | (delete-old-versions t) | |
941 | (make-backup-files t) | |
942 | (vc-make-backup-files t)) | |
943 | (kill-buffer (current-buffer))) | |
944 | ;; If running interactively, eval declarations and epilogue | |
945 | ;; code, then pop to the buffer visiting the generated file. | |
946 | (eval-region (point) (point-max)) | |
dd9af436 CY |
947 | ;; Loop over the defvars and eval them explicitly to force |
948 | ;; them to be evaluated and ready to use. | |
949 | (goto-char (point-min)) | |
950 | (while (re-search-forward "(defvar " nil t) | |
951 | (eval-defun nil)) | |
952 | ;; Move cursor to a logical spot in the generated code. | |
a2095e2e CY |
953 | (goto-char (point-min)) |
954 | (pop-to-buffer (current-buffer)) | |
955 | ;; The generated code has been evaluated and updated into | |
956 | ;; memory. Now find all buffers that match the major modes we | |
957 | ;; have created this language for, and force them to call our | |
958 | ;; setup function again, refreshing all semantic data, and | |
959 | ;; enabling them to work with the new code just created. | |
960 | ;;;; FIXME? | |
961 | ;; At this point, I don't know any user's defined setup code :-( | |
962 | ;; At least, what I can do for now, is to run the generated | |
963 | ;; parser-install function. | |
964 | (semantic-map-mode-buffers | |
965 | (semantic-grammar-setupfunction) | |
966 | (semantic-grammar-languagemode))) | |
967 | ) | |
968 | ;; Return the name of the generated package file. | |
969 | output)) | |
970 | ||
971 | (defun semantic-grammar-recreate-package () | |
9bf6c65c | 972 | "Unconditionally create Lisp code from grammar in current buffer. |
a2095e2e CY |
973 | Like \\[universal-argument] \\[semantic-grammar-create-package]." |
974 | (interactive) | |
975 | (semantic-grammar-create-package t)) | |
976 | ||
977 | (defun semantic-grammar-batch-build-one-package (file) | |
978 | "Build a Lisp package from the grammar in FILE. | |
979 | That is, generate Lisp code from FILE, and `byte-compile' it. | |
980 | Return non-nil if there were no errors, nil if errors." | |
981 | ;; We need this require so that we can find `byte-compile-dest-file'. | |
982 | (require 'bytecomp) | |
983 | (unless (auto-save-file-name-p file) | |
984 | ;; Create the package | |
985 | (let ((packagename | |
986 | (condition-case err | |
987 | (with-current-buffer (find-file-noselect file) | |
62a81506 CY |
988 | (let ((semantic-new-buffer-setup-functions nil) |
989 | (vc-handled-backends nil)) | |
990 | (setq semanticdb-new-database-class 'semanticdb-project-database) | |
991 | (semantic-mode 1) | |
992 | (semantic-grammar-create-package))) | |
a2095e2e CY |
993 | (error |
994 | (message "%s" (error-message-string err)) | |
995 | nil)))) | |
996 | (when packagename | |
997 | ;; Only byte compile if out of date | |
998 | (if (file-newer-than-file-p | |
999 | packagename (byte-compile-dest-file packagename)) | |
1000 | (let (;; Some complex grammar table expressions need a few | |
1001 | ;; more resources than the default. | |
1002 | (max-specpdl-size (max 3000 max-specpdl-size)) | |
1003 | (max-lisp-eval-depth (max 1000 max-lisp-eval-depth)) | |
1004 | ) | |
1005 | ;; byte compile the resultant file | |
1006 | (byte-compile-file packagename)) | |
1007 | t))))) | |
1008 | ||
1009 | (defun semantic-grammar-batch-build-packages () | |
1010 | "Build Lisp packages from grammar files on the command line. | |
1011 | That is, run `semantic-grammar-batch-build-one-package' for each file. | |
1012 | Each file is processed even if an error occurred previously. | |
1013 | Must be used from the command line, with `-batch'. | |
1014 | For example, to process grammar files in current directory, invoke: | |
1015 | ||
1016 | \"emacs -batch -f semantic-grammar-batch-build-packages .\". | |
1017 | ||
1018 | See also the variable `semantic-grammar-file-regexp'." | |
1019 | (or (semantic-grammar-noninteractive) | |
1020 | (error "\ | |
1021 | `semantic-grammar-batch-build-packages' must be used with -batch" | |
1022 | )) | |
1023 | (let ((status 0) | |
1024 | ;; Remove vc from find-file-hook. It causes bad stuff to | |
1025 | ;; happen in Emacs 20. | |
1026 | (find-file-hook (delete 'vc-find-file-hook find-file-hook))) | |
a2095e2e CY |
1027 | (dolist (arg command-line-args-left) |
1028 | (unless (and arg (file-exists-p arg)) | |
1029 | (error "Argument %s is not a valid file name" arg)) | |
1030 | (setq arg (expand-file-name arg)) | |
1031 | (if (file-directory-p arg) | |
1032 | ;; Directory as argument | |
1033 | (dolist (src (condition-case nil | |
1034 | (directory-files | |
1035 | arg nil semantic-grammar-file-regexp) | |
1036 | (error | |
1037 | (error "Unable to read directory files")))) | |
1038 | (or (semantic-grammar-batch-build-one-package | |
1039 | (expand-file-name src arg)) | |
1040 | (setq status 1))) | |
1041 | ;; Specific file argument | |
1042 | (or (semantic-grammar-batch-build-one-package arg) | |
1043 | (setq status 1)))) | |
1044 | (kill-emacs status) | |
1045 | )) | |
1046 | \f | |
1047 | ;;;; | |
1048 | ;;;; Macros highlighting | |
1049 | ;;;; | |
1050 | ||
1051 | (defvar semantic--grammar-macros-regexp-1 nil) | |
1052 | (make-variable-buffer-local 'semantic--grammar-macros-regexp-1) | |
1053 | ||
1054 | (defun semantic--grammar-macros-regexp-1 () | |
1055 | "Return font-lock keyword regexp for pre-installed macro names." | |
1056 | (and semantic-grammar-macros | |
1057 | (not semantic--grammar-macros-regexp-1) | |
1058 | (condition-case nil | |
1059 | (setq semantic--grammar-macros-regexp-1 | |
1060 | (concat "(\\s-*" | |
1061 | (regexp-opt | |
1062 | (mapcar #'(lambda (e) (symbol-name (car e))) | |
1063 | semantic-grammar-macros) | |
1064 | t) | |
1065 | "\\>")) | |
1066 | (error nil))) | |
1067 | semantic--grammar-macros-regexp-1) | |
1068 | ||
1069 | (defconst semantic--grammar-macdecl-re | |
1070 | "\\<%use-macros\\>[ \t\r\n]+\\(\\sw\\|\\s_\\)+[ \t\r\n]+{" | |
1071 | "Regexp that matches a macro declaration statement.") | |
1072 | ||
1073 | (defvar semantic--grammar-macros-regexp-2 nil) | |
1074 | (make-variable-buffer-local 'semantic--grammar-macros-regexp-2) | |
1075 | ||
1076 | (defun semantic--grammar-clear-macros-regexp-2 (&rest ignore) | |
1077 | "Clear the cached regexp that match macros local in this grammar. | |
1078 | IGNORE arguments. | |
1079 | Added to `before-change-functions' hooks to be run before each text | |
1080 | change." | |
1081 | (setq semantic--grammar-macros-regexp-2 nil)) | |
1082 | ||
1083 | (defun semantic--grammar-macros-regexp-2 () | |
1084 | "Return the regexp that match macros local in this grammar." | |
1085 | (unless semantic--grammar-macros-regexp-2 | |
1086 | (let (macs) | |
1087 | (save-excursion | |
1088 | (goto-char (point-min)) | |
1089 | (while (re-search-forward semantic--grammar-macdecl-re nil t) | |
1090 | (condition-case nil | |
1091 | (setq macs (nconc macs | |
1092 | (split-string | |
1093 | (buffer-substring-no-properties | |
1094 | (point) | |
1095 | (progn | |
1096 | (backward-char) | |
1097 | (forward-list 1) | |
1098 | (down-list -1) | |
1099 | (point)))))) | |
1100 | (error nil))) | |
1101 | (when macs | |
1102 | (setq semantic--grammar-macros-regexp-2 | |
1103 | (concat "(\\s-*" (regexp-opt macs t) "\\>")))))) | |
1104 | semantic--grammar-macros-regexp-2) | |
1105 | ||
1106 | (defun semantic--grammar-macros-matcher (end) | |
1107 | "Search for a grammar macro name to highlight. | |
1108 | END is the limit of the search." | |
1109 | (let ((regexp (semantic--grammar-macros-regexp-1))) | |
1110 | (or (and regexp (re-search-forward regexp end t)) | |
1111 | (and (setq regexp (semantic--grammar-macros-regexp-2)) | |
1112 | (re-search-forward regexp end t))))) | |
1113 | \f | |
1114 | ;;;; | |
1115 | ;;;; Define major mode | |
1116 | ;;;; | |
1117 | ||
1b3b87df SM |
1118 | (define-obsolete-variable-alias 'semantic-grammar-syntax-table |
1119 | 'semantic-grammar-mode-syntax-table "24.1") | |
1120 | (defvar semantic-grammar-mode-syntax-table | |
a2095e2e CY |
1121 | (let ((table (make-syntax-table (standard-syntax-table)))) |
1122 | (modify-syntax-entry ?\: "." table) ;; COLON | |
1123 | (modify-syntax-entry ?\> "." table) ;; GT | |
1124 | (modify-syntax-entry ?\< "." table) ;; LT | |
1125 | (modify-syntax-entry ?\| "." table) ;; OR | |
1126 | (modify-syntax-entry ?\; ". 12" table) ;; SEMI, Comment start ;; | |
1127 | (modify-syntax-entry ?\n ">" table) ;; Comment end | |
1128 | (modify-syntax-entry ?\" "\"" table) ;; String | |
1129 | (modify-syntax-entry ?\% "w" table) ;; Word | |
1130 | (modify-syntax-entry ?\- "_" table) ;; Symbol | |
1131 | (modify-syntax-entry ?\. "_" table) ;; Symbol | |
1132 | (modify-syntax-entry ?\\ "\\" table) ;; Quote | |
1133 | (modify-syntax-entry ?\` "'" table) ;; Prefix ` (backquote) | |
1134 | (modify-syntax-entry ?\' "'" table) ;; Prefix ' (quote) | |
1135 | (modify-syntax-entry ?\, "'" table) ;; Prefix , (comma) | |
1136 | (modify-syntax-entry ?\# "'" table) ;; Prefix # (sharp) | |
1137 | table) | |
1138 | "Syntax table used in a Semantic grammar buffers.") | |
1139 | ||
1140 | (defvar semantic-grammar-mode-hook nil | |
1141 | "Hook run when starting Semantic grammar mode.") | |
1142 | ||
1143 | (defvar semantic-grammar-mode-keywords-1 | |
1144 | `(("\\(\\<%%\\>\\|\\<%[{}]\\)" | |
1145 | 0 font-lock-reference-face) | |
1146 | ("\\(%\\)\\(\\(\\sw\\|\\s_\\)+\\)" | |
1147 | (1 font-lock-reference-face) | |
1148 | (2 font-lock-keyword-face)) | |
1149 | ("\\<error\\>" | |
1150 | 0 (unless (semantic-grammar-in-lisp-p) 'bold)) | |
1151 | ("^\\(\\(\\sw\\|\\s_\\)+\\)[ \n\r\t]*:" | |
1152 | 1 font-lock-function-name-face) | |
1153 | (semantic--grammar-macros-matcher | |
1154 | 1 ,(if (boundp 'font-lock-builtin-face) | |
1155 | 'font-lock-builtin-face | |
1156 | 'font-lock-preprocessor-face)) | |
1157 | ("\\$\\(\\sw\\|\\s_\\)*" | |
1158 | 0 font-lock-variable-name-face) | |
1159 | ("<\\(\\(\\sw\\|\\s_\\)+\\)>" | |
1160 | 1 font-lock-type-face) | |
1161 | (,semantic-grammar-lex-c-char-re | |
1162 | 0 ,(if (boundp 'font-lock-constant-face) | |
1163 | 'font-lock-constant-face | |
1164 | 'font-lock-string-face) t) | |
1165 | ;; Must highlight :keyword here, because ':' is a punctuation in | |
1166 | ;; grammar mode! | |
1167 | ("[\r\n\t ]+:\\sw+\\>" | |
1168 | 0 font-lock-builtin-face) | |
b90caf50 CY |
1169 | ;; ;; Append the Semantic keywords |
1170 | ;; ,@semantic-fw-font-lock-keywords | |
a2095e2e CY |
1171 | ) |
1172 | "Font Lock keywords used to highlight Semantic grammar buffers.") | |
1173 | ||
1174 | (defvar semantic-grammar-mode-keywords-2 | |
1175 | (append semantic-grammar-mode-keywords-1 | |
2388ae79 GM |
1176 | (if (boundp 'lisp-font-lock-keywords-1) |
1177 | lisp-font-lock-keywords-1 | |
1178 | lisp-el-font-lock-keywords-1)) | |
a2095e2e CY |
1179 | "Font Lock keywords used to highlight Semantic grammar buffers.") |
1180 | ||
1181 | (defvar semantic-grammar-mode-keywords-3 | |
1182 | (append semantic-grammar-mode-keywords-1 | |
2388ae79 GM |
1183 | (if (boundp 'lisp-font-lock-keywords-2) |
1184 | lisp-font-lock-keywords-2 | |
1185 | lisp-el-font-lock-keywords-2)) | |
a2095e2e CY |
1186 | "Font Lock keywords used to highlight Semantic grammar buffers.") |
1187 | ||
1188 | (defvar semantic-grammar-mode-keywords | |
1189 | semantic-grammar-mode-keywords-1 | |
1190 | "Font Lock keywords used to highlight Semantic grammar buffers.") | |
1191 | ||
1b3b87df SM |
1192 | (define-obsolete-variable-alias 'semantic-grammar-map |
1193 | 'semantic-grammar-mode-map "24.1") | |
1194 | (defvar semantic-grammar-mode-map | |
a2095e2e CY |
1195 | (let ((km (make-sparse-keymap))) |
1196 | ||
1197 | (define-key km "|" 'semantic-grammar-electric-punctuation) | |
1198 | (define-key km ";" 'semantic-grammar-electric-punctuation) | |
1199 | (define-key km "%" 'semantic-grammar-electric-punctuation) | |
1200 | (define-key km "(" 'semantic-grammar-electric-punctuation) | |
1201 | (define-key km ")" 'semantic-grammar-electric-punctuation) | |
1202 | (define-key km ":" 'semantic-grammar-electric-punctuation) | |
1203 | ||
1204 | (define-key km "\t" 'semantic-grammar-indent) | |
1205 | (define-key km "\M-\t" 'semantic-grammar-complete) | |
1206 | (define-key km "\C-c\C-c" 'semantic-grammar-create-package) | |
1207 | (define-key km "\C-cm" 'semantic-grammar-find-macro-expander) | |
1208 | (define-key km "\C-cik" 'semantic-grammar-insert-keyword) | |
1209 | ;; (define-key km "\C-cc" 'semantic-grammar-generate-and-load) | |
1210 | ;; (define-key km "\C-cr" 'semantic-grammar-generate-one-rule) | |
1211 | ||
1212 | km) | |
1213 | "Keymap used in `semantic-grammar-mode'.") | |
1214 | ||
1215 | (defvar semantic-grammar-menu | |
1216 | '("Grammar" | |
1217 | ["Indent Line" semantic-grammar-indent] | |
1218 | ["Complete Symbol" semantic-grammar-complete] | |
1219 | ["Find Macro" semantic-grammar-find-macro-expander] | |
1220 | "--" | |
1221 | ["Insert %keyword" semantic-grammar-insert-keyword] | |
1222 | "--" | |
1223 | ["Update Lisp Package" semantic-grammar-create-package] | |
1224 | ["Recreate Lisp Package" semantic-grammar-recreate-package] | |
1225 | ) | |
1226 | "Common semantic grammar menu.") | |
1227 | ||
1228 | (defun semantic-grammar-setup-menu-emacs (symbol mode-menu) | |
1229 | "Setup a GNU Emacs grammar menu in variable SYMBOL. | |
1230 | MODE-MENU is an optional specific menu whose items are appended to the | |
1231 | common grammar menu." | |
1232 | (let ((items (make-symbol "items"))) | |
1233 | `(unless (boundp ',symbol) | |
1234 | (easy-menu-define ,symbol (current-local-map) | |
1235 | "Grammar Menu" semantic-grammar-menu) | |
1236 | (let ((,items (cdr ,mode-menu))) | |
1237 | (when ,items | |
1238 | (easy-menu-add-item ,symbol nil "--") | |
1239 | (while ,items | |
1240 | (easy-menu-add-item ,symbol nil (car ,items)) | |
1241 | (setq ,items (cdr ,items)))))) | |
1242 | )) | |
1243 | ||
1244 | (defun semantic-grammar-setup-menu-xemacs (symbol mode-menu) | |
1245 | "Setup an XEmacs grammar menu in variable SYMBOL. | |
1246 | MODE-MENU is an optional specific menu whose items are appended to the | |
1247 | common grammar menu." | |
1248 | (let ((items (make-symbol "items")) | |
1249 | (path (make-symbol "path"))) | |
1250 | `(progn | |
1251 | (unless (boundp ',symbol) | |
1252 | (easy-menu-define ,symbol nil | |
1253 | "Grammar Menu" (copy-sequence semantic-grammar-menu))) | |
1254 | (easy-menu-add ,symbol) | |
1255 | (let ((,items (cdr ,mode-menu)) | |
1256 | (,path (list (car ,symbol)))) | |
1257 | (when ,items | |
1258 | (easy-menu-add-item nil ,path "--") | |
1259 | (while ,items | |
1260 | (easy-menu-add-item nil ,path (car ,items)) | |
1261 | (setq ,items (cdr ,items)))))) | |
1262 | )) | |
1263 | ||
1264 | (defmacro semantic-grammar-setup-menu (&optional mode-menu) | |
1265 | "Setup a mode local grammar menu. | |
1266 | MODE-MENU is an optional specific menu whose items are appended to the | |
1267 | common grammar menu." | |
1268 | (let ((menu (intern (format "%s-menu" major-mode)))) | |
1269 | (if (featurep 'xemacs) | |
1270 | (semantic-grammar-setup-menu-xemacs menu mode-menu) | |
1271 | (semantic-grammar-setup-menu-emacs menu mode-menu)))) | |
1272 | ||
1273 | (defsubst semantic-grammar-in-lisp-p () | |
1274 | "Return non-nil if point is in Lisp code." | |
1275 | (or (>= (point) (semantic-grammar-epilogue-start)) | |
1276 | (condition-case nil | |
1277 | (save-excursion | |
1278 | (up-list -1) | |
1279 | t) | |
1280 | (error nil)))) | |
1281 | ||
1282 | (defun semantic-grammar-edits-new-change-hook-fcn (overlay) | |
1283 | "Function set into `semantic-edits-new-change-hook'. | |
1284 | Argument OVERLAY is the overlay created to mark the change. | |
1285 | When OVERLAY marks a change in the scope of a nonterminal tag extend | |
1286 | the change bounds to encompass the whole nonterminal tag." | |
1287 | (let ((outer (car (semantic-find-tag-by-overlay-in-region | |
1288 | (semantic-edits-os overlay) | |
1289 | (semantic-edits-oe overlay))))) | |
1290 | (if (semantic-tag-of-class-p outer 'nonterminal) | |
1291 | (semantic-overlay-move overlay | |
1292 | (semantic-tag-start outer) | |
1293 | (semantic-tag-end outer))))) | |
1294 | ||
1b3b87df SM |
1295 | (define-derived-mode semantic-grammar-mode |
1296 | fundamental-mode "Semantic Grammar Framework" | |
a2095e2e CY |
1297 | "Initialize a buffer for editing Semantic grammars. |
1298 | ||
1b3b87df | 1299 | \\{semantic-grammar-mode-map}" |
a2095e2e CY |
1300 | (set (make-local-variable 'parse-sexp-ignore-comments) t) |
1301 | (set (make-local-variable 'comment-start) ";;") | |
1302 | ;; Look within the line for a ; following an even number of backslashes | |
1303 | ;; after either a non-backslash or the line beginning. | |
1304 | (set (make-local-variable 'comment-start-skip) | |
1305 | "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *") | |
a2095e2e CY |
1306 | (set (make-local-variable 'indent-line-function) |
1307 | 'semantic-grammar-indent) | |
1308 | (set (make-local-variable 'fill-paragraph-function) | |
1309 | 'lisp-fill-paragraph) | |
1310 | (set (make-local-variable 'font-lock-multiline) | |
1311 | 'undecided) | |
1312 | (set (make-local-variable 'font-lock-defaults) | |
1313 | '((semantic-grammar-mode-keywords | |
1314 | semantic-grammar-mode-keywords-1 | |
1315 | semantic-grammar-mode-keywords-2 | |
1316 | semantic-grammar-mode-keywords-3) | |
1317 | nil ;; perform string/comment fontification | |
1318 | nil ;; keywords are case sensitive. | |
333f9019 | 1319 | ;; This puts _ & - as a word constituent, |
a2095e2e CY |
1320 | ;; simplifying our keywords significantly |
1321 | ((?_ . "w") (?- . "w")))) | |
1322 | ;; Setup Semantic to parse grammar | |
1323 | (semantic-grammar-wy--install-parser) | |
1324 | (setq semantic-lex-comment-regex ";;" | |
1325 | semantic-lex-analyzer 'semantic-grammar-lexer | |
1326 | semantic-type-relation-separator-character '(":") | |
1327 | semantic-symbol->name-assoc-list | |
1328 | '( | |
1329 | (code . "Setup Code") | |
1330 | (keyword . "Keyword") | |
1331 | (token . "Token") | |
1332 | (nonterminal . "Nonterminal") | |
1333 | (rule . "Rule") | |
1334 | )) | |
1335 | (set (make-local-variable 'semantic-format-face-alist) | |
1336 | '( | |
1337 | (code . default) | |
1338 | (keyword . font-lock-keyword-face) | |
1339 | (token . font-lock-type-face) | |
1340 | (nonterminal . font-lock-function-name-face) | |
1341 | (rule . default) | |
1342 | )) | |
1343 | (set (make-local-variable 'semantic-stickyfunc-sticky-classes) | |
1344 | '(nonterminal)) | |
1345 | ;; Before each change, clear the cached regexp used to highlight | |
1346 | ;; macros local in this grammar. | |
1347 | (semantic-make-local-hook 'before-change-functions) | |
1348 | (add-hook 'before-change-functions | |
1349 | 'semantic--grammar-clear-macros-regexp-2 nil t) | |
1350 | ;; Handle safe re-parse of grammar rules. | |
237b5653 SM |
1351 | (semantic-make-local-hook 'semantic-edits-new-change-functions) |
1352 | (add-hook 'semantic-edits-new-change-functions | |
a2095e2e | 1353 | 'semantic-grammar-edits-new-change-hook-fcn |
1b3b87df | 1354 | nil t)) |
a2095e2e CY |
1355 | \f |
1356 | ;;;; | |
1357 | ;;;; Useful commands | |
1358 | ;;;; | |
1359 | ||
1360 | (defvar semantic-grammar-skip-quoted-syntax-table | |
1b3b87df | 1361 | (let ((st (copy-syntax-table semantic-grammar-mode-syntax-table))) |
a2095e2e CY |
1362 | (modify-syntax-entry ?\' "$" st) |
1363 | st) | |
1364 | "Syntax table to skip a whole quoted expression in grammar code. | |
1365 | Consider quote as a \"paired delimiter\", so `forward-sexp' will skip | |
1366 | whole quoted expression.") | |
1367 | ||
1368 | (defsubst semantic-grammar-backward-item () | |
1369 | "Move point to beginning of the previous grammar item." | |
1370 | (forward-comment (- (point-max))) | |
1371 | (if (zerop (skip-syntax-backward ".")) | |
1372 | (if (eq (char-before) ?\') | |
1373 | (with-syntax-table | |
1374 | ;; Can't be Lisp code here! Temporarily consider quote | |
1375 | ;; as a "paired delimiter", so `forward-sexp' can skip | |
1376 | ;; the whole quoted expression. | |
1377 | semantic-grammar-skip-quoted-syntax-table | |
1378 | (forward-sexp -1)) | |
1379 | (forward-sexp -1)))) | |
1380 | ||
1381 | (defun semantic-grammar-anchored-indentation () | |
1382 | "Return indentation based on previous anchor character found." | |
1383 | (let (indent) | |
1384 | (save-excursion | |
1385 | (while (not indent) | |
1386 | (semantic-grammar-backward-item) | |
1387 | (cond | |
1388 | ((bobp) | |
1389 | (setq indent 0)) | |
1390 | ((looking-at ":\\(\\s-\\|$\\)") | |
1391 | (setq indent (current-column)) | |
1392 | (forward-char) | |
1393 | (skip-syntax-forward "-") | |
1394 | (if (eolp) (setq indent 2)) | |
1395 | ) | |
1396 | ((and (looking-at "[;%]") | |
1397 | (not (looking-at "\\<%prec\\>"))) | |
1398 | (setq indent 0) | |
1399 | )))) | |
1400 | indent)) | |
1401 | ||
1402 | (defun semantic-grammar-do-grammar-indent () | |
1403 | "Indent a line of grammar. | |
1404 | When called the point is not in Lisp code." | |
1405 | (let (indent n) | |
1406 | (save-excursion | |
1407 | (beginning-of-line) | |
1408 | (skip-syntax-forward "-") | |
1409 | (setq indent (current-column)) | |
1410 | (cond | |
1411 | ((or (bobp) | |
1412 | (looking-at "\\(\\w\\|\\s_\\)+\\s-*:") | |
1413 | (and (looking-at "%") | |
1414 | (not (looking-at "%prec\\>")))) | |
1415 | (setq n 0)) | |
1416 | ((looking-at ":") | |
1417 | (setq n 2)) | |
1418 | ((and (looking-at ";;") | |
1419 | (save-excursion (forward-comment (point-max)) | |
1420 | (looking-at ":"))) | |
1421 | (setq n 1)) | |
1422 | (t | |
1423 | (setq n (semantic-grammar-anchored-indentation)) | |
1424 | (unless (zerop n) | |
1425 | (cond | |
1426 | ((looking-at ";;") | |
1427 | (setq n (1- n))) | |
1428 | ((looking-at "[|;]") | |
1429 | ) | |
1430 | (t | |
1431 | (setq n (+ n 2))))))) | |
1432 | (when (/= n indent) | |
1433 | (beginning-of-line) | |
1434 | (delete-horizontal-space) | |
1435 | (indent-to n))))) | |
1436 | ||
1437 | (defvar semantic-grammar-brackets-as-parens-syntax-table | |
1438 | (let ((st (copy-syntax-table emacs-lisp-mode-syntax-table))) | |
1439 | (modify-syntax-entry ?\{ "(} " st) | |
1440 | (modify-syntax-entry ?\} "){ " st) | |
1441 | st) | |
1442 | "Syntax table that consider brackets as parenthesis. | |
1443 | So `lisp-indent-line' will work inside bracket blocks.") | |
1444 | ||
1445 | (defun semantic-grammar-do-lisp-indent () | |
1446 | "Maybe run the Emacs Lisp indenter on a line of code. | |
1447 | Return nil if not in a Lisp expression." | |
1448 | (condition-case nil | |
1449 | (save-excursion | |
1450 | (beginning-of-line) | |
1451 | (skip-chars-forward "\t ") | |
1452 | (let ((first (point))) | |
1453 | (or (>= first (semantic-grammar-epilogue-start)) | |
1454 | (up-list -1)) | |
1455 | (condition-case nil | |
1456 | (while t | |
1457 | (up-list -1)) | |
1458 | (error nil)) | |
1459 | (beginning-of-line) | |
1460 | (save-restriction | |
1461 | (narrow-to-region (point) first) | |
1462 | (goto-char (point-max)) | |
1463 | (with-syntax-table | |
1464 | ;; Temporarily consider brackets as parenthesis so | |
1465 | ;; `lisp-indent-line' can indent Lisp code inside | |
1466 | ;; brackets. | |
1467 | semantic-grammar-brackets-as-parens-syntax-table | |
1468 | (lisp-indent-line)))) | |
1469 | t) | |
1470 | (error nil))) | |
1471 | ||
1472 | (defun semantic-grammar-indent () | |
1473 | "Indent the current line. | |
1474 | Use the Lisp or grammar indenter depending on point location." | |
1475 | (interactive) | |
1476 | (let ((orig (point)) | |
1477 | first) | |
1478 | (or (semantic-grammar-do-lisp-indent) | |
1479 | (semantic-grammar-do-grammar-indent)) | |
1480 | (setq first (save-excursion | |
1481 | (beginning-of-line) | |
1482 | (skip-chars-forward "\t ") | |
1483 | (point))) | |
1484 | (if (or (< orig first) (/= orig (point))) | |
1485 | (goto-char first)))) | |
1486 | ||
1487 | (defun semantic-grammar-electric-punctuation () | |
1488 | "Insert and reindent for the symbol just typed in." | |
1489 | (interactive) | |
1490 | (self-insert-command 1) | |
1491 | (save-excursion | |
1492 | (semantic-grammar-indent))) | |
1493 | ||
1494 | (defun semantic-grammar-complete () | |
1495 | "Attempt to complete the symbol under point. | |
1496 | Completion is position sensitive. If the cursor is in a match section of | |
1497 | a rule, then nonterminals symbols are scanned. If the cursor is in a Lisp | |
1498 | expression then Lisp symbols are completed." | |
1499 | (interactive) | |
1500 | (if (semantic-grammar-in-lisp-p) | |
1501 | ;; We are in lisp code. Do lisp completion. | |
7225c832 GM |
1502 | (let ((completion-at-point-functions |
1503 | (append '(lisp-completion-at-point) | |
1504 | completion-at-point-functions))) | |
1505 | (completion-at-point)) | |
a2095e2e CY |
1506 | ;; We are not in lisp code. Do rule completion. |
1507 | (let* ((nonterms (semantic-find-tags-by-class 'nonterminal (current-buffer))) | |
1508 | (sym (car (semantic-ctxt-current-symbol))) | |
1509 | (ans (try-completion sym nonterms))) | |
1510 | (cond ((eq ans t) | |
1511 | ;; All done | |
1512 | (message "Symbols is already complete")) | |
1513 | ((and (stringp ans) (string= ans sym)) | |
1514 | ;; Max matchable. Show completions. | |
1515 | (with-output-to-temp-buffer "*Completions*" | |
1516 | (display-completion-list (all-completions sym nonterms))) | |
1517 | ) | |
1518 | ((stringp ans) | |
1519 | ;; Expand the completions | |
1520 | (forward-sexp -1) | |
1521 | (delete-region (point) (progn (forward-sexp 1) (point))) | |
1522 | (insert ans)) | |
1523 | (t (message "No Completions.")) | |
1524 | )) | |
1525 | )) | |
1526 | ||
1527 | (defun semantic-grammar-insert-keyword (name) | |
1528 | "Insert a new %keyword declaration with NAME. | |
1529 | Assumes it is typed in with the correct casing." | |
1530 | (interactive "sKeyword: ") | |
1531 | (if (not (bolp)) (insert "\n")) | |
1532 | (insert "%keyword " (upcase name) " \"" name "\" | |
1533 | %put " (upcase name) " summary | |
1534 | \"\"\n") | |
1535 | (forward-char -2)) | |
1536 | ||
1537 | ;;; Macro facilities | |
1538 | ;; | |
1539 | ||
1540 | (defsubst semantic--grammar-macro-function-tag (name) | |
1541 | "Search for a function tag for the grammar macro with name NAME. | |
1542 | Return the tag found or nil if not found." | |
1543 | (car (semantic-find-tags-by-class | |
1544 | 'function | |
1545 | (or (semantic-find-tags-by-name name (current-buffer)) | |
ac73b1fa | 1546 | (and (featurep 'semantic/db) |
a2095e2e CY |
1547 | semanticdb-current-database |
1548 | (cdar (semanticdb-find-tags-by-name name nil t))))))) | |
1549 | ||
1550 | (defsubst semantic--grammar-macro-lib-part (def) | |
1551 | "Return the library part of the grammar macro defined by DEF." | |
1552 | (let ((suf (format "-%s\\'" (regexp-quote (symbol-name (car def))))) | |
1553 | (fun (symbol-name (cdr def)))) | |
1554 | (substring fun 0 (string-match suf fun)))) | |
1555 | ||
1556 | (defun semantic--grammar-macro-compl-elt (def &optional full) | |
1557 | "Return a completion entry for the grammar macro defined by DEF. | |
1558 | If optional argument FULL is non-nil qualify the macro name with the | |
1559 | library found in DEF." | |
1560 | (let ((mac (car def)) | |
1561 | (lib (semantic--grammar-macro-lib-part def))) | |
1562 | (cons (if full | |
1563 | (format "%s/%s" mac lib) | |
1564 | (symbol-name mac)) | |
1565 | (list mac lib)))) | |
1566 | ||
1567 | (defun semantic--grammar-macro-compl-dict () | |
9bf6c65c | 1568 | "Return a completion dictionary of macro definitions." |
a2095e2e CY |
1569 | (let ((defs (semantic-grammar-macros)) |
1570 | def dups dict) | |
1571 | (while defs | |
1572 | (setq def (car defs) | |
1573 | defs (cdr defs)) | |
1574 | (if (or (assoc (car def) defs) (assoc (car def) dups)) | |
1575 | (push def dups) | |
1576 | (push (semantic--grammar-macro-compl-elt def) dict))) | |
1577 | (while dups | |
1578 | (setq def (car dups) | |
1579 | dups (cdr dups)) | |
1580 | (push (semantic--grammar-macro-compl-elt def t) dict)) | |
1581 | dict)) | |
1582 | ||
1583 | (defun semantic-grammar-find-macro-expander (macro-name library) | |
1584 | "Visit the Emacs Lisp library where a grammar macro is implemented. | |
1585 | MACRO-NAME is a symbol that identifies a grammar macro. | |
1586 | LIBRARY is the name (sans extension) of the Emacs Lisp library where | |
1587 | to start searching the macro implementation. Lookup in included | |
1588 | libraries, if necessary. | |
1589 | Find a function tag (in current tags table) whose name contains MACRO-NAME. | |
1590 | Select the buffer containing the tag's definition, and move point there." | |
1591 | (interactive | |
1592 | (let* ((dic (semantic--grammar-macro-compl-dict)) | |
1593 | (def (assoc (completing-read "Macro: " dic nil 1) dic))) | |
1594 | (or (cdr def) '(nil nil)))) | |
1595 | (when (and macro-name library) | |
1596 | (let* ((lib (format "%s.el" library)) | |
1597 | (buf (find-file-noselect (or (locate-library lib t) lib))) | |
1598 | (tag (with-current-buffer buf | |
1599 | (semantic--grammar-macro-function-tag | |
1600 | (format "%s-%s" library macro-name))))) | |
1601 | (if tag | |
1602 | (progn | |
1603 | (require 'semantic/decorate) | |
1604 | (pop-to-buffer (semantic-tag-buffer tag)) | |
1605 | (goto-char (semantic-tag-start tag)) | |
1606 | (semantic-momentary-highlight-tag tag)) | |
1607 | (pop-to-buffer buf) | |
1608 | (message "No expander found in library %s for macro %s" | |
1609 | library macro-name))))) | |
1610 | ||
1611 | ;;; Additional help | |
1612 | ;; | |
1613 | ||
1614 | (defvar semantic-grammar-syntax-help | |
1615 | `( | |
1616 | ;; Lexical Symbols | |
1617 | ("symbol" . "Syntax: A symbol of alpha numeric and symbol characters") | |
1618 | ("number" . "Syntax: Numeric characters.") | |
1619 | ("punctuation" . "Syntax: Punctuation character.") | |
1620 | ("semantic-list" . "Syntax: A list delimited by any valid list characters") | |
1621 | ("open-paren" . "Syntax: Open Parenthesis character") | |
1622 | ("close-paren" . "Syntax: Close Parenthesis character") | |
1623 | ("string" . "Syntax: String character delimited text") | |
1624 | ("comment" . "Syntax: Comment character delimited text") | |
1625 | ;; Special Macros | |
1626 | ("EMPTY" . "Syntax: Match empty text") | |
1627 | ("ASSOC" . "Lambda Key: (ASSOC key1 value1 key2 value2 ...)") | |
1628 | ("EXPAND" . "Lambda Key: (EXPAND <list id> <rule>)") | |
1629 | ("EXPANDFULL" . "Lambda Key: (EXPANDFULL <list id> <rule>)") | |
1630 | ;; Tag Generator Macros | |
1631 | ("TAG" . "Generic Tag Generation: (TAG <name> <tag-class> [ :key value ]*)") | |
1632 | ("VARIABLE-TAG" . "(VARIABLE-TAG <name> <lang-type> <default-value> [ :key value ]*)") | |
1633 | ("FUNCTION-TAG" . "(FUNCTION-TAG <name> <lang-type> <arg-list> [ :key value ]*)") | |
1634 | ("TYPE-TAG" . "(TYPE-TAG <name> <lang-type> <part-list> <parents> [ :key value ]*)") | |
1635 | ("INCLUDE-TAG" . "(INCLUDE-TAG <name> <system-flag> [ :key value ]*)") | |
1636 | ("PACKAGE-TAG" . "(PACKAGE-TAG <name> <detail> [ :key value ]*)") | |
1637 | ("CODE-TAG" . "(CODE-TAG <name> <detail> [ :key value ]*)") | |
1638 | ("ALIAS-TAG" . "(ALIAS-TAG <name> <aliasclass> <definition> [:key value]*)") | |
1639 | ;; Special value macros | |
1640 | ("$1" . "Match Value: Value from match list in slot 1") | |
1641 | ("$2" . "Match Value: Value from match list in slot 2") | |
1642 | ("$3" . "Match Value: Value from match list in slot 3") | |
1643 | ("$4" . "Match Value: Value from match list in slot 4") | |
1644 | ("$5" . "Match Value: Value from match list in slot 5") | |
1645 | ("$6" . "Match Value: Value from match list in slot 6") | |
1646 | ("$7" . "Match Value: Value from match list in slot 7") | |
1647 | ("$8" . "Match Value: Value from match list in slot 8") | |
1648 | ("$9" . "Match Value: Value from match list in slot 9") | |
1649 | ;; Same, but with annoying , in front. | |
1650 | (",$1" . "Match Value: Value from match list in slot 1") | |
1651 | (",$2" . "Match Value: Value from match list in slot 2") | |
1652 | (",$3" . "Match Value: Value from match list in slot 3") | |
1653 | (",$4" . "Match Value: Value from match list in slot 4") | |
1654 | (",$5" . "Match Value: Value from match list in slot 5") | |
1655 | (",$6" . "Match Value: Value from match list in slot 6") | |
1656 | (",$7" . "Match Value: Value from match list in slot 7") | |
1657 | (",$8" . "Match Value: Value from match list in slot 8") | |
1658 | (",$9" . "Match Value: Value from match list in slot 9") | |
1659 | ) | |
1660 | "Association of syntax elements, and the corresponding help.") | |
1661 | ||
3f2a848d DE |
1662 | (declare-function eldoc-function-argstring "eldoc") |
1663 | (declare-function eldoc-docstring-format-sym-doc "eldoc") | |
1664 | (declare-function eldoc-last-data-store "eldoc") | |
1665 | (declare-function eldoc-get-fnsym-args-string "eldoc") | |
1666 | (declare-function eldoc-get-var-docstring "eldoc") | |
1667 | ||
a2095e2e CY |
1668 | (defun semantic-grammar-eldoc-get-macro-docstring (macro expander) |
1669 | "Return a one-line docstring for the given grammar MACRO. | |
1670 | EXPANDER is the name of the function that expands MACRO." | |
1671 | (require 'eldoc) | |
1672 | (if (and (eq expander (aref eldoc-last-data 0)) | |
1673 | (eq 'function (aref eldoc-last-data 2))) | |
1674 | (aref eldoc-last-data 1) | |
1675 | (let ((doc (help-split-fundoc (documentation expander t) expander))) | |
1676 | (cond | |
1677 | (doc | |
1678 | (setq doc (car doc)) | |
1679 | (string-match "\\`[^ )]* ?" doc) | |
1680 | (setq doc (concat "(" (substring doc (match-end 0))))) | |
1681 | (t | |
1682 | (setq doc (eldoc-function-argstring expander)))) | |
1683 | (when doc | |
1684 | (setq doc | |
1685 | (eldoc-docstring-format-sym-doc | |
1686 | macro (format "==> %s %s" expander doc) 'default)) | |
1687 | (eldoc-last-data-store expander doc 'function)) | |
1688 | doc))) | |
1689 | ||
1690 | (define-mode-local-override semantic-idle-summary-current-symbol-info | |
1691 | semantic-grammar-mode () | |
1692 | "Display additional eldoc information about grammar syntax elements. | |
1693 | Syntax element is the current symbol at point. | |
1694 | If it is associated a help string in `semantic-grammar-syntax-help', | |
1695 | return that string. | |
1696 | If it is a macro name, return a description of the associated expander | |
1697 | function parameter list. | |
1698 | If it is a function name, return a description of this function | |
1699 | parameter list. | |
1700 | It it is a variable name, return a brief (one-line) documentation | |
1701 | string for the variable. | |
1702 | If a default description of the current context can be obtained, | |
1703 | return it. | |
1704 | Otherwise return nil." | |
1705 | (require 'eldoc) | |
1706 | (let* ((elt (car (semantic-ctxt-current-symbol))) | |
1707 | (val (and elt (cdr (assoc elt semantic-grammar-syntax-help))))) | |
1708 | (when (and (not val) elt (semantic-grammar-in-lisp-p)) | |
1709 | ;; Ensure to load macro definitions before doing `intern-soft'. | |
1710 | (setq val (semantic-grammar-macros) | |
1711 | elt (intern-soft elt) | |
1712 | val (and elt (cdr (assq elt val)))) | |
1713 | (cond | |
1714 | ;; Grammar macro | |
1715 | ((and val (fboundp val)) | |
1716 | (setq val (semantic-grammar-eldoc-get-macro-docstring elt val))) | |
1717 | ;; Function | |
1718 | ((and elt (fboundp elt)) | |
1719 | (setq val (eldoc-get-fnsym-args-string elt))) | |
1720 | ;; Variable | |
1721 | ((and elt (boundp elt)) | |
1722 | (setq val (eldoc-get-var-docstring elt))) | |
1723 | (t nil))) | |
1724 | (or val (semantic-idle-summary-current-symbol-info-default)))) | |
1725 | ||
1726 | (define-mode-local-override semantic-tag-boundary-p | |
1727 | semantic-grammar-mode (tag) | |
1728 | "Return non-nil for tags that should have a boundary drawn. | |
1729 | Only tags of type 'nonterminal will be so marked." | |
1730 | (let ((c (semantic-tag-class tag))) | |
1731 | (eq c 'nonterminal))) | |
1732 | ||
1733 | (define-mode-local-override semantic-ctxt-current-function | |
1734 | semantic-grammar-mode (&optional point) | |
1735 | "Determine the name of the current function at POINT." | |
1736 | (save-excursion | |
1737 | (and point (goto-char point)) | |
1738 | (when (semantic-grammar-in-lisp-p) | |
1739 | (with-mode-local emacs-lisp-mode | |
1740 | (semantic-ctxt-current-function))))) | |
1741 | ||
1742 | (define-mode-local-override semantic-ctxt-current-argument | |
1743 | semantic-grammar-mode (&optional point) | |
1744 | "Determine the argument index of the called function at POINT." | |
1745 | (save-excursion | |
1746 | (and point (goto-char point)) | |
1747 | (when (semantic-grammar-in-lisp-p) | |
1748 | (with-mode-local emacs-lisp-mode | |
1749 | (semantic-ctxt-current-argument))))) | |
1750 | ||
1751 | (define-mode-local-override semantic-ctxt-current-assignment | |
1752 | semantic-grammar-mode (&optional point) | |
1753 | "Determine the tag being assigned into at POINT." | |
1754 | (save-excursion | |
1755 | (and point (goto-char point)) | |
1756 | (when (semantic-grammar-in-lisp-p) | |
1757 | (with-mode-local emacs-lisp-mode | |
1758 | (semantic-ctxt-current-assignment))))) | |
1759 | ||
1760 | (define-mode-local-override semantic-ctxt-current-class-list | |
1761 | semantic-grammar-mode (&optional point) | |
1762 | "Determine the class of tags that can be used at POINT." | |
1763 | (save-excursion | |
1764 | (and point (goto-char point)) | |
1765 | (if (semantic-grammar-in-lisp-p) | |
1766 | (with-mode-local emacs-lisp-mode | |
1767 | (semantic-ctxt-current-class-list)) | |
1768 | '(nonterminal keyword)))) | |
1769 | ||
1770 | (define-mode-local-override semantic-ctxt-current-mode | |
1771 | semantic-grammar-mode (&optional point) | |
1772 | "Return the major mode active at POINT. | |
1773 | POINT defaults to the value of point in current buffer. | |
1774 | Return `emacs-lisp-mode' is POINT is within Lisp code, otherwise | |
1775 | return the current major mode." | |
1776 | (save-excursion | |
1777 | (and point (goto-char point)) | |
1778 | (if (semantic-grammar-in-lisp-p) | |
1779 | 'emacs-lisp-mode | |
1780 | (semantic-ctxt-current-mode-default)))) | |
1781 | ||
1782 | (define-mode-local-override semantic-format-tag-abbreviate | |
1783 | semantic-grammar-mode (tag &optional parent color) | |
1784 | "Return a string abbreviation of TAG. | |
1785 | Optional PARENT is not used. | |
1786 | Optional COLOR is used to flag if color is added to the text." | |
1787 | (let ((class (semantic-tag-class tag)) | |
1788 | (name (semantic-format-tag-name tag parent color))) | |
1789 | (cond | |
1790 | ((eq class 'nonterminal) | |
1791 | (concat name ":")) | |
1792 | ((eq class 'setting) | |
1793 | "%settings%") | |
1794 | ((memq class '(rule keyword)) | |
1795 | name) | |
1796 | (t | |
1797 | (concat "%" (symbol-name class) " " name))))) | |
1798 | ||
1799 | (define-mode-local-override semantic-format-tag-summarize | |
1800 | semantic-grammar-mode (tag &optional parent color) | |
1801 | "Return a string summarizing TAG. | |
1802 | Optional PARENT is not used. | |
1803 | Optional argument COLOR determines if color is added to the text." | |
1804 | (let ((class (semantic-tag-class tag)) | |
1805 | (name (semantic-format-tag-name tag parent color)) | |
1806 | (label nil) | |
1807 | (desc nil)) | |
1808 | (cond | |
1809 | ((eq class 'nonterminal) | |
1810 | (setq label "Nonterminal: " | |
1811 | desc (format | |
1812 | " with %d match lists." | |
1813 | (length (semantic-tag-components tag))))) | |
1814 | ((eq class 'keyword) | |
1815 | (setq label "Keyword: ") | |
1816 | (let (summary) | |
1817 | (semantic--find-tags-by-function | |
1818 | #'(lambda (put) | |
1819 | (unless summary | |
1820 | (setq summary (cdr (assoc "summary" | |
1821 | (semantic-tag-get-attribute | |
1822 | put :value)))))) | |
1823 | ;; Get `put' tag with TAG name. | |
1824 | (semantic-find-tags-by-name-regexp | |
1825 | (regexp-quote (semantic-tag-name tag)) | |
1826 | (semantic-find-tags-by-class 'put (current-buffer)))) | |
1827 | (setq desc (concat " = " | |
1828 | (semantic-tag-get-attribute tag :value) | |
1829 | (if summary | |
1830 | (concat " - " (read summary)) | |
1831 | ""))))) | |
1832 | ((eq class 'token) | |
1833 | (setq label "Token: ") | |
1834 | (let ((val (semantic-tag-get-attribute tag :value)) | |
1835 | (names (semantic-tag-get-attribute tag :rest)) | |
1836 | (type (semantic-tag-type tag))) | |
1837 | (if names | |
1838 | (setq name (mapconcat 'identity (cons name names) " "))) | |
1839 | (setq desc (concat | |
1840 | (if type | |
1841 | (format " <%s>" type) | |
1842 | "") | |
1843 | (if val | |
1844 | (format "%s%S" val (if type " " "")) | |
1845 | ""))))) | |
1846 | ((eq class 'assoc) | |
1847 | (setq label "Assoc: ") | |
1848 | (let ((val (semantic-tag-get-attribute tag :value)) | |
1849 | (type (semantic-tag-type tag))) | |
1850 | (setq desc (concat | |
1851 | (if type | |
1852 | (format " <%s>" type) | |
1853 | "") | |
1854 | (if val | |
1855 | (concat " " (mapconcat 'identity val " ")) | |
1856 | ""))))) | |
1857 | (t | |
1858 | (setq desc (semantic-format-tag-abbreviate tag parent color)))) | |
1859 | (if (and color label) | |
1860 | (setq label (semantic--format-colorize-text label 'label))) | |
1861 | (if (and color label desc) | |
1862 | (setq desc (semantic--format-colorize-text desc 'comment))) | |
1863 | (if label | |
1864 | (concat label name desc) | |
1865 | ;; Just a description is the abbreviated version | |
1866 | desc))) | |
1867 | ||
1868 | ;;; Semantic Analysis | |
1869 | ||
1870 | (define-mode-local-override semantic-analyze-current-context | |
1871 | semantic-grammar-mode (point) | |
1872 | "Provide a semantic analysis object describing a context in a grammar." | |
1873 | (require 'semantic/analyze) | |
1874 | (if (semantic-grammar-in-lisp-p) | |
1875 | (with-mode-local emacs-lisp-mode | |
1876 | (semantic-analyze-current-context point)) | |
1877 | ||
1878 | (let* ((context-return nil) | |
1879 | (prefixandbounds (semantic-ctxt-current-symbol-and-bounds)) | |
1880 | (prefix (car prefixandbounds)) | |
1881 | (bounds (nth 2 prefixandbounds)) | |
1882 | (prefixsym nil) | |
1883 | (prefixclass (semantic-ctxt-current-class-list)) | |
1884 | ) | |
1885 | ||
1886 | ;; Do context for rules when in a match list. | |
1887 | (setq prefixsym | |
1888 | (semantic-find-first-tag-by-name | |
1889 | (car prefix) | |
1890 | (current-buffer))) | |
1891 | ||
1892 | (setq context-return | |
1893 | (semantic-analyze-context | |
1894 | "context-for-semantic-grammar" | |
1895 | :buffer (current-buffer) | |
1896 | :scope nil | |
1897 | :bounds bounds | |
1898 | :prefix (if prefixsym | |
1899 | (list prefixsym) | |
1900 | prefix) | |
1901 | :prefixtypes nil | |
1902 | :prefixclass prefixclass | |
1903 | )) | |
1904 | ||
1905 | context-return))) | |
1906 | ||
1907 | (define-mode-local-override semantic-analyze-possible-completions | |
1908 | semantic-grammar-mode (context) | |
1909 | "Return a list of possible completions based on CONTEXT." | |
1910 | (require 'semantic/analyze/complete) | |
1911 | (if (semantic-grammar-in-lisp-p) | |
1912 | (with-mode-local emacs-lisp-mode | |
1913 | (semantic-analyze-possible-completions context)) | |
0816d744 | 1914 | (with-current-buffer (oref context buffer) |
a2095e2e CY |
1915 | (let* ((prefix (car (oref context :prefix))) |
1916 | (completetext (cond ((semantic-tag-p prefix) | |
1917 | (semantic-tag-name prefix)) | |
1918 | ((stringp prefix) | |
1919 | prefix) | |
1920 | ((stringp (car prefix)) | |
1921 | (car prefix)))) | |
1922 | (tags (semantic-find-tags-for-completion completetext | |
1923 | (current-buffer)))) | |
1924 | (semantic-analyze-tags-of-class-list | |
1925 | tags (oref context prefixclass))) | |
1926 | ))) | |
1927 | ||
1928 | (provide 'semantic/grammar) | |
1929 | ||
f9b697dd | 1930 | \f |
6b91f903 GM |
1931 | ;; Local variables: |
1932 | ;; generated-autoload-load-name: "semantic/grammar" | |
1933 | ;; End: | |
1934 | ||
a2095e2e | 1935 | ;;; semantic/grammar.el ends here |