]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic.el
Minor whitespace changes and `require' fixes.
[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 'assoc)
38 (require 'semantic/tag)
39 (require 'semantic/lex)
40
41 (declare-function inversion-test "inversion")
42
43 (defun semantic-require-version (major minor &optional beta)
44 "Non-nil if this version of semantic does not satisfy a specific version.
45 Arguments can be:
46
47 (MAJOR MINOR &optional BETA)
48
49 Values MAJOR and MINOR must be integers. BETA can be an integer, or
50 excluded if a released version is required.
51
52 It is assumed that if the current version is newer than that specified,
53 everything passes. Exceptions occur when known incompatibilities are
54 introduced."
55 (require 'inversion)
56 (inversion-test 'semantic
57 (concat major "." minor
58 (when beta (concat "beta" beta)))))
59
60 (defgroup semantic nil
61 "Parser Generator and parser framework."
62 :group 'lisp)
63
64 (defgroup semantic-faces nil
65 "Faces used for Semantic enabled tools."
66 :group 'semantic)
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 tag 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 Semantic is setup.
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-mode-hooks nil
264 "*Hooks run when a buffer of a particular mode is initialized.")
265 (make-variable-buffer-local 'semantic-init-mode-hooks)
266
267 (defvar semantic-init-db-hooks nil
268 "Hooks run when a buffer is initialized with a parsing table for DBs.
269 This hook is for database functions which intend to swap in a tag table.
270 This guarantees that the DB will go before other modes that require
271 a parse of the buffer.")
272
273 (defvar semantic-new-buffer-fcn-was-run nil
274 "Non nil after `semantic-new-buffer-fcn' has been executed.")
275 (make-variable-buffer-local 'semantic-new-buffer-fcn-was-run)
276
277 (defsubst semantic-active-p ()
278 "Return non-nil if the current buffer was set up for parsing."
279 semantic-new-buffer-fcn-was-run)
280
281 (defsubst semantic--umatched-syntax-needs-refresh-p ()
282 "Return non-nil if the unmatched syntax cache needs a refresh.
283 That is if it is dirty or if the current parse tree isn't up to date."
284 (or semantic-unmatched-syntax-cache-check
285 (not (semantic-parse-tree-up-to-date-p))))
286
287 (defun semantic-new-buffer-fcn ()
288 "Setup the current buffer to use Semantic.
289 If the major mode is ready for Semantic, and no
290 `semantic-inhibit-functions' disabled it, the current buffer is setup
291 to use Semantic, and `semantic-init-hook' is run."
292 ;; Do stuff if semantic was activated by a mode hook in this buffer,
293 ;; and not afterwards disabled.
294 (when (and semantic--parse-table
295 (not (semantic-active-p))
296 (not (run-hook-with-args-until-success
297 'semantic-inhibit-functions)))
298 ;; Make sure that if this buffer is cloned, our tags and overlays
299 ;; don't go along for the ride.
300 (add-hook 'clone-indirect-buffer-hook 'semantic-clear-toplevel-cache
301 nil t)
302 ;; Specify that this function has done it's work. At this point
303 ;; we can consider that semantic is active in this buffer.
304 (setq semantic-new-buffer-fcn-was-run t)
305 ;; Here are some buffer local variables we can initialize ourselves
306 ;; of a mode does not choose to do so.
307 (semantic-lex-init)
308 ;; Force this buffer to have its cache refreshed.
309 (semantic-clear-toplevel-cache)
310 ;; Call DB hooks before regular init hooks
311 (run-hooks 'semantic-init-db-hooks)
312 ;; Set up semantic modes
313 (run-hooks 'semantic-init-hooks)
314 ;; Set up major-mode specific semantic modes
315 (run-hooks 'semantic-init-mode-hooks)
316 ))
317
318 (add-hook 'mode-local-init-hook 'semantic-new-buffer-fcn)
319
320 ;; Test the above hook.
321 ;;(add-hook 'semantic-init-hooks (lambda () (message "init for semantic")))
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
331 (if (boundp 'eval-defun-hooks)
332 (add-hook 'eval-defun-hooks 'semantic-fetch-tags-fast))
333 \f
334 ;;; Parsing Commands
335 ;;
336 (eval-when-compile
337 (condition-case nil (require 'pp) (error nil)))
338
339 (defvar semantic-edebug nil
340 "When non-nil, activate the interactive parsing debugger.
341 Do not set this yourself. Call `semantic-debug'.")
342
343 (defun semantic-elapsed-time (start end)
344 "Copied from elp.el. Was elp-elapsed-time.
345 Argument START and END bound the time being calculated."
346 (+ (* (- (car end) (car start)) 65536.0)
347 (- (car (cdr end)) (car (cdr start)))
348 (/ (- (car (cdr (cdr end))) (car (cdr (cdr start)))) 1000000.0)))
349
350 (defun bovinate (&optional clear)
351 "Parse the current buffer. Show output in a temp buffer.
352 Optional argument CLEAR will clear the cache before parsing.
353 If CLEAR is negative, it will do a full reparse, and also not display
354 the output buffer."
355 (interactive "P")
356 (if clear (semantic-clear-toplevel-cache))
357 (if (eq clear '-) (setq clear -1))
358 (let* ((start (current-time))
359 (out (semantic-fetch-tags))
360 (end (current-time)))
361 (message "Retrieving tags took %.2f seconds."
362 (semantic-elapsed-time start end))
363 (when (or (null clear) (not (listp clear)))
364 (pop-to-buffer "*Parser Output*")
365 (require 'pp)
366 (erase-buffer)
367 (insert (pp-to-string out))
368 (goto-char (point-min)))))
369 \f
370 ;;; Functions of the parser plug-in API
371 ;;
372 ;; Overload these functions to create new types of parsers.
373 ;;
374 (define-overloadable-function semantic-parse-stream (stream nonterminal)
375 "Parse STREAM, starting at the first NONTERMINAL rule.
376 For bovine and wisent based parsers, STREAM is from the output of
377 `semantic-lex', and NONTERMINAL is a rule in the apropriate language
378 specific rules file.
379 The default parser table used for bovine or wisent based parsers is
380 `semantic--parse-table'.
381
382 Must return a list: (STREAM TAGS) where STREAM is the unused elements
383 from STREAM, and TAGS is the list of semantic tags found, usually only
384 one tag is returned with the exception of compound statements")
385
386 (define-overloadable-function semantic-parse-changes ()
387 "Reparse changes in the current buffer.
388 The list of changes are tracked as a series of overlays in the buffer.
389 When overloading this function, use `semantic-changes-in-region' to
390 analyze.")
391
392 (define-overloadable-function semantic-parse-region
393 (start end &optional nonterminal depth returnonerror)
394 "Parse the area between START and END, and return any tags found.
395 If END needs to be extended due to a lexical token being too large, it
396 will be silently ignored.
397
398 Optional arguments:
399 NONTERMINAL is the rule to start parsing at.
400 DEPTH specifies the lexical depth to decend for parser that use
401 lexical analysis as their first step.
402 RETURNONERROR specifies that parsing should stop on the first
403 unmatched syntax encountered. When nil, parsing skips the syntax,
404 adding it to the unmatched syntax cache.
405
406 Must return a list of semantic tags wich have been cooked
407 \(repositioned properly) but which DO NOT HAVE OVERLAYS associated
408 with them. When overloading this function, use `semantic--tag-expand'
409 to cook raw tags.")
410
411 (defun semantic-parse-region-default
412 (start end &optional nonterminal depth returnonerror)
413 "Parse the area between START and END, and return any tags found.
414 If END needs to be extended due to a lexical token being too large, it
415 will be silently ignored.
416 Optional arguments:
417 NONTERMINAL is the rule to start parsing at if it is known.
418 DEPTH specifies the lexical depth to scan.
419 RETURNONERROR specifies that parsing should end when encountering
420 unterminated syntax."
421 (when (or (null semantic--parse-table) (eq semantic--parse-table t))
422 ;; If there is no table, or it was set to t, then we are here by
423 ;; some other mistake. Do not throw an error deep in the parser.
424 (error "No support found to parse buffer %S" (buffer-name)))
425 (save-restriction
426 (widen)
427 (when (or (< end start) (> end (point-max)))
428 (error "Invalid parse region bounds %S, %S" start end))
429 (nreverse
430 (semantic-repeat-parse-whole-stream
431 (or (cdr (assq start semantic-lex-block-streams))
432 (semantic-lex start end depth))
433 nonterminal returnonerror))))
434 \f
435 ;;; Parsing functions
436 ;;
437 (defun semantic-set-unmatched-syntax-cache (unmatched-syntax)
438 "Set the unmatched syntax cache.
439 Argument UNMATCHED-SYNTAX is the syntax to set into the cache."
440 ;; This function is not actually called by the main parse loop.
441 ;; This is intended for use by semanticdb.
442 (setq semantic-unmatched-syntax-cache unmatched-syntax
443 semantic-unmatched-syntax-cache-check nil)
444 ;; Refresh the display of unmatched syntax tokens if enabled
445 (run-hook-with-args 'semantic-unmatched-syntax-hook
446 semantic-unmatched-syntax-cache))
447
448 (defun semantic-clear-unmatched-syntax-cache ()
449 "Clear the cache of unmatched syntax tokens."
450 (setq semantic-unmatched-syntax-cache nil
451 semantic-unmatched-syntax-cache-check t))
452
453 (defun semantic-unmatched-syntax-tokens ()
454 "Return the list of unmatched syntax tokens."
455 ;; If the cache need refresh then do a full re-parse.
456 (if (semantic--umatched-syntax-needs-refresh-p)
457 ;; To avoid a recursive call, temporarily disable
458 ;; `semantic-unmatched-syntax-hook'.
459 (let (semantic-unmatched-syntax-hook)
460 (condition-case nil
461 (progn
462 (semantic-clear-toplevel-cache)
463 (semantic-fetch-tags))
464 (quit
465 (message "semantic-unmatched-syntax-tokens:\
466 parsing of buffer canceled"))
467 )))
468 semantic-unmatched-syntax-cache)
469
470 (defun semantic-clear-toplevel-cache ()
471 "Clear the toplevel tag cache for the current buffer.
472 Clearing the cache will force a complete reparse next time a tag list
473 is requested."
474 (interactive)
475 (run-hooks 'semantic-before-toplevel-cache-flush-hook)
476 (setq semantic--buffer-cache nil)
477 (semantic-clear-unmatched-syntax-cache)
478 (semantic-clear-parser-warnings)
479 ;; Nuke all semantic overlays. This is faster than deleting based
480 ;; on our data structure.
481 (let ((l (semantic-overlay-lists)))
482 (mapc 'semantic-delete-overlay-maybe (car l))
483 (mapc 'semantic-delete-overlay-maybe (cdr l))
484 )
485 (semantic-parse-tree-set-needs-rebuild)
486 ;; Remove this hook which tracks if a buffer is up to date or not.
487 (remove-hook 'after-change-functions 'semantic-change-function t)
488 ;; Old model. Delete someday.
489 ;;(run-hooks 'semantic-after-toplevel-bovinate-hook)
490
491 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
492 semantic--buffer-cache)
493 )
494
495 (defvar semantic-bovinate-nonterminal-check-obarray)
496
497 (defun semantic--set-buffer-cache (tagtable)
498 "Set the toplevel cache cache to TAGTABLE."
499 (setq semantic--buffer-cache tagtable
500 semantic-unmatched-syntax-cache-check nil)
501 ;; This is specific to the bovine parser.
502 (set (make-local-variable 'semantic-bovinate-nonterminal-check-obarray)
503 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 ;; Use Emacs' built-in progress-reporter
590 (let ((semantic--progress-reporter
591 (and (>= (point-max) semantic-minimum-working-buffer-size)
592 (eq semantic-working-type 'percent)
593 (make-progress-reporter
594 (semantic-parser-working-message (buffer-name))
595 0 100))))
596 (setq res (semantic-parse-region (point-min) (point-max)))
597 (progress-reporter-done semantic--progress-reporter))
598
599 ;; Clear the caches when we see there were no errors.
600 ;; But preserve the unmatched syntax cache and warnings!
601 (let (semantic-unmatched-syntax-cache
602 semantic-unmatched-syntax-cache-check
603 semantic-parser-warnings)
604 (semantic-clear-toplevel-cache))
605 ;; Set up the new overlays
606 (semantic--tag-link-list-to-buffer res)
607 ;; Set up the cache with the new results
608 (semantic--set-buffer-cache res)
609 ))))
610
611 ;; Always return the current parse tree.
612 semantic--buffer-cache)
613
614 (defun semantic-refresh-tags-safe ()
615 "Refreshes the current buffer's tags safely.
616
617 Return non-nil if the refresh was successful.
618 Return nil if there is some sort of syntax error preventing a reparse.
619
620 Does nothing if the current buffer doesn't need reparsing."
621
622 ;; These checks actually occur in `semantic-fetch-tags', but if we
623 ;; do them here, then all the bovination hooks are not run, and
624 ;; we save lots of time.
625 (cond
626 ;; If the buffer was previously marked unparseable,
627 ;; then don't waste our time.
628 ((semantic-parse-tree-unparseable-p)
629 nil)
630 ;; The parse tree is already ok.
631 ((semantic-parse-tree-up-to-date-p)
632 t)
633 (t
634 (let* ((inhibit-quit nil)
635 (lexically-safe t)
636 )
637
638 (unwind-protect
639 ;; Perform the parsing.
640 (progn
641 (when (semantic-lex-catch-errors safe-refresh
642 (save-excursion (semantic-fetch-tags))
643 nil)
644 ;; If we are here, it is because the lexical step failed,
645 ;; proably due to unterminated lists or something like that.
646
647 ;; We do nothing, and just wait for the next idle timer
648 ;; to go off. In the meantime, remember this, and make sure
649 ;; no other idle services can get executed.
650 (setq lexically-safe nil))
651 )
652 )
653 ;; Return if we are lexically safe
654 lexically-safe))))
655
656 (defun semantic-bovinate-toplevel (&optional ignored)
657 "Backward Compatibility Function."
658 (semantic-fetch-tags))
659 (make-obsolete 'semantic-bovinate-toplevel 'semantic-fetch-tags)
660
661 ;; Another approach is to let Emacs call the parser on idle time, when
662 ;; needed, use `semantic-fetch-available-tags' to only retrieve
663 ;; available tags, and setup the `semantic-after-*-hook' hooks to
664 ;; synchronize with new tags when they become available.
665
666 (defsubst semantic-fetch-available-tags ()
667 "Fetch available semantic tags from the current buffer.
668 That is, return tags currently in the cache without parsing the
669 current buffer.
670 Parse operations happen asynchronously when needed on Emacs idle time.
671 Use the `semantic-after-toplevel-cache-change-hook' and
672 `semantic-after-partial-cache-change-hook' hooks to synchronize with
673 new tags when they become available."
674 semantic--buffer-cache)
675 \f
676 ;;; Iterative parser helper function
677 ;;
678 ;; Iterative parsers are better than rule-based iterative functions
679 ;; in that they can handle obscure errors more cleanly.
680 ;;
681 ;; `semantic-repeat-parse-whole-stream' abstracts this action for
682 ;; other parser centric routines.
683 ;;
684 (defun semantic-repeat-parse-whole-stream
685 (stream nonterm &optional returnonerror)
686 "Iteratively parse the entire stream STREAM starting with NONTERM.
687 Optional argument RETURNONERROR indicates that the parser should exit
688 with the current results on a parse error.
689 This function returns semantic tags without overlays."
690 (let ((result nil)
691 (case-fold-search semantic-case-fold)
692 nontermsym tag)
693 (while stream
694 (setq nontermsym (semantic-parse-stream stream nonterm)
695 tag (car (cdr nontermsym)))
696 (if (not nontermsym)
697 (error "Parse error @ %d" (car (cdr (car stream)))))
698 (if (eq (car nontermsym) stream)
699 (error "Parser error: Infinite loop?"))
700 (if tag
701 (if (car tag)
702 (setq tag (mapcar
703 #'(lambda (tag)
704 ;; Set the 'reparse-symbol property to
705 ;; NONTERM unless it was already setup
706 ;; by a tag expander
707 (or (semantic--tag-get-property
708 tag 'reparse-symbol)
709 (semantic--tag-put-property
710 tag 'reparse-symbol nonterm))
711 tag)
712 (semantic--tag-expand tag))
713 result (append tag result))
714 ;; No error in this case, a purposeful nil means don't
715 ;; store anything.
716 )
717 (if returnonerror
718 (setq stream nil)
719 ;; The current item in the stream didn't match, so add it to
720 ;; the list of syntax items which didn't match.
721 (setq semantic-unmatched-syntax-cache
722 (cons (car stream) semantic-unmatched-syntax-cache))
723 ))
724 ;; Designated to ignore.
725 (setq stream (car nontermsym))
726 (if stream
727 ;; Use Emacs' built-in progress reporter:
728 (and (boundp 'semantic--progress-reporter)
729 semantic--progress-reporter
730 (eq semantic-working-type 'percent)
731 (progress-reporter-update
732 semantic--progress-reporter
733 (/ (* 100 (semantic-lex-token-start (car stream)))
734 (point-max))))))
735 result))
736 \f
737 ;;; Parsing Warnings:
738 ;;
739 ;; Parsing a buffer may result in non-critical things that we should
740 ;; alert the user to without interrupting the normal flow.
741 ;;
742 ;; Any parser can use this API to provide a list of warnings during a
743 ;; parse which a user may want to investigate.
744 (defvar semantic-parser-warnings nil
745 "A list of parser warnings since the last full reparse.")
746 (make-variable-buffer-local 'semantic-parser-warnings)
747
748 (defun semantic-clear-parser-warnings ()
749 "Clear the current list of parser warnings for this buffer."
750 (setq semantic-parser-warnings nil))
751
752 (defun semantic-push-parser-warning (warning start end)
753 "Add a parser WARNING that covers text from START to END."
754 (setq semantic-parser-warnings
755 (cons (cons warning (cons start end))
756 semantic-parser-warnings)))
757
758 (defun semantic-dump-parser-warnings ()
759 "Dump any parser warnings."
760 (interactive)
761 (if semantic-parser-warnings
762 (let ((pw semantic-parser-warnings))
763 (pop-to-buffer "*Parser Warnings*")
764 (require 'pp)
765 (erase-buffer)
766 (insert (pp-to-string pw))
767 (goto-char (point-min)))
768 (message "No parser warnings.")))
769
770
771 \f
772 ;;; Compatibility:
773 ;;
774 ;; Semantic 1.x parser action helper functions, used by some parsers.
775 ;; Please move away from these functions, and try using semantic 2.x
776 ;; interfaces instead.
777 ;;
778 (defsubst semantic-bovinate-region-until-error
779 (start end nonterm &optional depth)
780 "NOTE: Use `semantic-parse-region' instead.
781
782 Bovinate between START and END starting with NONTERM.
783 Optional DEPTH specifies how many levels of parenthesis to enter.
784 This command will parse until an error is encountered, and return
785 the list of everything found until that moment.
786 This is meant for finding variable definitions at the beginning of
787 code blocks in methods. If `bovine-inner-scope' can also support
788 commands, use `semantic-bovinate-from-nonterminal-full'."
789 (semantic-parse-region start end nonterm depth t))
790 (make-obsolete 'semantic-bovinate-region-until-error
791 'semantic-parse-region)
792
793 (defsubst semantic-bovinate-from-nonterminal
794 (start end nonterm &optional depth length)
795 "Bovinate from within a nonterminal lambda from START to END.
796 Argument NONTERM is the nonterminal symbol to start with.
797 Optional argument DEPTH is the depth of lists to dive into. When used
798 in a `lambda' of a MATCH-LIST, there is no need to include a START and
799 END part.
800 Optional argument LENGTH specifies we are only interested in LENGTH
801 tokens."
802 (car-safe (cdr (semantic-parse-stream
803 (semantic-lex start end (or depth 1) length)
804 nonterm))))
805
806 (defsubst semantic-bovinate-from-nonterminal-full
807 (start end nonterm &optional depth)
808 "NOTE: Use `semantic-parse-region' instead.
809
810 Bovinate from within a nonterminal lambda from START to END.
811 Iterates until all the space between START and END is exhausted.
812 Argument NONTERM is the nonterminal symbol to start with.
813 If NONTERM is nil, use `bovine-block-toplevel'.
814 Optional argument DEPTH is the depth of lists to dive into.
815 When used in a `lambda' of a MATCH-LIST, there is no need to include
816 a START and END part."
817 (semantic-parse-region start end nonterm (or depth 1)))
818 (make-obsolete 'semantic-bovinate-from-nonterminal-full
819 'semantic-parse-region)
820
821 (provide 'semantic)
822
823 ;; Semantic-util is a part of the semantic API. Include it last
824 ;; because it depends on semantic.
825 (require 'semantic/util)
826
827 ;;; semantic.el ends here