1 ;;; semantic-grammar.el --- Major mode framework for Semantic grammars
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2007 David Ponce
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 15 Aug 2002
9 ;; X-RCS: $Id: semantic-grammar.el,v 1.1 2007-11-26 15:10:37 michaels Exp $
11 ;; This file is not part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This software is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; Major mode framework for editing Semantic's input grammar files.
36 (require 'semantic-wisent)
43 (require 'semantic-edit)
44 (require 'semantic-find)
45 (require 'semantic-format)
46 (require 'semantic-idle))
52 (defconst semantic-grammar-lex-c-char-re "'\\s\\?.'"
53 "Regexp matching C-like character literals.")
55 ;; Most of the analyzers are auto-generated from the grammar, but the
56 ;; following which need special handling code.
58 (define-lex-regex-analyzer semantic-grammar-lex-prologue
59 "Detect and create a prologue token."
61 ;; Zing to the end of this brace block.
62 (semantic-lex-push-token
66 (semantic-lex-unterminated-syntax-protection 'PROLOGUE
71 (defsubst semantic-grammar-epilogue-start ()
72 "Return the start position of the grammar epilogue."
74 (goto-char (point-min))
75 (if (re-search-forward "^\\s-*\\<%%\\>\\s-*$" nil t 2)
79 (define-lex-regex-analyzer semantic-grammar-lex-epilogue
80 "Detect and create an epilogue or percent-percent token."
82 (let ((start (match-beginning 0))
84 (class 'PERCENT_PERCENT))
85 (when (>= start (semantic-grammar-epilogue-start))
88 (semantic-lex-push-token
89 (semantic-lex-token class start end))))
91 ;; Provide auto-generated analyzers and the lexer.
92 (require 'semantic-grammar-wy)
96 (defun semantic-grammar-lex-buffer ()
97 "Run `semantic-grammar-lex' on current buffer."
100 (setq semantic-lex-analyzer 'semantic-grammar-lexer)
102 (semantic-lex (point-min) (point-max))))
103 (with-current-buffer (get-buffer-create "*semantic-grammar-lex*")
105 (pp token-stream (current-buffer))
106 (goto-char (point-min))
107 (pop-to-buffer (current-buffer)))))
110 ;;;; Semantic action expansion
113 (defun semantic-grammar-ASSOC (&rest args)
114 "Return expansion of built-in ASSOC expression.
115 ARGS are ASSOC's key value list."
117 `(semantic-tag-make-assoc-list
118 ,@(mapcar #'(lambda (i)
123 (setq key (not key))))
126 (defsubst semantic-grammar-quote-p (sym)
127 "Return non-nil if SYM is bound to the `quote' function."
129 (eq (indirect-function sym)
130 (indirect-function 'quote))
133 (defsubst semantic-grammar-backquote-p (sym)
134 "Return non-nil if SYM is bound to the `backquote' function."
136 (eq (indirect-function sym)
137 (indirect-function 'backquote))
141 ;;;; API to access grammar tags
144 (defvar-mode-local semantic-grammar-mode
145 senator-add-log-tags '(nonterminal put token keyword)
146 "List of nonterminal tags used with add-log.")
148 (define-mode-local-override semantic-tag-components
149 semantic-grammar-mode (tag)
150 "Return the children of tag TAG."
151 (semantic-tag-get-attribute tag :children))
153 (defun semantic-grammar-first-tag-name (class)
154 "Return the name of the first tag of class CLASS found.
155 Warn if other tags of class CLASS exist."
156 (let* ((tags (semantic-find-tags-by-class
157 class (current-buffer))))
160 (semantic-tag-name (car tags))
162 (message "*** Ignore all but first declared %s"
165 (defun semantic-grammar-tag-symbols (class)
166 "Return the list of symbols defined in tags of class CLASS.
167 That is tag names plus names defined in tag attribute `:rest'."
168 (let* ((tags (semantic-find-tags-by-class
169 class (current-buffer))))
175 (cons (semantic-tag-name tag)
176 (semantic-tag-get-attribute tag :rest))))
179 (defsubst semantic-grammar-item-text (item)
180 "Return the readable string form of ITEM."
181 (if (string-match semantic-grammar-lex-c-char-re item)
182 (concat "?" (substring item 1 -1))
185 (defsubst semantic-grammar-item-value (item)
186 "Return symbol or character value of ITEM string."
187 (if (string-match semantic-grammar-lex-c-char-re item)
188 (let ((c (read (concat "?" (substring item 1 -1)))))
189 (if (featurep 'xemacs)
190 ;; Handle characters as integers in XEmacs like in GNU Emacs.
195 (defun semantic-grammar-prologue ()
196 "Return grammar prologue code as a string value."
197 (let ((tag (semantic-find-first-tag-by-name
199 (semantic-find-tags-by-class 'code (current-buffer)))))
205 (goto-char (semantic-tag-start tag))
206 (skip-chars-forward "%{\r\n\t ")
209 (goto-char (semantic-tag-end tag))
210 (skip-chars-backward "\r\n\t %}")
215 (defun semantic-grammar-epilogue ()
216 "Return grammar epilogue code as a string value."
217 (let ((tag (semantic-find-first-tag-by-name
219 (semantic-find-tags-by-class 'code (current-buffer)))))
225 (goto-char (semantic-tag-start tag))
226 (skip-chars-forward "%\r\n\t ")
229 (goto-char (semantic-tag-end tag))
230 (skip-chars-backward "\r\n\t")
231 ;; If a grammar footer is found, skip it.
232 (re-search-backward "^;;;\\s-+\\S-+\\s-+ends here"
237 (skip-chars-backward "\r\n\t")
242 (defsubst semantic-grammar-buffer-file (&optional buffer)
243 "Return name of file sans directory BUFFER is visiting.
244 No argument or nil as argument means use the current buffer."
245 (file-name-nondirectory (buffer-file-name buffer)))
247 (defun semantic-grammar-package ()
248 "Return the %package value as a string.
249 If there is no %package statement in the grammar, return a default
250 package name derived from the grammar file name. For example, the
251 default package name for the grammar file foo.wy is foo-wy, and for
252 foo.by it is foo-by."
253 (or (semantic-grammar-first-tag-name 'package)
254 (let* ((file (semantic-grammar-buffer-file))
255 (ext (file-name-extension file))
256 (i (string-match (format "\\([.]\\)%s\\'" ext) file)))
257 (concat (substring file 0 i) "-" ext))))
259 (defsubst semantic-grammar-languagemode ()
260 "Return the %languagemode value as a list of symbols or nil."
261 (semantic-grammar-tag-symbols 'languagemode))
263 (defsubst semantic-grammar-start ()
264 "Return the %start value as a list of symbols or nil."
265 (semantic-grammar-tag-symbols 'start))
267 (defsubst semantic-grammar-scopestart ()
268 "Return the %scopestart value as a symbol or nil."
269 (intern (or (semantic-grammar-first-tag-name 'scopestart) "nil")))
271 (defsubst semantic-grammar-quotemode ()
272 "Return the %quotemode value as a symbol or nil."
273 (intern (or (semantic-grammar-first-tag-name 'quotemode) "nil")))
275 (defsubst semantic-grammar-keywords ()
276 "Return the language keywords.
277 That is an alist of (VALUE . TOKEN) where VALUE is the string value of
278 the keyword and TOKEN is the terminal symbol identifying the keyword."
281 (cons (semantic-tag-get-attribute key :value)
282 (intern (semantic-tag-name key))))
283 (semantic-find-tags-by-class 'keyword (current-buffer))))
285 (defun semantic-grammar-keyword-properties (keywords)
286 "Return the list of KEYWORDS properties."
287 (let ((puts (semantic-find-tags-by-class
288 'put (current-buffer)))
289 put keys key plist assoc pkey pval props)
295 (cons (semantic-tag-name put)
296 (semantic-tag-get-attribute put :rest))))
300 assoc (rassq key keywords))
302 nil ;;(message "*** %%put to undefined keyword %s ignored" key)
303 (setq key (car assoc)
304 plist (semantic-tag-get-attribute put :value))
306 (setq pkey (intern (caar plist))
307 pval (read (cdar plist))
308 props (cons (list key pkey pval) props)
309 plist (cdr plist))))))
312 (defun semantic-grammar-tokens ()
313 "Return defined lexical tokens.
314 That is an alist (TYPE . DEFS) where type is a %token <type> symbol
315 and DEFS is an alist of (TOKEN . VALUE). TOKEN is the terminal symbol
316 identifying the token and VALUE is the string value of the token or
318 (let (tags alist assoc tag type term names value)
320 ;; Check for <type> in %left, %right & %nonassoc declarations
321 (setq tags (semantic-find-tags-by-class
322 'assoc (current-buffer)))
326 (when (setq type (semantic-tag-type tag))
327 (setq names (semantic-tag-get-attribute tag :value)
328 assoc (assoc type alist))
329 (or assoc (setq assoc (list type)
330 alist (cons assoc alist)))
332 (setq term (car names)
334 (or (string-match semantic-grammar-lex-c-char-re term)
335 (setcdr assoc (cons (list (intern term))
338 ;; Then process %token declarations so they can override any
339 ;; previous specifications
340 (setq tags (semantic-find-tags-by-class
341 'token (current-buffer)))
345 (setq names (cons (semantic-tag-name tag)
346 (semantic-tag-get-attribute tag :rest))
347 type (or (semantic-tag-type tag) "<no-type>")
348 value (semantic-tag-get-attribute tag :value)
349 assoc (assoc type alist))
350 (or assoc (setq assoc (list type)
351 alist (cons assoc alist)))
353 (setq term (intern (car names))
355 (setcdr assoc (cons (cons term value) (cdr assoc)))))
358 (defun semantic-grammar-token-%type-properties (&optional props)
359 "Return properties set by %type statements.
360 This declare a new type if necessary.
361 If optional argument PROPS is non-nil, it is an existing list of
362 properties where to add new properties."
364 (dolist (tag (semantic-find-tags-by-class 'type (current-buffer)))
365 (setq type (semantic-tag-name tag))
366 ;; Indicate to auto-generate the analyzer for this type
367 (push (list type :declared t) props)
368 (dolist (e (semantic-tag-get-attribute tag :value))
369 (push (list type (intern (car e)) (read (or (cdr e) "nil")))
373 (defun semantic-grammar-token-%put-properties (tokens)
374 "For types found in TOKENS, return properties set by %put statements."
375 (let (found type props)
376 (dolist (put (semantic-find-tags-by-class 'put (current-buffer)))
377 (dolist (type (cons (semantic-tag-name put)
378 (semantic-tag-get-attribute put :rest)))
379 (setq found (assoc type tokens))
381 nil ;; %put <type> ignored, no token defined
382 (setq type (car found))
383 (dolist (e (semantic-tag-get-attribute put :value))
384 (push (list type (intern (car e)) (read (or (cdr e) "nil")))
388 (defsubst semantic-grammar-token-properties (tokens)
389 "Return properties of declared types.
390 Types are explicitly declared by %type statements. Types found in
391 TOKENS are those declared implicitly by %token statements.
392 Properties can be set by %put and %type statements.
393 Properties set by %type statements take precedence over those set by
395 (let ((props (semantic-grammar-token-%put-properties tokens)))
396 (semantic-grammar-token-%type-properties props)))
398 (defun semantic-grammar-use-macros ()
399 "Return macro definitions from %use-macros statements.
400 Also load the specified macro libraries."
402 (dolist (tag (semantic-find-tags-by-class 'macro (current-buffer)))
403 (setq lib (intern (semantic-tag-type tag)))
405 ;;(load lib) ;; Be sure to use the latest macro library.
408 (dolist (mac (semantic-tag-get-attribute tag :value))
409 (push (cons (intern mac)
410 (intern (format "%s-%s" lib mac)))
414 (defvar semantic-grammar-macros nil
415 "List of associations (MACRO-NAME . EXPANDER).")
416 (make-variable-buffer-local 'semantic-grammar-macros)
418 (defun semantic-grammar-macros ()
419 "Build and return the alist of defined macros."
421 ;; Definitions found in tags.
422 (semantic-grammar-use-macros)
423 ;; Other pre-installed definitions.
424 semantic-grammar-macros))
427 ;;;; Overloaded functions that build parser data.
430 ;;; Keyword table builder
432 (defun semantic-grammar-keywordtable-builder-default ()
433 "Return the default value of the keyword table."
434 (let ((keywords (semantic-grammar-keywords)))
435 `(semantic-lex-make-keyword-table
437 ',(semantic-grammar-keyword-properties keywords))))
439 (define-overload semantic-grammar-keywordtable-builder ()
440 "Return the keyword table table value.")
442 ;;; Token table builder
444 (defun semantic-grammar-tokentable-builder-default ()
445 "Return the default value of the table of lexical tokens."
446 (let ((tokens (semantic-grammar-tokens)))
447 `(semantic-lex-make-type-table
449 ',(semantic-grammar-token-properties tokens))))
451 (define-overload semantic-grammar-tokentable-builder ()
452 "Return the value of the table of lexical tokens.")
454 ;;; Parser table builder
456 (defun semantic-grammar-parsetable-builder-default ()
457 "Return the default value of the parse table."
458 (error "`semantic-grammar-parsetable-builder' not defined"))
460 (define-overload semantic-grammar-parsetable-builder ()
461 "Return the parser table value.")
463 ;;; Parser setup code builder
465 (defun semantic-grammar-setupcode-builder-default ()
466 "Return the default value of the setup code form."
467 (error "`semantic-grammar-setupcode-builder' not defined"))
469 (define-overload semantic-grammar-setupcode-builder ()
470 "Return the parser setup code form.")
473 ;;;; Lisp code generation
475 (defvar semantic--grammar-input-buffer nil)
476 (defvar semantic--grammar-output-buffer nil)
478 (defsubst semantic-grammar-keywordtable ()
479 "Return the variable name of the keyword table."
480 (concat (file-name-sans-extension
481 (semantic-grammar-buffer-file
482 semantic--grammar-output-buffer))
485 (defsubst semantic-grammar-tokentable ()
486 "Return the variable name of the token table."
487 (concat (file-name-sans-extension
488 (semantic-grammar-buffer-file
489 semantic--grammar-output-buffer))
492 (defsubst semantic-grammar-parsetable ()
493 "Return the variable name of the parse table."
494 (concat (file-name-sans-extension
495 (semantic-grammar-buffer-file
496 semantic--grammar-output-buffer))
499 (defsubst semantic-grammar-setupfunction ()
500 "Return the name of the parser setup function."
501 (concat (file-name-sans-extension
502 (semantic-grammar-buffer-file
503 semantic--grammar-output-buffer))
506 (defmacro semantic-grammar-as-string (object)
507 "Return OBJECT as a string value."
508 `(if (stringp ,object)
511 (pp-to-string ,object)))
513 (defun semantic-grammar-insert-defconst (name value docstring)
514 "Insert declaration of constant NAME with VALUE and DOCSTRING."
515 (let ((start (point)))
516 (insert (format "(defconst %s\n%s%S)\n\n" name value docstring))
521 (defun semantic-grammar-insert-defun (name body docstring)
522 "Insert declaration of function NAME with BODY and DOCSTRING."
523 (let ((start (point)))
524 (insert (format "(defun %s ()\n%S\n%s)\n\n" name docstring body))
529 (defun semantic-grammar-insert-define (define)
530 "Insert the declaration specified by DEFINE expression.
531 Typically a DEFINE expression should look like this:
533 \(define-thing name docstring expression1 ...)"
535 (let ((start (point)))
536 (insert (format "(%S %S" (car define) (nth 1 define)))
537 (dolist (item (nthcdr 2 define))
540 (pp item (current-buffer)))
546 (defconst semantic-grammar-header-template
548 ;;; %F --- Generated parser support file
557 ;; This file is not part of GNU Emacs.
559 ;; This program is free software; you can redistribute it and/or
560 ;; modify it under the terms of the GNU General Public License as
561 ;; published by the Free Software Foundation; either version 2, or (at
562 ;; your option) any later version.
564 ;; This software is distributed in the hope that it will be useful,
565 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
566 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
567 ;; General Public License for more details.
569 ;; You should have received a copy of the GNU General Public License
570 ;; along with GNU Emacs; see the file COPYING. If not, write to the
571 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
572 ;; Boston, MA 02110-1301, USA.
576 ;; PLEASE DO NOT MANUALLY EDIT THIS FILE! It is automatically
577 ;; generated from the grammar file %G.
584 "Generated header template.")
586 (defconst semantic-grammar-footer-template
593 "Generated footer template.")
595 (defun semantic-grammar-copyright-line ()
596 "Return the grammar copyright line, or nil if not found."
598 (goto-char (point-min))
599 (when (re-search-forward "^;;+[ \t]+Copyright (C) .*$"
600 ;; Search only in the four top lines
601 (save-excursion (forward-line 4) (point))
605 (defun semantic-grammar-header ()
606 "Return text of a generated standard header."
607 (let ((file (semantic-grammar-buffer-file
608 semantic--grammar-output-buffer))
609 (gram (semantic-grammar-buffer-file))
610 (date (format-time-string "%Y-%m-%d %T%z"))
611 (vcid (concat "$" "Id" "$")) ;; Avoid expansion
612 ;; Try to get the copyright from the input grammar, or
613 ;; generate a new one if not found.
614 (copy (or (semantic-grammar-copyright-line)
615 (concat (format-time-string ";; Copyright (C) %Y ")
617 (Sformat '((?U user-full-name)
618 (?M user-mail-address)
624 semantic-grammar-header-template)))
626 (defun semantic-grammar-footer ()
627 "Return text of a generated standard footer."
628 (let* ((file (semantic-grammar-buffer-file
629 semantic--grammar-output-buffer))
630 (libr (file-name-sans-extension file)))
633 semantic-grammar-footer-template)))
635 (defun semantic-grammar-token-data ()
636 "Return the string value of the table of lexical tokens."
637 (semantic-grammar-as-string
638 (semantic-grammar-tokentable-builder)))
640 (defun semantic-grammar-keyword-data ()
641 "Return the string value of the table of keywords."
642 (semantic-grammar-as-string
643 (semantic-grammar-keywordtable-builder)))
645 (defun semantic-grammar-parser-data ()
646 "Return the parser table as a string value."
647 (semantic-grammar-as-string
648 (semantic-grammar-parsetable-builder)))
650 (defun semantic-grammar-setup-data ()
651 "Return the parser setup code form as a string value."
652 (semantic-grammar-as-string
653 (semantic-grammar-setupcode-builder)))
655 ;;; Generation of lexical analyzers.
657 (defvar semantic-grammar--lex-block-specs)
659 (defsubst semantic-grammar--lex-delim-spec (block-spec)
660 "Return delimiters specification from BLOCK-SPEC."
662 (let* ((standard-input (cdr block-spec))
664 (if (and (consp delim-spec)
665 (car delim-spec) (symbolp (car delim-spec))
666 (cadr delim-spec) (symbolp (cadr delim-spec)))
670 (error "Invalid delimiters specification %s in block token %s"
671 (cdr block-spec) (car block-spec)))))
673 (defun semantic-grammar--lex-block-specs ()
674 "Compute lexical block specifications for the current buffer.
675 Block definitions are read from the current table of lexical types."
677 ;; Block specifications have been parsed and are invalid.
678 ((eq semantic-grammar--lex-block-specs 'error)
681 ;; Parse block specifications.
682 ((null semantic-grammar--lex-block-specs)
684 (let* ((blocks (cdr (semantic-lex-type-value "block" t)))
685 (open-delims (cdr (semantic-lex-type-value "open-paren" t)))
686 (close-delims (cdr (semantic-lex-type-value "close-paren" t)))
687 olist clist block-spec delim-spec open-spec close-spec)
688 (dolist (block-spec blocks)
689 (setq delim-spec (semantic-grammar--lex-delim-spec block-spec)
690 open-spec (assq (car delim-spec) open-delims)
691 close-spec (assq (cadr delim-spec) close-delims))
693 (error "Missing open-paren token %s required by block %s"
694 (car delim-spec) (car block-spec)))
696 (error "Missing close-paren token %s required by block %s"
697 (cdr delim-spec) (car block-spec)))
698 ;; build alist ((OPEN-DELIM OPEN-SYM BLOCK-SYM) ...)
699 (push (list (cdr open-spec) (car open-spec) (car block-spec))
701 ;; build alist ((CLOSE-DELIM CLOSE-SYM) ...)
702 (push (list (cdr close-spec) (car close-spec))
704 (setq semantic-grammar--lex-block-specs (cons olist clist)))
706 (setq semantic-grammar--lex-block-specs 'error)
707 (message "%s" (error-message-string err))
710 ;; Block specifications already parsed.
712 semantic-grammar--lex-block-specs)))
714 (defsubst semantic-grammar-quoted-form (exp)
715 "Return a quoted form of EXP if it isn't a self evaluating form."
716 (if (and (not (null exp))
717 (or (listp exp) (symbolp exp)))
721 (defun semantic-grammar-insert-defanalyzer (type)
722 "Insert declaration of the lexical analyzer defined with TYPE."
723 (let* ((type-name (symbol-name type))
724 (type-value (symbol-value type))
725 (syntax (get type 'syntax))
726 (declared (get type :declared))
727 spec mtype prefix name doc)
728 ;; Generate an analyzer if the corresponding type has been
729 ;; explicitly declared in a %type statement, and if at least the
730 ;; syntax property has been provided.
731 (when (and declared syntax)
732 (setq prefix (file-name-sans-extension
733 (semantic-grammar-buffer-file
734 semantic--grammar-output-buffer))
735 mtype (or (get type 'matchdatatype) 'regexp)
736 name (intern (format "%s--<%s>-%s-analyzer" prefix type mtype))
737 doc (format "%s analyzer for <%s> tokens." mtype type))
739 ;; Regexp match analyzer
741 (semantic-grammar-insert-define
742 `(define-lex-regex-type-analyzer ,name
744 ,(semantic-grammar-quoted-form (cdr type-value))
745 ',(or (car type-value) (intern type-name))))
747 ;; String compare analyzer
749 (semantic-grammar-insert-define
750 `(define-lex-string-type-analyzer ,name
752 ,(semantic-grammar-quoted-form (cdr type-value))
753 ',(or (car type-value) (intern type-name))))
756 ((and (eq mtype 'block)
757 (setq spec (semantic-grammar--lex-block-specs)))
758 (semantic-grammar-insert-define
759 `(define-lex-block-type-analyzer ,name
761 ,(semantic-grammar-quoted-form spec)))
765 (semantic-grammar-insert-define
766 `(define-lex-sexp-type-analyzer ,name
768 ',(or (car type-value) (intern type-name))))
772 (semantic-grammar-insert-define
773 `(define-lex-keyword-type-analyzer ,name
779 (defun semantic-grammar-insert-defanalyzers ()
780 "Insert declarations of lexical analyzers."
782 (with-current-buffer semantic--grammar-input-buffer
783 (setq tokens (semantic-grammar-tokens)
784 props (semantic-grammar-token-properties tokens)))
785 (insert "(require 'semantic-lex)\n\n")
786 (let ((semantic-lex-types-obarray
787 (semantic-lex-make-type-table tokens props))
788 semantic-grammar--lex-block-specs)
789 (mapatoms 'semantic-grammar-insert-defanalyzer
790 semantic-lex-types-obarray))))
792 ;;; Generation of the grammar support file.
794 (defcustom semantic-grammar-file-regexp "\\.[wb]y$"
795 "Regexp which matches grammar source files."
799 (defsubst semantic-grammar-noninteractive ()
800 "Return non-nil if running without interactive terminal."
801 (if (featurep 'xemacs)
805 (defun semantic-grammar-create-package (&optional force)
806 "Create package Lisp code from grammar in current buffer.
807 Does nothing if the Lisp code seems up to date.
808 If optional argument FORCE is non-nil, unconditionally re-generate the
811 (setq force (or force current-prefix-arg))
812 (semantic-fetch-tags)
814 ;; Values of the following local variables are obtained from
815 ;; the grammar parsed tree in current buffer, that is before
816 ;; switching to the output file.
817 (package (semantic-grammar-package))
818 (output (concat package ".el"))
819 (semantic--grammar-input-buffer (current-buffer))
820 (semantic--grammar-output-buffer (find-file-noselect output))
821 (header (semantic-grammar-header))
822 (prologue (semantic-grammar-prologue))
823 (epilogue (semantic-grammar-epilogue))
824 (footer (semantic-grammar-footer))
827 (not (buffer-modified-p))
828 (file-newer-than-file-p
829 (buffer-file-name semantic--grammar-output-buffer)
830 (buffer-file-name semantic--grammar-input-buffer)))
831 (message "Package `%s' is up to date." package)
832 ;; Create the package
833 (set-buffer semantic--grammar-output-buffer)
834 ;; Use Unix EOLs, so that the file is portable to all platforms.
835 (setq buffer-file-coding-system 'raw-text-unix)
837 (unless (eq major-mode 'emacs-lisp-mode)
840 ;;;; Header + Prologue
843 "
\f\n;;; Prologue\n;;\n"
846 ;; Evaluate the prologue now, because it might provide definition
847 ;; of grammar macro expanders.
848 (eval-region (point-min) (point))
854 (insert "
\f\n;;; Declarations\n;;\n")
856 ;; `eval-defun' is not necessary to reset `defconst' values.
857 (semantic-grammar-insert-defconst
858 (semantic-grammar-keywordtable)
859 (with-current-buffer semantic--grammar-input-buffer
860 (semantic-grammar-keyword-data))
861 "Table of language keywords.")
863 (semantic-grammar-insert-defconst
864 (semantic-grammar-tokentable)
865 (with-current-buffer semantic--grammar-input-buffer
866 (semantic-grammar-token-data))
867 "Table of lexical tokens.")
869 (semantic-grammar-insert-defconst
870 (semantic-grammar-parsetable)
871 (with-current-buffer semantic--grammar-input-buffer
872 (semantic-grammar-parser-data))
875 (semantic-grammar-insert-defun
876 (semantic-grammar-setupfunction)
877 (with-current-buffer semantic--grammar-input-buffer
878 (semantic-grammar-setup-data))
879 "Setup the Semantic Parser.")
882 (insert "
\f\n;;; Analyzers\n;;\n")
884 (semantic-grammar-insert-defanalyzers)
886 ;;;; Epilogue & Footer
888 (insert "
\f\n;;; Epilogue\n;;\n"
895 ;; If running in batch mode, there is nothing more to do.
896 ;; Save the generated file and quit.
897 (if (semantic-grammar-noninteractive)
898 (let ((version-control t)
899 (delete-old-versions t)
900 (make-backup-files t)
901 (vc-make-backup-files t))
903 (kill-buffer (current-buffer)))
904 ;; If running interactively, eval declarations and epilogue
905 ;; code, then pop to the buffer visiting the generated file.
906 (eval-region (point) (point-max))
907 (goto-char (point-min))
908 (pop-to-buffer (current-buffer))
909 ;; The generated code has been evaluated and updated into
910 ;; memory. Now find all buffers that match the major modes we
911 ;; have created this language for, and force them to call our
912 ;; setup function again, refreshing all semantic data, and
913 ;; enabling them to work with the new code just created.
915 ;; At this point, I don't know any user's defined setup code :-(
916 ;; At least, what I can do for now, is to run the generated
917 ;; parser-install function.
918 (semantic-map-mode-buffers
919 (semantic-grammar-setupfunction)
920 (semantic-grammar-languagemode)))
922 ;; Return the name of the generated package file.
925 (defun semantic-grammar-recreate-package ()
926 "Unconditionnaly create Lisp code from grammar in current buffer.
927 Like \\[universal-argument] \\[semantic-grammar-create-package]."
929 (semantic-grammar-create-package t))
931 (defun semantic-grammar-batch-build-one-package (file)
932 "Build a Lisp package from the grammar in FILE.
933 That is, generate Lisp code from FILE, and `byte-compile' it.
934 Return non-nil if there were no errors, nil if errors."
935 ;; We need this require so that we can find `byte-compile-dest-file'.
937 (unless (auto-save-file-name-p file)
938 ;; Create the package
941 (with-current-buffer (find-file-noselect file)
942 (semantic-grammar-create-package))
944 (message "%s" (error-message-string err))
947 ;; Only byte compile if out of date
948 (if (file-newer-than-file-p
949 packagename (byte-compile-dest-file packagename))
950 (let (;; Some complex grammar table expressions need a few
951 ;; more resources than the default.
952 (max-specpdl-size (max 3000 max-specpdl-size))
953 (max-lisp-eval-depth (max 1000 max-lisp-eval-depth))
955 ;; byte compile the resultant file
956 (byte-compile-file packagename))
960 (defun semantic-grammar-batch-build-packages ()
961 "Build Lisp packages from grammar files on the command line.
962 That is, run `semantic-grammar-batch-build-one-package' for each file.
963 Each file is processed even if an error occurred previously.
964 Must be used from the command line, with `-batch'.
965 For example, to process grammar files in current directory, invoke:
967 \"emacs -batch -f semantic-grammar-batch-build-packages .\".
969 See also the variable `semantic-grammar-file-regexp'."
970 (or (semantic-grammar-noninteractive)
972 `semantic-grammar-batch-build-packages' must be used with -batch"
975 ;; Remove vc from find-file-hooks. It causes bad stuff to
976 ;; happen in Emacs 20.
977 (find-file-hooks (delete 'vc-find-file-hook find-file-hooks)))
978 (message "Compiling Grammars from: %s" (locate-library "semantic-grammar"))
979 (dolist (arg command-line-args-left)
980 (unless (and arg (file-exists-p arg))
981 (error "Argument %s is not a valid file name" arg))
982 (setq arg (expand-file-name arg))
983 (if (file-directory-p arg)
984 ;; Directory as argument
985 (dolist (src (condition-case nil
987 arg nil semantic-grammar-file-regexp)
989 (error "Unable to read directory files"))))
990 (or (semantic-grammar-batch-build-one-package
991 (expand-file-name src arg))
993 ;; Specific file argument
994 (or (semantic-grammar-batch-build-one-package arg)
1000 ;;;; Macros highlighting
1003 (defvar semantic--grammar-macros-regexp-1 nil)
1004 (make-variable-buffer-local 'semantic--grammar-macros-regexp-1)
1006 (defun semantic--grammar-macros-regexp-1 ()
1007 "Return font-lock keyword regexp for pre-installed macro names."
1008 (and semantic-grammar-macros
1009 (not semantic--grammar-macros-regexp-1)
1011 (setq semantic--grammar-macros-regexp-1
1014 (mapcar #'(lambda (e) (symbol-name (car e)))
1015 semantic-grammar-macros)
1019 semantic--grammar-macros-regexp-1)
1021 (defconst semantic--grammar-macdecl-re
1022 "\\<%use-macros\\>[ \t\r\n]+\\(\\sw\\|\\s_\\)+[ \t\r\n]+{"
1023 "Regexp that matches a macro declaration statement.")
1025 (defvar semantic--grammar-macros-regexp-2 nil)
1026 (make-variable-buffer-local 'semantic--grammar-macros-regexp-2)
1028 (defun semantic--grammar-clear-macros-regexp-2 (&rest ignore)
1029 "Clear the cached regexp that match macros local in this grammar.
1031 Added to `before-change-functions' hooks to be run before each text
1033 (setq semantic--grammar-macros-regexp-2 nil))
1035 (defun semantic--grammar-macros-regexp-2 ()
1036 "Return the regexp that match macros local in this grammar."
1037 (unless semantic--grammar-macros-regexp-2
1040 (goto-char (point-min))
1041 (while (re-search-forward semantic--grammar-macdecl-re nil t)
1043 (setq macs (nconc macs
1045 (buffer-substring-no-properties
1054 (setq semantic--grammar-macros-regexp-2
1055 (concat "(\\s-*" (regexp-opt macs t) "\\>"))))))
1056 semantic--grammar-macros-regexp-2)
1058 (defun semantic--grammar-macros-matcher (end)
1059 "Search for a grammar macro name to highlight.
1060 END is the limit of the search."
1061 (let ((regexp (semantic--grammar-macros-regexp-1)))
1062 (or (and regexp (re-search-forward regexp end t))
1063 (and (setq regexp (semantic--grammar-macros-regexp-2))
1064 (re-search-forward regexp end t)))))
1067 ;;;; Define major mode
1070 (defvar semantic-grammar-syntax-table
1071 (let ((table (make-syntax-table (standard-syntax-table))))
1072 (modify-syntax-entry ?\: "." table) ;; COLON
1073 (modify-syntax-entry ?\> "." table) ;; GT
1074 (modify-syntax-entry ?\< "." table) ;; LT
1075 (modify-syntax-entry ?\| "." table) ;; OR
1076 (modify-syntax-entry ?\; ". 12" table) ;; SEMI, Comment start ;;
1077 (modify-syntax-entry ?\n ">" table) ;; Comment end
1078 (modify-syntax-entry ?\" "\"" table) ;; String
1079 (modify-syntax-entry ?\% "w" table) ;; Word
1080 (modify-syntax-entry ?\- "_" table) ;; Symbol
1081 (modify-syntax-entry ?\. "_" table) ;; Symbol
1082 (modify-syntax-entry ?\\ "\\" table) ;; Quote
1083 (modify-syntax-entry ?\` "'" table) ;; Prefix ` (backquote)
1084 (modify-syntax-entry ?\' "'" table) ;; Prefix ' (quote)
1085 (modify-syntax-entry ?\, "'" table) ;; Prefix , (comma)
1086 (modify-syntax-entry ?\# "'" table) ;; Prefix # (sharp)
1088 "Syntax table used in a Semantic grammar buffers.")
1090 (defvar semantic-grammar-mode-hook nil
1091 "Hook run when starting Semantic grammar mode.")
1093 (defvar semantic-grammar-mode-keywords-1
1094 `(("\\(\\<%%\\>\\|\\<%[{}]\\)"
1095 0 font-lock-reference-face)
1096 ("\\(%\\)\\(\\(\\sw\\|\\s_\\)+\\)"
1097 (1 font-lock-reference-face)
1098 (2 font-lock-keyword-face))
1100 0 (unless (semantic-grammar-in-lisp-p) 'bold))
1101 ("^\\(\\(\\sw\\|\\s_\\)+\\)[ \n\r\t]*:"
1102 1 font-lock-function-name-face)
1103 (semantic--grammar-macros-matcher
1104 1 ,(if (boundp 'font-lock-builtin-face)
1105 'font-lock-builtin-face
1106 'font-lock-preprocessor-face))
1107 ("\\$\\(\\sw\\|\\s_\\)*"
1108 0 font-lock-variable-name-face)
1109 ("<\\(\\(\\sw\\|\\s_\\)+\\)>"
1110 1 font-lock-type-face)
1111 (,semantic-grammar-lex-c-char-re
1112 0 ,(if (boundp 'font-lock-constant-face)
1113 'font-lock-constant-face
1114 'font-lock-string-face) t)
1115 ;; Must highlight :keyword here, because ':' is a punctuation in
1117 ("[\r\n\t ]+:\\sw+\\>"
1118 0 font-lock-builtin-face)
1119 ;; Append the Semantic keywords
1120 ,@semantic-fw-font-lock-keywords
1122 "Font Lock keywords used to highlight Semantic grammar buffers.")
1124 (defvar semantic-grammar-mode-keywords-2
1125 (append semantic-grammar-mode-keywords-1
1126 lisp-font-lock-keywords-1)
1127 "Font Lock keywords used to highlight Semantic grammar buffers.")
1129 (defvar semantic-grammar-mode-keywords-3
1130 (append semantic-grammar-mode-keywords-1
1131 lisp-font-lock-keywords-2)
1132 "Font Lock keywords used to highlight Semantic grammar buffers.")
1134 (defvar semantic-grammar-mode-keywords
1135 semantic-grammar-mode-keywords-1
1136 "Font Lock keywords used to highlight Semantic grammar buffers.")
1138 (defvar semantic-grammar-map
1139 (let ((km (make-sparse-keymap)))
1141 (define-key km "|" 'semantic-grammar-electric-punctuation)
1142 (define-key km ";" 'semantic-grammar-electric-punctuation)
1143 (define-key km "%" 'semantic-grammar-electric-punctuation)
1144 (define-key km "(" 'semantic-grammar-electric-punctuation)
1145 (define-key km ")" 'semantic-grammar-electric-punctuation)
1146 (define-key km ":" 'semantic-grammar-electric-punctuation)
1148 (define-key km "\t" 'semantic-grammar-indent)
1149 (define-key km "\M-\t" 'semantic-grammar-complete)
1150 (define-key km "\C-c\C-c" 'semantic-grammar-create-package)
1151 (define-key km "\C-cm" 'semantic-grammar-find-macro-expander)
1152 (define-key km "\C-cik" 'semantic-grammar-insert-keyword)
1153 ;; (define-key km "\C-cc" 'semantic-grammar-generate-and-load)
1154 ;; (define-key km "\C-cr" 'semantic-grammar-generate-one-rule)
1157 "Keymap used in `semantic-grammar-mode'.")
1159 (defvar semantic-grammar-menu
1161 ["Indent Line" semantic-grammar-indent]
1162 ["Complete Symbol" semantic-grammar-complete]
1163 ["Find Macro" semantic-grammar-find-macro-expander]
1165 ["Insert %keyword" semantic-grammar-insert-keyword]
1167 ["Update Lisp Package" semantic-grammar-create-package]
1168 ["Recreate Lisp Package" semantic-grammar-recreate-package]
1170 "Common semantic grammar menu.")
1172 (defun semantic-grammar-setup-menu-emacs (symbol mode-menu)
1173 "Setup a GNU Emacs grammar menu in variable SYMBOL.
1174 MODE-MENU is an optional specific menu whose items are appended to the
1175 common grammar menu."
1176 (let ((items (make-symbol "items")))
1177 `(unless (boundp ',symbol)
1178 (easy-menu-define ,symbol (current-local-map)
1179 "Grammar Menu" semantic-grammar-menu)
1180 (let ((,items (cdr ,mode-menu)))
1182 (easy-menu-add-item ,symbol nil "--")
1184 (easy-menu-add-item ,symbol nil (car ,items))
1185 (setq ,items (cdr ,items))))))
1188 (defun semantic-grammar-setup-menu-xemacs (symbol mode-menu)
1189 "Setup an XEmacs grammar menu in variable SYMBOL.
1190 MODE-MENU is an optional specific menu whose items are appended to the
1191 common grammar menu."
1192 (let ((items (make-symbol "items"))
1193 (path (make-symbol "path")))
1195 (unless (boundp ',symbol)
1196 (easy-menu-define ,symbol nil
1197 "Grammar Menu" (copy-sequence semantic-grammar-menu)))
1198 (easy-menu-add ,symbol)
1199 (let ((,items (cdr ,mode-menu))
1200 (,path (list (car ,symbol))))
1202 (easy-menu-add-item nil ,path "--")
1204 (easy-menu-add-item nil ,path (car ,items))
1205 (setq ,items (cdr ,items))))))
1208 (defmacro semantic-grammar-setup-menu (&optional mode-menu)
1209 "Setup a mode local grammar menu.
1210 MODE-MENU is an optional specific menu whose items are appended to the
1211 common grammar menu."
1212 (let ((menu (intern (format "%s-menu" major-mode))))
1213 (if (featurep 'xemacs)
1214 (semantic-grammar-setup-menu-xemacs menu mode-menu)
1215 (semantic-grammar-setup-menu-emacs menu mode-menu))))
1217 (defsubst semantic-grammar-in-lisp-p ()
1218 "Return non-nil if point is in Lisp code."
1219 (or (>= (point) (semantic-grammar-epilogue-start))
1226 (defun semantic-grammar-edits-new-change-hook-fcn (overlay)
1227 "Function set into `semantic-edits-new-change-hook'.
1228 Argument OVERLAY is the overlay created to mark the change.
1229 When OVERLAY marks a change in the scope of a nonterminal tag extend
1230 the change bounds to encompass the whole nonterminal tag."
1231 (let ((outer (car (semantic-find-tag-by-overlay-in-region
1232 (semantic-edits-os overlay)
1233 (semantic-edits-oe overlay)))))
1234 (if (semantic-tag-of-class-p outer 'nonterminal)
1235 (semantic-overlay-move overlay
1236 (semantic-tag-start outer)
1237 (semantic-tag-end outer)))))
1239 (defun semantic-grammar-mode ()
1240 "Initialize a buffer for editing Semantic grammars.
1242 \\{semantic-grammar-map}"
1244 (kill-all-local-variables)
1245 (setq major-mode 'semantic-grammar-mode
1246 mode-name "Semantic Grammar Framework")
1247 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1248 (set (make-local-variable 'comment-start) ";;")
1249 ;; Look within the line for a ; following an even number of backslashes
1250 ;; after either a non-backslash or the line beginning.
1251 (set (make-local-variable 'comment-start-skip)
1252 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
1253 (set-syntax-table semantic-grammar-syntax-table)
1254 (use-local-map semantic-grammar-map)
1255 (set (make-local-variable 'indent-line-function)
1256 'semantic-grammar-indent)
1257 (set (make-local-variable 'fill-paragraph-function)
1258 'lisp-fill-paragraph)
1259 (set (make-local-variable 'font-lock-multiline)
1261 (set (make-local-variable 'font-lock-defaults)
1262 '((semantic-grammar-mode-keywords
1263 semantic-grammar-mode-keywords-1
1264 semantic-grammar-mode-keywords-2
1265 semantic-grammar-mode-keywords-3)
1266 nil ;; perform string/comment fontification
1267 nil ;; keywords are case sensitive.
1268 ;; This puts _ & - as a word constituant,
1269 ;; simplifying our keywords significantly
1270 ((?_ . "w") (?- . "w"))))
1271 ;; Setup Semantic to parse grammar
1272 (semantic-grammar-wy--install-parser)
1273 (setq semantic-lex-comment-regex ";;"
1274 semantic-lex-analyzer 'semantic-grammar-lexer
1275 semantic-type-relation-separator-character '(":")
1276 semantic-symbol->name-assoc-list
1278 (code . "Setup Code")
1279 (keyword . "Keyword")
1281 (nonterminal . "Nonterminal")
1284 (set (make-local-variable 'semantic-format-face-alist)
1287 (keyword . font-lock-keyword-face)
1288 (token . font-lock-type-face)
1289 (nonterminal . font-lock-function-name-face)
1292 (set (make-local-variable 'semantic-stickyfunc-sticky-classes)
1294 ;; Before each change, clear the cached regexp used to highlight
1295 ;; macros local in this grammar.
1296 (semantic-make-local-hook 'before-change-functions)
1297 (add-hook 'before-change-functions
1298 'semantic--grammar-clear-macros-regexp-2 nil t)
1299 ;; Handle safe re-parse of grammar rules.
1300 (semantic-make-local-hook 'semantic-edits-new-change-hooks)
1301 (add-hook 'semantic-edits-new-change-hooks
1302 'semantic-grammar-edits-new-change-hook-fcn
1304 (semantic-run-mode-hooks 'semantic-grammar-mode-hook))
1307 ;;;; Useful commands
1310 (defvar semantic-grammar-skip-quoted-syntax-table
1311 (let ((st (copy-syntax-table semantic-grammar-syntax-table)))
1312 (modify-syntax-entry ?\' "$" st)
1314 "Syntax table to skip a whole quoted expression in grammar code.
1315 Consider quote as a \"paired delimiter\", so `forward-sexp' will skip
1316 whole quoted expression.")
1318 (defsubst semantic-grammar-backward-item ()
1319 "Move point to beginning of the previous grammar item."
1320 (forward-comment (- (point-max)))
1321 (if (zerop (skip-syntax-backward "."))
1322 (if (eq (char-before) ?\')
1324 ;; Can't be Lisp code here! Temporarily consider quote
1325 ;; as a "paired delimiter", so `forward-sexp' can skip
1326 ;; the whole quoted expression.
1327 semantic-grammar-skip-quoted-syntax-table
1329 (forward-sexp -1))))
1331 (defun semantic-grammar-anchored-indentation ()
1332 "Return indentation based on previous anchor character found."
1336 (semantic-grammar-backward-item)
1340 ((looking-at ":\\(\\s-\\|$\\)")
1341 (setq indent (current-column))
1343 (skip-syntax-forward "-")
1344 (if (eolp) (setq indent 2))
1346 ((and (looking-at "[;%]")
1347 (not (looking-at "\\<%prec\\>")))
1352 (defun semantic-grammar-do-grammar-indent ()
1353 "Indent a line of grammar.
1354 When called the point is not in Lisp code."
1358 (skip-syntax-forward "-")
1359 (setq indent (current-column))
1362 (looking-at "\\(\\w\\|\\s_\\)+\\s-*:")
1363 (and (looking-at "%")
1364 (not (looking-at "%prec\\>"))))
1368 ((and (looking-at ";;")
1369 (save-excursion (forward-comment (point-max))
1373 (setq n (semantic-grammar-anchored-indentation))
1378 ((looking-at "[|;]")
1381 (setq n (+ n 2)))))))
1384 (delete-horizontal-space)
1387 (defvar semantic-grammar-brackets-as-parens-syntax-table
1388 (let ((st (copy-syntax-table emacs-lisp-mode-syntax-table)))
1389 (modify-syntax-entry ?\{ "(} " st)
1390 (modify-syntax-entry ?\} "){ " st)
1392 "Syntax table that consider brackets as parenthesis.
1393 So `lisp-indent-line' will work inside bracket blocks.")
1395 (defun semantic-grammar-do-lisp-indent ()
1396 "Maybe run the Emacs Lisp indenter on a line of code.
1397 Return nil if not in a Lisp expression."
1401 (skip-chars-forward "\t ")
1402 (let ((first (point)))
1403 (or (>= first (semantic-grammar-epilogue-start))
1411 (narrow-to-region (point) first)
1412 (goto-char (point-max))
1414 ;; Temporarily consider brackets as parenthesis so
1415 ;; `lisp-indent-line' can indent Lisp code inside
1417 semantic-grammar-brackets-as-parens-syntax-table
1418 (lisp-indent-line))))
1422 (defun semantic-grammar-indent ()
1423 "Indent the current line.
1424 Use the Lisp or grammar indenter depending on point location."
1426 (let ((orig (point))
1428 (or (semantic-grammar-do-lisp-indent)
1429 (semantic-grammar-do-grammar-indent))
1430 (setq first (save-excursion
1432 (skip-chars-forward "\t ")
1434 (if (or (< orig first) (/= orig (point)))
1435 (goto-char first))))
1437 (defun semantic-grammar-electric-punctuation ()
1438 "Insert and reindent for the symbol just typed in."
1440 (self-insert-command 1)
1442 (semantic-grammar-indent)))
1444 (defun semantic-grammar-complete ()
1445 "Attempt to complete the symbol under point.
1446 Completion is position sensitive. If the cursor is in a match section of
1447 a rule, then nonterminals symbols are scanned. If the cursor is in a Lisp
1448 expression then Lisp symbols are completed."
1450 (if (semantic-grammar-in-lisp-p)
1451 ;; We are in lisp code. Do lisp completion.
1452 (lisp-complete-symbol)
1453 ;; We are not in lisp code. Do rule completion.
1454 (let* ((nonterms (semantic-find-tags-by-class 'nonterminal (current-buffer)))
1455 (sym (car (semantic-ctxt-current-symbol)))
1456 (ans (try-completion sym nonterms)))
1459 (message "Symbols is already complete"))
1460 ((and (stringp ans) (string= ans sym))
1461 ;; Max matchable. Show completions.
1462 (let ((all (all-completions sym nonterms)))
1463 (with-output-to-temp-buffer "*Completions*"
1464 (display-completion-list (all-completions sym nonterms)))
1467 ;; Expand the completions
1469 (delete-region (point) (progn (forward-sexp 1) (point)))
1471 (t (message "No Completions."))
1475 (defun semantic-grammar-insert-keyword (name)
1476 "Insert a new %keyword declaration with NAME.
1477 Assumes it is typed in with the correct casing."
1478 (interactive "sKeyword: ")
1479 (if (not (bolp)) (insert "\n"))
1480 (insert "%keyword " (upcase name) " \"" name "\"
1481 %put " (upcase name) " summary
1485 ;;; Macro facilities
1488 (defsubst semantic--grammar-macro-function-tag (name)
1489 "Search for a function tag for the grammar macro with name NAME.
1490 Return the tag found or nil if not found."
1491 (car (semantic-find-tags-by-class
1493 (or (semantic-find-tags-by-name name (current-buffer))
1494 (and (featurep 'semanticdb)
1495 semanticdb-current-database
1496 (cdar (semanticdb-find-tags-by-name name nil t)))))))
1498 (defsubst semantic--grammar-macro-lib-part (def)
1499 "Return the library part of the grammar macro defined by DEF."
1500 (let ((suf (format "-%s\\'" (regexp-quote (symbol-name (car def)))))
1501 (fun (symbol-name (cdr def))))
1502 (substring fun 0 (string-match suf fun))))
1504 (defun semantic--grammar-macro-compl-elt (def &optional full)
1505 "Return a completion entry for the grammar macro defined by DEF.
1506 If optional argument FULL is non-nil qualify the macro name with the
1507 library found in DEF."
1508 (let ((mac (car def))
1509 (lib (semantic--grammar-macro-lib-part def)))
1511 (format "%s/%s" mac lib)
1515 (defun semantic--grammar-macro-compl-dict ()
1516 "Return a completion dictionnary of macro definitions."
1517 (let ((defs (semantic-grammar-macros))
1520 (setq def (car defs)
1522 (if (or (assoc (car def) defs) (assoc (car def) dups))
1524 (push (semantic--grammar-macro-compl-elt def) dict)))
1526 (setq def (car dups)
1528 (push (semantic--grammar-macro-compl-elt def t) dict))
1531 (defun semantic-grammar-find-macro-expander (macro-name library)
1532 "Visit the Emacs Lisp library where a grammar macro is implemented.
1533 MACRO-NAME is a symbol that identifies a grammar macro.
1534 LIBRARY is the name (sans extension) of the Emacs Lisp library where
1535 to start searching the macro implementation. Lookup in included
1536 libraries, if necessary.
1537 Find a function tag (in current tags table) whose name contains MACRO-NAME.
1538 Select the buffer containing the tag's definition, and move point there."
1540 (let* ((dic (semantic--grammar-macro-compl-dict))
1541 (def (assoc (completing-read "Macro: " dic nil 1) dic)))
1542 (or (cdr def) '(nil nil))))
1543 (when (and macro-name library)
1544 (let* ((lib (format "%s.el" library))
1545 (buf (find-file-noselect (or (locate-library lib t) lib)))
1546 (tag (with-current-buffer buf
1547 (semantic--grammar-macro-function-tag
1548 (format "%s-%s" library macro-name)))))
1551 (pop-to-buffer (semantic-tag-buffer tag))
1552 (goto-char (semantic-tag-start tag))
1553 (semantic-momentary-highlight-tag tag))
1555 (message "No expander found in library %s for macro %s"
1556 library macro-name)))))
1561 (defvar semantic-grammar-syntax-help
1564 ("symbol" . "Syntax: A symbol of alpha numeric and symbol characters")
1565 ("number" . "Syntax: Numeric characters.")
1566 ("punctuation" . "Syntax: Punctuation character.")
1567 ("semantic-list" . "Syntax: A list delimited by any valid list characters")
1568 ("open-paren" . "Syntax: Open Parenthesis character")
1569 ("close-paren" . "Syntax: Close Parenthesis character")
1570 ("string" . "Syntax: String character delimited text")
1571 ("comment" . "Syntax: Comment character delimited text")
1573 ("EMPTY" . "Syntax: Match empty text")
1574 ("ASSOC" . "Lambda Key: (ASSOC key1 value1 key2 value2 ...)")
1575 ("EXPAND" . "Lambda Key: (EXPAND <list id> <rule>)")
1576 ("EXPANDFULL" . "Lambda Key: (EXPANDFULL <list id> <rule>)")
1577 ;; Tag Generator Macros
1578 ("TAG" . "Generic Tag Generation: (TAG <name> <tag-class> [ :key value ]*)")
1579 ("VARIABLE-TAG" . "(VARIABLE-TAG <name> <lang-type> <default-value> [ :key value ]*)")
1580 ("FUNCTION-TAG" . "(FUNCTION-TAG <name> <lang-type> <arg-list> [ :key value ]*)")
1581 ("TYPE-TAG" . "(TYPE-TAG <name> <lang-type> <part-list> <parents> [ :key value ]*)")
1582 ("INCLUDE-TAG" . "(INCLUDE-TAG <name> <system-flag> [ :key value ]*)")
1583 ("PACKAGE-TAG" . "(PACKAGE-TAG <name> <detail> [ :key value ]*)")
1584 ("CODE-TAG" . "(CODE-TAG <name> <detail> [ :key value ]*)")
1585 ("ALIAS-TAG" . "(ALIAS-TAG <name> <aliasclass> <definition> [:key value]*)")
1586 ;; Special value macros
1587 ("$1" . "Match Value: Value from match list in slot 1")
1588 ("$2" . "Match Value: Value from match list in slot 2")
1589 ("$3" . "Match Value: Value from match list in slot 3")
1590 ("$4" . "Match Value: Value from match list in slot 4")
1591 ("$5" . "Match Value: Value from match list in slot 5")
1592 ("$6" . "Match Value: Value from match list in slot 6")
1593 ("$7" . "Match Value: Value from match list in slot 7")
1594 ("$8" . "Match Value: Value from match list in slot 8")
1595 ("$9" . "Match Value: Value from match list in slot 9")
1596 ;; Same, but with annoying , in front.
1597 (",$1" . "Match Value: Value from match list in slot 1")
1598 (",$2" . "Match Value: Value from match list in slot 2")
1599 (",$3" . "Match Value: Value from match list in slot 3")
1600 (",$4" . "Match Value: Value from match list in slot 4")
1601 (",$5" . "Match Value: Value from match list in slot 5")
1602 (",$6" . "Match Value: Value from match list in slot 6")
1603 (",$7" . "Match Value: Value from match list in slot 7")
1604 (",$8" . "Match Value: Value from match list in slot 8")
1605 (",$9" . "Match Value: Value from match list in slot 9")
1607 "Association of syntax elements, and the corresponding help.")
1611 (require 'semantic-ctxt)
1613 ((fboundp 'eldoc-function-argstring-from-docstring)
1614 (defalias 'semantic-grammar-function-argstring-from-docstring
1615 'eldoc-function-argstring-from-docstring)
1617 ((fboundp 'help-split-fundoc)
1618 (defsubst semantic-grammar-function-argstring-from-docstring (fun)
1619 (help-split-fundoc (documentation fun t) fun))
1622 (defalias 'semantic-grammar-function-argstring-from-docstring
1627 (defun semantic-grammar-eldoc-get-macro-docstring (macro expander)
1628 "Return a one-line docstring for the given grammar MACRO.
1629 EXPANDER is the name of the function that expands MACRO."
1630 (if (and (eq expander (aref eldoc-last-data 0))
1631 (eq 'function (aref eldoc-last-data 2)))
1632 (aref eldoc-last-data 1)
1633 (let ((doc (semantic-grammar-function-argstring-from-docstring expander)))
1636 (setq doc (car doc))
1637 (string-match "\\`[^ )]* ?" doc)
1638 (setq doc (concat "(" (substring doc (match-end 0)))))
1640 (setq doc (eldoc-function-argstring expander))))
1642 (setq doc (eldoc-docstring-format-sym-doc
1643 macro (format "==> %s %s" expander doc)))
1644 (eldoc-last-data-store expander doc 'function))
1647 (define-mode-local-override semantic-idle-summary-current-symbol-info
1648 semantic-grammar-mode ()
1649 "Display additional eldoc information about grammar syntax elements.
1650 Syntax element is the current symbol at point.
1651 If it is associated a help string in `semantic-grammar-syntax-help',
1653 If it is a macro name, return a description of the associated expander
1654 function parameter list.
1655 If it is a function name, return a description of this function
1657 It it is a variable name, return a brief (one-line) documentation
1658 string for the variable.
1659 If a default description of the current context can be obtained,
1661 Otherwise return nil."
1662 (let* ((elt (car (semantic-ctxt-current-symbol)))
1663 (val (and elt (cdr (assoc elt semantic-grammar-syntax-help)))))
1664 (when (and (not val) elt (semantic-grammar-in-lisp-p))
1665 ;; Ensure to load macro definitions before doing `intern-soft'.
1666 (setq val (semantic-grammar-macros)
1667 elt (intern-soft elt)
1668 val (and elt (cdr (assq elt val))))
1671 ((and val (fboundp val))
1672 (setq val (semantic-grammar-eldoc-get-macro-docstring elt val)))
1674 ((and elt (fboundp elt))
1675 (setq val (eldoc-get-fnsym-args-string elt)))
1677 ((and elt (boundp elt))
1678 (setq val (eldoc-get-var-docstring elt)))
1680 (or val (semantic-idle-summary-current-symbol-info-default))))
1682 (define-mode-local-override semantic-tag-boundary-p
1683 semantic-grammar-mode (tag)
1684 "Return non-nil for tags that should have a boundary drawn.
1685 Only tags of type 'nonterminal will be so marked."
1686 (let ((c (semantic-tag-class tag)))
1687 (eq c 'nonterminal)))
1689 (define-mode-local-override semantic-ctxt-current-function
1690 semantic-grammar-mode (&optional point)
1691 "Determine the name of the current function at POINT."
1693 (and point (goto-char point))
1694 (when (semantic-grammar-in-lisp-p)
1695 (with-mode-local emacs-lisp-mode
1696 (semantic-ctxt-current-function)))))
1698 (define-mode-local-override semantic-ctxt-current-argument
1699 semantic-grammar-mode (&optional point)
1700 "Determine the argument index of the called function at POINT."
1702 (and point (goto-char point))
1703 (when (semantic-grammar-in-lisp-p)
1704 (with-mode-local emacs-lisp-mode
1705 (semantic-ctxt-current-argument)))))
1707 (define-mode-local-override semantic-ctxt-current-assignment
1708 semantic-grammar-mode (&optional point)
1709 "Determine the tag being assigned into at POINT."
1711 (and point (goto-char point))
1712 (when (semantic-grammar-in-lisp-p)
1713 (with-mode-local emacs-lisp-mode
1714 (semantic-ctxt-current-assignment)))))
1716 (define-mode-local-override semantic-ctxt-current-class-list
1717 semantic-grammar-mode (&optional point)
1718 "Determine the class of tags that can be used at POINT."
1720 (and point (goto-char point))
1721 (if (semantic-grammar-in-lisp-p)
1722 (with-mode-local emacs-lisp-mode
1723 (semantic-ctxt-current-class-list))
1724 '(nonterminal keyword))))
1726 (define-mode-local-override semantic-ctxt-current-mode
1727 semantic-grammar-mode (&optional point)
1728 "Return the major mode active at POINT.
1729 POINT defaults to the value of point in current buffer.
1730 Return `emacs-lisp-mode' is POINT is within Lisp code, otherwise
1731 return the current major mode."
1733 (and point (goto-char point))
1734 (if (semantic-grammar-in-lisp-p)
1736 (semantic-ctxt-current-mode-default))))
1738 (define-mode-local-override semantic-format-tag-abbreviate
1739 semantic-grammar-mode (tag &optional parent color)
1740 "Return a string abbreviation of TAG.
1741 Optional PARENT is not used.
1742 Optional COLOR is used to flag if color is added to the text."
1743 (let ((class (semantic-tag-class tag))
1744 (name (semantic-format-tag-name tag parent color)))
1746 ((eq class 'nonterminal)
1748 ((eq class 'setting)
1750 ((memq class '(rule keyword))
1753 (concat "%" (symbol-name class) " " name)))))
1755 (define-mode-local-override semantic-format-tag-summarize
1756 semantic-grammar-mode (tag &optional parent color)
1757 "Return a string summarizing TAG.
1758 Optional PARENT is not used.
1759 Optional argument COLOR determines if color is added to the text."
1760 (let ((class (semantic-tag-class tag))
1761 (name (semantic-format-tag-name tag parent color))
1765 ((eq class 'nonterminal)
1766 (setq label "Nonterminal: "
1768 " with %d match lists."
1769 (length (semantic-tag-components tag)))))
1770 ((eq class 'keyword)
1771 (setq label "Keyword: ")
1773 (semantic--find-tags-by-function
1776 (setq summary (cdr (assoc "summary"
1777 (semantic-tag-get-attribute
1779 ;; Get `put' tag with TAG name.
1780 (semantic-find-tags-by-name-regexp
1781 (regexp-quote (semantic-tag-name tag))
1782 (semantic-find-tags-by-class 'put (current-buffer))))
1783 (setq desc (concat " = "
1784 (semantic-tag-get-attribute tag :value)
1786 (concat " - " (read summary))
1789 (setq label "Token: ")
1790 (let ((val (semantic-tag-get-attribute tag :value))
1791 (names (semantic-tag-get-attribute tag :rest))
1792 (type (semantic-tag-type tag)))
1794 (setq name (mapconcat 'identity (cons name names) " ")))
1797 (format " <%s>" type)
1800 (format "%s%S" val (if type " " ""))
1803 (setq label "Assoc: ")
1804 (let ((val (semantic-tag-get-attribute tag :value))
1805 (type (semantic-tag-type tag)))
1808 (format " <%s>" type)
1811 (concat " " (mapconcat 'identity val " "))
1814 (setq desc (semantic-format-tag-abbreviate tag parent color))))
1815 (if (and color label)
1816 (setq label (semantic--format-colorize-text label 'label)))
1817 (if (and color label desc)
1818 (setq desc (semantic--format-colorize-text desc 'comment)))
1820 (concat label name desc)
1821 ;; Just a description is the abbreviated version
1824 ;;; Semantic Analysis
1827 (require 'semantic-analyze))
1829 (define-mode-local-override semantic-analyze-current-context
1830 semantic-grammar-mode (point)
1831 "Provide a semantic analysis object describing a context in a grammar."
1832 (if (semantic-grammar-in-lisp-p)
1833 (with-mode-local emacs-lisp-mode
1834 (semantic-analyze-current-context point))
1836 (let* ((context-return nil)
1837 (startpoint (point))
1838 (prefixandbounds (semantic-analyze-calculate-bounds))
1839 (prefix (car prefixandbounds))
1840 (endsym (nth 1 prefixandbounds))
1841 (bounds (nth 2 prefixandbounds))
1843 (prefixclass (semantic-ctxt-current-class-list))
1846 ;; Do context for rules when in a match list.
1848 (semantic-find-first-tag-by-name
1852 (setq context-return
1853 (semantic-analyze-context
1854 "context-for-semantic-grammar"
1855 :buffer (current-buffer)
1860 :prefix (if prefixsym
1864 :prefixclass prefixclass
1869 (define-mode-local-override semantic-analyze-possible-completions
1870 semantic-grammar-mode (context)
1871 "Return a list of possible completions based on CONTEXT."
1872 (if (semantic-grammar-in-lisp-p)
1873 (with-mode-local emacs-lisp-mode
1874 (semantic-analyze-possible-completions context))
1876 (set-buffer (oref context buffer))
1877 (let* ((prefix (car (oref context :prefix)))
1878 (completetext (cond ((semantic-tag-p prefix)
1879 (semantic-tag-name prefix))
1882 ((stringp (car prefix))
1884 (tags (semantic-find-tags-for-completion completetext
1886 (semantic-analyze-tags-of-class-list
1887 tags (oref context prefixclass)))
1890 (provide 'semantic-grammar)
1892 ;;; semantic-grammar.el ends here