1 ;;; semantic/grammar.el --- Major mode framework for Semantic grammars
3 ;; Copyright (C) 2002-2005, 2007-2016 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Major mode framework for editing Semantic's input grammar files.
33 (require 'semantic/wisent)
34 (require 'semantic/ctxt)
35 (require 'semantic/format)
36 ;; FIXME this is a generated file, but we need to load this file to
38 (require 'semantic/grammar-wy)
39 (require 'semantic/idle)
42 (declare-function semantic-momentary-highlight-tag "semantic/decorate")
43 (declare-function semantic-analyze-context "semantic/analyze")
44 (declare-function semantic-analyze-tags-of-class-list
45 "semantic/analyze/complete")
49 (require 'semantic/edit)
50 (require 'semantic/find)
51 (require 'semantic/db))
53 (declare-function semantic-grammar-wy--install-parser "semantic/grammar-wy")
60 (defconst semantic-grammar-lex-c-char-re "'\\s\\?.'"
61 "Regexp matching C-like character literals.")
63 ;; Most of the analyzers are auto-generated from the grammar, but the
64 ;; following which need special handling code.
66 (define-lex-regex-analyzer semantic-grammar-lex-prologue
67 "Detect and create a prologue token."
69 ;; Zing to the end of this brace block.
70 (semantic-lex-push-token
74 (semantic-lex-unterminated-syntax-protection 'PROLOGUE
79 (defsubst semantic-grammar-epilogue-start ()
80 "Return the start position of the grammar epilogue."
82 (goto-char (point-min))
83 (if (re-search-forward "^\\s-*\\<%%\\>\\s-*$" nil t 2)
87 (define-lex-regex-analyzer semantic-grammar-lex-epilogue
88 "Detect and create an epilogue or percent-percent token."
90 (let ((start (match-beginning 0))
92 (class 'PERCENT_PERCENT))
93 (when (>= start (semantic-grammar-epilogue-start))
96 (semantic-lex-push-token
97 (semantic-lex-token class start end))))
99 (define-lex semantic-grammar-lexer
100 "Lexical analyzer that handles Semantic grammar buffers.
101 It ignores whitespaces, newlines and comments."
102 semantic-lex-ignore-newline
103 semantic-lex-ignore-whitespace
104 ;; Must detect prologue/epilogue before other symbols/keywords!
105 semantic-grammar-lex-prologue
106 semantic-grammar-lex-epilogue
107 semantic-grammar-wy--<keyword>-keyword-analyzer
108 semantic-grammar-wy--<symbol>-regexp-analyzer
109 semantic-grammar-wy--<char>-regexp-analyzer
110 semantic-grammar-wy--<string>-sexp-analyzer
111 ;; Must detect comments after strings because `comment-start-skip'
112 ;; regexp match semicolons inside strings!
113 semantic-lex-ignore-comments
114 ;; Must detect prefixed list before punctuation because prefix chars
115 ;; are also punctuation!
116 semantic-grammar-wy--<qlist>-sexp-analyzer
117 ;; Must detect punctuation after comments because the semicolon can
118 ;; be punctuation or a comment start!
119 semantic-grammar-wy--<punctuation>-string-analyzer
120 semantic-grammar-wy--<block>-block-analyzer
121 semantic-grammar-wy--<sexp>-sexp-analyzer)
125 (defun semantic-grammar-lex-buffer ()
126 "Run `semantic-grammar-lex' on current buffer."
129 (setq semantic-lex-analyzer 'semantic-grammar-lexer)
131 (semantic-lex (point-min) (point-max))))
132 (with-current-buffer (get-buffer-create "*semantic-grammar-lex*")
134 (pp token-stream (current-buffer))
135 (goto-char (point-min))
136 (pop-to-buffer (current-buffer)))))
139 ;;;; Semantic action expansion
142 (defun semantic-grammar-ASSOC (&rest args)
143 "Return expansion of built-in ASSOC expression.
144 ARGS are ASSOC's key value list."
146 `(semantic-tag-make-assoc-list
147 ,@(mapcar #'(lambda (i)
152 (setq key (not key))))
155 (defsubst semantic-grammar-quote-p (sym)
156 "Return non-nil if SYM is bound to the `quote' function."
158 (eq (indirect-function sym)
159 (indirect-function 'quote))
162 (defsubst semantic-grammar-backquote-p (sym)
163 "Return non-nil if SYM is bound to the `backquote' function."
165 (eq (indirect-function sym)
166 (indirect-function 'backquote))
170 ;;;; API to access grammar tags
173 (define-mode-local-override semantic-tag-components
174 semantic-grammar-mode (tag)
175 "Return the children of tag TAG."
176 (semantic-tag-get-attribute tag :children))
178 (defun semantic-grammar-first-tag-name (class)
179 "Return the name of the first tag of class CLASS found.
180 Warn if other tags of class CLASS exist."
181 (let* ((tags (semantic-find-tags-by-class
182 class (current-buffer))))
185 (semantic-tag-name (car tags))
187 (message "*** Ignore all but first declared %s"
190 (defun semantic-grammar-tag-symbols (class)
191 "Return the list of symbols defined in tags of class CLASS.
192 That is tag names plus names defined in tag attribute `:rest'."
193 (let* ((tags (semantic-find-tags-by-class
194 class (current-buffer))))
200 (cons (semantic-tag-name tag)
201 (semantic-tag-get-attribute tag :rest))))
204 (defsubst semantic-grammar-item-text (item)
205 "Return the readable string form of ITEM."
206 (if (string-match semantic-grammar-lex-c-char-re item)
207 (concat "?" (substring item 1 -1))
210 (defsubst semantic-grammar-item-value (item)
211 "Return symbol or character value of ITEM string."
212 (if (string-match semantic-grammar-lex-c-char-re item)
213 (let ((c (read (concat "?" (substring item 1 -1)))))
214 (if (featurep 'xemacs)
215 ;; Handle characters as integers in XEmacs like in GNU Emacs.
220 (defun semantic-grammar-prologue ()
221 "Return grammar prologue code as a string value."
222 (let ((tag (semantic-find-first-tag-by-name
224 (semantic-find-tags-by-class 'code (current-buffer)))))
230 (goto-char (semantic-tag-start tag))
231 (skip-chars-forward "%{\r\n\t ")
234 (goto-char (semantic-tag-end tag))
235 (skip-chars-backward "\r\n\t %}")
240 (defun semantic-grammar-epilogue ()
241 "Return grammar epilogue code as a string value."
242 (let ((tag (semantic-find-first-tag-by-name
244 (semantic-find-tags-by-class 'code (current-buffer)))))
250 (goto-char (semantic-tag-start tag))
251 (skip-chars-forward "%\r\n\t ")
254 (goto-char (semantic-tag-end tag))
255 (skip-chars-backward "\r\n\t")
256 ;; If a grammar footer is found, skip it.
257 (re-search-backward "^;;;\\s-+\\S-+\\s-+ends here"
259 (skip-chars-backward "\r\n\t")
264 (defsubst semantic-grammar-buffer-file (&optional buffer)
265 "Return name of file sans directory BUFFER is visiting.
266 No argument or nil as argument means use the current buffer."
267 (file-name-nondirectory (buffer-file-name buffer)))
269 (defun semantic-grammar-package ()
270 "Return the %package value as a string.
271 If there is no %package statement in the grammar, return a default
272 package name derived from the grammar file name. For example, the
273 default package name for the grammar file foo.wy is foo-wy, and for
274 foo.by it is foo-by."
275 (or (semantic-grammar-first-tag-name 'package)
276 (let* ((file (semantic-grammar-buffer-file))
277 (ext (file-name-extension file))
278 (i (string-match (format "\\([.]\\)%s\\'" ext) file)))
279 (concat (substring file 0 i) "-" ext))))
281 (defsubst semantic-grammar-languagemode ()
282 "Return the %languagemode value as a list of symbols or nil."
283 (semantic-grammar-tag-symbols 'languagemode))
285 (defsubst semantic-grammar-start ()
286 "Return the %start value as a list of symbols or nil."
287 (semantic-grammar-tag-symbols 'start))
289 (defsubst semantic-grammar-scopestart ()
290 "Return the %scopestart value as a symbol or nil."
291 (intern (or (semantic-grammar-first-tag-name 'scopestart) "nil")))
293 (defsubst semantic-grammar-quotemode ()
294 "Return the %quotemode value as a symbol or nil."
295 (intern (or (semantic-grammar-first-tag-name 'quotemode) "nil")))
297 (defsubst semantic-grammar-keywords ()
298 "Return the language keywords.
299 That is an alist of (VALUE . TOKEN) where VALUE is the string value of
300 the keyword and TOKEN is the terminal symbol identifying the keyword."
303 (cons (semantic-tag-get-attribute key :value)
304 (intern (semantic-tag-name key))))
305 (semantic-find-tags-by-class 'keyword (current-buffer))))
307 (defun semantic-grammar-keyword-properties (keywords)
308 "Return the list of KEYWORDS properties."
309 (let ((puts (semantic-find-tags-by-class
310 'put (current-buffer)))
311 put keys key plist assoc pkey pval props)
317 (cons (semantic-tag-name put)
318 (semantic-tag-get-attribute put :rest))))
322 assoc (rassq key keywords))
324 nil ;;(message "*** %%put to undefined keyword %s ignored" key)
325 (setq key (car assoc)
326 plist (semantic-tag-get-attribute put :value))
328 (setq pkey (intern (caar plist))
329 pval (read (cdar plist))
330 props (cons (list key pkey pval) props)
331 plist (cdr plist))))))
334 (defun semantic-grammar-tokens ()
335 "Return defined lexical tokens.
336 That is an alist (TYPE . DEFS) where type is a %token <type> symbol
337 and DEFS is an alist of (TOKEN . VALUE). TOKEN is the terminal symbol
338 identifying the token and VALUE is the string value of the token or
340 (let (tags alist assoc tag type term names value)
342 ;; Check for <type> in %left, %right & %nonassoc declarations
343 (setq tags (semantic-find-tags-by-class
344 'assoc (current-buffer)))
348 (when (setq type (semantic-tag-type tag))
349 (setq names (semantic-tag-get-attribute tag :value)
350 assoc (assoc type alist))
351 (or assoc (setq assoc (list type)
352 alist (cons assoc alist)))
354 (setq term (car names)
356 (or (string-match semantic-grammar-lex-c-char-re term)
357 (setcdr assoc (cons (list (intern term))
360 ;; Then process %token declarations so they can override any
361 ;; previous specifications
362 (setq tags (semantic-find-tags-by-class
363 'token (current-buffer)))
367 (setq names (cons (semantic-tag-name tag)
368 (semantic-tag-get-attribute tag :rest))
369 type (or (semantic-tag-type tag) "<no-type>")
370 value (semantic-tag-get-attribute tag :value)
371 assoc (assoc type alist))
372 (or assoc (setq assoc (list type)
373 alist (cons assoc alist)))
375 (setq term (intern (car names))
377 (setcdr assoc (cons (cons term value) (cdr assoc)))))
380 (defun semantic-grammar-token-%type-properties (&optional props)
381 "Return properties set by %type statements.
382 This declare a new type if necessary.
383 If optional argument PROPS is non-nil, it is an existing list of
384 properties where to add new properties."
386 (dolist (tag (semantic-find-tags-by-class 'type (current-buffer)))
387 (setq type (semantic-tag-name tag))
388 ;; Indicate to auto-generate the analyzer for this type
389 (push (list type :declared t) props)
390 (dolist (e (semantic-tag-get-attribute tag :value))
391 (push (list type (intern (car e)) (read (or (cdr e) "nil")))
395 (defun semantic-grammar-token-%put-properties (tokens)
396 "For types found in TOKENS, return properties set by %put statements."
398 (dolist (put (semantic-find-tags-by-class 'put (current-buffer)))
399 (dolist (type (cons (semantic-tag-name put)
400 (semantic-tag-get-attribute put :rest)))
401 (setq found (assoc type tokens))
403 nil ;; %put <type> ignored, no token defined
404 (setq type (car found))
405 (dolist (e (semantic-tag-get-attribute put :value))
406 (push (list type (intern (car e)) (read (or (cdr e) "nil")))
410 (defsubst semantic-grammar-token-properties (tokens)
411 "Return properties of declared types.
412 Types are explicitly declared by %type statements. Types found in
413 TOKENS are those declared implicitly by %token statements.
414 Properties can be set by %put and %type statements.
415 Properties set by %type statements take precedence over those set by
417 (let ((props (semantic-grammar-token-%put-properties tokens)))
418 (semantic-grammar-token-%type-properties props)))
420 (defun semantic-grammar-use-macros ()
421 "Return macro definitions from %use-macros statements.
422 Also load the specified macro libraries."
424 (dolist (tag (semantic-find-tags-by-class 'macro (current-buffer)))
425 (setq lib (intern (semantic-tag-type tag)))
427 ;;(load lib) ;; Be sure to use the latest macro library.
430 (dolist (mac (semantic-tag-get-attribute tag :value))
431 (push (cons (intern mac)
432 (intern (format "%s-%s" lib mac)))
436 (defvar semantic-grammar-macros nil
437 "List of associations (MACRO-NAME . EXPANDER).")
438 (make-variable-buffer-local 'semantic-grammar-macros)
440 (defun semantic-grammar-macros ()
441 "Build and return the alist of defined macros."
443 ;; Definitions found in tags.
444 (semantic-grammar-use-macros)
445 ;; Other pre-installed definitions.
446 semantic-grammar-macros))
449 ;;;; Overloaded functions that build parser data.
452 ;;; Keyword table builder
454 (defun semantic-grammar-keywordtable-builder-default ()
455 "Return the default value of the keyword table."
456 (let ((keywords (semantic-grammar-keywords)))
457 `(semantic-lex-make-keyword-table
459 ',(semantic-grammar-keyword-properties keywords))))
461 (define-overloadable-function semantic-grammar-keywordtable-builder ()
462 "Return the keyword table value.")
464 ;;; Token table builder
466 (defun semantic-grammar-tokentable-builder-default ()
467 "Return the default value of the table of lexical tokens."
468 (let ((tokens (semantic-grammar-tokens)))
469 `(semantic-lex-make-type-table
471 ',(semantic-grammar-token-properties tokens))))
473 (define-overloadable-function semantic-grammar-tokentable-builder ()
474 "Return the value of the table of lexical tokens.")
476 ;;; Parser table builder
478 (defun semantic-grammar-parsetable-builder-default ()
479 "Return the default value of the parse table."
480 (error "`semantic-grammar-parsetable-builder' not defined"))
482 (define-overloadable-function semantic-grammar-parsetable-builder ()
483 "Return the parser table value.")
485 ;;; Parser setup code builder
487 (defun semantic-grammar-setupcode-builder-default ()
488 "Return the default value of the setup code form."
489 (error "`semantic-grammar-setupcode-builder' not defined"))
491 (define-overloadable-function semantic-grammar-setupcode-builder ()
492 "Return the parser setup code form.")
495 ;;;; Lisp code generation
497 (defvar semantic--grammar-input-buffer nil)
498 (defvar semantic--grammar-output-buffer nil)
499 (defvar semantic--grammar-package nil)
500 (defvar semantic--grammar-provide nil)
502 (defsubst semantic-grammar-keywordtable ()
503 "Return the variable name of the keyword table."
504 (concat semantic--grammar-package
507 (defsubst semantic-grammar-tokentable ()
508 "Return the variable name of the token table."
509 (concat semantic--grammar-package
512 (defsubst semantic-grammar-parsetable ()
513 "Return the variable name of the parse table."
514 (concat semantic--grammar-package
517 (defsubst semantic-grammar-setupfunction ()
518 "Return the name of the parser setup function."
519 (concat semantic--grammar-package
522 (defmacro semantic-grammar-as-string (object)
523 "Return OBJECT as a string value."
524 `(if (stringp ,object)
527 (pp-to-string ,object)))
529 (defun semantic-grammar-insert-defconst (name value docstring)
530 "Insert declaration of constant NAME with VALUE and DOCSTRING."
531 (let ((start (point)))
532 (insert (format "(defconst %s\n%s%S)\n\n" name value docstring))
537 (defun semantic-grammar-insert-defun (name body docstring)
538 "Insert declaration of function NAME with BODY and DOCSTRING."
539 (let ((start (point)))
540 (insert (format "(defun %s ()\n%S\n%s)\n\n" name docstring body))
545 (defun semantic-grammar-insert-define (define)
546 "Insert the declaration specified by DEFINE expression.
547 Typically a DEFINE expression should look like this:
549 \(define-thing name docstring expression1 ...)"
551 (let ((start (point)))
552 (insert (format "(%S %S" (car define) (nth 1 define)))
553 (dolist (item (nthcdr 2 define))
556 (pp item (current-buffer)))
562 (defconst semantic-grammar-header-template
564 ;;; " file " --- Generated parser support file
568 ;; Author: " user-full-name " <" user-mail-address ">
573 ;; This file is not part of GNU Emacs.
575 ;; This program is free software; you can redistribute it and/or
576 ;; modify it under the terms of the GNU General Public License as
577 ;; published by the Free Software Foundation, either version 3 of
578 ;; the License, or (at your option) any later version.
580 ;; This software is distributed in the hope that it will be useful,
581 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
582 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
583 ;; General Public License for more details.
585 ;; You should have received a copy of the GNU General Public License
586 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
590 ;; PLEASE DO NOT MANUALLY EDIT THIS FILE! It is automatically
591 ;; generated from the grammar file " gram ".
598 (require 'semantic/lex)
599 (eval-when-compile (require 'semantic/bovine))
601 "Generated header template.
602 The symbols in the template are local variables in
603 `semantic-grammar-header'")
605 (defconst semantic-grammar-footer-template
611 ;; version-control: never
612 ;; no-update-autoloads: t
615 ;;; " file " ends here
617 "Generated footer template.
618 The symbols in the list are local variables in
619 `semantic-grammar-footer'.")
621 (defun semantic-grammar-copyright-line ()
622 "Return the grammar copyright line, or nil if not found."
624 (goto-char (point-min))
625 (when (re-search-forward "^;;+[ \t]+Copyright (C) .*$"
626 ;; Search only in the four top lines
627 (save-excursion (forward-line 4) (point))
631 (defun semantic-grammar--template-expand (template env)
632 (mapconcat (lambda (S)
634 (let ((x (assq S env)))
637 ((symbolp S) (symbol-value S))))))
640 (defun semantic-grammar-header ()
641 "Return text of a generated standard header."
642 (semantic-grammar--template-expand
643 semantic-grammar-header-template
644 `((file . ,(semantic-grammar-buffer-file
645 semantic--grammar-output-buffer))
646 (gram . ,(semantic-grammar-buffer-file))
647 (date . ,(format-time-string "%Y-%m-%d %T%z"))
648 (vcid . ,(concat "$" "Id" "$")) ;; Avoid expansion
649 ;; Try to get the copyright from the input grammar, or
650 ;; generate a new one if not found.
651 (copy . ,(or (semantic-grammar-copyright-line)
652 (concat (format-time-string ";; Copyright (C) %Y ")
655 (defun semantic-grammar-footer ()
656 "Return text of a generated standard footer."
657 (semantic-grammar--template-expand
658 semantic-grammar-footer-template
659 `((file . ,(semantic-grammar-buffer-file
660 semantic--grammar-output-buffer))
661 (libr . ,(or semantic--grammar-provide
662 semantic--grammar-package)))))
664 (defun semantic-grammar-token-data ()
665 "Return the string value of the table of lexical tokens."
666 (semantic-grammar-as-string
667 (semantic-grammar-tokentable-builder)))
669 (defun semantic-grammar-keyword-data ()
670 "Return the string value of the table of keywords."
671 (semantic-grammar-as-string
672 (semantic-grammar-keywordtable-builder)))
674 (defun semantic-grammar-parser-data ()
675 "Return the parser table as a string value."
676 (semantic-grammar-as-string
677 (semantic-grammar-parsetable-builder)))
679 (defun semantic-grammar-setup-data ()
680 "Return the parser setup code form as a string value."
681 (semantic-grammar-as-string
682 (semantic-grammar-setupcode-builder)))
684 ;;; Generation of lexical analyzers.
686 (defvar semantic-grammar--lex-block-specs)
688 (defsubst semantic-grammar--lex-delim-spec (block-spec)
689 "Return delimiters specification from BLOCK-SPEC."
691 (let* ((standard-input (cdr block-spec))
693 (if (and (consp delim-spec)
694 (car delim-spec) (symbolp (car delim-spec))
695 (cadr delim-spec) (symbolp (cadr delim-spec)))
697 (error "Invalid delimiter")))
699 (error "Invalid delimiters specification %s in block token %s"
700 (cdr block-spec) (car block-spec)))))
702 (defun semantic-grammar--lex-block-specs ()
703 "Compute lexical block specifications for the current buffer.
704 Block definitions are read from the current table of lexical types."
706 ;; Block specifications have been parsed and are invalid.
707 ((eq semantic-grammar--lex-block-specs 'error)
710 ;; Parse block specifications.
711 ((null semantic-grammar--lex-block-specs)
713 (let* ((blocks (cdr (semantic-lex-type-value "block" t)))
714 (open-delims (cdr (semantic-lex-type-value "open-paren" t)))
715 (close-delims (cdr (semantic-lex-type-value "close-paren" t)))
716 olist clist delim-spec open-spec close-spec)
717 (dolist (block-spec blocks)
718 (setq delim-spec (semantic-grammar--lex-delim-spec block-spec)
719 open-spec (assq (car delim-spec) open-delims)
720 close-spec (assq (cadr delim-spec) close-delims))
722 (error "Missing open-paren token %s required by block %s"
723 (car delim-spec) (car block-spec)))
725 (error "Missing close-paren token %s required by block %s"
726 (cdr delim-spec) (car block-spec)))
727 ;; build alist ((OPEN-DELIM OPEN-SYM BLOCK-SYM) ...)
728 (push (list (cdr open-spec) (car open-spec) (car block-spec))
730 ;; build alist ((CLOSE-DELIM CLOSE-SYM) ...)
731 (push (list (cdr close-spec) (car close-spec))
733 (setq semantic-grammar--lex-block-specs (cons olist clist)))
735 (setq semantic-grammar--lex-block-specs 'error)
736 (message "%s" (error-message-string err))
739 ;; Block specifications already parsed.
741 semantic-grammar--lex-block-specs)))
743 (defsubst semantic-grammar-quoted-form (exp)
744 "Return a quoted form of EXP if it isn't a self evaluating form."
745 (if (and (not (null exp))
746 (or (listp exp) (symbolp exp)))
750 (defun semantic-grammar-insert-defanalyzer (type)
751 "Insert declaration of the lexical analyzer defined with TYPE."
752 (let* ((type-name (symbol-name type))
753 (type-value (symbol-value type))
754 (syntax (get type 'syntax))
755 (declared (get type :declared))
756 spec mtype prefix name doc)
757 ;; Generate an analyzer if the corresponding type has been
758 ;; explicitly declared in a %type statement, and if at least the
759 ;; syntax property has been provided.
760 (when (and declared syntax)
761 (setq prefix semantic--grammar-package
762 mtype (or (get type 'matchdatatype) 'regexp)
763 name (intern (format "%s--<%s>-%s-analyzer" prefix type mtype))
764 doc (format "%s analyzer for <%s> tokens." mtype type))
766 ;; Regexp match analyzer
768 (semantic-grammar-insert-define
769 `(define-lex-regex-type-analyzer ,name
771 ,(semantic-grammar-quoted-form (cdr type-value))
772 ',(or (car type-value) (intern type-name))))
774 ;; String compare analyzer
776 (semantic-grammar-insert-define
777 `(define-lex-string-type-analyzer ,name
779 ,(semantic-grammar-quoted-form (cdr type-value))
780 ',(or (car type-value) (intern type-name))))
783 ((and (eq mtype 'block)
784 (setq spec (semantic-grammar--lex-block-specs)))
785 (semantic-grammar-insert-define
786 `(define-lex-block-type-analyzer ,name
788 ,(semantic-grammar-quoted-form spec)))
792 (semantic-grammar-insert-define
793 `(define-lex-sexp-type-analyzer ,name
795 ',(or (car type-value) (intern type-name))))
799 (semantic-grammar-insert-define
800 `(define-lex-keyword-type-analyzer ,name
806 (defun semantic-grammar-insert-defanalyzers ()
807 "Insert declarations of lexical analyzers."
809 (with-current-buffer semantic--grammar-input-buffer
810 (setq tokens (semantic-grammar-tokens)
811 props (semantic-grammar-token-properties tokens)))
812 (let ((semantic-lex-types-obarray
813 (semantic-lex-make-type-table tokens props))
814 semantic-grammar--lex-block-specs)
815 (mapatoms 'semantic-grammar-insert-defanalyzer
816 semantic-lex-types-obarray))))
818 ;;; Generation of the grammar support file.
820 (defcustom semantic-grammar-file-regexp "\\.[wb]y\\'"
821 "Regexp which matches grammar source files."
825 (defsubst semantic-grammar-noninteractive ()
826 "Return non-nil if running without interactive terminal."
827 (if (featurep 'xemacs)
831 (defun semantic-grammar-create-package (&optional force uptodate)
832 "Create package Lisp code from grammar in current buffer.
833 If the Lisp code seems up to date, do nothing (if UPTODATE
834 is non-nil, return nil in such cases).
835 If optional argument FORCE is non-nil, unconditionally re-generate the
838 (unless (semantic-active-p)
839 (error "You have to activate semantic-mode to create a package."))
840 (setq force (or force current-prefix-arg))
841 (semantic-fetch-tags)
843 ;; Values of the following local variables are obtained from
844 ;; the grammar parsed tree in current buffer, that is before
845 ;; switching to the output file.
846 (semantic--grammar-package (semantic-grammar-package))
847 (semantic--grammar-provide (semantic-grammar-first-tag-name 'provide))
848 (output (concat (or semantic--grammar-provide
849 semantic--grammar-package) ".el"))
850 (semantic--grammar-input-buffer (current-buffer))
851 (semantic--grammar-output-buffer
853 (file-name-nondirectory output)))
854 (header (semantic-grammar-header))
855 (prologue (semantic-grammar-prologue))
856 (epilogue (semantic-grammar-epilogue))
857 (footer (semantic-grammar-footer))
860 (not (buffer-modified-p))
861 (file-newer-than-file-p
862 (buffer-file-name semantic--grammar-output-buffer)
863 (buffer-file-name semantic--grammar-input-buffer)))
865 (message "Package `%s' is up to date." semantic--grammar-package)
866 ;; It would be better if this were always the case, IMO,
867 ;; but the (unspecified) return value of this function is
868 ;; assumed to be non-nil in some places, it seems.
869 (if uptodate (setq output nil)))
870 ;; Create the package
871 (set-buffer semantic--grammar-output-buffer)
872 ;; Use Unix EOLs, so that the file is portable to all platforms.
873 (setq buffer-file-coding-system 'raw-text-unix)
875 (unless (derived-mode-p 'emacs-lisp-mode)
878 ;;;; Header + Prologue
881 "
\f\n;;; Prologue\n;;\n"
884 ;; Evaluate the prologue now, because it might provide definition
885 ;; of grammar macro expanders.
886 (eval-region (point-min) (point))
892 (insert "
\f\n;;; Declarations\n;;\n")
894 ;; `eval-defun' is not necessary to reset `defconst' values.
895 (semantic-grammar-insert-defconst
896 (semantic-grammar-keywordtable)
897 (with-current-buffer semantic--grammar-input-buffer
898 (semantic-grammar-keyword-data))
899 "Table of language keywords.")
901 (semantic-grammar-insert-defconst
902 (semantic-grammar-tokentable)
903 (with-current-buffer semantic--grammar-input-buffer
904 (semantic-grammar-token-data))
905 "Table of lexical tokens.")
907 (semantic-grammar-insert-defconst
908 (semantic-grammar-parsetable)
909 (with-current-buffer semantic--grammar-input-buffer
910 (semantic-grammar-parser-data))
913 (semantic-grammar-insert-defun
914 (semantic-grammar-setupfunction)
915 (with-current-buffer semantic--grammar-input-buffer
916 (semantic-grammar-setup-data))
917 "Setup the Semantic Parser.")
920 (insert "
\f\n;;; Analyzers\n;;\n")
922 (semantic-grammar-insert-defanalyzers)
924 ;;;; Epilogue & Footer
926 (insert "
\f\n;;; Epilogue\n;;\n"
935 ;; If running in batch mode, there is nothing more to do.
936 ;; Save the generated file and quit.
937 (if (semantic-grammar-noninteractive)
938 (let ((version-control t)
939 (delete-old-versions t)
940 (make-backup-files t)
941 (vc-make-backup-files t))
942 (kill-buffer (current-buffer)))
943 ;; If running interactively, eval declarations and epilogue
944 ;; code, then pop to the buffer visiting the generated file.
945 (eval-region (point) (point-max))
946 ;; Loop over the defvars and eval them explicitly to force
947 ;; them to be evaluated and ready to use.
948 (goto-char (point-min))
949 (while (re-search-forward "(defvar " nil t)
951 ;; Move cursor to a logical spot in the generated code.
952 (goto-char (point-min))
953 (pop-to-buffer (current-buffer))
954 ;; The generated code has been evaluated and updated into
955 ;; memory. Now find all buffers that match the major modes we
956 ;; have created this language for, and force them to call our
957 ;; setup function again, refreshing all semantic data, and
958 ;; enabling them to work with the new code just created.
960 ;; At this point, I don't know any user's defined setup code :-(
961 ;; At least, what I can do for now, is to run the generated
962 ;; parser-install function.
963 (semantic-map-mode-buffers
964 (semantic-grammar-setupfunction)
965 (semantic-grammar-languagemode)))
967 ;; Return the name of the generated package file.
970 (defun semantic-grammar-recreate-package ()
971 "Unconditionally create Lisp code from grammar in current buffer.
972 Like \\[universal-argument] \\[semantic-grammar-create-package]."
974 (semantic-grammar-create-package t))
976 (defun semantic-grammar-batch-build-one-package (file)
977 "Build a Lisp package from the grammar in FILE.
978 That is, generate Lisp code from FILE, and `byte-compile' it.
979 Return non-nil if there were no errors, nil if errors."
980 ;; We need this require so that we can find `byte-compile-dest-file'.
982 (unless (auto-save-file-name-p file)
983 ;; Create the package
986 (with-current-buffer (find-file-noselect file)
987 (let ((semantic-new-buffer-setup-functions nil)
988 (vc-handled-backends nil))
989 (setq semanticdb-new-database-class 'semanticdb-project-database)
991 (semantic-grammar-create-package)))
993 (message "%s" (error-message-string err))
996 ;; Only byte compile if out of date
997 (if (file-newer-than-file-p
998 packagename (byte-compile-dest-file packagename))
999 (let (;; Some complex grammar table expressions need a few
1000 ;; more resources than the default.
1001 (max-specpdl-size (max 3000 max-specpdl-size))
1002 (max-lisp-eval-depth (max 1000 max-lisp-eval-depth))
1004 ;; byte compile the resultant file
1005 (byte-compile-file packagename))
1008 (defun semantic-grammar-batch-build-packages ()
1009 "Build Lisp packages from grammar files on the command line.
1010 That is, run `semantic-grammar-batch-build-one-package' for each file.
1011 Each file is processed even if an error occurred previously.
1012 Must be used from the command line, with `-batch'.
1013 For example, to process grammar files in current directory, invoke:
1015 \"emacs -batch -f semantic-grammar-batch-build-packages .\".
1017 See also the variable `semantic-grammar-file-regexp'."
1018 (or (semantic-grammar-noninteractive)
1020 `semantic-grammar-batch-build-packages' must be used with -batch"
1023 ;; Remove vc from find-file-hook. It causes bad stuff to
1024 ;; happen in Emacs 20.
1025 (find-file-hook (delete 'vc-find-file-hook find-file-hook)))
1026 (dolist (arg command-line-args-left)
1027 (unless (and arg (file-exists-p arg))
1028 (error "Argument %s is not a valid file name" arg))
1029 (setq arg (expand-file-name arg))
1030 (if (file-directory-p arg)
1031 ;; Directory as argument
1032 (dolist (src (condition-case nil
1034 arg nil semantic-grammar-file-regexp)
1036 (error "Unable to read directory files"))))
1037 (or (semantic-grammar-batch-build-one-package
1038 (expand-file-name src arg))
1040 ;; Specific file argument
1041 (or (semantic-grammar-batch-build-one-package arg)
1047 ;;;; Macros highlighting
1050 (defvar semantic--grammar-macros-regexp-1 nil)
1051 (make-variable-buffer-local 'semantic--grammar-macros-regexp-1)
1053 (defun semantic--grammar-macros-regexp-1 ()
1054 "Return font-lock keyword regexp for pre-installed macro names."
1055 (and semantic-grammar-macros
1056 (not semantic--grammar-macros-regexp-1)
1058 (setq semantic--grammar-macros-regexp-1
1061 (mapcar #'(lambda (e) (symbol-name (car e)))
1062 semantic-grammar-macros)
1066 semantic--grammar-macros-regexp-1)
1068 (defconst semantic--grammar-macdecl-re
1069 "\\<%use-macros\\>[ \t\r\n]+\\(\\sw\\|\\s_\\)+[ \t\r\n]+{"
1070 "Regexp that matches a macro declaration statement.")
1072 (defvar semantic--grammar-macros-regexp-2 nil)
1073 (make-variable-buffer-local 'semantic--grammar-macros-regexp-2)
1075 (defun semantic--grammar-clear-macros-regexp-2 (&rest _)
1076 "Clear the cached regexp that match macros local in this grammar.
1078 Added to `before-change-functions' hooks to be run before each text
1080 (setq semantic--grammar-macros-regexp-2 nil))
1082 (defun semantic--grammar-macros-regexp-2 ()
1083 "Return the regexp that match macros local in this grammar."
1084 (unless semantic--grammar-macros-regexp-2
1087 (goto-char (point-min))
1088 (while (re-search-forward semantic--grammar-macdecl-re nil t)
1090 (setq macs (nconc macs
1092 (buffer-substring-no-properties
1101 (setq semantic--grammar-macros-regexp-2
1102 (concat "(\\s-*" (regexp-opt macs t) "\\>"))))))
1103 semantic--grammar-macros-regexp-2)
1105 (defun semantic--grammar-macros-matcher (end)
1106 "Search for a grammar macro name to highlight.
1107 END is the limit of the search."
1108 (let ((regexp (semantic--grammar-macros-regexp-1)))
1109 (or (and regexp (re-search-forward regexp end t))
1110 (and (setq regexp (semantic--grammar-macros-regexp-2))
1111 (re-search-forward regexp end t)))))
1114 ;;;; Define major mode
1117 (define-obsolete-variable-alias 'semantic-grammar-syntax-table
1118 'semantic-grammar-mode-syntax-table "24.1")
1119 (defvar semantic-grammar-mode-syntax-table
1120 (let ((table (make-syntax-table (standard-syntax-table))))
1121 (modify-syntax-entry ?\: "." table) ;; COLON
1122 (modify-syntax-entry ?\> "." table) ;; GT
1123 (modify-syntax-entry ?\< "." table) ;; LT
1124 (modify-syntax-entry ?\| "." table) ;; OR
1125 (modify-syntax-entry ?\; ". 12" table) ;; SEMI, Comment start ;;
1126 (modify-syntax-entry ?\n ">" table) ;; Comment end
1127 (modify-syntax-entry ?\" "\"" table) ;; String
1128 (modify-syntax-entry ?\% "w" table) ;; Word
1129 (modify-syntax-entry ?\- "_" table) ;; Symbol
1130 (modify-syntax-entry ?\. "_" table) ;; Symbol
1131 (modify-syntax-entry ?\\ "\\" table) ;; Quote
1132 (modify-syntax-entry ?\` "'" table) ;; Prefix ` (backquote)
1133 (modify-syntax-entry ?\' "'" table) ;; Prefix ' (quote)
1134 (modify-syntax-entry ?\, "'" table) ;; Prefix , (comma)
1135 (modify-syntax-entry ?\# "'" table) ;; Prefix # (sharp)
1137 "Syntax table used in a Semantic grammar buffers.")
1139 (defvar semantic-grammar-mode-hook nil
1140 "Hook run when starting Semantic grammar mode.")
1142 (defvar semantic-grammar-mode-keywords-1
1143 `(("\\(\\<%%\\>\\|\\<%[{}]\\)"
1144 0 font-lock-reference-face)
1145 ("\\(%\\)\\(\\(\\sw\\|\\s_\\)+\\)"
1146 (1 font-lock-reference-face)
1147 (2 font-lock-keyword-face))
1149 0 (unless (semantic-grammar-in-lisp-p) 'bold))
1150 ("^\\(\\(\\sw\\|\\s_\\)+\\)[ \n\r\t]*:"
1151 1 font-lock-function-name-face)
1152 (semantic--grammar-macros-matcher
1153 1 ,(if (boundp 'font-lock-builtin-face)
1154 'font-lock-builtin-face
1155 'font-lock-preprocessor-face))
1156 ("\\$\\(\\sw\\|\\s_\\)*"
1157 0 font-lock-variable-name-face)
1158 ("<\\(\\(\\sw\\|\\s_\\)+\\)>"
1159 1 font-lock-type-face)
1160 (,semantic-grammar-lex-c-char-re
1161 0 ,(if (boundp 'font-lock-constant-face)
1162 'font-lock-constant-face
1163 'font-lock-string-face) t)
1164 ;; Must highlight :keyword here, because ':' is a punctuation in
1166 ("[\r\n\t ]+:\\sw+\\>"
1167 0 font-lock-builtin-face)
1168 ;; ;; Append the Semantic keywords
1169 ;; ,@semantic-fw-font-lock-keywords
1171 "Font Lock keywords used to highlight Semantic grammar buffers.")
1173 (defvar semantic-grammar-mode-keywords-2
1174 (append semantic-grammar-mode-keywords-1
1175 (if (boundp 'lisp-font-lock-keywords-1)
1176 lisp-font-lock-keywords-1
1177 lisp-el-font-lock-keywords-1))
1178 "Font Lock keywords used to highlight Semantic grammar buffers.")
1180 (defvar semantic-grammar-mode-keywords-3
1181 (append semantic-grammar-mode-keywords-1
1182 (if (boundp 'lisp-font-lock-keywords-2)
1183 lisp-font-lock-keywords-2
1184 lisp-el-font-lock-keywords-2))
1185 "Font Lock keywords used to highlight Semantic grammar buffers.")
1187 (defvar semantic-grammar-mode-keywords
1188 semantic-grammar-mode-keywords-1
1189 "Font Lock keywords used to highlight Semantic grammar buffers.")
1191 (define-obsolete-variable-alias 'semantic-grammar-map
1192 'semantic-grammar-mode-map "24.1")
1193 (defvar semantic-grammar-mode-map
1194 (let ((km (make-sparse-keymap)))
1196 (define-key km "|" 'semantic-grammar-electric-punctuation)
1197 (define-key km ";" 'semantic-grammar-electric-punctuation)
1198 (define-key km "%" 'semantic-grammar-electric-punctuation)
1199 (define-key km "(" 'semantic-grammar-electric-punctuation)
1200 (define-key km ")" 'semantic-grammar-electric-punctuation)
1201 (define-key km ":" 'semantic-grammar-electric-punctuation)
1203 (define-key km "\t" 'semantic-grammar-indent)
1204 (define-key km "\M-\t" 'semantic-grammar-complete)
1205 (define-key km "\C-c\C-c" 'semantic-grammar-create-package)
1206 (define-key km "\C-cm" 'semantic-grammar-find-macro-expander)
1207 (define-key km "\C-cik" 'semantic-grammar-insert-keyword)
1208 ;; (define-key km "\C-cc" 'semantic-grammar-generate-and-load)
1209 ;; (define-key km "\C-cr" 'semantic-grammar-generate-one-rule)
1212 "Keymap used in `semantic-grammar-mode'.")
1214 (defvar semantic-grammar-menu
1216 ["Indent Line" semantic-grammar-indent]
1217 ["Complete Symbol" semantic-grammar-complete]
1218 ["Find Macro" semantic-grammar-find-macro-expander]
1220 ["Insert %keyword" semantic-grammar-insert-keyword]
1222 ["Update Lisp Package" semantic-grammar-create-package]
1223 ["Recreate Lisp Package" semantic-grammar-recreate-package]
1225 "Common semantic grammar menu.")
1227 (defun semantic-grammar-setup-menu-emacs (symbol mode-menu)
1228 "Setup a GNU Emacs grammar menu in variable SYMBOL.
1229 MODE-MENU is an optional specific menu whose items are appended to the
1230 common grammar menu."
1231 (let ((items (make-symbol "items")))
1232 `(unless (boundp ',symbol)
1233 (easy-menu-define ,symbol (current-local-map)
1234 "Grammar Menu" semantic-grammar-menu)
1235 (let ((,items (cdr ,mode-menu)))
1237 (easy-menu-add-item ,symbol nil "--")
1239 (easy-menu-add-item ,symbol nil (car ,items))
1240 (setq ,items (cdr ,items))))))
1243 (defun semantic-grammar-setup-menu-xemacs (symbol mode-menu)
1244 "Setup an XEmacs grammar menu in variable SYMBOL.
1245 MODE-MENU is an optional specific menu whose items are appended to the
1246 common grammar menu."
1247 (let ((items (make-symbol "items"))
1248 (path (make-symbol "path")))
1250 (unless (boundp ',symbol)
1251 (easy-menu-define ,symbol nil
1252 "Grammar Menu" (copy-sequence semantic-grammar-menu)))
1253 (easy-menu-add ,symbol)
1254 (let ((,items (cdr ,mode-menu))
1255 (,path (list (car ,symbol))))
1257 (easy-menu-add-item nil ,path "--")
1259 (easy-menu-add-item nil ,path (car ,items))
1260 (setq ,items (cdr ,items))))))
1263 (defmacro semantic-grammar-setup-menu (&optional mode-menu)
1264 "Setup a mode local grammar menu.
1265 MODE-MENU is an optional specific menu whose items are appended to the
1266 common grammar menu."
1267 (let ((menu (intern (format "%s-menu" major-mode))))
1268 (if (featurep 'xemacs)
1269 (semantic-grammar-setup-menu-xemacs menu mode-menu)
1270 (semantic-grammar-setup-menu-emacs menu mode-menu))))
1272 (defsubst semantic-grammar-in-lisp-p ()
1273 "Return non-nil if point is in Lisp code."
1274 (or (>= (point) (semantic-grammar-epilogue-start))
1281 (defun semantic-grammar-edits-new-change-hook-fcn (overlay)
1282 "Function set into `semantic-edits-new-change-hook'.
1283 Argument OVERLAY is the overlay created to mark the change.
1284 When OVERLAY marks a change in the scope of a nonterminal tag extend
1285 the change bounds to encompass the whole nonterminal tag."
1286 (let ((outer (car (semantic-find-tag-by-overlay-in-region
1287 (semantic-edits-os overlay)
1288 (semantic-edits-oe overlay)))))
1289 (if (semantic-tag-of-class-p outer 'nonterminal)
1290 (semantic-overlay-move overlay
1291 (semantic-tag-start outer)
1292 (semantic-tag-end outer)))))
1294 (define-derived-mode semantic-grammar-mode
1295 fundamental-mode "Semantic Grammar Framework"
1296 "Initialize a buffer for editing Semantic grammars.
1298 \\{semantic-grammar-mode-map}"
1299 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1300 (set (make-local-variable 'comment-start) ";;")
1301 ;; Look within the line for a ; following an even number of backslashes
1302 ;; after either a non-backslash or the line beginning.
1303 (set (make-local-variable 'comment-start-skip)
1304 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
1305 (set (make-local-variable 'indent-line-function)
1306 'semantic-grammar-indent)
1307 (set (make-local-variable 'fill-paragraph-function)
1308 'lisp-fill-paragraph)
1309 (set (make-local-variable 'font-lock-multiline)
1311 (set (make-local-variable 'font-lock-defaults)
1312 '((semantic-grammar-mode-keywords
1313 semantic-grammar-mode-keywords-1
1314 semantic-grammar-mode-keywords-2
1315 semantic-grammar-mode-keywords-3)
1316 nil ;; perform string/comment fontification
1317 nil ;; keywords are case sensitive.
1318 ;; This puts _ & - as a word constituent,
1319 ;; simplifying our keywords significantly
1320 ((?_ . "w") (?- . "w"))))
1321 ;; Setup Semantic to parse grammar
1322 (semantic-grammar-wy--install-parser)
1323 (setq semantic-lex-comment-regex ";;"
1324 semantic-lex-analyzer 'semantic-grammar-lexer
1325 semantic-type-relation-separator-character '(":")
1326 semantic-symbol->name-assoc-list
1328 (code . "Setup Code")
1329 (keyword . "Keyword")
1331 (nonterminal . "Nonterminal")
1334 (set (make-local-variable 'semantic-format-face-alist)
1337 (keyword . font-lock-keyword-face)
1338 (token . font-lock-type-face)
1339 (nonterminal . font-lock-function-name-face)
1342 (set (make-local-variable 'semantic-stickyfunc-sticky-classes)
1344 ;; Before each change, clear the cached regexp used to highlight
1345 ;; macros local in this grammar.
1346 (semantic-make-local-hook 'before-change-functions)
1347 (add-hook 'before-change-functions
1348 'semantic--grammar-clear-macros-regexp-2 nil t)
1349 ;; Handle safe re-parse of grammar rules.
1350 (semantic-make-local-hook 'semantic-edits-new-change-functions)
1351 (add-hook 'semantic-edits-new-change-functions
1352 'semantic-grammar-edits-new-change-hook-fcn
1356 ;;;; Useful commands
1359 (defvar semantic-grammar-skip-quoted-syntax-table
1360 (let ((st (copy-syntax-table semantic-grammar-mode-syntax-table)))
1361 (modify-syntax-entry ?\' "$" st)
1363 "Syntax table to skip a whole quoted expression in grammar code.
1364 Consider quote as a \"paired delimiter\", so `forward-sexp' will skip
1365 whole quoted expression.")
1367 (defsubst semantic-grammar-backward-item ()
1368 "Move point to beginning of the previous grammar item."
1369 (forward-comment (- (point-max)))
1370 (if (zerop (skip-syntax-backward "."))
1371 (if (eq (char-before) ?\')
1373 ;; Can't be Lisp code here! Temporarily consider quote
1374 ;; as a "paired delimiter", so `forward-sexp' can skip
1375 ;; the whole quoted expression.
1376 semantic-grammar-skip-quoted-syntax-table
1378 (forward-sexp -1))))
1380 (defun semantic-grammar-anchored-indentation ()
1381 "Return indentation based on previous anchor character found."
1385 (semantic-grammar-backward-item)
1389 ((looking-at ":\\(\\s-\\|$\\)")
1390 (setq indent (current-column))
1392 (skip-syntax-forward "-")
1393 (if (eolp) (setq indent 2))
1395 ((and (looking-at "[;%]")
1396 (not (looking-at "\\<%prec\\>")))
1401 (defun semantic-grammar-do-grammar-indent ()
1402 "Indent a line of grammar.
1403 When called the point is not in Lisp code."
1407 (skip-syntax-forward "-")
1408 (setq indent (current-column))
1411 (looking-at "\\(\\w\\|\\s_\\)+\\s-*:")
1412 (and (looking-at "%")
1413 (not (looking-at "%prec\\>"))))
1417 ((and (looking-at ";;")
1418 (save-excursion (forward-comment (point-max))
1422 (setq n (semantic-grammar-anchored-indentation))
1427 ((looking-at "[|;]")
1430 (setq n (+ n 2)))))))
1433 (delete-horizontal-space)
1436 (defvar semantic-grammar-brackets-as-parens-syntax-table
1437 (let ((st (copy-syntax-table emacs-lisp-mode-syntax-table)))
1438 (modify-syntax-entry ?\{ "(} " st)
1439 (modify-syntax-entry ?\} "){ " st)
1441 "Syntax table that consider brackets as parenthesis.
1442 So `lisp-indent-line' will work inside bracket blocks.")
1444 (defun semantic-grammar-do-lisp-indent ()
1445 "Maybe run the Emacs Lisp indenter on a line of code.
1446 Return nil if not in a Lisp expression."
1450 (skip-chars-forward "\t ")
1451 (let ((first (point)))
1452 (or (>= first (semantic-grammar-epilogue-start))
1460 (narrow-to-region (point) first)
1461 (goto-char (point-max))
1463 ;; Temporarily consider brackets as parenthesis so
1464 ;; `lisp-indent-line' can indent Lisp code inside
1466 semantic-grammar-brackets-as-parens-syntax-table
1467 (lisp-indent-line))))
1471 (defun semantic-grammar-indent ()
1472 "Indent the current line.
1473 Use the Lisp or grammar indenter depending on point location."
1475 (let ((orig (point))
1477 (or (semantic-grammar-do-lisp-indent)
1478 (semantic-grammar-do-grammar-indent))
1479 (setq first (save-excursion
1481 (skip-chars-forward "\t ")
1483 (if (or (< orig first) (/= orig (point)))
1484 (goto-char first))))
1486 (defun semantic-grammar-electric-punctuation ()
1487 "Insert and reindent for the symbol just typed in."
1489 (self-insert-command 1)
1491 (semantic-grammar-indent)))
1493 (defun semantic-grammar-complete ()
1494 "Attempt to complete the symbol under point.
1495 Completion is position sensitive. If the cursor is in a match section of
1496 a rule, then nonterminals symbols are scanned. If the cursor is in a Lisp
1497 expression then Lisp symbols are completed."
1499 (if (semantic-grammar-in-lisp-p)
1500 ;; We are in lisp code. Do lisp completion.
1501 (let ((completion-at-point-functions
1502 (append '(lisp-completion-at-point)
1503 completion-at-point-functions)))
1504 (completion-at-point))
1505 ;; We are not in lisp code. Do rule completion.
1506 (let* ((nonterms (semantic-find-tags-by-class 'nonterminal (current-buffer)))
1507 (sym (car (semantic-ctxt-current-symbol)))
1508 (ans (try-completion sym nonterms)))
1511 (message "Symbols is already complete"))
1512 ((and (stringp ans) (string= ans sym))
1513 ;; Max matchable. Show completions.
1514 (with-output-to-temp-buffer "*Completions*"
1515 (display-completion-list (all-completions sym nonterms)))
1518 ;; Expand the completions
1520 (delete-region (point) (progn (forward-sexp 1) (point)))
1522 (t (message "No Completions."))
1526 (defun semantic-grammar-insert-keyword (name)
1527 "Insert a new %keyword declaration with NAME.
1528 Assumes it is typed in with the correct casing."
1529 (interactive "sKeyword: ")
1530 (if (not (bolp)) (insert "\n"))
1531 (insert "%keyword " (upcase name) " \"" name "\"
1532 %put " (upcase name) " summary
1536 ;;; Macro facilities
1539 (defsubst semantic--grammar-macro-function-tag (name)
1540 "Search for a function tag for the grammar macro with name NAME.
1541 Return the tag found or nil if not found."
1542 (car (semantic-find-tags-by-class
1544 (or (semantic-find-tags-by-name name (current-buffer))
1545 (and (featurep 'semantic/db)
1546 semanticdb-current-database
1547 (cdar (semanticdb-find-tags-by-name name nil t)))))))
1549 (defsubst semantic--grammar-macro-lib-part (def)
1550 "Return the library part of the grammar macro defined by DEF."
1551 (let ((suf (format "-%s\\'" (regexp-quote (symbol-name (car def)))))
1552 (fun (symbol-name (cdr def))))
1553 (substring fun 0 (string-match suf fun))))
1555 (defun semantic--grammar-macro-compl-elt (def &optional full)
1556 "Return a completion entry for the grammar macro defined by DEF.
1557 If optional argument FULL is non-nil qualify the macro name with the
1558 library found in DEF."
1559 (let ((mac (car def))
1560 (lib (semantic--grammar-macro-lib-part def)))
1562 (format "%s/%s" mac lib)
1566 (defun semantic--grammar-macro-compl-dict ()
1567 "Return a completion dictionary of macro definitions."
1568 (let ((defs (semantic-grammar-macros))
1571 (setq def (car defs)
1573 (if (or (assoc (car def) defs) (assoc (car def) dups))
1575 (push (semantic--grammar-macro-compl-elt def) dict)))
1577 (setq def (car dups)
1579 (push (semantic--grammar-macro-compl-elt def t) dict))
1582 (defun semantic-grammar-find-macro-expander (macro-name library)
1583 "Visit the Emacs Lisp library where a grammar macro is implemented.
1584 MACRO-NAME is a symbol that identifies a grammar macro.
1585 LIBRARY is the name (sans extension) of the Emacs Lisp library where
1586 to start searching the macro implementation. Lookup in included
1587 libraries, if necessary.
1588 Find a function tag (in current tags table) whose name contains MACRO-NAME.
1589 Select the buffer containing the tag's definition, and move point there."
1591 (let* ((dic (semantic--grammar-macro-compl-dict))
1592 (def (assoc (completing-read "Macro: " dic nil 1) dic)))
1593 (or (cdr def) '(nil nil))))
1594 (when (and macro-name library)
1595 (let* ((lib (format "%s.el" library))
1596 (buf (find-file-noselect (or (locate-library lib t) lib)))
1597 (tag (with-current-buffer buf
1598 (semantic--grammar-macro-function-tag
1599 (format "%s-%s" library macro-name)))))
1602 (require 'semantic/decorate)
1603 (pop-to-buffer (semantic-tag-buffer tag))
1604 (goto-char (semantic-tag-start tag))
1605 (semantic-momentary-highlight-tag tag))
1607 (message "No expander found in library %s for macro %s"
1608 library macro-name)))))
1613 (defvar semantic-grammar-syntax-help
1616 ("symbol" . "Syntax: A symbol of alpha numeric and symbol characters")
1617 ("number" . "Syntax: Numeric characters.")
1618 ("punctuation" . "Syntax: Punctuation character.")
1619 ("semantic-list" . "Syntax: A list delimited by any valid list characters")
1620 ("open-paren" . "Syntax: Open Parenthesis character")
1621 ("close-paren" . "Syntax: Close Parenthesis character")
1622 ("string" . "Syntax: String character delimited text")
1623 ("comment" . "Syntax: Comment character delimited text")
1625 ("EMPTY" . "Syntax: Match empty text")
1626 ("ASSOC" . "Lambda Key: (ASSOC key1 value1 key2 value2 ...)")
1627 ("EXPAND" . "Lambda Key: (EXPAND <list id> <rule>)")
1628 ("EXPANDFULL" . "Lambda Key: (EXPANDFULL <list id> <rule>)")
1629 ;; Tag Generator Macros
1630 ("TAG" . "Generic Tag Generation: (TAG <name> <tag-class> [ :key value ]*)")
1631 ("VARIABLE-TAG" . "(VARIABLE-TAG <name> <lang-type> <default-value> [ :key value ]*)")
1632 ("FUNCTION-TAG" . "(FUNCTION-TAG <name> <lang-type> <arg-list> [ :key value ]*)")
1633 ("TYPE-TAG" . "(TYPE-TAG <name> <lang-type> <part-list> <parents> [ :key value ]*)")
1634 ("INCLUDE-TAG" . "(INCLUDE-TAG <name> <system-flag> [ :key value ]*)")
1635 ("PACKAGE-TAG" . "(PACKAGE-TAG <name> <detail> [ :key value ]*)")
1636 ("CODE-TAG" . "(CODE-TAG <name> <detail> [ :key value ]*)")
1637 ("ALIAS-TAG" . "(ALIAS-TAG <name> <aliasclass> <definition> [:key value]*)")
1638 ;; Special value macros
1639 ("$1" . "Match Value: Value from match list in slot 1")
1640 ("$2" . "Match Value: Value from match list in slot 2")
1641 ("$3" . "Match Value: Value from match list in slot 3")
1642 ("$4" . "Match Value: Value from match list in slot 4")
1643 ("$5" . "Match Value: Value from match list in slot 5")
1644 ("$6" . "Match Value: Value from match list in slot 6")
1645 ("$7" . "Match Value: Value from match list in slot 7")
1646 ("$8" . "Match Value: Value from match list in slot 8")
1647 ("$9" . "Match Value: Value from match list in slot 9")
1648 ;; Same, but with annoying , in front.
1649 (",$1" . "Match Value: Value from match list in slot 1")
1650 (",$2" . "Match Value: Value from match list in slot 2")
1651 (",$3" . "Match Value: Value from match list in slot 3")
1652 (",$4" . "Match Value: Value from match list in slot 4")
1653 (",$5" . "Match Value: Value from match list in slot 5")
1654 (",$6" . "Match Value: Value from match list in slot 6")
1655 (",$7" . "Match Value: Value from match list in slot 7")
1656 (",$8" . "Match Value: Value from match list in slot 8")
1657 (",$9" . "Match Value: Value from match list in slot 9")
1659 "Association of syntax elements, and the corresponding help.")
1661 (defvar semantic-grammar-eldoc-last-data (cons nil nil))
1663 (defun semantic-grammar-eldoc-get-macro-docstring (macro expander)
1664 "Return a one-line docstring for the given grammar MACRO.
1665 EXPANDER is the name of the function that expands MACRO."
1668 ((eq expander (car semantic-grammar-eldoc-last-data))
1669 (cdr semantic-grammar-eldoc-last-data))
1670 ((fboundp 'eldoc-function-argstring) ;; Emacs<25
1671 (let* ((doc (help-split-fundoc (documentation expander t) expander)))
1674 (setq doc (car doc))
1675 (string-match "\\`[^ )]* ?" doc)
1676 (setq doc (concat "(" (substring doc (match-end 0)))))
1678 (setq doc (eldoc-function-argstring expander))))
1681 (eldoc-docstring-format-sym-doc
1682 macro (format "==> %s %s" expander doc) 'default))
1683 (setq semantic-grammar-eldoc-last-data (cons expander doc)))
1685 ((fboundp 'elisp-get-fnsym-args-string) ;; Emacsā„25
1686 (elisp-get-fnsym-args-string
1688 (concat (propertize (symbol-name macro)
1689 'face 'font-lock-keyword-face)
1691 (propertize (symbol-name macro)
1692 'face 'font-lock-function-name-face)
1695 (define-mode-local-override semantic-idle-summary-current-symbol-info
1696 semantic-grammar-mode ()
1697 "Display additional eldoc information about grammar syntax elements.
1698 Syntax element is the current symbol at point.
1699 If it is associated a help string in `semantic-grammar-syntax-help',
1701 If it is a macro name, return a description of the associated expander
1702 function parameter list.
1703 If it is a function name, return a description of this function
1705 It it is a variable name, return a brief (one-line) documentation
1706 string for the variable.
1707 If a default description of the current context can be obtained,
1709 Otherwise return nil."
1711 (let* ((elt (car (semantic-ctxt-current-symbol)))
1712 (val (and elt (cdr (assoc elt semantic-grammar-syntax-help)))))
1713 (when (and (not val) elt (semantic-grammar-in-lisp-p))
1714 ;; Ensure to load macro definitions before doing `intern-soft'.
1715 (setq val (semantic-grammar-macros)
1716 elt (intern-soft elt)
1717 val (and elt (cdr (assq elt val))))
1720 ((and val (fboundp val))
1721 (setq val (semantic-grammar-eldoc-get-macro-docstring elt val)))
1723 ((and elt (fboundp elt))
1724 (setq val (if (fboundp 'eldoc-get-fnsym-args-string)
1725 (eldoc-get-fnsym-args-string elt)
1726 (elisp-get-fnsym-args-string elt))))
1728 ((and elt (boundp elt))
1729 (setq val (if (fboundp 'eldoc-get-var-docstring)
1730 (eldoc-get-var-docstring elt)
1731 (elisp-get-var-docstring elt))))
1733 (or val (semantic-idle-summary-current-symbol-info-default))))
1735 (define-mode-local-override semantic-tag-boundary-p
1736 semantic-grammar-mode (tag)
1737 "Return non-nil for tags that should have a boundary drawn.
1738 Only tags of type 'nonterminal will be so marked."
1739 (let ((c (semantic-tag-class tag)))
1740 (eq c 'nonterminal)))
1742 (define-mode-local-override semantic-ctxt-current-function
1743 semantic-grammar-mode (&optional point)
1744 "Determine the name of the current function at POINT."
1746 (and point (goto-char point))
1747 (when (semantic-grammar-in-lisp-p)
1748 (with-mode-local emacs-lisp-mode
1749 (semantic-ctxt-current-function)))))
1751 (define-mode-local-override semantic-ctxt-current-argument
1752 semantic-grammar-mode (&optional point)
1753 "Determine the argument index of the called function at POINT."
1755 (and point (goto-char point))
1756 (when (semantic-grammar-in-lisp-p)
1757 (with-mode-local emacs-lisp-mode
1758 (semantic-ctxt-current-argument)))))
1760 (define-mode-local-override semantic-ctxt-current-assignment
1761 semantic-grammar-mode (&optional point)
1762 "Determine the tag being assigned into at POINT."
1764 (and point (goto-char point))
1765 (when (semantic-grammar-in-lisp-p)
1766 (with-mode-local emacs-lisp-mode
1767 (semantic-ctxt-current-assignment)))))
1769 (define-mode-local-override semantic-ctxt-current-class-list
1770 semantic-grammar-mode (&optional point)
1771 "Determine the class of tags that can be used at POINT."
1773 (and point (goto-char point))
1774 (if (semantic-grammar-in-lisp-p)
1775 (with-mode-local emacs-lisp-mode
1776 (semantic-ctxt-current-class-list))
1777 '(nonterminal keyword))))
1779 (define-mode-local-override semantic-ctxt-current-mode
1780 semantic-grammar-mode (&optional point)
1781 "Return the major mode active at POINT.
1782 POINT defaults to the value of point in current buffer.
1783 Return `emacs-lisp-mode' is POINT is within Lisp code, otherwise
1784 return the current major mode."
1786 (and point (goto-char point))
1787 (if (semantic-grammar-in-lisp-p)
1789 (semantic-ctxt-current-mode-default))))
1791 (define-mode-local-override semantic-format-tag-abbreviate
1792 semantic-grammar-mode (tag &optional parent color)
1793 "Return a string abbreviation of TAG.
1794 Optional PARENT is not used.
1795 Optional COLOR is used to flag if color is added to the text."
1796 (let ((class (semantic-tag-class tag))
1797 (name (semantic-format-tag-name tag parent color)))
1799 ((eq class 'nonterminal)
1801 ((eq class 'setting)
1803 ((memq class '(rule keyword))
1806 (concat "%" (symbol-name class) " " name)))))
1808 (define-mode-local-override semantic-format-tag-summarize
1809 semantic-grammar-mode (tag &optional parent color)
1810 "Return a string summarizing TAG.
1811 Optional PARENT is not used.
1812 Optional argument COLOR determines if color is added to the text."
1813 (let ((class (semantic-tag-class tag))
1814 (name (semantic-format-tag-name tag parent color))
1818 ((eq class 'nonterminal)
1819 (setq label "Nonterminal: "
1821 " with %d match lists."
1822 (length (semantic-tag-components tag)))))
1823 ((eq class 'keyword)
1824 (setq label "Keyword: ")
1826 (semantic--find-tags-by-function
1829 (setq summary (cdr (assoc "summary"
1830 (semantic-tag-get-attribute
1832 ;; Get `put' tag with TAG name.
1833 (semantic-find-tags-by-name-regexp
1834 (regexp-quote (semantic-tag-name tag))
1835 (semantic-find-tags-by-class 'put (current-buffer))))
1836 (setq desc (concat " = "
1837 (semantic-tag-get-attribute tag :value)
1839 (concat " - " (read summary))
1842 (setq label "Token: ")
1843 (let ((val (semantic-tag-get-attribute tag :value))
1844 (names (semantic-tag-get-attribute tag :rest))
1845 (type (semantic-tag-type tag)))
1847 (setq name (mapconcat 'identity (cons name names) " ")))
1850 (format " <%s>" type)
1853 (format "%s%S" val (if type " " ""))
1856 (setq label "Assoc: ")
1857 (let ((val (semantic-tag-get-attribute tag :value))
1858 (type (semantic-tag-type tag)))
1861 (format " <%s>" type)
1864 (concat " " (mapconcat 'identity val " "))
1867 (setq desc (semantic-format-tag-abbreviate tag parent color))))
1868 (if (and color label)
1869 (setq label (semantic--format-colorize-text label 'label)))
1870 (if (and color label desc)
1871 (setq desc (semantic--format-colorize-text desc 'comment)))
1873 (concat label name desc)
1874 ;; Just a description is the abbreviated version
1877 ;;; Semantic Analysis
1879 (define-mode-local-override semantic-analyze-current-context
1880 semantic-grammar-mode (point)
1881 "Provide a semantic analysis object describing a context in a grammar."
1882 (require 'semantic/analyze)
1883 (if (semantic-grammar-in-lisp-p)
1884 (with-mode-local emacs-lisp-mode
1885 (semantic-analyze-current-context point))
1887 (let* ((context-return nil)
1888 (prefixandbounds (semantic-ctxt-current-symbol-and-bounds))
1889 (prefix (car prefixandbounds))
1890 (bounds (nth 2 prefixandbounds))
1892 (prefixclass (semantic-ctxt-current-class-list))
1895 ;; Do context for rules when in a match list.
1897 (semantic-find-first-tag-by-name
1901 (setq context-return
1902 (semantic-analyze-context
1903 "context-for-semantic-grammar"
1904 :buffer (current-buffer)
1907 :prefix (if prefixsym
1911 :prefixclass prefixclass
1916 (define-mode-local-override semantic-analyze-possible-completions
1917 semantic-grammar-mode (context)
1918 "Return a list of possible completions based on CONTEXT."
1919 (require 'semantic/analyze/complete)
1920 (if (semantic-grammar-in-lisp-p)
1921 (with-mode-local emacs-lisp-mode
1922 (semantic-analyze-possible-completions context))
1923 (with-current-buffer (oref context buffer)
1924 (let* ((prefix (car (oref context :prefix)))
1925 (completetext (cond ((semantic-tag-p prefix)
1926 (semantic-tag-name prefix))
1929 ((stringp (car prefix))
1931 (tags (semantic-find-tags-for-completion completetext
1933 (semantic-analyze-tags-of-class-list
1934 tags (oref context prefixclass)))
1937 (provide 'semantic/grammar)
1941 ;; generated-autoload-load-name: "semantic/grammar"
1944 ;;; semantic/grammar.el ends here