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