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