1 ;;; semantic.el --- Semantic buffer evaluator.
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;;; 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; API for providing the semantic content of a buffer.
28 ;; The semantic API provides an interface to a series of different parser
29 ;; implementations. Each parser outputs a parse tree in a similar format
30 ;; designed to handle typical functional and object oriented languages.
33 (require 'semantic
/tag
)
34 (require 'semantic
/lex
)
36 (defvar semantic-version
"2.0pre7"
37 "Current version of Semantic.")
39 (declare-function inversion-test
"inversion")
40 (declare-function semanticdb-load-ebrowse-caches
"semantic/db-ebrowse")
42 (defun semantic-require-version (major minor
&optional beta
)
43 "Non-nil if this version of semantic does not satisfy a specific version.
46 (MAJOR MINOR &optional BETA)
48 Values MAJOR and MINOR must be integers. BETA can be an integer, or
49 excluded if a released version is required.
51 It is assumed that if the current version is newer than that specified,
52 everything passes. Exceptions occur when known incompatibilities are
55 (inversion-test 'semantic
56 (concat major
"." minor
57 (when beta
(concat "beta" beta
)))))
59 (defgroup semantic nil
60 "Parser Generator and parser framework."
63 (defgroup semantic-faces nil
64 "Faces used for Semantic enabled tools."
67 (require 'semantic
/fw
)
72 ;;; Variables and Configuration
74 (defvar semantic--parse-table nil
75 "Variable that defines how to parse top level items in a buffer.
76 This variable is for internal use only, and its content depends on the
77 external parser used.")
78 (make-variable-buffer-local 'semantic--parse-table
)
79 (semantic-varalias-obsolete 'semantic-toplevel-bovine-table
80 'semantic--parse-table
)
82 (defvar semantic-symbol-
>name-assoc-list
84 (variable .
"Variables")
85 (function .
"Functions")
86 (include .
"Dependencies")
87 (package .
"Provides"))
88 "Association between symbols returned, and a string.
89 The string is used to represent a group of objects of the given type.
90 It is sometimes useful for a language to use a different string
91 in place of the default, even though that language will still
92 return a symbol. For example, Java return's includes, but the
93 string can be replaced with `Imports'.")
94 (make-variable-buffer-local 'semantic-symbol-
>name-assoc-list
)
96 (defvar semantic-symbol-
>name-assoc-list-for-type-parts nil
97 "Like `semantic-symbol->name-assoc-list' for type parts.
98 Some tags that have children (see `semantic-tag-children-compatibility')
99 will want to define the names of classes of tags differently than at
100 the top level. For example, in C++, a Function may be called a
101 Method. In addition, there may be new types of tags that exist only
102 in classes, such as protection labels.")
103 (make-variable-buffer-local 'semantic-symbol-
>name-assoc-list-for-type-parts
)
105 (defvar semantic-case-fold nil
106 "Value for `case-fold-search' when parsing.")
107 (make-variable-buffer-local 'semantic-case-fold
)
109 (defvar semantic-expand-nonterminal nil
110 "Function to call for each nonterminal production.
111 Return a list of non-terminals derived from the first argument, or nil
112 if it does not need to be expanded.
113 Languages with compound definitions should use this function to expand
114 from one compound symbol into several. For example, in C the definition
116 is easily parsed into one tag. This function should take this
117 compound tag and turn it into two tags, one for A, and the other for B.")
118 (make-variable-buffer-local 'semantic-expand-nonterminal
)
120 (defvar semantic--buffer-cache nil
121 "A cache of the fully parsed buffer.
122 If no significant changes have been made (based on the state) then
123 this is returned instead of re-parsing the buffer.
125 DO NOT USE THIS VARIABLE IN PROGRAMS.
127 If you need a tag list, use `semantic-fetch-tags'. If you need the
128 cached values for some reason, chances are you can, add a hook to
129 `semantic-after-toplevel-cache-change-hook'.")
130 (make-variable-buffer-local 'semantic--buffer-cache
)
131 (semantic-varalias-obsolete 'semantic-toplevel-bovine-cache
132 'semantic--buffer-cache
)
134 (defvar semantic-unmatched-syntax-cache nil
135 "A cached copy of unmatched syntax tokens.")
136 (make-variable-buffer-local 'semantic-unmatched-syntax-cache
)
138 (defvar semantic-unmatched-syntax-cache-check nil
139 "Non nil if the unmatched syntax cache is out of date.
140 This is tracked with `semantic-change-function'.")
141 (make-variable-buffer-local 'semantic-unmatched-syntax-cache-check
)
143 (defvar semantic-edits-are-safe nil
144 "When non-nil, modifications do not require a reparse.
145 This prevents tags from being marked dirty, and it prevents top level
146 edits from causing a cache check.
147 Use this when writing programs that could cause a full reparse, but
148 will not change the tag structure, such as adding or updating
149 `top-level' comments.")
151 (defvar semantic-unmatched-syntax-hook nil
152 "Hooks run when semantic detects syntax not matched in a grammar.
153 Each individual piece of syntax (such as a symbol or punctuation
154 character) is called with this hook when it doesn't match in the
155 grammar, and multiple unmatched syntax elements are not grouped
156 together. Each hook is called with one argument, which is a list of
157 syntax tokens created by the semantic lexer. Use the functions
158 `semantic-lex-token-start', `semantic-lex-token-end' and
159 `semantic-lex-token-text' to get information about these tokens. The
160 current buffer is the buffer these tokens are derived from.")
162 (defvar semantic--before-fetch-tags-hook nil
163 "Hooks run before a buffer is parses for tags.
164 It is called before any request for tags is made via the function
165 `semantic-fetch-tags' by an application.
166 If any hook returns a nil value, the cached value is returned
167 immediately, even if it is empty.")
168 (semantic-varalias-obsolete 'semantic-before-toplevel-bovination-hook
169 'semantic--before-fetch-tags-hook
)
171 (defvar semantic-after-toplevel-bovinate-hook nil
172 "Hooks run after a toplevel parse.
173 It is not run if the toplevel parse command is called, and buffer does
174 not need to be fully reparsed.
175 For language specific hooks, make sure you define this as a local hook.
177 This hook should not be used any more.
178 Use `semantic-after-toplevel-cache-change-hook' instead.")
179 (make-obsolete-variable 'semantic-after-toplevel-bovinate-hook nil
)
181 (defvar semantic-after-toplevel-cache-change-hook nil
182 "Hooks run after the buffer tag list has changed.
183 This list will change when a buffer is reparsed, or when the tag list
184 in a buffer is cleared. It is *NOT* called if the current tag list is
187 Hook functions must take one argument, which is the new list of tags
188 associated with this buffer.
190 For language specific hooks, make sure you define this as a local hook.")
192 (defvar semantic-before-toplevel-cache-flush-hook nil
193 "Hooks run before the toplevel tag cache is flushed.
194 For language specific hooks, make sure you define this as a local
195 hook. This hook is called before a corresponding
196 `semantic-after-toplevel-cache-change-hook' which is also called
197 during a flush when the cache is given a new value of nil.")
199 (defcustom semantic-dump-parse nil
200 "When non-nil, dump parsing information."
204 (defvar semantic-parser-name
"LL"
205 "Optional name of the parser used to parse input stream.")
206 (make-variable-buffer-local 'semantic-parser-name
)
208 (defvar semantic--completion-cache nil
209 "Internal variable used by `semantic-complete-symbol'.")
210 (make-variable-buffer-local 'semantic--completion-cache
)
212 ;;; Parse tree state management API
214 (defvar semantic-parse-tree-state
'needs-rebuild
215 "State of the current parse tree.")
216 (make-variable-buffer-local 'semantic-parse-tree-state
)
218 (defmacro semantic-parse-tree-unparseable
()
219 "Indicate that the current buffer is unparseable.
220 It is also true that the parse tree will need either updating or
221 a rebuild. This state will be changed when the user edits the buffer."
222 `(setq semantic-parse-tree-state
'unparseable
))
224 (defmacro semantic-parse-tree-unparseable-p
()
225 "Return non-nil if the current buffer has been marked unparseable."
226 `(eq semantic-parse-tree-state
'unparseable
))
228 (defmacro semantic-parse-tree-set-needs-update
()
229 "Indicate that the current parse tree needs to be updated.
230 The parse tree can be updated by `semantic-parse-changes'."
231 `(setq semantic-parse-tree-state
'needs-update
))
233 (defmacro semantic-parse-tree-needs-update-p
()
234 "Return non-nil if the current parse tree needs to be updated."
235 `(eq semantic-parse-tree-state
'needs-update
))
237 (defmacro semantic-parse-tree-set-needs-rebuild
()
238 "Indicate that the current parse tree needs to be rebuilt.
239 The parse tree must be rebuilt by `semantic-parse-region'."
240 `(setq semantic-parse-tree-state
'needs-rebuild
))
242 (defmacro semantic-parse-tree-needs-rebuild-p
()
243 "Return non-nil if the current parse tree needs to be rebuilt."
244 `(eq semantic-parse-tree-state
'needs-rebuild
))
246 (defmacro semantic-parse-tree-set-up-to-date
()
247 "Indicate that the current parse tree is up to date."
248 `(setq semantic-parse-tree-state nil
))
250 (defmacro semantic-parse-tree-up-to-date-p
()
251 "Return non-nil if the current parse tree is up to date."
252 `(null semantic-parse-tree-state
))
254 ;;; Interfacing with the system
256 (defcustom semantic-inhibit-functions nil
257 "List of functions to call with no arguments before Semantic is setup.
258 If any of these functions returns non-nil, the current buffer is not
259 setup to use Semantic."
263 (defvar semantic-init-hook nil
264 "Hook run when a buffer is initialized with a parsing table.")
266 (defvar semantic-init-mode-hook nil
267 "Hook run when a buffer of a particular mode is initialized.")
268 (make-variable-buffer-local 'semantic-init-mode-hook
)
270 (defvar semantic-init-db-hook nil
271 "Hook run when a buffer is initialized with a parsing table for DBs.
272 This hook is for database functions which intend to swap in a tag table.
273 This guarantees that the DB will go before other modes that require
274 a parse of the buffer.")
276 (semantic-varalias-obsolete 'semantic-init-hooks
278 (semantic-varalias-obsolete 'semantic-init-mode-hooks
279 'semantic-init-mode-hook
)
280 (semantic-varalias-obsolete 'semantic-init-db-hooks
281 'semantic-init-db-hook
)
283 (defvar semantic-new-buffer-fcn-was-run nil
284 "Non nil after `semantic-new-buffer-fcn' has been executed.")
285 (make-variable-buffer-local 'semantic-new-buffer-fcn-was-run
)
287 (defsubst semantic-active-p
()
288 "Return non-nil if the current buffer was set up for parsing."
289 semantic-new-buffer-fcn-was-run
)
291 (defsubst semantic--umatched-syntax-needs-refresh-p
()
292 "Return non-nil if the unmatched syntax cache needs a refresh.
293 That is if it is dirty or if the current parse tree isn't up to date."
294 (or semantic-unmatched-syntax-cache-check
295 (not (semantic-parse-tree-up-to-date-p))))
297 (defun semantic-new-buffer-fcn ()
298 "Setup the current buffer to use Semantic.
299 If the major mode is ready for Semantic, and no
300 `semantic-inhibit-functions' disabled it, the current buffer is setup
301 to use Semantic, and `semantic-init-hook' is run."
302 ;; Do stuff if semantic was activated by a mode hook in this buffer,
303 ;; and not afterwards disabled.
304 (when (and semantic--parse-table
305 (not (semantic-active-p))
306 (not (run-hook-with-args-until-success
307 'semantic-inhibit-functions
)))
308 ;; Make sure that if this buffer is cloned, our tags and overlays
309 ;; don't go along for the ride.
310 (add-hook 'clone-indirect-buffer-hook
'semantic-clear-toplevel-cache
312 ;; Specify that this function has done it's work. At this point
313 ;; we can consider that semantic is active in this buffer.
314 (setq semantic-new-buffer-fcn-was-run t
)
315 ;; Here are some buffer local variables we can initialize ourselves
316 ;; of a mode does not choose to do so.
318 ;; Force this buffer to have its cache refreshed.
319 (semantic-clear-toplevel-cache)
320 ;; Call DB hooks before regular init hooks
321 (run-hooks 'semantic-init-db-hook
)
322 ;; Set up semantic modes
323 (run-hooks 'semantic-init-hook
)
324 ;; Set up major-mode specific semantic modes
325 (run-hooks 'semantic-init-mode-hook
)))
327 (defun semantic-fetch-tags-fast ()
328 "For use in a hook. When only a partial reparse is needed, reparse."
330 (if (semantic-parse-tree-needs-update-p)
331 (semantic-fetch-tags))
333 semantic--buffer-cache
)
338 (condition-case nil
(require 'pp
) (error nil
)))
340 (defvar semantic-edebug nil
341 "When non-nil, activate the interactive parsing debugger.
342 Do not set this yourself. Call `semantic-debug'.")
344 (defun semantic-elapsed-time (start end
)
345 "Copied from elp.el. Was elp-elapsed-time.
346 Argument START and END bound the time being calculated."
347 (+ (* (- (car end
) (car start
)) 65536.0)
348 (- (car (cdr end
)) (car (cdr start
)))
349 (/ (- (car (cdr (cdr end
))) (car (cdr (cdr start
)))) 1000000.0)))
351 (defun bovinate (&optional clear
)
352 "Parse the current buffer. Show output in a temp buffer.
353 Optional argument CLEAR will clear the cache before parsing.
354 If CLEAR is negative, it will do a full reparse, and also not display
357 (if clear
(semantic-clear-toplevel-cache))
358 (if (eq clear
'-
) (setq clear -
1))
359 (let* ((start (current-time))
360 (out (semantic-fetch-tags))
361 (end (current-time)))
362 (message "Retrieving tags took %.2f seconds."
363 (semantic-elapsed-time start end
))
364 (when (or (null clear
) (not (listp clear
)))
365 (pop-to-buffer "*Parser Output*")
368 (insert (pp-to-string out
))
369 (goto-char (point-min)))))
371 ;;; Functions of the parser plug-in API
373 ;; Overload these functions to create new types of parsers.
375 (define-overloadable-function semantic-parse-stream
(stream nonterminal
)
376 "Parse STREAM, starting at the first NONTERMINAL rule.
377 For bovine and wisent based parsers, STREAM is from the output of
378 `semantic-lex', and NONTERMINAL is a rule in the apropriate language
380 The default parser table used for bovine or wisent based parsers is
381 `semantic--parse-table'.
383 Must return a list: (STREAM TAGS) where STREAM is the unused elements
384 from STREAM, and TAGS is the list of semantic tags found, usually only
385 one tag is returned with the exception of compound statements")
387 (define-overloadable-function semantic-parse-changes
()
388 "Reparse changes in the current buffer.
389 The list of changes are tracked as a series of overlays in the buffer.
390 When overloading this function, use `semantic-changes-in-region' to
393 (define-overloadable-function semantic-parse-region
394 (start end
&optional nonterminal depth returnonerror
)
395 "Parse the area between START and END, and return any tags found.
396 If END needs to be extended due to a lexical token being too large, it
397 will be silently ignored.
400 NONTERMINAL is the rule to start parsing at.
401 DEPTH specifies the lexical depth to decend for parser that use
402 lexical analysis as their first step.
403 RETURNONERROR specifies that parsing should stop on the first
404 unmatched syntax encountered. When nil, parsing skips the syntax,
405 adding it to the unmatched syntax cache.
407 Must return a list of semantic tags wich have been cooked
408 \(repositioned properly) but which DO NOT HAVE OVERLAYS associated
409 with them. When overloading this function, use `semantic--tag-expand'
412 (defun semantic-parse-region-default
413 (start end
&optional nonterminal depth returnonerror
)
414 "Parse the area between START and END, and return any tags found.
415 If END needs to be extended due to a lexical token being too large, it
416 will be silently ignored.
418 NONTERMINAL is the rule to start parsing at if it is known.
419 DEPTH specifies the lexical depth to scan.
420 RETURNONERROR specifies that parsing should end when encountering
421 unterminated syntax."
422 (when (or (null semantic--parse-table
) (eq semantic--parse-table t
))
423 ;; If there is no table, or it was set to t, then we are here by
424 ;; some other mistake. Do not throw an error deep in the parser.
425 (error "No support found to parse buffer %S" (buffer-name)))
428 (when (or (< end start
) (> end
(point-max)))
429 (error "Invalid parse region bounds %S, %S" start end
))
431 (semantic-repeat-parse-whole-stream
432 (or (cdr (assq start semantic-lex-block-streams
))
433 (semantic-lex start end depth
))
434 nonterminal returnonerror
))))
436 ;;; Parsing functions
438 (defun semantic-set-unmatched-syntax-cache (unmatched-syntax)
439 "Set the unmatched syntax cache.
440 Argument UNMATCHED-SYNTAX is the syntax to set into the cache."
441 ;; This function is not actually called by the main parse loop.
442 ;; This is intended for use by semanticdb.
443 (setq semantic-unmatched-syntax-cache unmatched-syntax
444 semantic-unmatched-syntax-cache-check nil
)
445 ;; Refresh the display of unmatched syntax tokens if enabled
446 (run-hook-with-args 'semantic-unmatched-syntax-hook
447 semantic-unmatched-syntax-cache
))
449 (defun semantic-clear-unmatched-syntax-cache ()
450 "Clear the cache of unmatched syntax tokens."
451 (setq semantic-unmatched-syntax-cache nil
452 semantic-unmatched-syntax-cache-check t
))
454 (defun semantic-unmatched-syntax-tokens ()
455 "Return the list of unmatched syntax tokens."
456 ;; If the cache need refresh then do a full re-parse.
457 (if (semantic--umatched-syntax-needs-refresh-p)
458 ;; To avoid a recursive call, temporarily disable
459 ;; `semantic-unmatched-syntax-hook'.
460 (let (semantic-unmatched-syntax-hook)
463 (semantic-clear-toplevel-cache)
464 (semantic-fetch-tags))
466 (message "semantic-unmatched-syntax-tokens:\
467 parsing of buffer canceled"))
469 semantic-unmatched-syntax-cache
)
471 (defun semantic-clear-toplevel-cache ()
472 "Clear the toplevel tag cache for the current buffer.
473 Clearing the cache will force a complete reparse next time a tag list
476 (run-hooks 'semantic-before-toplevel-cache-flush-hook
)
477 (setq semantic--buffer-cache nil
)
478 (semantic-clear-unmatched-syntax-cache)
479 (semantic-clear-parser-warnings)
480 ;; Nuke all semantic overlays. This is faster than deleting based
481 ;; on our data structure.
482 (let ((l (semantic-overlay-lists)))
483 (mapc 'semantic-delete-overlay-maybe
(car l
))
484 (mapc 'semantic-delete-overlay-maybe
(cdr l
))
486 (semantic-parse-tree-set-needs-rebuild)
487 ;; Remove this hook which tracks if a buffer is up to date or not.
488 (remove-hook 'after-change-functions
'semantic-change-function t
)
489 ;; Old model. Delete someday.
490 ;;(run-hooks 'semantic-after-toplevel-bovinate-hook)
492 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
493 semantic--buffer-cache
)
495 (setq semantic--completion-cache nil
))
497 (defvar semantic-bovinate-nonterminal-check-obarray
)
499 (defun semantic--set-buffer-cache (tagtable)
500 "Set the toplevel cache cache to TAGTABLE."
501 (setq semantic--buffer-cache tagtable
502 semantic-unmatched-syntax-cache-check nil
)
503 ;; This is specific to the bovine parser.
504 (set (make-local-variable 'semantic-bovinate-nonterminal-check-obarray
)
506 (semantic-parse-tree-set-up-to-date)
507 (semantic-make-local-hook 'after-change-functions
)
508 (add-hook 'after-change-functions
'semantic-change-function nil t
)
509 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
510 semantic--buffer-cache
)
511 (setq semantic--completion-cache nil
)
512 ;; Refresh the display of unmatched syntax tokens if enabled
513 (run-hook-with-args 'semantic-unmatched-syntax-hook
514 semantic-unmatched-syntax-cache
)
515 ;; Old Semantic 1.3 hook API. Maybe useful forever?
516 (run-hooks 'semantic-after-toplevel-bovinate-hook
)
519 (defvar semantic-working-type
'percent
520 "*The type of working message to use when parsing.
521 'percent means we are doing a linear parse through the buffer.
522 'dynamic means we are reparsing specific tags.")
523 (semantic-varalias-obsolete 'semantic-bovination-working-type
524 'semantic-working-type
)
526 (defvar semantic-minimum-working-buffer-size
(* 1024 5)
527 "*The minimum size of a buffer before working messages are displayed.
528 Buffers smaller than will parse silently.
529 Bufferse larger than this will display the working progress bar.")
531 (defsubst semantic-parser-working-message
(&optional arg
)
532 "Return the message string displayed while parsing.
533 If optional argument ARG is non-nil it is appended to the message
536 (if arg
(format " %s" arg
))
537 (if semantic-parser-name
(format " (%s)" semantic-parser-name
))
540 ;;; Application Parser Entry Points
542 ;; The best way to call the parser from programs is via
543 ;; `semantic-fetch-tags'. This, in turn, uses other internal
544 ;; API functions which plug-in parsers can take advantage of.
546 (defun semantic-fetch-tags ()
547 "Fetch semantic tags from the current buffer.
548 If the buffer cache is up to date, return that.
549 If the buffer cache is out of date, attempt an incremental reparse.
550 If the buffer has not been parsed before, or if the incremental reparse
551 fails, then parse the entire buffer.
552 If a lexcial error had been previously discovered and the buffer
553 was marked unparseable, then do nothing, and return the cache."
555 ;; Is this a semantic enabled buffer?
557 ;; Application hooks say the buffer is safe for parsing
558 (run-hook-with-args-until-failure
559 'semantic-before-toplevel-bovination-hook
)
560 (run-hook-with-args-until-failure
561 'semantic--before-fetch-tags-hook
)
562 ;; If the buffer was previously marked unparseable,
563 ;; then don't waste our time.
564 (not (semantic-parse-tree-unparseable-p))
565 ;; The parse tree actually needs to be refreshed
566 (not (semantic-parse-tree-up-to-date-p))
568 (let* ((gc-cons-threshold (max gc-cons-threshold
10000000))
569 (semantic-lex-block-streams nil
)
574 ;;;; Try the incremental parser to do a fast update.
575 ((semantic-parse-tree-needs-update-p)
576 (setq res
(semantic-parse-changes))
577 (if (semantic-parse-tree-needs-rebuild-p)
578 ;; If the partial reparse fails, jump to a full reparse.
579 (semantic-fetch-tags)
580 ;; Clear the cache of unmatched syntax tokens
584 ;; Move this into the incremental parser. This is a bug.
586 (semantic-clear-unmatched-syntax-cache)
587 (run-hook-with-args ;; Let hooks know the updated tags
588 'semantic-after-partial-cache-change-hook res
))
589 (setq semantic--completion-cache nil
))
591 ;;;; Parse the whole system.
592 ((semantic-parse-tree-needs-rebuild-p)
593 ;; Use Emacs' built-in progress-reporter
594 (let ((semantic--progress-reporter
595 (and (>= (point-max) semantic-minimum-working-buffer-size
)
596 (eq semantic-working-type
'percent
)
597 (make-progress-reporter
598 (semantic-parser-working-message (buffer-name))
600 (setq res
(semantic-parse-region (point-min) (point-max)))
601 (if semantic--progress-reporter
602 (progress-reporter-done semantic--progress-reporter
)))
604 ;; Clear the caches when we see there were no errors.
605 ;; But preserve the unmatched syntax cache and warnings!
606 (let (semantic-unmatched-syntax-cache
607 semantic-unmatched-syntax-cache-check
608 semantic-parser-warnings
)
609 (semantic-clear-toplevel-cache))
610 ;; Set up the new overlays
611 (semantic--tag-link-list-to-buffer res
)
612 ;; Set up the cache with the new results
613 (semantic--set-buffer-cache res
)
616 ;; Always return the current parse tree.
617 semantic--buffer-cache
)
619 (defun semantic-refresh-tags-safe ()
620 "Refreshes the current buffer's tags safely.
622 Return non-nil if the refresh was successful.
623 Return nil if there is some sort of syntax error preventing a reparse.
625 Does nothing if the current buffer doesn't need reparsing."
627 ;; These checks actually occur in `semantic-fetch-tags', but if we
628 ;; do them here, then all the bovination hooks are not run, and
629 ;; we save lots of time.
631 ;; If the buffer was previously marked unparseable,
632 ;; then don't waste our time.
633 ((semantic-parse-tree-unparseable-p)
635 ;; The parse tree is already ok.
636 ((semantic-parse-tree-up-to-date-p)
639 (let* ((inhibit-quit nil
)
644 ;; Perform the parsing.
646 (when (semantic-lex-catch-errors safe-refresh
647 (save-excursion (semantic-fetch-tags))
649 ;; If we are here, it is because the lexical step failed,
650 ;; proably due to unterminated lists or something like that.
652 ;; We do nothing, and just wait for the next idle timer
653 ;; to go off. In the meantime, remember this, and make sure
654 ;; no other idle services can get executed.
655 (setq lexically-safe nil
))
658 ;; Return if we are lexically safe
661 (defun semantic-bovinate-toplevel (&optional ignored
)
662 "Backward Compatibility Function."
663 (semantic-fetch-tags))
664 (make-obsolete 'semantic-bovinate-toplevel
'semantic-fetch-tags
)
666 ;; Another approach is to let Emacs call the parser on idle time, when
667 ;; needed, use `semantic-fetch-available-tags' to only retrieve
668 ;; available tags, and setup the `semantic-after-*-hook' hooks to
669 ;; synchronize with new tags when they become available.
671 (defsubst semantic-fetch-available-tags
()
672 "Fetch available semantic tags from the current buffer.
673 That is, return tags currently in the cache without parsing the
675 Parse operations happen asynchronously when needed on Emacs idle time.
676 Use the `semantic-after-toplevel-cache-change-hook' and
677 `semantic-after-partial-cache-change-hook' hooks to synchronize with
678 new tags when they become available."
679 semantic--buffer-cache
)
681 ;;; Iterative parser helper function
683 ;; Iterative parsers are better than rule-based iterative functions
684 ;; in that they can handle obscure errors more cleanly.
686 ;; `semantic-repeat-parse-whole-stream' abstracts this action for
687 ;; other parser centric routines.
689 (defun semantic-repeat-parse-whole-stream
690 (stream nonterm
&optional returnonerror
)
691 "Iteratively parse the entire stream STREAM starting with NONTERM.
692 Optional argument RETURNONERROR indicates that the parser should exit
693 with the current results on a parse error.
694 This function returns semantic tags without overlays."
696 (case-fold-search semantic-case-fold
)
699 (setq nontermsym
(semantic-parse-stream stream nonterm
)
700 tag
(car (cdr nontermsym
)))
702 (error "Parse error @ %d" (car (cdr (car stream
)))))
703 (if (eq (car nontermsym
) stream
)
704 (error "Parser error: Infinite loop?"))
709 ;; Set the 'reparse-symbol property to
710 ;; NONTERM unless it was already setup
712 (or (semantic--tag-get-property
714 (semantic--tag-put-property
715 tag
'reparse-symbol nonterm
))
717 (semantic--tag-expand tag
))
718 result
(append tag result
))
719 ;; No error in this case, a purposeful nil means don't
724 ;; The current item in the stream didn't match, so add it to
725 ;; the list of syntax items which didn't match.
726 (setq semantic-unmatched-syntax-cache
727 (cons (car stream
) semantic-unmatched-syntax-cache
))
729 ;; Designated to ignore.
730 (setq stream
(car nontermsym
))
732 ;; Use Emacs' built-in progress reporter:
733 (and (boundp 'semantic--progress-reporter
)
734 semantic--progress-reporter
735 (eq semantic-working-type
'percent
)
736 (progress-reporter-update
737 semantic--progress-reporter
738 (/ (* 100 (semantic-lex-token-start (car stream
)))
742 ;;; Parsing Warnings:
744 ;; Parsing a buffer may result in non-critical things that we should
745 ;; alert the user to without interrupting the normal flow.
747 ;; Any parser can use this API to provide a list of warnings during a
748 ;; parse which a user may want to investigate.
749 (defvar semantic-parser-warnings nil
750 "A list of parser warnings since the last full reparse.")
751 (make-variable-buffer-local 'semantic-parser-warnings
)
753 (defun semantic-clear-parser-warnings ()
754 "Clear the current list of parser warnings for this buffer."
755 (setq semantic-parser-warnings nil
))
757 (defun semantic-push-parser-warning (warning start end
)
758 "Add a parser WARNING that covers text from START to END."
759 (setq semantic-parser-warnings
760 (cons (cons warning
(cons start end
))
761 semantic-parser-warnings
)))
763 (defun semantic-dump-parser-warnings ()
764 "Dump any parser warnings."
766 (if semantic-parser-warnings
767 (let ((pw semantic-parser-warnings
))
768 (pop-to-buffer "*Parser Warnings*")
771 (insert (pp-to-string pw
))
772 (goto-char (point-min)))
773 (message "No parser warnings.")))
779 ;; Semantic 1.x parser action helper functions, used by some parsers.
780 ;; Please move away from these functions, and try using semantic 2.x
781 ;; interfaces instead.
783 (defsubst semantic-bovinate-region-until-error
784 (start end nonterm
&optional depth
)
785 "NOTE: Use `semantic-parse-region' instead.
787 Bovinate between START and END starting with NONTERM.
788 Optional DEPTH specifies how many levels of parenthesis to enter.
789 This command will parse until an error is encountered, and return
790 the list of everything found until that moment.
791 This is meant for finding variable definitions at the beginning of
792 code blocks in methods. If `bovine-inner-scope' can also support
793 commands, use `semantic-bovinate-from-nonterminal-full'."
794 (semantic-parse-region start end nonterm depth t
))
795 (make-obsolete 'semantic-bovinate-region-until-error
796 'semantic-parse-region
)
798 (defsubst semantic-bovinate-from-nonterminal
799 (start end nonterm
&optional depth length
)
800 "Bovinate from within a nonterminal lambda from START to END.
801 Argument NONTERM is the nonterminal symbol to start with.
802 Optional argument DEPTH is the depth of lists to dive into. When used
803 in a `lambda' of a MATCH-LIST, there is no need to include a START and
805 Optional argument LENGTH specifies we are only interested in LENGTH
807 (car-safe (cdr (semantic-parse-stream
808 (semantic-lex start end
(or depth
1) length
)
811 (defsubst semantic-bovinate-from-nonterminal-full
812 (start end nonterm
&optional depth
)
813 "NOTE: Use `semantic-parse-region' instead.
815 Bovinate from within a nonterminal lambda from START to END.
816 Iterates until all the space between START and END is exhausted.
817 Argument NONTERM is the nonterminal symbol to start with.
818 If NONTERM is nil, use `bovine-block-toplevel'.
819 Optional argument DEPTH is the depth of lists to dive into.
820 When used in a `lambda' of a MATCH-LIST, there is no need to include
821 a START and END part."
822 (semantic-parse-region start end nonterm
(or depth
1)))
823 (make-obsolete 'semantic-bovinate-from-nonterminal-full
824 'semantic-parse-region
)
828 (defun semantic-force-refresh ()
829 "Force a full refresh of the current buffer's tags.
830 Throw away all the old tags, and recreate the tag database."
832 (semantic-clear-toplevel-cache)
833 (semantic-fetch-tags)
834 (message "Buffer reparsed."))
836 (defvar semantic-mode-map
837 (let ((map (make-sparse-keymap)))
839 ;; (define-key km "f" 'senator-search-set-tag-class-filter)
840 ;; (define-key km "i" 'senator-isearch-toggle-semantic-mode)
841 (define-key map
"\C-c,j" 'semantic-complete-jump-local
)
842 (define-key map
"\C-c,J" 'semantic-complete-jump
)
843 (define-key map
"\C-c,g" 'semantic-symref-symbol
)
844 (define-key map
"\C-c,G" 'semantic-symref
)
845 (define-key map
"\C-c,p" 'senator-previous-tag
)
846 (define-key map
"\C-c,n" 'senator-next-tag
)
847 (define-key map
"\C-c,u" 'senator-go-to-up-reference
)
848 (define-key map
"\C-c, " 'semantic-complete-analyze-inline
)
849 (define-key map
"\C-c,\C-w" 'senator-kill-tag
)
850 (define-key map
"\C-c,\M-w" 'senator-copy-tag
)
851 (define-key map
"\C-c,\C-y" 'senator-yank-tag
)
852 (define-key map
"\C-c,r" 'senator-copy-tag-to-register
)
853 (define-key map
[?\C-c ?
, up
] 'senator-transpose-tags-up
)
854 (define-key map
[?\C-c ?
, down
] 'senator-transpose-tags-down
)
855 (define-key map
"\C-c,l" 'semantic-analyze-possible-completions
)
856 ;; This hack avoids showing the CEDET menu twice if ede-minor-mode
857 ;; and Semantic are both enabled. Is there a better way?
858 (define-key map
[menu-bar cedet-menu
]
859 (list 'menu-item
"Development" cedet-menu-map
860 :enable
(quote (not (bound-and-true-p global-ede-mode
)))))
861 ;; (define-key km "-" 'senator-fold-tag)
862 ;; (define-key km "+" 'senator-unfold-tag)
865 ;; Activate the Semantic items in cedet-menu-map
866 (let ((navigate-menu (make-sparse-keymap "Navigate Tags"))
867 (edit-menu (make-sparse-keymap "Edit Tags")))
869 ;; Edit Tags submenu:
870 (define-key edit-menu
[semantic-analyze-possible-completions
]
871 '(menu-item "List Completions" semantic-analyze-possible-completions
872 :help
"Display a list of completions for the tag at point"))
873 (define-key edit-menu
[semantic-complete-analyze-inline
]
874 '(menu-item "Complete Tag Inline" semantic-complete-analyze-inline
875 :help
"Display inline completion for the tag at point"))
876 (define-key edit-menu
[semantic-completion-separator
]
878 (define-key edit-menu
[senator-transpose-tags-down
]
879 '(menu-item "Transpose Tags Down" senator-transpose-tags-down
880 :active
(semantic-current-tag)
881 :help
"Transpose the current tag and the next tag"))
882 (define-key edit-menu
[senator-transpose-tags-up
]
883 '(menu-item "Transpose Tags Up" senator-transpose-tags-up
884 :active
(semantic-current-tag)
885 :help
"Transpose the current tag and the previous tag"))
886 (define-key edit-menu
[semantic-edit-separator
]
888 (define-key edit-menu
[senator-yank-tag
]
889 '(menu-item "Yank Tag" senator-yank-tag
890 :active
(not (ring-empty-p senator-tag-ring
))
891 :help
"Yank the head of the tag ring into the buffer"))
892 (define-key edit-menu
[senator-copy-tag-to-register
]
893 '(menu-item "Copy Tag To Register" senator-copy-tag-to-register
894 :active
(semantic-current-tag)
895 :help
"Yank the head of the tag ring into the buffer"))
896 (define-key edit-menu
[senator-copy-tag
]
897 '(menu-item "Copy Tag" senator-copy-tag
898 :active
(semantic-current-tag)
899 :help
"Copy the current tag to the tag ring"))
900 (define-key edit-menu
[senator-kill-tag
]
901 '(menu-item "Kill Tag" senator-kill-tag
902 :active
(semantic-current-tag)
903 :help
"Kill the current tag, and copy it to the tag ring"))
905 ;; Navigate Tags submenu:
906 (define-key navigate-menu
[senator-narrow-to-defun
]
907 '(menu-item "Narrow to Tag" senator-narrow-to-defun
908 :active
(semantic-current-tag)
909 :help
"Narrow the buffer to the bounds of the current tag"))
910 (define-key navigate-menu
[semantic-narrow-to-defun-separator
]
912 (define-key navigate-menu
[semantic-symref-symbol
]
913 '(menu-item "Find Tag References..." semantic-symref-symbol
914 :help
"Read a tag and list the references to it"))
915 (define-key navigate-menu
[semantic-complete-jump
]
916 '(menu-item "Find Tag Globally..." semantic-complete-jump
917 :help
"Read a tag name and find it in the current project"))
918 (define-key navigate-menu
[semantic-complete-jump-local
]
919 '(menu-item "Find Tag in This Buffer..." semantic-complete-jump-local
920 :help
"Read a tag name and find it in this buffer"))
921 (define-key navigate-menu
[semantic-navigation-separator
]
923 (define-key navigate-menu
[senator-go-to-up-reference
]
924 '(menu-item "Parent Tag" senator-go-to-up-reference
925 :help
"Navigate up one reference by tag."))
926 (define-key navigate-menu
[senator-next-tag
]
927 '(menu-item "Next Tag" senator-next-tag
928 :help
"Go to the next tag"))
929 (define-key navigate-menu
[senator-previous-tag
]
930 '(menu-item "Previous Tag" senator-previous-tag
931 :help
"Go to the previous tag"))
933 ;; Top level menu items:
934 (define-key cedet-menu-map
[semantic-force-refresh
]
935 '(menu-item "Reparse Buffer" semantic-force-refresh
936 :help
"Force a full reparse of the current buffer."
937 :visible semantic-mode
))
938 (define-key cedet-menu-map
[semantic-edit-menu
]
939 `(menu-item "Edit Tags" ,edit-menu
940 :visible semantic-mode
))
941 (define-key cedet-menu-map
[navigate-menu
]
942 `(menu-item "Navigate Tags" ,navigate-menu
943 :visible semantic-mode
))
944 (define-key cedet-menu-map
[semantic-options-separator
]
946 (define-key cedet-menu-map
[global-semantic-highlight-func-mode
]
947 '(menu-item "Highlight Current Function" global-semantic-highlight-func-mode
948 :help
"Highlight the tag at point"
949 :visible semantic-mode
950 :button
(:toggle . global-semantic-highlight-func-mode
)))
951 (define-key cedet-menu-map
[global-semantic-decoration-mode
]
952 '(menu-item "Decorate Tags" global-semantic-decoration-mode
953 :help
"Decorate tags based on tag attributes"
954 :visible semantic-mode
955 :button
(:toggle .
(bound-and-true-p
956 global-semantic-decoration-mode
))))
957 (define-key cedet-menu-map
[global-semantic-idle-completions-mode
]
958 '(menu-item "Show Tag Completions" global-semantic-idle-completions-mode
959 :help
"Show tag completions when idle"
960 :visible semantic-mode
961 :button
(:toggle . global-semantic-idle-completions-mode
)))
962 (define-key cedet-menu-map
[global-semantic-idle-summary-mode
]
963 '(menu-item "Show Tag Summaries" global-semantic-idle-summary-mode
964 :help
"Show tag summaries when idle"
965 :visible semantic-mode
966 :button
(:toggle . global-semantic-idle-summary-mode
)))
967 (define-key cedet-menu-map
[global-semanticdb-minor-mode
]
968 '(menu-item "Semantic Database" global-semanticdb-minor-mode
969 :help
"Store tag information in a database"
970 :visible semantic-mode
971 :button
(:toggle . global-semanticdb-minor-mode
)))
972 (define-key cedet-menu-map
[global-semantic-idle-scheduler-mode
]
973 '(menu-item "Reparse When Idle" global-semantic-idle-scheduler-mode
974 :help
"Keep a buffer's parse tree up to date when idle"
975 :visible semantic-mode
976 :button
(:toggle . global-semantic-idle-scheduler-mode
)))
977 (define-key cedet-menu-map
[ede-menu-separator
] 'undefined
)
978 (define-key cedet-menu-map
[cedet-menu-separator
] 'undefined
)
979 (define-key cedet-menu-map
[semantic-menu-separator
] '("--")))
981 ;; The `semantic-mode' command, in conjuction with the
982 ;; `semantic-default-submodes' variable, toggles Semantic's various
983 ;; auxilliary minor modes.
985 (defvar semantic-load-system-cache-loaded nil
986 "Non nil when the Semantic system caches have been loaded.
987 Prevent this load system from loading files in twice.")
989 (defconst semantic-submode-list
990 '(global-semantic-highlight-func-mode
991 global-semantic-decoration-mode
992 global-semantic-stickyfunc-mode
993 global-semantic-idle-completions-mode
994 global-semantic-idle-scheduler-mode
995 global-semanticdb-minor-mode
996 global-semantic-idle-summary-mode
997 global-semantic-mru-bookmark-mode
)
998 "List of auxilliary minor modes in the Semantic package.")
1001 (defcustom semantic-default-submodes
1002 '(global-semantic-idle-scheduler-mode global-semanticdb-minor-mode
)
1003 "List of auxilliary Semantic minor modes enabled by `semantic-mode'.
1004 The possible elements of this list include the following:
1006 `semantic-highlight-func-mode' - Highlight the current tag.
1007 `semantic-decoration-mode' - Decorate tags based on various attributes.
1008 `semantic-stickyfunc-mode' - Track current function in the header-line.
1009 `semantic-idle-completions-mode' - Provide smart symbol completion
1010 automatically when idle.
1011 `semantic-idle-scheduler-mode' - Keep a buffer's parse tree up to date.
1012 `semanticdb-minor-mode' - Store tags when a buffer is not in memory.
1013 `semantic-idle-summary-mode' - Show a summary for the code at point.
1014 `semantic-mru-bookmark-mode' - Provide `switch-to-buffer'-like
1015 keybinding for tag names."
1017 :type
`(set ,@(mapcar (lambda (c) (list 'const c
))
1018 semantic-submode-list
)))
1021 (define-minor-mode semantic-mode
1022 "Toggle Semantic mode.
1023 With ARG, turn Semantic mode on if ARG is positive, off otherwise.
1025 In Semantic mode, Emacs parses the buffers you visit for their
1026 semantic content. This information is used by a variety of
1027 auxilliary minor modes, listed in `semantic-default-submodes';
1028 all the minor modes in this list are also enabled when you enable
1031 \\{semantic-mode-map}"
1035 ;; Turn on Semantic mode
1037 ;; Enable all the global auxilliary minor modes in
1038 ;; `semantic-submode-list'.
1039 (dolist (mode semantic-submode-list
)
1040 (if (memq mode semantic-default-submodes
)
1042 (unless semantic-load-system-cache-loaded
1043 (setq semantic-load-system-cache-loaded t
)
1044 (when (and (boundp 'semanticdb-default-system-save-directory
)
1045 (stringp semanticdb-default-system-save-directory
)
1046 (file-exists-p semanticdb-default-system-save-directory
))
1047 (require 'semantic
/db-ebrowse
)
1048 (semanticdb-load-ebrowse-caches)))
1049 (add-hook 'mode-local-init-hook
'semantic-new-buffer-fcn
)
1050 ;; Add mode-local hooks
1051 (add-hook 'javascript-mode-hook
'wisent-javascript-setup-parser
)
1052 (add-hook 'ecmascript-mode-hook
'wisent-javascript-setup-parser
)
1053 (add-hook 'java-mode-hook
'wisent-java-default-setup
)
1054 (add-hook 'scheme-mode-hook
'semantic-default-scheme-setup
)
1055 (add-hook 'makefile-mode-hook
'semantic-default-make-setup
)
1056 (add-hook 'c-mode-hook
'semantic-default-c-setup
)
1057 (add-hook 'c
++-mode-hook
'semantic-default-c-setup
)
1058 (add-hook 'html-mode-hook
'semantic-default-html-setup
))
1059 ;; Disable all Semantic features.
1060 (remove-hook 'mode-local-init-hook
'semantic-new-buffer-fcn
)
1061 (remove-hook 'javascript-mode-hook
'wisent-javascript-setup-parser
)
1062 (remove-hook 'ecmascript-mode-hook
'wisent-javascript-setup-parser
)
1063 (remove-hook 'java-mode-hook
'wisent-java-default-setup
)
1064 (remove-hook 'scheme-mode-hook
'semantic-default-scheme-setup
)
1065 (remove-hook 'makefile-mode-hook
'semantic-default-make-setup
)
1066 (remove-hook 'c-mode-hook
'semantic-default-c-setup
)
1067 (remove-hook 'c
++-mode-hook
'semantic-default-c-setup
)
1068 (remove-hook 'html-mode-hook
'semantic-default-html-setup
)
1070 ;; FIXME: handle semanticdb-load-ebrowse-caches
1071 (dolist (mode semantic-submode-list
)
1072 (if (and (boundp mode
) (eval mode
))
1073 (funcall mode -
1)))))
1075 ;;; Autoload some functions that are not in semantic/loaddefs
1077 (autoload 'global-semantic-idle-completions-mode
"semantic/idle"
1078 "Toggle global use of `semantic-idle-completions-mode'.
1079 If ARG is positive, enable, if it is negative, disable.
1080 If ARG is nil, then toggle." t nil
)
1082 (autoload 'semantic-idle-completions-mode
"semantic/idle"
1083 "Display a list of possible completions in a tooltip.
1085 This is a minor mode which performs actions during idle time.
1086 With prefix argument ARG, turn on if positive, otherwise off. The
1087 minor mode can be turned on only if semantic feature is available and
1088 the current buffer was set up for parsing. Return non-nil if the
1089 minor mode is enabled." t nil
)
1091 (autoload 'global-semantic-idle-summary-mode
"semantic/idle"
1092 "Toggle global use of `semantic-idle-summary-mode'.
1093 If ARG is positive, enable, if it is negative, disable.
1094 If ARG is nil, then toggle." t nil
)
1096 (autoload 'semantic-idle-summary-mode
"semantic/idle"
1097 "Display a tag summary of the lexical token under the cursor.
1098 Call `semantic-idle-summary-current-symbol-info' for getting the
1099 current tag to display information.
1101 This is a minor mode which performs actions during idle time.
1102 With prefix argument ARG, turn on if positive, otherwise off. The
1103 minor mode can be turned on only if semantic feature is available and
1104 the current buffer was set up for parsing. Return non-nil if the
1105 minor mode is enabled." t nil
)
1109 ;; Semantic-util is a part of the semantic API. Include it last
1110 ;; because it depends on semantic.
1111 (require 'semantic
/util
)
1113 ;; (require 'semantic/load)
1115 ;;; semantic.el ends here