]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/lex-spp.el
761cc1af5edb9d2a9549a84fe9825592d2d102f1
[gnu-emacs] / lisp / cedet / semantic / lex-spp.el
1 ;;; semantic/lex-spp.el --- Semantic Lexical Pre-processor
2
3 ;; Copyright (C) 2006-2015 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 (declare-function semantic-c-end-of-macro "semantic/bovine/c")
74
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)
81
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)
87
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)
94
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)
98
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.
103
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.
107
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)
119
120 (add-hook
121 'edebug-setup-hook
122 #'(lambda ()
123
124 (def-edebug-spec semantic-lex-with-macro-used
125 (symbolp def-body)
126 )
127
128 ))
129
130 ;;; MACRO TABLE UTILS
131 ;;
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.
135 ;;
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.
148 (or
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 ;; ...
159 )))
160
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)
164 t))
165
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))))
171
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))))
177
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)))))
185
186 (defvar semantic-lex-spp-debug-symbol nil
187 "A symbol to break on if it is being set somewhere.")
188
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)))
196
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))
205 ; )
206 nil
207 )
208
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))
218
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))))
225
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)))
235 )
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)))
241 )
242 ;; Set our new value here.
243 (set mapsym value)
244 ))
245
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)
254 )
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)))
262 )))
263
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))))
269
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:
273
274 NAME is the name of the spp macro symbol to define.
275 REPLACEMENT a string that would be substituted in for NAME."
276
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))
289
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))
302
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))
323
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))))
330
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)
341 ))
342
343 ;;; MACRO EXPANSION: Simple cases
344 ;;
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.
348
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))
355 (t
356 nil))))
357 (when regex
358 (string-match regex value))
359 ))
360
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.
366
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.
375 ;;
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)))
383
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)
399 ))
400
401 ;;; MACRO EXPANSION : Lexical token replacement
402 ;;
403 ;; When substituting in a macro from a token stream of formatted
404 ;; semantic lex tokens, things can be much more complicated.
405 ;;
406 ;; Some macros have arguments that get set into the dynamic macro
407 ;; table during replacement.
408 ;;
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.
412 ;;
413 ;; Argument lists are saved as a lexical token at the beginning
414 ;; of a replacement value.
415
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:
422
423 #define (a,b) a ## b;
424
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)
429 )
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)))
438 )
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))
452 val
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)))
459 ))
460 ((stringp txt)
461 txt)
462 (t nil))
463 ))
464
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)))))
470
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."
479
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.
484 ;;
485 ;; CASE 1: Simple token stream
486 ;;
487 ;; #define SUPER mysuper::
488 ;; ==>
489 ;;((symbol "mysuper" 480 . 487)
490 ;; (punctuation ":" 487 . 488)
491 ;; (punctuation ":" 488 . 489))
492 ;;
493 ;; CASE 2: Token stream with argument list
494 ;;
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))
501 ;;
502 ;; In the second case, a macro with an argument list as the args as the
503 ;; first entry.
504 ;;
505 ;; CASE 3: Symbol text merge
506 ;;
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))
512 ;;
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".
520 ;;
521 ;; CASE 4: Nested token streams
522 ;;
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))
529 ;;
530 ;; Nested token FOO shows up in the table of macros, and gets replace
531 ;; inline. This is the same as case 2.
532 ;;
533 ;; CASE 5: Macros which open a scope without closing it
534 ;;
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))
541 ;;
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).
552
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))))
559
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)))
574
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)))
580
581 ;; Push args into the replacement list.
582 (let ((AV argvalues))
583 (dolist (A arglist)
584 (let* ((argval (car AV)))
585
586 (semantic-lex-spp-symbol-push A argval)
587 (setq argalist (cons (cons A argval) argalist))
588 (setq AV (cdr AV)))))
589 )
590
591 ;; Set val-tmp after stripping arguments.
592 (setq val-tmp val)
593
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))
600
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)))
611 )
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)))
615 )
616
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))
623 ))
624
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))
631 )
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))
638 )
639
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))
646 ))
647
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))
658 )
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.
686 (t
687 ;; Nothing new.
688 (semantic-lex-push-token
689 (semantic-lex-token (semantic-lex-token-class v) beg end txt))
690 )
691 )))
692
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))
697 ))
698
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)))
713 (t
714 (message "Invalid merge macro encountered; \
715 will return empty string instead.")
716 "")))
717 txt
718 ""))
719
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))))))
742
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)))
750
751 ;;; Macro Merging
752 ;;
753 ;; Used when token streams from different macros include each other.
754 ;; Merged macro streams perform in place replacements.
755
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)
777 ))
778 (t
779 (push (car raw-stream) cooked-stream))
780 )
781 (setq raw-stream (cdr raw-stream))
782 )
783
784 (nreverse cooked-stream))
785 )
786
787 ;;; MACRO EXPANSION
788 ;;
789 ;; There are two types of expansion.
790 ;;
791 ;; 1. Expansion using a value made up of lexical tokens.
792 ;; 2. User input replacement from a plain string.
793
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))
814 ))
815
816 ;;; --------------------------------------------------------
817 ;;;
818 ;;; ANALYZERS:
819 ;;;
820
821 ;;; Symbol Is Macro
822 ;;
823 ;; An analyzer that will push tokens from a macro in place
824 ;; of the macro symbol.
825 ;;
826 (defun semantic-lex-spp-analyzer-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)
834 )
835
836 ;; Check for arguments.
837 (setq arg-in (semantic-lex-spp-macro-with-args val))
838
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.
848 ;;
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))
853
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)
858 ))
859 ))
860
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)
864 )
865 ))
866 (define-obsolete-function-alias
867 'semantic-lex-spp-anlyzer-do-replace
868 'semantic-lex-spp-analyzer-do-replace "25.1")
869
870 (defvar semantic-lex-spp-replacements-enabled t
871 "Non-nil means do replacements when finding keywords.
872 Disable this only to prevent recursive expansion issues.")
873
874 (defun semantic-lex-spp-analyzer-push-tokens-for-symbol (str beg end)
875 "Push lexical tokens for the symbol or keyword STR.
876 STR occurs in the current buffer between BEG and END."
877 (let (sym val count)
878 (cond
879 ;;
880 ;; It is a macro. Prepare for a replacement.
881 ((and semantic-lex-spp-replacements-enabled
882 (semantic-lex-spp-symbol-p str))
883 (setq sym (semantic-lex-spp-symbol str)
884 val (symbol-value sym)
885 count 0)
886
887 (let ((semantic-lex-spp-expanded-macro-stack
888 semantic-lex-spp-expanded-macro-stack))
889
890 (semantic-lex-with-macro-used str
891 ;; Do direct replacements of single value macros of macros.
892 ;; This solves issues with a macro containing one symbol that
893 ;; is another macro, and get arg lists passed around.
894 (while (and val (consp val)
895 (semantic-lex-token-p (car val))
896 (eq (length val) 1)
897 (eq (semantic-lex-token-class (car val)) 'symbol)
898 (semantic-lex-spp-symbol-p (semantic-lex-token-text (car val)))
899 (< count 10)
900 )
901 (setq str (semantic-lex-token-text (car val)))
902 (setq sym (semantic-lex-spp-symbol str)
903 val (symbol-value sym))
904 ;; Prevent recursion
905 (setq count (1+ count))
906 ;; This prevents a different kind of recursion.
907 (push str semantic-lex-spp-expanded-macro-stack)
908 )
909
910 (semantic-lex-spp-anlyzer-do-replace sym val beg end))
911
912 ))
913 ;; Anything else.
914 (t
915 ;; A regular keyword.
916 (semantic-lex-push-token
917 (semantic-lex-token (or (semantic-lex-keyword-p str) 'symbol)
918 beg end))))
919 ))
920
921 (define-lex-regex-analyzer semantic-lex-spp-replace-or-symbol-or-keyword
922 "Like `semantic-lex-symbol-or-keyword' plus preprocessor macro replacement."
923 "\\(\\sw\\|\\s_\\)+"
924 (let ((str (match-string 0))
925 (beg (match-beginning 0))
926 (end (match-end 0))
927 sppov)
928 (semantic-lex-spp-analyzer-push-tokens-for-symbol str beg end)
929 (when (setq sppov (semantic-lex-spp-get-overlay beg))
930 (setq semantic-lex-end-point (cdr (overlay-get sppov 'semantic-spp))))))
931
932 (define-lex-regex-analyzer semantic-lex-spp-paren-or-list
933 "Detect open parenthesis.
934 Contrary to `semantic-lex-paren-or-list', this will push a single
935 open-paren onto the stream if no closing paren can be found.
936 This is important for macros which open a scope which is closed
937 by another macro."
938 "\\s("
939 (if (or (not semantic-lex-maximum-depth)
940 (< semantic-lex-current-depth semantic-lex-maximum-depth))
941 (progn
942 (setq semantic-lex-current-depth (1+ semantic-lex-current-depth))
943 (semantic-lex-push-token
944 (semantic-lex-token
945 'open-paren (match-beginning 0) (match-end 0))))
946 (save-excursion
947 (let ((start (match-beginning 0))
948 (end (match-end 0))
949 (peom (save-excursion (semantic-c-end-of-macro) (point))))
950 (condition-case nil
951 (progn
952 ;; This will throw an error if no closing paren can be found.
953 (forward-list 1)
954 (when (> (point) peom)
955 ;; If we have left the macro, this is the wrong closing
956 ;; paren, so error out as well.
957 (error ""))
958 (semantic-lex-push-token
959 (semantic-lex-token
960 'semantic-list start (point))))
961 (error
962 ;; Only push a single open-paren.
963 (semantic-lex-push-token
964 (semantic-lex-token
965 'open-paren start end))))))))
966
967 ;;; ANALYZERS FOR NEW MACROS
968 ;;
969 ;; These utilities and analyzer declaration function are for
970 ;; creating an analyzer which produces new macros in the macro table.
971 ;;
972 ;; There are two analyzers. One for new macros, and one for removing
973 ;; a macro.
974
975 (defun semantic-lex-spp-first-token-arg-list (token)
976 "If TOKEN is a semantic-list, turn it into an SPP ARG LIST."
977 (when (and (consp token)
978 (symbolp (car token))
979 (eq 'semantic-list (car token)))
980 ;; Convert TOKEN in place.
981 (let ((argsplit (split-string (semantic-lex-token-text token)
982 "[(), ]" t)))
983 (setcar token 'spp-arg-list)
984 (setcar (nthcdr 1 token) argsplit))
985 ))
986
987 (defun semantic-lex-spp-one-token-and-move-for-macro (max)
988 "Lex up one token, and move to end of that token.
989 Don't go past MAX."
990 (let ((ans (semantic-lex (point) max 0 0)))
991 (if (not ans)
992 (progn (goto-char max)
993 nil)
994 (when (> (semantic-lex-token-end (car ans)) max)
995 (let ((bounds (semantic-lex-token-bounds (car ans))))
996 (setcdr bounds max)))
997 (goto-char (semantic-lex-token-end (car ans)))
998 (car ans))
999 ))
1000
1001 (defun semantic-lex-spp-stream-for-arglist (token)
1002 "Lex up the contents of the arglist TOKEN.
1003 Parsing starts inside the parens, and ends at the end of TOKEN."
1004 (let ((end (semantic-lex-token-end token))
1005 (fresh-toks nil)
1006 (toks nil))
1007 (save-excursion
1008
1009 (if (stringp (nth 1 token))
1010 ;; If the 2nd part of the token is a string, then we have
1011 ;; a token specifically extracted from a buffer. Possibly
1012 ;; a different buffer. This means we need to do something
1013 ;; nice to parse its contents.
1014 (let ((txt (semantic-lex-token-text token)))
1015 (semantic-lex-spp-lex-text-string
1016 (substring txt 1 (1- (length txt)))))
1017
1018 ;; This part is like the original
1019 (goto-char (semantic-lex-token-start token))
1020 ;; A cheat for going into the semantic list.
1021 (forward-char 1)
1022 (setq fresh-toks (semantic-lex-spp-stream-for-macro (1- end)))
1023 (dolist (tok fresh-toks)
1024 ;; march 2011: This is too restrictive! For example "void"
1025 ;; can't get through. What elements was I trying to expunge
1026 ;; to put this in here in the first place? If I comment it
1027 ;; out, does anything new break?
1028 ;(when (memq (semantic-lex-token-class tok) '(symbol semantic-list))
1029 ;; It appears the commas need to be dumped. perhaps this is better,
1030 ;; but will it cause more problems later?
1031 (unless (eq (semantic-lex-token-class tok) 'punctuation)
1032 (setq toks (cons tok toks))))
1033
1034 (nreverse toks)))))
1035
1036 (defvar semantic-lex-spp-hack-depth 0
1037 "Current depth of recursive calls to `semantic-lex-spp-lex-text-string'.")
1038
1039 (defun semantic-lex-spp-lex-text-string (text)
1040 "Lex the text string TEXT using the current buffer's state.
1041 Use this to parse text extracted from a macro as if it came from
1042 the current buffer. Since the lexer is designed to only work in
1043 a buffer, we need to create a new buffer, and populate it with rules
1044 and variable state from the current buffer."
1045 (let* ((semantic-lex-spp-hack-depth (1+ semantic-lex-spp-hack-depth))
1046 (buf (get-buffer-create (format " *SPP parse hack %d*"
1047 semantic-lex-spp-hack-depth)))
1048 (mode major-mode)
1049 (fresh-toks nil)
1050 (toks nil)
1051 (origbuff (current-buffer))
1052 (analyzer semantic-lex-analyzer)
1053 (important-vars '(semantic-lex-spp-macro-symbol-obarray
1054 semantic-lex-spp-project-macro-symbol-obarray
1055 semantic-lex-spp-dynamic-macro-symbol-obarray
1056 semantic-lex-spp-dynamic-macro-symbol-obarray-stack
1057 semantic-lex-spp-expanded-macro-stack
1058 ))
1059 )
1060 (if (> semantic-lex-spp-hack-depth 5)
1061 nil
1062 (with-current-buffer buf
1063 (erase-buffer)
1064 ;; Below is a painful hack to make sure everything is setup correctly.
1065 (when (not (eq major-mode mode))
1066 (save-match-data
1067
1068 ;; Protect against user-hooks that throw errors.
1069 (condition-case nil
1070 (funcall mode)
1071 (error nil))
1072
1073 ;; Hack in mode-local
1074 (activate-mode-local-bindings)
1075
1076 ;; Call the major mode's setup function
1077 (let ((entry (assq major-mode semantic-new-buffer-setup-functions)))
1078 (when entry
1079 (funcall (cdr entry))))
1080
1081 ;; CHEATER! The following 3 lines are from
1082 ;; `semantic-new-buffer-fcn', but we don't want to turn
1083 ;; on all the other annoying modes for this little task.
1084 (setq semantic-new-buffer-fcn-was-run t)
1085 (semantic-lex-init)
1086 (semantic-clear-toplevel-cache)
1087 (remove-hook 'semantic-lex-reset-functions
1088 'semantic-lex-spp-reset-hook t)
1089 ))
1090
1091 ;; Second Cheat: copy key variables regarding macro state from the
1092 ;; the originating buffer we are parsing. We need to do this every time
1093 ;; since the state changes.
1094 (dolist (V important-vars)
1095 (set V (semantic-buffer-local-value V origbuff)))
1096 (insert text)
1097 (goto-char (point-min))
1098
1099 (setq fresh-toks (semantic-lex-spp-stream-for-macro (point-max))))
1100
1101 (dolist (tok fresh-toks)
1102 (when (memq (semantic-lex-token-class tok) '(symbol semantic-list))
1103 (setq toks (cons tok toks)))))
1104
1105 (nreverse toks)))
1106
1107 ;;;; FIRST DRAFT
1108 ;; This is the fist version of semantic-lex-spp-stream-for-arglist
1109 ;; that worked pretty well. It doesn't work if the TOKEN was derived
1110 ;; from some other buffer, in which case it can get the wrong answer
1111 ;; or throw an error if the token location in the originating buffer is
1112 ;; larger than the current buffer.
1113 ;;(defun semantic-lex-spp-stream-for-arglist-orig (token)
1114 ;; "Lex up the contents of the arglist TOKEN.
1115 ;; Parsing starts inside the parens, and ends at the end of TOKEN."
1116 ;; (save-excursion
1117 ;; (let ((end (semantic-lex-token-end token))
1118 ;; (fresh-toks nil)
1119 ;; (toks nil))
1120 ;; (goto-char (semantic-lex-token-start token))
1121 ;; ;; A cheat for going into the semantic list.
1122 ;; (forward-char 1)
1123 ;; (setq fresh-toks (semantic-lex-spp-stream-for-macro (1- end)))
1124 ;; (dolist (tok fresh-toks)
1125 ;; (when (memq (semantic-lex-token-class tok) '(symbol semantic-list))
1126 ;; (setq toks (cons tok toks))))
1127 ;; (nreverse toks))
1128 ;; ))
1129
1130 ;;;; USING SPLIT
1131 ;; This doesn't work, because some arguments passed into a macro
1132 ;; might contain non-simple symbol words, which this doesn't handle.
1133 ;;
1134 ;; Thus, you need a full lex to occur.
1135 ;; (defun semantic-lex-spp-stream-for-arglist-split (token)
1136 ;; "Lex up the contents of the arglist TOKEN.
1137 ;; Parsing starts inside the parens, and ends at the end of TOKEN."
1138 ;; (let* ((txt (semantic-lex-token-text token))
1139 ;; (split (split-string (substring txt 1 (1- (length txt)))
1140 ;; "(), " t))
1141 ;; ;; Hack for lexing.
1142 ;; (semantic-lex-spp-analyzer-push-tokens-for-symbol nil))
1143 ;; (dolist (S split)
1144 ;; (semantic-lex-spp-analyzer-push-tokens-for-symbol S 0 1))
1145 ;; (reverse semantic-lex-spp-analyzer-push-tokens-for-symbol)))
1146
1147
1148 (defun semantic-lex-spp-stream-for-macro (eos)
1149 "Lex up a stream of tokens for a #define statement.
1150 Parsing starts at the current point location.
1151 EOS is the end of the stream to lex for this macro."
1152 (let ((stream nil))
1153 (while (< (point) eos)
1154 (let* ((tok (semantic-lex-spp-one-token-and-move-for-macro eos))
1155 (str (when tok
1156 (semantic-lex-token-text tok)))
1157 )
1158 (if str
1159 (push (semantic-lex-token (semantic-lex-token-class tok)
1160 (semantic-lex-token-start tok)
1161 (semantic-lex-token-end tok)
1162 str)
1163 stream)
1164 ;; Nothing to push.
1165 nil)))
1166 (goto-char eos)
1167 ;; Fix the order
1168 (nreverse stream)
1169 ))
1170
1171 (defmacro define-lex-spp-macro-declaration-analyzer (name doc regexp tokidx
1172 &rest valform)
1173 "Define a lexical analyzer for defining new MACROS.
1174 NAME is the name of the analyzer.
1175 DOC is the documentation for the analyzer.
1176 REGEXP is a regular expression for the analyzer to match.
1177 See `define-lex-regex-analyzer' for more on regexp.
1178 TOKIDX is an index into REGEXP for which a new lexical token
1179 of type `spp-macro-def' is to be created.
1180 VALFORM are forms that return the value to be saved for this macro, or nil.
1181 When implementing a macro, you can use `semantic-lex-spp-stream-for-macro'
1182 to convert text into a lexical stream for storage in the macro."
1183 (let ((start (make-symbol "start"))
1184 (end (make-symbol "end"))
1185 (val (make-symbol "val"))
1186 (startpnt (make-symbol "startpnt"))
1187 (endpnt (make-symbol "endpnt")))
1188 `(define-lex-regex-analyzer ,name
1189 ,doc
1190 ,regexp
1191 (let ((,start (match-beginning ,tokidx))
1192 (,end (match-end ,tokidx))
1193 (,startpnt semantic-lex-end-point)
1194 (,val (save-match-data ,@valform))
1195 (,endpnt semantic-lex-end-point))
1196 (semantic-lex-spp-symbol-set
1197 (buffer-substring-no-properties ,start ,end)
1198 ,val)
1199 (semantic-lex-push-token
1200 (semantic-lex-token 'spp-macro-def
1201 ,start ,end))
1202 ;; Preserve setting of the end point from the calling macro.
1203 (when (and (/= ,startpnt ,endpnt)
1204 (/= ,endpnt semantic-lex-end-point))
1205 (setq semantic-lex-end-point ,endpnt))
1206 ))))
1207
1208 (defmacro define-lex-spp-macro-undeclaration-analyzer (name doc regexp tokidx)
1209 "Undefine a lexical analyzer for defining new MACROS.
1210 NAME is the name of the analyzer.
1211 DOC is the documentation for the analyzer.
1212 REGEXP is a regular expression for the analyzer to match.
1213 See `define-lex-regex-analyzer' for more on regexp.
1214 TOKIDX is an index into REGEXP for which a new lexical token
1215 of type `spp-macro-undef' is to be created."
1216 (let ((start (make-symbol "start"))
1217 (end (make-symbol "end")))
1218 `(define-lex-regex-analyzer ,name
1219 ,doc
1220 ,regexp
1221 (let ((,start (match-beginning ,tokidx))
1222 (,end (match-end ,tokidx))
1223 )
1224 (semantic-lex-spp-symbol-remove
1225 (buffer-substring-no-properties ,start ,end))
1226 (semantic-lex-push-token
1227 (semantic-lex-token 'spp-macro-undef
1228 ,start ,end))
1229 ))))
1230
1231 ;;; INCLUDES
1232 ;;
1233 ;; These analyzers help a language define how include files
1234 ;; are identified. These are ONLY for languages that perform
1235 ;; an actual textual inclusion, and not for imports.
1236 ;;
1237 ;; This section is supposed to allow the macros from the headers to be
1238 ;; added to the local dynamic macro table, but that hasn't been
1239 ;; written yet.
1240 ;;
1241 (defcustom semantic-lex-spp-use-headers-flag nil
1242 "*Non-nil means to pre-parse headers as we go.
1243 For languages that use the Semantic pre-processor, this can
1244 improve the accuracy of parsed files where include files
1245 can change the state of what's parsed in the current file.
1246
1247 Note: Note implemented yet"
1248 :group 'semantic
1249 :type 'boolean)
1250
1251 (defun semantic-lex-spp-merge-header (name)
1252 "Extract and merge any macros from the header with NAME.
1253 Finds the header file belonging to NAME, gets the macros
1254 from that file, and then merge the macros with our current
1255 symbol table."
1256 (when semantic-lex-spp-use-headers-flag
1257 ;; @todo - do this someday, ok?
1258 ))
1259
1260 (defmacro define-lex-spp-include-analyzer (name doc regexp tokidx
1261 &rest valform)
1262 "Define a lexical analyzer for defining a new INCLUDE lexical token.
1263 Macros defined in the found include will be added to our running table
1264 at the time the include statement is found.
1265 NAME is the name of the analyzer.
1266 DOC is the documentation for the analyzer.
1267 REGEXP is a regular expression for the analyzer to match.
1268 See `define-lex-regex-analyzer' for more on regexp.
1269 TOKIDX is an index into REGEXP for which a new lexical token
1270 of type `spp-macro-include' is to be created.
1271 VALFORM are forms that return the name of the thing being included, and the
1272 type of include. The return value should be of the form:
1273 (NAME . TYPE)
1274 where NAME is the name of the include, and TYPE is the type of the include,
1275 where a valid symbol is 'system, or nil."
1276 (let ((start (make-symbol "start"))
1277 (end (make-symbol "end"))
1278 (val (make-symbol "val"))
1279 (startpnt (make-symbol "startpnt"))
1280 (endpnt (make-symbol "endpnt")))
1281 `(define-lex-regex-analyzer ,name
1282 ,doc
1283 ,regexp
1284 (let ((,start (match-beginning ,tokidx))
1285 (,end (match-end ,tokidx))
1286 (,startpnt semantic-lex-end-point)
1287 (,val (save-match-data ,@valform))
1288 (,endpnt semantic-lex-end-point))
1289 ;;(message "(car ,val) -> %S" (car ,val))
1290 (semantic-lex-spp-merge-header (car ,val))
1291 (semantic-lex-push-token
1292 (semantic-lex-token (if (eq (cdr ,val) 'system)
1293 'spp-system-include
1294 'spp-include)
1295 ,start ,end
1296 (car ,val)))
1297 ;; Preserve setting of the end point from the calling macro.
1298 (when (and (/= ,startpnt ,endpnt)
1299 (/= ,endpnt semantic-lex-end-point))
1300 (setq semantic-lex-end-point ,endpnt))
1301 ))))
1302
1303 ;;; EIEIO USAGE
1304 ;;
1305 ;; Semanticdb can save off macro tables for quick lookup later.
1306 ;;
1307 ;; These routines are for saving macro lists into an EIEIO persistent
1308 ;; file.
1309 (defvar semantic-lex-spp-macro-max-length-to-save 200
1310 "*Maximum length of an SPP macro before we opt to not save it.")
1311
1312 ;;;###autoload
1313 (defun semantic-lex-spp-table-write-slot-value (value)
1314 "Write out the VALUE of a slot for EIEIO.
1315 The VALUE is a spp lexical table."
1316 (if (not value)
1317 (princ "nil")
1318 (princ "\n '(")
1319 ;(princ value)
1320 (dolist (sym value)
1321 (princ "(")
1322 (prin1 (car sym))
1323 (let* ((first (car (cdr sym)))
1324 (rest (cdr sym)))
1325 (if (not (listp first))
1326 (insert "nil ;; bogus macro found.\n")
1327 (when (eq (car first) 'spp-arg-list)
1328 (princ " ")
1329 (prin1 first)
1330 (setq rest (cdr rest)))
1331
1332 (when rest
1333 (princ " . ")
1334 (let ((len (length (cdr rest))))
1335 (cond ((< len 2)
1336 (condition-case nil
1337 (prin1 rest)
1338 (error
1339 (princ "nil ;; Error writing macro\n"))))
1340 ((< len semantic-lex-spp-macro-max-length-to-save)
1341 (princ "\n ")
1342 (condition-case nil
1343 (prin1 rest)
1344 (error
1345 (princ "nil ;; Error writing macro\n "))))
1346 (t ;; Too Long!
1347 (princ "nil ;; Too Long!\n ")))))))
1348 (princ ")\n "))
1349 (princ ")\n")))
1350
1351 ;;; MACRO TABLE DEBUG
1352 ;;
1353 (defun semantic-lex-spp-describe (&optional buffer)
1354 "Describe the current list of spp macros for BUFFER.
1355 If BUFFER is not provided, use the current buffer."
1356 (interactive)
1357 (let ((syms (save-excursion
1358 (if buffer (set-buffer buffer))
1359 (semantic-lex-spp-macros)))
1360 (sym nil))
1361 (with-output-to-temp-buffer "*SPP MACROS*"
1362 (princ "Macro\t\tValue\n")
1363 (while syms
1364 (setq sym (car syms)
1365 syms (cdr syms))
1366 (princ (symbol-name sym))
1367 (princ "\t")
1368 (if (< (length (symbol-name sym)) 8)
1369 (princ "\t"))
1370 (prin1 (symbol-value sym))
1371 (princ "\n")
1372 ))))
1373
1374 ;;; EDEBUG Handlers
1375 ;;
1376 (add-hook
1377 'edebug-setup-hook
1378 #'(lambda ()
1379
1380 (def-edebug-spec define-lex-spp-macro-declaration-analyzer
1381 (&define name stringp stringp form def-body)
1382 )
1383
1384 (def-edebug-spec define-lex-spp-macro-undeclaration-analyzer
1385 (&define name stringp stringp form)
1386 )
1387
1388 (def-edebug-spec define-lex-spp-include-analyzer
1389 (&define name stringp stringp form def-body))))
1390
1391 (provide 'semantic/lex-spp)
1392
1393 ;; Local variables:
1394 ;; generated-autoload-file: "loaddefs.el"
1395 ;; generated-autoload-load-name: "semantic/lex-spp"
1396 ;; End:
1397
1398 ;;; semantic/lex-spp.el ends here