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