* titdic-cnv.el: Prevent "Local Variables" confusion.
[bpt/emacs.git] / 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 (derived-mode-p '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 (define-obsolete-variable-alias 'semantic-grammar-syntax-table
1106 'semantic-grammar-mode-syntax-table "24.1")
1107 (defvar semantic-grammar-mode-syntax-table
1108 (let ((table (make-syntax-table (standard-syntax-table))))
1109 (modify-syntax-entry ?\: "." table) ;; COLON
1110 (modify-syntax-entry ?\> "." table) ;; GT
1111 (modify-syntax-entry ?\< "." table) ;; LT
1112 (modify-syntax-entry ?\| "." table) ;; OR
1113 (modify-syntax-entry ?\; ". 12" table) ;; SEMI, Comment start ;;
1114 (modify-syntax-entry ?\n ">" table) ;; Comment end
1115 (modify-syntax-entry ?\" "\"" table) ;; String
1116 (modify-syntax-entry ?\% "w" table) ;; Word
1117 (modify-syntax-entry ?\- "_" table) ;; Symbol
1118 (modify-syntax-entry ?\. "_" table) ;; Symbol
1119 (modify-syntax-entry ?\\ "\\" table) ;; Quote
1120 (modify-syntax-entry ?\` "'" table) ;; Prefix ` (backquote)
1121 (modify-syntax-entry ?\' "'" table) ;; Prefix ' (quote)
1122 (modify-syntax-entry ?\, "'" table) ;; Prefix , (comma)
1123 (modify-syntax-entry ?\# "'" table) ;; Prefix # (sharp)
1124 table)
1125 "Syntax table used in a Semantic grammar buffers.")
1126
1127 (defvar semantic-grammar-mode-hook nil
1128 "Hook run when starting Semantic grammar mode.")
1129
1130 (defvar semantic-grammar-mode-keywords-1
1131 `(("\\(\\<%%\\>\\|\\<%[{}]\\)"
1132 0 font-lock-reference-face)
1133 ("\\(%\\)\\(\\(\\sw\\|\\s_\\)+\\)"
1134 (1 font-lock-reference-face)
1135 (2 font-lock-keyword-face))
1136 ("\\<error\\>"
1137 0 (unless (semantic-grammar-in-lisp-p) 'bold))
1138 ("^\\(\\(\\sw\\|\\s_\\)+\\)[ \n\r\t]*:"
1139 1 font-lock-function-name-face)
1140 (semantic--grammar-macros-matcher
1141 1 ,(if (boundp 'font-lock-builtin-face)
1142 'font-lock-builtin-face
1143 'font-lock-preprocessor-face))
1144 ("\\$\\(\\sw\\|\\s_\\)*"
1145 0 font-lock-variable-name-face)
1146 ("<\\(\\(\\sw\\|\\s_\\)+\\)>"
1147 1 font-lock-type-face)
1148 (,semantic-grammar-lex-c-char-re
1149 0 ,(if (boundp 'font-lock-constant-face)
1150 'font-lock-constant-face
1151 'font-lock-string-face) t)
1152 ;; Must highlight :keyword here, because ':' is a punctuation in
1153 ;; grammar mode!
1154 ("[\r\n\t ]+:\\sw+\\>"
1155 0 font-lock-builtin-face)
1156 ;; ;; Append the Semantic keywords
1157 ;; ,@semantic-fw-font-lock-keywords
1158 )
1159 "Font Lock keywords used to highlight Semantic grammar buffers.")
1160
1161 (defvar semantic-grammar-mode-keywords-2
1162 (append semantic-grammar-mode-keywords-1
1163 (if (boundp 'lisp-font-lock-keywords-1)
1164 lisp-font-lock-keywords-1
1165 lisp-el-font-lock-keywords-1))
1166 "Font Lock keywords used to highlight Semantic grammar buffers.")
1167
1168 (defvar semantic-grammar-mode-keywords-3
1169 (append semantic-grammar-mode-keywords-1
1170 (if (boundp 'lisp-font-lock-keywords-2)
1171 lisp-font-lock-keywords-2
1172 lisp-el-font-lock-keywords-2))
1173 "Font Lock keywords used to highlight Semantic grammar buffers.")
1174
1175 (defvar semantic-grammar-mode-keywords
1176 semantic-grammar-mode-keywords-1
1177 "Font Lock keywords used to highlight Semantic grammar buffers.")
1178
1179 (define-obsolete-variable-alias 'semantic-grammar-map
1180 'semantic-grammar-mode-map "24.1")
1181 (defvar semantic-grammar-mode-map
1182 (let ((km (make-sparse-keymap)))
1183
1184 (define-key km "|" 'semantic-grammar-electric-punctuation)
1185 (define-key km ";" 'semantic-grammar-electric-punctuation)
1186 (define-key km "%" 'semantic-grammar-electric-punctuation)
1187 (define-key km "(" 'semantic-grammar-electric-punctuation)
1188 (define-key km ")" 'semantic-grammar-electric-punctuation)
1189 (define-key km ":" 'semantic-grammar-electric-punctuation)
1190
1191 (define-key km "\t" 'semantic-grammar-indent)
1192 (define-key km "\M-\t" 'semantic-grammar-complete)
1193 (define-key km "\C-c\C-c" 'semantic-grammar-create-package)
1194 (define-key km "\C-cm" 'semantic-grammar-find-macro-expander)
1195 (define-key km "\C-cik" 'semantic-grammar-insert-keyword)
1196 ;; (define-key km "\C-cc" 'semantic-grammar-generate-and-load)
1197 ;; (define-key km "\C-cr" 'semantic-grammar-generate-one-rule)
1198
1199 km)
1200 "Keymap used in `semantic-grammar-mode'.")
1201
1202 (defvar semantic-grammar-menu
1203 '("Grammar"
1204 ["Indent Line" semantic-grammar-indent]
1205 ["Complete Symbol" semantic-grammar-complete]
1206 ["Find Macro" semantic-grammar-find-macro-expander]
1207 "--"
1208 ["Insert %keyword" semantic-grammar-insert-keyword]
1209 "--"
1210 ["Update Lisp Package" semantic-grammar-create-package]
1211 ["Recreate Lisp Package" semantic-grammar-recreate-package]
1212 )
1213 "Common semantic grammar menu.")
1214
1215 (defun semantic-grammar-setup-menu-emacs (symbol mode-menu)
1216 "Setup a GNU Emacs grammar menu in variable SYMBOL.
1217 MODE-MENU is an optional specific menu whose items are appended to the
1218 common grammar menu."
1219 (let ((items (make-symbol "items")))
1220 `(unless (boundp ',symbol)
1221 (easy-menu-define ,symbol (current-local-map)
1222 "Grammar Menu" semantic-grammar-menu)
1223 (let ((,items (cdr ,mode-menu)))
1224 (when ,items
1225 (easy-menu-add-item ,symbol nil "--")
1226 (while ,items
1227 (easy-menu-add-item ,symbol nil (car ,items))
1228 (setq ,items (cdr ,items))))))
1229 ))
1230
1231 (defun semantic-grammar-setup-menu-xemacs (symbol mode-menu)
1232 "Setup an XEmacs grammar menu in variable SYMBOL.
1233 MODE-MENU is an optional specific menu whose items are appended to the
1234 common grammar menu."
1235 (let ((items (make-symbol "items"))
1236 (path (make-symbol "path")))
1237 `(progn
1238 (unless (boundp ',symbol)
1239 (easy-menu-define ,symbol nil
1240 "Grammar Menu" (copy-sequence semantic-grammar-menu)))
1241 (easy-menu-add ,symbol)
1242 (let ((,items (cdr ,mode-menu))
1243 (,path (list (car ,symbol))))
1244 (when ,items
1245 (easy-menu-add-item nil ,path "--")
1246 (while ,items
1247 (easy-menu-add-item nil ,path (car ,items))
1248 (setq ,items (cdr ,items))))))
1249 ))
1250
1251 (defmacro semantic-grammar-setup-menu (&optional mode-menu)
1252 "Setup a mode local grammar menu.
1253 MODE-MENU is an optional specific menu whose items are appended to the
1254 common grammar menu."
1255 (let ((menu (intern (format "%s-menu" major-mode))))
1256 (if (featurep 'xemacs)
1257 (semantic-grammar-setup-menu-xemacs menu mode-menu)
1258 (semantic-grammar-setup-menu-emacs menu mode-menu))))
1259
1260 (defsubst semantic-grammar-in-lisp-p ()
1261 "Return non-nil if point is in Lisp code."
1262 (or (>= (point) (semantic-grammar-epilogue-start))
1263 (condition-case nil
1264 (save-excursion
1265 (up-list -1)
1266 t)
1267 (error nil))))
1268
1269 (defun semantic-grammar-edits-new-change-hook-fcn (overlay)
1270 "Function set into `semantic-edits-new-change-hook'.
1271 Argument OVERLAY is the overlay created to mark the change.
1272 When OVERLAY marks a change in the scope of a nonterminal tag extend
1273 the change bounds to encompass the whole nonterminal tag."
1274 (let ((outer (car (semantic-find-tag-by-overlay-in-region
1275 (semantic-edits-os overlay)
1276 (semantic-edits-oe overlay)))))
1277 (if (semantic-tag-of-class-p outer 'nonterminal)
1278 (semantic-overlay-move overlay
1279 (semantic-tag-start outer)
1280 (semantic-tag-end outer)))))
1281
1282 (define-derived-mode semantic-grammar-mode
1283 fundamental-mode "Semantic Grammar Framework"
1284 "Initialize a buffer for editing Semantic grammars.
1285
1286 \\{semantic-grammar-mode-map}"
1287 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1288 (set (make-local-variable 'comment-start) ";;")
1289 ;; Look within the line for a ; following an even number of backslashes
1290 ;; after either a non-backslash or the line beginning.
1291 (set (make-local-variable 'comment-start-skip)
1292 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
1293 (set (make-local-variable 'indent-line-function)
1294 'semantic-grammar-indent)
1295 (set (make-local-variable 'fill-paragraph-function)
1296 'lisp-fill-paragraph)
1297 (set (make-local-variable 'font-lock-multiline)
1298 'undecided)
1299 (set (make-local-variable 'font-lock-defaults)
1300 '((semantic-grammar-mode-keywords
1301 semantic-grammar-mode-keywords-1
1302 semantic-grammar-mode-keywords-2
1303 semantic-grammar-mode-keywords-3)
1304 nil ;; perform string/comment fontification
1305 nil ;; keywords are case sensitive.
1306 ;; This puts _ & - as a word constituent,
1307 ;; simplifying our keywords significantly
1308 ((?_ . "w") (?- . "w"))))
1309 ;; Setup Semantic to parse grammar
1310 (semantic-grammar-wy--install-parser)
1311 (setq semantic-lex-comment-regex ";;"
1312 semantic-lex-analyzer 'semantic-grammar-lexer
1313 semantic-type-relation-separator-character '(":")
1314 semantic-symbol->name-assoc-list
1315 '(
1316 (code . "Setup Code")
1317 (keyword . "Keyword")
1318 (token . "Token")
1319 (nonterminal . "Nonterminal")
1320 (rule . "Rule")
1321 ))
1322 (set (make-local-variable 'semantic-format-face-alist)
1323 '(
1324 (code . default)
1325 (keyword . font-lock-keyword-face)
1326 (token . font-lock-type-face)
1327 (nonterminal . font-lock-function-name-face)
1328 (rule . default)
1329 ))
1330 (set (make-local-variable 'semantic-stickyfunc-sticky-classes)
1331 '(nonterminal))
1332 ;; Before each change, clear the cached regexp used to highlight
1333 ;; macros local in this grammar.
1334 (semantic-make-local-hook 'before-change-functions)
1335 (add-hook 'before-change-functions
1336 'semantic--grammar-clear-macros-regexp-2 nil t)
1337 ;; Handle safe re-parse of grammar rules.
1338 (semantic-make-local-hook 'semantic-edits-new-change-functions)
1339 (add-hook 'semantic-edits-new-change-functions
1340 'semantic-grammar-edits-new-change-hook-fcn
1341 nil t))
1342 \f
1343 ;;;;
1344 ;;;; Useful commands
1345 ;;;;
1346
1347 (defvar semantic-grammar-skip-quoted-syntax-table
1348 (let ((st (copy-syntax-table semantic-grammar-mode-syntax-table)))
1349 (modify-syntax-entry ?\' "$" st)
1350 st)
1351 "Syntax table to skip a whole quoted expression in grammar code.
1352 Consider quote as a \"paired delimiter\", so `forward-sexp' will skip
1353 whole quoted expression.")
1354
1355 (defsubst semantic-grammar-backward-item ()
1356 "Move point to beginning of the previous grammar item."
1357 (forward-comment (- (point-max)))
1358 (if (zerop (skip-syntax-backward "."))
1359 (if (eq (char-before) ?\')
1360 (with-syntax-table
1361 ;; Can't be Lisp code here! Temporarily consider quote
1362 ;; as a "paired delimiter", so `forward-sexp' can skip
1363 ;; the whole quoted expression.
1364 semantic-grammar-skip-quoted-syntax-table
1365 (forward-sexp -1))
1366 (forward-sexp -1))))
1367
1368 (defun semantic-grammar-anchored-indentation ()
1369 "Return indentation based on previous anchor character found."
1370 (let (indent)
1371 (save-excursion
1372 (while (not indent)
1373 (semantic-grammar-backward-item)
1374 (cond
1375 ((bobp)
1376 (setq indent 0))
1377 ((looking-at ":\\(\\s-\\|$\\)")
1378 (setq indent (current-column))
1379 (forward-char)
1380 (skip-syntax-forward "-")
1381 (if (eolp) (setq indent 2))
1382 )
1383 ((and (looking-at "[;%]")
1384 (not (looking-at "\\<%prec\\>")))
1385 (setq indent 0)
1386 ))))
1387 indent))
1388
1389 (defun semantic-grammar-do-grammar-indent ()
1390 "Indent a line of grammar.
1391 When called the point is not in Lisp code."
1392 (let (indent n)
1393 (save-excursion
1394 (beginning-of-line)
1395 (skip-syntax-forward "-")
1396 (setq indent (current-column))
1397 (cond
1398 ((or (bobp)
1399 (looking-at "\\(\\w\\|\\s_\\)+\\s-*:")
1400 (and (looking-at "%")
1401 (not (looking-at "%prec\\>"))))
1402 (setq n 0))
1403 ((looking-at ":")
1404 (setq n 2))
1405 ((and (looking-at ";;")
1406 (save-excursion (forward-comment (point-max))
1407 (looking-at ":")))
1408 (setq n 1))
1409 (t
1410 (setq n (semantic-grammar-anchored-indentation))
1411 (unless (zerop n)
1412 (cond
1413 ((looking-at ";;")
1414 (setq n (1- n)))
1415 ((looking-at "[|;]")
1416 )
1417 (t
1418 (setq n (+ n 2)))))))
1419 (when (/= n indent)
1420 (beginning-of-line)
1421 (delete-horizontal-space)
1422 (indent-to n)))))
1423
1424 (defvar semantic-grammar-brackets-as-parens-syntax-table
1425 (let ((st (copy-syntax-table emacs-lisp-mode-syntax-table)))
1426 (modify-syntax-entry ?\{ "(} " st)
1427 (modify-syntax-entry ?\} "){ " st)
1428 st)
1429 "Syntax table that consider brackets as parenthesis.
1430 So `lisp-indent-line' will work inside bracket blocks.")
1431
1432 (defun semantic-grammar-do-lisp-indent ()
1433 "Maybe run the Emacs Lisp indenter on a line of code.
1434 Return nil if not in a Lisp expression."
1435 (condition-case nil
1436 (save-excursion
1437 (beginning-of-line)
1438 (skip-chars-forward "\t ")
1439 (let ((first (point)))
1440 (or (>= first (semantic-grammar-epilogue-start))
1441 (up-list -1))
1442 (condition-case nil
1443 (while t
1444 (up-list -1))
1445 (error nil))
1446 (beginning-of-line)
1447 (save-restriction
1448 (narrow-to-region (point) first)
1449 (goto-char (point-max))
1450 (with-syntax-table
1451 ;; Temporarily consider brackets as parenthesis so
1452 ;; `lisp-indent-line' can indent Lisp code inside
1453 ;; brackets.
1454 semantic-grammar-brackets-as-parens-syntax-table
1455 (lisp-indent-line))))
1456 t)
1457 (error nil)))
1458
1459 (defun semantic-grammar-indent ()
1460 "Indent the current line.
1461 Use the Lisp or grammar indenter depending on point location."
1462 (interactive)
1463 (let ((orig (point))
1464 first)
1465 (or (semantic-grammar-do-lisp-indent)
1466 (semantic-grammar-do-grammar-indent))
1467 (setq first (save-excursion
1468 (beginning-of-line)
1469 (skip-chars-forward "\t ")
1470 (point)))
1471 (if (or (< orig first) (/= orig (point)))
1472 (goto-char first))))
1473
1474 (defun semantic-grammar-electric-punctuation ()
1475 "Insert and reindent for the symbol just typed in."
1476 (interactive)
1477 (self-insert-command 1)
1478 (save-excursion
1479 (semantic-grammar-indent)))
1480
1481 (defun semantic-grammar-complete ()
1482 "Attempt to complete the symbol under point.
1483 Completion is position sensitive. If the cursor is in a match section of
1484 a rule, then nonterminals symbols are scanned. If the cursor is in a Lisp
1485 expression then Lisp symbols are completed."
1486 (interactive)
1487 (if (semantic-grammar-in-lisp-p)
1488 ;; We are in lisp code. Do lisp completion.
1489 (let ((completion-at-point-functions
1490 (append '(lisp-completion-at-point)
1491 completion-at-point-functions)))
1492 (completion-at-point))
1493 ;; We are not in lisp code. Do rule completion.
1494 (let* ((nonterms (semantic-find-tags-by-class 'nonterminal (current-buffer)))
1495 (sym (car (semantic-ctxt-current-symbol)))
1496 (ans (try-completion sym nonterms)))
1497 (cond ((eq ans t)
1498 ;; All done
1499 (message "Symbols is already complete"))
1500 ((and (stringp ans) (string= ans sym))
1501 ;; Max matchable. Show completions.
1502 (with-output-to-temp-buffer "*Completions*"
1503 (display-completion-list (all-completions sym nonterms)))
1504 )
1505 ((stringp ans)
1506 ;; Expand the completions
1507 (forward-sexp -1)
1508 (delete-region (point) (progn (forward-sexp 1) (point)))
1509 (insert ans))
1510 (t (message "No Completions."))
1511 ))
1512 ))
1513
1514 (defun semantic-grammar-insert-keyword (name)
1515 "Insert a new %keyword declaration with NAME.
1516 Assumes it is typed in with the correct casing."
1517 (interactive "sKeyword: ")
1518 (if (not (bolp)) (insert "\n"))
1519 (insert "%keyword " (upcase name) " \"" name "\"
1520 %put " (upcase name) " summary
1521 \"\"\n")
1522 (forward-char -2))
1523
1524 ;;; Macro facilities
1525 ;;
1526
1527 (defsubst semantic--grammar-macro-function-tag (name)
1528 "Search for a function tag for the grammar macro with name NAME.
1529 Return the tag found or nil if not found."
1530 (car (semantic-find-tags-by-class
1531 'function
1532 (or (semantic-find-tags-by-name name (current-buffer))
1533 (and (featurep 'semantic/db)
1534 semanticdb-current-database
1535 (cdar (semanticdb-find-tags-by-name name nil t)))))))
1536
1537 (defsubst semantic--grammar-macro-lib-part (def)
1538 "Return the library part of the grammar macro defined by DEF."
1539 (let ((suf (format "-%s\\'" (regexp-quote (symbol-name (car def)))))
1540 (fun (symbol-name (cdr def))))
1541 (substring fun 0 (string-match suf fun))))
1542
1543 (defun semantic--grammar-macro-compl-elt (def &optional full)
1544 "Return a completion entry for the grammar macro defined by DEF.
1545 If optional argument FULL is non-nil qualify the macro name with the
1546 library found in DEF."
1547 (let ((mac (car def))
1548 (lib (semantic--grammar-macro-lib-part def)))
1549 (cons (if full
1550 (format "%s/%s" mac lib)
1551 (symbol-name mac))
1552 (list mac lib))))
1553
1554 (defun semantic--grammar-macro-compl-dict ()
1555 "Return a completion dictionary of macro definitions."
1556 (let ((defs (semantic-grammar-macros))
1557 def dups dict)
1558 (while defs
1559 (setq def (car defs)
1560 defs (cdr defs))
1561 (if (or (assoc (car def) defs) (assoc (car def) dups))
1562 (push def dups)
1563 (push (semantic--grammar-macro-compl-elt def) dict)))
1564 (while dups
1565 (setq def (car dups)
1566 dups (cdr dups))
1567 (push (semantic--grammar-macro-compl-elt def t) dict))
1568 dict))
1569
1570 (defun semantic-grammar-find-macro-expander (macro-name library)
1571 "Visit the Emacs Lisp library where a grammar macro is implemented.
1572 MACRO-NAME is a symbol that identifies a grammar macro.
1573 LIBRARY is the name (sans extension) of the Emacs Lisp library where
1574 to start searching the macro implementation. Lookup in included
1575 libraries, if necessary.
1576 Find a function tag (in current tags table) whose name contains MACRO-NAME.
1577 Select the buffer containing the tag's definition, and move point there."
1578 (interactive
1579 (let* ((dic (semantic--grammar-macro-compl-dict))
1580 (def (assoc (completing-read "Macro: " dic nil 1) dic)))
1581 (or (cdr def) '(nil nil))))
1582 (when (and macro-name library)
1583 (let* ((lib (format "%s.el" library))
1584 (buf (find-file-noselect (or (locate-library lib t) lib)))
1585 (tag (with-current-buffer buf
1586 (semantic--grammar-macro-function-tag
1587 (format "%s-%s" library macro-name)))))
1588 (if tag
1589 (progn
1590 (require 'semantic/decorate)
1591 (pop-to-buffer (semantic-tag-buffer tag))
1592 (goto-char (semantic-tag-start tag))
1593 (semantic-momentary-highlight-tag tag))
1594 (pop-to-buffer buf)
1595 (message "No expander found in library %s for macro %s"
1596 library macro-name)))))
1597
1598 ;;; Additional help
1599 ;;
1600
1601 (defvar semantic-grammar-syntax-help
1602 `(
1603 ;; Lexical Symbols
1604 ("symbol" . "Syntax: A symbol of alpha numeric and symbol characters")
1605 ("number" . "Syntax: Numeric characters.")
1606 ("punctuation" . "Syntax: Punctuation character.")
1607 ("semantic-list" . "Syntax: A list delimited by any valid list characters")
1608 ("open-paren" . "Syntax: Open Parenthesis character")
1609 ("close-paren" . "Syntax: Close Parenthesis character")
1610 ("string" . "Syntax: String character delimited text")
1611 ("comment" . "Syntax: Comment character delimited text")
1612 ;; Special Macros
1613 ("EMPTY" . "Syntax: Match empty text")
1614 ("ASSOC" . "Lambda Key: (ASSOC key1 value1 key2 value2 ...)")
1615 ("EXPAND" . "Lambda Key: (EXPAND <list id> <rule>)")
1616 ("EXPANDFULL" . "Lambda Key: (EXPANDFULL <list id> <rule>)")
1617 ;; Tag Generator Macros
1618 ("TAG" . "Generic Tag Generation: (TAG <name> <tag-class> [ :key value ]*)")
1619 ("VARIABLE-TAG" . "(VARIABLE-TAG <name> <lang-type> <default-value> [ :key value ]*)")
1620 ("FUNCTION-TAG" . "(FUNCTION-TAG <name> <lang-type> <arg-list> [ :key value ]*)")
1621 ("TYPE-TAG" . "(TYPE-TAG <name> <lang-type> <part-list> <parents> [ :key value ]*)")
1622 ("INCLUDE-TAG" . "(INCLUDE-TAG <name> <system-flag> [ :key value ]*)")
1623 ("PACKAGE-TAG" . "(PACKAGE-TAG <name> <detail> [ :key value ]*)")
1624 ("CODE-TAG" . "(CODE-TAG <name> <detail> [ :key value ]*)")
1625 ("ALIAS-TAG" . "(ALIAS-TAG <name> <aliasclass> <definition> [:key value]*)")
1626 ;; Special value macros
1627 ("$1" . "Match Value: Value from match list in slot 1")
1628 ("$2" . "Match Value: Value from match list in slot 2")
1629 ("$3" . "Match Value: Value from match list in slot 3")
1630 ("$4" . "Match Value: Value from match list in slot 4")
1631 ("$5" . "Match Value: Value from match list in slot 5")
1632 ("$6" . "Match Value: Value from match list in slot 6")
1633 ("$7" . "Match Value: Value from match list in slot 7")
1634 ("$8" . "Match Value: Value from match list in slot 8")
1635 ("$9" . "Match Value: Value from match list in slot 9")
1636 ;; Same, but with annoying , in front.
1637 (",$1" . "Match Value: Value from match list in slot 1")
1638 (",$2" . "Match Value: Value from match list in slot 2")
1639 (",$3" . "Match Value: Value from match list in slot 3")
1640 (",$4" . "Match Value: Value from match list in slot 4")
1641 (",$5" . "Match Value: Value from match list in slot 5")
1642 (",$6" . "Match Value: Value from match list in slot 6")
1643 (",$7" . "Match Value: Value from match list in slot 7")
1644 (",$8" . "Match Value: Value from match list in slot 8")
1645 (",$9" . "Match Value: Value from match list in slot 9")
1646 )
1647 "Association of syntax elements, and the corresponding help.")
1648
1649 (declare-function eldoc-function-argstring "eldoc")
1650 (declare-function eldoc-docstring-format-sym-doc "eldoc")
1651 (declare-function eldoc-last-data-store "eldoc")
1652 (declare-function eldoc-get-fnsym-args-string "eldoc")
1653 (declare-function eldoc-get-var-docstring "eldoc")
1654
1655 (defun semantic-grammar-eldoc-get-macro-docstring (macro expander)
1656 "Return a one-line docstring for the given grammar MACRO.
1657 EXPANDER is the name of the function that expands MACRO."
1658 (require 'eldoc)
1659 (if (and (eq expander (aref eldoc-last-data 0))
1660 (eq 'function (aref eldoc-last-data 2)))
1661 (aref eldoc-last-data 1)
1662 (let ((doc (help-split-fundoc (documentation expander t) expander)))
1663 (cond
1664 (doc
1665 (setq doc (car doc))
1666 (string-match "\\`[^ )]* ?" doc)
1667 (setq doc (concat "(" (substring doc (match-end 0)))))
1668 (t
1669 (setq doc (eldoc-function-argstring expander))))
1670 (when doc
1671 (setq doc
1672 (eldoc-docstring-format-sym-doc
1673 macro (format "==> %s %s" expander doc) 'default))
1674 (eldoc-last-data-store expander doc 'function))
1675 doc)))
1676
1677 (define-mode-local-override semantic-idle-summary-current-symbol-info
1678 semantic-grammar-mode ()
1679 "Display additional eldoc information about grammar syntax elements.
1680 Syntax element is the current symbol at point.
1681 If it is associated a help string in `semantic-grammar-syntax-help',
1682 return that string.
1683 If it is a macro name, return a description of the associated expander
1684 function parameter list.
1685 If it is a function name, return a description of this function
1686 parameter list.
1687 It it is a variable name, return a brief (one-line) documentation
1688 string for the variable.
1689 If a default description of the current context can be obtained,
1690 return it.
1691 Otherwise return nil."
1692 (require 'eldoc)
1693 (let* ((elt (car (semantic-ctxt-current-symbol)))
1694 (val (and elt (cdr (assoc elt semantic-grammar-syntax-help)))))
1695 (when (and (not val) elt (semantic-grammar-in-lisp-p))
1696 ;; Ensure to load macro definitions before doing `intern-soft'.
1697 (setq val (semantic-grammar-macros)
1698 elt (intern-soft elt)
1699 val (and elt (cdr (assq elt val))))
1700 (cond
1701 ;; Grammar macro
1702 ((and val (fboundp val))
1703 (setq val (semantic-grammar-eldoc-get-macro-docstring elt val)))
1704 ;; Function
1705 ((and elt (fboundp elt))
1706 (setq val (eldoc-get-fnsym-args-string elt)))
1707 ;; Variable
1708 ((and elt (boundp elt))
1709 (setq val (eldoc-get-var-docstring elt)))
1710 (t nil)))
1711 (or val (semantic-idle-summary-current-symbol-info-default))))
1712
1713 (define-mode-local-override semantic-tag-boundary-p
1714 semantic-grammar-mode (tag)
1715 "Return non-nil for tags that should have a boundary drawn.
1716 Only tags of type 'nonterminal will be so marked."
1717 (let ((c (semantic-tag-class tag)))
1718 (eq c 'nonterminal)))
1719
1720 (define-mode-local-override semantic-ctxt-current-function
1721 semantic-grammar-mode (&optional point)
1722 "Determine the name of the current function at POINT."
1723 (save-excursion
1724 (and point (goto-char point))
1725 (when (semantic-grammar-in-lisp-p)
1726 (with-mode-local emacs-lisp-mode
1727 (semantic-ctxt-current-function)))))
1728
1729 (define-mode-local-override semantic-ctxt-current-argument
1730 semantic-grammar-mode (&optional point)
1731 "Determine the argument index of the called function at POINT."
1732 (save-excursion
1733 (and point (goto-char point))
1734 (when (semantic-grammar-in-lisp-p)
1735 (with-mode-local emacs-lisp-mode
1736 (semantic-ctxt-current-argument)))))
1737
1738 (define-mode-local-override semantic-ctxt-current-assignment
1739 semantic-grammar-mode (&optional point)
1740 "Determine the tag being assigned into at POINT."
1741 (save-excursion
1742 (and point (goto-char point))
1743 (when (semantic-grammar-in-lisp-p)
1744 (with-mode-local emacs-lisp-mode
1745 (semantic-ctxt-current-assignment)))))
1746
1747 (define-mode-local-override semantic-ctxt-current-class-list
1748 semantic-grammar-mode (&optional point)
1749 "Determine the class of tags that can be used at POINT."
1750 (save-excursion
1751 (and point (goto-char point))
1752 (if (semantic-grammar-in-lisp-p)
1753 (with-mode-local emacs-lisp-mode
1754 (semantic-ctxt-current-class-list))
1755 '(nonterminal keyword))))
1756
1757 (define-mode-local-override semantic-ctxt-current-mode
1758 semantic-grammar-mode (&optional point)
1759 "Return the major mode active at POINT.
1760 POINT defaults to the value of point in current buffer.
1761 Return `emacs-lisp-mode' is POINT is within Lisp code, otherwise
1762 return the current major mode."
1763 (save-excursion
1764 (and point (goto-char point))
1765 (if (semantic-grammar-in-lisp-p)
1766 'emacs-lisp-mode
1767 (semantic-ctxt-current-mode-default))))
1768
1769 (define-mode-local-override semantic-format-tag-abbreviate
1770 semantic-grammar-mode (tag &optional parent color)
1771 "Return a string abbreviation of TAG.
1772 Optional PARENT is not used.
1773 Optional COLOR is used to flag if color is added to the text."
1774 (let ((class (semantic-tag-class tag))
1775 (name (semantic-format-tag-name tag parent color)))
1776 (cond
1777 ((eq class 'nonterminal)
1778 (concat name ":"))
1779 ((eq class 'setting)
1780 "%settings%")
1781 ((memq class '(rule keyword))
1782 name)
1783 (t
1784 (concat "%" (symbol-name class) " " name)))))
1785
1786 (define-mode-local-override semantic-format-tag-summarize
1787 semantic-grammar-mode (tag &optional parent color)
1788 "Return a string summarizing TAG.
1789 Optional PARENT is not used.
1790 Optional argument COLOR determines if color is added to the text."
1791 (let ((class (semantic-tag-class tag))
1792 (name (semantic-format-tag-name tag parent color))
1793 (label nil)
1794 (desc nil))
1795 (cond
1796 ((eq class 'nonterminal)
1797 (setq label "Nonterminal: "
1798 desc (format
1799 " with %d match lists."
1800 (length (semantic-tag-components tag)))))
1801 ((eq class 'keyword)
1802 (setq label "Keyword: ")
1803 (let (summary)
1804 (semantic--find-tags-by-function
1805 #'(lambda (put)
1806 (unless summary
1807 (setq summary (cdr (assoc "summary"
1808 (semantic-tag-get-attribute
1809 put :value))))))
1810 ;; Get `put' tag with TAG name.
1811 (semantic-find-tags-by-name-regexp
1812 (regexp-quote (semantic-tag-name tag))
1813 (semantic-find-tags-by-class 'put (current-buffer))))
1814 (setq desc (concat " = "
1815 (semantic-tag-get-attribute tag :value)
1816 (if summary
1817 (concat " - " (read summary))
1818 "")))))
1819 ((eq class 'token)
1820 (setq label "Token: ")
1821 (let ((val (semantic-tag-get-attribute tag :value))
1822 (names (semantic-tag-get-attribute tag :rest))
1823 (type (semantic-tag-type tag)))
1824 (if names
1825 (setq name (mapconcat 'identity (cons name names) " ")))
1826 (setq desc (concat
1827 (if type
1828 (format " <%s>" type)
1829 "")
1830 (if val
1831 (format "%s%S" val (if type " " ""))
1832 "")))))
1833 ((eq class 'assoc)
1834 (setq label "Assoc: ")
1835 (let ((val (semantic-tag-get-attribute tag :value))
1836 (type (semantic-tag-type tag)))
1837 (setq desc (concat
1838 (if type
1839 (format " <%s>" type)
1840 "")
1841 (if val
1842 (concat " " (mapconcat 'identity val " "))
1843 "")))))
1844 (t
1845 (setq desc (semantic-format-tag-abbreviate tag parent color))))
1846 (if (and color label)
1847 (setq label (semantic--format-colorize-text label 'label)))
1848 (if (and color label desc)
1849 (setq desc (semantic--format-colorize-text desc 'comment)))
1850 (if label
1851 (concat label name desc)
1852 ;; Just a description is the abbreviated version
1853 desc)))
1854
1855 ;;; Semantic Analysis
1856
1857 (define-mode-local-override semantic-analyze-current-context
1858 semantic-grammar-mode (point)
1859 "Provide a semantic analysis object describing a context in a grammar."
1860 (require 'semantic/analyze)
1861 (if (semantic-grammar-in-lisp-p)
1862 (with-mode-local emacs-lisp-mode
1863 (semantic-analyze-current-context point))
1864
1865 (let* ((context-return nil)
1866 (prefixandbounds (semantic-ctxt-current-symbol-and-bounds))
1867 (prefix (car prefixandbounds))
1868 (bounds (nth 2 prefixandbounds))
1869 (prefixsym nil)
1870 (prefixclass (semantic-ctxt-current-class-list))
1871 )
1872
1873 ;; Do context for rules when in a match list.
1874 (setq prefixsym
1875 (semantic-find-first-tag-by-name
1876 (car prefix)
1877 (current-buffer)))
1878
1879 (setq context-return
1880 (semantic-analyze-context
1881 "context-for-semantic-grammar"
1882 :buffer (current-buffer)
1883 :scope nil
1884 :bounds bounds
1885 :prefix (if prefixsym
1886 (list prefixsym)
1887 prefix)
1888 :prefixtypes nil
1889 :prefixclass prefixclass
1890 ))
1891
1892 context-return)))
1893
1894 (define-mode-local-override semantic-analyze-possible-completions
1895 semantic-grammar-mode (context)
1896 "Return a list of possible completions based on CONTEXT."
1897 (require 'semantic/analyze/complete)
1898 (if (semantic-grammar-in-lisp-p)
1899 (with-mode-local emacs-lisp-mode
1900 (semantic-analyze-possible-completions context))
1901 (with-current-buffer (oref context buffer)
1902 (let* ((prefix (car (oref context :prefix)))
1903 (completetext (cond ((semantic-tag-p prefix)
1904 (semantic-tag-name prefix))
1905 ((stringp prefix)
1906 prefix)
1907 ((stringp (car prefix))
1908 (car prefix))))
1909 (tags (semantic-find-tags-for-completion completetext
1910 (current-buffer))))
1911 (semantic-analyze-tags-of-class-list
1912 tags (oref context prefixclass)))
1913 )))
1914
1915 (provide 'semantic/grammar)
1916
1917 ;; Local variables:
1918 ;; generated-autoload-load-name: "semantic/grammar"
1919 ;; End:
1920
1921 ;;; semantic/grammar.el ends here