Remove non-free old and crusty clearcase pkg
[packages] / xemacs-packages / semantic / semantic.el.upstream
1 ;;; semantic.el --- Semantic buffer evaluator.
2
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Eric M. Ludlam
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: syntax
7 ;; X-RCS: $Id: semantic.el.upstream,v 1.4 2007-11-26 15:10:44 michaels Exp $
8
9 (eval-and-compile
10   ;; Other package depend on this value at compile time via inversion.
11
12   (defvar semantic-version "2.0pre4"
13     "Current version of Semantic.")
14
15   )
16
17 ;; This file is not part of GNU Emacs.
18
19 ;; Semantic is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation; either version 2, or (at your option)
22 ;; any later version.
23
24 ;; This software is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 ;; GNU General Public License for more details.
28
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
31 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 ;; Boston, MA 02110-1301, USA.
33
34 ;;; Commentary:
35 ;;
36 ;; API for providing the semantic content of a buffer.
37 ;;
38 ;; The semantic API provides an interface to a series of different parser
39 ;; implementations.  Each parser outputs a parse tree in a similar format
40 ;; designed to handle typical functional and object oriented languages.
41
42 (require 'working)
43 (require 'assoc)
44 (require 'semantic-tag)
45 (require 'semantic-lex)
46 (require 'inversion)
47
48 (defun semantic-require-version (major minor &optional beta)
49   "Non-nil if this version of semantic does not satisfy a specific version.
50 Arguments can be:
51
52   (MAJOR MINOR &optional BETA)
53
54   Values MAJOR and MINOR must be integers.  BETA can be an integer, or
55 excluded if a released version is required.
56
57 It is assumed that if the current version is newer than that specified,
58 everything passes.  Exceptions occur when known incompatibilities are
59 introduced."
60   (inversion-test 'semantic
61                   (concat major "." minor
62                           (when beta (concat "beta" beta)))))
63
64 (defgroup semantic nil
65   "Parser Generator/Parser."
66   :group 'lisp)
67
68 (require 'semantic-fw)
69
70 ;;; Code:
71 ;;
72
73 ;;; Variables and Configuration
74 ;;
75 (defvar semantic--parse-table nil
76   "Variable that defines how to parse top level items in a buffer.
77 This variable is for internal use only, and its content depends on the
78 external parser used.")
79 (make-variable-buffer-local 'semantic--parse-table)
80 (semantic-varalias-obsolete 'semantic-toplevel-bovine-table
81                             'semantic--parse-table)
82
83 (defvar semantic-symbol->name-assoc-list
84   '((type     . "Types")
85     (variable . "Variables")
86     (function . "Functions")
87     (include  . "Dependencies")
88     (package  . "Provides"))
89   "Association between symbols returned, and a string.
90 The string is used to represent a group of objects of the given type.
91 It is sometimes useful for a language to use a different string
92 in place of the default, even though that language will still
93 return a symbol.  For example, Java return's includes, but the
94 string can be replaced with `Imports'.")
95 (make-variable-buffer-local 'semantic-symbol->name-assoc-list)
96
97 (defvar semantic-symbol->name-assoc-list-for-type-parts nil
98   "Like `semantic-symbol->name-assoc-list' for type parts.
99 Some tags that have children (see `semantic-tag-children-compatibility')
100 will want to define the names of classes of tags differently than at
101 the top level.  For example, in C++, a Function may be called a
102 Method.  In addition, there may be new types of tags that exist only
103 in classes, such as protection labels.")
104 (make-variable-buffer-local 'semantic-symbol->name-assoc-list-for-type-parts)
105
106 (defvar semantic-case-fold nil
107   "Value for `case-fold-search' when parsing.")
108 (make-variable-buffer-local 'semantic-case-fold)
109
110 (defvar semantic-expand-nonterminal nil
111   "Function to call for each nonterminal production.
112 Return a list of non-terminals derived from the first argument, or nil
113 if it does not need to be expanded.
114 Languages with compound definitions should use this function to expand
115 from one compound symbol into several.  For example, in C the definition
116   int a, b;
117 is easily parsed into one tag.  This function should take this
118 compound tag and turn it into two tags, one for A, and the other for B.")
119 (make-variable-buffer-local 'semantic-expand-nonterminal)
120
121 (defvar semantic--buffer-cache nil
122   "A cache of the fully parsed buffer.
123 If no significant changes have been made (based on the state) then
124 this is returned instead of re-parsing the buffer.
125  
126   DO NOT USE THIS VARIABLE IN PROGRAMS.
127
128 If you need a tag list, use `semantic-fetch-tags'.  If you need the
129 cached values for some reason, chances are you can, add a hook to
130 `semantic-after-toplevel-cache-change-hook'.")
131 (make-variable-buffer-local 'semantic--buffer-cache)
132 (semantic-varalias-obsolete 'semantic-toplevel-bovine-cache
133                             'semantic--buffer-cache)
134
135 (defvar semantic-unmatched-syntax-cache nil
136   "A cached copy of unmatched syntax tokens.")
137 (make-variable-buffer-local 'semantic-unmatched-syntax-cache)
138
139 (defvar semantic-unmatched-syntax-cache-check nil
140   "Non nil if the unmatched syntax cache is out of date.
141 This is tracked with `semantic-change-function'.")
142 (make-variable-buffer-local 'semantic-unmatched-syntax-cache-check)
143
144 (defvar semantic-edits-are-safe nil
145   "When non-nil, modifications do not require a reparse.
146 This prevents tags from being marked dirty, and it prevents top level
147 edits from causing a cache check.
148 Use this when writing programs that could cause a full reparse, but
149 will not change the tag structure, such as adding or updating
150 `top-level' comments.")
151
152 (defvar semantic-unmatched-syntax-hook nil
153   "Hooks run when semantic detects syntax not matched in a grammar.
154 Each individual piece of syntax (such as a symbol or punctuation
155 character) is called with this hook when it doesn't match in the
156 grammar, and multiple unmatched syntax elements are not grouped
157 together.  Each hook is called with one argument, which is a list of
158 syntax tokens created by the semantic lexer.  Use the functions
159 `semantic-lex-token-start', `semantic-lex-token-end' and
160 `semantic-lex-token-text' to get information about these tokens.  The
161 current buffer is the buffer these tokens are derived from.")
162
163 (defvar semantic--before-fetch-tags-hook nil
164   "Hooks run before a buffer is parses for tags.
165 It is called before any request for tags is made via the function
166 `semantic-fetch-tags' by an application.
167 If any hook returns a nil value, the cached value is returned
168 immediately, even if it is empty.")
169 (semantic-varalias-obsolete 'semantic-before-toplevel-bovination-hook
170                             'semantic--before-fetch-tags-hook)
171
172 (defvar semantic-after-toplevel-bovinate-hook nil
173   "Hooks run after a toplevel parse.
174 It is not run if the toplevel parse command is called, and buffer does
175 not need to be fully reparsed.
176 For language specific hooks, make sure you define this as a local hook.
177
178 This hook should not be used any more.
179 Use `semantic-after-toplevel-cache-change-hook' instead.")
180 (make-obsolete-variable 'semantic-after-toplevel-bovinate-hook nil)
181
182 (defvar semantic-after-toplevel-cache-change-hook nil
183   "Hooks run after the buffer tag list has changed.
184 This list will change when a buffer is reparsed, or when the tag list
185 in a buffer is cleared.  It is *NOT* called if the current tag list is
186 partially reparsed.
187
188 Hook functions must take one argument, which is the new list of tags
189 associated with this buffer.
190
191 For language specific hooks, make sure you define this as a local hook.")
192
193 (defvar semantic-before-toplevel-cache-flush-hook nil
194   "Hooks run before the toplevel nonterminal cache is flushed.
195 For language specific hooks, make sure you define this as a local
196 hook.  This hook is called before a corresponding
197 `semantic-after-toplevel-cache-change-hook' which is also called
198 during a flush when the cache is given a new value of nil.")
199
200 (defcustom semantic-dump-parse nil
201   "When non-nil, dump parsing information."
202   :group 'semantic
203   :type 'boolean)
204
205 (defvar semantic-parser-name "LL"
206   "Optional name of the parser used to parse input stream.")
207 (make-variable-buffer-local 'semantic-parser-name)
208 \f
209 ;;; Parse tree state management API
210 ;;
211 (defvar semantic-parse-tree-state 'needs-rebuild
212   "State of the current parse tree.")
213 (make-variable-buffer-local 'semantic-parse-tree-state)
214
215 (defmacro semantic-parse-tree-unparseable ()
216   "Indicate that the current buffer is unparseable.
217 It is also true that the parse tree will need either updating or
218 a rebuild.  This state will be changed when the user edits the buffer."
219   `(setq semantic-parse-tree-state 'unparseable))
220
221 (defmacro semantic-parse-tree-unparseable-p ()
222   "Return non-nil if the current buffer has been marked unparseable."
223   `(eq semantic-parse-tree-state 'unparseable))
224
225 (defmacro semantic-parse-tree-set-needs-update ()
226   "Indicate that the current parse tree needs to be updated.
227 The parse tree can be updated by `semantic-parse-changes'."
228   `(setq semantic-parse-tree-state 'needs-update))
229
230 (defmacro semantic-parse-tree-needs-update-p ()
231   "Return non-nil if the current parse tree needs to be updated."
232   `(eq semantic-parse-tree-state 'needs-update))
233
234 (defmacro semantic-parse-tree-set-needs-rebuild ()
235   "Indicate that the current parse tree needs to be rebuilt.
236 The parse tree must be rebuilt by `semantic-parse-region'."
237   `(setq semantic-parse-tree-state 'needs-rebuild))
238
239 (defmacro semantic-parse-tree-needs-rebuild-p ()
240   "Return non-nil if the current parse tree needs to be rebuilt."
241   `(eq semantic-parse-tree-state 'needs-rebuild))
242
243 (defmacro semantic-parse-tree-set-up-to-date ()
244   "Indicate that the current parse tree is up to date."
245   `(setq semantic-parse-tree-state nil))
246
247 (defmacro semantic-parse-tree-up-to-date-p ()
248   "Return non-nil if the current parse tree is up to date."
249   `(null semantic-parse-tree-state))
250
251 ;;; Interfacing with the system
252 ;;
253 (defcustom semantic-inhibit-functions nil
254   "List of functions to call with no arguments before to setup Semantic.
255 If any of these functions returns non-nil, the current buffer is not
256 setup to use Semantic."
257   :group 'semantic
258   :type 'hook)
259
260 (defvar semantic-init-hooks nil
261   "*Hooks run when a buffer is initialized with a parsing table.")
262
263 (defvar semantic-init-db-hooks nil
264   "Hooks run when a buffer is initialized with a parsing table for DBs.
265 This hook is for database functions which intend to swap in a tag table.
266 This guarantees that the DB will go before other modes that require
267 a parse of the buffer.")
268
269 (defvar semantic-new-buffer-fcn-was-run nil
270   "Non nil after `semantic-new-buffer-fcn' has been executed.")
271 (make-variable-buffer-local 'semantic-new-buffer-fcn-was-run)
272
273 (defsubst semantic-active-p ()
274   "Return non-nil if the current buffer was set up for parsing."
275   semantic-new-buffer-fcn-was-run)
276
277 (defsubst semantic--umatched-syntax-needs-refresh-p  ()
278   "Return non-nil if the unmatched syntax cache needs a refresh.
279 That is if it is dirty or if the current parse tree isn't up to date."
280   (or semantic-unmatched-syntax-cache-check
281       (not (semantic-parse-tree-up-to-date-p))))
282
283 (defun semantic-new-buffer-fcn ()
284   "Setup the current buffer to use Semantic.
285 If the major mode is ready for Semantic, and no
286 `semantic-inhibit-functions' disabled it, the current buffer is setup
287 to use Semantic, and `semantic-init-hook' is run."
288   ;; Do stuff if semantic was activated by a mode hook in this buffer,
289   ;; and not afterwards disabled.
290   (when (and semantic--parse-table
291              (not (semantic-active-p))
292              (not (run-hook-with-args-until-success
293                    'semantic-inhibit-functions)))
294     ;; Specify that this function has done it's work.  At this point
295     ;; we can consider that semantic is active in this buffer.
296     (setq semantic-new-buffer-fcn-was-run t)
297     ;; Here are some buffer local variables we can initialize ourselves
298     ;; of a mode does not choose to do so.
299     (semantic-lex-init)
300     ;; Force this buffer to have its cache refreshed.
301     (semantic-clear-toplevel-cache)
302     ;; Call DB hooks before regular init hooks
303     (run-hooks 'semantic-init-db-hooks)
304     ;; Lastly, set up semantic modes
305     (run-hooks 'semantic-init-hooks)
306     ))
307
308 (add-hook 'mode-local-init-hook 'semantic-new-buffer-fcn)
309
310 ;; Test the above hook.
311 ;;(add-hook 'semantic-init-hooks (lambda () (message "init for semantic")))
312
313 (defun semantic-fetch-tags-fast ()
314   "For use in a hook.  When only a partial reparse is needed, reparse."
315   (condition-case nil
316       (if (semantic-parse-tree-needs-update-p)
317           (semantic-fetch-tags))
318     (error nil)))
319
320 (if (boundp 'eval-defun-hooks)
321     (add-hook 'eval-defun-hooks 'semantic-fetch-tags-fast))
322 \f
323 ;;; Parsing Commands
324 ;;
325 (eval-when-compile
326   (condition-case nil (require 'pp) (error nil)))
327
328 (defvar semantic-edebug nil
329   "When non-nil, activate the interactive parsing debugger.
330 Do not set this yourself.  Call `semantic-debug'.")
331
332 (defun semantic-elapsed-time (start end)
333   "Copied from elp.el.  Was elp-elapsed-time.
334 Argument START and END bound the time being calculated."
335   (+ (* (- (car end) (car start)) 65536.0)
336      (- (car (cdr end)) (car (cdr start)))
337      (/ (- (car (cdr (cdr end))) (car (cdr (cdr start)))) 1000000.0)))
338
339 (defun bovinate (&optional clear)
340   "Parse the current buffer.  Show output in a temp buffer.
341 Optional argument CLEAR will clear the cache before parsing.
342 If CLEAR is negative, it will do a full reparse, and also not display
343 the output buffer."
344   (interactive "P")
345   (if clear (semantic-clear-toplevel-cache))
346   (if (eq clear '-) (setq clear -1))
347   (let* ((start (current-time))
348          (out (semantic-fetch-tags))
349          (end (current-time)))
350     (message "Retrieving tags took %.2f seconds."
351              (semantic-elapsed-time start end))
352     (when (or (null clear) (not (listp clear)))
353       (pop-to-buffer "*Parser Output*")
354       (require 'pp)
355       (erase-buffer)
356       (insert (pp-to-string out))
357       (goto-char (point-min)))))
358 \f
359 ;;; Functions of the parser plug-in API
360 ;;
361 ;; Overload these functions to create new types of parsers.
362 ;;
363 (define-overload semantic-parse-stream (stream nonterminal)
364   "Parse STREAM, starting at the first NONTERMINAL rule.
365 For bovine and wisent based parsers, STREAM is from the output of
366 `semantic-lex', and NONTERMINAL is a rule in the apropriate language
367 specific rules file.
368 The default parser table used for bovine or wisent based parsers is
369 `semantic--parse-table'.
370
371 Must return a list: (STREAM TAGS) where STREAM is the unused elements
372 from STREAM, and TAGS is the list of semantic tags found, usually only
373 one tag is returned with the exception of compound statements")
374
375 (define-overload semantic-parse-changes ()
376   "Reparse changes in the current buffer.
377 The list of changes are tracked as a series of overlays in the buffer.
378 When overloading this function, use `semantic-changes-in-region' to
379 analyze.")
380
381 (define-overload semantic-parse-region
382   (start end &optional nonterminal depth returnonerror)
383   "Parse the area between START and END, and return any tags found.
384 If END needs to be extended due to a lexical token being too large, it
385 will be silently ignored.
386
387 Optional arguments:
388 NONTERMINAL is the rule to start parsing at.
389 DEPTH specifies the lexical depth to decend for parser that use
390 lexical analysis as their first step.
391 RETURNONERROR specifies that parsing should stop on the first
392 unmatched syntax encountered.  When nil, parsing skips the syntax,
393 adding it to the unmatched syntax cache.
394
395 Must return a list of semantic tags wich have been cooked
396 \(repositioned properly) but which DO NOT HAVE OVERLAYS associated
397 with them.  When overloading this function, use `semantic--tag-expand'
398 to cook raw tags.")
399
400 ;;;###autoload
401 (defun semantic-parse-region-default
402   (start end &optional nonterminal depth returnonerror)
403   "Parse the area between START and END, and return any tags found.
404 If END needs to be extended due to a lexical token being too large, it
405 will be silently ignored.
406 Optional arguments:
407 NONTERMINAL is the rule to start parsing at if it is known.
408 DEPTH specifies the lexical depth to scan.
409 RETURNONERROR specifies that parsing should end when encountering
410 unterminated syntax."
411   (when (or (null semantic--parse-table) (eq semantic--parse-table t))
412     ;; If there is no table, or it was set to t, then we are here by
413     ;; some other mistake.  Do not throw an error deep in the parser.
414     (error "No support found to parse buffer %S" (buffer-name)))
415   (when (or (< end start) (> end (point-max)))
416     (error "Invalid parse region bounds %S, %S" start end))
417   (nreverse
418    (semantic-repeat-parse-whole-stream
419     (or (cdr (assq start semantic-lex-block-streams))
420         (semantic-lex start end depth))
421     nonterminal returnonerror)))
422 \f
423 ;;; Parsing functions
424 ;;
425 (defun semantic-set-unmatched-syntax-cache (unmatched-syntax)
426   "Set the unmatched syntax cache.
427 Argument UNMATCHED-SYNTAX is the syntax to set into the cache."
428   ;; This function is not actually called by the main parse loop.
429   ;; This is intended for use by semanticdb.
430   (setq semantic-unmatched-syntax-cache unmatched-syntax
431         semantic-unmatched-syntax-cache-check nil)
432     ;; Refresh the display of unmatched syntax tokens if enabled
433   (run-hook-with-args 'semantic-unmatched-syntax-hook
434                       semantic-unmatched-syntax-cache))
435
436 (defun semantic-clear-unmatched-syntax-cache ()
437   "Clear the cache of unmatched syntax tokens."
438   (setq semantic-unmatched-syntax-cache nil
439         semantic-unmatched-syntax-cache-check t))
440
441 (defun semantic-unmatched-syntax-tokens ()
442   "Return the list of unmatched syntax tokens."
443   ;; If the cache need refresh then do a full re-parse.
444   (if (semantic--umatched-syntax-needs-refresh-p)
445       ;; To avoid a recursive call, temporarily disable
446       ;; `semantic-unmatched-syntax-hook'.
447       (let (semantic-unmatched-syntax-hook)
448         (condition-case nil
449             (progn
450               (semantic-clear-toplevel-cache)
451               (semantic-fetch-tags))
452           (quit
453            (message "semantic-unmatched-syntax-tokens:\
454  parsing of buffer canceled"))
455           )))
456     semantic-unmatched-syntax-cache)
457
458 (defun semantic-clear-toplevel-cache ()
459   "Clear the toplevel tag cache for the current buffer.
460 Clearing the cache will force a complete reparse next time a tag list
461 is requested."
462   (interactive)
463   (run-hooks 'semantic-before-toplevel-cache-flush-hook)
464   (setq semantic--buffer-cache nil)
465   (semantic-clear-unmatched-syntax-cache)
466   (semantic-clear-parser-warnings)
467   ;; Nuke all semantic overlays.  This is faster than deleting based
468   ;; on our data structure.
469   (let ((l (semantic-overlay-lists)))
470     (mapcar 'semantic-delete-overlay-maybe (car l))
471     (mapcar 'semantic-delete-overlay-maybe (cdr l))
472     )
473   (semantic-parse-tree-set-needs-rebuild)
474   ;; Remove this hook which tracks if a buffer is up to date or not.
475   (remove-hook 'after-change-functions 'semantic-change-function t)
476   ;; Old model.  Delete someday.
477   ;;(run-hooks 'semantic-after-toplevel-bovinate-hook)
478
479   (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
480                       semantic--buffer-cache)
481   )
482
483 (defun semantic--set-buffer-cache (tagtable)
484   "Set the toplevel cache cache to TAGTABLE."
485   (setq semantic--buffer-cache tagtable
486         semantic-unmatched-syntax-cache-check nil
487         ;; This is specific to the bovine parser.
488         semantic-bovinate-nonterminal-check-obarray nil)
489   (semantic-parse-tree-set-up-to-date)
490   (semantic-make-local-hook 'after-change-functions)
491   (add-hook 'after-change-functions 'semantic-change-function nil t)
492   (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
493                       semantic--buffer-cache)
494   ;; Refresh the display of unmatched syntax tokens if enabled
495   (run-hook-with-args 'semantic-unmatched-syntax-hook
496                       semantic-unmatched-syntax-cache)
497   ;; Old Semantic 1.3 hook API.  Maybe useful forever?
498   (run-hooks 'semantic-after-toplevel-bovinate-hook)
499   )
500
501 (defvar semantic-working-type 'percent
502   "*The type of working message to use when parsing.
503 'percent means we are doing a linear parse through the buffer.
504 'dynamic means we are reparsing specific tags.")
505 (semantic-varalias-obsolete 'semantic-bovination-working-type
506                             'semantic-working-type)
507
508 (defsubst semantic-parser-working-message (&optional arg)
509   "Return the message string displayed while parsing.
510 If optional argument ARG is non-nil it is appended to the message
511 string.  See also the function `working-status-forms'."
512   (if semantic-parser-name
513       (format "%s/%s" semantic-parser-name (or arg ""))
514     (format "%s" (or arg ""))))
515 \f
516 ;;; Application Parser Entry Points
517 ;;
518 ;; The best way to call the parser from programs is via
519 ;; `semantic-fetch-tags'.  This, in turn, uses other internal
520 ;; API functions which plug-in parsers can take advantage of.
521
522 ;;;###autoload
523 (defun semantic-fetch-tags ()
524   "Fetch semantic tags from the current buffer.
525 If the buffer cache is up to date, return that.
526 If the buffer cache is out of date, attempt an incremental reparse.
527 If the buffer has not been parsed before, or if the incremental reparse
528 fails, then parse the entire buffer.
529 If a lexcial error had been previously discovered and the buffer
530 was marked unparseable, then do nothing, and return the cache."
531   (and
532    ;; Is this a semantic enabled buffer?
533    (semantic-active-p)
534    ;; Application hooks say the buffer is safe for parsing
535    (run-hook-with-args-until-failure
536     'semantic-before-toplevel-bovination-hook)
537    (run-hook-with-args-until-failure
538     'semantic--before-fetch-tags-hook)
539    ;; If the buffer was previously marked unparseable,
540    ;; then don't waste our time.
541    (not (semantic-parse-tree-unparseable-p))
542    ;; The parse tree actually needs to be refreshed
543    (not (semantic-parse-tree-up-to-date-p))
544    ;; So do it!
545    (let* ((gc-cons-threshold (max gc-cons-threshold 10000000))
546           (semantic-lex-block-streams nil)
547           (res nil))
548      (garbage-collect)
549      (cond
550    
551 ;;;; Try the incremental parser to do a fast update.
552      ((semantic-parse-tree-needs-update-p)
553       (setq res (semantic-parse-changes))
554       (if (semantic-parse-tree-needs-rebuild-p)
555           ;; If the partial reparse fails, jump to a full reparse.
556           (semantic-fetch-tags)
557         ;; Clear the cache of unmatched syntax tokens
558         ;;
559         ;; NOTE TO SELF:
560         ;;
561         ;; Move this into the incremental parser.  This is a bug.
562         ;;
563         (semantic-clear-unmatched-syntax-cache)
564         (run-hook-with-args ;; Let hooks know the updated tags
565          'semantic-after-partial-cache-change-hook res))
566       )
567    
568 ;;;; Parse the whole system.
569      ((semantic-parse-tree-needs-rebuild-p)
570       (working-status-forms
571           (semantic-parser-working-message (buffer-name)) "done"
572         (setq res (semantic-parse-region (point-min) (point-max)))
573         (working-status t))
574       ;; Clear the caches when we see there were no errors.
575       ;; But preserve the unmatched syntax cache and warnings!
576       (let (semantic-unmatched-syntax-cache
577             semantic-unmatched-syntax-cache-check
578             semantic-parser-warnings)
579         (semantic-clear-toplevel-cache))
580       ;; Set up the new overlays
581       (semantic--tag-link-list-to-buffer res)
582       ;; Set up the cache with the new results
583       (semantic--set-buffer-cache res)
584       ))))
585   
586   ;; Always return the current parse tree.
587   semantic--buffer-cache)
588
589 ;;;###autoload
590 (defun semantic-refresh-tags-safe ()
591   "Refreshes the current buffer's tags safely.
592
593 Return non-nil if the refresh was successful.
594 Return nil if there is some sort of syntax error preventing a reparse.
595
596 Does nothing if the current buffer doesn't need reparsing."
597
598   ;; These checks actually occur in `semantic-fetch-tags', but if we
599   ;; do them here, then all the bovination hooks are not run, and
600   ;; we save lots of time.
601   (cond
602    ;; If the buffer was previously marked unparseable,
603    ;; then don't waste our time.
604    ((semantic-parse-tree-unparseable-p)
605     nil)
606    ;; The parse tree is already ok.
607    ((semantic-parse-tree-up-to-date-p)
608     t)
609    (t
610     (let* ((inhibit-quit nil)
611            (lexically-safe t)
612            )
613
614       (unwind-protect
615           ;; Perform the parsing.
616           (progn
617             (when (semantic-lex-catch-errors safe-refresh
618                     (save-excursion (semantic-fetch-tags))
619                     nil)
620               ;; If we are here, it is because the lexical step failed,
621               ;; proably due to unterminated lists or something like that.
622             
623               ;; We do nothing, and just wait for the next idle timer
624               ;; to go off.  In the meantime, remember this, and make sure
625               ;; no other idle services can get executed.
626               (setq lexically-safe nil))
627             )
628         )
629       ;; Return if we are lexically safe
630       lexically-safe))))
631
632 ;;;###autoload
633 (defun semantic-bovinate-toplevel (&optional ignored)
634   "Backward Compatibility Function."
635   (semantic-fetch-tags))
636 ;;;###autoload
637 (make-obsolete 'semantic-bovinate-toplevel 'semantic-fetch-tags)
638
639 ;; Another approach is to let Emacs call the parser on idle time, when
640 ;; needed, use `semantic-fetch-available-tags' to only retrieve
641 ;; available tags, and setup the `semantic-after-*-hook' hooks to
642 ;; synchronize with new tags when they become available.
643
644 ;;;###autoload
645 (defsubst semantic-fetch-available-tags ()
646   "Fetch available semantic tags from the current buffer.
647 That is, return tags currently in the cache without parsing the
648 current buffer.
649 Parse operations happen asynchronously when needed on Emacs idle time.
650 Use the `semantic-after-toplevel-cache-change-hook' and
651 `semantic-after-partial-cache-change-hook' hooks to synchronize with
652 new tags when they become available."
653   semantic--buffer-cache)
654 \f
655 ;;; Iterative parser helper function
656 ;;
657 ;; Iterative parsers are better than rule-based iterative functions
658 ;; in that they can handle obscure errors more cleanly.
659 ;;
660 ;; `semantic-repeat-parse-whole-stream' abstracts this action for
661 ;; other parser centric routines.
662 ;;
663 (defun semantic-repeat-parse-whole-stream
664   (stream nonterm &optional returnonerror)
665   "Iteratively parse the entire stream STREAM starting with NONTERM.
666 Optional argument RETURNONERROR indicates that the parser should exit
667 with the current results on a parse error.
668 This function returns semantic tags without overlays."
669   (let ((result nil)
670         (case-fold-search semantic-case-fold)
671         nontermsym tag)
672     (while stream
673       (setq nontermsym (semantic-parse-stream stream nonterm)
674             tag (car (cdr nontermsym)))
675       (if (not nontermsym)
676           (error "Parse error @ %d" (car (cdr (car stream)))))
677       (if (eq (car nontermsym) stream)
678           (error "Parser error: Infinite loop?"))
679       (if tag
680           (if (car tag)
681               (setq tag (mapcar
682                          #'(lambda (tag)
683                              ;; Set the 'reparse-symbol property to
684                              ;; NONTERM unless it was already setup
685                              ;; by a tag expander
686                              (or (semantic--tag-get-property
687                                   tag 'reparse-symbol)
688                                  (semantic--tag-put-property
689                                   tag 'reparse-symbol nonterm))
690                              tag)
691                          (semantic--tag-expand tag))
692                     result (append tag result))
693             ;; No error in this case, a purposeful nil means don't
694             ;; store anything.
695             )
696         (if returnonerror
697             (setq stream nil)
698           ;; The current item in the stream didn't match, so add it to
699           ;; the list of syntax items which didn't match.
700           (setq semantic-unmatched-syntax-cache
701                 (cons (car stream) semantic-unmatched-syntax-cache))
702           ))
703       ;; Designated to ignore.
704       (setq stream (car nontermsym))
705       (if stream
706           (if (eq semantic-working-type 'percent)
707               (working-status
708                (/ (* 100 (semantic-lex-token-start (car stream)))
709                   (point-max)))
710             (working-dynamic-status))))
711     result))
712 \f
713 ;;; Parsing Warnings:
714 ;;
715 ;; Parsing a buffer may result in non-critical things that we should
716 ;; alert the user to without interrupting the normal flow.
717 ;;
718 ;; Any parser can use this API to provide a list of warnings during a
719 ;; parse which a user may want to investigate.
720 (defvar semantic-parser-warnings nil
721   "A list of parser warnings since the last full reparse.")
722 (make-variable-buffer-local 'semantic-parser-warnings)
723
724 (defun semantic-clear-parser-warnings ()
725   "Clear the current list of parser warnings for this buffer."
726   (setq semantic-parser-warnings nil))
727
728 (defun semantic-push-parser-warning (warning start end)
729   "Add a parser WARNING that covers text from START to END."
730   (setq semantic-parser-warnings
731         (cons (cons warning (cons start end))
732               semantic-parser-warnings)))
733
734 (defun semantic-dump-parser-warnings ()
735   "Dump any parser warnings."
736   (interactive)
737   (if semantic-parser-warnings
738       (let ((pw semantic-parser-warnings))
739         (pop-to-buffer "*Parser Warnings*")
740         (require 'pp)
741         (erase-buffer)
742         (insert (pp-to-string pw))
743         (goto-char (point-min)))
744     (message "No parser warnings.")))
745
746
747 \f
748 ;;; Compatibility:
749 ;;
750 ;; Semantic 1.x parser action helper functions, used by some parsers.
751 ;; Please move away from these functions, and try using semantic 2.x
752 ;; interfaces instead.
753 ;;
754 (defsubst semantic-bovinate-region-until-error
755   (start end nonterm &optional depth)
756   "NOTE: Use `semantic-parse-region' instead.
757
758 Bovinate between START and END starting with NONTERM.
759 Optional DEPTH specifies how many levels of parenthesis to enter.
760 This command will parse until an error is encountered, and return
761 the list of everything found until that moment.
762 This is meant for finding variable definitions at the beginning of
763 code blocks in methods.  If `bovine-inner-scope' can also support
764 commands, use `semantic-bovinate-from-nonterminal-full'."
765   (semantic-parse-region start end nonterm depth t))
766 (make-obsolete 'semantic-bovinate-region-until-error
767                'semantic-parse-region)
768
769 (defsubst semantic-bovinate-from-nonterminal
770   (start end nonterm &optional depth length)
771   "Bovinate from within a nonterminal lambda from START to END.
772 Argument NONTERM is the nonterminal symbol to start with.
773 Optional argument DEPTH is the depth of lists to dive into.  When used
774 in a `lambda' of a MATCH-LIST, there is no need to include a START and
775 END part.
776 Optional argument LENGTH specifies we are only interested in LENGTH
777 tokens."
778   (car-safe (cdr (semantic-parse-stream
779                   (semantic-lex start end (or depth 1) length)
780                   nonterm))))
781
782 (defsubst semantic-bovinate-from-nonterminal-full
783   (start end nonterm &optional depth)
784   "NOTE: Use `semantic-parse-region' instead.
785
786 Bovinate from within a nonterminal lambda from START to END.
787 Iterates until all the space between START and END is exhausted.
788 Argument NONTERM is the nonterminal symbol to start with.
789 If NONTERM is nil, use `bovine-block-toplevel'.
790 Optional argument DEPTH is the depth of lists to dive into.
791 When used in a `lambda' of a MATCH-LIST, there is no need to include
792 a START and END part."
793   (semantic-parse-region start end nonterm (or depth 1)))
794 (make-obsolete 'semantic-bovinate-from-nonterminal-full
795                'semantic-parse-region)
796
797 (provide 'semantic)
798
799 ;;; semantic.el ends here
800
801 ;; Semantic-util is a part of the semantic API.  Include it last
802 ;; because it depends on semantic.
803 (require 'semantic-util)