1 ;;; semantic/grammar.el --- Major mode framework for Semantic grammars
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 ;; Free Software Foundation, Inc.
6 ;; Author: David Ponce <david@dponce.com>
7 ;; Maintainer: David Ponce <david@dponce.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Major mode framework for editing Semantic's input grammar files.
34 (require 'semantic/ctxt)
35 (require 'semantic/format)
36 (require 'semantic/grammar-wy)
37 (require 'semantic/idle)
38 (declare-function semantic-momentary-highlight-tag "semantic/decorate")
39 (declare-function semantic-analyze-context "semantic/analyze")
40 (declare-function semantic-analyze-tags-of-class-list
41 "semantic/analyze/complete")
45 (require 'semantic/edit)
46 (require 'semantic/find))
53 (defconst semantic-grammar-lex-c-char-re "'\\s\\?.'"
54 "Regexp matching C-like character literals.")
56 ;; Most of the analyzers are auto-generated from the grammar, but the
57 ;; following which need special handling code.
59 (define-lex-regex-analyzer semantic-grammar-lex-prologue
60 "Detect and create a prologue token."
62 ;; Zing to the end of this brace block.
63 (semantic-lex-push-token
67 (semantic-lex-unterminated-syntax-protection 'PROLOGUE
72 (defsubst semantic-grammar-epilogue-start ()
73 "Return the start position of the grammar epilogue."
75 (goto-char (point-min))
76 (if (re-search-forward "^\\s-*\\<%%\\>\\s-*$" nil t 2)
80 (define-lex-regex-analyzer semantic-grammar-lex-epilogue
81 "Detect and create an epilogue or percent-percent token."
83 (let ((start (match-beginning 0))
85 (class 'PERCENT_PERCENT))
86 (when (>= start (semantic-grammar-epilogue-start))
89 (semantic-lex-push-token
90 (semantic-lex-token class start end))))
92 (define-lex semantic-grammar-lexer
93 "Lexical analyzer that handles Semantic grammar buffers.
94 It ignores whitespaces, newlines and comments."
95 semantic-lex-ignore-newline
96 semantic-lex-ignore-whitespace
97 ;; Must detect prologue/epilogue before other symbols/keywords!
98 semantic-grammar-lex-prologue
99 semantic-grammar-lex-epilogue
100 semantic-grammar-wy--<keyword>-keyword-analyzer
101 semantic-grammar-wy--<symbol>-regexp-analyzer
102 semantic-grammar-wy--<char>-regexp-analyzer
103 semantic-grammar-wy--<string>-sexp-analyzer
104 ;; Must detect comments after strings because `comment-start-skip'
105 ;; regexp match semicolons inside strings!
106 semantic-lex-ignore-comments
107 ;; Must detect prefixed list before punctuation because prefix chars
108 ;; are also punctuations!
109 semantic-grammar-wy--<qlist>-sexp-analyzer
110 ;; Must detect punctuations after comments because the semicolon can
111 ;; be a punctuation or a comment start!
112 semantic-grammar-wy--<punctuation>-string-analyzer
113 semantic-grammar-wy--<block>-block-analyzer
114 semantic-grammar-wy--<sexp>-sexp-analyzer)
118 (defun semantic-grammar-lex-buffer ()
119 "Run `semantic-grammar-lex' on current buffer."
122 (setq semantic-lex-analyzer 'semantic-grammar-lexer)
124 (semantic-lex (point-min) (point-max))))
125 (with-current-buffer (get-buffer-create "*semantic-grammar-lex*")
127 (pp token-stream (current-buffer))
128 (goto-char (point-min))
129 (pop-to-buffer (current-buffer)))))
132 ;;;; Semantic action expansion
135 (defun semantic-grammar-ASSOC (&rest args)
136 "Return expansion of built-in ASSOC expression.
137 ARGS are ASSOC's key value list."
139 `(semantic-tag-make-assoc-list
140 ,@(mapcar #'(lambda (i)
145 (setq key (not key))))
148 (defsubst semantic-grammar-quote-p (sym)
149 "Return non-nil if SYM is bound to the `quote' function."
151 (eq (indirect-function sym)
152 (indirect-function 'quote))
155 (defsubst semantic-grammar-backquote-p (sym)
156 "Return non-nil if SYM is bound to the `backquote' function."
158 (eq (indirect-function sym)
159 (indirect-function 'backquote))
163 ;;;; API to access grammar tags
166 (define-mode-local-override semantic-tag-components
167 semantic-grammar-mode (tag)
168 "Return the children of tag TAG."
169 (semantic-tag-get-attribute tag :children))
171 (defun semantic-grammar-first-tag-name (class)
172 "Return the name of the first tag of class CLASS found.
173 Warn if other tags of class CLASS exist."
174 (let* ((tags (semantic-find-tags-by-class
175 class (current-buffer))))
178 (semantic-tag-name (car tags))
180 (message "*** Ignore all but first declared %s"
183 (defun semantic-grammar-tag-symbols (class)
184 "Return the list of symbols defined in tags of class CLASS.
185 That is tag names plus names defined in tag attribute `:rest'."
186 (let* ((tags (semantic-find-tags-by-class
187 class (current-buffer))))
193 (cons (semantic-tag-name tag)
194 (semantic-tag-get-attribute tag :rest))))
197 (defsubst semantic-grammar-item-text (item)
198 "Return the readable string form of ITEM."
199 (if (string-match semantic-grammar-lex-c-char-re item)
200 (concat "?" (substring item 1 -1))
203 (defsubst semantic-grammar-item-value (item)
204 "Return symbol or character value of ITEM string."
205 (if (string-match semantic-grammar-lex-c-char-re item)
206 (let ((c (read (concat "?" (substring item 1 -1)))))
207 (if (featurep 'xemacs)
208 ;; Handle characters as integers in XEmacs like in GNU Emacs.
213 (defun semantic-grammar-prologue ()
214 "Return grammar prologue code as a string value."
215 (let ((tag (semantic-find-first-tag-by-name
217 (semantic-find-tags-by-class 'code (current-buffer)))))
223 (goto-char (semantic-tag-start tag))
224 (skip-chars-forward "%{\r\n\t ")
227 (goto-char (semantic-tag-end tag))
228 (skip-chars-backward "\r\n\t %}")
233 (defun semantic-grammar-epilogue ()
234 "Return grammar epilogue code as a string value."
235 (let ((tag (semantic-find-first-tag-by-name
237 (semantic-find-tags-by-class 'code (current-buffer)))))
243 (goto-char (semantic-tag-start tag))
244 (skip-chars-forward "%\r\n\t ")
247 (goto-char (semantic-tag-end tag))
248 (skip-chars-backward "\r\n\t")
249 ;; If a grammar footer is found, skip it.
250 (re-search-backward "^;;;\\s-+\\S-+\\s-+ends here"
255 (skip-chars-backward "\r\n\t")
260 (defsubst semantic-grammar-buffer-file (&optional buffer)
261 "Return name of file sans directory BUFFER is visiting.
262 No argument or nil as argument means use the current buffer."
263 (file-name-nondirectory (buffer-file-name buffer)))
265 (defun semantic-grammar-package ()
266 "Return the %package value as a string.
267 If there is no %package statement in the grammar, return a default
268 package name derived from the grammar file name. For example, the
269 default package name for the grammar file foo.wy is foo-wy, and for
270 foo.by it is foo-by."
271 (or (semantic-grammar-first-tag-name 'package)
272 (let* ((file (semantic-grammar-buffer-file))
273 (ext (file-name-extension file))
274 (i (string-match (format "\\([.]\\)%s\\'" ext) file)))
275 (concat (substring file 0 i) "-" ext))))
277 (defsubst semantic-grammar-languagemode ()
278 "Return the %languagemode value as a list of symbols or nil."
279 (semantic-grammar-tag-symbols 'languagemode))
281 (defsubst semantic-grammar-start ()
282 "Return the %start value as a list of symbols or nil."
283 (semantic-grammar-tag-symbols 'start))
285 (defsubst semantic-grammar-scopestart ()
286 "Return the %scopestart value as a symbol or nil."
287 (intern (or (semantic-grammar-first-tag-name 'scopestart) "nil")))
289 (defsubst semantic-grammar-quotemode ()
290 "Return the %quotemode value as a symbol or nil."
291 (intern (or (semantic-grammar-first-tag-name 'quotemode) "nil")))
293 (defsubst semantic-grammar-keywords ()
294 "Return the language keywords.
295 That is an alist of (VALUE . TOKEN) where VALUE is the string value of
296 the keyword and TOKEN is the terminal symbol identifying the keyword."
299 (cons (semantic-tag-get-attribute key :value)
300 (intern (semantic-tag-name key))))
301 (semantic-find-tags-by-class 'keyword (current-buffer))))
303 (defun semantic-grammar-keyword-properties (keywords)
304 "Return the list of KEYWORDS properties."
305 (let ((puts (semantic-find-tags-by-class
306 'put (current-buffer)))
307 put keys key plist assoc pkey pval props)
313 (cons (semantic-tag-name put)
314 (semantic-tag-get-attribute put :rest))))
318 assoc (rassq key keywords))
320 nil ;;(message "*** %%put to undefined keyword %s ignored" key)
321 (setq key (car assoc)
322 plist (semantic-tag-get-attribute put :value))
324 (setq pkey (intern (caar plist))
325 pval (read (cdar plist))
326 props (cons (list key pkey pval) props)
327 plist (cdr plist))))))
330 (defun semantic-grammar-tokens ()
331 "Return defined lexical tokens.
332 That is an alist (TYPE . DEFS) where type is a %token <type> symbol
333 and DEFS is an alist of (TOKEN . VALUE). TOKEN is the terminal symbol
334 identifying the token and VALUE is the string value of the token or
336 (let (tags alist assoc tag type term names value)
338 ;; Check for <type> in %left, %right & %nonassoc declarations
339 (setq tags (semantic-find-tags-by-class
340 'assoc (current-buffer)))
344 (when (setq type (semantic-tag-type tag))
345 (setq names (semantic-tag-get-attribute tag :value)
346 assoc (assoc type alist))
347 (or assoc (setq assoc (list type)
348 alist (cons assoc alist)))
350 (setq term (car names)
352 (or (string-match semantic-grammar-lex-c-char-re term)
353 (setcdr assoc (cons (list (intern term))
356 ;; Then process %token declarations so they can override any
357 ;; previous specifications
358 (setq tags (semantic-find-tags-by-class
359 'token (current-buffer)))
363 (setq names (cons (semantic-tag-name tag)
364 (semantic-tag-get-attribute tag :rest))
365 type (or (semantic-tag-type tag) "<no-type>")
366 value (semantic-tag-get-attribute tag :value)
367 assoc (assoc type alist))
368 (or assoc (setq assoc (list type)
369 alist (cons assoc alist)))
371 (setq term (intern (car names))
373 (setcdr assoc (cons (cons term value) (cdr assoc)))))
376 (defun semantic-grammar-token-%type-properties (&optional props)
377 "Return properties set by %type statements.
378 This declare a new type if necessary.
379 If optional argument PROPS is non-nil, it is an existing list of
380 properties where to add new properties."
382 (dolist (tag (semantic-find-tags-by-class 'type (current-buffer)))
383 (setq type (semantic-tag-name tag))
384 ;; Indicate to auto-generate the analyzer for this type
385 (push (list type :declared t) props)
386 (dolist (e (semantic-tag-get-attribute tag :value))
387 (push (list type (intern (car e)) (read (or (cdr e) "nil")))
391 (defun semantic-grammar-token-%put-properties (tokens)
392 "For types found in TOKENS, return properties set by %put statements."
394 (dolist (put (semantic-find-tags-by-class 'put (current-buffer)))
395 (dolist (type (cons (semantic-tag-name put)
396 (semantic-tag-get-attribute put :rest)))
397 (setq found (assoc type tokens))
399 nil ;; %put <type> ignored, no token defined
400 (setq type (car found))
401 (dolist (e (semantic-tag-get-attribute put :value))
402 (push (list type (intern (car e)) (read (or (cdr e) "nil")))
406 (defsubst semantic-grammar-token-properties (tokens)
407 "Return properties of declared types.
408 Types are explicitly declared by %type statements. Types found in
409 TOKENS are those declared implicitly by %token statements.
410 Properties can be set by %put and %type statements.
411 Properties set by %type statements take precedence over those set by
413 (let ((props (semantic-grammar-token-%put-properties tokens)))
414 (semantic-grammar-token-%type-properties props)))
416 (defun semantic-grammar-use-macros ()
417 "Return macro definitions from %use-macros statements.
418 Also load the specified macro libraries."
420 (dolist (tag (semantic-find-tags-by-class 'macro (current-buffer)))
421 (setq lib (intern (semantic-tag-type tag)))
423 ;;(load lib) ;; Be sure to use the latest macro library.
426 (dolist (mac (semantic-tag-get-attribute tag :value))
427 (push (cons (intern mac)
428 (intern (format "%s-%s" lib mac)))
432 (defvar semantic-grammar-macros nil
433 "List of associations (MACRO-NAME . EXPANDER).")
434 (make-variable-buffer-local 'semantic-grammar-macros)
436 (defun semantic-grammar-macros ()
437 "Build and return the alist of defined macros."
439 ;; Definitions found in tags.
440 (semantic-grammar-use-macros)
441 ;; Other pre-installed definitions.
442 semantic-grammar-macros))
445 ;;;; Overloaded functions that build parser data.
448 ;;; Keyword table builder
450 (defun semantic-grammar-keywordtable-builder-default ()
451 "Return the default value of the keyword table."
452 (let ((keywords (semantic-grammar-keywords)))
453 `(semantic-lex-make-keyword-table
455 ',(semantic-grammar-keyword-properties keywords))))
457 (define-overloadable-function semantic-grammar-keywordtable-builder ()
458 "Return the keyword table table value.")
460 ;;; Token table builder
462 (defun semantic-grammar-tokentable-builder-default ()
463 "Return the default value of the table of lexical tokens."
464 (let ((tokens (semantic-grammar-tokens)))
465 `(semantic-lex-make-type-table
467 ',(semantic-grammar-token-properties tokens))))
469 (define-overloadable-function semantic-grammar-tokentable-builder ()
470 "Return the value of the table of lexical tokens.")
472 ;;; Parser table builder
474 (defun semantic-grammar-parsetable-builder-default ()
475 "Return the default value of the parse table."
476 (error "`semantic-grammar-parsetable-builder' not defined"))
478 (define-overloadable-function semantic-grammar-parsetable-builder ()
479 "Return the parser table value.")
481 ;;; Parser setup code builder
483 (defun semantic-grammar-setupcode-builder-default ()
484 "Return the default value of the setup code form."
485 (error "`semantic-grammar-setupcode-builder' not defined"))
487 (define-overloadable-function semantic-grammar-setupcode-builder ()
488 "Return the parser setup code form.")
491 ;;;; Lisp code generation
493 (defvar semantic--grammar-input-buffer nil)
494 (defvar semantic--grammar-output-buffer nil)
496 (defsubst semantic-grammar-keywordtable ()
497 "Return the variable name of the keyword table."
498 (concat (file-name-sans-extension
499 (semantic-grammar-buffer-file
500 semantic--grammar-output-buffer))
503 (defsubst semantic-grammar-tokentable ()
504 "Return the variable name of the token table."
505 (concat (file-name-sans-extension
506 (semantic-grammar-buffer-file
507 semantic--grammar-output-buffer))
510 (defsubst semantic-grammar-parsetable ()
511 "Return the variable name of the parse table."
512 (concat (file-name-sans-extension
513 (semantic-grammar-buffer-file
514 semantic--grammar-output-buffer))
517 (defsubst semantic-grammar-setupfunction ()
518 "Return the name of the parser setup function."
519 (concat (file-name-sans-extension
520 (semantic-grammar-buffer-file
521 semantic--grammar-output-buffer))
524 (defmacro semantic-grammar-as-string (object)
525 "Return OBJECT as a string value."
526 `(if (stringp ,object)
529 (pp-to-string ,object)))
531 (defun semantic-grammar-insert-defconst (name value docstring)
532 "Insert declaration of constant NAME with VALUE and DOCSTRING."
533 (let ((start (point)))
534 (insert (format "(defconst %s\n%s%S)\n\n" name value docstring))
539 (defun semantic-grammar-insert-defun (name body docstring)
540 "Insert declaration of function NAME with BODY and DOCSTRING."
541 (let ((start (point)))
542 (insert (format "(defun %s ()\n%S\n%s)\n\n" name docstring body))
547 (defun semantic-grammar-insert-define (define)
548 "Insert the declaration specified by DEFINE expression.
549 Typically a DEFINE expression should look like this:
551 \(define-thing name docstring expression1 ...)"
553 (let ((start (point)))
554 (insert (format "(%S %S" (car define) (nth 1 define)))
555 (dolist (item (nthcdr 2 define))
558 (pp item (current-buffer)))
564 (defconst semantic-grammar-header-template
566 ;;; " file " --- Generated parser support file
570 ;; Author: " user-full-name " <" user-mail-address ">
575 ;; This file is not part of GNU Emacs.
577 ;; This program is free software; you can redistribute it and/or
578 ;; modify it under the terms of the GNU General Public License as
579 ;; published by the Free Software Foundation, either version 3 of
580 ;; the License, or (at your option) any later version.
582 ;; This software is distributed in the hope that it will be useful,
583 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
584 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
585 ;; General Public License for more details.
587 ;; You should have received a copy of the GNU General Public License
588 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
592 ;; PLEASE DO NOT MANUALLY EDIT THIS FILE! It is automatically
593 ;; generated from the grammar file " gram ".
600 "Generated header template.
601 The symbols in the template are local variables in
602 `semantic-grammar-header'")
604 (defconst semantic-grammar-footer-template
609 ;;; " file " ends here
611 "Generated footer template.
612 The symbols in the list are local variables in
613 `semantic-grammar-footer'.")
615 (defun semantic-grammar-copyright-line ()
616 "Return the grammar copyright line, or nil if not found."
618 (goto-char (point-min))
619 (when (re-search-forward "^;;+[ \t]+Copyright (C) .*$"
620 ;; Search only in the four top lines
621 (save-excursion (forward-line 4) (point))
625 (defun semantic-grammar-header ()
626 "Return text of a generated standard header."
627 (let ((file (semantic-grammar-buffer-file
628 semantic--grammar-output-buffer))
629 (gram (semantic-grammar-buffer-file))
630 (date (format-time-string "%Y-%m-%d %T%z"))
631 (vcid (concat "$" "Id" "$")) ;; Avoid expansion
632 ;; Try to get the copyright from the input grammar, or
633 ;; generate a new one if not found.
634 (copy (or (semantic-grammar-copyright-line)
635 (concat (format-time-string ";; Copyright (C) %Y ")
638 (dolist (S semantic-grammar-header-template)
640 (setq out (concat out S)))
642 (setq out (concat out (symbol-value S))))))
645 (defun semantic-grammar-footer ()
646 "Return text of a generated standard footer."
647 (let* ((file (semantic-grammar-buffer-file
648 semantic--grammar-output-buffer))
649 (libr (file-name-sans-extension file))
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 (file-name-sans-extension
756 (semantic-grammar-buffer-file
757 semantic--grammar-output-buffer))
758 mtype (or (get type 'matchdatatype) 'regexp)
759 name (intern (format "%s--<%s>-%s-analyzer" prefix type mtype))
760 doc (format "%s analyzer for <%s> tokens." mtype type))
762 ;; Regexp match analyzer
764 (semantic-grammar-insert-define
765 `(define-lex-regex-type-analyzer ,name
767 ,(semantic-grammar-quoted-form (cdr type-value))
768 ',(or (car type-value) (intern type-name))))
770 ;; String compare analyzer
772 (semantic-grammar-insert-define
773 `(define-lex-string-type-analyzer ,name
775 ,(semantic-grammar-quoted-form (cdr type-value))
776 ',(or (car type-value) (intern type-name))))
779 ((and (eq mtype 'block)
780 (setq spec (semantic-grammar--lex-block-specs)))
781 (semantic-grammar-insert-define
782 `(define-lex-block-type-analyzer ,name
784 ,(semantic-grammar-quoted-form spec)))
788 (semantic-grammar-insert-define
789 `(define-lex-sexp-type-analyzer ,name
791 ',(or (car type-value) (intern type-name))))
795 (semantic-grammar-insert-define
796 `(define-lex-keyword-type-analyzer ,name
802 (defun semantic-grammar-insert-defanalyzers ()
803 "Insert declarations of lexical analyzers."
805 (with-current-buffer semantic--grammar-input-buffer
806 (setq tokens (semantic-grammar-tokens)
807 props (semantic-grammar-token-properties tokens)))
808 (insert "(require 'semantic-lex)\n\n")
809 (let ((semantic-lex-types-obarray
810 (semantic-lex-make-type-table tokens props))
811 semantic-grammar--lex-block-specs)
812 (mapatoms 'semantic-grammar-insert-defanalyzer
813 semantic-lex-types-obarray))))
815 ;;; Generation of the grammar support file.
817 (defcustom semantic-grammar-file-regexp "\\.[wb]y$"
818 "Regexp which matches grammar source files."
822 (defsubst semantic-grammar-noninteractive ()
823 "Return non-nil if running without interactive terminal."
824 (if (featurep 'xemacs)
828 (defun semantic-grammar-create-package (&optional force)
829 "Create package Lisp code from grammar in current buffer.
830 Does nothing if the Lisp code seems up to date.
831 If optional argument FORCE is non-nil, unconditionally re-generate the
834 (setq force (or force current-prefix-arg))
835 (semantic-fetch-tags)
837 ;; Values of the following local variables are obtained from
838 ;; the grammar parsed tree in current buffer, that is before
839 ;; switching to the output file.
840 (package (semantic-grammar-package))
841 (output (concat package ".el"))
842 (semantic--grammar-input-buffer (current-buffer))
843 (semantic--grammar-output-buffer (find-file-noselect output))
844 (header (semantic-grammar-header))
845 (prologue (semantic-grammar-prologue))
846 (epilogue (semantic-grammar-epilogue))
847 (footer (semantic-grammar-footer))
850 (not (buffer-modified-p))
851 (file-newer-than-file-p
852 (buffer-file-name semantic--grammar-output-buffer)
853 (buffer-file-name semantic--grammar-input-buffer)))
854 (message "Package `%s' is up to date." package)
855 ;; Create the package
856 (set-buffer semantic--grammar-output-buffer)
857 ;; Use Unix EOLs, so that the file is portable to all platforms.
858 (setq buffer-file-coding-system 'raw-text-unix)
860 (unless (eq major-mode 'emacs-lisp-mode)
863 ;;;; Header + Prologue
866 "
\f\n;;; Prologue\n;;\n"
869 ;; Evaluate the prologue now, because it might provide definition
870 ;; of grammar macro expanders.
871 (eval-region (point-min) (point))
877 (insert "
\f\n;;; Declarations\n;;\n")
879 ;; `eval-defun' is not necessary to reset `defconst' values.
880 (semantic-grammar-insert-defconst
881 (semantic-grammar-keywordtable)
882 (with-current-buffer semantic--grammar-input-buffer
883 (semantic-grammar-keyword-data))
884 "Table of language keywords.")
886 (semantic-grammar-insert-defconst
887 (semantic-grammar-tokentable)
888 (with-current-buffer semantic--grammar-input-buffer
889 (semantic-grammar-token-data))
890 "Table of lexical tokens.")
892 (semantic-grammar-insert-defconst
893 (semantic-grammar-parsetable)
894 (with-current-buffer semantic--grammar-input-buffer
895 (semantic-grammar-parser-data))
898 (semantic-grammar-insert-defun
899 (semantic-grammar-setupfunction)
900 (with-current-buffer semantic--grammar-input-buffer
901 (semantic-grammar-setup-data))
902 "Setup the Semantic Parser.")
905 (insert "
\f\n;;; Analyzers\n;;\n")
907 (semantic-grammar-insert-defanalyzers)
909 ;;;; Epilogue & Footer
911 (insert "
\f\n;;; Epilogue\n;;\n"
920 ;; If running in batch mode, there is nothing more to do.
921 ;; Save the generated file and quit.
922 (if (semantic-grammar-noninteractive)
923 (let ((version-control t)
924 (delete-old-versions t)
925 (make-backup-files t)
926 (vc-make-backup-files t))
927 (kill-buffer (current-buffer)))
928 ;; If running interactively, eval declarations and epilogue
929 ;; code, then pop to the buffer visiting the generated file.
930 (eval-region (point) (point-max))
931 ;; Loop over the defvars and eval them explicitly to force
932 ;; them to be evaluated and ready to use.
933 (goto-char (point-min))
934 (while (re-search-forward "(defvar " nil t)
936 ;; Move cursor to a logical spot in the generated code.
937 (goto-char (point-min))
938 (pop-to-buffer (current-buffer))
939 ;; The generated code has been evaluated and updated into
940 ;; memory. Now find all buffers that match the major modes we
941 ;; have created this language for, and force them to call our
942 ;; setup function again, refreshing all semantic data, and
943 ;; enabling them to work with the new code just created.
945 ;; At this point, I don't know any user's defined setup code :-(
946 ;; At least, what I can do for now, is to run the generated
947 ;; parser-install function.
948 (semantic-map-mode-buffers
949 (semantic-grammar-setupfunction)
950 (semantic-grammar-languagemode)))
952 ;; Return the name of the generated package file.
955 (defun semantic-grammar-recreate-package ()
956 "Unconditionally create Lisp code from grammar in current buffer.
957 Like \\[universal-argument] \\[semantic-grammar-create-package]."
959 (semantic-grammar-create-package t))
961 (defun semantic-grammar-batch-build-one-package (file)
962 "Build a Lisp package from the grammar in FILE.
963 That is, generate Lisp code from FILE, and `byte-compile' it.
964 Return non-nil if there were no errors, nil if errors."
965 ;; We need this require so that we can find `byte-compile-dest-file'.
967 (unless (auto-save-file-name-p file)
968 ;; Create the package
971 (with-current-buffer (find-file-noselect file)
972 (semantic-grammar-create-package))
974 (message "%s" (error-message-string err))
977 ;; Only byte compile if out of date
978 (if (file-newer-than-file-p
979 packagename (byte-compile-dest-file packagename))
980 (let (;; Some complex grammar table expressions need a few
981 ;; more resources than the default.
982 (max-specpdl-size (max 3000 max-specpdl-size))
983 (max-lisp-eval-depth (max 1000 max-lisp-eval-depth))
985 ;; byte compile the resultant file
986 (byte-compile-file packagename))
989 (defun semantic-grammar-batch-build-packages ()
990 "Build Lisp packages from grammar files on the command line.
991 That is, run `semantic-grammar-batch-build-one-package' for each file.
992 Each file is processed even if an error occurred previously.
993 Must be used from the command line, with `-batch'.
994 For example, to process grammar files in current directory, invoke:
996 \"emacs -batch -f semantic-grammar-batch-build-packages .\".
998 See also the variable `semantic-grammar-file-regexp'."
999 (or (semantic-grammar-noninteractive)
1001 `semantic-grammar-batch-build-packages' must be used with -batch"
1004 ;; Remove vc from find-file-hook. It causes bad stuff to
1005 ;; happen in Emacs 20.
1006 (find-file-hook (delete 'vc-find-file-hook find-file-hook)))
1007 (message "Compiling Grammars from: %s" (locate-library "semantic-grammar"))
1008 (dolist (arg command-line-args-left)
1009 (unless (and arg (file-exists-p arg))
1010 (error "Argument %s is not a valid file name" arg))
1011 (setq arg (expand-file-name arg))
1012 (if (file-directory-p arg)
1013 ;; Directory as argument
1014 (dolist (src (condition-case nil
1016 arg nil semantic-grammar-file-regexp)
1018 (error "Unable to read directory files"))))
1019 (or (semantic-grammar-batch-build-one-package
1020 (expand-file-name src arg))
1022 ;; Specific file argument
1023 (or (semantic-grammar-batch-build-one-package arg)
1029 ;;;; Macros highlighting
1032 (defvar semantic--grammar-macros-regexp-1 nil)
1033 (make-variable-buffer-local 'semantic--grammar-macros-regexp-1)
1035 (defun semantic--grammar-macros-regexp-1 ()
1036 "Return font-lock keyword regexp for pre-installed macro names."
1037 (and semantic-grammar-macros
1038 (not semantic--grammar-macros-regexp-1)
1040 (setq semantic--grammar-macros-regexp-1
1043 (mapcar #'(lambda (e) (symbol-name (car e)))
1044 semantic-grammar-macros)
1048 semantic--grammar-macros-regexp-1)
1050 (defconst semantic--grammar-macdecl-re
1051 "\\<%use-macros\\>[ \t\r\n]+\\(\\sw\\|\\s_\\)+[ \t\r\n]+{"
1052 "Regexp that matches a macro declaration statement.")
1054 (defvar semantic--grammar-macros-regexp-2 nil)
1055 (make-variable-buffer-local 'semantic--grammar-macros-regexp-2)
1057 (defun semantic--grammar-clear-macros-regexp-2 (&rest ignore)
1058 "Clear the cached regexp that match macros local in this grammar.
1060 Added to `before-change-functions' hooks to be run before each text
1062 (setq semantic--grammar-macros-regexp-2 nil))
1064 (defun semantic--grammar-macros-regexp-2 ()
1065 "Return the regexp that match macros local in this grammar."
1066 (unless semantic--grammar-macros-regexp-2
1069 (goto-char (point-min))
1070 (while (re-search-forward semantic--grammar-macdecl-re nil t)
1072 (setq macs (nconc macs
1074 (buffer-substring-no-properties
1083 (setq semantic--grammar-macros-regexp-2
1084 (concat "(\\s-*" (regexp-opt macs t) "\\>"))))))
1085 semantic--grammar-macros-regexp-2)
1087 (defun semantic--grammar-macros-matcher (end)
1088 "Search for a grammar macro name to highlight.
1089 END is the limit of the search."
1090 (let ((regexp (semantic--grammar-macros-regexp-1)))
1091 (or (and regexp (re-search-forward regexp end t))
1092 (and (setq regexp (semantic--grammar-macros-regexp-2))
1093 (re-search-forward regexp end t)))))
1096 ;;;; Define major mode
1099 (defvar semantic-grammar-syntax-table
1100 (let ((table (make-syntax-table (standard-syntax-table))))
1101 (modify-syntax-entry ?\: "." table) ;; COLON
1102 (modify-syntax-entry ?\> "." table) ;; GT
1103 (modify-syntax-entry ?\< "." table) ;; LT
1104 (modify-syntax-entry ?\| "." table) ;; OR
1105 (modify-syntax-entry ?\; ". 12" table) ;; SEMI, Comment start ;;
1106 (modify-syntax-entry ?\n ">" table) ;; Comment end
1107 (modify-syntax-entry ?\" "\"" table) ;; String
1108 (modify-syntax-entry ?\% "w" table) ;; Word
1109 (modify-syntax-entry ?\- "_" table) ;; Symbol
1110 (modify-syntax-entry ?\. "_" table) ;; Symbol
1111 (modify-syntax-entry ?\\ "\\" table) ;; Quote
1112 (modify-syntax-entry ?\` "'" table) ;; Prefix ` (backquote)
1113 (modify-syntax-entry ?\' "'" table) ;; Prefix ' (quote)
1114 (modify-syntax-entry ?\, "'" table) ;; Prefix , (comma)
1115 (modify-syntax-entry ?\# "'" table) ;; Prefix # (sharp)
1117 "Syntax table used in a Semantic grammar buffers.")
1119 (defvar semantic-grammar-mode-hook nil
1120 "Hook run when starting Semantic grammar mode.")
1122 (defvar semantic-grammar-mode-keywords-1
1123 `(("\\(\\<%%\\>\\|\\<%[{}]\\)"
1124 0 font-lock-reference-face)
1125 ("\\(%\\)\\(\\(\\sw\\|\\s_\\)+\\)"
1126 (1 font-lock-reference-face)
1127 (2 font-lock-keyword-face))
1129 0 (unless (semantic-grammar-in-lisp-p) 'bold))
1130 ("^\\(\\(\\sw\\|\\s_\\)+\\)[ \n\r\t]*:"
1131 1 font-lock-function-name-face)
1132 (semantic--grammar-macros-matcher
1133 1 ,(if (boundp 'font-lock-builtin-face)
1134 'font-lock-builtin-face
1135 'font-lock-preprocessor-face))
1136 ("\\$\\(\\sw\\|\\s_\\)*"
1137 0 font-lock-variable-name-face)
1138 ("<\\(\\(\\sw\\|\\s_\\)+\\)>"
1139 1 font-lock-type-face)
1140 (,semantic-grammar-lex-c-char-re
1141 0 ,(if (boundp 'font-lock-constant-face)
1142 'font-lock-constant-face
1143 'font-lock-string-face) t)
1144 ;; Must highlight :keyword here, because ':' is a punctuation in
1146 ("[\r\n\t ]+:\\sw+\\>"
1147 0 font-lock-builtin-face)
1148 ;; ;; Append the Semantic keywords
1149 ;; ,@semantic-fw-font-lock-keywords
1151 "Font Lock keywords used to highlight Semantic grammar buffers.")
1153 (defvar semantic-grammar-mode-keywords-2
1154 (append semantic-grammar-mode-keywords-1
1155 lisp-font-lock-keywords-1)
1156 "Font Lock keywords used to highlight Semantic grammar buffers.")
1158 (defvar semantic-grammar-mode-keywords-3
1159 (append semantic-grammar-mode-keywords-1
1160 lisp-font-lock-keywords-2)
1161 "Font Lock keywords used to highlight Semantic grammar buffers.")
1163 (defvar semantic-grammar-mode-keywords
1164 semantic-grammar-mode-keywords-1
1165 "Font Lock keywords used to highlight Semantic grammar buffers.")
1167 (defvar semantic-grammar-map
1168 (let ((km (make-sparse-keymap)))
1170 (define-key km "|" 'semantic-grammar-electric-punctuation)
1171 (define-key km ";" 'semantic-grammar-electric-punctuation)
1172 (define-key km "%" 'semantic-grammar-electric-punctuation)
1173 (define-key km "(" 'semantic-grammar-electric-punctuation)
1174 (define-key km ")" 'semantic-grammar-electric-punctuation)
1175 (define-key km ":" 'semantic-grammar-electric-punctuation)
1177 (define-key km "\t" 'semantic-grammar-indent)
1178 (define-key km "\M-\t" 'semantic-grammar-complete)
1179 (define-key km "\C-c\C-c" 'semantic-grammar-create-package)
1180 (define-key km "\C-cm" 'semantic-grammar-find-macro-expander)
1181 (define-key km "\C-cik" 'semantic-grammar-insert-keyword)
1182 ;; (define-key km "\C-cc" 'semantic-grammar-generate-and-load)
1183 ;; (define-key km "\C-cr" 'semantic-grammar-generate-one-rule)
1186 "Keymap used in `semantic-grammar-mode'.")
1188 (defvar semantic-grammar-menu
1190 ["Indent Line" semantic-grammar-indent]
1191 ["Complete Symbol" semantic-grammar-complete]
1192 ["Find Macro" semantic-grammar-find-macro-expander]
1194 ["Insert %keyword" semantic-grammar-insert-keyword]
1196 ["Update Lisp Package" semantic-grammar-create-package]
1197 ["Recreate Lisp Package" semantic-grammar-recreate-package]
1199 "Common semantic grammar menu.")
1201 (defun semantic-grammar-setup-menu-emacs (symbol mode-menu)
1202 "Setup a GNU Emacs grammar menu in variable SYMBOL.
1203 MODE-MENU is an optional specific menu whose items are appended to the
1204 common grammar menu."
1205 (let ((items (make-symbol "items")))
1206 `(unless (boundp ',symbol)
1207 (easy-menu-define ,symbol (current-local-map)
1208 "Grammar Menu" semantic-grammar-menu)
1209 (let ((,items (cdr ,mode-menu)))
1211 (easy-menu-add-item ,symbol nil "--")
1213 (easy-menu-add-item ,symbol nil (car ,items))
1214 (setq ,items (cdr ,items))))))
1217 (defun semantic-grammar-setup-menu-xemacs (symbol mode-menu)
1218 "Setup an XEmacs grammar menu in variable SYMBOL.
1219 MODE-MENU is an optional specific menu whose items are appended to the
1220 common grammar menu."
1221 (let ((items (make-symbol "items"))
1222 (path (make-symbol "path")))
1224 (unless (boundp ',symbol)
1225 (easy-menu-define ,symbol nil
1226 "Grammar Menu" (copy-sequence semantic-grammar-menu)))
1227 (easy-menu-add ,symbol)
1228 (let ((,items (cdr ,mode-menu))
1229 (,path (list (car ,symbol))))
1231 (easy-menu-add-item nil ,path "--")
1233 (easy-menu-add-item nil ,path (car ,items))
1234 (setq ,items (cdr ,items))))))
1237 (defmacro semantic-grammar-setup-menu (&optional mode-menu)
1238 "Setup a mode local grammar menu.
1239 MODE-MENU is an optional specific menu whose items are appended to the
1240 common grammar menu."
1241 (let ((menu (intern (format "%s-menu" major-mode))))
1242 (if (featurep 'xemacs)
1243 (semantic-grammar-setup-menu-xemacs menu mode-menu)
1244 (semantic-grammar-setup-menu-emacs menu mode-menu))))
1246 (defsubst semantic-grammar-in-lisp-p ()
1247 "Return non-nil if point is in Lisp code."
1248 (or (>= (point) (semantic-grammar-epilogue-start))
1255 (defun semantic-grammar-edits-new-change-hook-fcn (overlay)
1256 "Function set into `semantic-edits-new-change-hook'.
1257 Argument OVERLAY is the overlay created to mark the change.
1258 When OVERLAY marks a change in the scope of a nonterminal tag extend
1259 the change bounds to encompass the whole nonterminal tag."
1260 (let ((outer (car (semantic-find-tag-by-overlay-in-region
1261 (semantic-edits-os overlay)
1262 (semantic-edits-oe overlay)))))
1263 (if (semantic-tag-of-class-p outer 'nonterminal)
1264 (semantic-overlay-move overlay
1265 (semantic-tag-start outer)
1266 (semantic-tag-end outer)))))
1268 (defun semantic-grammar-mode ()
1269 "Initialize a buffer for editing Semantic grammars.
1271 \\{semantic-grammar-map}"
1273 (kill-all-local-variables)
1274 (setq major-mode 'semantic-grammar-mode
1275 mode-name "Semantic Grammar Framework")
1276 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1277 (set (make-local-variable 'comment-start) ";;")
1278 ;; Look within the line for a ; following an even number of backslashes
1279 ;; after either a non-backslash or the line beginning.
1280 (set (make-local-variable 'comment-start-skip)
1281 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
1282 (set-syntax-table semantic-grammar-syntax-table)
1283 (use-local-map semantic-grammar-map)
1284 (set (make-local-variable 'indent-line-function)
1285 'semantic-grammar-indent)
1286 (set (make-local-variable 'fill-paragraph-function)
1287 'lisp-fill-paragraph)
1288 (set (make-local-variable 'font-lock-multiline)
1290 (set (make-local-variable 'font-lock-defaults)
1291 '((semantic-grammar-mode-keywords
1292 semantic-grammar-mode-keywords-1
1293 semantic-grammar-mode-keywords-2
1294 semantic-grammar-mode-keywords-3)
1295 nil ;; perform string/comment fontification
1296 nil ;; keywords are case sensitive.
1297 ;; This puts _ & - as a word constituant,
1298 ;; simplifying our keywords significantly
1299 ((?_ . "w") (?- . "w"))))
1300 ;; Setup Semantic to parse grammar
1301 (semantic-grammar-wy--install-parser)
1302 (setq semantic-lex-comment-regex ";;"
1303 semantic-lex-analyzer 'semantic-grammar-lexer
1304 semantic-type-relation-separator-character '(":")
1305 semantic-symbol->name-assoc-list
1307 (code . "Setup Code")
1308 (keyword . "Keyword")
1310 (nonterminal . "Nonterminal")
1313 (set (make-local-variable 'semantic-format-face-alist)
1316 (keyword . font-lock-keyword-face)
1317 (token . font-lock-type-face)
1318 (nonterminal . font-lock-function-name-face)
1321 (set (make-local-variable 'semantic-stickyfunc-sticky-classes)
1323 ;; Before each change, clear the cached regexp used to highlight
1324 ;; macros local in this grammar.
1325 (semantic-make-local-hook 'before-change-functions)
1326 (add-hook 'before-change-functions
1327 'semantic--grammar-clear-macros-regexp-2 nil t)
1328 ;; Handle safe re-parse of grammar rules.
1329 (semantic-make-local-hook 'semantic-edits-new-change-hooks)
1330 (add-hook 'semantic-edits-new-change-hooks
1331 'semantic-grammar-edits-new-change-hook-fcn
1333 (semantic-run-mode-hooks 'semantic-grammar-mode-hook))
1336 ;;;; Useful commands
1339 (defvar semantic-grammar-skip-quoted-syntax-table
1340 (let ((st (copy-syntax-table semantic-grammar-syntax-table)))
1341 (modify-syntax-entry ?\' "$" st)
1343 "Syntax table to skip a whole quoted expression in grammar code.
1344 Consider quote as a \"paired delimiter\", so `forward-sexp' will skip
1345 whole quoted expression.")
1347 (defsubst semantic-grammar-backward-item ()
1348 "Move point to beginning of the previous grammar item."
1349 (forward-comment (- (point-max)))
1350 (if (zerop (skip-syntax-backward "."))
1351 (if (eq (char-before) ?\')
1353 ;; Can't be Lisp code here! Temporarily consider quote
1354 ;; as a "paired delimiter", so `forward-sexp' can skip
1355 ;; the whole quoted expression.
1356 semantic-grammar-skip-quoted-syntax-table
1358 (forward-sexp -1))))
1360 (defun semantic-grammar-anchored-indentation ()
1361 "Return indentation based on previous anchor character found."
1365 (semantic-grammar-backward-item)
1369 ((looking-at ":\\(\\s-\\|$\\)")
1370 (setq indent (current-column))
1372 (skip-syntax-forward "-")
1373 (if (eolp) (setq indent 2))
1375 ((and (looking-at "[;%]")
1376 (not (looking-at "\\<%prec\\>")))
1381 (defun semantic-grammar-do-grammar-indent ()
1382 "Indent a line of grammar.
1383 When called the point is not in Lisp code."
1387 (skip-syntax-forward "-")
1388 (setq indent (current-column))
1391 (looking-at "\\(\\w\\|\\s_\\)+\\s-*:")
1392 (and (looking-at "%")
1393 (not (looking-at "%prec\\>"))))
1397 ((and (looking-at ";;")
1398 (save-excursion (forward-comment (point-max))
1402 (setq n (semantic-grammar-anchored-indentation))
1407 ((looking-at "[|;]")
1410 (setq n (+ n 2)))))))
1413 (delete-horizontal-space)
1416 (defvar semantic-grammar-brackets-as-parens-syntax-table
1417 (let ((st (copy-syntax-table emacs-lisp-mode-syntax-table)))
1418 (modify-syntax-entry ?\{ "(} " st)
1419 (modify-syntax-entry ?\} "){ " st)
1421 "Syntax table that consider brackets as parenthesis.
1422 So `lisp-indent-line' will work inside bracket blocks.")
1424 (defun semantic-grammar-do-lisp-indent ()
1425 "Maybe run the Emacs Lisp indenter on a line of code.
1426 Return nil if not in a Lisp expression."
1430 (skip-chars-forward "\t ")
1431 (let ((first (point)))
1432 (or (>= first (semantic-grammar-epilogue-start))
1440 (narrow-to-region (point) first)
1441 (goto-char (point-max))
1443 ;; Temporarily consider brackets as parenthesis so
1444 ;; `lisp-indent-line' can indent Lisp code inside
1446 semantic-grammar-brackets-as-parens-syntax-table
1447 (lisp-indent-line))))
1451 (defun semantic-grammar-indent ()
1452 "Indent the current line.
1453 Use the Lisp or grammar indenter depending on point location."
1455 (let ((orig (point))
1457 (or (semantic-grammar-do-lisp-indent)
1458 (semantic-grammar-do-grammar-indent))
1459 (setq first (save-excursion
1461 (skip-chars-forward "\t ")
1463 (if (or (< orig first) (/= orig (point)))
1464 (goto-char first))))
1466 (defun semantic-grammar-electric-punctuation ()
1467 "Insert and reindent for the symbol just typed in."
1469 (self-insert-command 1)
1471 (semantic-grammar-indent)))
1473 (defun semantic-grammar-complete ()
1474 "Attempt to complete the symbol under point.
1475 Completion is position sensitive. If the cursor is in a match section of
1476 a rule, then nonterminals symbols are scanned. If the cursor is in a Lisp
1477 expression then Lisp symbols are completed."
1479 (if (semantic-grammar-in-lisp-p)
1480 ;; We are in lisp code. Do lisp completion.
1481 (lisp-complete-symbol)
1482 ;; We are not in lisp code. Do rule completion.
1483 (let* ((nonterms (semantic-find-tags-by-class 'nonterminal (current-buffer)))
1484 (sym (car (semantic-ctxt-current-symbol)))
1485 (ans (try-completion sym nonterms)))
1488 (message "Symbols is already complete"))
1489 ((and (stringp ans) (string= ans sym))
1490 ;; Max matchable. Show completions.
1491 (with-output-to-temp-buffer "*Completions*"
1492 (display-completion-list (all-completions sym nonterms)))
1495 ;; Expand the completions
1497 (delete-region (point) (progn (forward-sexp 1) (point)))
1499 (t (message "No Completions."))
1503 (defun semantic-grammar-insert-keyword (name)
1504 "Insert a new %keyword declaration with NAME.
1505 Assumes it is typed in with the correct casing."
1506 (interactive "sKeyword: ")
1507 (if (not (bolp)) (insert "\n"))
1508 (insert "%keyword " (upcase name) " \"" name "\"
1509 %put " (upcase name) " summary
1513 ;;; Macro facilities
1516 (defsubst semantic--grammar-macro-function-tag (name)
1517 "Search for a function tag for the grammar macro with name NAME.
1518 Return the tag found or nil if not found."
1519 (car (semantic-find-tags-by-class
1521 (or (semantic-find-tags-by-name name (current-buffer))
1522 (and (featurep 'semantic/db)
1523 semanticdb-current-database
1524 (cdar (semanticdb-find-tags-by-name name nil t)))))))
1526 (defsubst semantic--grammar-macro-lib-part (def)
1527 "Return the library part of the grammar macro defined by DEF."
1528 (let ((suf (format "-%s\\'" (regexp-quote (symbol-name (car def)))))
1529 (fun (symbol-name (cdr def))))
1530 (substring fun 0 (string-match suf fun))))
1532 (defun semantic--grammar-macro-compl-elt (def &optional full)
1533 "Return a completion entry for the grammar macro defined by DEF.
1534 If optional argument FULL is non-nil qualify the macro name with the
1535 library found in DEF."
1536 (let ((mac (car def))
1537 (lib (semantic--grammar-macro-lib-part def)))
1539 (format "%s/%s" mac lib)
1543 (defun semantic--grammar-macro-compl-dict ()
1544 "Return a completion dictionary of macro definitions."
1545 (let ((defs (semantic-grammar-macros))
1548 (setq def (car defs)
1550 (if (or (assoc (car def) defs) (assoc (car def) dups))
1552 (push (semantic--grammar-macro-compl-elt def) dict)))
1554 (setq def (car dups)
1556 (push (semantic--grammar-macro-compl-elt def t) dict))
1559 (defun semantic-grammar-find-macro-expander (macro-name library)
1560 "Visit the Emacs Lisp library where a grammar macro is implemented.
1561 MACRO-NAME is a symbol that identifies a grammar macro.
1562 LIBRARY is the name (sans extension) of the Emacs Lisp library where
1563 to start searching the macro implementation. Lookup in included
1564 libraries, if necessary.
1565 Find a function tag (in current tags table) whose name contains MACRO-NAME.
1566 Select the buffer containing the tag's definition, and move point there."
1568 (let* ((dic (semantic--grammar-macro-compl-dict))
1569 (def (assoc (completing-read "Macro: " dic nil 1) dic)))
1570 (or (cdr def) '(nil nil))))
1571 (when (and macro-name library)
1572 (let* ((lib (format "%s.el" library))
1573 (buf (find-file-noselect (or (locate-library lib t) lib)))
1574 (tag (with-current-buffer buf
1575 (semantic--grammar-macro-function-tag
1576 (format "%s-%s" library macro-name)))))
1579 (require 'semantic/decorate)
1580 (pop-to-buffer (semantic-tag-buffer tag))
1581 (goto-char (semantic-tag-start tag))
1582 (semantic-momentary-highlight-tag tag))
1584 (message "No expander found in library %s for macro %s"
1585 library macro-name)))))
1590 (defvar semantic-grammar-syntax-help
1593 ("symbol" . "Syntax: A symbol of alpha numeric and symbol characters")
1594 ("number" . "Syntax: Numeric characters.")
1595 ("punctuation" . "Syntax: Punctuation character.")
1596 ("semantic-list" . "Syntax: A list delimited by any valid list characters")
1597 ("open-paren" . "Syntax: Open Parenthesis character")
1598 ("close-paren" . "Syntax: Close Parenthesis character")
1599 ("string" . "Syntax: String character delimited text")
1600 ("comment" . "Syntax: Comment character delimited text")
1602 ("EMPTY" . "Syntax: Match empty text")
1603 ("ASSOC" . "Lambda Key: (ASSOC key1 value1 key2 value2 ...)")
1604 ("EXPAND" . "Lambda Key: (EXPAND <list id> <rule>)")
1605 ("EXPANDFULL" . "Lambda Key: (EXPANDFULL <list id> <rule>)")
1606 ;; Tag Generator Macros
1607 ("TAG" . "Generic Tag Generation: (TAG <name> <tag-class> [ :key value ]*)")
1608 ("VARIABLE-TAG" . "(VARIABLE-TAG <name> <lang-type> <default-value> [ :key value ]*)")
1609 ("FUNCTION-TAG" . "(FUNCTION-TAG <name> <lang-type> <arg-list> [ :key value ]*)")
1610 ("TYPE-TAG" . "(TYPE-TAG <name> <lang-type> <part-list> <parents> [ :key value ]*)")
1611 ("INCLUDE-TAG" . "(INCLUDE-TAG <name> <system-flag> [ :key value ]*)")
1612 ("PACKAGE-TAG" . "(PACKAGE-TAG <name> <detail> [ :key value ]*)")
1613 ("CODE-TAG" . "(CODE-TAG <name> <detail> [ :key value ]*)")
1614 ("ALIAS-TAG" . "(ALIAS-TAG <name> <aliasclass> <definition> [:key value]*)")
1615 ;; Special value macros
1616 ("$1" . "Match Value: Value from match list in slot 1")
1617 ("$2" . "Match Value: Value from match list in slot 2")
1618 ("$3" . "Match Value: Value from match list in slot 3")
1619 ("$4" . "Match Value: Value from match list in slot 4")
1620 ("$5" . "Match Value: Value from match list in slot 5")
1621 ("$6" . "Match Value: Value from match list in slot 6")
1622 ("$7" . "Match Value: Value from match list in slot 7")
1623 ("$8" . "Match Value: Value from match list in slot 8")
1624 ("$9" . "Match Value: Value from match list in slot 9")
1625 ;; Same, but with annoying , in front.
1626 (",$1" . "Match Value: Value from match list in slot 1")
1627 (",$2" . "Match Value: Value from match list in slot 2")
1628 (",$3" . "Match Value: Value from match list in slot 3")
1629 (",$4" . "Match Value: Value from match list in slot 4")
1630 (",$5" . "Match Value: Value from match list in slot 5")
1631 (",$6" . "Match Value: Value from match list in slot 6")
1632 (",$7" . "Match Value: Value from match list in slot 7")
1633 (",$8" . "Match Value: Value from match list in slot 8")
1634 (",$9" . "Match Value: Value from match list in slot 9")
1636 "Association of syntax elements, and the corresponding help.")
1638 (defun semantic-grammar-eldoc-get-macro-docstring (macro expander)
1639 "Return a one-line docstring for the given grammar MACRO.
1640 EXPANDER is the name of the function that expands MACRO."
1642 (if (and (eq expander (aref eldoc-last-data 0))
1643 (eq 'function (aref eldoc-last-data 2)))
1644 (aref eldoc-last-data 1)
1645 (let ((doc (help-split-fundoc (documentation expander t) expander)))
1648 (setq doc (car doc))
1649 (string-match "\\`[^ )]* ?" doc)
1650 (setq doc (concat "(" (substring doc (match-end 0)))))
1652 (setq doc (eldoc-function-argstring expander))))
1655 (eldoc-docstring-format-sym-doc
1656 macro (format "==> %s %s" expander doc) 'default))
1657 (eldoc-last-data-store expander doc 'function))
1660 (define-mode-local-override semantic-idle-summary-current-symbol-info
1661 semantic-grammar-mode ()
1662 "Display additional eldoc information about grammar syntax elements.
1663 Syntax element is the current symbol at point.
1664 If it is associated a help string in `semantic-grammar-syntax-help',
1666 If it is a macro name, return a description of the associated expander
1667 function parameter list.
1668 If it is a function name, return a description of this function
1670 It it is a variable name, return a brief (one-line) documentation
1671 string for the variable.
1672 If a default description of the current context can be obtained,
1674 Otherwise return nil."
1676 (let* ((elt (car (semantic-ctxt-current-symbol)))
1677 (val (and elt (cdr (assoc elt semantic-grammar-syntax-help)))))
1678 (when (and (not val) elt (semantic-grammar-in-lisp-p))
1679 ;; Ensure to load macro definitions before doing `intern-soft'.
1680 (setq val (semantic-grammar-macros)
1681 elt (intern-soft elt)
1682 val (and elt (cdr (assq elt val))))
1685 ((and val (fboundp val))
1686 (setq val (semantic-grammar-eldoc-get-macro-docstring elt val)))
1688 ((and elt (fboundp elt))
1689 (setq val (eldoc-get-fnsym-args-string elt)))
1691 ((and elt (boundp elt))
1692 (setq val (eldoc-get-var-docstring elt)))
1694 (or val (semantic-idle-summary-current-symbol-info-default))))
1696 (define-mode-local-override semantic-tag-boundary-p
1697 semantic-grammar-mode (tag)
1698 "Return non-nil for tags that should have a boundary drawn.
1699 Only tags of type 'nonterminal will be so marked."
1700 (let ((c (semantic-tag-class tag)))
1701 (eq c 'nonterminal)))
1703 (define-mode-local-override semantic-ctxt-current-function
1704 semantic-grammar-mode (&optional point)
1705 "Determine the name of the current function at POINT."
1707 (and point (goto-char point))
1708 (when (semantic-grammar-in-lisp-p)
1709 (with-mode-local emacs-lisp-mode
1710 (semantic-ctxt-current-function)))))
1712 (define-mode-local-override semantic-ctxt-current-argument
1713 semantic-grammar-mode (&optional point)
1714 "Determine the argument index of the called function at POINT."
1716 (and point (goto-char point))
1717 (when (semantic-grammar-in-lisp-p)
1718 (with-mode-local emacs-lisp-mode
1719 (semantic-ctxt-current-argument)))))
1721 (define-mode-local-override semantic-ctxt-current-assignment
1722 semantic-grammar-mode (&optional point)
1723 "Determine the tag being assigned into at POINT."
1725 (and point (goto-char point))
1726 (when (semantic-grammar-in-lisp-p)
1727 (with-mode-local emacs-lisp-mode
1728 (semantic-ctxt-current-assignment)))))
1730 (define-mode-local-override semantic-ctxt-current-class-list
1731 semantic-grammar-mode (&optional point)
1732 "Determine the class of tags that can be used at POINT."
1734 (and point (goto-char point))
1735 (if (semantic-grammar-in-lisp-p)
1736 (with-mode-local emacs-lisp-mode
1737 (semantic-ctxt-current-class-list))
1738 '(nonterminal keyword))))
1740 (define-mode-local-override semantic-ctxt-current-mode
1741 semantic-grammar-mode (&optional point)
1742 "Return the major mode active at POINT.
1743 POINT defaults to the value of point in current buffer.
1744 Return `emacs-lisp-mode' is POINT is within Lisp code, otherwise
1745 return the current major mode."
1747 (and point (goto-char point))
1748 (if (semantic-grammar-in-lisp-p)
1750 (semantic-ctxt-current-mode-default))))
1752 (define-mode-local-override semantic-format-tag-abbreviate
1753 semantic-grammar-mode (tag &optional parent color)
1754 "Return a string abbreviation of TAG.
1755 Optional PARENT is not used.
1756 Optional COLOR is used to flag if color is added to the text."
1757 (let ((class (semantic-tag-class tag))
1758 (name (semantic-format-tag-name tag parent color)))
1760 ((eq class 'nonterminal)
1762 ((eq class 'setting)
1764 ((memq class '(rule keyword))
1767 (concat "%" (symbol-name class) " " name)))))
1769 (define-mode-local-override semantic-format-tag-summarize
1770 semantic-grammar-mode (tag &optional parent color)
1771 "Return a string summarizing TAG.
1772 Optional PARENT is not used.
1773 Optional argument COLOR determines if color is added to the text."
1774 (let ((class (semantic-tag-class tag))
1775 (name (semantic-format-tag-name tag parent color))
1779 ((eq class 'nonterminal)
1780 (setq label "Nonterminal: "
1782 " with %d match lists."
1783 (length (semantic-tag-components tag)))))
1784 ((eq class 'keyword)
1785 (setq label "Keyword: ")
1787 (semantic--find-tags-by-function
1790 (setq summary (cdr (assoc "summary"
1791 (semantic-tag-get-attribute
1793 ;; Get `put' tag with TAG name.
1794 (semantic-find-tags-by-name-regexp
1795 (regexp-quote (semantic-tag-name tag))
1796 (semantic-find-tags-by-class 'put (current-buffer))))
1797 (setq desc (concat " = "
1798 (semantic-tag-get-attribute tag :value)
1800 (concat " - " (read summary))
1803 (setq label "Token: ")
1804 (let ((val (semantic-tag-get-attribute tag :value))
1805 (names (semantic-tag-get-attribute tag :rest))
1806 (type (semantic-tag-type tag)))
1808 (setq name (mapconcat 'identity (cons name names) " ")))
1811 (format " <%s>" type)
1814 (format "%s%S" val (if type " " ""))
1817 (setq label "Assoc: ")
1818 (let ((val (semantic-tag-get-attribute tag :value))
1819 (type (semantic-tag-type tag)))
1822 (format " <%s>" type)
1825 (concat " " (mapconcat 'identity val " "))
1828 (setq desc (semantic-format-tag-abbreviate tag parent color))))
1829 (if (and color label)
1830 (setq label (semantic--format-colorize-text label 'label)))
1831 (if (and color label desc)
1832 (setq desc (semantic--format-colorize-text desc 'comment)))
1834 (concat label name desc)
1835 ;; Just a description is the abbreviated version
1838 ;;; Semantic Analysis
1840 (define-mode-local-override semantic-analyze-current-context
1841 semantic-grammar-mode (point)
1842 "Provide a semantic analysis object describing a context in a grammar."
1843 (require 'semantic/analyze)
1844 (if (semantic-grammar-in-lisp-p)
1845 (with-mode-local emacs-lisp-mode
1846 (semantic-analyze-current-context point))
1848 (let* ((context-return nil)
1849 (prefixandbounds (semantic-ctxt-current-symbol-and-bounds))
1850 (prefix (car prefixandbounds))
1851 (bounds (nth 2 prefixandbounds))
1853 (prefixclass (semantic-ctxt-current-class-list))
1856 ;; Do context for rules when in a match list.
1858 (semantic-find-first-tag-by-name
1862 (setq context-return
1863 (semantic-analyze-context
1864 "context-for-semantic-grammar"
1865 :buffer (current-buffer)
1868 :prefix (if prefixsym
1872 :prefixclass prefixclass
1877 (define-mode-local-override semantic-analyze-possible-completions
1878 semantic-grammar-mode (context)
1879 "Return a list of possible completions based on CONTEXT."
1880 (require 'semantic/analyze/complete)
1881 (if (semantic-grammar-in-lisp-p)
1882 (with-mode-local emacs-lisp-mode
1883 (semantic-analyze-possible-completions context))
1884 (with-current-buffer (oref context buffer)
1885 (let* ((prefix (car (oref context :prefix)))
1886 (completetext (cond ((semantic-tag-p prefix)
1887 (semantic-tag-name prefix))
1890 ((stringp (car prefix))
1892 (tags (semantic-find-tags-for-completion completetext
1894 (semantic-analyze-tags-of-class-list
1895 tags (oref context prefixclass)))
1898 (provide 'semantic/grammar)
1900 ;; arch-tag: 12ffc9d5-557d-49af-a5fd-a66a006ddb3e
1901 ;;; semantic/grammar.el ends here