]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic.el
7b7748b56e83ab376317838bfd7cb4df61d217e3
[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 (eval-and-compile
33 ;; Other package depend on this value at compile time via inversion.
34 (defvar semantic-version "2.0pre7"
35 "Current version of Semantic."))
36
37 ;; (require 'working)
38 (require 'assoc)
39 (require 'semantic-tag)
40 (require 'semantic-lex)
41
42 (declare-function inversion-test "inversion")
43
44 (defun semantic-require-version (major minor &optional beta)
45 "Non-nil if this version of semantic does not satisfy a specific version.
46 Arguments can be:
47
48 (MAJOR MINOR &optional BETA)
49
50 Values MAJOR and MINOR must be integers. BETA can be an integer, or
51 excluded if a released version is required.
52
53 It is assumed that if the current version is newer than that specified,
54 everything passes. Exceptions occur when known incompatibilities are
55 introduced."
56 (require 'inversion)
57 (inversion-test 'semantic
58 (concat major "." minor
59 (when beta (concat "beta" beta)))))
60
61 (defgroup semantic nil
62 "Parser Generator and parser framework."
63 :group 'lisp)
64
65 (defgroup semantic-faces nil
66 "Faces used for Semantic enabled tools."
67 :group 'semantic)
68
69 (require 'semantic-fw)
70
71 ;;; Code:
72 ;;
73
74 ;;; Variables and Configuration
75 ;;
76 (defvar semantic--parse-table nil
77 "Variable that defines how to parse top level items in a buffer.
78 This variable is for internal use only, and its content depends on the
79 external parser used.")
80 (make-variable-buffer-local 'semantic--parse-table)
81 (semantic-varalias-obsolete 'semantic-toplevel-bovine-table
82 'semantic--parse-table)
83
84 (defvar semantic-symbol->name-assoc-list
85 '((type . "Types")
86 (variable . "Variables")
87 (function . "Functions")
88 (include . "Dependencies")
89 (package . "Provides"))
90 "Association between symbols returned, and a string.
91 The string is used to represent a group of objects of the given type.
92 It is sometimes useful for a language to use a different string
93 in place of the default, even though that language will still
94 return a symbol. For example, Java return's includes, but the
95 string can be replaced with `Imports'.")
96 (make-variable-buffer-local 'semantic-symbol->name-assoc-list)
97
98 (defvar semantic-symbol->name-assoc-list-for-type-parts nil
99 "Like `semantic-symbol->name-assoc-list' for type parts.
100 Some tags that have children (see `semantic-tag-children-compatibility')
101 will want to define the names of classes of tags differently than at
102 the top level. For example, in C++, a Function may be called a
103 Method. In addition, there may be new types of tags that exist only
104 in classes, such as protection labels.")
105 (make-variable-buffer-local 'semantic-symbol->name-assoc-list-for-type-parts)
106
107 (defvar semantic-case-fold nil
108 "Value for `case-fold-search' when parsing.")
109 (make-variable-buffer-local 'semantic-case-fold)
110
111 (defvar semantic-expand-nonterminal nil
112 "Function to call for each nonterminal production.
113 Return a list of non-terminals derived from the first argument, or nil
114 if it does not need to be expanded.
115 Languages with compound definitions should use this function to expand
116 from one compound symbol into several. For example, in C the definition
117 int a, b;
118 is easily parsed into one tag. This function should take this
119 compound tag and turn it into two tags, one for A, and the other for B.")
120 (make-variable-buffer-local 'semantic-expand-nonterminal)
121
122 (defvar semantic--buffer-cache nil
123 "A cache of the fully parsed buffer.
124 If no significant changes have been made (based on the state) then
125 this is returned instead of re-parsing the buffer.
126
127 DO NOT USE THIS VARIABLE IN PROGRAMS.
128
129 If you need a tag list, use `semantic-fetch-tags'. If you need the
130 cached values for some reason, chances are you can, add a hook to
131 `semantic-after-toplevel-cache-change-hook'.")
132 (make-variable-buffer-local 'semantic--buffer-cache)
133 (semantic-varalias-obsolete 'semantic-toplevel-bovine-cache
134 'semantic--buffer-cache)
135
136 (defvar semantic-unmatched-syntax-cache nil
137 "A cached copy of unmatched syntax tokens.")
138 (make-variable-buffer-local 'semantic-unmatched-syntax-cache)
139
140 (defvar semantic-unmatched-syntax-cache-check nil
141 "Non nil if the unmatched syntax cache is out of date.
142 This is tracked with `semantic-change-function'.")
143 (make-variable-buffer-local 'semantic-unmatched-syntax-cache-check)
144
145 (defvar semantic-edits-are-safe nil
146 "When non-nil, modifications do not require a reparse.
147 This prevents tags from being marked dirty, and it prevents top level
148 edits from causing a cache check.
149 Use this when writing programs that could cause a full reparse, but
150 will not change the tag structure, such as adding or updating
151 `top-level' comments.")
152
153 (defvar semantic-unmatched-syntax-hook nil
154 "Hooks run when semantic detects syntax not matched in a grammar.
155 Each individual piece of syntax (such as a symbol or punctuation
156 character) is called with this hook when it doesn't match in the
157 grammar, and multiple unmatched syntax elements are not grouped
158 together. Each hook is called with one argument, which is a list of
159 syntax tokens created by the semantic lexer. Use the functions
160 `semantic-lex-token-start', `semantic-lex-token-end' and
161 `semantic-lex-token-text' to get information about these tokens. The
162 current buffer is the buffer these tokens are derived from.")
163
164 (defvar semantic--before-fetch-tags-hook nil
165 "Hooks run before a buffer is parses for tags.
166 It is called before any request for tags is made via the function
167 `semantic-fetch-tags' by an application.
168 If any hook returns a nil value, the cached value is returned
169 immediately, even if it is empty.")
170 (semantic-varalias-obsolete 'semantic-before-toplevel-bovination-hook
171 'semantic--before-fetch-tags-hook)
172
173 (defvar semantic-after-toplevel-bovinate-hook nil
174 "Hooks run after a toplevel parse.
175 It is not run if the toplevel parse command is called, and buffer does
176 not need to be fully reparsed.
177 For language specific hooks, make sure you define this as a local hook.
178
179 This hook should not be used any more.
180 Use `semantic-after-toplevel-cache-change-hook' instead.")
181 (make-obsolete-variable 'semantic-after-toplevel-bovinate-hook nil)
182
183 (defvar semantic-after-toplevel-cache-change-hook nil
184 "Hooks run after the buffer tag list has changed.
185 This list will change when a buffer is reparsed, or when the tag list
186 in a buffer is cleared. It is *NOT* called if the current tag list is
187 partially reparsed.
188
189 Hook functions must take one argument, which is the new list of tags
190 associated with this buffer.
191
192 For language specific hooks, make sure you define this as a local hook.")
193
194 (defvar semantic-before-toplevel-cache-flush-hook nil
195 "Hooks run before the toplevel tag cache is flushed.
196 For language specific hooks, make sure you define this as a local
197 hook. This hook is called before a corresponding
198 `semantic-after-toplevel-cache-change-hook' which is also called
199 during a flush when the cache is given a new value of nil.")
200
201 (defcustom semantic-dump-parse nil
202 "When non-nil, dump parsing information."
203 :group 'semantic
204 :type 'boolean)
205
206 (defvar semantic-parser-name "LL"
207 "Optional name of the parser used to parse input stream.")
208 (make-variable-buffer-local 'semantic-parser-name)
209 \f
210 ;;; Parse tree state management API
211 ;;
212 (defvar semantic-parse-tree-state 'needs-rebuild
213 "State of the current parse tree.")
214 (make-variable-buffer-local 'semantic-parse-tree-state)
215
216 (defmacro semantic-parse-tree-unparseable ()
217 "Indicate that the current buffer is unparseable.
218 It is also true that the parse tree will need either updating or
219 a rebuild. This state will be changed when the user edits the buffer."
220 `(setq semantic-parse-tree-state 'unparseable))
221
222 (defmacro semantic-parse-tree-unparseable-p ()
223 "Return non-nil if the current buffer has been marked unparseable."
224 `(eq semantic-parse-tree-state 'unparseable))
225
226 (defmacro semantic-parse-tree-set-needs-update ()
227 "Indicate that the current parse tree needs to be updated.
228 The parse tree can be updated by `semantic-parse-changes'."
229 `(setq semantic-parse-tree-state 'needs-update))
230
231 (defmacro semantic-parse-tree-needs-update-p ()
232 "Return non-nil if the current parse tree needs to be updated."
233 `(eq semantic-parse-tree-state 'needs-update))
234
235 (defmacro semantic-parse-tree-set-needs-rebuild ()
236 "Indicate that the current parse tree needs to be rebuilt.
237 The parse tree must be rebuilt by `semantic-parse-region'."
238 `(setq semantic-parse-tree-state 'needs-rebuild))
239
240 (defmacro semantic-parse-tree-needs-rebuild-p ()
241 "Return non-nil if the current parse tree needs to be rebuilt."
242 `(eq semantic-parse-tree-state 'needs-rebuild))
243
244 (defmacro semantic-parse-tree-set-up-to-date ()
245 "Indicate that the current parse tree is up to date."
246 `(setq semantic-parse-tree-state nil))
247
248 (defmacro semantic-parse-tree-up-to-date-p ()
249 "Return non-nil if the current parse tree is up to date."
250 `(null semantic-parse-tree-state))
251
252 ;;; Interfacing with the system
253 ;;
254 (defcustom semantic-inhibit-functions nil
255 "List of functions to call with no arguments before Semantic is setup.
256 If any of these functions returns non-nil, the current buffer is not
257 setup to use Semantic."
258 :group 'semantic
259 :type 'hook)
260
261 (defvar semantic-init-hooks nil
262 "*Hooks run when a buffer is initialized with a parsing table.")
263
264 (defvar semantic-init-mode-hooks nil
265 "*Hooks run when a buffer of a particular mode is initialized.")
266 (make-variable-buffer-local 'semantic-init-mode-hooks)
267
268 (defvar semantic-init-db-hooks nil
269 "Hooks run when a buffer is initialized with a parsing table for DBs.
270 This hook is for database functions which intend to swap in a tag table.
271 This guarantees that the DB will go before other modes that require
272 a parse of the buffer.")
273
274 (defvar semantic-new-buffer-fcn-was-run nil
275 "Non nil after `semantic-new-buffer-fcn' has been executed.")
276 (make-variable-buffer-local 'semantic-new-buffer-fcn-was-run)
277
278 (defsubst semantic-active-p ()
279 "Return non-nil if the current buffer was set up for parsing."
280 semantic-new-buffer-fcn-was-run)
281
282 (defsubst semantic--umatched-syntax-needs-refresh-p ()
283 "Return non-nil if the unmatched syntax cache needs a refresh.
284 That is if it is dirty or if the current parse tree isn't up to date."
285 (or semantic-unmatched-syntax-cache-check
286 (not (semantic-parse-tree-up-to-date-p))))
287
288 (defun semantic-new-buffer-fcn ()
289 "Setup the current buffer to use Semantic.
290 If the major mode is ready for Semantic, and no
291 `semantic-inhibit-functions' disabled it, the current buffer is setup
292 to use Semantic, and `semantic-init-hook' is run."
293 ;; Do stuff if semantic was activated by a mode hook in this buffer,
294 ;; and not afterwards disabled.
295 (when (and semantic--parse-table
296 (not (semantic-active-p))
297 (not (run-hook-with-args-until-success
298 'semantic-inhibit-functions)))
299 ;; Make sure that if this buffer is cloned, our tags and overlays
300 ;; don't go along for the ride.
301 (add-hook 'clone-indirect-buffer-hook 'semantic-clear-toplevel-cache
302 nil t)
303 ;; Specify that this function has done it's work. At this point
304 ;; we can consider that semantic is active in this buffer.
305 (setq semantic-new-buffer-fcn-was-run t)
306 ;; Here are some buffer local variables we can initialize ourselves
307 ;; of a mode does not choose to do so.
308 (semantic-lex-init)
309 ;; Force this buffer to have its cache refreshed.
310 (semantic-clear-toplevel-cache)
311 ;; Call DB hooks before regular init hooks
312 (run-hooks 'semantic-init-db-hooks)
313 ;; Set up semantic modes
314 (run-hooks 'semantic-init-hooks)
315 ;; Set up major-mode specific semantic modes
316 (run-hooks 'semantic-init-mode-hooks)
317 ))
318
319 (add-hook 'mode-local-init-hook 'semantic-new-buffer-fcn)
320
321 ;; Test the above hook.
322 ;;(add-hook 'semantic-init-hooks (lambda () (message "init for semantic")))
323
324 (defun semantic-fetch-tags-fast ()
325 "For use in a hook. When only a partial reparse is needed, reparse."
326 (condition-case nil
327 (if (semantic-parse-tree-needs-update-p)
328 (semantic-fetch-tags))
329 (error nil))
330 semantic--buffer-cache)
331
332 (if (boundp 'eval-defun-hooks)
333 (add-hook 'eval-defun-hooks 'semantic-fetch-tags-fast))
334 \f
335 ;;; Parsing Commands
336 ;;
337 (eval-when-compile
338 (condition-case nil (require 'pp) (error nil)))
339
340 (defvar semantic-edebug nil
341 "When non-nil, activate the interactive parsing debugger.
342 Do not set this yourself. Call `semantic-debug'.")
343
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)))
350
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
355 the output buffer."
356 (interactive "P")
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*")
366 (require 'pp)
367 (erase-buffer)
368 (insert (pp-to-string out))
369 (goto-char (point-min)))))
370 \f
371 ;;; Functions of the parser plug-in API
372 ;;
373 ;; Overload these functions to create new types of parsers.
374 ;;
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
379 specific rules file.
380 The default parser table used for bovine or wisent based parsers is
381 `semantic--parse-table'.
382
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")
386
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
391 analyze.")
392
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.
398
399 Optional arguments:
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.
406
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'
410 to cook raw tags.")
411
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.
417 Optional arguments:
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)))
426 (save-restriction
427 (widen)
428 (when (or (< end start) (> end (point-max)))
429 (error "Invalid parse region bounds %S, %S" start end))
430 (nreverse
431 (semantic-repeat-parse-whole-stream
432 (or (cdr (assq start semantic-lex-block-streams))
433 (semantic-lex start end depth))
434 nonterminal returnonerror))))
435 \f
436 ;;; Parsing functions
437 ;;
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))
448
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))
453
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)
461 (condition-case nil
462 (progn
463 (semantic-clear-toplevel-cache)
464 (semantic-fetch-tags))
465 (quit
466 (message "semantic-unmatched-syntax-tokens:\
467 parsing of buffer canceled"))
468 )))
469 semantic-unmatched-syntax-cache)
470
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
474 is requested."
475 (interactive)
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))
485 )
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)
491
492 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
493 semantic--buffer-cache)
494 )
495
496 (defvar semantic-bovinate-nonterminal-check-obarray)
497
498 (defun semantic--set-buffer-cache (tagtable)
499 "Set the toplevel cache cache to TAGTABLE."
500 (setq semantic--buffer-cache tagtable
501 semantic-unmatched-syntax-cache-check nil
502 ;; This is specific to the bovine parser.
503 semantic-bovinate-nonterminal-check-obarray nil)
504 (semantic-parse-tree-set-up-to-date)
505 (semantic-make-local-hook 'after-change-functions)
506 (add-hook 'after-change-functions 'semantic-change-function nil t)
507 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
508 semantic--buffer-cache)
509 ;; Refresh the display of unmatched syntax tokens if enabled
510 (run-hook-with-args 'semantic-unmatched-syntax-hook
511 semantic-unmatched-syntax-cache)
512 ;; Old Semantic 1.3 hook API. Maybe useful forever?
513 (run-hooks 'semantic-after-toplevel-bovinate-hook)
514 )
515
516 (defvar semantic-working-type 'percent
517 "*The type of working message to use when parsing.
518 'percent means we are doing a linear parse through the buffer.
519 'dynamic means we are reparsing specific tags.")
520 (semantic-varalias-obsolete 'semantic-bovination-working-type
521 'semantic-working-type)
522
523 (defvar semantic-minimum-working-buffer-size (* 1024 5)
524 "*The minimum size of a buffer before working messages are displayed.
525 Buffers smaller than will parse silently.
526 Bufferse larger than this will display the working progress bar.")
527
528 (defsubst semantic-parser-working-message (&optional arg)
529 "Return the message string displayed while parsing.
530 If optional argument ARG is non-nil it is appended to the message
531 string."
532 (if semantic-parser-name
533 (format "%s/%s" semantic-parser-name (or arg ""))
534 (format "%s" (or arg ""))))
535 \f
536 ;;; Application Parser Entry Points
537 ;;
538 ;; The best way to call the parser from programs is via
539 ;; `semantic-fetch-tags'. This, in turn, uses other internal
540 ;; API functions which plug-in parsers can take advantage of.
541
542 (defun semantic-fetch-tags ()
543 "Fetch semantic tags from the current buffer.
544 If the buffer cache is up to date, return that.
545 If the buffer cache is out of date, attempt an incremental reparse.
546 If the buffer has not been parsed before, or if the incremental reparse
547 fails, then parse the entire buffer.
548 If a lexcial error had been previously discovered and the buffer
549 was marked unparseable, then do nothing, and return the cache."
550 (and
551 ;; Is this a semantic enabled buffer?
552 (semantic-active-p)
553 ;; Application hooks say the buffer is safe for parsing
554 (run-hook-with-args-until-failure
555 'semantic-before-toplevel-bovination-hook)
556 (run-hook-with-args-until-failure
557 'semantic--before-fetch-tags-hook)
558 ;; If the buffer was previously marked unparseable,
559 ;; then don't waste our time.
560 (not (semantic-parse-tree-unparseable-p))
561 ;; The parse tree actually needs to be refreshed
562 (not (semantic-parse-tree-up-to-date-p))
563 ;; So do it!
564 (let* ((gc-cons-threshold (max gc-cons-threshold 10000000))
565 (semantic-lex-block-streams nil)
566 (res nil))
567 (garbage-collect)
568 (cond
569
570 ;;;; Try the incremental parser to do a fast update.
571 ((semantic-parse-tree-needs-update-p)
572 (setq res (semantic-parse-changes))
573 (if (semantic-parse-tree-needs-rebuild-p)
574 ;; If the partial reparse fails, jump to a full reparse.
575 (semantic-fetch-tags)
576 ;; Clear the cache of unmatched syntax tokens
577 ;;
578 ;; NOTE TO SELF:
579 ;;
580 ;; Move this into the incremental parser. This is a bug.
581 ;;
582 (semantic-clear-unmatched-syntax-cache)
583 (run-hook-with-args ;; Let hooks know the updated tags
584 'semantic-after-partial-cache-change-hook res))
585 )
586
587 ;;;; Parse the whole system.
588 ((semantic-parse-tree-needs-rebuild-p)
589 ;; (let ((working-status-dynamic-type
590 ;; (if (< (point-max) semantic-minimum-working-buffer-size)
591 ;; nil
592 ;; working-status-dynamic-type))
593 ;; (working-status-percentage-type
594 ;; (if (< (point-max) semantic-minimum-working-buffer-size)
595 ;; nil
596 ;; working-status-percentage-type)))
597 ;; (working-status-forms
598 ;; (semantic-parser-working-message (buffer-name)) "done"
599 ;; (setq res (semantic-parse-region (point-min) (point-max)))
600 ;; (working-status t)))
601
602 ;; Use Emacs' built-in progress-reporter
603 (let ((semantic--progress-reporter
604 (and (>= (point-max) semantic-minimum-working-buffer-size)
605 (eq semantic-working-type 'percent)
606 (make-progress-reporter
607 (semantic-parser-working-message (buffer-name))
608 0 100))))
609 (setq res (semantic-parse-region (point-min) (point-max)))
610 (progress-reporter-done semantic--progress-reporter))
611
612 ;; Clear the caches when we see there were no errors.
613 ;; But preserve the unmatched syntax cache and warnings!
614 (let (semantic-unmatched-syntax-cache
615 semantic-unmatched-syntax-cache-check
616 semantic-parser-warnings)
617 (semantic-clear-toplevel-cache))
618 ;; Set up the new overlays
619 (semantic--tag-link-list-to-buffer res)
620 ;; Set up the cache with the new results
621 (semantic--set-buffer-cache res)
622 ))))
623
624 ;; Always return the current parse tree.
625 semantic--buffer-cache)
626
627 (defun semantic-refresh-tags-safe ()
628 "Refreshes the current buffer's tags safely.
629
630 Return non-nil if the refresh was successful.
631 Return nil if there is some sort of syntax error preventing a reparse.
632
633 Does nothing if the current buffer doesn't need reparsing."
634
635 ;; These checks actually occur in `semantic-fetch-tags', but if we
636 ;; do them here, then all the bovination hooks are not run, and
637 ;; we save lots of time.
638 (cond
639 ;; If the buffer was previously marked unparseable,
640 ;; then don't waste our time.
641 ((semantic-parse-tree-unparseable-p)
642 nil)
643 ;; The parse tree is already ok.
644 ((semantic-parse-tree-up-to-date-p)
645 t)
646 (t
647 (let* ((inhibit-quit nil)
648 (lexically-safe t)
649 )
650
651 (unwind-protect
652 ;; Perform the parsing.
653 (progn
654 (when (semantic-lex-catch-errors safe-refresh
655 (save-excursion (semantic-fetch-tags))
656 nil)
657 ;; If we are here, it is because the lexical step failed,
658 ;; proably due to unterminated lists or something like that.
659
660 ;; We do nothing, and just wait for the next idle timer
661 ;; to go off. In the meantime, remember this, and make sure
662 ;; no other idle services can get executed.
663 (setq lexically-safe nil))
664 )
665 )
666 ;; Return if we are lexically safe
667 lexically-safe))))
668
669 (defun semantic-bovinate-toplevel (&optional ignored)
670 "Backward Compatibility Function."
671 (semantic-fetch-tags))
672 (make-obsolete 'semantic-bovinate-toplevel 'semantic-fetch-tags)
673
674 ;; Another approach is to let Emacs call the parser on idle time, when
675 ;; needed, use `semantic-fetch-available-tags' to only retrieve
676 ;; available tags, and setup the `semantic-after-*-hook' hooks to
677 ;; synchronize with new tags when they become available.
678
679 (defsubst semantic-fetch-available-tags ()
680 "Fetch available semantic tags from the current buffer.
681 That is, return tags currently in the cache without parsing the
682 current buffer.
683 Parse operations happen asynchronously when needed on Emacs idle time.
684 Use the `semantic-after-toplevel-cache-change-hook' and
685 `semantic-after-partial-cache-change-hook' hooks to synchronize with
686 new tags when they become available."
687 semantic--buffer-cache)
688 \f
689 ;;; Iterative parser helper function
690 ;;
691 ;; Iterative parsers are better than rule-based iterative functions
692 ;; in that they can handle obscure errors more cleanly.
693 ;;
694 ;; `semantic-repeat-parse-whole-stream' abstracts this action for
695 ;; other parser centric routines.
696 ;;
697 (defun semantic-repeat-parse-whole-stream
698 (stream nonterm &optional returnonerror)
699 "Iteratively parse the entire stream STREAM starting with NONTERM.
700 Optional argument RETURNONERROR indicates that the parser should exit
701 with the current results on a parse error.
702 This function returns semantic tags without overlays."
703 (let ((result nil)
704 (case-fold-search semantic-case-fold)
705 nontermsym tag)
706 (while stream
707 (setq nontermsym (semantic-parse-stream stream nonterm)
708 tag (car (cdr nontermsym)))
709 (if (not nontermsym)
710 (error "Parse error @ %d" (car (cdr (car stream)))))
711 (if (eq (car nontermsym) stream)
712 (error "Parser error: Infinite loop?"))
713 (if tag
714 (if (car tag)
715 (setq tag (mapcar
716 #'(lambda (tag)
717 ;; Set the 'reparse-symbol property to
718 ;; NONTERM unless it was already setup
719 ;; by a tag expander
720 (or (semantic--tag-get-property
721 tag 'reparse-symbol)
722 (semantic--tag-put-property
723 tag 'reparse-symbol nonterm))
724 tag)
725 (semantic--tag-expand tag))
726 result (append tag result))
727 ;; No error in this case, a purposeful nil means don't
728 ;; store anything.
729 )
730 (if returnonerror
731 (setq stream nil)
732 ;; The current item in the stream didn't match, so add it to
733 ;; the list of syntax items which didn't match.
734 (setq semantic-unmatched-syntax-cache
735 (cons (car stream) semantic-unmatched-syntax-cache))
736 ))
737 ;; Designated to ignore.
738 (setq stream (car nontermsym))
739 (if stream
740 ;; (if (eq semantic-working-type 'percent)
741 ;; (working-status
742 ;; (/ (* 100 (semantic-lex-token-start (car stream)))
743 ;; (point-max)))
744 ;; (working-dynamic-status))
745
746 ;; Use Emacs' built-in progress reporter:
747 (and (boundp 'semantic--progress-reporter)
748 semantic--progress-reporter
749 (progress-reporter-update
750 semantic--progress-reporter
751 (/ (* 100 (semantic-lex-token-start (car stream)))
752 (point-max))))))
753 result))
754 \f
755 ;;; Parsing Warnings:
756 ;;
757 ;; Parsing a buffer may result in non-critical things that we should
758 ;; alert the user to without interrupting the normal flow.
759 ;;
760 ;; Any parser can use this API to provide a list of warnings during a
761 ;; parse which a user may want to investigate.
762 (defvar semantic-parser-warnings nil
763 "A list of parser warnings since the last full reparse.")
764 (make-variable-buffer-local 'semantic-parser-warnings)
765
766 (defun semantic-clear-parser-warnings ()
767 "Clear the current list of parser warnings for this buffer."
768 (setq semantic-parser-warnings nil))
769
770 (defun semantic-push-parser-warning (warning start end)
771 "Add a parser WARNING that covers text from START to END."
772 (setq semantic-parser-warnings
773 (cons (cons warning (cons start end))
774 semantic-parser-warnings)))
775
776 (defun semantic-dump-parser-warnings ()
777 "Dump any parser warnings."
778 (interactive)
779 (if semantic-parser-warnings
780 (let ((pw semantic-parser-warnings))
781 (pop-to-buffer "*Parser Warnings*")
782 (require 'pp)
783 (erase-buffer)
784 (insert (pp-to-string pw))
785 (goto-char (point-min)))
786 (message "No parser warnings.")))
787
788
789 \f
790 ;;; Compatibility:
791 ;;
792 ;; Semantic 1.x parser action helper functions, used by some parsers.
793 ;; Please move away from these functions, and try using semantic 2.x
794 ;; interfaces instead.
795 ;;
796 (defsubst semantic-bovinate-region-until-error
797 (start end nonterm &optional depth)
798 "NOTE: Use `semantic-parse-region' instead.
799
800 Bovinate between START and END starting with NONTERM.
801 Optional DEPTH specifies how many levels of parenthesis to enter.
802 This command will parse until an error is encountered, and return
803 the list of everything found until that moment.
804 This is meant for finding variable definitions at the beginning of
805 code blocks in methods. If `bovine-inner-scope' can also support
806 commands, use `semantic-bovinate-from-nonterminal-full'."
807 (semantic-parse-region start end nonterm depth t))
808 (make-obsolete 'semantic-bovinate-region-until-error
809 'semantic-parse-region)
810
811 (defsubst semantic-bovinate-from-nonterminal
812 (start end nonterm &optional depth length)
813 "Bovinate from within a nonterminal lambda from START to END.
814 Argument NONTERM is the nonterminal symbol to start with.
815 Optional argument DEPTH is the depth of lists to dive into. When used
816 in a `lambda' of a MATCH-LIST, there is no need to include a START and
817 END part.
818 Optional argument LENGTH specifies we are only interested in LENGTH
819 tokens."
820 (car-safe (cdr (semantic-parse-stream
821 (semantic-lex start end (or depth 1) length)
822 nonterm))))
823
824 (defsubst semantic-bovinate-from-nonterminal-full
825 (start end nonterm &optional depth)
826 "NOTE: Use `semantic-parse-region' instead.
827
828 Bovinate from within a nonterminal lambda from START to END.
829 Iterates until all the space between START and END is exhausted.
830 Argument NONTERM is the nonterminal symbol to start with.
831 If NONTERM is nil, use `bovine-block-toplevel'.
832 Optional argument DEPTH is the depth of lists to dive into.
833 When used in a `lambda' of a MATCH-LIST, there is no need to include
834 a START and END part."
835 (semantic-parse-region start end nonterm (or depth 1)))
836 (make-obsolete 'semantic-bovinate-from-nonterminal-full
837 'semantic-parse-region)
838
839 (provide 'semantic)
840
841 ;;; semantic.el ends here
842
843 ;; Semantic-util is a part of the semantic API. Include it last
844 ;; because it depends on semantic.
845 (require 'semantic-util)