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