Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cedet / semantic / lex-spp.el
blob67f944a09ae107ef8991e0860b76f4e6df9a5347
1 ;;; semantic/lex-spp.el --- Semantic Lexical Pre-processor
3 ;; Copyright (C) 2006-2014 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; The Semantic Preprocessor works with semantic-lex to provide a phase
25 ;; during lexical analysis to do the work of a pre-processor.
27 ;; A pre-processor identifies lexical syntax mixed in with another language
28 ;; and replaces some keyword tokens with streams of alternate tokens.
30 ;; If you use SPP in your language, be sure to specify this in your
31 ;; semantic language setup function:
33 ;; (add-hook 'semantic-lex-reset-functions 'semantic-lex-spp-reset-hook nil t)
36 ;; Special Lexical Tokens:
38 ;; There are several special lexical tokens that are used by the
39 ;; Semantic PreProcessor lexer. They are:
41 ;; Declarations:
42 ;; spp-macro-def - A definition of a lexical macro.
43 ;; spp-macro-undef - A removal of a definition of a lexical macro.
44 ;; spp-system-include - A system level include file
45 ;; spp-include - An include file
46 ;; spp-concat - A lexical token representing textual concatenation
47 ;; of symbol parts.
49 ;; Operational tokens:
50 ;; spp-arg-list - Represents an argument list to a macro.
51 ;; spp-symbol-merge - A request for multiple symbols to be textually merged.
53 ;;; TODO:
55 ;; Use `semantic-push-parser-warning' for situations where there are likely
56 ;; macros that are undefined unexpectedly, or other problem.
58 ;; TODO:
60 ;; Try to handle the case of:
62 ;; #define NN namespace nn {
63 ;; #define NN_END }
65 ;; NN
66 ;; int mydecl() {}
67 ;; NN_END
70 (require 'semantic)
71 (require 'semantic/lex)
73 (declare-function semantic-c-end-of-macro "semantic/bovine/c")
75 ;;; Code:
76 (defvar semantic-lex-spp-macro-symbol-obarray nil
77 "Table of macro keywords used by the Semantic Preprocessor.
78 These symbols will be used in addition to those in
79 `semantic-lex-spp-dynamic-macro-symbol-obarray'.")
80 (make-variable-buffer-local 'semantic-lex-spp-macro-symbol-obarray)
82 (defvar semantic-lex-spp-project-macro-symbol-obarray nil
83 "Table of macro keywords for this project.
84 These symbols will be used in addition to those in
85 `semantic-lex-spp-dynamic-macro-symbol-obarray'.")
86 (make-variable-buffer-local 'semantic-lex-spp-project-macro-symbol-obarray)
88 (defvar semantic-lex-spp-dynamic-macro-symbol-obarray nil
89 "Table of macro keywords used during lexical analysis.
90 Macros are lexical symbols which are replaced by other lexical
91 tokens during lexical analysis. During analysis symbols can be
92 added and removed from this symbol table.")
93 (make-variable-buffer-local 'semantic-lex-spp-dynamic-macro-symbol-obarray)
95 (defvar semantic-lex-spp-dynamic-macro-symbol-obarray-stack nil
96 "A stack of obarrays for temporarily scoped macro values.")
97 (make-variable-buffer-local 'semantic-lex-spp-dynamic-macro-symbol-obarray-stack)
99 (defvar semantic-lex-spp-expanded-macro-stack nil
100 "The stack of lexical SPP macros we have expanded.")
101 ;; The above is not buffer local. Some macro expansions need to be
102 ;; dumped into a secondary buffer for re-lexing.
104 ;;; NON-RECURSIVE MACRO STACK
105 ;; C Pre-processor does not allow recursive macros. Here are some utils
106 ;; for managing the symbol stack of where we've been.
108 (defmacro semantic-lex-with-macro-used (name &rest body)
109 "With the macro NAME currently being expanded, execute BODY.
110 Pushes NAME into the macro stack. The above stack is checked
111 by `semantic-lex-spp-symbol' to not return true for any symbol
112 currently being expanded."
113 `(unwind-protect
114 (progn
115 (push ,name semantic-lex-spp-expanded-macro-stack)
116 ,@body)
117 (pop semantic-lex-spp-expanded-macro-stack)))
118 (put 'semantic-lex-with-macro-used 'lisp-indent-function 1)
120 (add-hook
121 'edebug-setup-hook
122 #'(lambda ()
124 (def-edebug-spec semantic-lex-with-macro-used
125 (symbolp def-body)
130 ;;; MACRO TABLE UTILS
132 ;; The dynamic macro table is a buffer local variable that is modified
133 ;; during the analysis. OBARRAYs are used, so the language must
134 ;; have symbols that are compatible with Emacs Lisp symbols.
136 (defsubst semantic-lex-spp-symbol (name)
137 "Return spp symbol with NAME or nil if not found.
138 The search priority is:
139 1. DYNAMIC symbols
140 2. PROJECT specified symbols.
141 3. SYSTEM specified symbols."
142 (and
143 ;; Only strings...
144 (stringp name)
145 ;; Make sure we don't recurse.
146 (not (member name semantic-lex-spp-expanded-macro-stack))
147 ;; Do the check of the various tables.
149 ;; DYNAMIC
150 (and (arrayp semantic-lex-spp-dynamic-macro-symbol-obarray)
151 (intern-soft name semantic-lex-spp-dynamic-macro-symbol-obarray))
152 ;; PROJECT
153 (and (arrayp semantic-lex-spp-project-macro-symbol-obarray)
154 (intern-soft name semantic-lex-spp-project-macro-symbol-obarray))
155 ;; SYSTEM
156 (and (arrayp semantic-lex-spp-macro-symbol-obarray)
157 (intern-soft name semantic-lex-spp-macro-symbol-obarray))
158 ;; ...
161 (defsubst semantic-lex-spp-symbol-p (name)
162 "Return non-nil if a keyword with NAME exists in any keyword table."
163 (if (semantic-lex-spp-symbol name)
166 (defsubst semantic-lex-spp-dynamic-map ()
167 "Return the dynamic macro map for the current buffer."
168 (or semantic-lex-spp-dynamic-macro-symbol-obarray
169 (setq semantic-lex-spp-dynamic-macro-symbol-obarray
170 (make-vector 13 0))))
172 (defsubst semantic-lex-spp-dynamic-map-stack ()
173 "Return the dynamic macro map for the current buffer."
174 (or semantic-lex-spp-dynamic-macro-symbol-obarray-stack
175 (setq semantic-lex-spp-dynamic-macro-symbol-obarray-stack
176 (make-vector 13 0))))
178 (defun semantic-lex-spp-value-valid-p (value)
179 "Return non-nil if VALUE is valid."
180 (or (null value)
181 (stringp value)
182 (and (consp value)
183 (or (semantic-lex-token-p (car value))
184 (eq (car (car value)) 'spp-arg-list)))))
186 (defvar semantic-lex-spp-debug-symbol nil
187 "A symbol to break on if it is being set somewhere.")
189 (defun semantic-lex-spp-enable-debug-symbol (sym)
190 "Enable debugging for symbol SYM.
191 Disable debugging by entering nothing."
192 (interactive "sSymbol: ")
193 (if (string= sym "")
194 (setq semantic-lex-spp-debug-symbol nil)
195 (setq semantic-lex-spp-debug-symbol sym)))
197 (defmacro semantic-lex-spp-validate-value (name value)
198 "Validate the NAME and VALUE of a macro before it is set."
199 ; `(progn
200 ; (when (not (semantic-lex-spp-value-valid-p ,value))
201 ; (error "Symbol \"%s\" with bogus value %S" ,name ,value))
202 ; (when (and semantic-lex-spp-debug-symbol
203 ; (string= semantic-lex-spp-debug-symbol name))
204 ; (debug))
209 (defun semantic-lex-spp-symbol-set (name value &optional obarray-in)
210 "Set value of spp symbol with NAME to VALUE and return VALUE.
211 If optional OBARRAY-IN is non-nil, then use that obarray instead of
212 the dynamic map."
213 (semantic-lex-spp-validate-value name value)
214 (if (and (stringp value) (string= value "")) (setq value nil))
215 (set (intern name (or obarray-in
216 (semantic-lex-spp-dynamic-map)))
217 value))
219 (defsubst semantic-lex-spp-symbol-remove (name &optional obarray)
220 "Remove the spp symbol with NAME.
221 If optional OBARRAY is non-nil, then use that obarray instead of
222 the dynamic map."
223 (unintern name (or obarray
224 (semantic-lex-spp-dynamic-map))))
226 (defun semantic-lex-spp-symbol-push (name value)
227 "Push macro NAME with VALUE into the map.
228 Reverse with `semantic-lex-spp-symbol-pop'."
229 (semantic-lex-spp-validate-value name value)
230 (let* ((map (semantic-lex-spp-dynamic-map))
231 (stack (semantic-lex-spp-dynamic-map-stack))
232 (mapsym (intern name map))
233 (stacksym (intern name stack))
234 (mapvalue (when (boundp mapsym) (symbol-value mapsym)))
236 (when (boundp mapsym)
237 ;; Make sure there is a stack
238 (if (not (boundp stacksym)) (set stacksym nil))
239 ;; If there is a value to push, then push it.
240 (set stacksym (cons mapvalue (symbol-value stacksym)))
242 ;; Set our new value here.
243 (set mapsym value)
246 (defun semantic-lex-spp-symbol-pop (name)
247 "Pop macro NAME from the stackmap into the orig map.
248 Reverse with `semantic-lex-spp-symbol-pop'."
249 (let* ((map (semantic-lex-spp-dynamic-map))
250 (stack (semantic-lex-spp-dynamic-map-stack))
251 (mapsym (intern name map))
252 (stacksym (intern name stack))
253 (oldvalue nil)
255 (if (or (not (boundp stacksym) )
256 (= (length (symbol-value stacksym)) 0))
257 ;; Nothing to pop, remove it.
258 (unintern name map)
259 ;; If there is a value to pop, then add it to the map.
260 (set mapsym (car (symbol-value stacksym)))
261 (set stacksym (cdr (symbol-value stacksym)))
264 (defsubst semantic-lex-spp-symbol-stream (name)
265 "Return replacement stream of macro with NAME."
266 (let ((spp (semantic-lex-spp-symbol name)))
267 (if spp
268 (symbol-value spp))))
270 (defun semantic-lex-make-spp-table (specs)
271 "Convert spp macro list SPECS into an obarray and return it.
272 SPECS must be a list of (NAME . REPLACEMENT) elements, where:
274 NAME is the name of the spp macro symbol to define.
275 REPLACEMENT a string that would be substituted in for NAME."
277 ;; Create the symbol hash table
278 (let ((semantic-lex-spp-macro-symbol-obarray (make-vector 13 0))
279 spec)
280 ;; fill it with stuff
281 (while specs
282 (setq spec (car specs)
283 specs (cdr specs))
284 (semantic-lex-spp-symbol-set
285 (car spec)
286 (cdr spec)
287 semantic-lex-spp-macro-symbol-obarray))
288 semantic-lex-spp-macro-symbol-obarray))
290 (defun semantic-lex-spp-save-table ()
291 "Return a list of spp macros and values.
292 The return list is meant to be saved in a semanticdb table."
293 (let (macros)
294 (when (arrayp semantic-lex-spp-dynamic-macro-symbol-obarray)
295 (mapatoms
296 #'(lambda (symbol)
297 (setq macros (cons (cons (symbol-name symbol)
298 (symbol-value symbol))
299 macros)))
300 semantic-lex-spp-dynamic-macro-symbol-obarray))
301 macros))
303 (defun semantic-lex-spp-macros ()
304 "Return a list of spp macros as Lisp symbols.
305 The value of each symbol is the replacement stream."
306 (let (macros)
307 (when (arrayp semantic-lex-spp-macro-symbol-obarray)
308 (mapatoms
309 #'(lambda (symbol)
310 (setq macros (cons symbol macros)))
311 semantic-lex-spp-macro-symbol-obarray))
312 (when (arrayp semantic-lex-spp-project-macro-symbol-obarray)
313 (mapatoms
314 #'(lambda (symbol)
315 (setq macros (cons symbol macros)))
316 semantic-lex-spp-project-macro-symbol-obarray))
317 (when (arrayp semantic-lex-spp-dynamic-macro-symbol-obarray)
318 (mapatoms
319 #'(lambda (symbol)
320 (setq macros (cons symbol macros)))
321 semantic-lex-spp-dynamic-macro-symbol-obarray))
322 macros))
324 (defun semantic-lex-spp-set-dynamic-table (new-entries)
325 "Set the dynamic symbol table to NEW-ENTRIES.
326 For use with semanticdb restoration of state."
327 (dolist (e new-entries)
328 ;; Default obarray for below is the dynamic map.
329 (semantic-lex-spp-symbol-set (car e) (cdr e))))
331 (defun semantic-lex-spp-reset-hook (start end)
332 "Reset anything needed by SPP for parsing.
333 In this case, reset the dynamic macro symbol table if
334 START is (point-min).
335 END is not used."
336 (when (= start (point-min))
337 (setq semantic-lex-spp-dynamic-macro-symbol-obarray nil
338 semantic-lex-spp-dynamic-macro-symbol-obarray-stack nil
339 ;; This shouldn't not be nil, but reset just in case.
340 semantic-lex-spp-expanded-macro-stack nil)
343 ;;; MACRO EXPANSION: Simple cases
345 ;; If a user fills in the table with simple strings, we can
346 ;; support that by converting them into tokens with the
347 ;; various analyzers that are available.
349 (defun semantic-lex-spp-extract-regex-and-compare (analyzer value)
350 "Extract a regexp from an ANALYZER and use to match VALUE.
351 Return non-nil if it matches"
352 (let* ((condition (car analyzer))
353 (regex (cond ((eq (car condition) 'looking-at)
354 (nth 1 condition))
356 nil))))
357 (when regex
358 (string-match regex value))
361 (defun semantic-lex-spp-simple-macro-to-macro-stream (val beg end argvalues)
362 "Convert lexical macro contents VAL into a macro expansion stream.
363 These are for simple macro expansions that a user may have typed in directly.
364 As such, we need to analyze the input text, to figure out what kind of real
365 lexical token we should be inserting in its place.
367 Argument VAL is the value of some macro to be converted into a stream.
368 BEG and END are the token bounds of the macro to be expanded
369 that will somehow gain a much longer token stream.
370 ARGVALUES are values for any arg list, or nil."
371 (cond
372 ;; We perform a replacement. Technically, this should
373 ;; be a full lexical step over the "val" string, but take
374 ;; a guess that its just a keyword or existing symbol.
376 ;; Probably a really bad idea. See how it goes.
377 ((semantic-lex-spp-extract-regex-and-compare
378 semantic-lex-symbol-or-keyword val)
379 (semantic-lex-push-token
380 (semantic-lex-token (or (semantic-lex-keyword-p val) 'symbol)
381 beg end
382 val)))
384 ;; Ok, the rest of these are various types of syntax.
385 ;; Conveniences for users that type in their symbol table.
386 ((semantic-lex-spp-extract-regex-and-compare
387 semantic-lex-punctuation val)
388 (semantic-lex-token 'punctuation beg end val))
389 ((semantic-lex-spp-extract-regex-and-compare
390 semantic-lex-number val)
391 (semantic-lex-token 'number beg end val))
392 ((semantic-lex-spp-extract-regex-and-compare
393 semantic-lex-paren-or-list val)
394 (semantic-lex-token 'semantic-list beg end val))
395 ((semantic-lex-spp-extract-regex-and-compare
396 semantic-lex-string val)
397 (semantic-lex-token 'string beg end val))
398 (t nil)
401 ;;; MACRO EXPANSION : Lexical token replacement
403 ;; When substituting in a macro from a token stream of formatted
404 ;; semantic lex tokens, things can be much more complicated.
406 ;; Some macros have arguments that get set into the dynamic macro
407 ;; table during replacement.
409 ;; In general, the macro tokens are substituted into the regular
410 ;; token stream, but placed under the characters of the original
411 ;; macro symbol.
413 ;; Argument lists are saved as a lexical token at the beginning
414 ;; of a replacement value.
416 (defun semantic-lex-spp-one-token-to-txt (tok &optional blocktok)
417 "Convert the token TOK into a string.
418 If TOK is made of multiple tokens, convert those to text. This
419 conversion is needed if a macro has a merge symbol in it that
420 combines the text of two previously distinct symbols. For
421 example, in c:
423 #define (a,b) a ## b;
425 If optional string BLOCKTOK matches the expanded value, then do not
426 continue processing recursively."
427 (let ((txt (semantic-lex-token-text tok))
428 (sym nil)
430 (cond
431 ;; Recursion prevention
432 ((and (stringp blocktok) (string= txt blocktok))
433 blocktok)
434 ;; A complex symbol
435 ((and (eq (car tok) 'symbol)
436 (setq sym (semantic-lex-spp-symbol txt))
437 (not (semantic-lex-spp-macro-with-args (symbol-value sym)))
439 ;; Now that we have a symbol,
440 (let ((val (symbol-value sym)))
441 (cond
442 ;; This is another lexical token.
443 ((and (consp val)
444 (symbolp (car val)))
445 (semantic-lex-spp-one-token-to-txt val txt))
446 ;; This is a list of tokens.
447 ((and (consp val)
448 (consp (car val))
449 (symbolp (car (car val))))
450 (mapconcat (lambda (subtok)
451 (semantic-lex-spp-one-token-to-txt subtok))
453 ""))
454 ;; If val is nil, that's probably wrong.
455 ;; Found a system header case where this was true.
456 ((null val) "")
457 ;; Debug weird stuff.
458 (t (debug)))
460 ((stringp txt)
461 txt)
462 (t nil))
465 (defun semantic-lex-spp-macro-with-args (val)
466 "If the macro value VAL has an argument list, return the arglist."
467 (when (and val (consp val) (consp (car val))
468 (eq 'spp-arg-list (car (car val))))
469 (car (cdr (car val)))))
471 (defun semantic-lex-spp-token-macro-to-macro-stream (val beg end argvalues)
472 "Convert lexical macro contents VAL into a macro expansion stream.
473 Argument VAL is the value of some macro to be converted into a stream.
474 BEG and END are the token bounds of the macro to be expanded
475 that will somehow gain a much longer token stream.
476 ARGVALUES are values for any arg list, or nil.
477 See comments in code for information about how token streams are processed
478 and what valid VAL values are."
480 ;; A typical VAL value might be either a stream of tokens.
481 ;; Tokens saved into a macro stream always includes the text from the
482 ;; buffer, since the locations specified probably don't represent
483 ;; that text anymore, or even the same buffer.
485 ;; CASE 1: Simple token stream
487 ;; #define SUPER mysuper::
488 ;; ==>
489 ;;((symbol "mysuper" 480 . 487)
490 ;; (punctuation ":" 487 . 488)
491 ;; (punctuation ":" 488 . 489))
493 ;; CASE 2: Token stream with argument list
495 ;; #define INT_FCN(name) int name (int in)
496 ;; ==>
497 ;; ((spp-arg-list ("name") 558 . 564)
498 ;; (INT "int" 565 . 568)
499 ;; (symbol "name" 569 . 573)
500 ;; (semantic-list "(int in)" 574 . 582))
502 ;; In the second case, a macro with an argument list as the args as the
503 ;; first entry.
505 ;; CASE 3: Symbol text merge
507 ;; #define TMP(a) foo_ ## a
508 ;; ==>
509 ;; ((spp-arg-list ("a") 20 . 23)
510 ;; (spp-symbol-merge ((symbol "foo_" 24 . 28) (symbol "a" 32 . 33))
511 ;; 24 . 33))
513 ;; Usually in conjunction with a macro with an argument, merging symbol
514 ;; parts is a way of fabricating new symbols from pieces inside the macro.
515 ;; These macros use `spp-symbol-merge' tokens whose TEXT part is another
516 ;; token stream. This sub-stream ought to consist of only 2 SYMBOL pieces,
517 ;; though I suppose keywords might be ok. The end result of this example
518 ;; merge symbol would be (symbol "foo_A" 24 . 33) where A is the symbol
519 ;; passed in from the arg list "a".
521 ;; CASE 4: Nested token streams
523 ;; #define FOO(f) f
524 ;; #define BLA bla FOO(foo)
525 ;; ==>
526 ;; ((INT "int" 82 . 85)
527 ;; (symbol "FOO" 86 . 89)
528 ;; (semantic-list "(foo)" 89 . 94))
530 ;; Nested token FOO shows up in the table of macros, and gets replace
531 ;; inline. This is the same as case 2.
533 ;; CASE 5: Macros which open a scope without closing it
535 ;; #define __NAMESPACE_STD namespace std {
536 ;; #define __NAMESPACE_END }
537 ;; ==>
538 ;; ((NAMESPACE "namespace" 140 . 149)
539 ;; (symbol "std" 150 . 153)
540 ;; (open-paren "{" 154 . 155))
542 ;; Note that we get a single 'open-paren' instead of a
543 ;; 'semantic-list', which is because we use
544 ;; 'semantic-lex-spp-paren-or-list' instead of
545 ;; 'semantic-lex-paren-or-list' in our spp-lexer. To keep things
546 ;; reasonably simple, we assume that such an open scope will always
547 ;; be closed by another macro (see
548 ;; `semantic-lex-spp-find-closing-macro'). We generate a
549 ;; 'semantic-list' to this closing macro, and we leave an overlay
550 ;; which contains information how far we got into the macro's
551 ;; stream (since it might open several scopes).
553 (let* ((arglist (semantic-lex-spp-macro-with-args val))
554 (argalist nil)
555 (val-tmp nil)
556 (v nil)
557 (sppov (semantic-lex-spp-get-overlay beg))
558 (sppinfo (when sppov (overlay-get sppov 'semantic-spp))))
560 ;; First, check if we were already here and left information
561 (when sppinfo
562 ;; Advance in the tokens as far as we got last time
563 (when (numberp (car sppinfo))
564 (while (and val
565 (>= (car sppinfo) (car (last (car val)))))
566 (setq val (cdr val))))
567 ;; And push an open paren
568 (semantic-lex-push-token
569 (semantic-lex-token 'open-paren beg (1+ beg) "{"))
570 (setq semantic-lex-current-depth (1+ semantic-lex-current-depth))
571 (unless val
572 ;; We reached the end of this macro, so delete overlay
573 (delete-overlay sppov)))
575 ;; CASE 2: Dealing with the arg list.
576 (when (and val arglist)
577 ;; Skip the arg list.
578 (when (eq (caar val) 'spp-arg-list)
579 (setq val (cdr val)))
581 ;; Push args into the replacement list.
582 (let ((AV argvalues))
583 (dolist (A arglist)
584 (let* ((argval (car AV)))
586 (semantic-lex-spp-symbol-push A argval)
587 (setq argalist (cons (cons A argval) argalist))
588 (setq AV (cdr AV)))))
591 ;; Set val-tmp after stripping arguments.
592 (setq val-tmp val)
594 ;; CASE 1: Push everything else onto the list.
595 ;; Once the arg list is stripped off, CASE 2 is the same
596 ;; as CASE 1.
597 (while val-tmp
598 (setq v (car val-tmp))
599 (setq val-tmp (cdr val-tmp))
601 (let* (;; The text of the current lexical token.
602 (txt (car (cdr v)))
603 ;; Try to convert txt into a macro declaration. If it is
604 ;; not a macro, use nil.
605 (txt-macro-or-nil (semantic-lex-spp-symbol txt))
606 ;; If our current token is a macro, then pull off the argument
607 ;; list.
608 (macro-and-args
609 (when txt-macro-or-nil
610 (semantic-lex-spp-macro-with-args (symbol-value txt-macro-or-nil)))
612 ;; We need to peek at the next token when testing for
613 ;; used macros with arg lists.
614 (next-tok-class (semantic-lex-token-class (car val-tmp)))
617 (cond
618 ;; CASE 3: Merge symbols together.
619 ((eq (semantic-lex-token-class v) 'spp-symbol-merge)
620 (let ((newsym (semantic-lex-spp-symbol-merge txt)))
621 (semantic-lex-push-token
622 (semantic-lex-token 'symbol beg end newsym))
625 ;; CASE 2: Argument replacement. If a discovered symbol is in
626 ;; the active list of arguments, then we need to substitute
627 ;; in the new value.
628 ((and (eq (semantic-lex-token-class v) 'symbol) txt-macro-or-nil
629 (or (and macro-and-args (eq next-tok-class 'semantic-list))
630 (not macro-and-args))
632 (let ((AV nil))
633 (when macro-and-args
634 (setq AV
635 (semantic-lex-spp-stream-for-arglist (car val-tmp)))
636 ;; We used up these args. Pull from the stream.
637 (setq val-tmp (cdr val-tmp))
640 (semantic-lex-with-macro-used txt
641 ;; Don't recurse directly into this same fcn, because it is
642 ;; convenient to have plain string replacements too.
643 (semantic-lex-spp-macro-to-macro-stream
644 (symbol-value txt-macro-or-nil)
645 beg end AV))
648 ;; This is a HACK for the C parser. The 'macros text
649 ;; property is some storage so that the parser can do
650 ;; some C specific text manipulations.
651 ((eq (semantic-lex-token-class v) 'semantic-list)
652 ;; Push our arg list onto the semantic list.
653 (when argalist
654 (setq txt (concat txt)) ; Copy the text.
655 (put-text-property 0 1 'macros argalist txt))
656 (semantic-lex-push-token
657 (semantic-lex-token (semantic-lex-token-class v) beg end txt))
659 ;; CASE 5: Macro which opens a scope
660 ((eq (semantic-lex-token-class v) 'open-paren)
661 ;; We assume that the scope will be closed by another macro.
662 ;; (Everything else would be a terrible idea anyway.)
663 (let* ((endpoint (semantic-lex-spp-find-closing-macro))
664 (ov (when endpoint
665 (or sppov
666 (make-overlay beg end)))))
667 (when ov
668 ;; Generate a semantic-list which spans to the end of
669 ;; the closing macro
670 (semantic-lex-push-token
671 (semantic-lex-token 'semantic-list beg endpoint))
672 ;; The rest of the current macro's stream will be parsed
673 ;; next time.
674 (setq val-tmp nil)
675 ;; Store our current state were we are in the macro and
676 ;; the endpoint.
677 (overlay-put ov 'semantic-spp
678 (cons (car (last v)) endpoint)))))
679 ((eq (semantic-lex-token-class v) 'close-paren)
680 ;; Macro which closes a scope
681 ;; Just push the close paren, but also decrease depth
682 (semantic-lex-push-token
683 (semantic-lex-token 'close-paren beg end txt))
684 (setq semantic-lex-current-depth (1- semantic-lex-current-depth)))
685 ;; CASE 1: Just another token in the stream.
687 ;; Nothing new.
688 (semantic-lex-push-token
689 (semantic-lex-token (semantic-lex-token-class v) beg end txt))
693 ;; CASE 2: The arg list we pushed onto the symbol table
694 ;; must now be removed.
695 (dolist (A arglist)
696 (semantic-lex-spp-symbol-pop A))
699 (defun semantic-lex-spp-symbol-merge (txt)
700 "Merge the tokens listed in TXT.
701 TXT might contain further 'spp-symbol-merge, which will
702 be merged recursively."
703 ;; We need to merge the tokens in the 'text segment together,
704 ;; and produce a single symbol from it.
705 (mapconcat (lambda (tok)
706 (cond
707 ((eq (car tok) 'symbol)
708 (semantic-lex-spp-one-token-to-txt tok))
709 ((eq (car tok) 'spp-symbol-merge)
710 ;; Call recursively for multiple merges, like
711 ;; #define FOO(a) foo##a##bar
712 (semantic-lex-spp-symbol-merge (cadr tok)))
714 (message "Invalid merge macro encountered; \
715 will return empty string instead.")
716 "")))
718 ""))
720 (defun semantic-lex-spp-find-closing-macro ()
721 "Find next macro which closes a scope through a close-paren.
722 Returns position with the end of that macro."
723 (let ((macros (semantic-lex-spp-macros))
724 (cmacro-regexp "\\(")
725 (case-fold-search nil))
726 ;; Build a regexp which search for all macros with a closing
727 ;; paren, and search for it.
728 (dolist (cur macros)
729 (let ((stream (symbol-value cur)))
730 (when (and (listp stream) (listp (car stream)))
731 (while stream
732 (if (and (eq (caar stream) 'close-paren)
733 (string= (nth 1 (car stream)) "}"))
734 (setq cmacro-regexp (concat cmacro-regexp (symbol-name cur) "\\|")
735 stream nil)
736 (setq stream (cdr-safe stream)))))))
737 (when cmacro-regexp
738 (save-excursion
739 (when (re-search-forward
740 (concat (substring cmacro-regexp 0 -2) "\\)[^0-9a-zA-Z_]") nil t)
741 (point))))))
743 (defun semantic-lex-spp-get-overlay (&optional point)
744 "Return first overlay which has a 'semantic-spp property."
745 (let ((overlays (overlays-at (or point (point)))))
746 (while (and overlays
747 (null (overlay-get (car overlays) 'semantic-spp)))
748 (setq overlays (cdr overlays)))
749 (car-safe overlays)))
751 ;;; Macro Merging
753 ;; Used when token streams from different macros include each other.
754 ;; Merged macro streams perform in place replacements.
756 (defun semantic-lex-spp-merge-streams (raw-stream)
757 "Merge elements from the RAW-STREAM together.
758 Handle spp-concat symbol concatenation.
759 Handle Nested macro replacements.
760 Return the cooked stream."
761 (let ((cooked-stream nil))
762 ;; Merge the stream
763 (while raw-stream
764 (cond ((eq (semantic-lex-token-class (car raw-stream)) 'spp-concat)
765 ;; handle hashhash, by skipping it.
766 (setq raw-stream (cdr raw-stream))
767 ;; Now merge the symbols.
768 (let ((prev-tok (car cooked-stream))
769 (next-tok (car raw-stream)))
770 (setq cooked-stream (cdr cooked-stream))
771 (push (semantic-lex-token
772 'spp-symbol-merge
773 (semantic-lex-token-start prev-tok)
774 (semantic-lex-token-end next-tok)
775 (list prev-tok next-tok))
776 cooked-stream)
779 (push (car raw-stream) cooked-stream))
781 (setq raw-stream (cdr raw-stream))
784 (nreverse cooked-stream))
787 ;;; MACRO EXPANSION
789 ;; There are two types of expansion.
791 ;; 1. Expansion using a value made up of lexical tokens.
792 ;; 2. User input replacement from a plain string.
794 (defun semantic-lex-spp-macro-to-macro-stream (val beg end argvalues)
795 "Convert lexical macro contents VAL into a macro expansion stream.
796 Argument VAL is the value of some macro to be converted into a stream.
797 BEG and END are the token bounds of the macro to be expanded
798 that will somehow gain a much longer token stream.
799 ARGVALUES are values for any arg list, or nil."
800 (cond
801 ;; If val is nil, then just skip it.
802 ((null val) t)
803 ;; If it is a token, then return that token rebuilt.
804 ((and (consp val) (car val) (symbolp (car val)))
805 (semantic-lex-push-token
806 (semantic-lex-token (car val) beg end (semantic-lex-token-text val))))
807 ;; Test for a token list.
808 ((and (consp val) (consp (car val)) (car (car val))
809 (symbolp (car (car val))))
810 (semantic-lex-spp-token-macro-to-macro-stream val beg end argvalues))
811 ;; Test for miscellaneous strings.
812 ((stringp val)
813 (semantic-lex-spp-simple-macro-to-macro-stream val beg end argvalues))
816 ;;; --------------------------------------------------------
818 ;;; ANALYZERS:
821 ;;; Symbol Is Macro
823 ;; An analyzer that will push tokens from a macro in place
824 ;; of the macro symbol.
826 (defun semantic-lex-spp-anlyzer-do-replace (sym val beg end)
827 "Do the lexical replacement for SYM with VAL.
828 Argument BEG and END specify the bounds of SYM in the buffer."
829 (if (not val)
830 (setq semantic-lex-end-point end)
831 (let ((arg-in nil)
832 (arg-parsed nil)
833 (arg-split nil)
836 ;; Check for arguments.
837 (setq arg-in (semantic-lex-spp-macro-with-args val))
839 (when arg-in
840 (save-excursion
841 (goto-char end)
842 (setq arg-parsed
843 (semantic-lex-spp-one-token-and-move-for-macro
844 ;; NOTE: This used to be (point-at-eol), but
845 ;; that was too close for multi-line arguments
846 ;; to a macro. Point max may be too far if there
847 ;; is a typo in the buffer.
849 ;; Look here for performance issues while a user is typing
850 ;; incomplete code.
851 (point-max)))
852 (setq end (semantic-lex-token-end arg-parsed))
854 (when (and (listp arg-parsed) (eq (car arg-parsed) 'semantic-list))
855 (setq arg-split
856 ;; Use lex to split up the contents of the argument list.
857 (semantic-lex-spp-stream-for-arglist arg-parsed)
861 ;; if we have something to sub in, then do it.
862 (semantic-lex-spp-macro-to-macro-stream val beg end arg-split)
863 (setq semantic-lex-end-point end)
867 (defvar semantic-lex-spp-replacements-enabled t
868 "Non-nil means do replacements when finding keywords.
869 Disable this only to prevent recursive expansion issues.")
871 (defun semantic-lex-spp-analyzer-push-tokens-for-symbol (str beg end)
872 "Push lexical tokens for the symbol or keyword STR.
873 STR occurs in the current buffer between BEG and END."
874 (let (sym val count)
875 (cond
877 ;; It is a macro. Prepare for a replacement.
878 ((and semantic-lex-spp-replacements-enabled
879 (semantic-lex-spp-symbol-p str))
880 (setq sym (semantic-lex-spp-symbol str)
881 val (symbol-value sym)
882 count 0)
884 (let ((semantic-lex-spp-expanded-macro-stack
885 semantic-lex-spp-expanded-macro-stack))
887 (semantic-lex-with-macro-used str
888 ;; Do direct replacements of single value macros of macros.
889 ;; This solves issues with a macro containing one symbol that
890 ;; is another macro, and get arg lists passed around.
891 (while (and val (consp val)
892 (semantic-lex-token-p (car val))
893 (eq (length val) 1)
894 (eq (semantic-lex-token-class (car val)) 'symbol)
895 (semantic-lex-spp-symbol-p (semantic-lex-token-text (car val)))
896 (< count 10)
898 (setq str (semantic-lex-token-text (car val)))
899 (setq sym (semantic-lex-spp-symbol str)
900 val (symbol-value sym))
901 ;; Prevent recursion
902 (setq count (1+ count))
903 ;; This prevents a different kind of recursion.
904 (push str semantic-lex-spp-expanded-macro-stack)
907 (semantic-lex-spp-anlyzer-do-replace sym val beg end))
910 ;; Anything else.
912 ;; A regular keyword.
913 (semantic-lex-push-token
914 (semantic-lex-token (or (semantic-lex-keyword-p str) 'symbol)
915 beg end))))
918 (define-lex-regex-analyzer semantic-lex-spp-replace-or-symbol-or-keyword
919 "Like 'semantic-lex-symbol-or-keyword' plus preprocessor macro replacement."
920 "\\(\\sw\\|\\s_\\)+"
921 (let ((str (match-string 0))
922 (beg (match-beginning 0))
923 (end (match-end 0))
924 sppov)
925 (semantic-lex-spp-analyzer-push-tokens-for-symbol str beg end)
926 (when (setq sppov (semantic-lex-spp-get-overlay beg))
927 (setq semantic-lex-end-point (cdr (overlay-get sppov 'semantic-spp))))))
929 (define-lex-regex-analyzer semantic-lex-spp-paren-or-list
930 "Detect open parenthesis.
931 Contrary to `semantic-lex-paren-or-list', this will push a single
932 open-paren onto the stream if no closing paren can be found.
933 This is important for macros which open a scope which is closed
934 by another macro."
935 "\\s("
936 (if (or (not semantic-lex-maximum-depth)
937 (< semantic-lex-current-depth semantic-lex-maximum-depth))
938 (progn
939 (setq semantic-lex-current-depth (1+ semantic-lex-current-depth))
940 (semantic-lex-push-token
941 (semantic-lex-token
942 'open-paren (match-beginning 0) (match-end 0))))
943 (save-excursion
944 (let ((start (match-beginning 0))
945 (end (match-end 0))
946 (peom (save-excursion (semantic-c-end-of-macro) (point))))
947 (condition-case nil
948 (progn
949 ;; This will throw an error if no closing paren can be found.
950 (forward-list 1)
951 (when (> (point) peom)
952 ;; If we have left the macro, this is the wrong closing
953 ;; paren, so error out as well.
954 (error ""))
955 (semantic-lex-push-token
956 (semantic-lex-token
957 'semantic-list start (point))))
958 (error
959 ;; Only push a single open-paren.
960 (semantic-lex-push-token
961 (semantic-lex-token
962 'open-paren start end))))))))
964 ;;; ANALYZERS FOR NEW MACROS
966 ;; These utilities and analyzer declaration function are for
967 ;; creating an analyzer which produces new macros in the macro table.
969 ;; There are two analyzers. One for new macros, and one for removing
970 ;; a macro.
972 (defun semantic-lex-spp-first-token-arg-list (token)
973 "If TOKEN is a semantic-list, turn it into an SPP ARG LIST."
974 (when (and (consp token)
975 (symbolp (car token))
976 (eq 'semantic-list (car token)))
977 ;; Convert TOKEN in place.
978 (let ((argsplit (split-string (semantic-lex-token-text token)
979 "[(), ]" t)))
980 (setcar token 'spp-arg-list)
981 (setcar (nthcdr 1 token) argsplit))
984 (defun semantic-lex-spp-one-token-and-move-for-macro (max)
985 "Lex up one token, and move to end of that token.
986 Don't go past MAX."
987 (let ((ans (semantic-lex (point) max 0 0)))
988 (if (not ans)
989 (progn (goto-char max)
990 nil)
991 (when (> (semantic-lex-token-end (car ans)) max)
992 (let ((bounds (semantic-lex-token-bounds (car ans))))
993 (setcdr bounds max)))
994 (goto-char (semantic-lex-token-end (car ans)))
995 (car ans))
998 (defun semantic-lex-spp-stream-for-arglist (token)
999 "Lex up the contents of the arglist TOKEN.
1000 Parsing starts inside the parens, and ends at the end of TOKEN."
1001 (let ((end (semantic-lex-token-end token))
1002 (fresh-toks nil)
1003 (toks nil))
1004 (save-excursion
1006 (if (stringp (nth 1 token))
1007 ;; If the 2nd part of the token is a string, then we have
1008 ;; a token specifically extracted from a buffer. Possibly
1009 ;; a different buffer. This means we need to do something
1010 ;; nice to parse its contents.
1011 (let ((txt (semantic-lex-token-text token)))
1012 (semantic-lex-spp-lex-text-string
1013 (substring txt 1 (1- (length txt)))))
1015 ;; This part is like the original
1016 (goto-char (semantic-lex-token-start token))
1017 ;; A cheat for going into the semantic list.
1018 (forward-char 1)
1019 (setq fresh-toks (semantic-lex-spp-stream-for-macro (1- end)))
1020 (dolist (tok fresh-toks)
1021 ;; march 2011: This is too restrictive! For example "void"
1022 ;; can't get through. What elements was I trying to expunge
1023 ;; to put this in here in the first place? If I comment it
1024 ;; out, does anything new break?
1025 ;(when (memq (semantic-lex-token-class tok) '(symbol semantic-list))
1026 ;; It appears the commas need to be dumped. perhaps this is better,
1027 ;; but will it cause more problems later?
1028 (unless (eq (semantic-lex-token-class tok) 'punctuation)
1029 (setq toks (cons tok toks))))
1031 (nreverse toks)))))
1033 (defvar semantic-lex-spp-hack-depth 0
1034 "Current depth of recursive calls to `semantic-lex-spp-lex-text-string'.")
1036 (defun semantic-lex-spp-lex-text-string (text)
1037 "Lex the text string TEXT using the current buffer's state.
1038 Use this to parse text extracted from a macro as if it came from
1039 the current buffer. Since the lexer is designed to only work in
1040 a buffer, we need to create a new buffer, and populate it with rules
1041 and variable state from the current buffer."
1042 (let* ((semantic-lex-spp-hack-depth (1+ semantic-lex-spp-hack-depth))
1043 (buf (get-buffer-create (format " *SPP parse hack %d*"
1044 semantic-lex-spp-hack-depth)))
1045 (mode major-mode)
1046 (fresh-toks nil)
1047 (toks nil)
1048 (origbuff (current-buffer))
1049 (analyzer semantic-lex-analyzer)
1050 (important-vars '(semantic-lex-spp-macro-symbol-obarray
1051 semantic-lex-spp-project-macro-symbol-obarray
1052 semantic-lex-spp-dynamic-macro-symbol-obarray
1053 semantic-lex-spp-dynamic-macro-symbol-obarray-stack
1054 semantic-lex-spp-expanded-macro-stack
1057 (if (> semantic-lex-spp-hack-depth 5)
1059 (with-current-buffer buf
1060 (erase-buffer)
1061 ;; Below is a painful hack to make sure everything is setup correctly.
1062 (when (not (eq major-mode mode))
1063 (save-match-data
1065 ;; Protect against user-hooks that throw errors.
1066 (condition-case nil
1067 (funcall mode)
1068 (error nil))
1070 ;; Hack in mode-local
1071 (activate-mode-local-bindings)
1073 ;; Call the major mode's setup function
1074 (let ((entry (assq major-mode semantic-new-buffer-setup-functions)))
1075 (when entry
1076 (funcall (cdr entry))))
1078 ;; CHEATER! The following 3 lines are from
1079 ;; `semantic-new-buffer-fcn', but we don't want to turn
1080 ;; on all the other annoying modes for this little task.
1081 (setq semantic-new-buffer-fcn-was-run t)
1082 (semantic-lex-init)
1083 (semantic-clear-toplevel-cache)
1084 (remove-hook 'semantic-lex-reset-functions
1085 'semantic-lex-spp-reset-hook t)
1088 ;; Second Cheat: copy key variables regarding macro state from the
1089 ;; the originating buffer we are parsing. We need to do this every time
1090 ;; since the state changes.
1091 (dolist (V important-vars)
1092 (set V (semantic-buffer-local-value V origbuff)))
1093 (insert text)
1094 (goto-char (point-min))
1096 (setq fresh-toks (semantic-lex-spp-stream-for-macro (point-max))))
1098 (dolist (tok fresh-toks)
1099 (when (memq (semantic-lex-token-class tok) '(symbol semantic-list))
1100 (setq toks (cons tok toks)))))
1102 (nreverse toks)))
1104 ;;;; FIRST DRAFT
1105 ;; This is the fist version of semantic-lex-spp-stream-for-arglist
1106 ;; that worked pretty well. It doesn't work if the TOKEN was derived
1107 ;; from some other buffer, in which case it can get the wrong answer
1108 ;; or throw an error if the token location in the originating buffer is
1109 ;; larger than the current buffer.
1110 ;;(defun semantic-lex-spp-stream-for-arglist-orig (token)
1111 ;; "Lex up the contents of the arglist TOKEN.
1112 ;; Parsing starts inside the parens, and ends at the end of TOKEN."
1113 ;; (save-excursion
1114 ;; (let ((end (semantic-lex-token-end token))
1115 ;; (fresh-toks nil)
1116 ;; (toks nil))
1117 ;; (goto-char (semantic-lex-token-start token))
1118 ;; ;; A cheat for going into the semantic list.
1119 ;; (forward-char 1)
1120 ;; (setq fresh-toks (semantic-lex-spp-stream-for-macro (1- end)))
1121 ;; (dolist (tok fresh-toks)
1122 ;; (when (memq (semantic-lex-token-class tok) '(symbol semantic-list))
1123 ;; (setq toks (cons tok toks))))
1124 ;; (nreverse toks))
1125 ;; ))
1127 ;;;; USING SPLIT
1128 ;; This doesn't work, because some arguments passed into a macro
1129 ;; might contain non-simple symbol words, which this doesn't handle.
1131 ;; Thus, you need a full lex to occur.
1132 ;; (defun semantic-lex-spp-stream-for-arglist-split (token)
1133 ;; "Lex up the contents of the arglist TOKEN.
1134 ;; Parsing starts inside the parens, and ends at the end of TOKEN."
1135 ;; (let* ((txt (semantic-lex-token-text token))
1136 ;; (split (split-string (substring txt 1 (1- (length txt)))
1137 ;; "(), " t))
1138 ;; ;; Hack for lexing.
1139 ;; (semantic-lex-spp-analyzer-push-tokens-for-symbol nil))
1140 ;; (dolist (S split)
1141 ;; (semantic-lex-spp-analyzer-push-tokens-for-symbol S 0 1))
1142 ;; (reverse semantic-lex-spp-analyzer-push-tokens-for-symbol)))
1145 (defun semantic-lex-spp-stream-for-macro (eos)
1146 "Lex up a stream of tokens for a #define statement.
1147 Parsing starts at the current point location.
1148 EOS is the end of the stream to lex for this macro."
1149 (let ((stream nil))
1150 (while (< (point) eos)
1151 (let* ((tok (semantic-lex-spp-one-token-and-move-for-macro eos))
1152 (str (when tok
1153 (semantic-lex-token-text tok)))
1155 (if str
1156 (push (semantic-lex-token (semantic-lex-token-class tok)
1157 (semantic-lex-token-start tok)
1158 (semantic-lex-token-end tok)
1159 str)
1160 stream)
1161 ;; Nothing to push.
1162 nil)))
1163 (goto-char eos)
1164 ;; Fix the order
1165 (nreverse stream)
1168 (defmacro define-lex-spp-macro-declaration-analyzer (name doc regexp tokidx
1169 &rest valform)
1170 "Define a lexical analyzer for defining new MACROS.
1171 NAME is the name of the analyzer.
1172 DOC is the documentation for the analyzer.
1173 REGEXP is a regular expression for the analyzer to match.
1174 See `define-lex-regex-analyzer' for more on regexp.
1175 TOKIDX is an index into REGEXP for which a new lexical token
1176 of type `spp-macro-def' is to be created.
1177 VALFORM are forms that return the value to be saved for this macro, or nil.
1178 When implementing a macro, you can use `semantic-lex-spp-stream-for-macro'
1179 to convert text into a lexical stream for storage in the macro."
1180 (let ((start (make-symbol "start"))
1181 (end (make-symbol "end"))
1182 (val (make-symbol "val"))
1183 (startpnt (make-symbol "startpnt"))
1184 (endpnt (make-symbol "endpnt")))
1185 `(define-lex-regex-analyzer ,name
1186 ,doc
1187 ,regexp
1188 (let ((,start (match-beginning ,tokidx))
1189 (,end (match-end ,tokidx))
1190 (,startpnt semantic-lex-end-point)
1191 (,val (save-match-data ,@valform))
1192 (,endpnt semantic-lex-end-point))
1193 (semantic-lex-spp-symbol-set
1194 (buffer-substring-no-properties ,start ,end)
1195 ,val)
1196 (semantic-lex-push-token
1197 (semantic-lex-token 'spp-macro-def
1198 ,start ,end))
1199 ;; Preserve setting of the end point from the calling macro.
1200 (when (and (/= ,startpnt ,endpnt)
1201 (/= ,endpnt semantic-lex-end-point))
1202 (setq semantic-lex-end-point ,endpnt))
1203 ))))
1205 (defmacro define-lex-spp-macro-undeclaration-analyzer (name doc regexp tokidx)
1206 "Undefine a lexical analyzer for defining new MACROS.
1207 NAME is the name of the analyzer.
1208 DOC is the documentation for the analyzer.
1209 REGEXP is a regular expression for the analyzer to match.
1210 See `define-lex-regex-analyzer' for more on regexp.
1211 TOKIDX is an index into REGEXP for which a new lexical token
1212 of type `spp-macro-undef' is to be created."
1213 (let ((start (make-symbol "start"))
1214 (end (make-symbol "end")))
1215 `(define-lex-regex-analyzer ,name
1216 ,doc
1217 ,regexp
1218 (let ((,start (match-beginning ,tokidx))
1219 (,end (match-end ,tokidx))
1221 (semantic-lex-spp-symbol-remove
1222 (buffer-substring-no-properties ,start ,end))
1223 (semantic-lex-push-token
1224 (semantic-lex-token 'spp-macro-undef
1225 ,start ,end))
1226 ))))
1228 ;;; INCLUDES
1230 ;; These analyzers help a language define how include files
1231 ;; are identified. These are ONLY for languages that perform
1232 ;; an actual textual inclusion, and not for imports.
1234 ;; This section is supposed to allow the macros from the headers to be
1235 ;; added to the local dynamic macro table, but that hasn't been
1236 ;; written yet.
1238 (defcustom semantic-lex-spp-use-headers-flag nil
1239 "*Non-nil means to pre-parse headers as we go.
1240 For languages that use the Semantic pre-processor, this can
1241 improve the accuracy of parsed files where include files
1242 can change the state of what's parsed in the current file.
1244 Note: Note implemented yet"
1245 :group 'semantic
1246 :type 'boolean)
1248 (defun semantic-lex-spp-merge-header (name)
1249 "Extract and merge any macros from the header with NAME.
1250 Finds the header file belonging to NAME, gets the macros
1251 from that file, and then merge the macros with our current
1252 symbol table."
1253 (when semantic-lex-spp-use-headers-flag
1254 ;; @todo - do this someday, ok?
1257 (defmacro define-lex-spp-include-analyzer (name doc regexp tokidx
1258 &rest valform)
1259 "Define a lexical analyzer for defining a new INCLUDE lexical token.
1260 Macros defined in the found include will be added to our running table
1261 at the time the include statement is found.
1262 NAME is the name of the analyzer.
1263 DOC is the documentation for the analyzer.
1264 REGEXP is a regular expression for the analyzer to match.
1265 See `define-lex-regex-analyzer' for more on regexp.
1266 TOKIDX is an index into REGEXP for which a new lexical token
1267 of type `spp-macro-include' is to be created.
1268 VALFORM are forms that return the name of the thing being included, and the
1269 type of include. The return value should be of the form:
1270 (NAME . TYPE)
1271 where NAME is the name of the include, and TYPE is the type of the include,
1272 where a valid symbol is 'system, or nil."
1273 (let ((start (make-symbol "start"))
1274 (end (make-symbol "end"))
1275 (val (make-symbol "val"))
1276 (startpnt (make-symbol "startpnt"))
1277 (endpnt (make-symbol "endpnt")))
1278 `(define-lex-regex-analyzer ,name
1279 ,doc
1280 ,regexp
1281 (let ((,start (match-beginning ,tokidx))
1282 (,end (match-end ,tokidx))
1283 (,startpnt semantic-lex-end-point)
1284 (,val (save-match-data ,@valform))
1285 (,endpnt semantic-lex-end-point))
1286 ;;(message "(car ,val) -> %S" (car ,val))
1287 (semantic-lex-spp-merge-header (car ,val))
1288 (semantic-lex-push-token
1289 (semantic-lex-token (if (eq (cdr ,val) 'system)
1290 'spp-system-include
1291 'spp-include)
1292 ,start ,end
1293 (car ,val)))
1294 ;; Preserve setting of the end point from the calling macro.
1295 (when (and (/= ,startpnt ,endpnt)
1296 (/= ,endpnt semantic-lex-end-point))
1297 (setq semantic-lex-end-point ,endpnt))
1298 ))))
1300 ;;; EIEIO USAGE
1302 ;; Semanticdb can save off macro tables for quick lookup later.
1304 ;; These routines are for saving macro lists into an EIEIO persistent
1305 ;; file.
1306 (defvar semantic-lex-spp-macro-max-length-to-save 200
1307 "*Maximum length of an SPP macro before we opt to not save it.")
1309 ;;;###autoload
1310 (defun semantic-lex-spp-table-write-slot-value (value)
1311 "Write out the VALUE of a slot for EIEIO.
1312 The VALUE is a spp lexical table."
1313 (if (not value)
1314 (princ "nil")
1315 (princ "\n '(")
1316 ;(princ value)
1317 (dolist (sym value)
1318 (princ "(")
1319 (prin1 (car sym))
1320 (let* ((first (car (cdr sym)))
1321 (rest (cdr sym)))
1322 (if (not (listp first))
1323 (insert "nil ;; bogus macro found.\n")
1324 (when (eq (car first) 'spp-arg-list)
1325 (princ " ")
1326 (prin1 first)
1327 (setq rest (cdr rest)))
1329 (when rest
1330 (princ " . ")
1331 (let ((len (length (cdr rest))))
1332 (cond ((< len 2)
1333 (condition-case nil
1334 (prin1 rest)
1335 (error
1336 (princ "nil ;; Error writing macro\n"))))
1337 ((< len semantic-lex-spp-macro-max-length-to-save)
1338 (princ "\n ")
1339 (condition-case nil
1340 (prin1 rest)
1341 (error
1342 (princ "nil ;; Error writing macro\n "))))
1343 (t ;; Too Long!
1344 (princ "nil ;; Too Long!\n ")))))))
1345 (princ ")\n "))
1346 (princ ")\n")))
1348 ;;; MACRO TABLE DEBUG
1350 (defun semantic-lex-spp-describe (&optional buffer)
1351 "Describe the current list of spp macros for BUFFER.
1352 If BUFFER is not provided, use the current buffer."
1353 (interactive)
1354 (let ((syms (save-excursion
1355 (if buffer (set-buffer buffer))
1356 (semantic-lex-spp-macros)))
1357 (sym nil))
1358 (with-output-to-temp-buffer "*SPP MACROS*"
1359 (princ "Macro\t\tValue\n")
1360 (while syms
1361 (setq sym (car syms)
1362 syms (cdr syms))
1363 (princ (symbol-name sym))
1364 (princ "\t")
1365 (if (< (length (symbol-name sym)) 8)
1366 (princ "\t"))
1367 (prin1 (symbol-value sym))
1368 (princ "\n")
1369 ))))
1371 ;;; EDEBUG Handlers
1373 (add-hook
1374 'edebug-setup-hook
1375 #'(lambda ()
1377 (def-edebug-spec define-lex-spp-macro-declaration-analyzer
1378 (&define name stringp stringp form def-body)
1381 (def-edebug-spec define-lex-spp-macro-undeclaration-analyzer
1382 (&define name stringp stringp form)
1385 (def-edebug-spec define-lex-spp-include-analyzer
1386 (&define name stringp stringp form def-body))))
1388 (provide 'semantic/lex-spp)
1390 ;; Local variables:
1391 ;; generated-autoload-file: "loaddefs.el"
1392 ;; generated-autoload-load-name: "semantic/lex-spp"
1393 ;; End:
1395 ;;; semantic/lex-spp.el ends here