1 ;;; semantic/grammar.el --- Major mode framework for Semantic grammars
3 ;; Copyright (C) 2002-2005, 2007-2014 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-header ()
632 "Return text of a generated standard header."
633 (let ((file (semantic-grammar-buffer-file
634 semantic--grammar-output-buffer
))
635 (gram (semantic-grammar-buffer-file))
636 (date (format-time-string "%Y-%m-%d %T%z"))
637 (vcid (concat "$" "Id" "$")) ;; Avoid expansion
638 ;; Try to get the copyright from the input grammar, or
639 ;; generate a new one if not found.
640 (copy (or (semantic-grammar-copyright-line)
641 (concat (format-time-string ";; Copyright (C) %Y ")
644 (dolist (S semantic-grammar-header-template
)
646 (setq out
(concat out S
)))
648 (setq out
(concat out
(symbol-value S
))))))
651 (defun semantic-grammar-footer ()
652 "Return text of a generated standard footer."
653 (let* ((file (semantic-grammar-buffer-file
654 semantic--grammar-output-buffer
))
655 (libr (or semantic--grammar-provide
656 semantic--grammar-package
))
658 (dolist (S semantic-grammar-footer-template
)
660 (setq out
(concat out S
)))
662 (setq out
(concat out
(symbol-value S
))))))
665 (defun semantic-grammar-token-data ()
666 "Return the string value of the table of lexical tokens."
667 (semantic-grammar-as-string
668 (semantic-grammar-tokentable-builder)))
670 (defun semantic-grammar-keyword-data ()
671 "Return the string value of the table of keywords."
672 (semantic-grammar-as-string
673 (semantic-grammar-keywordtable-builder)))
675 (defun semantic-grammar-parser-data ()
676 "Return the parser table as a string value."
677 (semantic-grammar-as-string
678 (semantic-grammar-parsetable-builder)))
680 (defun semantic-grammar-setup-data ()
681 "Return the parser setup code form as a string value."
682 (semantic-grammar-as-string
683 (semantic-grammar-setupcode-builder)))
685 ;;; Generation of lexical analyzers.
687 (defvar semantic-grammar--lex-block-specs
)
689 (defsubst semantic-grammar--lex-delim-spec
(block-spec)
690 "Return delimiters specification from BLOCK-SPEC."
692 (let* ((standard-input (cdr block-spec
))
694 (if (and (consp delim-spec
)
695 (car delim-spec
) (symbolp (car delim-spec
))
696 (cadr delim-spec
) (symbolp (cadr delim-spec
)))
698 (error "Invalid delimiter")))
700 (error "Invalid delimiters specification %s in block token %s"
701 (cdr block-spec
) (car block-spec
)))))
703 (defun semantic-grammar--lex-block-specs ()
704 "Compute lexical block specifications for the current buffer.
705 Block definitions are read from the current table of lexical types."
707 ;; Block specifications have been parsed and are invalid.
708 ((eq semantic-grammar--lex-block-specs
'error
)
711 ;; Parse block specifications.
712 ((null semantic-grammar--lex-block-specs
)
714 (let* ((blocks (cdr (semantic-lex-type-value "block" t
)))
715 (open-delims (cdr (semantic-lex-type-value "open-paren" t
)))
716 (close-delims (cdr (semantic-lex-type-value "close-paren" t
)))
717 olist clist block-spec delim-spec open-spec close-spec
)
718 (dolist (block-spec blocks
)
719 (setq delim-spec
(semantic-grammar--lex-delim-spec block-spec
)
720 open-spec
(assq (car delim-spec
) open-delims
)
721 close-spec
(assq (cadr delim-spec
) close-delims
))
723 (error "Missing open-paren token %s required by block %s"
724 (car delim-spec
) (car block-spec
)))
726 (error "Missing close-paren token %s required by block %s"
727 (cdr delim-spec
) (car block-spec
)))
728 ;; build alist ((OPEN-DELIM OPEN-SYM BLOCK-SYM) ...)
729 (push (list (cdr open-spec
) (car open-spec
) (car block-spec
))
731 ;; build alist ((CLOSE-DELIM CLOSE-SYM) ...)
732 (push (list (cdr close-spec
) (car close-spec
))
734 (setq semantic-grammar--lex-block-specs
(cons olist clist
)))
736 (setq semantic-grammar--lex-block-specs
'error
)
737 (message "%s" (error-message-string err
))
740 ;; Block specifications already parsed.
742 semantic-grammar--lex-block-specs
)))
744 (defsubst semantic-grammar-quoted-form
(exp)
745 "Return a quoted form of EXP if it isn't a self evaluating form."
746 (if (and (not (null exp
))
747 (or (listp exp
) (symbolp exp
)))
751 (defun semantic-grammar-insert-defanalyzer (type)
752 "Insert declaration of the lexical analyzer defined with TYPE."
753 (let* ((type-name (symbol-name type
))
754 (type-value (symbol-value type
))
755 (syntax (get type
'syntax
))
756 (declared (get type
:declared
))
757 spec mtype prefix name doc
)
758 ;; Generate an analyzer if the corresponding type has been
759 ;; explicitly declared in a %type statement, and if at least the
760 ;; syntax property has been provided.
761 (when (and declared syntax
)
762 (setq prefix semantic--grammar-package
763 mtype
(or (get type
'matchdatatype
) 'regexp
)
764 name
(intern (format "%s--<%s>-%s-analyzer" prefix type mtype
))
765 doc
(format "%s analyzer for <%s> tokens." mtype type
))
767 ;; Regexp match analyzer
769 (semantic-grammar-insert-define
770 `(define-lex-regex-type-analyzer ,name
772 ,(semantic-grammar-quoted-form (cdr type-value
))
773 ',(or (car type-value
) (intern type-name
))))
775 ;; String compare analyzer
777 (semantic-grammar-insert-define
778 `(define-lex-string-type-analyzer ,name
780 ,(semantic-grammar-quoted-form (cdr type-value
))
781 ',(or (car type-value
) (intern type-name
))))
784 ((and (eq mtype
'block
)
785 (setq spec
(semantic-grammar--lex-block-specs)))
786 (semantic-grammar-insert-define
787 `(define-lex-block-type-analyzer ,name
789 ,(semantic-grammar-quoted-form spec
)))
793 (semantic-grammar-insert-define
794 `(define-lex-sexp-type-analyzer ,name
796 ',(or (car type-value
) (intern type-name
))))
800 (semantic-grammar-insert-define
801 `(define-lex-keyword-type-analyzer ,name
807 (defun semantic-grammar-insert-defanalyzers ()
808 "Insert declarations of lexical analyzers."
810 (with-current-buffer semantic--grammar-input-buffer
811 (setq tokens
(semantic-grammar-tokens)
812 props
(semantic-grammar-token-properties tokens
)))
813 (let ((semantic-lex-types-obarray
814 (semantic-lex-make-type-table tokens props
))
815 semantic-grammar--lex-block-specs
)
816 (mapatoms 'semantic-grammar-insert-defanalyzer
817 semantic-lex-types-obarray
))))
819 ;;; Generation of the grammar support file.
821 (defcustom semantic-grammar-file-regexp
"\\.[wb]y$"
822 "Regexp which matches grammar source files."
826 (defsubst semantic-grammar-noninteractive
()
827 "Return non-nil if running without interactive terminal."
828 (if (featurep 'xemacs
)
832 (defun semantic-grammar-create-package (&optional force uptodate
)
833 "Create package Lisp code from grammar in current buffer.
834 If the Lisp code seems up to date, do nothing (if UPTODATE
835 is non-nil, return nil in such cases).
836 If optional argument FORCE is non-nil, unconditionally re-generate the
839 (unless (semantic-active-p)
840 (error "You have to activate semantic-mode to create a package."))
841 (setq force
(or force current-prefix-arg
))
842 (semantic-fetch-tags)
844 ;; Values of the following local variables are obtained from
845 ;; the grammar parsed tree in current buffer, that is before
846 ;; switching to the output file.
847 (semantic--grammar-package (semantic-grammar-package))
848 (semantic--grammar-provide (semantic-grammar-first-tag-name 'provide
))
849 (output (concat (or semantic--grammar-provide
850 semantic--grammar-package
) ".el"))
851 (semantic--grammar-input-buffer (current-buffer))
852 (semantic--grammar-output-buffer
854 (file-name-nondirectory output
)))
855 (header (semantic-grammar-header))
856 (prologue (semantic-grammar-prologue))
857 (epilogue (semantic-grammar-epilogue))
858 (footer (semantic-grammar-footer))
861 (not (buffer-modified-p))
862 (file-newer-than-file-p
863 (buffer-file-name semantic--grammar-output-buffer
)
864 (buffer-file-name semantic--grammar-input-buffer
)))
866 (message "Package `%s' is up to date." semantic--grammar-package
)
867 ;; It would be better if this were always the case, IMO,
868 ;; but the (unspecified) return value of this function is
869 ;; assumed to be non-nil in some places, it seems.
870 (if uptodate
(setq output nil
)))
871 ;; Create the package
872 (set-buffer semantic--grammar-output-buffer
)
873 ;; Use Unix EOLs, so that the file is portable to all platforms.
874 (setq buffer-file-coding-system
'raw-text-unix
)
876 (unless (derived-mode-p 'emacs-lisp-mode
)
879 ;;;; Header + Prologue
882 "\f\n;;; Prologue\n;;\n"
885 ;; Evaluate the prologue now, because it might provide definition
886 ;; of grammar macro expanders.
887 (eval-region (point-min) (point))
893 (insert "\f\n;;; Declarations\n;;\n")
895 ;; `eval-defun' is not necessary to reset `defconst' values.
896 (semantic-grammar-insert-defconst
897 (semantic-grammar-keywordtable)
898 (with-current-buffer semantic--grammar-input-buffer
899 (semantic-grammar-keyword-data))
900 "Table of language keywords.")
902 (semantic-grammar-insert-defconst
903 (semantic-grammar-tokentable)
904 (with-current-buffer semantic--grammar-input-buffer
905 (semantic-grammar-token-data))
906 "Table of lexical tokens.")
908 (semantic-grammar-insert-defconst
909 (semantic-grammar-parsetable)
910 (with-current-buffer semantic--grammar-input-buffer
911 (semantic-grammar-parser-data))
914 (semantic-grammar-insert-defun
915 (semantic-grammar-setupfunction)
916 (with-current-buffer semantic--grammar-input-buffer
917 (semantic-grammar-setup-data))
918 "Setup the Semantic Parser.")
921 (insert "\f\n;;; Analyzers\n;;\n")
923 (semantic-grammar-insert-defanalyzers)
925 ;;;; Epilogue & Footer
927 (insert "\f\n;;; Epilogue\n;;\n"
936 ;; If running in batch mode, there is nothing more to do.
937 ;; Save the generated file and quit.
938 (if (semantic-grammar-noninteractive)
939 (let ((version-control t
)
940 (delete-old-versions t
)
941 (make-backup-files t
)
942 (vc-make-backup-files t
))
943 (kill-buffer (current-buffer)))
944 ;; If running interactively, eval declarations and epilogue
945 ;; code, then pop to the buffer visiting the generated file.
946 (eval-region (point) (point-max))
947 ;; Loop over the defvars and eval them explicitly to force
948 ;; them to be evaluated and ready to use.
949 (goto-char (point-min))
950 (while (re-search-forward "(defvar " nil t
)
952 ;; Move cursor to a logical spot in the generated code.
953 (goto-char (point-min))
954 (pop-to-buffer (current-buffer))
955 ;; The generated code has been evaluated and updated into
956 ;; memory. Now find all buffers that match the major modes we
957 ;; have created this language for, and force them to call our
958 ;; setup function again, refreshing all semantic data, and
959 ;; enabling them to work with the new code just created.
961 ;; At this point, I don't know any user's defined setup code :-(
962 ;; At least, what I can do for now, is to run the generated
963 ;; parser-install function.
964 (semantic-map-mode-buffers
965 (semantic-grammar-setupfunction)
966 (semantic-grammar-languagemode)))
968 ;; Return the name of the generated package file.
971 (defun semantic-grammar-recreate-package ()
972 "Unconditionally create Lisp code from grammar in current buffer.
973 Like \\[universal-argument] \\[semantic-grammar-create-package]."
975 (semantic-grammar-create-package t
))
977 (defun semantic-grammar-batch-build-one-package (file)
978 "Build a Lisp package from the grammar in FILE.
979 That is, generate Lisp code from FILE, and `byte-compile' it.
980 Return non-nil if there were no errors, nil if errors."
981 ;; We need this require so that we can find `byte-compile-dest-file'.
983 (unless (auto-save-file-name-p file
)
984 ;; Create the package
987 (with-current-buffer (find-file-noselect file
)
988 (let ((semantic-new-buffer-setup-functions nil
)
989 (vc-handled-backends nil
))
990 (setq semanticdb-new-database-class
'semanticdb-project-database
)
992 (semantic-grammar-create-package)))
994 (message "%s" (error-message-string err
))
997 ;; Only byte compile if out of date
998 (if (file-newer-than-file-p
999 packagename
(byte-compile-dest-file packagename
))
1000 (let (;; Some complex grammar table expressions need a few
1001 ;; more resources than the default.
1002 (max-specpdl-size (max 3000 max-specpdl-size
))
1003 (max-lisp-eval-depth (max 1000 max-lisp-eval-depth
))
1005 ;; byte compile the resultant file
1006 (byte-compile-file packagename
))
1009 (defun semantic-grammar-batch-build-packages ()
1010 "Build Lisp packages from grammar files on the command line.
1011 That is, run `semantic-grammar-batch-build-one-package' for each file.
1012 Each file is processed even if an error occurred previously.
1013 Must be used from the command line, with `-batch'.
1014 For example, to process grammar files in current directory, invoke:
1016 \"emacs -batch -f semantic-grammar-batch-build-packages .\".
1018 See also the variable `semantic-grammar-file-regexp'."
1019 (or (semantic-grammar-noninteractive)
1021 `semantic-grammar-batch-build-packages' must be used with -batch"
1024 ;; Remove vc from find-file-hook. It causes bad stuff to
1025 ;; happen in Emacs 20.
1026 (find-file-hook (delete 'vc-find-file-hook find-file-hook
)))
1027 (dolist (arg command-line-args-left
)
1028 (unless (and arg
(file-exists-p arg
))
1029 (error "Argument %s is not a valid file name" arg
))
1030 (setq arg
(expand-file-name arg
))
1031 (if (file-directory-p arg
)
1032 ;; Directory as argument
1033 (dolist (src (condition-case nil
1035 arg nil semantic-grammar-file-regexp
)
1037 (error "Unable to read directory files"))))
1038 (or (semantic-grammar-batch-build-one-package
1039 (expand-file-name src arg
))
1041 ;; Specific file argument
1042 (or (semantic-grammar-batch-build-one-package arg
)
1048 ;;;; Macros highlighting
1051 (defvar semantic--grammar-macros-regexp-1 nil
)
1052 (make-variable-buffer-local 'semantic--grammar-macros-regexp-1
)
1054 (defun semantic--grammar-macros-regexp-1 ()
1055 "Return font-lock keyword regexp for pre-installed macro names."
1056 (and semantic-grammar-macros
1057 (not semantic--grammar-macros-regexp-1
)
1059 (setq semantic--grammar-macros-regexp-1
1062 (mapcar #'(lambda (e) (symbol-name (car e
)))
1063 semantic-grammar-macros
)
1067 semantic--grammar-macros-regexp-1
)
1069 (defconst semantic--grammar-macdecl-re
1070 "\\<%use-macros\\>[ \t\r\n]+\\(\\sw\\|\\s_\\)+[ \t\r\n]+{"
1071 "Regexp that matches a macro declaration statement.")
1073 (defvar semantic--grammar-macros-regexp-2 nil
)
1074 (make-variable-buffer-local 'semantic--grammar-macros-regexp-2
)
1076 (defun semantic--grammar-clear-macros-regexp-2 (&rest ignore
)
1077 "Clear the cached regexp that match macros local in this grammar.
1079 Added to `before-change-functions' hooks to be run before each text
1081 (setq semantic--grammar-macros-regexp-2 nil
))
1083 (defun semantic--grammar-macros-regexp-2 ()
1084 "Return the regexp that match macros local in this grammar."
1085 (unless semantic--grammar-macros-regexp-2
1088 (goto-char (point-min))
1089 (while (re-search-forward semantic--grammar-macdecl-re nil t
)
1091 (setq macs
(nconc macs
1093 (buffer-substring-no-properties
1102 (setq semantic--grammar-macros-regexp-2
1103 (concat "(\\s-*" (regexp-opt macs t
) "\\>"))))))
1104 semantic--grammar-macros-regexp-2
)
1106 (defun semantic--grammar-macros-matcher (end)
1107 "Search for a grammar macro name to highlight.
1108 END is the limit of the search."
1109 (let ((regexp (semantic--grammar-macros-regexp-1)))
1110 (or (and regexp
(re-search-forward regexp end t
))
1111 (and (setq regexp
(semantic--grammar-macros-regexp-2))
1112 (re-search-forward regexp end t
)))))
1115 ;;;; Define major mode
1118 (define-obsolete-variable-alias 'semantic-grammar-syntax-table
1119 'semantic-grammar-mode-syntax-table
"24.1")
1120 (defvar semantic-grammar-mode-syntax-table
1121 (let ((table (make-syntax-table (standard-syntax-table))))
1122 (modify-syntax-entry ?\
: "." table
) ;; COLON
1123 (modify-syntax-entry ?\
> "." table
) ;; GT
1124 (modify-syntax-entry ?\
< "." table
) ;; LT
1125 (modify-syntax-entry ?\|
"." table
) ;; OR
1126 (modify-syntax-entry ?\
; ". 12" table) ;; SEMI, Comment start ;;
1127 (modify-syntax-entry ?
\n ">" table
) ;; Comment end
1128 (modify-syntax-entry ?
\" "\"" table
) ;; String
1129 (modify-syntax-entry ?\%
"w" table
) ;; Word
1130 (modify-syntax-entry ?\-
"_" table
) ;; Symbol
1131 (modify-syntax-entry ?\.
"_" table
) ;; Symbol
1132 (modify-syntax-entry ?
\\ "\\" table
) ;; Quote
1133 (modify-syntax-entry ?\
` "'" table
) ;; Prefix ` (backquote)
1134 (modify-syntax-entry ?
\' "'" table
) ;; Prefix ' (quote)
1135 (modify-syntax-entry ?\
, "'" table
) ;; Prefix , (comma)
1136 (modify-syntax-entry ?\
# "'" table
) ;; Prefix # (sharp)
1138 "Syntax table used in a Semantic grammar buffers.")
1140 (defvar semantic-grammar-mode-hook nil
1141 "Hook run when starting Semantic grammar mode.")
1143 (defvar semantic-grammar-mode-keywords-1
1144 `(("\\(\\<%%\\>\\|\\<%[{}]\\)"
1145 0 font-lock-reference-face
)
1146 ("\\(%\\)\\(\\(\\sw\\|\\s_\\)+\\)"
1147 (1 font-lock-reference-face
)
1148 (2 font-lock-keyword-face
))
1150 0 (unless (semantic-grammar-in-lisp-p) 'bold
))
1151 ("^\\(\\(\\sw\\|\\s_\\)+\\)[ \n\r\t]*:"
1152 1 font-lock-function-name-face
)
1153 (semantic--grammar-macros-matcher
1154 1 ,(if (boundp 'font-lock-builtin-face
)
1155 'font-lock-builtin-face
1156 'font-lock-preprocessor-face
))
1157 ("\\$\\(\\sw\\|\\s_\\)*"
1158 0 font-lock-variable-name-face
)
1159 ("<\\(\\(\\sw\\|\\s_\\)+\\)>"
1160 1 font-lock-type-face
)
1161 (,semantic-grammar-lex-c-char-re
1162 0 ,(if (boundp 'font-lock-constant-face
)
1163 'font-lock-constant-face
1164 'font-lock-string-face
) t
)
1165 ;; Must highlight :keyword here, because ':' is a punctuation in
1167 ("[\r\n\t ]+:\\sw+\\>"
1168 0 font-lock-builtin-face
)
1169 ;; ;; Append the Semantic keywords
1170 ;; ,@semantic-fw-font-lock-keywords
1172 "Font Lock keywords used to highlight Semantic grammar buffers.")
1174 (defvar semantic-grammar-mode-keywords-2
1175 (append semantic-grammar-mode-keywords-1
1176 (if (boundp 'lisp-font-lock-keywords-1
)
1177 lisp-font-lock-keywords-1
1178 lisp-el-font-lock-keywords-1
))
1179 "Font Lock keywords used to highlight Semantic grammar buffers.")
1181 (defvar semantic-grammar-mode-keywords-3
1182 (append semantic-grammar-mode-keywords-1
1183 (if (boundp 'lisp-font-lock-keywords-2
)
1184 lisp-font-lock-keywords-2
1185 lisp-el-font-lock-keywords-2
))
1186 "Font Lock keywords used to highlight Semantic grammar buffers.")
1188 (defvar semantic-grammar-mode-keywords
1189 semantic-grammar-mode-keywords-1
1190 "Font Lock keywords used to highlight Semantic grammar buffers.")
1192 (define-obsolete-variable-alias 'semantic-grammar-map
1193 'semantic-grammar-mode-map
"24.1")
1194 (defvar semantic-grammar-mode-map
1195 (let ((km (make-sparse-keymap)))
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
)
1202 (define-key km
":" 'semantic-grammar-electric-punctuation
)
1204 (define-key km
"\t" 'semantic-grammar-indent
)
1205 (define-key km
"\M-\t" 'semantic-grammar-complete
)
1206 (define-key km
"\C-c\C-c" 'semantic-grammar-create-package
)
1207 (define-key km
"\C-cm" 'semantic-grammar-find-macro-expander
)
1208 (define-key km
"\C-cik" 'semantic-grammar-insert-keyword
)
1209 ;; (define-key km "\C-cc" 'semantic-grammar-generate-and-load)
1210 ;; (define-key km "\C-cr" 'semantic-grammar-generate-one-rule)
1213 "Keymap used in `semantic-grammar-mode'.")
1215 (defvar semantic-grammar-menu
1217 ["Indent Line" semantic-grammar-indent
]
1218 ["Complete Symbol" semantic-grammar-complete
]
1219 ["Find Macro" semantic-grammar-find-macro-expander
]
1221 ["Insert %keyword" semantic-grammar-insert-keyword
]
1223 ["Update Lisp Package" semantic-grammar-create-package
]
1224 ["Recreate Lisp Package" semantic-grammar-recreate-package
]
1226 "Common semantic grammar menu.")
1228 (defun semantic-grammar-setup-menu-emacs (symbol mode-menu
)
1229 "Setup a GNU Emacs grammar menu in variable SYMBOL.
1230 MODE-MENU is an optional specific menu whose items are appended to the
1231 common grammar menu."
1232 (let ((items (make-symbol "items")))
1233 `(unless (boundp ',symbol
)
1234 (easy-menu-define ,symbol
(current-local-map)
1235 "Grammar Menu" semantic-grammar-menu
)
1236 (let ((,items
(cdr ,mode-menu
)))
1238 (easy-menu-add-item ,symbol nil
"--")
1240 (easy-menu-add-item ,symbol nil
(car ,items
))
1241 (setq ,items
(cdr ,items
))))))
1244 (defun semantic-grammar-setup-menu-xemacs (symbol mode-menu
)
1245 "Setup an XEmacs grammar menu in variable SYMBOL.
1246 MODE-MENU is an optional specific menu whose items are appended to the
1247 common grammar menu."
1248 (let ((items (make-symbol "items"))
1249 (path (make-symbol "path")))
1251 (unless (boundp ',symbol
)
1252 (easy-menu-define ,symbol nil
1253 "Grammar Menu" (copy-sequence semantic-grammar-menu
)))
1254 (easy-menu-add ,symbol
)
1255 (let ((,items
(cdr ,mode-menu
))
1256 (,path
(list (car ,symbol
))))
1258 (easy-menu-add-item nil
,path
"--")
1260 (easy-menu-add-item nil
,path
(car ,items
))
1261 (setq ,items
(cdr ,items
))))))
1264 (defmacro semantic-grammar-setup-menu
(&optional mode-menu
)
1265 "Setup a mode local grammar menu.
1266 MODE-MENU is an optional specific menu whose items are appended to the
1267 common grammar menu."
1268 (let ((menu (intern (format "%s-menu" major-mode
))))
1269 (if (featurep 'xemacs
)
1270 (semantic-grammar-setup-menu-xemacs menu mode-menu
)
1271 (semantic-grammar-setup-menu-emacs menu mode-menu
))))
1273 (defsubst semantic-grammar-in-lisp-p
()
1274 "Return non-nil if point is in Lisp code."
1275 (or (>= (point) (semantic-grammar-epilogue-start))
1282 (defun semantic-grammar-edits-new-change-hook-fcn (overlay)
1283 "Function set into `semantic-edits-new-change-hook'.
1284 Argument OVERLAY is the overlay created to mark the change.
1285 When OVERLAY marks a change in the scope of a nonterminal tag extend
1286 the change bounds to encompass the whole nonterminal tag."
1287 (let ((outer (car (semantic-find-tag-by-overlay-in-region
1288 (semantic-edits-os overlay
)
1289 (semantic-edits-oe overlay
)))))
1290 (if (semantic-tag-of-class-p outer
'nonterminal
)
1291 (semantic-overlay-move overlay
1292 (semantic-tag-start outer
)
1293 (semantic-tag-end outer
)))))
1295 (define-derived-mode semantic-grammar-mode
1296 fundamental-mode
"Semantic Grammar Framework"
1297 "Initialize a buffer for editing Semantic grammars.
1299 \\{semantic-grammar-mode-map}"
1300 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
1301 (set (make-local-variable 'comment-start
) ";;")
1302 ;; Look within the line for a ; following an even number of backslashes
1303 ;; after either a non-backslash or the line beginning.
1304 (set (make-local-variable 'comment-start-skip
)
1305 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
1306 (set (make-local-variable 'indent-line-function
)
1307 'semantic-grammar-indent
)
1308 (set (make-local-variable 'fill-paragraph-function
)
1309 'lisp-fill-paragraph
)
1310 (set (make-local-variable 'font-lock-multiline
)
1312 (set (make-local-variable 'font-lock-defaults
)
1313 '((semantic-grammar-mode-keywords
1314 semantic-grammar-mode-keywords-1
1315 semantic-grammar-mode-keywords-2
1316 semantic-grammar-mode-keywords-3
)
1317 nil
;; perform string/comment fontification
1318 nil
;; keywords are case sensitive.
1319 ;; This puts _ & - as a word constituent,
1320 ;; simplifying our keywords significantly
1321 ((?_ .
"w") (?- .
"w"))))
1322 ;; Setup Semantic to parse grammar
1323 (semantic-grammar-wy--install-parser)
1324 (setq semantic-lex-comment-regex
";;"
1325 semantic-lex-analyzer
'semantic-grammar-lexer
1326 semantic-type-relation-separator-character
'(":")
1327 semantic-symbol-
>name-assoc-list
1329 (code .
"Setup Code")
1330 (keyword .
"Keyword")
1332 (nonterminal .
"Nonterminal")
1335 (set (make-local-variable 'semantic-format-face-alist
)
1338 (keyword . font-lock-keyword-face
)
1339 (token . font-lock-type-face
)
1340 (nonterminal . font-lock-function-name-face
)
1343 (set (make-local-variable 'semantic-stickyfunc-sticky-classes
)
1345 ;; Before each change, clear the cached regexp used to highlight
1346 ;; macros local in this grammar.
1347 (semantic-make-local-hook 'before-change-functions
)
1348 (add-hook 'before-change-functions
1349 'semantic--grammar-clear-macros-regexp-2 nil t
)
1350 ;; Handle safe re-parse of grammar rules.
1351 (semantic-make-local-hook 'semantic-edits-new-change-functions
)
1352 (add-hook 'semantic-edits-new-change-functions
1353 'semantic-grammar-edits-new-change-hook-fcn
1357 ;;;; Useful commands
1360 (defvar semantic-grammar-skip-quoted-syntax-table
1361 (let ((st (copy-syntax-table semantic-grammar-mode-syntax-table
)))
1362 (modify-syntax-entry ?
\' "$" st
)
1364 "Syntax table to skip a whole quoted expression in grammar code.
1365 Consider quote as a \"paired delimiter\", so `forward-sexp' will skip
1366 whole quoted expression.")
1368 (defsubst semantic-grammar-backward-item
()
1369 "Move point to beginning of the previous grammar item."
1370 (forward-comment (- (point-max)))
1371 (if (zerop (skip-syntax-backward "."))
1372 (if (eq (char-before) ?
\')
1374 ;; Can't be Lisp code here! Temporarily consider quote
1375 ;; as a "paired delimiter", so `forward-sexp' can skip
1376 ;; the whole quoted expression.
1377 semantic-grammar-skip-quoted-syntax-table
1379 (forward-sexp -
1))))
1381 (defun semantic-grammar-anchored-indentation ()
1382 "Return indentation based on previous anchor character found."
1386 (semantic-grammar-backward-item)
1390 ((looking-at ":\\(\\s-\\|$\\)")
1391 (setq indent
(current-column))
1393 (skip-syntax-forward "-")
1394 (if (eolp) (setq indent
2))
1396 ((and (looking-at "[;%]")
1397 (not (looking-at "\\<%prec\\>")))
1402 (defun semantic-grammar-do-grammar-indent ()
1403 "Indent a line of grammar.
1404 When called the point is not in Lisp code."
1408 (skip-syntax-forward "-")
1409 (setq indent
(current-column))
1412 (looking-at "\\(\\w\\|\\s_\\)+\\s-*:")
1413 (and (looking-at "%")
1414 (not (looking-at "%prec\\>"))))
1418 ((and (looking-at ";;")
1419 (save-excursion (forward-comment (point-max))
1423 (setq n
(semantic-grammar-anchored-indentation))
1428 ((looking-at "[|;]")
1431 (setq n
(+ n
2)))))))
1434 (delete-horizontal-space)
1437 (defvar semantic-grammar-brackets-as-parens-syntax-table
1438 (let ((st (copy-syntax-table emacs-lisp-mode-syntax-table
)))
1439 (modify-syntax-entry ?\
{ "(} " st
)
1440 (modify-syntax-entry ?\
} "){ " st
)
1442 "Syntax table that consider brackets as parenthesis.
1443 So `lisp-indent-line' will work inside bracket blocks.")
1445 (defun semantic-grammar-do-lisp-indent ()
1446 "Maybe run the Emacs Lisp indenter on a line of code.
1447 Return nil if not in a Lisp expression."
1451 (skip-chars-forward "\t ")
1452 (let ((first (point)))
1453 (or (>= first
(semantic-grammar-epilogue-start))
1461 (narrow-to-region (point) first
)
1462 (goto-char (point-max))
1464 ;; Temporarily consider brackets as parenthesis so
1465 ;; `lisp-indent-line' can indent Lisp code inside
1467 semantic-grammar-brackets-as-parens-syntax-table
1468 (lisp-indent-line))))
1472 (defun semantic-grammar-indent ()
1473 "Indent the current line.
1474 Use the Lisp or grammar indenter depending on point location."
1476 (let ((orig (point))
1478 (or (semantic-grammar-do-lisp-indent)
1479 (semantic-grammar-do-grammar-indent))
1480 (setq first
(save-excursion
1482 (skip-chars-forward "\t ")
1484 (if (or (< orig first
) (/= orig
(point)))
1485 (goto-char first
))))
1487 (defun semantic-grammar-electric-punctuation ()
1488 "Insert and reindent for the symbol just typed in."
1490 (self-insert-command 1)
1492 (semantic-grammar-indent)))
1494 (defun semantic-grammar-complete ()
1495 "Attempt to complete the symbol under point.
1496 Completion is position sensitive. If the cursor is in a match section of
1497 a rule, then nonterminals symbols are scanned. If the cursor is in a Lisp
1498 expression then Lisp symbols are completed."
1500 (if (semantic-grammar-in-lisp-p)
1501 ;; We are in lisp code. Do lisp completion.
1502 (let ((completion-at-point-functions
1503 (append '(lisp-completion-at-point)
1504 completion-at-point-functions
)))
1505 (completion-at-point))
1506 ;; We are not in lisp code. Do rule completion.
1507 (let* ((nonterms (semantic-find-tags-by-class 'nonterminal
(current-buffer)))
1508 (sym (car (semantic-ctxt-current-symbol)))
1509 (ans (try-completion sym nonterms
)))
1512 (message "Symbols is already complete"))
1513 ((and (stringp ans
) (string= ans sym
))
1514 ;; Max matchable. Show completions.
1515 (with-output-to-temp-buffer "*Completions*"
1516 (display-completion-list (all-completions sym nonterms
)))
1519 ;; Expand the completions
1521 (delete-region (point) (progn (forward-sexp 1) (point)))
1523 (t (message "No Completions."))
1527 (defun semantic-grammar-insert-keyword (name)
1528 "Insert a new %keyword declaration with NAME.
1529 Assumes it is typed in with the correct casing."
1530 (interactive "sKeyword: ")
1531 (if (not (bolp)) (insert "\n"))
1532 (insert "%keyword " (upcase name
) " \"" name
"\"
1533 %put " (upcase name
) " summary
1537 ;;; Macro facilities
1540 (defsubst semantic--grammar-macro-function-tag
(name)
1541 "Search for a function tag for the grammar macro with name NAME.
1542 Return the tag found or nil if not found."
1543 (car (semantic-find-tags-by-class
1545 (or (semantic-find-tags-by-name name
(current-buffer))
1546 (and (featurep 'semantic
/db
)
1547 semanticdb-current-database
1548 (cdar (semanticdb-find-tags-by-name name nil t
)))))))
1550 (defsubst semantic--grammar-macro-lib-part
(def)
1551 "Return the library part of the grammar macro defined by DEF."
1552 (let ((suf (format "-%s\\'" (regexp-quote (symbol-name (car def
)))))
1553 (fun (symbol-name (cdr def
))))
1554 (substring fun
0 (string-match suf fun
))))
1556 (defun semantic--grammar-macro-compl-elt (def &optional full
)
1557 "Return a completion entry for the grammar macro defined by DEF.
1558 If optional argument FULL is non-nil qualify the macro name with the
1559 library found in DEF."
1560 (let ((mac (car def
))
1561 (lib (semantic--grammar-macro-lib-part def
)))
1563 (format "%s/%s" mac lib
)
1567 (defun semantic--grammar-macro-compl-dict ()
1568 "Return a completion dictionary of macro definitions."
1569 (let ((defs (semantic-grammar-macros))
1572 (setq def
(car defs
)
1574 (if (or (assoc (car def
) defs
) (assoc (car def
) dups
))
1576 (push (semantic--grammar-macro-compl-elt def
) dict
)))
1578 (setq def
(car dups
)
1580 (push (semantic--grammar-macro-compl-elt def t
) dict
))
1583 (defun semantic-grammar-find-macro-expander (macro-name library
)
1584 "Visit the Emacs Lisp library where a grammar macro is implemented.
1585 MACRO-NAME is a symbol that identifies a grammar macro.
1586 LIBRARY is the name (sans extension) of the Emacs Lisp library where
1587 to start searching the macro implementation. Lookup in included
1588 libraries, if necessary.
1589 Find a function tag (in current tags table) whose name contains MACRO-NAME.
1590 Select the buffer containing the tag's definition, and move point there."
1592 (let* ((dic (semantic--grammar-macro-compl-dict))
1593 (def (assoc (completing-read "Macro: " dic nil
1) dic
)))
1594 (or (cdr def
) '(nil nil
))))
1595 (when (and macro-name library
)
1596 (let* ((lib (format "%s.el" library
))
1597 (buf (find-file-noselect (or (locate-library lib t
) lib
)))
1598 (tag (with-current-buffer buf
1599 (semantic--grammar-macro-function-tag
1600 (format "%s-%s" library macro-name
)))))
1603 (require 'semantic
/decorate
)
1604 (pop-to-buffer (semantic-tag-buffer tag
))
1605 (goto-char (semantic-tag-start tag
))
1606 (semantic-momentary-highlight-tag tag
))
1608 (message "No expander found in library %s for macro %s"
1609 library macro-name
)))))
1614 (defvar semantic-grammar-syntax-help
1617 ("symbol" .
"Syntax: A symbol of alpha numeric and symbol characters")
1618 ("number" .
"Syntax: Numeric characters.")
1619 ("punctuation" .
"Syntax: Punctuation character.")
1620 ("semantic-list" .
"Syntax: A list delimited by any valid list characters")
1621 ("open-paren" .
"Syntax: Open Parenthesis character")
1622 ("close-paren" .
"Syntax: Close Parenthesis character")
1623 ("string" .
"Syntax: String character delimited text")
1624 ("comment" .
"Syntax: Comment character delimited text")
1626 ("EMPTY" .
"Syntax: Match empty text")
1627 ("ASSOC" .
"Lambda Key: (ASSOC key1 value1 key2 value2 ...)")
1628 ("EXPAND" .
"Lambda Key: (EXPAND <list id> <rule>)")
1629 ("EXPANDFULL" .
"Lambda Key: (EXPANDFULL <list id> <rule>)")
1630 ;; Tag Generator Macros
1631 ("TAG" .
"Generic Tag Generation: (TAG <name> <tag-class> [ :key value ]*)")
1632 ("VARIABLE-TAG" .
"(VARIABLE-TAG <name> <lang-type> <default-value> [ :key value ]*)")
1633 ("FUNCTION-TAG" .
"(FUNCTION-TAG <name> <lang-type> <arg-list> [ :key value ]*)")
1634 ("TYPE-TAG" .
"(TYPE-TAG <name> <lang-type> <part-list> <parents> [ :key value ]*)")
1635 ("INCLUDE-TAG" .
"(INCLUDE-TAG <name> <system-flag> [ :key value ]*)")
1636 ("PACKAGE-TAG" .
"(PACKAGE-TAG <name> <detail> [ :key value ]*)")
1637 ("CODE-TAG" .
"(CODE-TAG <name> <detail> [ :key value ]*)")
1638 ("ALIAS-TAG" .
"(ALIAS-TAG <name> <aliasclass> <definition> [:key value]*)")
1639 ;; Special value macros
1640 ("$1" .
"Match Value: Value from match list in slot 1")
1641 ("$2" .
"Match Value: Value from match list in slot 2")
1642 ("$3" .
"Match Value: Value from match list in slot 3")
1643 ("$4" .
"Match Value: Value from match list in slot 4")
1644 ("$5" .
"Match Value: Value from match list in slot 5")
1645 ("$6" .
"Match Value: Value from match list in slot 6")
1646 ("$7" .
"Match Value: Value from match list in slot 7")
1647 ("$8" .
"Match Value: Value from match list in slot 8")
1648 ("$9" .
"Match Value: Value from match list in slot 9")
1649 ;; Same, but with annoying , in front.
1650 (",$1" .
"Match Value: Value from match list in slot 1")
1651 (",$2" .
"Match Value: Value from match list in slot 2")
1652 (",$3" .
"Match Value: Value from match list in slot 3")
1653 (",$4" .
"Match Value: Value from match list in slot 4")
1654 (",$5" .
"Match Value: Value from match list in slot 5")
1655 (",$6" .
"Match Value: Value from match list in slot 6")
1656 (",$7" .
"Match Value: Value from match list in slot 7")
1657 (",$8" .
"Match Value: Value from match list in slot 8")
1658 (",$9" .
"Match Value: Value from match list in slot 9")
1660 "Association of syntax elements, and the corresponding help.")
1662 (declare-function eldoc-function-argstring
"eldoc")
1663 (declare-function eldoc-docstring-format-sym-doc
"eldoc")
1664 (declare-function eldoc-last-data-store
"eldoc")
1665 (declare-function eldoc-get-fnsym-args-string
"eldoc")
1666 (declare-function eldoc-get-var-docstring
"eldoc")
1668 (defun semantic-grammar-eldoc-get-macro-docstring (macro expander
)
1669 "Return a one-line docstring for the given grammar MACRO.
1670 EXPANDER is the name of the function that expands MACRO."
1672 (if (and (eq expander
(aref eldoc-last-data
0))
1673 (eq 'function
(aref eldoc-last-data
2)))
1674 (aref eldoc-last-data
1)
1675 (let ((doc (help-split-fundoc (documentation expander t
) expander
)))
1678 (setq doc
(car doc
))
1679 (string-match "\\`[^ )]* ?" doc
)
1680 (setq doc
(concat "(" (substring doc
(match-end 0)))))
1682 (setq doc
(eldoc-function-argstring expander
))))
1685 (eldoc-docstring-format-sym-doc
1686 macro
(format "==> %s %s" expander doc
) 'default
))
1687 (eldoc-last-data-store expander doc
'function
))
1690 (define-mode-local-override semantic-idle-summary-current-symbol-info
1691 semantic-grammar-mode
()
1692 "Display additional eldoc information about grammar syntax elements.
1693 Syntax element is the current symbol at point.
1694 If it is associated a help string in `semantic-grammar-syntax-help',
1696 If it is a macro name, return a description of the associated expander
1697 function parameter list.
1698 If it is a function name, return a description of this function
1700 It it is a variable name, return a brief (one-line) documentation
1701 string for the variable.
1702 If a default description of the current context can be obtained,
1704 Otherwise return nil."
1706 (let* ((elt (car (semantic-ctxt-current-symbol)))
1707 (val (and elt
(cdr (assoc elt semantic-grammar-syntax-help
)))))
1708 (when (and (not val
) elt
(semantic-grammar-in-lisp-p))
1709 ;; Ensure to load macro definitions before doing `intern-soft'.
1710 (setq val
(semantic-grammar-macros)
1711 elt
(intern-soft elt
)
1712 val
(and elt
(cdr (assq elt val
))))
1715 ((and val
(fboundp val
))
1716 (setq val
(semantic-grammar-eldoc-get-macro-docstring elt val
)))
1718 ((and elt
(fboundp elt
))
1719 (setq val
(eldoc-get-fnsym-args-string elt
)))
1721 ((and elt
(boundp elt
))
1722 (setq val
(eldoc-get-var-docstring elt
)))
1724 (or val
(semantic-idle-summary-current-symbol-info-default))))
1726 (define-mode-local-override semantic-tag-boundary-p
1727 semantic-grammar-mode
(tag)
1728 "Return non-nil for tags that should have a boundary drawn.
1729 Only tags of type 'nonterminal will be so marked."
1730 (let ((c (semantic-tag-class tag
)))
1731 (eq c
'nonterminal
)))
1733 (define-mode-local-override semantic-ctxt-current-function
1734 semantic-grammar-mode
(&optional point
)
1735 "Determine the name of the current function at POINT."
1737 (and point
(goto-char point
))
1738 (when (semantic-grammar-in-lisp-p)
1739 (with-mode-local emacs-lisp-mode
1740 (semantic-ctxt-current-function)))))
1742 (define-mode-local-override semantic-ctxt-current-argument
1743 semantic-grammar-mode
(&optional point
)
1744 "Determine the argument index of the called 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-argument)))))
1751 (define-mode-local-override semantic-ctxt-current-assignment
1752 semantic-grammar-mode
(&optional point
)
1753 "Determine the tag being assigned into 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-assignment)))))
1760 (define-mode-local-override semantic-ctxt-current-class-list
1761 semantic-grammar-mode
(&optional point
)
1762 "Determine the class of tags that can be used at POINT."
1764 (and point
(goto-char point
))
1765 (if (semantic-grammar-in-lisp-p)
1766 (with-mode-local emacs-lisp-mode
1767 (semantic-ctxt-current-class-list))
1768 '(nonterminal keyword
))))
1770 (define-mode-local-override semantic-ctxt-current-mode
1771 semantic-grammar-mode
(&optional point
)
1772 "Return the major mode active at POINT.
1773 POINT defaults to the value of point in current buffer.
1774 Return `emacs-lisp-mode' is POINT is within Lisp code, otherwise
1775 return the current major mode."
1777 (and point
(goto-char point
))
1778 (if (semantic-grammar-in-lisp-p)
1780 (semantic-ctxt-current-mode-default))))
1782 (define-mode-local-override semantic-format-tag-abbreviate
1783 semantic-grammar-mode
(tag &optional parent color
)
1784 "Return a string abbreviation of TAG.
1785 Optional PARENT is not used.
1786 Optional COLOR is used to flag if color is added to the text."
1787 (let ((class (semantic-tag-class tag
))
1788 (name (semantic-format-tag-name tag parent color
)))
1790 ((eq class
'nonterminal
)
1792 ((eq class
'setting
)
1794 ((memq class
'(rule keyword
))
1797 (concat "%" (symbol-name class
) " " name
)))))
1799 (define-mode-local-override semantic-format-tag-summarize
1800 semantic-grammar-mode
(tag &optional parent color
)
1801 "Return a string summarizing TAG.
1802 Optional PARENT is not used.
1803 Optional argument COLOR determines if color is added to the text."
1804 (let ((class (semantic-tag-class tag
))
1805 (name (semantic-format-tag-name tag parent color
))
1809 ((eq class
'nonterminal
)
1810 (setq label
"Nonterminal: "
1812 " with %d match lists."
1813 (length (semantic-tag-components tag
)))))
1814 ((eq class
'keyword
)
1815 (setq label
"Keyword: ")
1817 (semantic--find-tags-by-function
1820 (setq summary
(cdr (assoc "summary"
1821 (semantic-tag-get-attribute
1823 ;; Get `put' tag with TAG name.
1824 (semantic-find-tags-by-name-regexp
1825 (regexp-quote (semantic-tag-name tag
))
1826 (semantic-find-tags-by-class 'put
(current-buffer))))
1827 (setq desc
(concat " = "
1828 (semantic-tag-get-attribute tag
:value
)
1830 (concat " - " (read summary
))
1833 (setq label
"Token: ")
1834 (let ((val (semantic-tag-get-attribute tag
:value
))
1835 (names (semantic-tag-get-attribute tag
:rest
))
1836 (type (semantic-tag-type tag
)))
1838 (setq name
(mapconcat 'identity
(cons name names
) " ")))
1841 (format " <%s>" type
)
1844 (format "%s%S" val
(if type
" " ""))
1847 (setq label
"Assoc: ")
1848 (let ((val (semantic-tag-get-attribute tag
:value
))
1849 (type (semantic-tag-type tag
)))
1852 (format " <%s>" type
)
1855 (concat " " (mapconcat 'identity val
" "))
1858 (setq desc
(semantic-format-tag-abbreviate tag parent color
))))
1859 (if (and color label
)
1860 (setq label
(semantic--format-colorize-text label
'label
)))
1861 (if (and color label desc
)
1862 (setq desc
(semantic--format-colorize-text desc
'comment
)))
1864 (concat label name desc
)
1865 ;; Just a description is the abbreviated version
1868 ;;; Semantic Analysis
1870 (define-mode-local-override semantic-analyze-current-context
1871 semantic-grammar-mode
(point)
1872 "Provide a semantic analysis object describing a context in a grammar."
1873 (require 'semantic
/analyze
)
1874 (if (semantic-grammar-in-lisp-p)
1875 (with-mode-local emacs-lisp-mode
1876 (semantic-analyze-current-context point
))
1878 (let* ((context-return nil
)
1879 (prefixandbounds (semantic-ctxt-current-symbol-and-bounds))
1880 (prefix (car prefixandbounds
))
1881 (bounds (nth 2 prefixandbounds
))
1883 (prefixclass (semantic-ctxt-current-class-list))
1886 ;; Do context for rules when in a match list.
1888 (semantic-find-first-tag-by-name
1892 (setq context-return
1893 (semantic-analyze-context
1894 "context-for-semantic-grammar"
1895 :buffer
(current-buffer)
1898 :prefix
(if prefixsym
1902 :prefixclass prefixclass
1907 (define-mode-local-override semantic-analyze-possible-completions
1908 semantic-grammar-mode
(context)
1909 "Return a list of possible completions based on CONTEXT."
1910 (require 'semantic
/analyze
/complete
)
1911 (if (semantic-grammar-in-lisp-p)
1912 (with-mode-local emacs-lisp-mode
1913 (semantic-analyze-possible-completions context
))
1914 (with-current-buffer (oref context buffer
)
1915 (let* ((prefix (car (oref context
:prefix
)))
1916 (completetext (cond ((semantic-tag-p prefix
)
1917 (semantic-tag-name prefix
))
1920 ((stringp (car prefix
))
1922 (tags (semantic-find-tags-for-completion completetext
1924 (semantic-analyze-tags-of-class-list
1925 tags
(oref context prefixclass
)))
1928 (provide 'semantic
/grammar
)
1932 ;; generated-autoload-load-name: "semantic/grammar"
1935 ;;; semantic/grammar.el ends here