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