]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic.el
9848d5d79650bfcdb637db87c0887efdf14cd7f5
[gnu-emacs] / lisp / cedet / semantic.el
1 ;;; semantic.el --- Semantic buffer evaluator.
2
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;;; 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: syntax
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25 ;;
26 ;; API for providing the semantic content of a buffer.
27 ;;
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.
31
32 (require 'assoc)
33 (require 'semantic/tag)
34 (require 'semantic/lex)
35
36 (defvar semantic-version "2.0pre7"
37 "Current version of Semantic.")
38
39 (declare-function inversion-test "inversion")
40 (declare-function semanticdb-load-ebrowse-caches "semantic/db-ebrowse")
41
42 (defun semantic-require-version (major minor &optional beta)
43 "Non-nil if this version of semantic does not satisfy a specific version.
44 Arguments can be:
45
46 (MAJOR MINOR &optional BETA)
47
48 Values MAJOR and MINOR must be integers. BETA can be an integer, or
49 excluded if a released version is required.
50
51 It is assumed that if the current version is newer than that specified,
52 everything passes. Exceptions occur when known incompatibilities are
53 introduced."
54 (require 'inversion)
55 (inversion-test 'semantic
56 (concat major "." minor
57 (when beta (concat "beta" beta)))))
58
59 (defgroup semantic nil
60 "Parser Generator and parser framework."
61 :group 'lisp)
62
63 (defgroup semantic-faces nil
64 "Faces used for Semantic enabled tools."
65 :group 'semantic)
66
67 (require 'semantic/fw)
68
69 ;;; Code:
70 ;;
71
72 ;;; Variables and Configuration
73 ;;
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)
81
82 (defvar semantic-symbol->name-assoc-list
83 '((type . "Types")
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)
95
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)
104
105 (defvar semantic-case-fold nil
106 "Value for `case-fold-search' when parsing.")
107 (make-variable-buffer-local 'semantic-case-fold)
108
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
115 int a, b;
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)
119
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.
124
125 DO NOT USE THIS VARIABLE IN PROGRAMS.
126
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)
133
134 (defvar semantic-unmatched-syntax-cache nil
135 "A cached copy of unmatched syntax tokens.")
136 (make-variable-buffer-local 'semantic-unmatched-syntax-cache)
137
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)
142
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.")
150
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.")
161
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)
170
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.
176
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)
180
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
185 partially reparsed.
186
187 Hook functions must take one argument, which is the new list of tags
188 associated with this buffer.
189
190 For language specific hooks, make sure you define this as a local hook.")
191
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.")
198
199 (defcustom semantic-dump-parse nil
200 "When non-nil, dump parsing information."
201 :group 'semantic
202 :type 'boolean)
203
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)
207 \f
208 ;;; Parse tree state management API
209 ;;
210 (defvar semantic-parse-tree-state 'needs-rebuild
211 "State of the current parse tree.")
212 (make-variable-buffer-local 'semantic-parse-tree-state)
213
214 (defmacro semantic-parse-tree-unparseable ()
215 "Indicate that the current buffer is unparseable.
216 It is also true that the parse tree will need either updating or
217 a rebuild. This state will be changed when the user edits the buffer."
218 `(setq semantic-parse-tree-state 'unparseable))
219
220 (defmacro semantic-parse-tree-unparseable-p ()
221 "Return non-nil if the current buffer has been marked unparseable."
222 `(eq semantic-parse-tree-state 'unparseable))
223
224 (defmacro semantic-parse-tree-set-needs-update ()
225 "Indicate that the current parse tree needs to be updated.
226 The parse tree can be updated by `semantic-parse-changes'."
227 `(setq semantic-parse-tree-state 'needs-update))
228
229 (defmacro semantic-parse-tree-needs-update-p ()
230 "Return non-nil if the current parse tree needs to be updated."
231 `(eq semantic-parse-tree-state 'needs-update))
232
233 (defmacro semantic-parse-tree-set-needs-rebuild ()
234 "Indicate that the current parse tree needs to be rebuilt.
235 The parse tree must be rebuilt by `semantic-parse-region'."
236 `(setq semantic-parse-tree-state 'needs-rebuild))
237
238 (defmacro semantic-parse-tree-needs-rebuild-p ()
239 "Return non-nil if the current parse tree needs to be rebuilt."
240 `(eq semantic-parse-tree-state 'needs-rebuild))
241
242 (defmacro semantic-parse-tree-set-up-to-date ()
243 "Indicate that the current parse tree is up to date."
244 `(setq semantic-parse-tree-state nil))
245
246 (defmacro semantic-parse-tree-up-to-date-p ()
247 "Return non-nil if the current parse tree is up to date."
248 `(null semantic-parse-tree-state))
249
250 ;;; Interfacing with the system
251 ;;
252 (defcustom semantic-inhibit-functions nil
253 "List of functions to call with no arguments before Semantic is setup.
254 If any of these functions returns non-nil, the current buffer is not
255 setup to use Semantic."
256 :group 'semantic
257 :type 'hook)
258
259 (defvar semantic-init-hook nil
260 "Hook run when a buffer is initialized with a parsing table.")
261
262 (defvar semantic-init-mode-hook nil
263 "Hook run when a buffer of a particular mode is initialized.")
264 (make-variable-buffer-local 'semantic-init-mode-hook)
265
266 (defvar semantic-init-db-hook nil
267 "Hook run when a buffer is initialized with a parsing table for DBs.
268 This hook is for database functions which intend to swap in a tag table.
269 This guarantees that the DB will go before other modes that require
270 a parse of the buffer.")
271
272 (semantic-varalias-obsolete 'semantic-init-hooks
273 'semantic-init-hook)
274 (semantic-varalias-obsolete 'semantic-init-mode-hooks
275 'semantic-init-mode-hook)
276 (semantic-varalias-obsolete 'semantic-init-db-hooks
277 'semantic-init-db-hook)
278
279 (defvar semantic-new-buffer-fcn-was-run nil
280 "Non nil after `semantic-new-buffer-fcn' has been executed.")
281 (make-variable-buffer-local 'semantic-new-buffer-fcn-was-run)
282
283 (defsubst semantic-active-p ()
284 "Return non-nil if the current buffer was set up for parsing."
285 semantic-new-buffer-fcn-was-run)
286
287 (defsubst semantic--umatched-syntax-needs-refresh-p ()
288 "Return non-nil if the unmatched syntax cache needs a refresh.
289 That is if it is dirty or if the current parse tree isn't up to date."
290 (or semantic-unmatched-syntax-cache-check
291 (not (semantic-parse-tree-up-to-date-p))))
292
293 (defun semantic-new-buffer-fcn ()
294 "Setup the current buffer to use Semantic.
295 If the major mode is ready for Semantic, and no
296 `semantic-inhibit-functions' disabled it, the current buffer is setup
297 to use Semantic, and `semantic-init-hook' is run."
298 ;; Do stuff if semantic was activated by a mode hook in this buffer,
299 ;; and not afterwards disabled.
300 (when (and semantic--parse-table
301 (not (semantic-active-p))
302 (not (run-hook-with-args-until-success
303 'semantic-inhibit-functions)))
304 ;; Make sure that if this buffer is cloned, our tags and overlays
305 ;; don't go along for the ride.
306 (add-hook 'clone-indirect-buffer-hook 'semantic-clear-toplevel-cache
307 nil t)
308 ;; Specify that this function has done it's work. At this point
309 ;; we can consider that semantic is active in this buffer.
310 (setq semantic-new-buffer-fcn-was-run t)
311 ;; Here are some buffer local variables we can initialize ourselves
312 ;; of a mode does not choose to do so.
313 (semantic-lex-init)
314 ;; Force this buffer to have its cache refreshed.
315 (semantic-clear-toplevel-cache)
316 ;; Call DB hooks before regular init hooks
317 (run-hooks 'semantic-init-db-hook)
318 ;; Set up semantic modes
319 (run-hooks 'semantic-init-hook)
320 ;; Set up major-mode specific semantic modes
321 (run-hooks 'semantic-init-mode-hook)))
322
323 (defun semantic-fetch-tags-fast ()
324 "For use in a hook. When only a partial reparse is needed, reparse."
325 (condition-case nil
326 (if (semantic-parse-tree-needs-update-p)
327 (semantic-fetch-tags))
328 (error nil))
329 semantic--buffer-cache)
330 \f
331 ;;; Parsing Commands
332 ;;
333 (eval-when-compile
334 (condition-case nil (require 'pp) (error nil)))
335
336 (defvar semantic-edebug nil
337 "When non-nil, activate the interactive parsing debugger.
338 Do not set this yourself. Call `semantic-debug'.")
339
340 (defun semantic-elapsed-time (start end)
341 "Copied from elp.el. Was elp-elapsed-time.
342 Argument START and END bound the time being calculated."
343 (+ (* (- (car end) (car start)) 65536.0)
344 (- (car (cdr end)) (car (cdr start)))
345 (/ (- (car (cdr (cdr end))) (car (cdr (cdr start)))) 1000000.0)))
346
347 (defun bovinate (&optional clear)
348 "Parse the current buffer. Show output in a temp buffer.
349 Optional argument CLEAR will clear the cache before parsing.
350 If CLEAR is negative, it will do a full reparse, and also not display
351 the output buffer."
352 (interactive "P")
353 (if clear (semantic-clear-toplevel-cache))
354 (if (eq clear '-) (setq clear -1))
355 (let* ((start (current-time))
356 (out (semantic-fetch-tags))
357 (end (current-time)))
358 (message "Retrieving tags took %.2f seconds."
359 (semantic-elapsed-time start end))
360 (when (or (null clear) (not (listp clear)))
361 (pop-to-buffer "*Parser Output*")
362 (require 'pp)
363 (erase-buffer)
364 (insert (pp-to-string out))
365 (goto-char (point-min)))))
366 \f
367 ;;; Functions of the parser plug-in API
368 ;;
369 ;; Overload these functions to create new types of parsers.
370 ;;
371 (define-overloadable-function semantic-parse-stream (stream nonterminal)
372 "Parse STREAM, starting at the first NONTERMINAL rule.
373 For bovine and wisent based parsers, STREAM is from the output of
374 `semantic-lex', and NONTERMINAL is a rule in the apropriate language
375 specific rules file.
376 The default parser table used for bovine or wisent based parsers is
377 `semantic--parse-table'.
378
379 Must return a list: (STREAM TAGS) where STREAM is the unused elements
380 from STREAM, and TAGS is the list of semantic tags found, usually only
381 one tag is returned with the exception of compound statements")
382
383 (define-overloadable-function semantic-parse-changes ()
384 "Reparse changes in the current buffer.
385 The list of changes are tracked as a series of overlays in the buffer.
386 When overloading this function, use `semantic-changes-in-region' to
387 analyze.")
388
389 (define-overloadable-function semantic-parse-region
390 (start end &optional nonterminal depth returnonerror)
391 "Parse the area between START and END, and return any tags found.
392 If END needs to be extended due to a lexical token being too large, it
393 will be silently ignored.
394
395 Optional arguments:
396 NONTERMINAL is the rule to start parsing at.
397 DEPTH specifies the lexical depth to decend for parser that use
398 lexical analysis as their first step.
399 RETURNONERROR specifies that parsing should stop on the first
400 unmatched syntax encountered. When nil, parsing skips the syntax,
401 adding it to the unmatched syntax cache.
402
403 Must return a list of semantic tags wich have been cooked
404 \(repositioned properly) but which DO NOT HAVE OVERLAYS associated
405 with them. When overloading this function, use `semantic--tag-expand'
406 to cook raw tags.")
407
408 (defun semantic-parse-region-default
409 (start end &optional nonterminal depth returnonerror)
410 "Parse the area between START and END, and return any tags found.
411 If END needs to be extended due to a lexical token being too large, it
412 will be silently ignored.
413 Optional arguments:
414 NONTERMINAL is the rule to start parsing at if it is known.
415 DEPTH specifies the lexical depth to scan.
416 RETURNONERROR specifies that parsing should end when encountering
417 unterminated syntax."
418 (when (or (null semantic--parse-table) (eq semantic--parse-table t))
419 ;; If there is no table, or it was set to t, then we are here by
420 ;; some other mistake. Do not throw an error deep in the parser.
421 (error "No support found to parse buffer %S" (buffer-name)))
422 (save-restriction
423 (widen)
424 (when (or (< end start) (> end (point-max)))
425 (error "Invalid parse region bounds %S, %S" start end))
426 (nreverse
427 (semantic-repeat-parse-whole-stream
428 (or (cdr (assq start semantic-lex-block-streams))
429 (semantic-lex start end depth))
430 nonterminal returnonerror))))
431 \f
432 ;;; Parsing functions
433 ;;
434 (defun semantic-set-unmatched-syntax-cache (unmatched-syntax)
435 "Set the unmatched syntax cache.
436 Argument UNMATCHED-SYNTAX is the syntax to set into the cache."
437 ;; This function is not actually called by the main parse loop.
438 ;; This is intended for use by semanticdb.
439 (setq semantic-unmatched-syntax-cache unmatched-syntax
440 semantic-unmatched-syntax-cache-check nil)
441 ;; Refresh the display of unmatched syntax tokens if enabled
442 (run-hook-with-args 'semantic-unmatched-syntax-hook
443 semantic-unmatched-syntax-cache))
444
445 (defun semantic-clear-unmatched-syntax-cache ()
446 "Clear the cache of unmatched syntax tokens."
447 (setq semantic-unmatched-syntax-cache nil
448 semantic-unmatched-syntax-cache-check t))
449
450 (defun semantic-unmatched-syntax-tokens ()
451 "Return the list of unmatched syntax tokens."
452 ;; If the cache need refresh then do a full re-parse.
453 (if (semantic--umatched-syntax-needs-refresh-p)
454 ;; To avoid a recursive call, temporarily disable
455 ;; `semantic-unmatched-syntax-hook'.
456 (let (semantic-unmatched-syntax-hook)
457 (condition-case nil
458 (progn
459 (semantic-clear-toplevel-cache)
460 (semantic-fetch-tags))
461 (quit
462 (message "semantic-unmatched-syntax-tokens:\
463 parsing of buffer canceled"))
464 )))
465 semantic-unmatched-syntax-cache)
466
467 (defun semantic-clear-toplevel-cache ()
468 "Clear the toplevel tag cache for the current buffer.
469 Clearing the cache will force a complete reparse next time a tag list
470 is requested."
471 (interactive)
472 (run-hooks 'semantic-before-toplevel-cache-flush-hook)
473 (setq semantic--buffer-cache nil)
474 (semantic-clear-unmatched-syntax-cache)
475 (semantic-clear-parser-warnings)
476 ;; Nuke all semantic overlays. This is faster than deleting based
477 ;; on our data structure.
478 (let ((l (semantic-overlay-lists)))
479 (mapc 'semantic-delete-overlay-maybe (car l))
480 (mapc 'semantic-delete-overlay-maybe (cdr l))
481 )
482 (semantic-parse-tree-set-needs-rebuild)
483 ;; Remove this hook which tracks if a buffer is up to date or not.
484 (remove-hook 'after-change-functions 'semantic-change-function t)
485 ;; Old model. Delete someday.
486 ;;(run-hooks 'semantic-after-toplevel-bovinate-hook)
487
488 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
489 semantic--buffer-cache)
490 )
491
492 (defvar semantic-bovinate-nonterminal-check-obarray)
493
494 (defun semantic--set-buffer-cache (tagtable)
495 "Set the toplevel cache cache to TAGTABLE."
496 (setq semantic--buffer-cache tagtable
497 semantic-unmatched-syntax-cache-check nil)
498 ;; This is specific to the bovine parser.
499 (set (make-local-variable 'semantic-bovinate-nonterminal-check-obarray)
500 nil)
501 (semantic-parse-tree-set-up-to-date)
502 (semantic-make-local-hook 'after-change-functions)
503 (add-hook 'after-change-functions 'semantic-change-function nil t)
504 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
505 semantic--buffer-cache)
506 ;; Refresh the display of unmatched syntax tokens if enabled
507 (run-hook-with-args 'semantic-unmatched-syntax-hook
508 semantic-unmatched-syntax-cache)
509 ;; Old Semantic 1.3 hook API. Maybe useful forever?
510 (run-hooks 'semantic-after-toplevel-bovinate-hook)
511 )
512
513 (defvar semantic-working-type 'percent
514 "*The type of working message to use when parsing.
515 'percent means we are doing a linear parse through the buffer.
516 'dynamic means we are reparsing specific tags.")
517 (semantic-varalias-obsolete 'semantic-bovination-working-type
518 'semantic-working-type)
519
520 (defvar semantic-minimum-working-buffer-size (* 1024 5)
521 "*The minimum size of a buffer before working messages are displayed.
522 Buffers smaller than will parse silently.
523 Bufferse larger than this will display the working progress bar.")
524
525 (defsubst semantic-parser-working-message (&optional arg)
526 "Return the message string displayed while parsing.
527 If optional argument ARG is non-nil it is appended to the message
528 string."
529 (concat "Parsing"
530 (if arg (format " %s" arg))
531 (if semantic-parser-name (format " (%s)" semantic-parser-name))
532 "..."))
533 \f
534 ;;; Application Parser Entry Points
535 ;;
536 ;; The best way to call the parser from programs is via
537 ;; `semantic-fetch-tags'. This, in turn, uses other internal
538 ;; API functions which plug-in parsers can take advantage of.
539
540 (defun semantic-fetch-tags ()
541 "Fetch semantic tags from the current buffer.
542 If the buffer cache is up to date, return that.
543 If the buffer cache is out of date, attempt an incremental reparse.
544 If the buffer has not been parsed before, or if the incremental reparse
545 fails, then parse the entire buffer.
546 If a lexcial error had been previously discovered and the buffer
547 was marked unparseable, then do nothing, and return the cache."
548 (and
549 ;; Is this a semantic enabled buffer?
550 (semantic-active-p)
551 ;; Application hooks say the buffer is safe for parsing
552 (run-hook-with-args-until-failure
553 'semantic-before-toplevel-bovination-hook)
554 (run-hook-with-args-until-failure
555 'semantic--before-fetch-tags-hook)
556 ;; If the buffer was previously marked unparseable,
557 ;; then don't waste our time.
558 (not (semantic-parse-tree-unparseable-p))
559 ;; The parse tree actually needs to be refreshed
560 (not (semantic-parse-tree-up-to-date-p))
561 ;; So do it!
562 (let* ((gc-cons-threshold (max gc-cons-threshold 10000000))
563 (semantic-lex-block-streams nil)
564 (res nil))
565 (garbage-collect)
566 (cond
567
568 ;;;; Try the incremental parser to do a fast update.
569 ((semantic-parse-tree-needs-update-p)
570 (setq res (semantic-parse-changes))
571 (if (semantic-parse-tree-needs-rebuild-p)
572 ;; If the partial reparse fails, jump to a full reparse.
573 (semantic-fetch-tags)
574 ;; Clear the cache of unmatched syntax tokens
575 ;;
576 ;; NOTE TO SELF:
577 ;;
578 ;; Move this into the incremental parser. This is a bug.
579 ;;
580 (semantic-clear-unmatched-syntax-cache)
581 (run-hook-with-args ;; Let hooks know the updated tags
582 'semantic-after-partial-cache-change-hook res))
583 )
584
585 ;;;; Parse the whole system.
586 ((semantic-parse-tree-needs-rebuild-p)
587 ;; Use Emacs' built-in progress-reporter
588 (let ((semantic--progress-reporter
589 (and (>= (point-max) semantic-minimum-working-buffer-size)
590 (eq semantic-working-type 'percent)
591 (make-progress-reporter
592 (semantic-parser-working-message (buffer-name))
593 0 100))))
594 (setq res (semantic-parse-region (point-min) (point-max)))
595 (if semantic--progress-reporter
596 (progress-reporter-done semantic--progress-reporter)))
597
598 ;; Clear the caches when we see there were no errors.
599 ;; But preserve the unmatched syntax cache and warnings!
600 (let (semantic-unmatched-syntax-cache
601 semantic-unmatched-syntax-cache-check
602 semantic-parser-warnings)
603 (semantic-clear-toplevel-cache))
604 ;; Set up the new overlays
605 (semantic--tag-link-list-to-buffer res)
606 ;; Set up the cache with the new results
607 (semantic--set-buffer-cache res)
608 ))))
609
610 ;; Always return the current parse tree.
611 semantic--buffer-cache)
612
613 (defun semantic-refresh-tags-safe ()
614 "Refreshes the current buffer's tags safely.
615
616 Return non-nil if the refresh was successful.
617 Return nil if there is some sort of syntax error preventing a reparse.
618
619 Does nothing if the current buffer doesn't need reparsing."
620
621 ;; These checks actually occur in `semantic-fetch-tags', but if we
622 ;; do them here, then all the bovination hooks are not run, and
623 ;; we save lots of time.
624 (cond
625 ;; If the buffer was previously marked unparseable,
626 ;; then don't waste our time.
627 ((semantic-parse-tree-unparseable-p)
628 nil)
629 ;; The parse tree is already ok.
630 ((semantic-parse-tree-up-to-date-p)
631 t)
632 (t
633 (let* ((inhibit-quit nil)
634 (lexically-safe t)
635 )
636
637 (unwind-protect
638 ;; Perform the parsing.
639 (progn
640 (when (semantic-lex-catch-errors safe-refresh
641 (save-excursion (semantic-fetch-tags))
642 nil)
643 ;; If we are here, it is because the lexical step failed,
644 ;; proably due to unterminated lists or something like that.
645
646 ;; We do nothing, and just wait for the next idle timer
647 ;; to go off. In the meantime, remember this, and make sure
648 ;; no other idle services can get executed.
649 (setq lexically-safe nil))
650 )
651 )
652 ;; Return if we are lexically safe
653 lexically-safe))))
654
655 (defun semantic-bovinate-toplevel (&optional ignored)
656 "Backward Compatibility Function."
657 (semantic-fetch-tags))
658 (make-obsolete 'semantic-bovinate-toplevel 'semantic-fetch-tags)
659
660 ;; Another approach is to let Emacs call the parser on idle time, when
661 ;; needed, use `semantic-fetch-available-tags' to only retrieve
662 ;; available tags, and setup the `semantic-after-*-hook' hooks to
663 ;; synchronize with new tags when they become available.
664
665 (defsubst semantic-fetch-available-tags ()
666 "Fetch available semantic tags from the current buffer.
667 That is, return tags currently in the cache without parsing the
668 current buffer.
669 Parse operations happen asynchronously when needed on Emacs idle time.
670 Use the `semantic-after-toplevel-cache-change-hook' and
671 `semantic-after-partial-cache-change-hook' hooks to synchronize with
672 new tags when they become available."
673 semantic--buffer-cache)
674 \f
675 ;;; Iterative parser helper function
676 ;;
677 ;; Iterative parsers are better than rule-based iterative functions
678 ;; in that they can handle obscure errors more cleanly.
679 ;;
680 ;; `semantic-repeat-parse-whole-stream' abstracts this action for
681 ;; other parser centric routines.
682 ;;
683 (defun semantic-repeat-parse-whole-stream
684 (stream nonterm &optional returnonerror)
685 "Iteratively parse the entire stream STREAM starting with NONTERM.
686 Optional argument RETURNONERROR indicates that the parser should exit
687 with the current results on a parse error.
688 This function returns semantic tags without overlays."
689 (let ((result nil)
690 (case-fold-search semantic-case-fold)
691 nontermsym tag)
692 (while stream
693 (setq nontermsym (semantic-parse-stream stream nonterm)
694 tag (car (cdr nontermsym)))
695 (if (not nontermsym)
696 (error "Parse error @ %d" (car (cdr (car stream)))))
697 (if (eq (car nontermsym) stream)
698 (error "Parser error: Infinite loop?"))
699 (if tag
700 (if (car tag)
701 (setq tag (mapcar
702 #'(lambda (tag)
703 ;; Set the 'reparse-symbol property to
704 ;; NONTERM unless it was already setup
705 ;; by a tag expander
706 (or (semantic--tag-get-property
707 tag 'reparse-symbol)
708 (semantic--tag-put-property
709 tag 'reparse-symbol nonterm))
710 tag)
711 (semantic--tag-expand tag))
712 result (append tag result))
713 ;; No error in this case, a purposeful nil means don't
714 ;; store anything.
715 )
716 (if returnonerror
717 (setq stream nil)
718 ;; The current item in the stream didn't match, so add it to
719 ;; the list of syntax items which didn't match.
720 (setq semantic-unmatched-syntax-cache
721 (cons (car stream) semantic-unmatched-syntax-cache))
722 ))
723 ;; Designated to ignore.
724 (setq stream (car nontermsym))
725 (if stream
726 ;; Use Emacs' built-in progress reporter:
727 (and (boundp 'semantic--progress-reporter)
728 semantic--progress-reporter
729 (eq semantic-working-type 'percent)
730 (progress-reporter-update
731 semantic--progress-reporter
732 (/ (* 100 (semantic-lex-token-start (car stream)))
733 (point-max))))))
734 result))
735 \f
736 ;;; Parsing Warnings:
737 ;;
738 ;; Parsing a buffer may result in non-critical things that we should
739 ;; alert the user to without interrupting the normal flow.
740 ;;
741 ;; Any parser can use this API to provide a list of warnings during a
742 ;; parse which a user may want to investigate.
743 (defvar semantic-parser-warnings nil
744 "A list of parser warnings since the last full reparse.")
745 (make-variable-buffer-local 'semantic-parser-warnings)
746
747 (defun semantic-clear-parser-warnings ()
748 "Clear the current list of parser warnings for this buffer."
749 (setq semantic-parser-warnings nil))
750
751 (defun semantic-push-parser-warning (warning start end)
752 "Add a parser WARNING that covers text from START to END."
753 (setq semantic-parser-warnings
754 (cons (cons warning (cons start end))
755 semantic-parser-warnings)))
756
757 (defun semantic-dump-parser-warnings ()
758 "Dump any parser warnings."
759 (interactive)
760 (if semantic-parser-warnings
761 (let ((pw semantic-parser-warnings))
762 (pop-to-buffer "*Parser Warnings*")
763 (require 'pp)
764 (erase-buffer)
765 (insert (pp-to-string pw))
766 (goto-char (point-min)))
767 (message "No parser warnings.")))
768
769
770 \f
771 ;;; Compatibility:
772 ;;
773 ;; Semantic 1.x parser action helper functions, used by some parsers.
774 ;; Please move away from these functions, and try using semantic 2.x
775 ;; interfaces instead.
776 ;;
777 (defsubst semantic-bovinate-region-until-error
778 (start end nonterm &optional depth)
779 "NOTE: Use `semantic-parse-region' instead.
780
781 Bovinate between START and END starting with NONTERM.
782 Optional DEPTH specifies how many levels of parenthesis to enter.
783 This command will parse until an error is encountered, and return
784 the list of everything found until that moment.
785 This is meant for finding variable definitions at the beginning of
786 code blocks in methods. If `bovine-inner-scope' can also support
787 commands, use `semantic-bovinate-from-nonterminal-full'."
788 (semantic-parse-region start end nonterm depth t))
789 (make-obsolete 'semantic-bovinate-region-until-error
790 'semantic-parse-region)
791
792 (defsubst semantic-bovinate-from-nonterminal
793 (start end nonterm &optional depth length)
794 "Bovinate from within a nonterminal lambda from START to END.
795 Argument NONTERM is the nonterminal symbol to start with.
796 Optional argument DEPTH is the depth of lists to dive into. When used
797 in a `lambda' of a MATCH-LIST, there is no need to include a START and
798 END part.
799 Optional argument LENGTH specifies we are only interested in LENGTH
800 tokens."
801 (car-safe (cdr (semantic-parse-stream
802 (semantic-lex start end (or depth 1) length)
803 nonterm))))
804
805 (defsubst semantic-bovinate-from-nonterminal-full
806 (start end nonterm &optional depth)
807 "NOTE: Use `semantic-parse-region' instead.
808
809 Bovinate from within a nonterminal lambda from START to END.
810 Iterates until all the space between START and END is exhausted.
811 Argument NONTERM is the nonterminal symbol to start with.
812 If NONTERM is nil, use `bovine-block-toplevel'.
813 Optional argument DEPTH is the depth of lists to dive into.
814 When used in a `lambda' of a MATCH-LIST, there is no need to include
815 a START and END part."
816 (semantic-parse-region start end nonterm (or depth 1)))
817 (make-obsolete 'semantic-bovinate-from-nonterminal-full
818 'semantic-parse-region)
819
820 ;;; User interface
821
822 ;; The `semantic-mode' command, in conjuction with the
823 ;; `semantic-default-submodes' variable, are used to collectively
824 ;; toggle Semantic's various auxilliary minor modes.
825
826 (defvar semantic-load-system-cache-loaded nil
827 "Non nil when the Semantic system caches have been loaded.
828 Prevent this load system from loading files in twice.")
829
830 (defconst semantic-submode-list
831 '(global-semantic-highlight-func-mode
832 global-semantic-decoration-mode
833 global-semantic-stickyfunc-mode
834 global-semantic-idle-completions-mode
835 global-semantic-idle-scheduler-mode
836 global-semanticdb-minor-mode
837 global-semantic-idle-summary-mode
838 global-semantic-mru-bookmark-mode)
839 "List of auxilliary minor modes in the Semantic package.")
840
841 ;;;###autoload
842 (defcustom semantic-default-submodes
843 '(global-semantic-idle-scheduler-mode global-semanticdb-minor-mode)
844 "List of auxilliary Semantic minor modes enabled by `semantic-mode'.
845 The possible elements of this list include the following:
846
847 `semantic-highlight-func-mode' - Highlight the current tag.
848 `semantic-decoration-mode' - Decorate tags based on various attributes.
849 `semantic-stickyfunc-mode' - Track current function in the header-line.
850 `semantic-idle-completions-mode' - Provide smart symbol completion
851 automatically when idle.
852 `semantic-idle-scheduler-mode' - Keep a buffer's parse tree up to date.
853 `semanticdb-minor-mode' - Store tags when a buffer is not in memory.
854 `semantic-idle-summary-mode' - Show a summary for the code at point.
855 `semantic-mru-bookmark-mode' - Provide `switch-to-buffer'-like
856 keybinding for tag names."
857 :group 'semantic
858 :type `(set ,@(mapcar (lambda (c) (list 'const c))
859 semantic-submode-list)))
860
861 ;;;###autoload
862 (define-minor-mode semantic-mode
863 "Toggle Semantic mode.
864 With ARG, turn Semantic mode on if ARG is positive, off otherwise.
865
866 In Semantic mode, Emacs parses the buffers you visit for their
867 semantic content. This information is used by a variety of
868 auxilliary minor modes, listed in `semantic-default-submodes';
869 all the minor modes in this list are also enabled when you enable
870 Semantic mode."
871 :group 'semantic
872 (if semantic-mode
873 ;; Turn on Semantic mode
874 (progn
875 (dolist (mode semantic-submode-list)
876 (if (memq mode semantic-default-submodes)
877 (funcall mode 1)))
878 (unless semantic-load-system-cache-loaded
879 (setq semantic-load-system-cache-loaded t)
880 (when (and (boundp 'semanticdb-default-system-save-directory)
881 (stringp semanticdb-default-system-save-directory)
882 (file-exists-p semanticdb-default-system-save-directory))
883 (require 'semantic/db-ebrowse)
884 (semanticdb-load-ebrowse-caches)))
885 (add-hook 'mode-local-init-hook 'semantic-new-buffer-fcn)
886 ;; Add mode-local hooks
887 (add-hook 'javascript-mode-hook 'wisent-javascript-setup-parser)
888 (add-hook 'ecmascript-mode-hook 'wisent-javascript-setup-parser)
889 (add-hook 'java-mode-hook 'wisent-java-default-setup)
890 (add-hook 'scheme-mode-hook 'semantic-default-scheme-setup)
891 (add-hook 'makefile-mode-hook 'semantic-default-make-setup)
892 (add-hook 'c-mode-hook 'semantic-default-c-setup)
893 (add-hook 'c++-mode-hook 'semantic-default-c-setup)
894 (add-hook 'html-mode-hook 'semantic-default-html-setup))
895 ;; Disable all Semantic features.
896 (remove-hook 'mode-local-init-hook 'semantic-new-buffer-fcn)
897 (remove-hook 'javascript-mode-hook 'wisent-javascript-setup-parser)
898 (remove-hook 'ecmascript-mode-hook 'wisent-javascript-setup-parser)
899 (remove-hook 'java-mode-hook 'wisent-java-default-setup)
900 (remove-hook 'scheme-mode-hook 'semantic-default-scheme-setup)
901 (remove-hook 'makefile-mode-hook 'semantic-default-make-setup)
902 (remove-hook 'c-mode-hook 'semantic-default-c-setup)
903 (remove-hook 'c++-mode-hook 'semantic-default-c-setup)
904 (remove-hook 'html-mode-hook 'semantic-default-html-setup)
905
906 ;; FIXME: handle semanticdb-load-ebrowse-caches
907
908 (dolist (mode semantic-submode-list)
909 (if (and (boundp mode) (eval mode))
910 (funcall mode -1)))))
911
912 ;;; Autoload some functions that are not in semantic/loaddefs
913
914 (autoload 'global-semantic-idle-completions-mode "semantic/idle"
915 "Toggle global use of `semantic-idle-completions-mode'.
916 If ARG is positive, enable, if it is negative, disable.
917 If ARG is nil, then toggle." t nil)
918
919 (autoload 'semantic-idle-completions-mode "semantic/idle"
920 "Display a list of possible completions in a tooltip.
921
922 This is a minor mode which performs actions during idle time.
923 With prefix argument ARG, turn on if positive, otherwise off. The
924 minor mode can be turned on only if semantic feature is available and
925 the current buffer was set up for parsing. Return non-nil if the
926 minor mode is enabled." t nil)
927
928 (autoload 'global-semantic-idle-summary-mode "semantic/idle"
929 "Toggle global use of `semantic-idle-summary-mode'.
930 If ARG is positive, enable, if it is negative, disable.
931 If ARG is nil, then toggle." t nil)
932
933 (autoload 'semantic-idle-summary-mode "semantic/idle"
934 "Display a tag summary of the lexical token under the cursor.
935 Call `semantic-idle-summary-current-symbol-info' for getting the
936 current tag to display information.
937
938 This is a minor mode which performs actions during idle time.
939 With prefix argument ARG, turn on if positive, otherwise off. The
940 minor mode can be turned on only if semantic feature is available and
941 the current buffer was set up for parsing. Return non-nil if the
942 minor mode is enabled." t nil)
943
944 (provide 'semantic)
945
946 ;; Semantic-util is a part of the semantic API. Include it last
947 ;; because it depends on semantic.
948 (require 'semantic/util)
949
950 ;; (require 'semantic/load)
951
952 ;;; semantic.el ends here