1 ;;; semantic/grammar.el --- Major mode framework for Semantic grammars
3 ;; Copyright (C) 2002-2005, 2007-2013 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;; Major mode framework for editing Semantic's input grammar files.
33 (require 'semantic/wisent)
34 (require 'semantic/ctxt)
35 (require 'semantic/format)
36 (require 'semantic/grammar-wy)
37 (require 'semantic/idle)
40 (declare-function semantic-momentary-highlight-tag "semantic/decorate")
41 (declare-function semantic-analyze-context "semantic/analyze")
42 (declare-function semantic-analyze-tags-of-class-list
43 "semantic/analyze/complete")
47 (require 'semantic/edit)
48 (require 'semantic/find)
49 (require 'semantic/db))
51 (declare-function semantic-grammar-wy--install-parser "semantic/grammar-wy")
58 (defconst semantic-grammar-lex-c-char-re "'\\s\\?.'"
59 "Regexp matching C-like character literals.")
61 ;; Most of the analyzers are auto-generated from the grammar, but the
62 ;; following which need special handling code.
64 (define-lex-regex-analyzer semantic-grammar-lex-prologue
65 "Detect and create a prologue token."
67 ;; Zing to the end of this brace block.
68 (semantic-lex-push-token
72 (semantic-lex-unterminated-syntax-protection 'PROLOGUE
77 (defsubst semantic-grammar-epilogue-start ()
78 "Return the start position of the grammar epilogue."
80 (goto-char (point-min))
81 (if (re-search-forward "^\\s-*\\<%%\\>\\s-*$" nil t 2)
85 (define-lex-regex-analyzer semantic-grammar-lex-epilogue
86 "Detect and create an epilogue or percent-percent token."
88 (let ((start (match-beginning 0))
90 (class 'PERCENT_PERCENT))
91 (when (>= start (semantic-grammar-epilogue-start))
94 (semantic-lex-push-token
95 (semantic-lex-token class start end))))
97 (define-lex semantic-grammar-lexer
98 "Lexical analyzer that handles Semantic grammar buffers.
99 It ignores whitespaces, newlines and comments."
100 semantic-lex-ignore-newline
101 semantic-lex-ignore-whitespace
102 ;; Must detect prologue/epilogue before other symbols/keywords!
103 semantic-grammar-lex-prologue
104 semantic-grammar-lex-epilogue
105 semantic-grammar-wy--<keyword>-keyword-analyzer
106 semantic-grammar-wy--<symbol>-regexp-analyzer
107 semantic-grammar-wy--<char>-regexp-analyzer
108 semantic-grammar-wy--<string>-sexp-analyzer
109 ;; Must detect comments after strings because `comment-start-skip'
110 ;; regexp match semicolons inside strings!
111 semantic-lex-ignore-comments
112 ;; Must detect prefixed list before punctuation because prefix chars
113 ;; are also punctuation!
114 semantic-grammar-wy--<qlist>-sexp-analyzer
115 ;; Must detect punctuation after comments because the semicolon can
116 ;; be punctuation or a comment start!
117 semantic-grammar-wy--<punctuation>-string-analyzer
118 semantic-grammar-wy--<block>-block-analyzer
119 semantic-grammar-wy--<sexp>-sexp-analyzer)
123 (defun semantic-grammar-lex-buffer ()
124 "Run `semantic-grammar-lex' on current buffer."
127 (setq semantic-lex-analyzer 'semantic-grammar-lexer)
129 (semantic-lex (point-min) (point-max))))
130 (with-current-buffer (get-buffer-create "*semantic-grammar-lex*")
132 (pp token-stream (current-buffer))
133 (goto-char (point-min))
134 (pop-to-buffer (current-buffer)))))
137 ;;;; Semantic action expansion
140 (defun semantic-grammar-ASSOC (&rest args)
141 "Return expansion of built-in ASSOC expression.
142 ARGS are ASSOC's key value list."
144 `(semantic-tag-make-assoc-list
145 ,@(mapcar #'(lambda (i)
150 (setq key (not key))))
153 (defsubst semantic-grammar-quote-p (sym)
154 "Return non-nil if SYM is bound to the `quote' function."
156 (eq (indirect-function sym)
157 (indirect-function 'quote))
160 (defsubst semantic-grammar-backquote-p (sym)
161 "Return non-nil if SYM is bound to the `backquote' function."
163 (eq (indirect-function sym)
164 (indirect-function 'backquote))
168 ;;;; API to access grammar tags
171 (define-mode-local-override semantic-tag-components
172 semantic-grammar-mode (tag)
173 "Return the children of tag TAG."
174 (semantic-tag-get-attribute tag :children))
176 (defun semantic-grammar-first-tag-name (class)
177 "Return the name of the first tag of class CLASS found.
178 Warn if other tags of class CLASS exist."
179 (let* ((tags (semantic-find-tags-by-class
180 class (current-buffer))))
183 (semantic-tag-name (car tags))
185 (message "*** Ignore all but first declared %s"
188 (defun semantic-grammar-tag-symbols (class)
189 "Return the list of symbols defined in tags of class CLASS.
190 That is tag names plus names defined in tag attribute `:rest'."
191 (let* ((tags (semantic-find-tags-by-class
192 class (current-buffer))))
198 (cons (semantic-tag-name tag)
199 (semantic-tag-get-attribute tag :rest))))
202 (defsubst semantic-grammar-item-text (item)
203 "Return the readable string form of ITEM."
204 (if (string-match semantic-grammar-lex-c-char-re item)
205 (concat "?" (substring item 1 -1))
208 (defsubst semantic-grammar-item-value (item)
209 "Return symbol or character value of ITEM string."
210 (if (string-match semantic-grammar-lex-c-char-re item)
211 (let ((c (read (concat "?" (substring item 1 -1)))))
212 (if (featurep 'xemacs)
213 ;; Handle characters as integers in XEmacs like in GNU Emacs.
218 (defun semantic-grammar-prologue ()
219 "Return grammar prologue code as a string value."
220 (let ((tag (semantic-find-first-tag-by-name
222 (semantic-find-tags-by-class 'code (current-buffer)))))
228 (goto-char (semantic-tag-start tag))
229 (skip-chars-forward "%{\r\n\t ")
232 (goto-char (semantic-tag-end tag))
233 (skip-chars-backward "\r\n\t %}")
238 (defun semantic-grammar-epilogue ()
239 "Return grammar epilogue code as a string value."
240 (let ((tag (semantic-find-first-tag-by-name
242 (semantic-find-tags-by-class 'code (current-buffer)))))
248 (goto-char (semantic-tag-start tag))
249 (skip-chars-forward "%\r\n\t ")
252 (goto-char (semantic-tag-end tag))
253 (skip-chars-backward "\r\n\t")
254 ;; If a grammar footer is found, skip it.
255 (re-search-backward "^;;;\\s-+\\S-+\\s-+ends here"
257 (skip-chars-backward "\r\n\t")
262 (defsubst semantic-grammar-buffer-file (&optional buffer)
263 "Return name of file sans directory BUFFER is visiting.
264 No argument or nil as argument means use the current buffer."
265 (file-name-nondirectory (buffer-file-name buffer)))
267 (defun semantic-grammar-package ()
268 "Return the %package value as a string.
269 If there is no %package statement in the grammar, return a default
270 package name derived from the grammar file name. For example, the
271 default package name for the grammar file foo.wy is foo-wy, and for
272 foo.by it is foo-by."
273 (or (semantic-grammar-first-tag-name 'package)
274 (let* ((file (semantic-grammar-buffer-file))
275 (ext (file-name-extension file))
276 (i (string-match (format "\\([.]\\)%s\\'" ext) file)))
277 (concat (substring file 0 i) "-" ext))))
279 (defsubst semantic-grammar-languagemode ()
280 "Return the %languagemode value as a list of symbols or nil."
281 (semantic-grammar-tag-symbols 'languagemode))
283 (defsubst semantic-grammar-start ()
284 "Return the %start value as a list of symbols or nil."
285 (semantic-grammar-tag-symbols 'start))
287 (defsubst semantic-grammar-scopestart ()
288 "Return the %scopestart value as a symbol or nil."
289 (intern (or (semantic-grammar-first-tag-name 'scopestart) "nil")))
291 (defsubst semantic-grammar-quotemode ()
292 "Return the %quotemode value as a symbol or nil."
293 (intern (or (semantic-grammar-first-tag-name 'quotemode) "nil")))
295 (defsubst semantic-grammar-keywords ()
296 "Return the language keywords.
297 That is an alist of (VALUE . TOKEN) where VALUE is the string value of
298 the keyword and TOKEN is the terminal symbol identifying the keyword."
301 (cons (semantic-tag-get-attribute key :value)
302 (intern (semantic-tag-name key))))
303 (semantic-find-tags-by-class 'keyword (current-buffer))))
305 (defun semantic-grammar-keyword-properties (keywords)
306 "Return the list of KEYWORDS properties."
307 (let ((puts (semantic-find-tags-by-class
308 'put (current-buffer)))
309 put keys key plist assoc pkey pval props)
315 (cons (semantic-tag-name put)
316 (semantic-tag-get-attribute put :rest))))
320 assoc (rassq key keywords))
322 nil ;;(message "*** %%put to undefined keyword %s ignored" key)
323 (setq key (car assoc)
324 plist (semantic-tag-get-attribute put :value))
326 (setq pkey (intern (caar plist))
327 pval (read (cdar plist))
328 props (cons (list key pkey pval) props)
329 plist (cdr plist))))))
332 (defun semantic-grammar-tokens ()
333 "Return defined lexical tokens.
334 That is an alist (TYPE . DEFS) where type is a %token <type> symbol
335 and DEFS is an alist of (TOKEN . VALUE). TOKEN is the terminal symbol
336 identifying the token and VALUE is the string value of the token or
338 (let (tags alist assoc tag type term names value)
340 ;; Check for <type> in %left, %right & %nonassoc declarations
341 (setq tags (semantic-find-tags-by-class
342 'assoc (current-buffer)))
346 (when (setq type (semantic-tag-type tag))
347 (setq names (semantic-tag-get-attribute tag :value)
348 assoc (assoc type alist))
349 (or assoc (setq assoc (list type)
350 alist (cons assoc alist)))
352 (setq term (car names)
354 (or (string-match semantic-grammar-lex-c-char-re term)
355 (setcdr assoc (cons (list (intern term))
358 ;; Then process %token declarations so they can override any
359 ;; previous specifications
360 (setq tags (semantic-find-tags-by-class
361 'token (current-buffer)))
365 (setq names (cons (semantic-tag-name tag)
366 (semantic-tag-get-attribute tag :rest))
367 type (or (semantic-tag-type tag) "<no-type>")
368 value (semantic-tag-get-attribute tag :value)
369 assoc (assoc type alist))
370 (or assoc (setq assoc (list type)
371 alist (cons assoc alist)))
373 (setq term (intern (car names))
375 (setcdr assoc (cons (cons term value) (cdr assoc)))))
378 (defun semantic-grammar-token-%type-properties (&optional props)
379 "Return properties set by %type statements.
380 This declare a new type if necessary.
381 If optional argument PROPS is non-nil, it is an existing list of
382 properties where to add new properties."
384 (dolist (tag (semantic-find-tags-by-class 'type (current-buffer)))
385 (setq type (semantic-tag-name tag))
386 ;; Indicate to auto-generate the analyzer for this type
387 (push (list type :declared t) props)
388 (dolist (e (semantic-tag-get-attribute tag :value))
389 (push (list type (intern (car e)) (read (or (cdr e) "nil")))
393 (defun semantic-grammar-token-%put-properties (tokens)
394 "For types found in TOKENS, return properties set by %put statements."
396 (dolist (put (semantic-find-tags-by-class 'put (current-buffer)))
397 (dolist (type (cons (semantic-tag-name put)
398 (semantic-tag-get-attribute put :rest)))
399 (setq found (assoc type tokens))
401 nil ;; %put <type> ignored, no token defined
402 (setq type (car found))
403 (dolist (e (semantic-tag-get-attribute put :value))
404 (push (list type (intern (car e)) (read (or (cdr e) "nil")))
408 (defsubst semantic-grammar-token-properties (tokens)
409 "Return properties of declared types.
410 Types are explicitly declared by %type statements. Types found in
411 TOKENS are those declared implicitly by %token statements.
412 Properties can be set by %put and %type statements.
413 Properties set by %type statements take precedence over those set by
415 (let ((props (semantic-grammar-token-%put-properties tokens)))
416 (semantic-grammar-token-%type-properties props)))
418 (defun semantic-grammar-use-macros ()
419 "Return macro definitions from %use-macros statements.
420 Also load the specified macro libraries."
422 (dolist (tag (semantic-find-tags-by-class 'macro (current-buffer)))
423 (setq lib (intern (semantic-tag-type tag)))
425 ;;(load lib) ;; Be sure to use the latest macro library.
428 (dolist (mac (semantic-tag-get-attribute tag :value))
429 (push (cons (intern mac)
430 (intern (format "%s-%s" lib mac)))
434 (defvar semantic-grammar-macros nil
435 "List of associations (MACRO-NAME . EXPANDER).")
436 (make-variable-buffer-local 'semantic-grammar-macros)
438 (defun semantic-grammar-macros ()
439 "Build and return the alist of defined macros."
441 ;; Definitions found in tags.
442 (semantic-grammar-use-macros)
443 ;; Other pre-installed definitions.
444 semantic-grammar-macros))
447 ;;;; Overloaded functions that build parser data.
450 ;;; Keyword table builder
452 (defun semantic-grammar-keywordtable-builder-default ()
453 "Return the default value of the keyword table."
454 (let ((keywords (semantic-grammar-keywords)))
455 `(semantic-lex-make-keyword-table
457 ',(semantic-grammar-keyword-properties keywords))))
459 (define-overloadable-function semantic-grammar-keywordtable-builder ()
460 "Return the keyword table value.")
462 ;;; Token table builder
464 (defun semantic-grammar-tokentable-builder-default ()
465 "Return the default value of the table of lexical tokens."
466 (let ((tokens (semantic-grammar-tokens)))
467 `(semantic-lex-make-type-table
469 ',(semantic-grammar-token-properties tokens))))
471 (define-overloadable-function semantic-grammar-tokentable-builder ()
472 "Return the value of the table of lexical tokens.")
474 ;;; Parser table builder
476 (defun semantic-grammar-parsetable-builder-default ()
477 "Return the default value of the parse table."
478 (error "`semantic-grammar-parsetable-builder' not defined"))
480 (define-overloadable-function semantic-grammar-parsetable-builder ()
481 "Return the parser table value.")
483 ;;; Parser setup code builder
485 (defun semantic-grammar-setupcode-builder-default ()
486 "Return the default value of the setup code form."
487 (error "`semantic-grammar-setupcode-builder' not defined"))
489 (define-overloadable-function semantic-grammar-setupcode-builder ()
490 "Return the parser setup code form.")
493 ;;;; Lisp code generation
495 (defvar semantic--grammar-input-buffer nil)
496 (defvar semantic--grammar-output-buffer nil)
497 (defvar semantic--grammar-package nil)
498 (defvar semantic--grammar-provide nil)
500 (defsubst semantic-grammar-keywordtable ()
501 "Return the variable name of the keyword table."
502 (concat semantic--grammar-package
505 (defsubst semantic-grammar-tokentable ()
506 "Return the variable name of the token table."
507 (concat semantic--grammar-package
510 (defsubst semantic-grammar-parsetable ()
511 "Return the variable name of the parse table."
512 (concat semantic--grammar-package
515 (defsubst semantic-grammar-setupfunction ()
516 "Return the name of the parser setup function."
517 (concat semantic--grammar-package
520 (defmacro semantic-grammar-as-string (object)
521 "Return OBJECT as a string value."
522 `(if (stringp ,object)
525 (pp-to-string ,object)))
527 (defun semantic-grammar-insert-defconst (name value docstring)
528 "Insert declaration of constant NAME with VALUE and DOCSTRING."
529 (let ((start (point)))
530 (insert (format "(defconst %s\n%s%S)\n\n" name value docstring))
535 (defun semantic-grammar-insert-defun (name body docstring)
536 "Insert declaration of function NAME with BODY and DOCSTRING."
537 (let ((start (point)))
538 (insert (format "(defun %s ()\n%S\n%s)\n\n" name docstring body))
543 (defun semantic-grammar-insert-define (define)
544 "Insert the declaration specified by DEFINE expression.
545 Typically a DEFINE expression should look like this:
547 \(define-thing name docstring expression1 ...)"
549 (let ((start (point)))
550 (insert (format "(%S %S" (car define) (nth 1 define)))
551 (dolist (item (nthcdr 2 define))
554 (pp item (current-buffer)))
560 (defconst semantic-grammar-header-template
562 ;;; " file " --- Generated parser support file
566 ;; Author: " user-full-name " <" user-mail-address ">
571 ;; This file is not part of GNU Emacs.
573 ;; This program is free software; you can redistribute it and/or
574 ;; modify it under the terms of the GNU General Public License as
575 ;; published by the Free Software Foundation, either version 3 of
576 ;; the License, or (at your option) any later version.
578 ;; This software is distributed in the hope that it will be useful,
579 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
580 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
581 ;; General Public License for more details.
583 ;; You should have received a copy of the GNU General Public License
584 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
588 ;; PLEASE DO NOT MANUALLY EDIT THIS FILE! It is automatically
589 ;; generated from the grammar file " gram ".
596 (require 'semantic/lex)
597 (eval-when-compile (require 'semantic/bovine))
599 "Generated header template.
600 The symbols in the template are local variables in
601 `semantic-grammar-header'")
603 (defconst semantic-grammar-footer-template
608 ;;; " file " ends here
610 "Generated footer template.
611 The symbols in the list are local variables in
612 `semantic-grammar-footer'.")
614 (defun semantic-grammar-copyright-line ()
615 "Return the grammar copyright line, or nil if not found."
617 (goto-char (point-min))
618 (when (re-search-forward "^;;+[ \t]+Copyright (C) .*$"
619 ;; Search only in the four top lines
620 (save-excursion (forward-line 4) (point))
624 (defun semantic-grammar-header ()
625 "Return text of a generated standard header."
626 (let ((file (semantic-grammar-buffer-file
627 semantic--grammar-output-buffer))
628 (gram (semantic-grammar-buffer-file))
629 (date (format-time-string "%Y-%m-%d %T%z"))
630 (vcid (concat "$" "Id" "$")) ;; Avoid expansion
631 ;; Try to get the copyright from the input grammar, or
632 ;; generate a new one if not found.
633 (copy (or (semantic-grammar-copyright-line)
634 (concat (format-time-string ";; Copyright (C) %Y ")
637 (dolist (S semantic-grammar-header-template)
639 (setq out (concat out S)))
641 (setq out (concat out (symbol-value S))))))
644 (defun semantic-grammar-footer ()
645 "Return text of a generated standard footer."
646 (let* ((file (semantic-grammar-buffer-file
647 semantic--grammar-output-buffer))
648 (libr (or semantic--grammar-provide
649 semantic--grammar-package))
651 (dolist (S semantic-grammar-footer-template)
653 (setq out (concat out S)))
655 (setq out (concat out (symbol-value S))))))
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)))
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)))
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)))
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)))
678 ;;; Generation of lexical analyzers.
680 (defvar semantic-grammar--lex-block-specs)
682 (defsubst semantic-grammar--lex-delim-spec (block-spec)
683 "Return delimiters specification from BLOCK-SPEC."
685 (let* ((standard-input (cdr block-spec))
687 (if (and (consp delim-spec)
688 (car delim-spec) (symbolp (car delim-spec))
689 (cadr delim-spec) (symbolp (cadr delim-spec)))
691 (error "Invalid delimiter")))
693 (error "Invalid delimiters specification %s in block token %s"
694 (cdr block-spec) (car block-spec)))))
696 (defun semantic-grammar--lex-block-specs ()
697 "Compute lexical block specifications for the current buffer.
698 Block definitions are read from the current table of lexical types."
700 ;; Block specifications have been parsed and are invalid.
701 ((eq semantic-grammar--lex-block-specs 'error)
704 ;; Parse block specifications.
705 ((null semantic-grammar--lex-block-specs)
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))
716 (error "Missing open-paren token %s required by block %s"
717 (car delim-spec) (car block-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))
724 ;; build alist ((CLOSE-DELIM CLOSE-SYM) ...)
725 (push (list (cdr close-spec) (car close-spec))
727 (setq semantic-grammar--lex-block-specs (cons olist clist)))
729 (setq semantic-grammar--lex-block-specs 'error)
730 (message "%s" (error-message-string err))
733 ;; Block specifications already parsed.
735 semantic-grammar--lex-block-specs)))
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)))
744 (defun semantic-grammar-insert-defanalyzer (type)
745 "Insert declaration of the lexical analyzer defined with TYPE."
746 (let* ((type-name (symbol-name type))
747 (type-value (symbol-value type))
748 (syntax (get type 'syntax))
749 (declared (get type :declared))
750 spec mtype prefix name doc)
751 ;; Generate an analyzer if the corresponding type has been
752 ;; explicitly declared in a %type statement, and if at least the
753 ;; syntax property has been provided.
754 (when (and declared syntax)
755 (setq prefix semantic--grammar-package
756 mtype (or (get type 'matchdatatype) 'regexp)
757 name (intern (format "%s--<%s>-%s-analyzer" prefix type mtype))
758 doc (format "%s analyzer for <%s> tokens." mtype type))
760 ;; Regexp match analyzer
762 (semantic-grammar-insert-define
763 `(define-lex-regex-type-analyzer ,name
765 ,(semantic-grammar-quoted-form (cdr type-value))
766 ',(or (car type-value) (intern type-name))))
768 ;; String compare analyzer
770 (semantic-grammar-insert-define
771 `(define-lex-string-type-analyzer ,name
773 ,(semantic-grammar-quoted-form (cdr type-value))
774 ',(or (car type-value) (intern type-name))))
777 ((and (eq mtype 'block)
778 (setq spec (semantic-grammar--lex-block-specs)))
779 (semantic-grammar-insert-define
780 `(define-lex-block-type-analyzer ,name
782 ,(semantic-grammar-quoted-form spec)))
786 (semantic-grammar-insert-define
787 `(define-lex-sexp-type-analyzer ,name
789 ',(or (car type-value) (intern type-name))))
793 (semantic-grammar-insert-define
794 `(define-lex-keyword-type-analyzer ,name
800 (defun semantic-grammar-insert-defanalyzers ()
801 "Insert declarations of lexical analyzers."
803 (with-current-buffer semantic--grammar-input-buffer
804 (setq tokens (semantic-grammar-tokens)
805 props (semantic-grammar-token-properties tokens)))
806 (let ((semantic-lex-types-obarray
807 (semantic-lex-make-type-table tokens props))
808 semantic-grammar--lex-block-specs)
809 (mapatoms 'semantic-grammar-insert-defanalyzer
810 semantic-lex-types-obarray))))
812 ;;; Generation of the grammar support file.
814 (defcustom semantic-grammar-file-regexp "\\.[wb]y$"
815 "Regexp which matches grammar source files."
819 (defsubst semantic-grammar-noninteractive ()
820 "Return non-nil if running without interactive terminal."
821 (if (featurep 'xemacs)
825 (defun semantic-grammar-create-package (&optional force)
826 "Create package Lisp code from grammar in current buffer.
827 Does nothing if the Lisp code seems up to date.
828 If optional argument FORCE is non-nil, unconditionally re-generate the
831 (unless (semantic-active-p)
832 (error "You have to activate semantic-mode to create a package."))
833 (setq force (or force current-prefix-arg))
834 (semantic-fetch-tags)
836 ;; Values of the following local variables are obtained from
837 ;; the grammar parsed tree in current buffer, that is before
838 ;; switching to the output file.
839 (semantic--grammar-package (semantic-grammar-package))
840 (semantic--grammar-provide (semantic-grammar-first-tag-name 'provide))
841 (output (concat (or semantic--grammar-provide
842 semantic--grammar-package) ".el"))
843 (semantic--grammar-input-buffer (current-buffer))
844 (semantic--grammar-output-buffer
846 (file-name-nondirectory output)))
847 (header (semantic-grammar-header))
848 (prologue (semantic-grammar-prologue))
849 (epilogue (semantic-grammar-epilogue))
850 (footer (semantic-grammar-footer))
853 (not (buffer-modified-p))
854 (file-newer-than-file-p
855 (buffer-file-name semantic--grammar-output-buffer)
856 (buffer-file-name semantic--grammar-input-buffer)))
857 (message "Package `%s' is up to date." semantic--grammar-package)
858 ;; Create the package
859 (set-buffer semantic--grammar-output-buffer)
860 ;; Use Unix EOLs, so that the file is portable to all platforms.
861 (setq buffer-file-coding-system 'raw-text-unix)
863 (unless (eq major-mode 'emacs-lisp-mode)
866 ;;;; Header + Prologue
869 "
\f\n;;; Prologue\n;;\n"
872 ;; Evaluate the prologue now, because it might provide definition
873 ;; of grammar macro expanders.
874 (eval-region (point-min) (point))
880 (insert "
\f\n;;; Declarations\n;;\n")
882 ;; `eval-defun' is not necessary to reset `defconst' values.
883 (semantic-grammar-insert-defconst
884 (semantic-grammar-keywordtable)
885 (with-current-buffer semantic--grammar-input-buffer
886 (semantic-grammar-keyword-data))
887 "Table of language keywords.")
889 (semantic-grammar-insert-defconst
890 (semantic-grammar-tokentable)
891 (with-current-buffer semantic--grammar-input-buffer
892 (semantic-grammar-token-data))
893 "Table of lexical tokens.")
895 (semantic-grammar-insert-defconst
896 (semantic-grammar-parsetable)
897 (with-current-buffer semantic--grammar-input-buffer
898 (semantic-grammar-parser-data))
901 (semantic-grammar-insert-defun
902 (semantic-grammar-setupfunction)
903 (with-current-buffer semantic--grammar-input-buffer
904 (semantic-grammar-setup-data))
905 "Setup the Semantic Parser.")
908 (insert "
\f\n;;; Analyzers\n;;\n")
910 (semantic-grammar-insert-defanalyzers)
912 ;;;; Epilogue & Footer
914 (insert "
\f\n;;; Epilogue\n;;\n"
923 ;; If running in batch mode, there is nothing more to do.
924 ;; Save the generated file and quit.
925 (if (semantic-grammar-noninteractive)
926 (let ((version-control t)
927 (delete-old-versions t)
928 (make-backup-files t)
929 (vc-make-backup-files t))
930 (kill-buffer (current-buffer)))
931 ;; If running interactively, eval declarations and epilogue
932 ;; code, then pop to the buffer visiting the generated file.
933 (eval-region (point) (point-max))
934 ;; Loop over the defvars and eval them explicitly to force
935 ;; them to be evaluated and ready to use.
936 (goto-char (point-min))
937 (while (re-search-forward "(defvar " nil t)
939 ;; Move cursor to a logical spot in the generated code.
940 (goto-char (point-min))
941 (pop-to-buffer (current-buffer))
942 ;; The generated code has been evaluated and updated into
943 ;; memory. Now find all buffers that match the major modes we
944 ;; have created this language for, and force them to call our
945 ;; setup function again, refreshing all semantic data, and
946 ;; enabling them to work with the new code just created.
948 ;; At this point, I don't know any user's defined setup code :-(
949 ;; At least, what I can do for now, is to run the generated
950 ;; parser-install function.
951 (semantic-map-mode-buffers
952 (semantic-grammar-setupfunction)
953 (semantic-grammar-languagemode)))
955 ;; Return the name of the generated package file.
958 (defun semantic-grammar-recreate-package ()
959 "Unconditionally create Lisp code from grammar in current buffer.
960 Like \\[universal-argument] \\[semantic-grammar-create-package]."
962 (semantic-grammar-create-package t))
964 (defun semantic-grammar-batch-build-one-package (file)
965 "Build a Lisp package from the grammar in FILE.
966 That is, generate Lisp code from FILE, and `byte-compile' it.
967 Return non-nil if there were no errors, nil if errors."
968 ;; We need this require so that we can find `byte-compile-dest-file'.
970 (unless (auto-save-file-name-p file)
971 ;; Create the package
974 (with-current-buffer (find-file-noselect file)
975 (let ((semantic-new-buffer-setup-functions nil)
976 (vc-handled-backends nil))
977 (setq semanticdb-new-database-class 'semanticdb-project-database)
979 (semantic-grammar-create-package)))
981 (message "%s" (error-message-string err))
984 ;; Only byte compile if out of date
985 (if (file-newer-than-file-p
986 packagename (byte-compile-dest-file packagename))
987 (let (;; Some complex grammar table expressions need a few
988 ;; more resources than the default.
989 (max-specpdl-size (max 3000 max-specpdl-size))
990 (max-lisp-eval-depth (max 1000 max-lisp-eval-depth))
992 ;; byte compile the resultant file
993 (byte-compile-file packagename))
996 (defun semantic-grammar-batch-build-packages ()
997 "Build Lisp packages from grammar files on the command line.
998 That is, run `semantic-grammar-batch-build-one-package' for each file.
999 Each file is processed even if an error occurred previously.
1000 Must be used from the command line, with `-batch'.
1001 For example, to process grammar files in current directory, invoke:
1003 \"emacs -batch -f semantic-grammar-batch-build-packages .\".
1005 See also the variable `semantic-grammar-file-regexp'."
1006 (or (semantic-grammar-noninteractive)
1008 `semantic-grammar-batch-build-packages' must be used with -batch"
1011 ;; Remove vc from find-file-hook. It causes bad stuff to
1012 ;; happen in Emacs 20.
1013 (find-file-hook (delete 'vc-find-file-hook find-file-hook)))
1014 (dolist (arg command-line-args-left)
1015 (unless (and arg (file-exists-p arg))
1016 (error "Argument %s is not a valid file name" arg))
1017 (setq arg (expand-file-name arg))
1018 (if (file-directory-p arg)
1019 ;; Directory as argument
1020 (dolist (src (condition-case nil
1022 arg nil semantic-grammar-file-regexp)
1024 (error "Unable to read directory files"))))
1025 (or (semantic-grammar-batch-build-one-package
1026 (expand-file-name src arg))
1028 ;; Specific file argument
1029 (or (semantic-grammar-batch-build-one-package arg)
1035 ;;;; Macros highlighting
1038 (defvar semantic--grammar-macros-regexp-1 nil)
1039 (make-variable-buffer-local 'semantic--grammar-macros-regexp-1)
1041 (defun semantic--grammar-macros-regexp-1 ()
1042 "Return font-lock keyword regexp for pre-installed macro names."
1043 (and semantic-grammar-macros
1044 (not semantic--grammar-macros-regexp-1)
1046 (setq semantic--grammar-macros-regexp-1
1049 (mapcar #'(lambda (e) (symbol-name (car e)))
1050 semantic-grammar-macros)
1054 semantic--grammar-macros-regexp-1)
1056 (defconst semantic--grammar-macdecl-re
1057 "\\<%use-macros\\>[ \t\r\n]+\\(\\sw\\|\\s_\\)+[ \t\r\n]+{"
1058 "Regexp that matches a macro declaration statement.")
1060 (defvar semantic--grammar-macros-regexp-2 nil)
1061 (make-variable-buffer-local 'semantic--grammar-macros-regexp-2)
1063 (defun semantic--grammar-clear-macros-regexp-2 (&rest ignore)
1064 "Clear the cached regexp that match macros local in this grammar.
1066 Added to `before-change-functions' hooks to be run before each text
1068 (setq semantic--grammar-macros-regexp-2 nil))
1070 (defun semantic--grammar-macros-regexp-2 ()
1071 "Return the regexp that match macros local in this grammar."
1072 (unless semantic--grammar-macros-regexp-2
1075 (goto-char (point-min))
1076 (while (re-search-forward semantic--grammar-macdecl-re nil t)
1078 (setq macs (nconc macs
1080 (buffer-substring-no-properties
1089 (setq semantic--grammar-macros-regexp-2
1090 (concat "(\\s-*" (regexp-opt macs t) "\\>"))))))
1091 semantic--grammar-macros-regexp-2)
1093 (defun semantic--grammar-macros-matcher (end)
1094 "Search for a grammar macro name to highlight.
1095 END is the limit of the search."
1096 (let ((regexp (semantic--grammar-macros-regexp-1)))
1097 (or (and regexp (re-search-forward regexp end t))
1098 (and (setq regexp (semantic--grammar-macros-regexp-2))
1099 (re-search-forward regexp end t)))))
1102 ;;;; Define major mode
1105 (defvar semantic-grammar-syntax-table
1106 (let ((table (make-syntax-table (standard-syntax-table))))
1107 (modify-syntax-entry ?\: "." table) ;; COLON
1108 (modify-syntax-entry ?\> "." table) ;; GT
1109 (modify-syntax-entry ?\< "." table) ;; LT
1110 (modify-syntax-entry ?\| "." table) ;; OR
1111 (modify-syntax-entry ?\; ". 12" table) ;; SEMI, Comment start ;;
1112 (modify-syntax-entry ?\n ">" table) ;; Comment end
1113 (modify-syntax-entry ?\" "\"" table) ;; String
1114 (modify-syntax-entry ?\% "w" table) ;; Word
1115 (modify-syntax-entry ?\- "_" table) ;; Symbol
1116 (modify-syntax-entry ?\. "_" table) ;; Symbol
1117 (modify-syntax-entry ?\\ "\\" table) ;; Quote
1118 (modify-syntax-entry ?\` "'" table) ;; Prefix ` (backquote)
1119 (modify-syntax-entry ?\' "'" table) ;; Prefix ' (quote)
1120 (modify-syntax-entry ?\, "'" table) ;; Prefix , (comma)
1121 (modify-syntax-entry ?\# "'" table) ;; Prefix # (sharp)
1123 "Syntax table used in a Semantic grammar buffers.")
1125 (defvar semantic-grammar-mode-hook nil
1126 "Hook run when starting Semantic grammar mode.")
1128 (defvar semantic-grammar-mode-keywords-1
1129 `(("\\(\\<%%\\>\\|\\<%[{}]\\)"
1130 0 font-lock-reference-face)
1131 ("\\(%\\)\\(\\(\\sw\\|\\s_\\)+\\)"
1132 (1 font-lock-reference-face)
1133 (2 font-lock-keyword-face))
1135 0 (unless (semantic-grammar-in-lisp-p) 'bold))
1136 ("^\\(\\(\\sw\\|\\s_\\)+\\)[ \n\r\t]*:"
1137 1 font-lock-function-name-face)
1138 (semantic--grammar-macros-matcher
1139 1 ,(if (boundp 'font-lock-builtin-face)
1140 'font-lock-builtin-face
1141 'font-lock-preprocessor-face))
1142 ("\\$\\(\\sw\\|\\s_\\)*"
1143 0 font-lock-variable-name-face)
1144 ("<\\(\\(\\sw\\|\\s_\\)+\\)>"
1145 1 font-lock-type-face)
1146 (,semantic-grammar-lex-c-char-re
1147 0 ,(if (boundp 'font-lock-constant-face)
1148 'font-lock-constant-face
1149 'font-lock-string-face) t)
1150 ;; Must highlight :keyword here, because ':' is a punctuation in
1152 ("[\r\n\t ]+:\\sw+\\>"
1153 0 font-lock-builtin-face)
1154 ;; ;; Append the Semantic keywords
1155 ;; ,@semantic-fw-font-lock-keywords
1157 "Font Lock keywords used to highlight Semantic grammar buffers.")
1159 (defvar semantic-grammar-mode-keywords-2
1160 (append semantic-grammar-mode-keywords-1
1161 lisp-font-lock-keywords-1)
1162 "Font Lock keywords used to highlight Semantic grammar buffers.")
1164 (defvar semantic-grammar-mode-keywords-3
1165 (append semantic-grammar-mode-keywords-1
1166 lisp-font-lock-keywords-2)
1167 "Font Lock keywords used to highlight Semantic grammar buffers.")
1169 (defvar semantic-grammar-mode-keywords
1170 semantic-grammar-mode-keywords-1
1171 "Font Lock keywords used to highlight Semantic grammar buffers.")
1173 (defvar semantic-grammar-map
1174 (let ((km (make-sparse-keymap)))
1176 (define-key km "|" 'semantic-grammar-electric-punctuation)
1177 (define-key km ";" 'semantic-grammar-electric-punctuation)
1178 (define-key km "%" 'semantic-grammar-electric-punctuation)
1179 (define-key km "(" 'semantic-grammar-electric-punctuation)
1180 (define-key km ")" 'semantic-grammar-electric-punctuation)
1181 (define-key km ":" 'semantic-grammar-electric-punctuation)
1183 (define-key km "\t" 'semantic-grammar-indent)
1184 (define-key km "\M-\t" 'semantic-grammar-complete)
1185 (define-key km "\C-c\C-c" 'semantic-grammar-create-package)
1186 (define-key km "\C-cm" 'semantic-grammar-find-macro-expander)
1187 (define-key km "\C-cik" 'semantic-grammar-insert-keyword)
1188 ;; (define-key km "\C-cc" 'semantic-grammar-generate-and-load)
1189 ;; (define-key km "\C-cr" 'semantic-grammar-generate-one-rule)
1192 "Keymap used in `semantic-grammar-mode'.")
1194 (defvar semantic-grammar-menu
1196 ["Indent Line" semantic-grammar-indent]
1197 ["Complete Symbol" semantic-grammar-complete]
1198 ["Find Macro" semantic-grammar-find-macro-expander]
1200 ["Insert %keyword" semantic-grammar-insert-keyword]
1202 ["Update Lisp Package" semantic-grammar-create-package]
1203 ["Recreate Lisp Package" semantic-grammar-recreate-package]
1205 "Common semantic grammar menu.")
1207 (defun semantic-grammar-setup-menu-emacs (symbol mode-menu)
1208 "Setup a GNU Emacs grammar menu in variable SYMBOL.
1209 MODE-MENU is an optional specific menu whose items are appended to the
1210 common grammar menu."
1211 (let ((items (make-symbol "items")))
1212 `(unless (boundp ',symbol)
1213 (easy-menu-define ,symbol (current-local-map)
1214 "Grammar Menu" semantic-grammar-menu)
1215 (let ((,items (cdr ,mode-menu)))
1217 (easy-menu-add-item ,symbol nil "--")
1219 (easy-menu-add-item ,symbol nil (car ,items))
1220 (setq ,items (cdr ,items))))))
1223 (defun semantic-grammar-setup-menu-xemacs (symbol mode-menu)
1224 "Setup an XEmacs grammar menu in variable SYMBOL.
1225 MODE-MENU is an optional specific menu whose items are appended to the
1226 common grammar menu."
1227 (let ((items (make-symbol "items"))
1228 (path (make-symbol "path")))
1230 (unless (boundp ',symbol)
1231 (easy-menu-define ,symbol nil
1232 "Grammar Menu" (copy-sequence semantic-grammar-menu)))
1233 (easy-menu-add ,symbol)
1234 (let ((,items (cdr ,mode-menu))
1235 (,path (list (car ,symbol))))
1237 (easy-menu-add-item nil ,path "--")
1239 (easy-menu-add-item nil ,path (car ,items))
1240 (setq ,items (cdr ,items))))))
1243 (defmacro semantic-grammar-setup-menu (&optional mode-menu)
1244 "Setup a mode local grammar menu.
1245 MODE-MENU is an optional specific menu whose items are appended to the
1246 common grammar menu."
1247 (let ((menu (intern (format "%s-menu" major-mode))))
1248 (if (featurep 'xemacs)
1249 (semantic-grammar-setup-menu-xemacs menu mode-menu)
1250 (semantic-grammar-setup-menu-emacs menu mode-menu))))
1252 (defsubst semantic-grammar-in-lisp-p ()
1253 "Return non-nil if point is in Lisp code."
1254 (or (>= (point) (semantic-grammar-epilogue-start))
1261 (defun semantic-grammar-edits-new-change-hook-fcn (overlay)
1262 "Function set into `semantic-edits-new-change-hook'.
1263 Argument OVERLAY is the overlay created to mark the change.
1264 When OVERLAY marks a change in the scope of a nonterminal tag extend
1265 the change bounds to encompass the whole nonterminal tag."
1266 (let ((outer (car (semantic-find-tag-by-overlay-in-region
1267 (semantic-edits-os overlay)
1268 (semantic-edits-oe overlay)))))
1269 (if (semantic-tag-of-class-p outer 'nonterminal)
1270 (semantic-overlay-move overlay
1271 (semantic-tag-start outer)
1272 (semantic-tag-end outer)))))
1274 (defun semantic-grammar-mode ()
1275 "Initialize a buffer for editing Semantic grammars.
1277 \\{semantic-grammar-map}"
1279 (kill-all-local-variables)
1280 (setq major-mode 'semantic-grammar-mode
1281 mode-name "Semantic Grammar Framework")
1282 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1283 (set (make-local-variable 'comment-start) ";;")
1284 ;; Look within the line for a ; following an even number of backslashes
1285 ;; after either a non-backslash or the line beginning.
1286 (set (make-local-variable 'comment-start-skip)
1287 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
1288 (set-syntax-table semantic-grammar-syntax-table)
1289 (use-local-map semantic-grammar-map)
1290 (set (make-local-variable 'indent-line-function)
1291 'semantic-grammar-indent)
1292 (set (make-local-variable 'fill-paragraph-function)
1293 'lisp-fill-paragraph)
1294 (set (make-local-variable 'font-lock-multiline)
1296 (set (make-local-variable 'font-lock-defaults)
1297 '((semantic-grammar-mode-keywords
1298 semantic-grammar-mode-keywords-1
1299 semantic-grammar-mode-keywords-2
1300 semantic-grammar-mode-keywords-3)
1301 nil ;; perform string/comment fontification
1302 nil ;; keywords are case sensitive.
1303 ;; This puts _ & - as a word constituent,
1304 ;; simplifying our keywords significantly
1305 ((?_ . "w") (?- . "w"))))
1306 ;; Setup Semantic to parse grammar
1307 (semantic-grammar-wy--install-parser)
1308 (setq semantic-lex-comment-regex ";;"
1309 semantic-lex-analyzer 'semantic-grammar-lexer
1310 semantic-type-relation-separator-character '(":")
1311 semantic-symbol->name-assoc-list
1313 (code . "Setup Code")
1314 (keyword . "Keyword")
1316 (nonterminal . "Nonterminal")
1319 (set (make-local-variable 'semantic-format-face-alist)
1322 (keyword . font-lock-keyword-face)
1323 (token . font-lock-type-face)
1324 (nonterminal . font-lock-function-name-face)
1327 (set (make-local-variable 'semantic-stickyfunc-sticky-classes)
1329 ;; Before each change, clear the cached regexp used to highlight
1330 ;; macros local in this grammar.
1331 (semantic-make-local-hook 'before-change-functions)
1332 (add-hook 'before-change-functions
1333 'semantic--grammar-clear-macros-regexp-2 nil t)
1334 ;; Handle safe re-parse of grammar rules.
1335 (semantic-make-local-hook 'semantic-edits-new-change-functions)
1336 (add-hook 'semantic-edits-new-change-functions
1337 'semantic-grammar-edits-new-change-hook-fcn
1339 (semantic-run-mode-hooks 'semantic-grammar-mode-hook))
1342 ;;;; Useful commands
1345 (defvar semantic-grammar-skip-quoted-syntax-table
1346 (let ((st (copy-syntax-table semantic-grammar-syntax-table)))
1347 (modify-syntax-entry ?\' "$" st)
1349 "Syntax table to skip a whole quoted expression in grammar code.
1350 Consider quote as a \"paired delimiter\", so `forward-sexp' will skip
1351 whole quoted expression.")
1353 (defsubst semantic-grammar-backward-item ()
1354 "Move point to beginning of the previous grammar item."
1355 (forward-comment (- (point-max)))
1356 (if (zerop (skip-syntax-backward "."))
1357 (if (eq (char-before) ?\')
1359 ;; Can't be Lisp code here! Temporarily consider quote
1360 ;; as a "paired delimiter", so `forward-sexp' can skip
1361 ;; the whole quoted expression.
1362 semantic-grammar-skip-quoted-syntax-table
1364 (forward-sexp -1))))
1366 (defun semantic-grammar-anchored-indentation ()
1367 "Return indentation based on previous anchor character found."
1371 (semantic-grammar-backward-item)
1375 ((looking-at ":\\(\\s-\\|$\\)")
1376 (setq indent (current-column))
1378 (skip-syntax-forward "-")
1379 (if (eolp) (setq indent 2))
1381 ((and (looking-at "[;%]")
1382 (not (looking-at "\\<%prec\\>")))
1387 (defun semantic-grammar-do-grammar-indent ()
1388 "Indent a line of grammar.
1389 When called the point is not in Lisp code."
1393 (skip-syntax-forward "-")
1394 (setq indent (current-column))
1397 (looking-at "\\(\\w\\|\\s_\\)+\\s-*:")
1398 (and (looking-at "%")
1399 (not (looking-at "%prec\\>"))))
1403 ((and (looking-at ";;")
1404 (save-excursion (forward-comment (point-max))
1408 (setq n (semantic-grammar-anchored-indentation))
1413 ((looking-at "[|;]")
1416 (setq n (+ n 2)))))))
1419 (delete-horizontal-space)
1422 (defvar semantic-grammar-brackets-as-parens-syntax-table
1423 (let ((st (copy-syntax-table emacs-lisp-mode-syntax-table)))
1424 (modify-syntax-entry ?\{ "(} " st)
1425 (modify-syntax-entry ?\} "){ " st)
1427 "Syntax table that consider brackets as parenthesis.
1428 So `lisp-indent-line' will work inside bracket blocks.")
1430 (defun semantic-grammar-do-lisp-indent ()
1431 "Maybe run the Emacs Lisp indenter on a line of code.
1432 Return nil if not in a Lisp expression."
1436 (skip-chars-forward "\t ")
1437 (let ((first (point)))
1438 (or (>= first (semantic-grammar-epilogue-start))
1446 (narrow-to-region (point) first)
1447 (goto-char (point-max))
1449 ;; Temporarily consider brackets as parenthesis so
1450 ;; `lisp-indent-line' can indent Lisp code inside
1452 semantic-grammar-brackets-as-parens-syntax-table
1453 (lisp-indent-line))))
1457 (defun semantic-grammar-indent ()
1458 "Indent the current line.
1459 Use the Lisp or grammar indenter depending on point location."
1461 (let ((orig (point))
1463 (or (semantic-grammar-do-lisp-indent)
1464 (semantic-grammar-do-grammar-indent))
1465 (setq first (save-excursion
1467 (skip-chars-forward "\t ")
1469 (if (or (< orig first) (/= orig (point)))
1470 (goto-char first))))
1472 (defun semantic-grammar-electric-punctuation ()
1473 "Insert and reindent for the symbol just typed in."
1475 (self-insert-command 1)
1477 (semantic-grammar-indent)))
1479 (defun semantic-grammar-complete ()
1480 "Attempt to complete the symbol under point.
1481 Completion is position sensitive. If the cursor is in a match section of
1482 a rule, then nonterminals symbols are scanned. If the cursor is in a Lisp
1483 expression then Lisp symbols are completed."
1485 (if (semantic-grammar-in-lisp-p)
1486 ;; We are in lisp code. Do lisp completion.
1487 (lisp-complete-symbol)
1488 ;; We are not in lisp code. Do rule completion.
1489 (let* ((nonterms (semantic-find-tags-by-class 'nonterminal (current-buffer)))
1490 (sym (car (semantic-ctxt-current-symbol)))
1491 (ans (try-completion sym nonterms)))
1494 (message "Symbols is already complete"))
1495 ((and (stringp ans) (string= ans sym))
1496 ;; Max matchable. Show completions.
1497 (with-output-to-temp-buffer "*Completions*"
1498 (display-completion-list (all-completions sym nonterms)))
1501 ;; Expand the completions
1503 (delete-region (point) (progn (forward-sexp 1) (point)))
1505 (t (message "No Completions."))
1509 (defun semantic-grammar-insert-keyword (name)
1510 "Insert a new %keyword declaration with NAME.
1511 Assumes it is typed in with the correct casing."
1512 (interactive "sKeyword: ")
1513 (if (not (bolp)) (insert "\n"))
1514 (insert "%keyword " (upcase name) " \"" name "\"
1515 %put " (upcase name) " summary
1519 ;;; Macro facilities
1522 (defsubst semantic--grammar-macro-function-tag (name)
1523 "Search for a function tag for the grammar macro with name NAME.
1524 Return the tag found or nil if not found."
1525 (car (semantic-find-tags-by-class
1527 (or (semantic-find-tags-by-name name (current-buffer))
1528 (and (featurep 'semantic/db)
1529 semanticdb-current-database
1530 (cdar (semanticdb-find-tags-by-name name nil t)))))))
1532 (defsubst semantic--grammar-macro-lib-part (def)
1533 "Return the library part of the grammar macro defined by DEF."
1534 (let ((suf (format "-%s\\'" (regexp-quote (symbol-name (car def)))))
1535 (fun (symbol-name (cdr def))))
1536 (substring fun 0 (string-match suf fun))))
1538 (defun semantic--grammar-macro-compl-elt (def &optional full)
1539 "Return a completion entry for the grammar macro defined by DEF.
1540 If optional argument FULL is non-nil qualify the macro name with the
1541 library found in DEF."
1542 (let ((mac (car def))
1543 (lib (semantic--grammar-macro-lib-part def)))
1545 (format "%s/%s" mac lib)
1549 (defun semantic--grammar-macro-compl-dict ()
1550 "Return a completion dictionary of macro definitions."
1551 (let ((defs (semantic-grammar-macros))
1554 (setq def (car defs)
1556 (if (or (assoc (car def) defs) (assoc (car def) dups))
1558 (push (semantic--grammar-macro-compl-elt def) dict)))
1560 (setq def (car dups)
1562 (push (semantic--grammar-macro-compl-elt def t) dict))
1565 (defun semantic-grammar-find-macro-expander (macro-name library)
1566 "Visit the Emacs Lisp library where a grammar macro is implemented.
1567 MACRO-NAME is a symbol that identifies a grammar macro.
1568 LIBRARY is the name (sans extension) of the Emacs Lisp library where
1569 to start searching the macro implementation. Lookup in included
1570 libraries, if necessary.
1571 Find a function tag (in current tags table) whose name contains MACRO-NAME.
1572 Select the buffer containing the tag's definition, and move point there."
1574 (let* ((dic (semantic--grammar-macro-compl-dict))
1575 (def (assoc (completing-read "Macro: " dic nil 1) dic)))
1576 (or (cdr def) '(nil nil))))
1577 (when (and macro-name library)
1578 (let* ((lib (format "%s.el" library))
1579 (buf (find-file-noselect (or (locate-library lib t) lib)))
1580 (tag (with-current-buffer buf
1581 (semantic--grammar-macro-function-tag
1582 (format "%s-%s" library macro-name)))))
1585 (require 'semantic/decorate)
1586 (pop-to-buffer (semantic-tag-buffer tag))
1587 (goto-char (semantic-tag-start tag))
1588 (semantic-momentary-highlight-tag tag))
1590 (message "No expander found in library %s for macro %s"
1591 library macro-name)))))
1596 (defvar semantic-grammar-syntax-help
1599 ("symbol" . "Syntax: A symbol of alpha numeric and symbol characters")
1600 ("number" . "Syntax: Numeric characters.")
1601 ("punctuation" . "Syntax: Punctuation character.")
1602 ("semantic-list" . "Syntax: A list delimited by any valid list characters")
1603 ("open-paren" . "Syntax: Open Parenthesis character")
1604 ("close-paren" . "Syntax: Close Parenthesis character")
1605 ("string" . "Syntax: String character delimited text")
1606 ("comment" . "Syntax: Comment character delimited text")
1608 ("EMPTY" . "Syntax: Match empty text")
1609 ("ASSOC" . "Lambda Key: (ASSOC key1 value1 key2 value2 ...)")
1610 ("EXPAND" . "Lambda Key: (EXPAND <list id> <rule>)")
1611 ("EXPANDFULL" . "Lambda Key: (EXPANDFULL <list id> <rule>)")
1612 ;; Tag Generator Macros
1613 ("TAG" . "Generic Tag Generation: (TAG <name> <tag-class> [ :key value ]*)")
1614 ("VARIABLE-TAG" . "(VARIABLE-TAG <name> <lang-type> <default-value> [ :key value ]*)")
1615 ("FUNCTION-TAG" . "(FUNCTION-TAG <name> <lang-type> <arg-list> [ :key value ]*)")
1616 ("TYPE-TAG" . "(TYPE-TAG <name> <lang-type> <part-list> <parents> [ :key value ]*)")
1617 ("INCLUDE-TAG" . "(INCLUDE-TAG <name> <system-flag> [ :key value ]*)")
1618 ("PACKAGE-TAG" . "(PACKAGE-TAG <name> <detail> [ :key value ]*)")
1619 ("CODE-TAG" . "(CODE-TAG <name> <detail> [ :key value ]*)")
1620 ("ALIAS-TAG" . "(ALIAS-TAG <name> <aliasclass> <definition> [:key value]*)")
1621 ;; Special value macros
1622 ("$1" . "Match Value: Value from match list in slot 1")
1623 ("$2" . "Match Value: Value from match list in slot 2")
1624 ("$3" . "Match Value: Value from match list in slot 3")
1625 ("$4" . "Match Value: Value from match list in slot 4")
1626 ("$5" . "Match Value: Value from match list in slot 5")
1627 ("$6" . "Match Value: Value from match list in slot 6")
1628 ("$7" . "Match Value: Value from match list in slot 7")
1629 ("$8" . "Match Value: Value from match list in slot 8")
1630 ("$9" . "Match Value: Value from match list in slot 9")
1631 ;; Same, but with annoying , in front.
1632 (",$1" . "Match Value: Value from match list in slot 1")
1633 (",$2" . "Match Value: Value from match list in slot 2")
1634 (",$3" . "Match Value: Value from match list in slot 3")
1635 (",$4" . "Match Value: Value from match list in slot 4")
1636 (",$5" . "Match Value: Value from match list in slot 5")
1637 (",$6" . "Match Value: Value from match list in slot 6")
1638 (",$7" . "Match Value: Value from match list in slot 7")
1639 (",$8" . "Match Value: Value from match list in slot 8")
1640 (",$9" . "Match Value: Value from match list in slot 9")
1642 "Association of syntax elements, and the corresponding help.")
1644 (declare-function eldoc-function-argstring "eldoc")
1645 (declare-function eldoc-docstring-format-sym-doc "eldoc")
1646 (declare-function eldoc-last-data-store "eldoc")
1647 (declare-function eldoc-get-fnsym-args-string "eldoc")
1648 (declare-function eldoc-get-var-docstring "eldoc")
1650 (defun semantic-grammar-eldoc-get-macro-docstring (macro expander)
1651 "Return a one-line docstring for the given grammar MACRO.
1652 EXPANDER is the name of the function that expands MACRO."
1654 (if (and (eq expander (aref eldoc-last-data 0))
1655 (eq 'function (aref eldoc-last-data 2)))
1656 (aref eldoc-last-data 1)
1657 (let ((doc (help-split-fundoc (documentation expander t) expander)))
1660 (setq doc (car doc))
1661 (string-match "\\`[^ )]* ?" doc)
1662 (setq doc (concat "(" (substring doc (match-end 0)))))
1664 (setq doc (eldoc-function-argstring expander))))
1667 (eldoc-docstring-format-sym-doc
1668 macro (format "==> %s %s" expander doc) 'default))
1669 (eldoc-last-data-store expander doc 'function))
1672 (define-mode-local-override semantic-idle-summary-current-symbol-info
1673 semantic-grammar-mode ()
1674 "Display additional eldoc information about grammar syntax elements.
1675 Syntax element is the current symbol at point.
1676 If it is associated a help string in `semantic-grammar-syntax-help',
1678 If it is a macro name, return a description of the associated expander
1679 function parameter list.
1680 If it is a function name, return a description of this function
1682 It it is a variable name, return a brief (one-line) documentation
1683 string for the variable.
1684 If a default description of the current context can be obtained,
1686 Otherwise return nil."
1688 (let* ((elt (car (semantic-ctxt-current-symbol)))
1689 (val (and elt (cdr (assoc elt semantic-grammar-syntax-help)))))
1690 (when (and (not val) elt (semantic-grammar-in-lisp-p))
1691 ;; Ensure to load macro definitions before doing `intern-soft'.
1692 (setq val (semantic-grammar-macros)
1693 elt (intern-soft elt)
1694 val (and elt (cdr (assq elt val))))
1697 ((and val (fboundp val))
1698 (setq val (semantic-grammar-eldoc-get-macro-docstring elt val)))
1700 ((and elt (fboundp elt))
1701 (setq val (eldoc-get-fnsym-args-string elt)))
1703 ((and elt (boundp elt))
1704 (setq val (eldoc-get-var-docstring elt)))
1706 (or val (semantic-idle-summary-current-symbol-info-default))))
1708 (define-mode-local-override semantic-tag-boundary-p
1709 semantic-grammar-mode (tag)
1710 "Return non-nil for tags that should have a boundary drawn.
1711 Only tags of type 'nonterminal will be so marked."
1712 (let ((c (semantic-tag-class tag)))
1713 (eq c 'nonterminal)))
1715 (define-mode-local-override semantic-ctxt-current-function
1716 semantic-grammar-mode (&optional point)
1717 "Determine the name of the current function at POINT."
1719 (and point (goto-char point))
1720 (when (semantic-grammar-in-lisp-p)
1721 (with-mode-local emacs-lisp-mode
1722 (semantic-ctxt-current-function)))))
1724 (define-mode-local-override semantic-ctxt-current-argument
1725 semantic-grammar-mode (&optional point)
1726 "Determine the argument index of the called function at POINT."
1728 (and point (goto-char point))
1729 (when (semantic-grammar-in-lisp-p)
1730 (with-mode-local emacs-lisp-mode
1731 (semantic-ctxt-current-argument)))))
1733 (define-mode-local-override semantic-ctxt-current-assignment
1734 semantic-grammar-mode (&optional point)
1735 "Determine the tag being assigned into at POINT."
1737 (and point (goto-char point))
1738 (when (semantic-grammar-in-lisp-p)
1739 (with-mode-local emacs-lisp-mode
1740 (semantic-ctxt-current-assignment)))))
1742 (define-mode-local-override semantic-ctxt-current-class-list
1743 semantic-grammar-mode (&optional point)
1744 "Determine the class of tags that can be used at POINT."
1746 (and point (goto-char point))
1747 (if (semantic-grammar-in-lisp-p)
1748 (with-mode-local emacs-lisp-mode
1749 (semantic-ctxt-current-class-list))
1750 '(nonterminal keyword))))
1752 (define-mode-local-override semantic-ctxt-current-mode
1753 semantic-grammar-mode (&optional point)
1754 "Return the major mode active at POINT.
1755 POINT defaults to the value of point in current buffer.
1756 Return `emacs-lisp-mode' is POINT is within Lisp code, otherwise
1757 return the current major mode."
1759 (and point (goto-char point))
1760 (if (semantic-grammar-in-lisp-p)
1762 (semantic-ctxt-current-mode-default))))
1764 (define-mode-local-override semantic-format-tag-abbreviate
1765 semantic-grammar-mode (tag &optional parent color)
1766 "Return a string abbreviation of TAG.
1767 Optional PARENT is not used.
1768 Optional COLOR is used to flag if color is added to the text."
1769 (let ((class (semantic-tag-class tag))
1770 (name (semantic-format-tag-name tag parent color)))
1772 ((eq class 'nonterminal)
1774 ((eq class 'setting)
1776 ((memq class '(rule keyword))
1779 (concat "%" (symbol-name class) " " name)))))
1781 (define-mode-local-override semantic-format-tag-summarize
1782 semantic-grammar-mode (tag &optional parent color)
1783 "Return a string summarizing TAG.
1784 Optional PARENT is not used.
1785 Optional argument COLOR determines if color is added to the text."
1786 (let ((class (semantic-tag-class tag))
1787 (name (semantic-format-tag-name tag parent color))
1791 ((eq class 'nonterminal)
1792 (setq label "Nonterminal: "
1794 " with %d match lists."
1795 (length (semantic-tag-components tag)))))
1796 ((eq class 'keyword)
1797 (setq label "Keyword: ")
1799 (semantic--find-tags-by-function
1802 (setq summary (cdr (assoc "summary"
1803 (semantic-tag-get-attribute
1805 ;; Get `put' tag with TAG name.
1806 (semantic-find-tags-by-name-regexp
1807 (regexp-quote (semantic-tag-name tag))
1808 (semantic-find-tags-by-class 'put (current-buffer))))
1809 (setq desc (concat " = "
1810 (semantic-tag-get-attribute tag :value)
1812 (concat " - " (read summary))
1815 (setq label "Token: ")
1816 (let ((val (semantic-tag-get-attribute tag :value))
1817 (names (semantic-tag-get-attribute tag :rest))
1818 (type (semantic-tag-type tag)))
1820 (setq name (mapconcat 'identity (cons name names) " ")))
1823 (format " <%s>" type)
1826 (format "%s%S" val (if type " " ""))
1829 (setq label "Assoc: ")
1830 (let ((val (semantic-tag-get-attribute tag :value))
1831 (type (semantic-tag-type tag)))
1834 (format " <%s>" type)
1837 (concat " " (mapconcat 'identity val " "))
1840 (setq desc (semantic-format-tag-abbreviate tag parent color))))
1841 (if (and color label)
1842 (setq label (semantic--format-colorize-text label 'label)))
1843 (if (and color label desc)
1844 (setq desc (semantic--format-colorize-text desc 'comment)))
1846 (concat label name desc)
1847 ;; Just a description is the abbreviated version
1850 ;;; Semantic Analysis
1852 (define-mode-local-override semantic-analyze-current-context
1853 semantic-grammar-mode (point)
1854 "Provide a semantic analysis object describing a context in a grammar."
1855 (require 'semantic/analyze)
1856 (if (semantic-grammar-in-lisp-p)
1857 (with-mode-local emacs-lisp-mode
1858 (semantic-analyze-current-context point))
1860 (let* ((context-return nil)
1861 (prefixandbounds (semantic-ctxt-current-symbol-and-bounds))
1862 (prefix (car prefixandbounds))
1863 (bounds (nth 2 prefixandbounds))
1865 (prefixclass (semantic-ctxt-current-class-list))
1868 ;; Do context for rules when in a match list.
1870 (semantic-find-first-tag-by-name
1874 (setq context-return
1875 (semantic-analyze-context
1876 "context-for-semantic-grammar"
1877 :buffer (current-buffer)
1880 :prefix (if prefixsym
1884 :prefixclass prefixclass
1889 (define-mode-local-override semantic-analyze-possible-completions
1890 semantic-grammar-mode (context)
1891 "Return a list of possible completions based on CONTEXT."
1892 (require 'semantic/analyze/complete)
1893 (if (semantic-grammar-in-lisp-p)
1894 (with-mode-local emacs-lisp-mode
1895 (semantic-analyze-possible-completions context))
1896 (with-current-buffer (oref context buffer)
1897 (let* ((prefix (car (oref context :prefix)))
1898 (completetext (cond ((semantic-tag-p prefix)
1899 (semantic-tag-name prefix))
1902 ((stringp (car prefix))
1904 (tags (semantic-find-tags-for-completion completetext
1906 (semantic-analyze-tags-of-class-list
1907 tags (oref context prefixclass)))
1910 (provide 'semantic/grammar)
1913 ;; generated-autoload-load-name: "semantic/grammar"
1916 ;;; semantic/grammar.el ends here