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