1 ;;; semantic/grammar.el --- Major mode framework for Semantic grammars
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
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"
252 (skip-chars-backward "\r\n\t")
257 (defsubst semantic-grammar-buffer-file (&optional buffer)
258 "Return name of file sans directory BUFFER is visiting.
259 No argument or nil as argument means use the current buffer."
260 (file-name-nondirectory (buffer-file-name buffer)))
262 (defun semantic-grammar-package ()
263 "Return the %package value as a string.
264 If there is no %package statement in the grammar, return a default
265 package name derived from the grammar file name. For example, the
266 default package name for the grammar file foo.wy is foo-wy, and for
267 foo.by it is foo-by."
268 (or (semantic-grammar-first-tag-name 'package)
269 (let* ((file (semantic-grammar-buffer-file))
270 (ext (file-name-extension file))
271 (i (string-match (format "\\([.]\\)%s\\'" ext) file)))
272 (concat (substring file 0 i) "-" ext))))
274 (defsubst semantic-grammar-languagemode ()
275 "Return the %languagemode value as a list of symbols or nil."
276 (semantic-grammar-tag-symbols 'languagemode))
278 (defsubst semantic-grammar-start ()
279 "Return the %start value as a list of symbols or nil."
280 (semantic-grammar-tag-symbols 'start))
282 (defsubst semantic-grammar-scopestart ()
283 "Return the %scopestart value as a symbol or nil."
284 (intern (or (semantic-grammar-first-tag-name 'scopestart) "nil")))
286 (defsubst semantic-grammar-quotemode ()
287 "Return the %quotemode value as a symbol or nil."
288 (intern (or (semantic-grammar-first-tag-name 'quotemode) "nil")))
290 (defsubst semantic-grammar-keywords ()
291 "Return the language keywords.
292 That is an alist of (VALUE . TOKEN) where VALUE is the string value of
293 the keyword and TOKEN is the terminal symbol identifying the keyword."
296 (cons (semantic-tag-get-attribute key :value)
297 (intern (semantic-tag-name key))))
298 (semantic-find-tags-by-class 'keyword (current-buffer))))
300 (defun semantic-grammar-keyword-properties (keywords)
301 "Return the list of KEYWORDS properties."
302 (let ((puts (semantic-find-tags-by-class
303 'put (current-buffer)))
304 put keys key plist assoc pkey pval props)
310 (cons (semantic-tag-name put)
311 (semantic-tag-get-attribute put :rest))))
315 assoc (rassq key keywords))
317 nil ;;(message "*** %%put to undefined keyword %s ignored" key)
318 (setq key (car assoc)
319 plist (semantic-tag-get-attribute put :value))
321 (setq pkey (intern (caar plist))
322 pval (read (cdar plist))
323 props (cons (list key pkey pval) props)
324 plist (cdr plist))))))
327 (defun semantic-grammar-tokens ()
328 "Return defined lexical tokens.
329 That is an alist (TYPE . DEFS) where type is a %token <type> symbol
330 and DEFS is an alist of (TOKEN . VALUE). TOKEN is the terminal symbol
331 identifying the token and VALUE is the string value of the token or
333 (let (tags alist assoc tag type term names value)
335 ;; Check for <type> in %left, %right & %nonassoc declarations
336 (setq tags (semantic-find-tags-by-class
337 'assoc (current-buffer)))
341 (when (setq type (semantic-tag-type tag))
342 (setq names (semantic-tag-get-attribute tag :value)
343 assoc (assoc type alist))
344 (or assoc (setq assoc (list type)
345 alist (cons assoc alist)))
347 (setq term (car names)
349 (or (string-match semantic-grammar-lex-c-char-re term)
350 (setcdr assoc (cons (list (intern term))
353 ;; Then process %token declarations so they can override any
354 ;; previous specifications
355 (setq tags (semantic-find-tags-by-class
356 'token (current-buffer)))
360 (setq names (cons (semantic-tag-name tag)
361 (semantic-tag-get-attribute tag :rest))
362 type (or (semantic-tag-type tag) "<no-type>")
363 value (semantic-tag-get-attribute tag :value)
364 assoc (assoc type alist))
365 (or assoc (setq assoc (list type)
366 alist (cons assoc alist)))
368 (setq term (intern (car names))
370 (setcdr assoc (cons (cons term value) (cdr assoc)))))
373 (defun semantic-grammar-token-%type-properties (&optional props)
374 "Return properties set by %type statements.
375 This declare a new type if necessary.
376 If optional argument PROPS is non-nil, it is an existing list of
377 properties where to add new properties."
379 (dolist (tag (semantic-find-tags-by-class 'type (current-buffer)))
380 (setq type (semantic-tag-name tag))
381 ;; Indicate to auto-generate the analyzer for this type
382 (push (list type :declared t) props)
383 (dolist (e (semantic-tag-get-attribute tag :value))
384 (push (list type (intern (car e)) (read (or (cdr e) "nil")))
388 (defun semantic-grammar-token-%put-properties (tokens)
389 "For types found in TOKENS, return properties set by %put statements."
391 (dolist (put (semantic-find-tags-by-class 'put (current-buffer)))
392 (dolist (type (cons (semantic-tag-name put)
393 (semantic-tag-get-attribute put :rest)))
394 (setq found (assoc type tokens))
396 nil ;; %put <type> ignored, no token defined
397 (setq type (car found))
398 (dolist (e (semantic-tag-get-attribute put :value))
399 (push (list type (intern (car e)) (read (or (cdr e) "nil")))
403 (defsubst semantic-grammar-token-properties (tokens)
404 "Return properties of declared types.
405 Types are explicitly declared by %type statements. Types found in
406 TOKENS are those declared implicitly by %token statements.
407 Properties can be set by %put and %type statements.
408 Properties set by %type statements take precedence over those set by
410 (let ((props (semantic-grammar-token-%put-properties tokens)))
411 (semantic-grammar-token-%type-properties props)))
413 (defun semantic-grammar-use-macros ()
414 "Return macro definitions from %use-macros statements.
415 Also load the specified macro libraries."
417 (dolist (tag (semantic-find-tags-by-class 'macro (current-buffer)))
418 (setq lib (intern (semantic-tag-type tag)))
420 ;;(load lib) ;; Be sure to use the latest macro library.
423 (dolist (mac (semantic-tag-get-attribute tag :value))
424 (push (cons (intern mac)
425 (intern (format "%s-%s" lib mac)))
429 (defvar semantic-grammar-macros nil
430 "List of associations (MACRO-NAME . EXPANDER).")
431 (make-variable-buffer-local 'semantic-grammar-macros)
433 (defun semantic-grammar-macros ()
434 "Build and return the alist of defined macros."
436 ;; Definitions found in tags.
437 (semantic-grammar-use-macros)
438 ;; Other pre-installed definitions.
439 semantic-grammar-macros))
442 ;;;; Overloaded functions that build parser data.
445 ;;; Keyword table builder
447 (defun semantic-grammar-keywordtable-builder-default ()
448 "Return the default value of the keyword table."
449 (let ((keywords (semantic-grammar-keywords)))
450 `(semantic-lex-make-keyword-table
452 ',(semantic-grammar-keyword-properties keywords))))
454 (define-overloadable-function semantic-grammar-keywordtable-builder ()
455 "Return the keyword table table value.")
457 ;;; Token table builder
459 (defun semantic-grammar-tokentable-builder-default ()
460 "Return the default value of the table of lexical tokens."
461 (let ((tokens (semantic-grammar-tokens)))
462 `(semantic-lex-make-type-table
464 ',(semantic-grammar-token-properties tokens))))
466 (define-overloadable-function semantic-grammar-tokentable-builder ()
467 "Return the value of the table of lexical tokens.")
469 ;;; Parser table builder
471 (defun semantic-grammar-parsetable-builder-default ()
472 "Return the default value of the parse table."
473 (error "`semantic-grammar-parsetable-builder' not defined"))
475 (define-overloadable-function semantic-grammar-parsetable-builder ()
476 "Return the parser table value.")
478 ;;; Parser setup code builder
480 (defun semantic-grammar-setupcode-builder-default ()
481 "Return the default value of the setup code form."
482 (error "`semantic-grammar-setupcode-builder' not defined"))
484 (define-overloadable-function semantic-grammar-setupcode-builder ()
485 "Return the parser setup code form.")
488 ;;;; Lisp code generation
490 (defvar semantic--grammar-input-buffer nil)
491 (defvar semantic--grammar-output-buffer nil)
493 (defsubst semantic-grammar-keywordtable ()
494 "Return the variable name of the keyword table."
495 (concat (file-name-sans-extension
496 (semantic-grammar-buffer-file
497 semantic--grammar-output-buffer))
500 (defsubst semantic-grammar-tokentable ()
501 "Return the variable name of the token table."
502 (concat (file-name-sans-extension
503 (semantic-grammar-buffer-file
504 semantic--grammar-output-buffer))
507 (defsubst semantic-grammar-parsetable ()
508 "Return the variable name of the parse table."
509 (concat (file-name-sans-extension
510 (semantic-grammar-buffer-file
511 semantic--grammar-output-buffer))
514 (defsubst semantic-grammar-setupfunction ()
515 "Return the name of the parser setup function."
516 (concat (file-name-sans-extension
517 (semantic-grammar-buffer-file
518 semantic--grammar-output-buffer))
521 (defmacro semantic-grammar-as-string (object)
522 "Return OBJECT as a string value."
523 `(if (stringp ,object)
526 (pp-to-string ,object)))
528 (defun semantic-grammar-insert-defconst (name value docstring)
529 "Insert declaration of constant NAME with VALUE and DOCSTRING."
530 (let ((start (point)))
531 (insert (format "(defconst %s\n%s%S)\n\n" name value docstring))
536 (defun semantic-grammar-insert-defun (name body docstring)
537 "Insert declaration of function NAME with BODY and DOCSTRING."
538 (let ((start (point)))
539 (insert (format "(defun %s ()\n%S\n%s)\n\n" name docstring body))
544 (defun semantic-grammar-insert-define (define)
545 "Insert the declaration specified by DEFINE expression.
546 Typically a DEFINE expression should look like this:
548 \(define-thing name docstring expression1 ...)"
550 (let ((start (point)))
551 (insert (format "(%S %S" (car define) (nth 1 define)))
552 (dolist (item (nthcdr 2 define))
555 (pp item (current-buffer)))
561 (defconst semantic-grammar-header-template
563 ;;; " file " --- Generated parser support file
567 ;; Author: " user-full-name " <" user-mail-address ">
572 ;; This file is not part of GNU Emacs.
574 ;; This program is free software; you can redistribute it and/or
575 ;; modify it under the terms of the GNU General Public License as
576 ;; published by the Free Software Foundation, either version 3 of
577 ;; the License, or (at your option) any later version.
579 ;; This software is distributed in the hope that it will be useful,
580 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
581 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
582 ;; General Public License for more details.
584 ;; You should have received a copy of the GNU General Public License
585 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
589 ;; PLEASE DO NOT MANUALLY EDIT THIS FILE! It is automatically
590 ;; generated from the grammar file " gram ".
597 "Generated header template.
598 The symbols in the template are local variables in
599 `semantic-grammar-header'")
601 (defconst semantic-grammar-footer-template
606 ;;; " file " ends here
608 "Generated footer template.
609 The symbols in the list are local variables in
610 `semantic-grammar-footer'.")
612 (defun semantic-grammar-copyright-line ()
613 "Return the grammar copyright line, or nil if not found."
615 (goto-char (point-min))
616 (when (re-search-forward "^;;+[ \t]+Copyright (C) .*$"
617 ;; Search only in the four top lines
618 (save-excursion (forward-line 4) (point))
622 (defun semantic-grammar-header ()
623 "Return text of a generated standard header."
624 (let ((file (semantic-grammar-buffer-file
625 semantic--grammar-output-buffer))
626 (gram (semantic-grammar-buffer-file))
627 (date (format-time-string "%Y-%m-%d %T%z"))
628 (vcid (concat "$" "Id" "$")) ;; Avoid expansion
629 ;; Try to get the copyright from the input grammar, or
630 ;; generate a new one if not found.
631 (copy (or (semantic-grammar-copyright-line)
632 (concat (format-time-string ";; Copyright (C) %Y ")
635 (dolist (S semantic-grammar-header-template)
637 (setq out (concat out S)))
639 (setq out (concat out (symbol-value S))))))
642 (defun semantic-grammar-footer ()
643 "Return text of a generated standard footer."
644 (let* ((file (semantic-grammar-buffer-file
645 semantic--grammar-output-buffer))
646 (libr (file-name-sans-extension file))
648 (dolist (S semantic-grammar-footer-template)
650 (setq out (concat out S)))
652 (setq out (concat out (symbol-value S))))))
655 (defun semantic-grammar-token-data ()
656 "Return the string value of the table of lexical tokens."
657 (semantic-grammar-as-string
658 (semantic-grammar-tokentable-builder)))
660 (defun semantic-grammar-keyword-data ()
661 "Return the string value of the table of keywords."
662 (semantic-grammar-as-string
663 (semantic-grammar-keywordtable-builder)))
665 (defun semantic-grammar-parser-data ()
666 "Return the parser table as a string value."
667 (semantic-grammar-as-string
668 (semantic-grammar-parsetable-builder)))
670 (defun semantic-grammar-setup-data ()
671 "Return the parser setup code form as a string value."
672 (semantic-grammar-as-string
673 (semantic-grammar-setupcode-builder)))
675 ;;; Generation of lexical analyzers.
677 (defvar semantic-grammar--lex-block-specs)
679 (defsubst semantic-grammar--lex-delim-spec (block-spec)
680 "Return delimiters specification from BLOCK-SPEC."
682 (let* ((standard-input (cdr block-spec))
684 (if (and (consp delim-spec)
685 (car delim-spec) (symbolp (car delim-spec))
686 (cadr delim-spec) (symbolp (cadr delim-spec)))
688 (error "Invalid delimiter")))
690 (error "Invalid delimiters specification %s in block token %s"
691 (cdr block-spec) (car block-spec)))))
693 (defun semantic-grammar--lex-block-specs ()
694 "Compute lexical block specifications for the current buffer.
695 Block definitions are read from the current table of lexical types."
697 ;; Block specifications have been parsed and are invalid.
698 ((eq semantic-grammar--lex-block-specs 'error)
701 ;; Parse block specifications.
702 ((null semantic-grammar--lex-block-specs)
704 (let* ((blocks (cdr (semantic-lex-type-value "block" t)))
705 (open-delims (cdr (semantic-lex-type-value "open-paren" t)))
706 (close-delims (cdr (semantic-lex-type-value "close-paren" t)))
707 olist clist block-spec delim-spec open-spec close-spec)
708 (dolist (block-spec blocks)
709 (setq delim-spec (semantic-grammar--lex-delim-spec block-spec)
710 open-spec (assq (car delim-spec) open-delims)
711 close-spec (assq (cadr delim-spec) close-delims))
713 (error "Missing open-paren token %s required by block %s"
714 (car delim-spec) (car block-spec)))
716 (error "Missing close-paren token %s required by block %s"
717 (cdr delim-spec) (car block-spec)))
718 ;; build alist ((OPEN-DELIM OPEN-SYM BLOCK-SYM) ...)
719 (push (list (cdr open-spec) (car open-spec) (car block-spec))
721 ;; build alist ((CLOSE-DELIM CLOSE-SYM) ...)
722 (push (list (cdr close-spec) (car close-spec))
724 (setq semantic-grammar--lex-block-specs (cons olist clist)))
726 (setq semantic-grammar--lex-block-specs 'error)
727 (message "%s" (error-message-string err))
730 ;; Block specifications already parsed.
732 semantic-grammar--lex-block-specs)))
734 (defsubst semantic-grammar-quoted-form (exp)
735 "Return a quoted form of EXP if it isn't a self evaluating form."
736 (if (and (not (null exp))
737 (or (listp exp) (symbolp exp)))
741 (defun semantic-grammar-insert-defanalyzer (type)
742 "Insert declaration of the lexical analyzer defined with TYPE."
743 (let* ((type-name (symbol-name type))
744 (type-value (symbol-value type))
745 (syntax (get type 'syntax))
746 (declared (get type :declared))
747 spec mtype prefix name doc)
748 ;; Generate an analyzer if the corresponding type has been
749 ;; explicitly declared in a %type statement, and if at least the
750 ;; syntax property has been provided.
751 (when (and declared syntax)
752 (setq prefix (file-name-sans-extension
753 (semantic-grammar-buffer-file
754 semantic--grammar-output-buffer))
755 mtype (or (get type 'matchdatatype) 'regexp)
756 name (intern (format "%s--<%s>-%s-analyzer" prefix type mtype))
757 doc (format "%s analyzer for <%s> tokens." mtype type))
759 ;; Regexp match analyzer
761 (semantic-grammar-insert-define
762 `(define-lex-regex-type-analyzer ,name
764 ,(semantic-grammar-quoted-form (cdr type-value))
765 ',(or (car type-value) (intern type-name))))
767 ;; String compare analyzer
769 (semantic-grammar-insert-define
770 `(define-lex-string-type-analyzer ,name
772 ,(semantic-grammar-quoted-form (cdr type-value))
773 ',(or (car type-value) (intern type-name))))
776 ((and (eq mtype 'block)
777 (setq spec (semantic-grammar--lex-block-specs)))
778 (semantic-grammar-insert-define
779 `(define-lex-block-type-analyzer ,name
781 ,(semantic-grammar-quoted-form spec)))
785 (semantic-grammar-insert-define
786 `(define-lex-sexp-type-analyzer ,name
788 ',(or (car type-value) (intern type-name))))
792 (semantic-grammar-insert-define
793 `(define-lex-keyword-type-analyzer ,name
799 (defun semantic-grammar-insert-defanalyzers ()
800 "Insert declarations of lexical analyzers."
802 (with-current-buffer semantic--grammar-input-buffer
803 (setq tokens (semantic-grammar-tokens)
804 props (semantic-grammar-token-properties tokens)))
805 (insert "(require 'semantic-lex)\n\n")
806 (let ((semantic-lex-types-obarray
807 (semantic-lex-make-type-table tokens props))
808 semantic-grammar--lex-block-specs)
809 (mapatoms 'semantic-grammar-insert-defanalyzer
810 semantic-lex-types-obarray))))
812 ;;; Generation of the grammar support file.
814 (defcustom semantic-grammar-file-regexp "\\.[wb]y$"
815 "Regexp which matches grammar source files."
819 (defsubst semantic-grammar-noninteractive ()
820 "Return non-nil if running without interactive terminal."
821 (if (featurep 'xemacs)
825 (defun semantic-grammar-create-package (&optional force)
826 "Create package Lisp code from grammar in current buffer.
827 Does nothing if the Lisp code seems up to date.
828 If optional argument FORCE is non-nil, unconditionally re-generate the
831 (setq force (or force current-prefix-arg))
832 (semantic-fetch-tags)
834 ;; Values of the following local variables are obtained from
835 ;; the grammar parsed tree in current buffer, that is before
836 ;; switching to the output file.
837 (package (semantic-grammar-package))
838 (output (concat package ".el"))
839 (semantic--grammar-input-buffer (current-buffer))
840 (semantic--grammar-output-buffer (find-file-noselect output))
841 (header (semantic-grammar-header))
842 (prologue (semantic-grammar-prologue))
843 (epilogue (semantic-grammar-epilogue))
844 (footer (semantic-grammar-footer))
847 (not (buffer-modified-p))
848 (file-newer-than-file-p
849 (buffer-file-name semantic--grammar-output-buffer)
850 (buffer-file-name semantic--grammar-input-buffer)))
851 (message "Package `%s' is up to date." package)
852 ;; Create the package
853 (set-buffer semantic--grammar-output-buffer)
854 ;; Use Unix EOLs, so that the file is portable to all platforms.
855 (setq buffer-file-coding-system 'raw-text-unix)
857 (unless (eq major-mode 'emacs-lisp-mode)
860 ;;;; Header + Prologue
863 "
\f\n;;; Prologue\n;;\n"
866 ;; Evaluate the prologue now, because it might provide definition
867 ;; of grammar macro expanders.
868 (eval-region (point-min) (point))
874 (insert "
\f\n;;; Declarations\n;;\n")
876 ;; `eval-defun' is not necessary to reset `defconst' values.
877 (semantic-grammar-insert-defconst
878 (semantic-grammar-keywordtable)
879 (with-current-buffer semantic--grammar-input-buffer
880 (semantic-grammar-keyword-data))
881 "Table of language keywords.")
883 (semantic-grammar-insert-defconst
884 (semantic-grammar-tokentable)
885 (with-current-buffer semantic--grammar-input-buffer
886 (semantic-grammar-token-data))
887 "Table of lexical tokens.")
889 (semantic-grammar-insert-defconst
890 (semantic-grammar-parsetable)
891 (with-current-buffer semantic--grammar-input-buffer
892 (semantic-grammar-parser-data))
895 (semantic-grammar-insert-defun
896 (semantic-grammar-setupfunction)
897 (with-current-buffer semantic--grammar-input-buffer
898 (semantic-grammar-setup-data))
899 "Setup the Semantic Parser.")
902 (insert "
\f\n;;; Analyzers\n;;\n")
904 (semantic-grammar-insert-defanalyzers)
906 ;;;; Epilogue & Footer
908 (insert "
\f\n;;; Epilogue\n;;\n"
917 ;; If running in batch mode, there is nothing more to do.
918 ;; Save the generated file and quit.
919 (if (semantic-grammar-noninteractive)
920 (let ((version-control t)
921 (delete-old-versions t)
922 (make-backup-files t)
923 (vc-make-backup-files t))
924 (kill-buffer (current-buffer)))
925 ;; If running interactively, eval declarations and epilogue
926 ;; code, then pop to the buffer visiting the generated file.
927 (eval-region (point) (point-max))
928 ;; Loop over the defvars and eval them explicitly to force
929 ;; them to be evaluated and ready to use.
930 (goto-char (point-min))
931 (while (re-search-forward "(defvar " nil t)
933 ;; Move cursor to a logical spot in the generated code.
934 (goto-char (point-min))
935 (pop-to-buffer (current-buffer))
936 ;; The generated code has been evaluated and updated into
937 ;; memory. Now find all buffers that match the major modes we
938 ;; have created this language for, and force them to call our
939 ;; setup function again, refreshing all semantic data, and
940 ;; enabling them to work with the new code just created.
942 ;; At this point, I don't know any user's defined setup code :-(
943 ;; At least, what I can do for now, is to run the generated
944 ;; parser-install function.
945 (semantic-map-mode-buffers
946 (semantic-grammar-setupfunction)
947 (semantic-grammar-languagemode)))
949 ;; Return the name of the generated package file.
952 (defun semantic-grammar-recreate-package ()
953 "Unconditionally create Lisp code from grammar in current buffer.
954 Like \\[universal-argument] \\[semantic-grammar-create-package]."
956 (semantic-grammar-create-package t))
958 (defun semantic-grammar-batch-build-one-package (file)
959 "Build a Lisp package from the grammar in FILE.
960 That is, generate Lisp code from FILE, and `byte-compile' it.
961 Return non-nil if there were no errors, nil if errors."
962 ;; We need this require so that we can find `byte-compile-dest-file'.
964 (unless (auto-save-file-name-p file)
965 ;; Create the package
968 (with-current-buffer (find-file-noselect file)
969 (semantic-grammar-create-package))
971 (message "%s" (error-message-string err))
974 ;; Only byte compile if out of date
975 (if (file-newer-than-file-p
976 packagename (byte-compile-dest-file packagename))
977 (let (;; Some complex grammar table expressions need a few
978 ;; more resources than the default.
979 (max-specpdl-size (max 3000 max-specpdl-size))
980 (max-lisp-eval-depth (max 1000 max-lisp-eval-depth))
982 ;; byte compile the resultant file
983 (byte-compile-file packagename))
986 (defun semantic-grammar-batch-build-packages ()
987 "Build Lisp packages from grammar files on the command line.
988 That is, run `semantic-grammar-batch-build-one-package' for each file.
989 Each file is processed even if an error occurred previously.
990 Must be used from the command line, with `-batch'.
991 For example, to process grammar files in current directory, invoke:
993 \"emacs -batch -f semantic-grammar-batch-build-packages .\".
995 See also the variable `semantic-grammar-file-regexp'."
996 (or (semantic-grammar-noninteractive)
998 `semantic-grammar-batch-build-packages' must be used with -batch"
1001 ;; Remove vc from find-file-hook. It causes bad stuff to
1002 ;; happen in Emacs 20.
1003 (find-file-hook (delete 'vc-find-file-hook find-file-hook)))
1004 (message "Compiling Grammars from: %s" (locate-library "semantic-grammar"))
1005 (dolist (arg command-line-args-left)
1006 (unless (and arg (file-exists-p arg))
1007 (error "Argument %s is not a valid file name" arg))
1008 (setq arg (expand-file-name arg))
1009 (if (file-directory-p arg)
1010 ;; Directory as argument
1011 (dolist (src (condition-case nil
1013 arg nil semantic-grammar-file-regexp)
1015 (error "Unable to read directory files"))))
1016 (or (semantic-grammar-batch-build-one-package
1017 (expand-file-name src arg))
1019 ;; Specific file argument
1020 (or (semantic-grammar-batch-build-one-package arg)
1026 ;;;; Macros highlighting
1029 (defvar semantic--grammar-macros-regexp-1 nil)
1030 (make-variable-buffer-local 'semantic--grammar-macros-regexp-1)
1032 (defun semantic--grammar-macros-regexp-1 ()
1033 "Return font-lock keyword regexp for pre-installed macro names."
1034 (and semantic-grammar-macros
1035 (not semantic--grammar-macros-regexp-1)
1037 (setq semantic--grammar-macros-regexp-1
1040 (mapcar #'(lambda (e) (symbol-name (car e)))
1041 semantic-grammar-macros)
1045 semantic--grammar-macros-regexp-1)
1047 (defconst semantic--grammar-macdecl-re
1048 "\\<%use-macros\\>[ \t\r\n]+\\(\\sw\\|\\s_\\)+[ \t\r\n]+{"
1049 "Regexp that matches a macro declaration statement.")
1051 (defvar semantic--grammar-macros-regexp-2 nil)
1052 (make-variable-buffer-local 'semantic--grammar-macros-regexp-2)
1054 (defun semantic--grammar-clear-macros-regexp-2 (&rest ignore)
1055 "Clear the cached regexp that match macros local in this grammar.
1057 Added to `before-change-functions' hooks to be run before each text
1059 (setq semantic--grammar-macros-regexp-2 nil))
1061 (defun semantic--grammar-macros-regexp-2 ()
1062 "Return the regexp that match macros local in this grammar."
1063 (unless semantic--grammar-macros-regexp-2
1066 (goto-char (point-min))
1067 (while (re-search-forward semantic--grammar-macdecl-re nil t)
1069 (setq macs (nconc macs
1071 (buffer-substring-no-properties
1080 (setq semantic--grammar-macros-regexp-2
1081 (concat "(\\s-*" (regexp-opt macs t) "\\>"))))))
1082 semantic--grammar-macros-regexp-2)
1084 (defun semantic--grammar-macros-matcher (end)
1085 "Search for a grammar macro name to highlight.
1086 END is the limit of the search."
1087 (let ((regexp (semantic--grammar-macros-regexp-1)))
1088 (or (and regexp (re-search-forward regexp end t))
1089 (and (setq regexp (semantic--grammar-macros-regexp-2))
1090 (re-search-forward regexp end t)))))
1093 ;;;; Define major mode
1096 (defvar semantic-grammar-syntax-table
1097 (let ((table (make-syntax-table (standard-syntax-table))))
1098 (modify-syntax-entry ?\: "." table) ;; COLON
1099 (modify-syntax-entry ?\> "." table) ;; GT
1100 (modify-syntax-entry ?\< "." table) ;; LT
1101 (modify-syntax-entry ?\| "." table) ;; OR
1102 (modify-syntax-entry ?\; ". 12" table) ;; SEMI, Comment start ;;
1103 (modify-syntax-entry ?\n ">" table) ;; Comment end
1104 (modify-syntax-entry ?\" "\"" table) ;; String
1105 (modify-syntax-entry ?\% "w" table) ;; Word
1106 (modify-syntax-entry ?\- "_" table) ;; Symbol
1107 (modify-syntax-entry ?\. "_" table) ;; Symbol
1108 (modify-syntax-entry ?\\ "\\" table) ;; Quote
1109 (modify-syntax-entry ?\` "'" table) ;; Prefix ` (backquote)
1110 (modify-syntax-entry ?\' "'" table) ;; Prefix ' (quote)
1111 (modify-syntax-entry ?\, "'" table) ;; Prefix , (comma)
1112 (modify-syntax-entry ?\# "'" table) ;; Prefix # (sharp)
1114 "Syntax table used in a Semantic grammar buffers.")
1116 (defvar semantic-grammar-mode-hook nil
1117 "Hook run when starting Semantic grammar mode.")
1119 (defvar semantic-grammar-mode-keywords-1
1120 `(("\\(\\<%%\\>\\|\\<%[{}]\\)"
1121 0 font-lock-reference-face)
1122 ("\\(%\\)\\(\\(\\sw\\|\\s_\\)+\\)"
1123 (1 font-lock-reference-face)
1124 (2 font-lock-keyword-face))
1126 0 (unless (semantic-grammar-in-lisp-p) 'bold))
1127 ("^\\(\\(\\sw\\|\\s_\\)+\\)[ \n\r\t]*:"
1128 1 font-lock-function-name-face)
1129 (semantic--grammar-macros-matcher
1130 1 ,(if (boundp 'font-lock-builtin-face)
1131 'font-lock-builtin-face
1132 'font-lock-preprocessor-face))
1133 ("\\$\\(\\sw\\|\\s_\\)*"
1134 0 font-lock-variable-name-face)
1135 ("<\\(\\(\\sw\\|\\s_\\)+\\)>"
1136 1 font-lock-type-face)
1137 (,semantic-grammar-lex-c-char-re
1138 0 ,(if (boundp 'font-lock-constant-face)
1139 'font-lock-constant-face
1140 'font-lock-string-face) t)
1141 ;; Must highlight :keyword here, because ':' is a punctuation in
1143 ("[\r\n\t ]+:\\sw+\\>"
1144 0 font-lock-builtin-face)
1145 ;; ;; Append the Semantic keywords
1146 ;; ,@semantic-fw-font-lock-keywords
1148 "Font Lock keywords used to highlight Semantic grammar buffers.")
1150 (defvar semantic-grammar-mode-keywords-2
1151 (append semantic-grammar-mode-keywords-1
1152 lisp-font-lock-keywords-1)
1153 "Font Lock keywords used to highlight Semantic grammar buffers.")
1155 (defvar semantic-grammar-mode-keywords-3
1156 (append semantic-grammar-mode-keywords-1
1157 lisp-font-lock-keywords-2)
1158 "Font Lock keywords used to highlight Semantic grammar buffers.")
1160 (defvar semantic-grammar-mode-keywords
1161 semantic-grammar-mode-keywords-1
1162 "Font Lock keywords used to highlight Semantic grammar buffers.")
1164 (defvar semantic-grammar-map
1165 (let ((km (make-sparse-keymap)))
1167 (define-key km "|" 'semantic-grammar-electric-punctuation)
1168 (define-key km ";" 'semantic-grammar-electric-punctuation)
1169 (define-key km "%" 'semantic-grammar-electric-punctuation)
1170 (define-key km "(" 'semantic-grammar-electric-punctuation)
1171 (define-key km ")" 'semantic-grammar-electric-punctuation)
1172 (define-key km ":" 'semantic-grammar-electric-punctuation)
1174 (define-key km "\t" 'semantic-grammar-indent)
1175 (define-key km "\M-\t" 'semantic-grammar-complete)
1176 (define-key km "\C-c\C-c" 'semantic-grammar-create-package)
1177 (define-key km "\C-cm" 'semantic-grammar-find-macro-expander)
1178 (define-key km "\C-cik" 'semantic-grammar-insert-keyword)
1179 ;; (define-key km "\C-cc" 'semantic-grammar-generate-and-load)
1180 ;; (define-key km "\C-cr" 'semantic-grammar-generate-one-rule)
1183 "Keymap used in `semantic-grammar-mode'.")
1185 (defvar semantic-grammar-menu
1187 ["Indent Line" semantic-grammar-indent]
1188 ["Complete Symbol" semantic-grammar-complete]
1189 ["Find Macro" semantic-grammar-find-macro-expander]
1191 ["Insert %keyword" semantic-grammar-insert-keyword]
1193 ["Update Lisp Package" semantic-grammar-create-package]
1194 ["Recreate Lisp Package" semantic-grammar-recreate-package]
1196 "Common semantic grammar menu.")
1198 (defun semantic-grammar-setup-menu-emacs (symbol mode-menu)
1199 "Setup a GNU Emacs grammar menu in variable SYMBOL.
1200 MODE-MENU is an optional specific menu whose items are appended to the
1201 common grammar menu."
1202 (let ((items (make-symbol "items")))
1203 `(unless (boundp ',symbol)
1204 (easy-menu-define ,symbol (current-local-map)
1205 "Grammar Menu" semantic-grammar-menu)
1206 (let ((,items (cdr ,mode-menu)))
1208 (easy-menu-add-item ,symbol nil "--")
1210 (easy-menu-add-item ,symbol nil (car ,items))
1211 (setq ,items (cdr ,items))))))
1214 (defun semantic-grammar-setup-menu-xemacs (symbol mode-menu)
1215 "Setup an XEmacs grammar menu in variable SYMBOL.
1216 MODE-MENU is an optional specific menu whose items are appended to the
1217 common grammar menu."
1218 (let ((items (make-symbol "items"))
1219 (path (make-symbol "path")))
1221 (unless (boundp ',symbol)
1222 (easy-menu-define ,symbol nil
1223 "Grammar Menu" (copy-sequence semantic-grammar-menu)))
1224 (easy-menu-add ,symbol)
1225 (let ((,items (cdr ,mode-menu))
1226 (,path (list (car ,symbol))))
1228 (easy-menu-add-item nil ,path "--")
1230 (easy-menu-add-item nil ,path (car ,items))
1231 (setq ,items (cdr ,items))))))
1234 (defmacro semantic-grammar-setup-menu (&optional mode-menu)
1235 "Setup a mode local grammar menu.
1236 MODE-MENU is an optional specific menu whose items are appended to the
1237 common grammar menu."
1238 (let ((menu (intern (format "%s-menu" major-mode))))
1239 (if (featurep 'xemacs)
1240 (semantic-grammar-setup-menu-xemacs menu mode-menu)
1241 (semantic-grammar-setup-menu-emacs menu mode-menu))))
1243 (defsubst semantic-grammar-in-lisp-p ()
1244 "Return non-nil if point is in Lisp code."
1245 (or (>= (point) (semantic-grammar-epilogue-start))
1252 (defun semantic-grammar-edits-new-change-hook-fcn (overlay)
1253 "Function set into `semantic-edits-new-change-hook'.
1254 Argument OVERLAY is the overlay created to mark the change.
1255 When OVERLAY marks a change in the scope of a nonterminal tag extend
1256 the change bounds to encompass the whole nonterminal tag."
1257 (let ((outer (car (semantic-find-tag-by-overlay-in-region
1258 (semantic-edits-os overlay)
1259 (semantic-edits-oe overlay)))))
1260 (if (semantic-tag-of-class-p outer 'nonterminal)
1261 (semantic-overlay-move overlay
1262 (semantic-tag-start outer)
1263 (semantic-tag-end outer)))))
1265 (defun semantic-grammar-mode ()
1266 "Initialize a buffer for editing Semantic grammars.
1268 \\{semantic-grammar-map}"
1270 (kill-all-local-variables)
1271 (setq major-mode 'semantic-grammar-mode
1272 mode-name "Semantic Grammar Framework")
1273 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1274 (set (make-local-variable 'comment-start) ";;")
1275 ;; Look within the line for a ; following an even number of backslashes
1276 ;; after either a non-backslash or the line beginning.
1277 (set (make-local-variable 'comment-start-skip)
1278 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
1279 (set-syntax-table semantic-grammar-syntax-table)
1280 (use-local-map semantic-grammar-map)
1281 (set (make-local-variable 'indent-line-function)
1282 'semantic-grammar-indent)
1283 (set (make-local-variable 'fill-paragraph-function)
1284 'lisp-fill-paragraph)
1285 (set (make-local-variable 'font-lock-multiline)
1287 (set (make-local-variable 'font-lock-defaults)
1288 '((semantic-grammar-mode-keywords
1289 semantic-grammar-mode-keywords-1
1290 semantic-grammar-mode-keywords-2
1291 semantic-grammar-mode-keywords-3)
1292 nil ;; perform string/comment fontification
1293 nil ;; keywords are case sensitive.
1294 ;; This puts _ & - as a word constituant,
1295 ;; simplifying our keywords significantly
1296 ((?_ . "w") (?- . "w"))))
1297 ;; Setup Semantic to parse grammar
1298 (semantic-grammar-wy--install-parser)
1299 (setq semantic-lex-comment-regex ";;"
1300 semantic-lex-analyzer 'semantic-grammar-lexer
1301 semantic-type-relation-separator-character '(":")
1302 semantic-symbol->name-assoc-list
1304 (code . "Setup Code")
1305 (keyword . "Keyword")
1307 (nonterminal . "Nonterminal")
1310 (set (make-local-variable 'semantic-format-face-alist)
1313 (keyword . font-lock-keyword-face)
1314 (token . font-lock-type-face)
1315 (nonterminal . font-lock-function-name-face)
1318 (set (make-local-variable 'semantic-stickyfunc-sticky-classes)
1320 ;; Before each change, clear the cached regexp used to highlight
1321 ;; macros local in this grammar.
1322 (semantic-make-local-hook 'before-change-functions)
1323 (add-hook 'before-change-functions
1324 'semantic--grammar-clear-macros-regexp-2 nil t)
1325 ;; Handle safe re-parse of grammar rules.
1326 (semantic-make-local-hook 'semantic-edits-new-change-hooks)
1327 (add-hook 'semantic-edits-new-change-hooks
1328 'semantic-grammar-edits-new-change-hook-fcn
1330 (semantic-run-mode-hooks 'semantic-grammar-mode-hook))
1333 ;;;; Useful commands
1336 (defvar semantic-grammar-skip-quoted-syntax-table
1337 (let ((st (copy-syntax-table semantic-grammar-syntax-table)))
1338 (modify-syntax-entry ?\' "$" st)
1340 "Syntax table to skip a whole quoted expression in grammar code.
1341 Consider quote as a \"paired delimiter\", so `forward-sexp' will skip
1342 whole quoted expression.")
1344 (defsubst semantic-grammar-backward-item ()
1345 "Move point to beginning of the previous grammar item."
1346 (forward-comment (- (point-max)))
1347 (if (zerop (skip-syntax-backward "."))
1348 (if (eq (char-before) ?\')
1350 ;; Can't be Lisp code here! Temporarily consider quote
1351 ;; as a "paired delimiter", so `forward-sexp' can skip
1352 ;; the whole quoted expression.
1353 semantic-grammar-skip-quoted-syntax-table
1355 (forward-sexp -1))))
1357 (defun semantic-grammar-anchored-indentation ()
1358 "Return indentation based on previous anchor character found."
1362 (semantic-grammar-backward-item)
1366 ((looking-at ":\\(\\s-\\|$\\)")
1367 (setq indent (current-column))
1369 (skip-syntax-forward "-")
1370 (if (eolp) (setq indent 2))
1372 ((and (looking-at "[;%]")
1373 (not (looking-at "\\<%prec\\>")))
1378 (defun semantic-grammar-do-grammar-indent ()
1379 "Indent a line of grammar.
1380 When called the point is not in Lisp code."
1384 (skip-syntax-forward "-")
1385 (setq indent (current-column))
1388 (looking-at "\\(\\w\\|\\s_\\)+\\s-*:")
1389 (and (looking-at "%")
1390 (not (looking-at "%prec\\>"))))
1394 ((and (looking-at ";;")
1395 (save-excursion (forward-comment (point-max))
1399 (setq n (semantic-grammar-anchored-indentation))
1404 ((looking-at "[|;]")
1407 (setq n (+ n 2)))))))
1410 (delete-horizontal-space)
1413 (defvar semantic-grammar-brackets-as-parens-syntax-table
1414 (let ((st (copy-syntax-table emacs-lisp-mode-syntax-table)))
1415 (modify-syntax-entry ?\{ "(} " st)
1416 (modify-syntax-entry ?\} "){ " st)
1418 "Syntax table that consider brackets as parenthesis.
1419 So `lisp-indent-line' will work inside bracket blocks.")
1421 (defun semantic-grammar-do-lisp-indent ()
1422 "Maybe run the Emacs Lisp indenter on a line of code.
1423 Return nil if not in a Lisp expression."
1427 (skip-chars-forward "\t ")
1428 (let ((first (point)))
1429 (or (>= first (semantic-grammar-epilogue-start))
1437 (narrow-to-region (point) first)
1438 (goto-char (point-max))
1440 ;; Temporarily consider brackets as parenthesis so
1441 ;; `lisp-indent-line' can indent Lisp code inside
1443 semantic-grammar-brackets-as-parens-syntax-table
1444 (lisp-indent-line))))
1448 (defun semantic-grammar-indent ()
1449 "Indent the current line.
1450 Use the Lisp or grammar indenter depending on point location."
1452 (let ((orig (point))
1454 (or (semantic-grammar-do-lisp-indent)
1455 (semantic-grammar-do-grammar-indent))
1456 (setq first (save-excursion
1458 (skip-chars-forward "\t ")
1460 (if (or (< orig first) (/= orig (point)))
1461 (goto-char first))))
1463 (defun semantic-grammar-electric-punctuation ()
1464 "Insert and reindent for the symbol just typed in."
1466 (self-insert-command 1)
1468 (semantic-grammar-indent)))
1470 (defun semantic-grammar-complete ()
1471 "Attempt to complete the symbol under point.
1472 Completion is position sensitive. If the cursor is in a match section of
1473 a rule, then nonterminals symbols are scanned. If the cursor is in a Lisp
1474 expression then Lisp symbols are completed."
1476 (if (semantic-grammar-in-lisp-p)
1477 ;; We are in lisp code. Do lisp completion.
1478 (lisp-complete-symbol)
1479 ;; We are not in lisp code. Do rule completion.
1480 (let* ((nonterms (semantic-find-tags-by-class 'nonterminal (current-buffer)))
1481 (sym (car (semantic-ctxt-current-symbol)))
1482 (ans (try-completion sym nonterms)))
1485 (message "Symbols is already complete"))
1486 ((and (stringp ans) (string= ans sym))
1487 ;; Max matchable. Show completions.
1488 (with-output-to-temp-buffer "*Completions*"
1489 (display-completion-list (all-completions sym nonterms)))
1492 ;; Expand the completions
1494 (delete-region (point) (progn (forward-sexp 1) (point)))
1496 (t (message "No Completions."))
1500 (defun semantic-grammar-insert-keyword (name)
1501 "Insert a new %keyword declaration with NAME.
1502 Assumes it is typed in with the correct casing."
1503 (interactive "sKeyword: ")
1504 (if (not (bolp)) (insert "\n"))
1505 (insert "%keyword " (upcase name) " \"" name "\"
1506 %put " (upcase name) " summary
1510 ;;; Macro facilities
1513 (defsubst semantic--grammar-macro-function-tag (name)
1514 "Search for a function tag for the grammar macro with name NAME.
1515 Return the tag found or nil if not found."
1516 (car (semantic-find-tags-by-class
1518 (or (semantic-find-tags-by-name name (current-buffer))
1519 (and (featurep 'semantic/db)
1520 semanticdb-current-database
1521 (cdar (semanticdb-find-tags-by-name name nil t)))))))
1523 (defsubst semantic--grammar-macro-lib-part (def)
1524 "Return the library part of the grammar macro defined by DEF."
1525 (let ((suf (format "-%s\\'" (regexp-quote (symbol-name (car def)))))
1526 (fun (symbol-name (cdr def))))
1527 (substring fun 0 (string-match suf fun))))
1529 (defun semantic--grammar-macro-compl-elt (def &optional full)
1530 "Return a completion entry for the grammar macro defined by DEF.
1531 If optional argument FULL is non-nil qualify the macro name with the
1532 library found in DEF."
1533 (let ((mac (car def))
1534 (lib (semantic--grammar-macro-lib-part def)))
1536 (format "%s/%s" mac lib)
1540 (defun semantic--grammar-macro-compl-dict ()
1541 "Return a completion dictionary of macro definitions."
1542 (let ((defs (semantic-grammar-macros))
1545 (setq def (car defs)
1547 (if (or (assoc (car def) defs) (assoc (car def) dups))
1549 (push (semantic--grammar-macro-compl-elt def) dict)))
1551 (setq def (car dups)
1553 (push (semantic--grammar-macro-compl-elt def t) dict))
1556 (defun semantic-grammar-find-macro-expander (macro-name library)
1557 "Visit the Emacs Lisp library where a grammar macro is implemented.
1558 MACRO-NAME is a symbol that identifies a grammar macro.
1559 LIBRARY is the name (sans extension) of the Emacs Lisp library where
1560 to start searching the macro implementation. Lookup in included
1561 libraries, if necessary.
1562 Find a function tag (in current tags table) whose name contains MACRO-NAME.
1563 Select the buffer containing the tag's definition, and move point there."
1565 (let* ((dic (semantic--grammar-macro-compl-dict))
1566 (def (assoc (completing-read "Macro: " dic nil 1) dic)))
1567 (or (cdr def) '(nil nil))))
1568 (when (and macro-name library)
1569 (let* ((lib (format "%s.el" library))
1570 (buf (find-file-noselect (or (locate-library lib t) lib)))
1571 (tag (with-current-buffer buf
1572 (semantic--grammar-macro-function-tag
1573 (format "%s-%s" library macro-name)))))
1576 (require 'semantic/decorate)
1577 (pop-to-buffer (semantic-tag-buffer tag))
1578 (goto-char (semantic-tag-start tag))
1579 (semantic-momentary-highlight-tag tag))
1581 (message "No expander found in library %s for macro %s"
1582 library macro-name)))))
1587 (defvar semantic-grammar-syntax-help
1590 ("symbol" . "Syntax: A symbol of alpha numeric and symbol characters")
1591 ("number" . "Syntax: Numeric characters.")
1592 ("punctuation" . "Syntax: Punctuation character.")
1593 ("semantic-list" . "Syntax: A list delimited by any valid list characters")
1594 ("open-paren" . "Syntax: Open Parenthesis character")
1595 ("close-paren" . "Syntax: Close Parenthesis character")
1596 ("string" . "Syntax: String character delimited text")
1597 ("comment" . "Syntax: Comment character delimited text")
1599 ("EMPTY" . "Syntax: Match empty text")
1600 ("ASSOC" . "Lambda Key: (ASSOC key1 value1 key2 value2 ...)")
1601 ("EXPAND" . "Lambda Key: (EXPAND <list id> <rule>)")
1602 ("EXPANDFULL" . "Lambda Key: (EXPANDFULL <list id> <rule>)")
1603 ;; Tag Generator Macros
1604 ("TAG" . "Generic Tag Generation: (TAG <name> <tag-class> [ :key value ]*)")
1605 ("VARIABLE-TAG" . "(VARIABLE-TAG <name> <lang-type> <default-value> [ :key value ]*)")
1606 ("FUNCTION-TAG" . "(FUNCTION-TAG <name> <lang-type> <arg-list> [ :key value ]*)")
1607 ("TYPE-TAG" . "(TYPE-TAG <name> <lang-type> <part-list> <parents> [ :key value ]*)")
1608 ("INCLUDE-TAG" . "(INCLUDE-TAG <name> <system-flag> [ :key value ]*)")
1609 ("PACKAGE-TAG" . "(PACKAGE-TAG <name> <detail> [ :key value ]*)")
1610 ("CODE-TAG" . "(CODE-TAG <name> <detail> [ :key value ]*)")
1611 ("ALIAS-TAG" . "(ALIAS-TAG <name> <aliasclass> <definition> [:key value]*)")
1612 ;; Special value macros
1613 ("$1" . "Match Value: Value from match list in slot 1")
1614 ("$2" . "Match Value: Value from match list in slot 2")
1615 ("$3" . "Match Value: Value from match list in slot 3")
1616 ("$4" . "Match Value: Value from match list in slot 4")
1617 ("$5" . "Match Value: Value from match list in slot 5")
1618 ("$6" . "Match Value: Value from match list in slot 6")
1619 ("$7" . "Match Value: Value from match list in slot 7")
1620 ("$8" . "Match Value: Value from match list in slot 8")
1621 ("$9" . "Match Value: Value from match list in slot 9")
1622 ;; Same, but with annoying , in front.
1623 (",$1" . "Match Value: Value from match list in slot 1")
1624 (",$2" . "Match Value: Value from match list in slot 2")
1625 (",$3" . "Match Value: Value from match list in slot 3")
1626 (",$4" . "Match Value: Value from match list in slot 4")
1627 (",$5" . "Match Value: Value from match list in slot 5")
1628 (",$6" . "Match Value: Value from match list in slot 6")
1629 (",$7" . "Match Value: Value from match list in slot 7")
1630 (",$8" . "Match Value: Value from match list in slot 8")
1631 (",$9" . "Match Value: Value from match list in slot 9")
1633 "Association of syntax elements, and the corresponding help.")
1635 (defun semantic-grammar-eldoc-get-macro-docstring (macro expander)
1636 "Return a one-line docstring for the given grammar MACRO.
1637 EXPANDER is the name of the function that expands MACRO."
1639 (if (and (eq expander (aref eldoc-last-data 0))
1640 (eq 'function (aref eldoc-last-data 2)))
1641 (aref eldoc-last-data 1)
1642 (let ((doc (help-split-fundoc (documentation expander t) expander)))
1645 (setq doc (car doc))
1646 (string-match "\\`[^ )]* ?" doc)
1647 (setq doc (concat "(" (substring doc (match-end 0)))))
1649 (setq doc (eldoc-function-argstring expander))))
1652 (eldoc-docstring-format-sym-doc
1653 macro (format "==> %s %s" expander doc) 'default))
1654 (eldoc-last-data-store expander doc 'function))
1657 (define-mode-local-override semantic-idle-summary-current-symbol-info
1658 semantic-grammar-mode ()
1659 "Display additional eldoc information about grammar syntax elements.
1660 Syntax element is the current symbol at point.
1661 If it is associated a help string in `semantic-grammar-syntax-help',
1663 If it is a macro name, return a description of the associated expander
1664 function parameter list.
1665 If it is a function name, return a description of this function
1667 It it is a variable name, return a brief (one-line) documentation
1668 string for the variable.
1669 If a default description of the current context can be obtained,
1671 Otherwise return nil."
1673 (let* ((elt (car (semantic-ctxt-current-symbol)))
1674 (val (and elt (cdr (assoc elt semantic-grammar-syntax-help)))))
1675 (when (and (not val) elt (semantic-grammar-in-lisp-p))
1676 ;; Ensure to load macro definitions before doing `intern-soft'.
1677 (setq val (semantic-grammar-macros)
1678 elt (intern-soft elt)
1679 val (and elt (cdr (assq elt val))))
1682 ((and val (fboundp val))
1683 (setq val (semantic-grammar-eldoc-get-macro-docstring elt val)))
1685 ((and elt (fboundp elt))
1686 (setq val (eldoc-get-fnsym-args-string elt)))
1688 ((and elt (boundp elt))
1689 (setq val (eldoc-get-var-docstring elt)))
1691 (or val (semantic-idle-summary-current-symbol-info-default))))
1693 (define-mode-local-override semantic-tag-boundary-p
1694 semantic-grammar-mode (tag)
1695 "Return non-nil for tags that should have a boundary drawn.
1696 Only tags of type 'nonterminal will be so marked."
1697 (let ((c (semantic-tag-class tag)))
1698 (eq c 'nonterminal)))
1700 (define-mode-local-override semantic-ctxt-current-function
1701 semantic-grammar-mode (&optional point)
1702 "Determine the name of the current function at POINT."
1704 (and point (goto-char point))
1705 (when (semantic-grammar-in-lisp-p)
1706 (with-mode-local emacs-lisp-mode
1707 (semantic-ctxt-current-function)))))
1709 (define-mode-local-override semantic-ctxt-current-argument
1710 semantic-grammar-mode (&optional point)
1711 "Determine the argument index of the called function at POINT."
1713 (and point (goto-char point))
1714 (when (semantic-grammar-in-lisp-p)
1715 (with-mode-local emacs-lisp-mode
1716 (semantic-ctxt-current-argument)))))
1718 (define-mode-local-override semantic-ctxt-current-assignment
1719 semantic-grammar-mode (&optional point)
1720 "Determine the tag being assigned into at POINT."
1722 (and point (goto-char point))
1723 (when (semantic-grammar-in-lisp-p)
1724 (with-mode-local emacs-lisp-mode
1725 (semantic-ctxt-current-assignment)))))
1727 (define-mode-local-override semantic-ctxt-current-class-list
1728 semantic-grammar-mode (&optional point)
1729 "Determine the class of tags that can be used at POINT."
1731 (and point (goto-char point))
1732 (if (semantic-grammar-in-lisp-p)
1733 (with-mode-local emacs-lisp-mode
1734 (semantic-ctxt-current-class-list))
1735 '(nonterminal keyword))))
1737 (define-mode-local-override semantic-ctxt-current-mode
1738 semantic-grammar-mode (&optional point)
1739 "Return the major mode active at POINT.
1740 POINT defaults to the value of point in current buffer.
1741 Return `emacs-lisp-mode' is POINT is within Lisp code, otherwise
1742 return the current major mode."
1744 (and point (goto-char point))
1745 (if (semantic-grammar-in-lisp-p)
1747 (semantic-ctxt-current-mode-default))))
1749 (define-mode-local-override semantic-format-tag-abbreviate
1750 semantic-grammar-mode (tag &optional parent color)
1751 "Return a string abbreviation of TAG.
1752 Optional PARENT is not used.
1753 Optional COLOR is used to flag if color is added to the text."
1754 (let ((class (semantic-tag-class tag))
1755 (name (semantic-format-tag-name tag parent color)))
1757 ((eq class 'nonterminal)
1759 ((eq class 'setting)
1761 ((memq class '(rule keyword))
1764 (concat "%" (symbol-name class) " " name)))))
1766 (define-mode-local-override semantic-format-tag-summarize
1767 semantic-grammar-mode (tag &optional parent color)
1768 "Return a string summarizing TAG.
1769 Optional PARENT is not used.
1770 Optional argument COLOR determines if color is added to the text."
1771 (let ((class (semantic-tag-class tag))
1772 (name (semantic-format-tag-name tag parent color))
1776 ((eq class 'nonterminal)
1777 (setq label "Nonterminal: "
1779 " with %d match lists."
1780 (length (semantic-tag-components tag)))))
1781 ((eq class 'keyword)
1782 (setq label "Keyword: ")
1784 (semantic--find-tags-by-function
1787 (setq summary (cdr (assoc "summary"
1788 (semantic-tag-get-attribute
1790 ;; Get `put' tag with TAG name.
1791 (semantic-find-tags-by-name-regexp
1792 (regexp-quote (semantic-tag-name tag))
1793 (semantic-find-tags-by-class 'put (current-buffer))))
1794 (setq desc (concat " = "
1795 (semantic-tag-get-attribute tag :value)
1797 (concat " - " (read summary))
1800 (setq label "Token: ")
1801 (let ((val (semantic-tag-get-attribute tag :value))
1802 (names (semantic-tag-get-attribute tag :rest))
1803 (type (semantic-tag-type tag)))
1805 (setq name (mapconcat 'identity (cons name names) " ")))
1808 (format " <%s>" type)
1811 (format "%s%S" val (if type " " ""))
1814 (setq label "Assoc: ")
1815 (let ((val (semantic-tag-get-attribute tag :value))
1816 (type (semantic-tag-type tag)))
1819 (format " <%s>" type)
1822 (concat " " (mapconcat 'identity val " "))
1825 (setq desc (semantic-format-tag-abbreviate tag parent color))))
1826 (if (and color label)
1827 (setq label (semantic--format-colorize-text label 'label)))
1828 (if (and color label desc)
1829 (setq desc (semantic--format-colorize-text desc 'comment)))
1831 (concat label name desc)
1832 ;; Just a description is the abbreviated version
1835 ;;; Semantic Analysis
1837 (define-mode-local-override semantic-analyze-current-context
1838 semantic-grammar-mode (point)
1839 "Provide a semantic analysis object describing a context in a grammar."
1840 (require 'semantic/analyze)
1841 (if (semantic-grammar-in-lisp-p)
1842 (with-mode-local emacs-lisp-mode
1843 (semantic-analyze-current-context point))
1845 (let* ((context-return nil)
1846 (prefixandbounds (semantic-ctxt-current-symbol-and-bounds))
1847 (prefix (car prefixandbounds))
1848 (bounds (nth 2 prefixandbounds))
1850 (prefixclass (semantic-ctxt-current-class-list))
1853 ;; Do context for rules when in a match list.
1855 (semantic-find-first-tag-by-name
1859 (setq context-return
1860 (semantic-analyze-context
1861 "context-for-semantic-grammar"
1862 :buffer (current-buffer)
1865 :prefix (if prefixsym
1869 :prefixclass prefixclass
1874 (define-mode-local-override semantic-analyze-possible-completions
1875 semantic-grammar-mode (context)
1876 "Return a list of possible completions based on CONTEXT."
1877 (require 'semantic/analyze/complete)
1878 (if (semantic-grammar-in-lisp-p)
1879 (with-mode-local emacs-lisp-mode
1880 (semantic-analyze-possible-completions context))
1881 (with-current-buffer (oref context buffer)
1882 (let* ((prefix (car (oref context :prefix)))
1883 (completetext (cond ((semantic-tag-p prefix)
1884 (semantic-tag-name prefix))
1887 ((stringp (car prefix))
1889 (tags (semantic-find-tags-for-completion completetext
1891 (semantic-analyze-tags-of-class-list
1892 tags (oref context prefixclass)))
1895 (provide 'semantic/grammar)
1897 ;;; semantic/grammar.el ends here