1 ;;; semantic.el --- Semantic buffer evaluator.
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;;; 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; API for providing the semantic content of a buffer.
28 ;; The semantic API provides an interface to a series of different parser
29 ;; implementations. Each parser outputs a parse tree in a similar format
30 ;; designed to handle typical functional and object oriented languages.
33 ;; Other package depend on this value at compile time via inversion.
34 (defvar semantic-version "2.0pre7"
35 "Current version of Semantic."))
38 (require 'semantic/tag)
39 (require 'semantic/lex)
41 (declare-function inversion-test "inversion")
43 (defun semantic-require-version (major minor &optional beta)
44 "Non-nil if this version of semantic does not satisfy a specific version.
47 (MAJOR MINOR &optional BETA)
49 Values MAJOR and MINOR must be integers. BETA can be an integer, or
50 excluded if a released version is required.
52 It is assumed that if the current version is newer than that specified,
53 everything passes. Exceptions occur when known incompatibilities are
56 (inversion-test 'semantic
57 (concat major "." minor
58 (when beta (concat "beta" beta)))))
60 (defgroup semantic nil
61 "Parser Generator and parser framework."
64 (defgroup semantic-faces nil
65 "Faces used for Semantic enabled tools."
68 (require 'semantic/fw)
73 ;;; Variables and Configuration
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)
83 (defvar semantic-symbol->name-assoc-list
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)
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)
106 (defvar semantic-case-fold nil
107 "Value for `case-fold-search' when parsing.")
108 (make-variable-buffer-local 'semantic-case-fold)
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
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)
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.
126 DO NOT USE THIS VARIABLE IN PROGRAMS.
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)
135 (defvar semantic-unmatched-syntax-cache nil
136 "A cached copy of unmatched syntax tokens.")
137 (make-variable-buffer-local 'semantic-unmatched-syntax-cache)
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)
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.")
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.")
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)
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.
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)
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
188 Hook functions must take one argument, which is the new list of tags
189 associated with this buffer.
191 For language specific hooks, make sure you define this as a local hook.")
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.")
200 (defcustom semantic-dump-parse nil
201 "When non-nil, dump parsing information."
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)
209 ;;; Parse tree state management API
211 (defvar semantic-parse-tree-state 'needs-rebuild
212 "State of the current parse tree.")
213 (make-variable-buffer-local 'semantic-parse-tree-state)
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))
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))
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))
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))
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))
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))
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))
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))
251 ;;; Interfacing with the system
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."
260 (defvar semantic-init-hooks nil
261 "*Hooks run when a buffer is initialized with a parsing table.")
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)
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.")
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)
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)
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))))
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
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.
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)
318 (add-hook 'mode-local-init-hook 'semantic-new-buffer-fcn)
320 ;; Test the above hook.
321 ;;(add-hook 'semantic-init-hooks (lambda () (message "init for semantic")))
323 (defun semantic-fetch-tags-fast ()
324 "For use in a hook. When only a partial reparse is needed, reparse."
326 (if (semantic-parse-tree-needs-update-p)
327 (semantic-fetch-tags))
329 semantic--buffer-cache)
331 (if (boundp 'eval-defun-hooks)
332 (add-hook 'eval-defun-hooks 'semantic-fetch-tags-fast))
337 (condition-case nil (require 'pp) (error nil)))
339 (defvar semantic-edebug nil
340 "When non-nil, activate the interactive parsing debugger.
341 Do not set this yourself. Call `semantic-debug'.")
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)))
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
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*")
367 (insert (pp-to-string out))
368 (goto-char (point-min)))))
370 ;;; Functions of the parser plug-in API
372 ;; Overload these functions to create new types of parsers.
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
379 The default parser table used for bovine or wisent based parsers is
380 `semantic--parse-table'.
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")
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
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.
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.
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'
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.
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)))
427 (when (or (< end start) (> end (point-max)))
428 (error "Invalid parse region bounds %S, %S" start end))
430 (semantic-repeat-parse-whole-stream
431 (or (cdr (assq start semantic-lex-block-streams))
432 (semantic-lex start end depth))
433 nonterminal returnonerror))))
435 ;;; Parsing functions
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))
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))
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)
462 (semantic-clear-toplevel-cache)
463 (semantic-fetch-tags))
465 (message "semantic-unmatched-syntax-tokens:\
466 parsing of buffer canceled"))
468 semantic-unmatched-syntax-cache)
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
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))
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)
491 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
492 semantic--buffer-cache)
495 (defvar semantic-bovinate-nonterminal-check-obarray)
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)
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)
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)
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.")
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
532 (if semantic-parser-name
533 (format "%s/%s" semantic-parser-name (or arg ""))
534 (format "%s" (or arg ""))))
536 ;;; Application Parser Entry Points
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.
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."
551 ;; Is this a semantic enabled buffer?
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))
564 (let* ((gc-cons-threshold (max gc-cons-threshold 10000000))
565 (semantic-lex-block-streams nil)
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
580 ;; Move this into the incremental parser. This is a bug.
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))
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))
596 (setq res (semantic-parse-region (point-min) (point-max)))
597 (progress-reporter-done semantic--progress-reporter))
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)
611 ;; Always return the current parse tree.
612 semantic--buffer-cache)
614 (defun semantic-refresh-tags-safe ()
615 "Refreshes the current buffer's tags safely.
617 Return non-nil if the refresh was successful.
618 Return nil if there is some sort of syntax error preventing a reparse.
620 Does nothing if the current buffer doesn't need reparsing."
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.
626 ;; If the buffer was previously marked unparseable,
627 ;; then don't waste our time.
628 ((semantic-parse-tree-unparseable-p)
630 ;; The parse tree is already ok.
631 ((semantic-parse-tree-up-to-date-p)
634 (let* ((inhibit-quit nil)
639 ;; Perform the parsing.
641 (when (semantic-lex-catch-errors safe-refresh
642 (save-excursion (semantic-fetch-tags))
644 ;; If we are here, it is because the lexical step failed,
645 ;; proably due to unterminated lists or something like that.
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))
653 ;; Return if we are lexically safe
656 (defun semantic-bovinate-toplevel (&optional ignored)
657 "Backward Compatibility Function."
658 (semantic-fetch-tags))
659 (make-obsolete 'semantic-bovinate-toplevel 'semantic-fetch-tags)
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.
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
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)
676 ;;; Iterative parser helper function
678 ;; Iterative parsers are better than rule-based iterative functions
679 ;; in that they can handle obscure errors more cleanly.
681 ;; `semantic-repeat-parse-whole-stream' abstracts this action for
682 ;; other parser centric routines.
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."
691 (case-fold-search semantic-case-fold)
694 (setq nontermsym (semantic-parse-stream stream nonterm)
695 tag (car (cdr nontermsym)))
697 (error "Parse error @ %d" (car (cdr (car stream)))))
698 (if (eq (car nontermsym) stream)
699 (error "Parser error: Infinite loop?"))
704 ;; Set the 'reparse-symbol property to
705 ;; NONTERM unless it was already setup
707 (or (semantic--tag-get-property
709 (semantic--tag-put-property
710 tag 'reparse-symbol nonterm))
712 (semantic--tag-expand tag))
713 result (append tag result))
714 ;; No error in this case, a purposeful nil means don't
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))
724 ;; Designated to ignore.
725 (setq stream (car nontermsym))
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)))
737 ;;; Parsing Warnings:
739 ;; Parsing a buffer may result in non-critical things that we should
740 ;; alert the user to without interrupting the normal flow.
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)
748 (defun semantic-clear-parser-warnings ()
749 "Clear the current list of parser warnings for this buffer."
750 (setq semantic-parser-warnings nil))
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)))
758 (defun semantic-dump-parser-warnings ()
759 "Dump any parser warnings."
761 (if semantic-parser-warnings
762 (let ((pw semantic-parser-warnings))
763 (pop-to-buffer "*Parser Warnings*")
766 (insert (pp-to-string pw))
767 (goto-char (point-min)))
768 (message "No parser warnings.")))
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.
778 (defsubst semantic-bovinate-region-until-error
779 (start end nonterm &optional depth)
780 "NOTE: Use `semantic-parse-region' instead.
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)
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
800 Optional argument LENGTH specifies we are only interested in LENGTH
802 (car-safe (cdr (semantic-parse-stream
803 (semantic-lex start end (or depth 1) length)
806 (defsubst semantic-bovinate-from-nonterminal-full
807 (start end nonterm &optional depth)
808 "NOTE: Use `semantic-parse-region' instead.
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)
823 ;; Semantic-util is a part of the semantic API. Include it last
824 ;; because it depends on semantic.
825 (require 'semantic/util)
827 ;;; semantic.el ends here