1 ;;; semantic.el --- Semantic buffer evaluator.
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: syntax tools
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; API for providing the semantic content of a buffer.
29 ;; The Semantic API provides an interface to a series of different parser
30 ;; implementations. Each parser outputs a parse tree in a similar format
31 ;; designed to handle typical functional and object oriented languages.
33 ;; To enable Semantic, turn on `semantic-mode', a global minor mode
34 ;; (M-x semantic-mode RET, or "Source Code Parsers" from the Tools
35 ;; menu). To enable it at startup, put (semantic-mode 1) in your init
39 (require 'semantic
/tag
)
40 (require 'semantic
/lex
)
42 (defvar semantic-version
"2.0"
43 "Current version of Semantic.")
45 (declare-function inversion-test
"inversion")
46 (declare-function semanticdb-load-ebrowse-caches
"semantic/db-ebrowse")
48 (defun semantic-require-version (major minor
&optional beta
)
49 "Non-nil if this version of Semantic does not satisfy a specific version.
52 (MAJOR MINOR &optional BETA)
54 Values MAJOR and MINOR must be integers. BETA can be an integer, or
55 excluded if a released version is required.
57 It is assumed that if the current version is newer than that specified,
58 everything passes. Exceptions occur when known incompatibilities are
61 (inversion-test 'semantic
62 (concat major
"." minor
63 (when beta
(concat "beta" beta
)))))
65 (defgroup semantic nil
66 "Parser Generator and parser framework."
69 (defgroup semantic-faces nil
70 "Faces used for Semantic enabled tools."
73 (require 'semantic
/fw
)
78 ;;; Variables and Configuration
80 (defvar semantic--parse-table nil
81 "Variable that defines how to parse top level items in a buffer.
82 This variable is for internal use only, and its content depends on the
83 external parser used.")
84 (make-variable-buffer-local 'semantic--parse-table
)
85 (semantic-varalias-obsolete 'semantic-toplevel-bovine-table
86 'semantic--parse-table
"23.2")
88 (defvar semantic-symbol-
>name-assoc-list
90 (variable .
"Variables")
91 (function .
"Functions")
92 (include .
"Dependencies")
93 (package .
"Provides"))
94 "Association between symbols returned, and a string.
95 The string is used to represent a group of objects of the given type.
96 It is sometimes useful for a language to use a different string
97 in place of the default, even though that language will still
98 return a symbol. For example, Java return's includes, but the
99 string can be replaced with `Imports'.")
100 (make-variable-buffer-local 'semantic-symbol-
>name-assoc-list
)
102 (defvar semantic-symbol-
>name-assoc-list-for-type-parts nil
103 "Like `semantic-symbol->name-assoc-list' for type parts.
104 Some tags that have children (see `semantic-tag-children-compatibility')
105 will want to define the names of classes of tags differently than at
106 the top level. For example, in C++, a Function may be called a
107 Method. In addition, there may be new types of tags that exist only
108 in classes, such as protection labels.")
109 (make-variable-buffer-local 'semantic-symbol-
>name-assoc-list-for-type-parts
)
111 (defvar semantic-case-fold nil
112 "Value for `case-fold-search' when parsing.")
113 (make-variable-buffer-local 'semantic-case-fold
)
115 (defvar semantic-expand-nonterminal nil
116 "Function to call for each nonterminal production.
117 Return a list of non-terminals derived from the first argument, or nil
118 if it does not need to be expanded.
119 Languages with compound definitions should use this function to expand
120 from one compound symbol into several. For example, in C the definition
122 is easily parsed into one tag. This function should take this
123 compound tag and turn it into two tags, one for A, and the other for B.")
124 (make-variable-buffer-local 'semantic-expand-nonterminal
)
126 (defvar semantic--buffer-cache nil
127 "A cache of the fully parsed buffer.
128 If no significant changes have been made (based on the state) then
129 this is returned instead of re-parsing the buffer.
131 DO NOT USE THIS VARIABLE IN PROGRAMS.
133 If you need a tag list, use `semantic-fetch-tags'. If you need the
134 cached values for some reason, chances are you can add a hook to
135 `semantic-after-toplevel-cache-change-hook'.")
136 (make-variable-buffer-local 'semantic--buffer-cache
)
137 (semantic-varalias-obsolete 'semantic-toplevel-bovine-cache
138 'semantic--buffer-cache
"23.2")
140 (defvar semantic-unmatched-syntax-cache nil
141 "A cached copy of unmatched syntax tokens.")
142 (make-variable-buffer-local 'semantic-unmatched-syntax-cache
)
144 (defvar semantic-unmatched-syntax-cache-check nil
145 "Non-nil if the unmatched syntax cache is out of date.
146 This is tracked with `semantic-change-function'.")
147 (make-variable-buffer-local 'semantic-unmatched-syntax-cache-check
)
149 (defvar semantic-edits-are-safe nil
150 "When non-nil, modifications do not require a reparse.
151 This prevents tags from being marked dirty, and it prevents top level
152 edits from causing a cache check.
153 Use this when writing programs that could cause a full reparse, but
154 will not change the tag structure, such as adding or updating
155 `top-level' comments.")
157 (defvar semantic-unmatched-syntax-hook nil
158 "Hooks run when Semantic detects syntax not matched in a grammar.
159 Each individual piece of syntax (such as a symbol or punctuation
160 character) is called with this hook when it doesn't match in the
161 grammar, and multiple unmatched syntax elements are not grouped
162 together. Each hook is called with one argument, which is a list
163 of syntax tokens created by the semantic lexer. Use the functions
164 `semantic-lex-token-start', `semantic-lex-token-end' and
165 `semantic-lex-token-text' to get information about these tokens.
166 The current buffer is the buffer these tokens are derived from.")
168 (defvar semantic--before-fetch-tags-hook nil
169 "Hooks run before a buffer is parsed for tags.
170 It is called before any request for tags is made via the function
171 `semantic-fetch-tags' by an application.
172 If any hook returns a nil value, the cached value is returned
173 immediately, even if it is empty.")
174 (semantic-varalias-obsolete 'semantic-before-toplevel-bovination-hook
175 'semantic--before-fetch-tags-hook
"23.2")
177 (defvar semantic-after-toplevel-bovinate-hook nil
178 "Hooks run after a toplevel parse.
179 It is not run if the toplevel parse command is called, and buffer does
180 not need to be fully reparsed.
181 For language specific hooks, make sure you define this as a local hook.
183 This hook should not be used any more.
184 Use `semantic-after-toplevel-cache-change-hook' instead.")
185 (make-obsolete-variable 'semantic-after-toplevel-bovinate-hook nil
"23.2")
187 (defvar semantic-after-toplevel-cache-change-hook nil
188 "Hooks run after the buffer tag list has changed.
189 This list will change when a buffer is reparsed, or when the tag list
190 in a buffer is cleared. It is *NOT* called if the current tag list is
193 Hook functions must take one argument, which is the new list of tags
194 associated with this buffer.
196 For language specific hooks, make sure you define this as a local hook.")
198 (defvar semantic-before-toplevel-cache-flush-hook nil
199 "Hooks run before the toplevel tag cache is flushed.
200 For language specific hooks, make sure you define this as a local
201 hook. This hook is called before a corresponding
202 `semantic-after-toplevel-cache-change-hook' which is also called
203 during a flush when the cache is given a new value of nil.")
205 (defcustom semantic-dump-parse nil
206 "When non-nil, dump parsing information."
210 (defvar semantic-parser-name
"LL"
211 "Optional name of the parser used to parse input stream.")
212 (make-variable-buffer-local 'semantic-parser-name
)
214 (defvar semantic--completion-cache nil
215 "Internal variable used by `semantic-complete-symbol'.")
216 (make-variable-buffer-local 'semantic--completion-cache
)
218 ;;; Parse tree state management API
220 (defvar semantic-parse-tree-state
'needs-rebuild
221 "State of the current parse tree.")
222 (make-variable-buffer-local 'semantic-parse-tree-state
)
224 (defmacro semantic-parse-tree-unparseable
()
225 "Indicate that the current buffer is unparseable.
226 It is also true that the parse tree will need either updating or
227 a rebuild. This state will be changed when the user edits the buffer."
228 `(setq semantic-parse-tree-state
'unparseable
))
230 (defmacro semantic-parse-tree-unparseable-p
()
231 "Return non-nil if the current buffer has been marked unparseable."
232 `(eq semantic-parse-tree-state
'unparseable
))
234 (defmacro semantic-parse-tree-set-needs-update
()
235 "Indicate that the current parse tree needs to be updated.
236 The parse tree can be updated by `semantic-parse-changes'."
237 `(setq semantic-parse-tree-state
'needs-update
))
239 (defmacro semantic-parse-tree-needs-update-p
()
240 "Return non-nil if the current parse tree needs to be updated."
241 `(eq semantic-parse-tree-state
'needs-update
))
243 (defmacro semantic-parse-tree-set-needs-rebuild
()
244 "Indicate that the current parse tree needs to be rebuilt.
245 The parse tree must be rebuilt by `semantic-parse-region'."
246 `(setq semantic-parse-tree-state
'needs-rebuild
))
248 (defmacro semantic-parse-tree-needs-rebuild-p
()
249 "Return non-nil if the current parse tree needs to be rebuilt."
250 `(eq semantic-parse-tree-state
'needs-rebuild
))
252 (defmacro semantic-parse-tree-set-up-to-date
()
253 "Indicate that the current parse tree is up to date."
254 `(setq semantic-parse-tree-state nil
))
256 (defmacro semantic-parse-tree-up-to-date-p
()
257 "Return non-nil if the current parse tree is up to date."
258 `(null semantic-parse-tree-state
))
260 ;;; Interfacing with the system
262 (defcustom semantic-inhibit-functions nil
263 "List of functions to call with no arguments before Semantic is setup.
264 If any of these functions returns non-nil, the current buffer is not
265 setup to use Semantic."
269 (defcustom semantic-new-buffer-setup-functions
270 '((c-mode . semantic-default-c-setup
)
271 (c++-mode . semantic-default-c-setup
)
272 (html-mode . semantic-default-html-setup
)
273 (java-mode . wisent-java-default-setup
)
274 (js-mode . wisent-javascript-setup-parser
)
275 (python-mode . wisent-python-default-setup
)
276 (scheme-mode . semantic-default-scheme-setup
)
277 (srecode-template-mode . srecode-template-setup-parser
)
278 (makefile-automake-mode . semantic-default-make-setup
)
279 (makefile-gmake-mode . semantic-default-make-setup
)
280 (makefile-makepp-mode . semantic-default-make-setup
)
281 (makefile-bsdmake-mode . semantic-default-make-setup
)
282 (makefile-imake-mode . semantic-default-make-setup
)
283 (makefile-mode . semantic-default-make-setup
))
284 "Alist of functions to call to set up Semantic parsing in the buffer.
285 Each element has the form (MODE . FN), where MODE is a value of
286 `major-mode' for the buffer and FN is the corresponding function
287 to call, with no arguments, to set up the parser.
289 These functions are called by `semantic-new-buffer-fcn', before
290 `semantic-inhibit-functions'."
292 :type
'(alist :key-type symbol
:value-type function
))
294 (defvar semantic-init-hook nil
295 "Hook run when a buffer is initialized with a parsing table.")
297 (defvar semantic-init-mode-hook nil
298 "Hook run when a buffer of a particular mode is initialized.")
299 (make-variable-buffer-local 'semantic-init-mode-hook
)
301 (defvar semantic-init-db-hook nil
302 "Hook run when a buffer is initialized with a parsing table for DBs.
303 This hook is for database functions which intend to swap in a tag table.
304 This guarantees that the DB will go before other modes that require
305 a parse of the buffer.")
307 (semantic-varalias-obsolete 'semantic-init-hooks
308 'semantic-init-hook
"23.2")
309 (semantic-varalias-obsolete 'semantic-init-mode-hooks
310 'semantic-init-mode-hook
"23.2")
311 (semantic-varalias-obsolete 'semantic-init-db-hooks
312 'semantic-init-db-hook
"23.2")
314 (defvar semantic-new-buffer-fcn-was-run nil
315 "Non-nil after `semantic-new-buffer-fcn' has been executed.")
316 (make-variable-buffer-local 'semantic-new-buffer-fcn-was-run
)
318 (defsubst semantic-active-p
()
319 "Return non-nil if the current buffer was set up for parsing."
320 semantic-new-buffer-fcn-was-run
)
322 (defsubst semantic--umatched-syntax-needs-refresh-p
()
323 "Return non-nil if the unmatched syntax cache needs a refresh.
324 That is, if it is dirty or if the current parse tree isn't up to date."
325 (or semantic-unmatched-syntax-cache-check
326 (not (semantic-parse-tree-up-to-date-p))))
328 (defun semantic-new-buffer-fcn ()
329 "Setup the current buffer to use Semantic.
330 If the major mode is ready for Semantic, and no
331 `semantic-inhibit-functions' disabled it, the current buffer is setup
332 to use Semantic, and `semantic-init-hook' is run."
333 ;; In upstream Semantic, the parser setup functions are called from
334 ;; mode hooks. In the version bundled with Emacs, we do it here.
335 (let ((entry (assq major-mode semantic-new-buffer-setup-functions
)))
337 (funcall (cdr entry
))))
338 ;; Do stuff if semantic was activated by a mode hook in this buffer,
339 ;; and not afterwards disabled.
340 (when (and semantic--parse-table
341 (not (semantic-active-p))
342 (not (run-hook-with-args-until-success
343 'semantic-inhibit-functions
)))
344 ;; Make sure that if this buffer is cloned, our tags and overlays
345 ;; don't go along for the ride.
346 (add-hook 'clone-indirect-buffer-hook
'semantic-clear-toplevel-cache
348 ;; Specify that this function has done it's work. At this point
349 ;; we can consider that semantic is active in this buffer.
350 (setq semantic-new-buffer-fcn-was-run t
)
351 ;; Here are some buffer local variables we can initialize ourselves
352 ;; of a mode does not choose to do so.
354 ;; Force this buffer to have its cache refreshed.
355 (semantic-clear-toplevel-cache)
356 ;; Call DB hooks before regular init hooks
357 (run-hooks 'semantic-init-db-hook
)
358 ;; Set up semantic modes
359 (run-hooks 'semantic-init-hook
)
360 ;; Set up major-mode specific semantic modes
361 (run-hooks 'semantic-init-mode-hook
)))
363 (defun semantic-fetch-tags-fast ()
364 "For use in a hook. When only a partial reparse is needed, reparse."
366 (if (semantic-parse-tree-needs-update-p)
367 (semantic-fetch-tags))
369 semantic--buffer-cache
)
374 (condition-case nil
(require 'pp
) (error nil
)))
376 (defvar semantic-edebug nil
377 "When non-nil, activate the interactive parsing debugger.
378 Do not set this yourself. Call `semantic-debug'.")
380 (defun semantic-elapsed-time (start end
)
381 "Copied from elp.el. Was `elp-elapsed-time'.
382 Argument START and END bound the time being calculated."
383 (+ (* (- (car end
) (car start
)) 65536.0)
384 (- (car (cdr end
)) (car (cdr start
)))
385 (/ (- (car (cdr (cdr end
))) (car (cdr (cdr start
)))) 1000000.0)))
387 (defun bovinate (&optional clear
)
388 "Parse the current buffer. Show output in a temp buffer.
389 Optional argument CLEAR will clear the cache before parsing.
390 If CLEAR is negative, it will do a full reparse, and also not display
393 (if clear
(semantic-clear-toplevel-cache))
394 (if (eq clear
'-
) (setq clear -
1))
395 (let* ((start (current-time))
396 (out (semantic-fetch-tags))
397 (end (current-time)))
398 (message "Retrieving tags took %.2f seconds."
399 (semantic-elapsed-time start end
))
400 (when (or (null clear
) (not (listp clear
)))
401 (pop-to-buffer "*Parser Output*")
404 (insert (pp-to-string out
))
405 (goto-char (point-min)))))
407 ;;; Functions of the parser plug-in API
409 ;; Overload these functions to create new types of parsers.
411 (define-overloadable-function semantic-parse-stream
(stream nonterminal
)
412 "Parse STREAM, starting at the first NONTERMINAL rule.
413 For bovine and wisent based parsers, STREAM is from the output of
414 `semantic-lex', and NONTERMINAL is a rule in the appropriate language
416 The default parser table used for bovine or wisent based parsers is
417 `semantic--parse-table'.
419 Must return a list: (STREAM TAGS) where STREAM is the unused elements
420 from STREAM, and TAGS is the list of semantic tags found; usually only
421 one tag is returned with the exception of compound statements.")
423 (define-overloadable-function semantic-parse-changes
()
424 "Reparse changes in the current buffer.
425 The list of changes are tracked as a series of overlays in the buffer.
426 When overloading this function, use `semantic-changes-in-region' to
429 (define-overloadable-function semantic-parse-region
430 (start end
&optional nonterminal depth returnonerror
)
431 "Parse the area between START and END, and return any tags found.
432 If END needs to be extended due to a lexical token being too large, it
433 will be silently ignored.
436 NONTERMINAL is the rule to start parsing at.
437 DEPTH specifies the lexical depth to descend for parser that use
438 lexical analysis as their first step.
439 RETURNONERROR specifies that parsing should stop on the first
440 unmatched syntax encountered. When nil, parsing skips the syntax,
441 adding it to the unmatched syntax cache.
443 Must return a list of semantic tags wich have been cooked
444 \(repositioned properly) but which DO NOT HAVE OVERLAYS associated
445 with them. When overloading this function, use `semantic--tag-expand'
448 (defun semantic-parse-region-default
449 (start end
&optional nonterminal depth returnonerror
)
450 "Parse the area between START and END, and return any tags found.
451 If END needs to be extended due to a lexical token being too large,
452 it will be silently ignored.
454 NONTERMINAL is the rule to start parsing at if it is known.
455 DEPTH specifies the lexical depth to scan.
456 RETURNONERROR specifies that parsing should end when encountering
457 unterminated syntax."
458 (when (or (null semantic--parse-table
) (eq semantic--parse-table t
))
459 ;; If there is no table, or it was set to t, then we are here by
460 ;; some other mistake. Do not throw an error deep in the parser.
461 (error "No support found to parse buffer %S" (buffer-name)))
464 (when (or (< end start
) (> end
(point-max)))
465 (error "Invalid parse region bounds %S, %S" start end
))
467 (semantic-repeat-parse-whole-stream
468 (or (cdr (assq start semantic-lex-block-streams
))
469 (semantic-lex start end depth
))
470 nonterminal returnonerror
))))
472 ;;; Parsing functions
474 (defun semantic-set-unmatched-syntax-cache (unmatched-syntax)
475 "Set the unmatched syntax cache.
476 Argument UNMATCHED-SYNTAX is the syntax to set into the cache."
477 ;; This function is not actually called by the main parse loop.
478 ;; This is intended for use by semanticdb.
479 (setq semantic-unmatched-syntax-cache unmatched-syntax
480 semantic-unmatched-syntax-cache-check nil
)
481 ;; Refresh the display of unmatched syntax tokens if enabled
482 (run-hook-with-args 'semantic-unmatched-syntax-hook
483 semantic-unmatched-syntax-cache
))
485 (defun semantic-clear-unmatched-syntax-cache ()
486 "Clear the cache of unmatched syntax tokens."
487 (setq semantic-unmatched-syntax-cache nil
488 semantic-unmatched-syntax-cache-check t
))
490 (defun semantic-unmatched-syntax-tokens ()
491 "Return the list of unmatched syntax tokens."
492 ;; If the cache need refresh then do a full re-parse.
493 (if (semantic--umatched-syntax-needs-refresh-p)
494 ;; To avoid a recursive call, temporarily disable
495 ;; `semantic-unmatched-syntax-hook'.
496 (let (semantic-unmatched-syntax-hook)
499 (semantic-clear-toplevel-cache)
500 (semantic-fetch-tags))
502 (message "semantic-unmatched-syntax-tokens:\
503 parsing of buffer canceled"))
505 semantic-unmatched-syntax-cache
)
507 (defun semantic-clear-toplevel-cache ()
508 "Clear the toplevel tag cache for the current buffer.
509 Clearing the cache will force a complete reparse next time a tag list
512 (run-hooks 'semantic-before-toplevel-cache-flush-hook
)
513 (setq semantic--buffer-cache nil
)
514 (semantic-clear-unmatched-syntax-cache)
515 (semantic-clear-parser-warnings)
516 ;; Nuke all semantic overlays. This is faster than deleting based
517 ;; on our data structure.
518 (let ((l (semantic-overlay-lists)))
519 (mapc 'semantic-delete-overlay-maybe
(car l
))
520 (mapc 'semantic-delete-overlay-maybe
(cdr l
))
522 (semantic-parse-tree-set-needs-rebuild)
523 ;; Remove this hook which tracks if a buffer is up to date or not.
524 (remove-hook 'after-change-functions
'semantic-change-function t
)
525 ;; Old model. Delete someday.
526 ;;(run-hooks 'semantic-after-toplevel-bovinate-hook)
528 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
529 semantic--buffer-cache
)
531 (setq semantic--completion-cache nil
))
533 (defvar semantic-bovinate-nonterminal-check-obarray
)
535 (defun semantic--set-buffer-cache (tagtable)
536 "Set the toplevel tag cache to TAGTABLE."
537 (setq semantic--buffer-cache tagtable
538 semantic-unmatched-syntax-cache-check nil
)
539 ;; This is specific to the bovine parser.
540 (set (make-local-variable 'semantic-bovinate-nonterminal-check-obarray
)
542 (semantic-parse-tree-set-up-to-date)
543 (semantic-make-local-hook 'after-change-functions
)
544 (add-hook 'after-change-functions
'semantic-change-function nil t
)
545 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
546 semantic--buffer-cache
)
547 (setq semantic--completion-cache nil
)
548 ;; Refresh the display of unmatched syntax tokens if enabled
549 (run-hook-with-args 'semantic-unmatched-syntax-hook
550 semantic-unmatched-syntax-cache
)
551 ;; Old Semantic 1.3 hook API. Maybe useful forever?
552 (run-hooks 'semantic-after-toplevel-bovinate-hook
)
555 (defvar semantic-working-type
'percent
556 "*The type of working message to use when parsing.
557 'percent means we are doing a linear parse through the buffer.
558 'dynamic means we are reparsing specific tags.")
559 (semantic-varalias-obsolete 'semantic-bovination-working-type
560 'semantic-working-type
"23.2")
562 (defvar semantic-minimum-working-buffer-size
(* 1024 5)
563 "*The minimum size of a buffer before working messages are displayed.
564 Buffers smaller than this will parse silently.
565 Buffers larger than this will display the working progress bar.")
567 (defsubst semantic-parser-working-message
(&optional arg
)
568 "Return the message string displayed while parsing.
569 If optional argument ARG is non-nil it is appended to the message
572 (if arg
(format " %s" arg
))
573 (if semantic-parser-name
(format " (%s)" semantic-parser-name
))
576 ;;; Application Parser Entry Points
578 ;; The best way to call the parser from programs is via
579 ;; `semantic-fetch-tags'. This, in turn, uses other internal
580 ;; API functions which plug-in parsers can take advantage of.
582 (defun semantic-fetch-tags ()
583 "Fetch semantic tags from the current buffer.
584 If the buffer cache is up to date, return that.
585 If the buffer cache is out of date, attempt an incremental reparse.
586 If the buffer has not been parsed before, or if the incremental reparse
587 fails, then parse the entire buffer.
588 If a lexical error had been previously discovered and the buffer
589 was marked unparseable, then do nothing, and return the cache."
591 ;; Is this a semantic enabled buffer?
593 ;; Application hooks say the buffer is safe for parsing
594 (run-hook-with-args-until-failure
595 'semantic-before-toplevel-bovination-hook
)
596 (run-hook-with-args-until-failure
597 'semantic--before-fetch-tags-hook
)
598 ;; If the buffer was previously marked unparseable,
599 ;; then don't waste our time.
600 (not (semantic-parse-tree-unparseable-p))
601 ;; The parse tree actually needs to be refreshed
602 (not (semantic-parse-tree-up-to-date-p))
604 (let* ((gc-cons-threshold (max gc-cons-threshold
10000000))
605 (semantic-lex-block-streams nil
)
610 ;;;; Try the incremental parser to do a fast update.
611 ((semantic-parse-tree-needs-update-p)
612 (setq res
(semantic-parse-changes))
613 (if (semantic-parse-tree-needs-rebuild-p)
614 ;; If the partial reparse fails, jump to a full reparse.
615 (semantic-fetch-tags)
616 ;; Clear the cache of unmatched syntax tokens
620 ;; Move this into the incremental parser. This is a bug.
622 (semantic-clear-unmatched-syntax-cache)
623 (run-hook-with-args ;; Let hooks know the updated tags
624 'semantic-after-partial-cache-change-hook res
))
625 (setq semantic--completion-cache nil
))
627 ;;;; Parse the whole system.
628 ((semantic-parse-tree-needs-rebuild-p)
629 ;; Use Emacs' built-in progress-reporter
630 (let ((semantic--progress-reporter
631 (and (>= (point-max) semantic-minimum-working-buffer-size
)
632 (eq semantic-working-type
'percent
)
633 (make-progress-reporter
634 (semantic-parser-working-message (buffer-name))
636 (setq res
(semantic-parse-region (point-min) (point-max)))
637 (if semantic--progress-reporter
638 (progress-reporter-done semantic--progress-reporter
)))
640 ;; Clear the caches when we see there were no errors.
641 ;; But preserve the unmatched syntax cache and warnings!
642 (let (semantic-unmatched-syntax-cache
643 semantic-unmatched-syntax-cache-check
644 semantic-parser-warnings
)
645 (semantic-clear-toplevel-cache))
646 ;; Set up the new overlays
647 (semantic--tag-link-list-to-buffer res
)
648 ;; Set up the cache with the new results
649 (semantic--set-buffer-cache res
)
652 ;; Always return the current parse tree.
653 semantic--buffer-cache
)
655 (defun semantic-refresh-tags-safe ()
656 "Refresh the current buffer's tags safely.
658 Return non-nil if the refresh was successful.
659 Return nil if there is some sort of syntax error preventing a reparse.
661 Does nothing if the current buffer doesn't need reparsing."
663 ;; These checks actually occur in `semantic-fetch-tags', but if we
664 ;; do them here, then all the bovination hooks are not run, and
665 ;; we save lots of time.
667 ;; If the buffer was previously marked unparseable,
668 ;; then don't waste our time.
669 ((semantic-parse-tree-unparseable-p)
671 ;; The parse tree is already ok.
672 ((semantic-parse-tree-up-to-date-p)
675 (let* ((inhibit-quit nil
)
680 ;; Perform the parsing.
682 (when (semantic-lex-catch-errors safe-refresh
683 (save-excursion (semantic-fetch-tags))
685 ;; If we are here, it is because the lexical step failed,
686 ;; proably due to unterminated lists or something like that.
688 ;; We do nothing, and just wait for the next idle timer
689 ;; to go off. In the meantime, remember this, and make sure
690 ;; no other idle services can get executed.
691 (setq lexically-safe nil
))
694 ;; Return if we are lexically safe
697 (defun semantic-bovinate-toplevel (&optional ignored
)
698 "Backward compatibility function."
699 (semantic-fetch-tags))
700 (make-obsolete 'semantic-bovinate-toplevel
'semantic-fetch-tags
"23.2")
702 ;; Another approach is to let Emacs call the parser on idle time, when
703 ;; needed, use `semantic-fetch-available-tags' to only retrieve
704 ;; available tags, and setup the `semantic-after-*-hook' hooks to
705 ;; synchronize with new tags when they become available.
707 (defsubst semantic-fetch-available-tags
()
708 "Fetch available semantic tags from the current buffer.
709 That is, return tags currently in the cache without parsing the
711 Parse operations happen asynchronously when needed on Emacs idle time.
712 Use the `semantic-after-toplevel-cache-change-hook' and
713 `semantic-after-partial-cache-change-hook' hooks to synchronize with
714 new tags when they become available."
715 semantic--buffer-cache
)
717 ;;; Iterative parser helper function
719 ;; Iterative parsers are better than rule-based iterative functions
720 ;; in that they can handle obscure errors more cleanly.
722 ;; `semantic-repeat-parse-whole-stream' abstracts this action for
723 ;; other parser centric routines.
725 (defun semantic-repeat-parse-whole-stream
726 (stream nonterm
&optional returnonerror
)
727 "Iteratively parse the entire stream STREAM starting with NONTERM.
728 Optional argument RETURNONERROR indicates that the parser should exit
729 with the current results on a parse error.
730 This function returns semantic tags without overlays."
732 (case-fold-search semantic-case-fold
)
735 (setq nontermsym
(semantic-parse-stream stream nonterm
)
736 tag
(car (cdr nontermsym
)))
738 (error "Parse error @ %d" (car (cdr (car stream
)))))
739 (if (eq (car nontermsym
) stream
)
740 (error "Parser error: Infinite loop?"))
745 ;; Set the 'reparse-symbol property to
746 ;; NONTERM unless it was already setup
748 (or (semantic--tag-get-property
750 (semantic--tag-put-property
751 tag
'reparse-symbol nonterm
))
753 (semantic--tag-expand tag
))
754 result
(append tag result
))
755 ;; No error in this case, a purposeful nil means don't
760 ;; The current item in the stream didn't match, so add it to
761 ;; the list of syntax items which didn't match.
762 (setq semantic-unmatched-syntax-cache
763 (cons (car stream
) semantic-unmatched-syntax-cache
))
765 ;; Designated to ignore.
766 (setq stream
(car nontermsym
))
768 ;; Use Emacs' built-in progress reporter:
769 (and (boundp 'semantic--progress-reporter
)
770 semantic--progress-reporter
771 (eq semantic-working-type
'percent
)
772 (progress-reporter-update
773 semantic--progress-reporter
774 (/ (* 100 (semantic-lex-token-start (car stream
)))
778 ;;; Parsing Warnings:
780 ;; Parsing a buffer may result in non-critical things that we should
781 ;; alert the user to without interrupting the normal flow.
783 ;; Any parser can use this API to provide a list of warnings during a
784 ;; parse which a user may want to investigate.
785 (defvar semantic-parser-warnings nil
786 "A list of parser warnings since the last full reparse.")
787 (make-variable-buffer-local 'semantic-parser-warnings
)
789 (defun semantic-clear-parser-warnings ()
790 "Clear the current list of parser warnings for this buffer."
791 (setq semantic-parser-warnings nil
))
793 (defun semantic-push-parser-warning (warning start end
)
794 "Add a parser WARNING that covers text from START to END."
795 (setq semantic-parser-warnings
796 (cons (cons warning
(cons start end
))
797 semantic-parser-warnings
)))
799 (defun semantic-dump-parser-warnings ()
800 "Dump any parser warnings."
802 (if semantic-parser-warnings
803 (let ((pw semantic-parser-warnings
))
804 (pop-to-buffer "*Parser Warnings*")
807 (insert (pp-to-string pw
))
808 (goto-char (point-min)))
809 (message "No parser warnings.")))
815 ;; Semantic 1.x parser action helper functions, used by some parsers.
816 ;; Please move away from these functions, and try using semantic 2.x
817 ;; interfaces instead.
819 (defsubst semantic-bovinate-region-until-error
820 (start end nonterm
&optional depth
)
821 "NOTE: Use `semantic-parse-region' instead.
823 Bovinate between START and END starting with NONTERM.
824 Optional DEPTH specifies how many levels of parenthesis to enter.
825 This command will parse until an error is encountered, and return
826 the list of everything found until that moment.
827 This is meant for finding variable definitions at the beginning of
828 code blocks in methods. If `bovine-inner-scope' can also support
829 commands, use `semantic-bovinate-from-nonterminal-full'."
830 (semantic-parse-region start end nonterm depth t
))
831 (make-obsolete 'semantic-bovinate-region-until-error
832 'semantic-parse-region
"23.2")
834 (defsubst semantic-bovinate-from-nonterminal
835 (start end nonterm
&optional depth length
)
836 "Bovinate from within a nonterminal lambda from START to END.
837 Argument NONTERM is the nonterminal symbol to start with.
838 Optional argument DEPTH is the depth of lists to dive into. When used
839 in a `lambda' of a MATCH-LIST, there is no need to include a START and
841 Optional argument LENGTH specifies we are only interested in LENGTH
843 (car-safe (cdr (semantic-parse-stream
844 (semantic-lex start end
(or depth
1) length
)
847 (defsubst semantic-bovinate-from-nonterminal-full
848 (start end nonterm
&optional depth
)
849 "NOTE: Use `semantic-parse-region' instead.
851 Bovinate from within a nonterminal lambda from START to END.
852 Iterates until all the space between START and END is exhausted.
853 Argument NONTERM is the nonterminal symbol to start with.
854 If NONTERM is nil, use `bovine-block-toplevel'.
855 Optional argument DEPTH is the depth of lists to dive into.
856 When used in a `lambda' of a MATCH-LIST, there is no need to include
857 a START and END part."
858 (semantic-parse-region start end nonterm
(or depth
1)))
859 (make-obsolete 'semantic-bovinate-from-nonterminal-full
860 'semantic-parse-region
"23.2")
864 (defun semantic-force-refresh ()
865 "Force a full refresh of the current buffer's tags.
866 Throw away all the old tags, and recreate the tag database."
868 (semantic-clear-toplevel-cache)
869 (semantic-fetch-tags)
870 (message "Buffer reparsed."))
872 (defvar semantic-mode-map
873 (let ((map (make-sparse-keymap)))
875 ;; (define-key km "f" 'senator-search-set-tag-class-filter)
876 ;; (define-key km "i" 'senator-isearch-toggle-semantic-mode)
877 (define-key map
"\C-c,j" 'semantic-complete-jump-local
)
878 (define-key map
"\C-c,J" 'semantic-complete-jump
)
879 (define-key map
"\C-c,m" 'semantic-complete-jump-local-members
)
880 (define-key map
"\C-c,g" 'semantic-symref-symbol
)
881 (define-key map
"\C-c,G" 'semantic-symref
)
882 (define-key map
"\C-c,p" 'senator-previous-tag
)
883 (define-key map
"\C-c,n" 'senator-next-tag
)
884 (define-key map
"\C-c,u" 'senator-go-to-up-reference
)
885 (define-key map
"\C-c, " 'semantic-complete-analyze-inline
)
886 (define-key map
"\C-c,\C-w" 'senator-kill-tag
)
887 (define-key map
"\C-c,\M-w" 'senator-copy-tag
)
888 (define-key map
"\C-c,\C-y" 'senator-yank-tag
)
889 (define-key map
"\C-c,r" 'senator-copy-tag-to-register
)
890 (define-key map
"\C-c,," 'semantic-force-refresh
)
891 (define-key map
[?\C-c ?
, up
] 'senator-transpose-tags-up
)
892 (define-key map
[?\C-c ?
, down
] 'senator-transpose-tags-down
)
893 (define-key map
"\C-c,l" 'semantic-analyze-possible-completions
)
894 ;; This hack avoids showing the CEDET menu twice if ede-minor-mode
895 ;; and Semantic are both enabled. Is there a better way?
896 (define-key map
[menu-bar cedet-menu
]
897 (list 'menu-item
"Development" cedet-menu-map
898 :enable
(quote (not (bound-and-true-p global-ede-mode
)))))
899 ;; (define-key km "-" 'senator-fold-tag)
900 ;; (define-key km "+" 'senator-unfold-tag)
903 ;; Activate the Semantic items in cedet-menu-map
904 (let ((navigate-menu (make-sparse-keymap "Navigate Tags"))
905 (edit-menu (make-sparse-keymap "Edit Tags")))
907 ;; Edit Tags submenu:
908 (define-key edit-menu
[semantic-analyze-possible-completions
]
909 '(menu-item "List Completions" semantic-analyze-possible-completions
910 :help
"Display a list of completions for the tag at point"))
911 (define-key edit-menu
[semantic-complete-analyze-inline
]
912 '(menu-item "Complete Tag Inline" semantic-complete-analyze-inline
913 :help
"Display inline completion for the tag at point"))
914 (define-key edit-menu
[semantic-completion-separator
]
916 (define-key edit-menu
[senator-transpose-tags-down
]
917 '(menu-item "Transpose Tags Down" senator-transpose-tags-down
918 :active
(semantic-current-tag)
919 :help
"Transpose the current tag and the next tag"))
920 (define-key edit-menu
[senator-transpose-tags-up
]
921 '(menu-item "Transpose Tags Up" senator-transpose-tags-up
922 :active
(semantic-current-tag)
923 :help
"Transpose the current tag and the previous tag"))
924 (define-key edit-menu
[semantic-edit-separator
]
926 (define-key edit-menu
[senator-yank-tag
]
927 '(menu-item "Yank Tag" senator-yank-tag
928 :active
(not (ring-empty-p senator-tag-ring
))
929 :help
"Yank the head of the tag ring into the buffer"))
930 (define-key edit-menu
[senator-copy-tag-to-register
]
931 '(menu-item "Copy Tag To Register" senator-copy-tag-to-register
932 :active
(semantic-current-tag)
933 :help
"Yank the head of the tag ring into the buffer"))
934 (define-key edit-menu
[senator-copy-tag
]
935 '(menu-item "Copy Tag" senator-copy-tag
936 :active
(semantic-current-tag)
937 :help
"Copy the current tag to the tag ring"))
938 (define-key edit-menu
[senator-kill-tag
]
939 '(menu-item "Kill Tag" senator-kill-tag
940 :active
(semantic-current-tag)
941 :help
"Kill the current tag, and copy it to the tag ring"))
943 ;; Navigate Tags submenu:
944 (define-key navigate-menu
[senator-narrow-to-defun
]
945 '(menu-item "Narrow to Tag" senator-narrow-to-defun
946 :active
(semantic-current-tag)
947 :help
"Narrow the buffer to the bounds of the current tag"))
948 (define-key navigate-menu
[semantic-narrow-to-defun-separator
]
950 (define-key navigate-menu
[semantic-symref-symbol
]
951 '(menu-item "Find Tag References..." semantic-symref-symbol
952 :help
"Read a tag and list the references to it"))
953 (define-key navigate-menu
[semantic-complete-jump
]
954 '(menu-item "Find Tag Globally..." semantic-complete-jump
955 :help
"Read a tag name and find it in the current project"))
956 (define-key navigate-menu
[semantic-complete-jump-local-members
]
957 '(menu-item "Find Local Members ..." semantic-complete-jump-local-members
958 :help
"Read a tag name and find a local member with that name"))
959 (define-key navigate-menu
[semantic-complete-jump-local
]
960 '(menu-item "Find Tag in This Buffer..." semantic-complete-jump-local
961 :help
"Read a tag name and find it in this buffer"))
962 (define-key navigate-menu
[semantic-navigation-separator
]
964 (define-key navigate-menu
[senator-go-to-up-reference
]
965 '(menu-item "Parent Tag" senator-go-to-up-reference
966 :help
"Navigate up one reference by tag."))
967 (define-key navigate-menu
[senator-next-tag
]
968 '(menu-item "Next Tag" senator-next-tag
969 :help
"Go to the next tag"))
970 (define-key navigate-menu
[senator-previous-tag
]
971 '(menu-item "Previous Tag" senator-previous-tag
972 :help
"Go to the previous tag"))
974 ;; Top level menu items:
975 (define-key cedet-menu-map
[semantic-force-refresh
]
976 '(menu-item "Reparse Buffer" semantic-force-refresh
977 :help
"Force a full reparse of the current buffer."
978 :visible semantic-mode
))
979 (define-key cedet-menu-map
[semantic-edit-menu
]
980 `(menu-item "Edit Tags" ,edit-menu
981 :visible semantic-mode
))
982 (define-key cedet-menu-map
[navigate-menu
]
983 `(menu-item "Navigate Tags" ,navigate-menu
984 :visible semantic-mode
))
985 (define-key cedet-menu-map
[semantic-options-separator
]
987 (define-key cedet-menu-map
[global-semantic-highlight-func-mode
]
988 '(menu-item "Highlight Current Function" global-semantic-highlight-func-mode
989 :help
"Highlight the tag at point"
990 :visible semantic-mode
991 :button
(:toggle . global-semantic-highlight-func-mode
)))
992 (define-key cedet-menu-map
[global-semantic-decoration-mode
]
993 '(menu-item "Decorate Tags" global-semantic-decoration-mode
994 :help
"Decorate tags based on tag attributes"
995 :visible semantic-mode
996 :button
(:toggle .
(bound-and-true-p
997 global-semantic-decoration-mode
))))
998 (define-key cedet-menu-map
[global-semantic-idle-completions-mode
]
999 '(menu-item "Show Tag Completions" global-semantic-idle-completions-mode
1000 :help
"Show tag completions when idle"
1001 :visible semantic-mode
1002 :enable global-semantic-idle-scheduler-mode
1003 :button
(:toggle . global-semantic-idle-completions-mode
)))
1004 (define-key cedet-menu-map
[global-semantic-idle-summary-mode
]
1005 '(menu-item "Show Tag Summaries" global-semantic-idle-summary-mode
1006 :help
"Show tag summaries when idle"
1007 :visible semantic-mode
1008 :enable global-semantic-idle-scheduler-mode
1009 :button
(:toggle . global-semantic-idle-summary-mode
)))
1010 (define-key cedet-menu-map
[global-semantic-idle-scheduler-mode
]
1011 '(menu-item "Reparse When Idle" global-semantic-idle-scheduler-mode
1012 :help
"Keep a buffer's parse tree up to date when idle"
1013 :visible semantic-mode
1014 :button
(:toggle . global-semantic-idle-scheduler-mode
)))
1015 (define-key cedet-menu-map
[global-semanticdb-minor-mode
]
1016 '(menu-item "Semantic Database" global-semanticdb-minor-mode
1017 :help
"Store tag information in a database"
1018 :visible semantic-mode
1019 :button
(:toggle . global-semanticdb-minor-mode
))))
1021 ;; The `semantic-mode' command, in conjuction with the
1022 ;; `semantic-default-submodes' variable, toggles Semantic's various
1023 ;; auxiliary minor modes.
1025 (defvar semantic-load-system-cache-loaded nil
1026 "Non-nil when the Semantic system caches have been loaded.
1027 Prevent this load system from loading files in twice.")
1029 (defconst semantic-submode-list
1030 '(global-semantic-highlight-func-mode
1031 global-semantic-decoration-mode
1032 global-semantic-stickyfunc-mode
1033 global-semantic-idle-completions-mode
1034 global-semantic-idle-scheduler-mode
1035 global-semanticdb-minor-mode
1036 global-semantic-idle-summary-mode
1037 global-semantic-mru-bookmark-mode
)
1038 "List of auxiliary minor modes in the Semantic package.")
1041 (defcustom semantic-default-submodes
1042 '(global-semantic-idle-scheduler-mode global-semanticdb-minor-mode
)
1043 "List of auxiliary Semantic minor modes enabled by `semantic-mode'.
1044 The possible elements of this list include the following:
1046 `global-semanticdb-minor-mode' - Maintain tag database.
1047 `global-semantic-idle-scheduler-mode' - Reparse buffer when idle.
1048 `global-semantic-idle-summary-mode' - Show summary of tag at point.
1049 `global-semantic-idle-completions-mode' - Show completions when idle.
1050 `global-semantic-decoration-mode' - Additional tag decorations.
1051 `global-semantic-highlight-func-mode' - Highlight the current tag.
1052 `global-semantic-stickyfunc-mode' - Show current fun in header line.
1053 `global-semantic-mru-bookmark-mode' - Provide `switch-to-buffer'-like
1054 keybinding for tag names."
1056 :type
`(set ,@(mapcar (lambda (c) (list 'const c
))
1057 semantic-submode-list
)))
1060 (define-minor-mode semantic-mode
1061 "Toggle Semantic mode.
1062 With ARG, turn Semantic mode on if ARG is positive, off otherwise.
1064 In Semantic mode, Emacs parses the buffers you visit for their
1065 semantic content. This information is used by a variety of
1066 auxiliary minor modes, listed in `semantic-default-submodes';
1067 all the minor modes in this list are also enabled when you enable
1070 \\{semantic-mode-map}"
1074 ;; Turn on Semantic mode
1076 ;; Enable all the global auxiliary minor modes in
1077 ;; `semantic-submode-list'.
1078 (dolist (mode semantic-submode-list
)
1079 (if (memq mode semantic-default-submodes
)
1081 (unless semantic-load-system-cache-loaded
1082 (setq semantic-load-system-cache-loaded t
)
1083 (when (and (boundp 'semanticdb-default-system-save-directory
)
1084 (stringp semanticdb-default-system-save-directory
)
1085 (file-exists-p semanticdb-default-system-save-directory
))
1086 (require 'semantic
/db-ebrowse
)
1087 (semanticdb-load-ebrowse-caches)))
1088 (add-hook 'mode-local-init-hook
'semantic-new-buffer-fcn
)
1089 ;; Add semantic-ia-complete-symbol to
1090 ;; completion-at-point-functions, so that it is run from
1092 (add-hook 'completion-at-point-functions
1093 'semantic-completion-at-point-function
)
1095 (define-key cedet-menu-map
[cedet-menu-separator
] '("--")))
1096 (dolist (b (buffer-list))
1097 (with-current-buffer b
1098 (semantic-new-buffer-fcn))))
1099 ;; Disable all Semantic features.
1100 (remove-hook 'mode-local-init-hook
'semantic-new-buffer-fcn
)
1101 (remove-hook 'completion-at-point-functions
1102 'semantic-completion-at-point-function
)
1103 (define-key cedet-menu-map
[cedet-menu-separator
] nil
)
1104 (define-key cedet-menu-map
[semantic-options-separator
] nil
)
1105 ;; FIXME: handle semanticdb-load-ebrowse-caches
1106 (dolist (mode semantic-submode-list
)
1107 (if (and (boundp mode
) (eval mode
))
1108 (funcall mode -
1)))))
1110 (defun semantic-completion-at-point-function ()
1111 'semantic-ia-complete-symbol
)
1113 ;;; Autoload some functions that are not in semantic/loaddefs
1115 (autoload 'global-semantic-idle-completions-mode
"semantic/idle"
1116 "Toggle global use of `semantic-idle-completions-mode'.
1117 If ARG is positive, enable, if it is negative, disable.
1118 If ARG is nil, then toggle." t nil
)
1120 (autoload 'semantic-idle-completions-mode
"semantic/idle"
1121 "Display a list of possible completions in a tooltip.
1123 This is a minor mode which performs actions during idle time.
1124 With prefix argument ARG, turn on if positive, otherwise off. The
1125 minor mode can be turned on only if semantic feature is available and
1126 the current buffer was set up for parsing. Return non-nil if the
1127 minor mode is enabled." t nil
)
1129 (autoload 'global-semantic-idle-summary-mode
"semantic/idle"
1130 "Toggle global use of `semantic-idle-summary-mode'.
1131 If ARG is positive, enable, if it is negative, disable.
1132 If ARG is nil, then toggle." t nil
)
1134 (autoload 'semantic-idle-summary-mode
"semantic/idle"
1135 "Display a tag summary of the lexical token under the cursor.
1136 Call `semantic-idle-summary-current-symbol-info' for getting the
1137 current tag to display information.
1139 This is a minor mode which performs actions during idle time.
1140 With prefix argument ARG, turn on if positive, otherwise off. The
1141 minor mode can be turned on only if semantic feature is available and
1142 the current buffer was set up for parsing. Return non-nil if the
1143 minor mode is enabled." t nil
)
1145 (autoload 'srecode-template-setup-parser
"srecode/srecode-template"
1146 "Set up buffer for parsing SRecode template files." t nil
)
1150 ;; Semantic-util is a part of the semantic API. Include it last
1151 ;; because it depends on semantic.
1152 (require 'semantic
/util
)
1154 ;; (require 'semantic/load)
1156 ;; arch-tag: 31583e10-6508-41a9-be40-f83d0ae0a4ed
1157 ;;; semantic.el ends here