]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/ctxt.el
Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[gnu-emacs] / lisp / cedet / semantic / ctxt.el
1 ;;; semantic/ctxt.el --- Context calculations for Semantic tools.
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010, 2011, 2012 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 ;; Semantic, as a tool, provides a nice list of searchable tags.
27 ;; That information can provide some very accurate answers if the current
28 ;; context of a position is known.
29 ;;
30 ;; This library provides the hooks needed for a language to specify how
31 ;; the current context is calculated.
32 ;;
33 (require 'semantic)
34
35 ;;; Code:
36 (defvar semantic-command-separation-character
37 ";"
38 "String which indicates the end of a command.
39 Used for identifying the end of a single command.")
40 (make-variable-buffer-local 'semantic-command-separation-character)
41
42 (defvar semantic-function-argument-separation-character
43 ","
44 "String which indicates the end of an argument.
45 Used for identifying arguments to functions.")
46 (make-variable-buffer-local 'semantic-function-argument-separation-character)
47
48 ;;; Local Contexts
49 ;;
50 ;; These context are nested blocks of code, such as code in an
51 ;; if clause
52 (declare-function semantic-current-tag-of-class "semantic/find")
53
54 (define-overloadable-function semantic-up-context (&optional point bounds-type)
55 "Move point up one context from POINT.
56 Return non-nil if there are no more context levels.
57 Overloaded functions using `up-context' take no parameters.
58 BOUNDS-TYPE is a symbol representing a tag class to restrict
59 movement to. If this is nil, 'function is used.
60 This will find the smallest tag of that class (function, variable,
61 type, etc) and make sure non-nil is returned if you cannot
62 go up past the bounds of that tag."
63 (require 'semantic/find)
64 (if point (goto-char point))
65 (let ((nar (semantic-current-tag-of-class (or bounds-type 'function))))
66 (if nar
67 (semantic-with-buffer-narrowed-to-tag nar (:override-with-args ()))
68 (when bounds-type
69 (error "No context of type %s to advance in" bounds-type))
70 (:override-with-args ()))))
71
72 (defun semantic-up-context-default ()
73 "Move the point up and out one context level.
74 Works with languages that use parenthetical grouping."
75 ;; By default, assume that the language uses some form of parenthetical
76 ;; do dads for their context.
77 (condition-case nil
78 (progn
79 (up-list -1)
80 nil)
81 (error t)))
82
83 (define-overloadable-function semantic-beginning-of-context (&optional point)
84 "Move POINT to the beginning of the current context.
85 Return non-nil if there is no upper context.
86 The default behavior uses `semantic-up-context'.")
87
88 (defun semantic-beginning-of-context-default (&optional point)
89 "Move POINT to the beginning of the current context via parenthesis.
90 Return non-nil if there is no upper context."
91 (if point (goto-char point))
92 (if (semantic-up-context)
93 t
94 (forward-char 1)
95 nil))
96
97 (define-overloadable-function semantic-end-of-context (&optional point)
98 "Move POINT to the end of the current context.
99 Return non-nil if there is no upper context.
100 Be default, this uses `semantic-up-context', and assumes parenthetical
101 block delimiters.")
102
103 (defun semantic-end-of-context-default (&optional point)
104 "Move POINT to the end of the current context via parenthesis.
105 Return non-nil if there is no upper context."
106 (if point (goto-char point))
107 (let ((start (point)))
108 (if (semantic-up-context)
109 t
110 ;; Go over the list, and back over the end parenthisis.
111 (condition-case nil
112 (progn
113 (forward-sexp 1)
114 (forward-char -1))
115 (error
116 ;; If an error occurs, get the current tag from the cache,
117 ;; and just go to the end of that. Make sure we end up at least
118 ;; where start was so parse-region type calls work.
119 (if (semantic-current-tag)
120 (progn
121 (goto-char (semantic-tag-end (semantic-current-tag)))
122 (when (< (point) start)
123 (goto-char start)))
124 (goto-char start))
125 t)))
126 nil))
127
128 (defun semantic-narrow-to-context ()
129 "Narrow the buffer to the extent of the current context."
130 (let (b e)
131 (save-excursion
132 (if (semantic-beginning-of-context)
133 nil
134 (setq b (point))))
135 (save-excursion
136 (if (semantic-end-of-context)
137 nil
138 (setq e (point))))
139 (if (and b e) (narrow-to-region b e))))
140
141 (defmacro semantic-with-buffer-narrowed-to-context (&rest body)
142 "Execute BODY with the buffer narrowed to the current context."
143 `(save-restriction
144 (semantic-narrow-to-context)
145 ,@body))
146 (put 'semantic-with-buffer-narrowed-to-context 'lisp-indent-function 0)
147 (add-hook 'edebug-setup-hook
148 (lambda ()
149 (def-edebug-spec semantic-with-buffer-narrowed-to-context
150 (def-body))))
151
152 ;;; Local Variables
153 ;;
154 ;;
155 (define-overloadable-function semantic-get-local-variables (&optional point)
156 "Get the local variables based on POINT's context.
157 Local variables are returned in Semantic tag format.
158 This can be overriden with `get-local-variables'."
159 ;; Disable parsing messages
160 (let ((semantic--progress-reporter nil))
161 (save-excursion
162 (if point (goto-char point))
163 (let* ((case-fold-search semantic-case-fold))
164 (:override-with-args ())))))
165
166 (defun semantic-get-local-variables-default ()
167 "Get local values from a specific context.
168 Uses the bovinator with the special top-symbol `bovine-inner-scope'
169 to collect tags, such as local variables or prototypes."
170 ;; This assumes a bovine parser. Make sure we don't do
171 ;; anything in that case.
172 (when (and semantic--parse-table (not (eq semantic--parse-table t))
173 (not (semantic-parse-tree-unparseable-p)))
174 (let ((vars (semantic-get-cache-data 'get-local-variables)))
175 (if vars
176 (progn
177 ;;(message "Found cached vars.")
178 vars)
179 (let ((vars2 nil)
180 ;; We want nothing to do with funny syntaxing while doing this.
181 (semantic-unmatched-syntax-hook nil)
182 (start (point))
183 (firstusefulstart nil)
184 )
185 (while (not (semantic-up-context (point) 'function))
186 (when (not vars)
187 (setq firstusefulstart (point)))
188 (save-excursion
189 (forward-char 1)
190 (setq vars
191 ;; Note to self: semantic-parse-region returns cooked
192 ;; but unlinked tags. File information is lost here
193 ;; and is added next.
194 (append (semantic-parse-region
195 (point)
196 (save-excursion (semantic-end-of-context) (point))
197 'bovine-inner-scope
198 nil
199 t)
200 vars))))
201 ;; Modify the tags in place.
202 (setq vars2 vars)
203 (while vars2
204 (semantic--tag-put-property (car vars2) :filename (buffer-file-name))
205 (setq vars2 (cdr vars2)))
206 ;; Hash our value into the first context that produced useful results.
207 (when (and vars firstusefulstart)
208 (let ((end (save-excursion
209 (goto-char firstusefulstart)
210 (save-excursion
211 (unless (semantic-end-of-context)
212 (point))))))
213 ;;(message "Caching values %d->%d." firstusefulstart end)
214 (semantic-cache-data-to-buffer
215 (current-buffer) firstusefulstart
216 (or end
217 ;; If the end-of-context fails,
218 ;; just use our cursor starting
219 ;; position.
220 start)
221 vars 'get-local-variables 'exit-cache-zone))
222 )
223 ;; Return our list.
224 vars)))))
225
226 (define-overloadable-function semantic-get-local-arguments (&optional point)
227 "Get arguments (variables) from the current context at POINT.
228 Parameters are available if the point is in a function or method.
229 Return a list of tags unlinked from the originating buffer.
230 Arguments are obtained by overriding `get-local-arguments', or by the
231 default function `semantic-get-local-arguments-default'. This, must
232 return a list of tags, or a list of strings that will be converted to
233 tags."
234 (save-excursion
235 (if point (goto-char point))
236 (let* ((case-fold-search semantic-case-fold)
237 (args (:override-with-args ()))
238 arg tags)
239 ;; Convert unsafe arguments to the right thing.
240 (while args
241 (setq arg (car args)
242 args (cdr args)
243 tags (cons (cond
244 ((semantic-tag-p arg)
245 ;; Return a copy of tag without overlay.
246 ;; The overlay is preserved.
247 (semantic-tag-copy arg nil t))
248 ((stringp arg)
249 (semantic--tag-put-property
250 (semantic-tag-new-variable arg nil nil)
251 :filename (buffer-file-name)))
252 (t
253 (error "Unknown parameter element %S" arg)))
254 tags)))
255 (nreverse tags))))
256
257 (defun semantic-get-local-arguments-default ()
258 "Get arguments (variables) from the current context.
259 Parameters are available if the point is in a function or method."
260 (let ((tag (semantic-current-tag)))
261 (if (and tag (semantic-tag-of-class-p tag 'function))
262 (semantic-tag-function-arguments tag))))
263
264 (define-overloadable-function semantic-get-all-local-variables (&optional point)
265 "Get all local variables for this context, and parent contexts.
266 Local variables are returned in Semantic tag format.
267 Be default, this gets local variables, and local arguments.
268 Optional argument POINT is the location to start getting the variables from.")
269
270 (defun semantic-get-all-local-variables-default (&optional point)
271 "Get all local variables for this context.
272 Optional argument POINT is the location to start getting the variables from.
273 That is a cons (LOCAL-ARGUMENTS . LOCAL-VARIABLES) where:
274
275 - LOCAL-ARGUMENTS is collected by `semantic-get-local-arguments'.
276 - LOCAL-VARIABLES is collected by `semantic-get-local-variables'."
277 (save-excursion
278 (if point (goto-char point))
279 (let ((case-fold-search semantic-case-fold))
280 (append (semantic-get-local-arguments)
281 (semantic-get-local-variables)))))
282
283 ;;; Local context parsing
284 ;;
285 ;; Context parsing assumes a series of language independent commonalities.
286 ;; These terms are used to describe those contexts:
287 ;;
288 ;; command - One command in the language.
289 ;; symbol - The symbol the cursor is on.
290 ;; This would include a series of type/field when applicable.
291 ;; assignment - The variable currently being assigned to
292 ;; function - The function call the cursor is on/in
293 ;; argument - The index to the argument the cursor is on.
294 ;;
295 ;;
296 (define-overloadable-function semantic-end-of-command ()
297 "Move to the end of the current command.
298 Be default, uses `semantic-command-separation-character'.")
299
300 (defun semantic-end-of-command-default ()
301 "Move to the end of the current command.
302 Depends on `semantic-command-separation-character' to find the
303 beginning and end of a command."
304 (semantic-with-buffer-narrowed-to-context
305 (let ((case-fold-search semantic-case-fold))
306 (with-syntax-table semantic-lex-syntax-table
307
308 (if (re-search-forward (regexp-quote semantic-command-separation-character)
309 nil t)
310 (forward-char -1)
311 ;; If there wasn't a command after this, we are the last
312 ;; command, and we are incomplete.
313 (goto-char (point-max)))))))
314
315 (define-overloadable-function semantic-beginning-of-command ()
316 "Move to the beginning of the current command.
317 Be default, uses `semantic-command-separation-character'.")
318
319 (defun semantic-beginning-of-command-default ()
320 "Move to the beginning of the current command.
321 Depends on `semantic-command-separation-character' to find the
322 beginning and end of a command."
323 (semantic-with-buffer-narrowed-to-context
324 (with-syntax-table semantic-lex-syntax-table
325 (let ((case-fold-search semantic-case-fold))
326 (skip-chars-backward semantic-command-separation-character)
327 (if (re-search-backward (regexp-quote semantic-command-separation-character)
328 nil t)
329 (goto-char (match-end 0))
330 ;; If there wasn't a command after this, we are the last
331 ;; command, and we are incomplete.
332 (goto-char (point-min)))
333 (skip-chars-forward " \t\n")
334 ))))
335
336
337 (defsubst semantic-point-at-beginning-of-command ()
338 "Return the point at the beginning of the current command."
339 (save-excursion (semantic-beginning-of-command) (point)))
340
341 (defsubst semantic-point-at-end-of-command ()
342 "Return the point at the beginning of the current command."
343 (save-excursion (semantic-end-of-command) (point)))
344
345 (defsubst semantic-narrow-to-command ()
346 "Narrow the current buffer to the current command."
347 (narrow-to-region (semantic-point-at-beginning-of-command)
348 (semantic-point-at-end-of-command)))
349
350 (defmacro semantic-with-buffer-narrowed-to-command (&rest body)
351 "Execute BODY with the buffer narrowed to the current command."
352 `(save-restriction
353 (semantic-narrow-to-command)
354 ,@body))
355 (put 'semantic-with-buffer-narrowed-to-command 'lisp-indent-function 0)
356 (add-hook 'edebug-setup-hook
357 (lambda ()
358 (def-edebug-spec semantic-with-buffer-narrowed-to-command
359 (def-body))))
360
361
362 (define-overloadable-function semantic-ctxt-current-symbol (&optional point)
363 "Return the current symbol the cursor is on at POINT in a list.
364 The symbol includes all logical parts of a complex reference.
365 For example, in C the statement:
366 this.that().entry
367
368 Would be object `this' calling method `that' which returns some structure
369 whose field `entry' is being reference. In this case, this function
370 would return the list:
371 ( \"this\" \"that\" \"entry\" )")
372
373 (defun semantic-ctxt-current-symbol-default (&optional point)
374 "Return the current symbol the cursor is on at POINT in a list.
375 This will include a list of type/field names when applicable.
376 Depends on `semantic-type-relation-separator-character'."
377 (save-excursion
378 (if point (goto-char point))
379 (let* ((fieldsep1 (mapconcat (lambda (a) (regexp-quote a))
380 semantic-type-relation-separator-character
381 "\\|"))
382 ;; NOTE: The [ \n] expression below should used \\s-, but that
383 ;; doesn't work in C since \n means end-of-comment, and isn't
384 ;; really whitespace.
385 (fieldsep (concat "[ \t\n\r]*\\(" fieldsep1 "\\)[ \t\n\r]*\\(\\w\\|\\s_\\)"))
386 (case-fold-search semantic-case-fold)
387 (symlist nil)
388 end)
389 (with-syntax-table semantic-lex-syntax-table
390 (save-excursion
391 (cond ((looking-at "\\w\\|\\s_")
392 ;; In the middle of a symbol, move to the end.
393 (forward-sexp 1))
394 ((looking-at fieldsep1)
395 ;; We are in a find spot.. do nothing.
396 nil
397 )
398 ((save-excursion
399 (and (condition-case nil
400 (progn (forward-sexp -1)
401 (forward-sexp 1)
402 t)
403 (error nil))
404 (looking-at fieldsep1)))
405 (setq symlist (list ""))
406 (forward-sexp -1)
407 ;; Skip array expressions.
408 (while (looking-at "\\s(") (forward-sexp -1))
409 (forward-sexp 1))
410 )
411 ;; Set our end point.
412 (setq end (point))
413
414 ;; Now that we have gotten started, lets do the rest.
415 (condition-case nil
416 (while (save-excursion
417 (forward-char -1)
418 (looking-at "\\w\\|\\s_"))
419 ;; We have a symbol.. Do symbol things
420 (forward-sexp -1)
421 (setq symlist (cons (buffer-substring-no-properties (point) end)
422 symlist))
423 ;; Skip the next syntactic expression backwards, then go forwards.
424 (let ((cp (point)))
425 (forward-sexp -1)
426 (forward-sexp 1)
427 ;; If we end up at the same place we started, we are at the
428 ;; beginning of a buffer, or narrowed to a command and
429 ;; have to stop.
430 (if (<= cp (point)) (error nil)))
431 (if (looking-at fieldsep)
432 (progn
433 (forward-sexp -1)
434 ;; Skip array expressions.
435 (while (and (looking-at "\\s(") (not (bobp)))
436 (forward-sexp -1))
437 (forward-sexp 1)
438 (setq end (point)))
439 (error nil))
440 )
441 (error nil)))
442 symlist))))
443
444
445 (define-overloadable-function semantic-ctxt-current-symbol-and-bounds (&optional point)
446 "Return the current symbol and bounds the cursor is on at POINT.
447 The symbol should be the same as returned by `semantic-ctxt-current-symbol'.
448 Return (PREFIX ENDSYM BOUNDS).")
449
450 (defun semantic-ctxt-current-symbol-and-bounds-default (&optional point)
451 "Return the current symbol and bounds the cursor is on at POINT.
452 Uses `semantic-ctxt-current-symbol' to calculate the symbol.
453 Return (PREFIX ENDSYM BOUNDS)."
454 (save-excursion
455 (when point (goto-char (point)))
456 (let* ((prefix (semantic-ctxt-current-symbol))
457 (endsym (car (reverse prefix)))
458 ;; @todo - Can we get this data direct from ctxt-current-symbol?
459 (bounds (save-excursion
460 (cond ((string= endsym "")
461 (cons (point) (point))
462 )
463 ((and prefix (looking-at endsym))
464 (cons (point) (progn
465 (condition-case nil
466 (forward-sexp 1)
467 (error nil))
468 (point))))
469 (prefix
470 (condition-case nil
471 (cons (progn (forward-sexp -1) (point))
472 (progn (forward-sexp 1) (point)))
473 (error nil)))
474 (t nil))))
475 )
476 (list prefix endsym bounds))))
477
478 (define-overloadable-function semantic-ctxt-current-assignment (&optional point)
479 "Return the current assignment near the cursor at POINT.
480 Return a list as per `semantic-ctxt-current-symbol'.
481 Return nil if there is nothing relevant.")
482
483 (defun semantic-ctxt-current-assignment-default (&optional point)
484 "Return the current assignment near the cursor at POINT.
485 By default, assume that \"=\" indicates an assignment."
486 (if point (goto-char point))
487 (let ((case-fold-search semantic-case-fold))
488 (with-syntax-table semantic-lex-syntax-table
489 (condition-case nil
490 (semantic-with-buffer-narrowed-to-command
491 (save-excursion
492 (skip-chars-forward " \t=")
493 (condition-case nil (forward-char 1) (error nil))
494 (re-search-backward "[^=]=\\([^=]\\|$\\)")
495 ;; We are at an equals sign. Go backwards a sexp, and
496 ;; we'll have the variable. Otherwise we threw an error
497 (forward-sexp -1)
498 (semantic-ctxt-current-symbol)))
499 (error nil)))))
500
501 (define-overloadable-function semantic-ctxt-current-function (&optional point)
502 "Return the current function call the cursor is in at POINT.
503 The function returned is the one accepting the arguments that
504 the cursor is currently in. It will not return function symbol if the
505 cursor is on the text representing that function.")
506
507 (defun semantic-ctxt-current-function-default (&optional point)
508 "Return the current function call the cursor is in at POINT.
509 The call will be identified for C like languages with the form
510 NAME ( args ... )"
511 (if point (goto-char point))
512 (let ((case-fold-search semantic-case-fold))
513 (with-syntax-table semantic-lex-syntax-table
514 (save-excursion
515 (semantic-up-context)
516 (when (looking-at "(")
517 (semantic-ctxt-current-symbol))))
518 ))
519
520 (define-overloadable-function semantic-ctxt-current-argument (&optional point)
521 "Return the index of the argument position the cursor is on at POINT.")
522
523 (defun semantic-ctxt-current-argument-default (&optional point)
524 "Return the index of the argument the cursor is on at POINT.
525 Depends on `semantic-function-argument-separation-character'."
526 (if point (goto-char point))
527 (let ((case-fold-search semantic-case-fold))
528 (with-syntax-table semantic-lex-syntax-table
529 (when (semantic-ctxt-current-function)
530 (save-excursion
531 ;; Only get the current arg index if we are in function args.
532 (let ((p (point))
533 (idx 1))
534 (semantic-up-context)
535 (while (re-search-forward
536 (regexp-quote semantic-function-argument-separation-character)
537 p t)
538 (setq idx (1+ idx)))
539 idx))))))
540
541 (defun semantic-ctxt-current-thing ()
542 "Calculate a thing identified by the current cursor position.
543 Calls previously defined `semantic-ctxt-current-...' calls until something
544 gets a match. See `semantic-ctxt-current-symbol',
545 `semantic-ctxt-current-function', and `semantic-ctxt-current-assignment'
546 for details on the return value."
547 (or (semantic-ctxt-current-symbol)
548 (semantic-ctxt-current-function)
549 (semantic-ctxt-current-assignment)))
550
551 (define-overloadable-function semantic-ctxt-current-class-list (&optional point)
552 "Return a list of tag classes that are allowed at POINT.
553 If POINT is nil, the current buffer location is used.
554 For example, in Emacs Lisp, the symbol after a ( is most likely
555 a function. In a makefile, symbols after a : are rules, and symbols
556 after a $( are variables.")
557
558 (defun semantic-ctxt-current-class-list-default (&optional point)
559 "Return a list of tag classes that are allowed at POINT.
560 Assume a functional typed language. Uses very simple rules."
561 (save-excursion
562 (if point (goto-char point))
563
564 (let ((tag (semantic-current-tag)))
565 (if tag
566 (cond ((semantic-tag-of-class-p tag 'function)
567 '(function variable type))
568 ((or (semantic-tag-of-class-p tag 'type)
569 (semantic-tag-of-class-p tag 'variable))
570 '(type))
571 (t nil))
572 '(type)
573 ))))
574
575 ;;;###autoload
576 (define-overloadable-function semantic-ctxt-current-mode (&optional point)
577 "Return the major mode active at POINT.
578 POINT defaults to the value of point in current buffer.
579 You should override this function in multiple mode buffers to
580 determine which major mode apply at point.")
581
582 (defun semantic-ctxt-current-mode-default (&optional point)
583 "Return the major mode active at POINT.
584 POINT defaults to the value of point in current buffer.
585 This default implementation returns the current major mode."
586 major-mode)
587 \f
588 ;;; Scoped Types
589 ;;
590 ;; Scoped types are types that the current code would have access to.
591 ;; The come from the global namespace or from special commands such as "using"
592 (define-overloadable-function semantic-ctxt-scoped-types (&optional point)
593 "Return a list of type names currently in scope at POINT.
594 The return value can be a mixed list of either strings (names of
595 types that are in scope) or actual tags (type declared locally
596 that may or may not have a name.)")
597
598 (defun semantic-ctxt-scoped-types-default (&optional point)
599 "Return a list of scoped types by name for the current context at POINT.
600 This is very different for various languages, and does nothing unless
601 overridden."
602 nil)
603
604 (define-overloadable-function semantic-ctxt-imported-packages (&optional point)
605 "Return a list of package tags or names which are being imported at POINT.
606 The return value is a list of strings which are package names
607 that are implied in code. Thus a C++ symbol:
608 foo::bar();
609 where there is a statement such as:
610 using baz;
611 means that the first symbol might be:
612 baz::foo::bar();"
613 nil)
614
615 (provide 'semantic/ctxt)
616
617 ;; Local variables:
618 ;; generated-autoload-file: "loaddefs.el"
619 ;; generated-autoload-load-name: "semantic/ctxt"
620 ;; End:
621
622 ;; arch-tag: 04f3ae3c-78bb-40ca-b112-ba77f5e4ea88
623 ;;; semantic/ctxt.el ends here