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