1 ;;; lex-spp.el --- Semantic Lexical Pre-processor
3 ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; The Semantic Preprocessor works with semantic-lex to provide a phase
25 ;; during lexical analysis to do the work of a pre-processor.
27 ;; A pre-processor identifies lexical syntax mixed in with another language
28 ;; and replaces some keyword tokens with streams of alternate tokens.
30 ;; If you use SPP in your language, be sure to specify this in your
31 ;; semantic language setup function:
33 ;; (add-hook 'semantic-lex-reset-hooks 'semantic-lex-spp-reset-hook nil t)
36 ;; Special Lexical Tokens:
38 ;; There are several special lexical tokens that are used by the
39 ;; Semantic PreProcessor lexer. They are:
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
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.
55 ;; Use `semantic-push-parser-warning' for situations where there are likely
56 ;; macros that are undefined unexpectedly, or other problem.
60 ;; Try to handle the case of:
62 ;; #define NN namespace nn {
71 (require 'semantic
/lex
)
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
)
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
)
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
)
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
)
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.
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.
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."
113 (push ,name semantic-lex-spp-expanded-macro-stack
)
115 (pop semantic-lex-spp-expanded-macro-stack
)))
116 (put 'semantic-lex-with-macro-used
'lisp-indent-function
1)
122 (def-edebug-spec semantic-lex-with-macro-used
128 ;;; MACRO TABLE UTILS
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.
134 (defsubst semantic-lex-spp-symbol
(name)
135 "Return spp symbol with NAME or nil if not found.
136 The search priority is:
138 2. PROJECT specified symbols.
139 3. SYSTEM specified symbols."
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.
148 (and (arrayp semantic-lex-spp-dynamic-macro-symbol-obarray
)
149 (intern-soft name semantic-lex-spp-dynamic-macro-symbol-obarray
))
151 (and (arrayp semantic-lex-spp-project-macro-symbol-obarray
)
152 (intern-soft name semantic-lex-spp-project-macro-symbol-obarray
))
154 (and (arrayp semantic-lex-spp-macro-symbol-obarray
)
155 (intern-soft name semantic-lex-spp-macro-symbol-obarray
))
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
)
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))))
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))))
176 (defun semantic-lex-spp-symbol-set (name value
&optional obarray-in
)
177 "Set value of spp symbol with NAME to VALUE and return VALUE.
178 If optional OBARRAY-IN is non-nil, then use that obarray instead of
180 (if (and (stringp value
) (string= value
"")) (setq value nil
))
181 (set (intern name
(or obarray-in
182 (semantic-lex-spp-dynamic-map)))
185 (defsubst semantic-lex-spp-symbol-remove
(name &optional obarray
)
186 "Remove the spp symbol with NAME.
187 If optional OBARRAY is non-nil, then use that obarray instead of
189 (unintern name
(or obarray
190 (semantic-lex-spp-dynamic-map))))
192 (defun semantic-lex-spp-symbol-push (name value
)
193 "Push macro NAME with VALUE into the map.
194 Reverse with `semantic-lex-spp-symbol-pop'."
195 (let* ((map (semantic-lex-spp-dynamic-map))
196 (stack (semantic-lex-spp-dynamic-map-stack))
197 (mapsym (intern name map
))
198 (stacksym (intern name stack
))
199 (mapvalue (when (boundp mapsym
) (symbol-value mapsym
)))
201 (when (boundp mapsym
)
202 ;; Make sure there is a stack
203 (if (not (boundp stacksym
)) (set stacksym nil
))
204 ;; If there is a value to push, then push it.
205 (set stacksym
(cons mapvalue
(symbol-value stacksym
)))
207 ;; Set our new value here.
211 (defun semantic-lex-spp-symbol-pop (name)
212 "Pop macro NAME from the stackmap into the orig map.
213 Reverse with `semantic-lex-spp-symbol-pop'."
214 (let* ((map (semantic-lex-spp-dynamic-map))
215 (stack (semantic-lex-spp-dynamic-map-stack))
216 (mapsym (intern name map
))
217 (stacksym (intern name stack
))
220 (if (or (not (boundp stacksym
) )
221 (= (length (symbol-value stacksym
)) 0))
222 ;; Nothing to pop, remove it.
224 ;; If there is a value to pop, then add it to the map.
225 (set mapsym
(car (symbol-value stacksym
)))
226 (set stacksym
(cdr (symbol-value stacksym
)))
229 (defsubst semantic-lex-spp-symbol-stream
(name)
230 "Return replacement stream of macro with NAME."
231 (let ((spp (semantic-lex-spp-symbol name
)))
233 (symbol-value spp
))))
235 (defun semantic-lex-make-spp-table (specs)
236 "Convert spp macro list SPECS into an obarray and return it.
237 SPECS must be a list of (NAME . REPLACEMENT) elements, where:
239 NAME is the name of the spp macro symbol to define.
240 REPLACEMENT a string that would be substituted in for NAME."
242 ;; Create the symbol hash table
243 (let ((semantic-lex-spp-macro-symbol-obarray (make-vector 13 0))
245 ;; fill it with stuff
247 (setq spec
(car specs
)
249 (semantic-lex-spp-symbol-set
252 semantic-lex-spp-macro-symbol-obarray
))
253 semantic-lex-spp-macro-symbol-obarray
))
255 (defun semantic-lex-spp-save-table ()
256 "Return a list of spp macros and values.
257 The return list is meant to be saved in a semanticdb table."
259 (when (arrayp semantic-lex-spp-dynamic-macro-symbol-obarray
)
262 (setq macros
(cons (cons (symbol-name symbol
)
263 (symbol-value symbol
))
265 semantic-lex-spp-dynamic-macro-symbol-obarray
))
268 (defun semantic-lex-spp-macros ()
269 "Return a list of spp macros as Lisp symbols.
270 The value of each symbol is the replacement stream."
272 (when (arrayp semantic-lex-spp-macro-symbol-obarray
)
275 (setq macros
(cons symbol macros
)))
276 semantic-lex-spp-macro-symbol-obarray
))
277 (when (arrayp semantic-lex-spp-project-macro-symbol-obarray
)
280 (setq macros
(cons symbol macros
)))
281 semantic-lex-spp-project-macro-symbol-obarray
))
282 (when (arrayp semantic-lex-spp-dynamic-macro-symbol-obarray
)
285 (setq macros
(cons symbol macros
)))
286 semantic-lex-spp-dynamic-macro-symbol-obarray
))
289 (defun semantic-lex-spp-set-dynamic-table (new-entries)
290 "Set the dynamic symbol table to NEW-ENTRIES.
291 For use with semanticdb restoration of state."
292 (dolist (e new-entries
)
293 ;; Default obarray for below is the dynamic map.
294 (semantic-lex-spp-symbol-set (car e
) (cdr e
))))
296 (defun semantic-lex-spp-reset-hook (start end
)
297 "Reset anything needed by SPP for parsing.
298 In this case, reset the dynamic macro symbol table if
299 START is (point-min).
301 (when (= start
(point-min))
302 (setq semantic-lex-spp-dynamic-macro-symbol-obarray nil
303 semantic-lex-spp-dynamic-macro-symbol-obarray-stack nil
304 ;; This shouldn't not be nil, but reset just in case.
305 semantic-lex-spp-expanded-macro-stack nil
)
308 ;;; MACRO EXPANSION: Simple cases
310 ;; If a user fills in the table with simple strings, we can
311 ;; support that by converting them into tokens with the
312 ;; various analyzers that are available.
314 (defun semantic-lex-spp-extract-regex-and-compare (analyzer value
)
315 "Extract a regexp from an ANALYZER and use to match VALUE.
316 Return non-nil if it matches"
317 (let* ((condition (car analyzer
))
318 (regex (cond ((eq (car condition
) 'looking-at
)
323 (string-match regex value
))
326 (defun semantic-lex-spp-simple-macro-to-macro-stream (val beg end argvalues
)
327 "Convert lexical macro contents VAL into a macro expansion stream.
328 These are for simple macro expansions that a user may have typed in directly.
329 As such, we need to analyze the input text, to figure out what kind of real
330 lexical token we should be inserting in its place.
332 Argument VAL is the value of some macro to be converted into a stream.
333 BEG and END are the token bounds of the macro to be expanded
334 that will somehow gain a much longer token stream.
335 ARGVALUES are values for any arg list, or nil."
337 ;; We perform a replacement. Technically, this should
338 ;; be a full lexical step over the "val" string, but take
339 ;; a guess that its just a keyword or existing symbol.
341 ;; Probably a really bad idea. See how it goes.
342 ((semantic-lex-spp-extract-regex-and-compare
343 semantic-lex-symbol-or-keyword val
)
344 (semantic-lex-push-token
345 (semantic-lex-token (or (semantic-lex-keyword-p val
) 'symbol
)
349 ;; Ok, the rest of these are various types of syntax.
350 ;; Conveniences for users that type in their symbol table.
351 ((semantic-lex-spp-extract-regex-and-compare
352 semantic-lex-punctuation val
)
353 (semantic-lex-token 'punctuation beg end val
))
354 ((semantic-lex-spp-extract-regex-and-compare
355 semantic-lex-number val
)
356 (semantic-lex-token 'number beg end val
))
357 ((semantic-lex-spp-extract-regex-and-compare
358 semantic-lex-paren-or-list val
)
359 (semantic-lex-token 'semantic-list beg end val
))
360 ((semantic-lex-spp-extract-regex-and-compare
361 semantic-lex-string val
)
362 (semantic-lex-token 'string beg end val
))
366 ;;; MACRO EXPANSION : Lexical token replacement
368 ;; When substituting in a macro from a token stream of formatted
369 ;; semantic lex tokens, things can be much more complicated.
371 ;; Some macros have arguments that get set into the dynamic macro
372 ;; table during replacement.
374 ;; In general, the macro tokens are substituted into the regular
375 ;; token stream, but placed under the characters of the original
378 ;; Argument lists are saved as a lexical token at the beginning
379 ;; of a replacement value.
381 (defun semantic-lex-spp-one-token-to-txt (tok &optional blocktok
)
382 "Convert the token TOK into a string.
383 If TOK is made of multiple tokens, convert those to text. This
384 conversion is needed if a macro has a merge symbol in it that
385 combines the text of two previously distinct symbols. For
388 #define (a,b) a ## b;
390 If optional string BLOCKTOK matches the expanded value, then do not
391 continue processing recursively."
392 (let ((txt (semantic-lex-token-text tok
))
396 ;; Recursion prevention
397 ((and (stringp blocktok
) (string= txt blocktok
))
400 ((and (eq (car tok
) 'symbol
)
401 (setq sym
(semantic-lex-spp-symbol txt
))
402 (not (semantic-lex-spp-macro-with-args (symbol-value sym
)))
404 ;; Now that we have a symbol,
405 (let ((val (symbol-value sym
)))
407 ;; This is another lexical token.
410 (semantic-lex-spp-one-token-to-txt val txt
))
411 ;; This is a list of tokens.
414 (symbolp (car (car val
))))
415 (mapconcat (lambda (subtok)
416 (semantic-lex-spp-one-token-to-txt subtok
))
419 ;; If val is nil, that's probably wrong.
420 ;; Found a system header case where this was true.
422 ;; Debug wierd stuff.
430 (defun semantic-lex-spp-macro-with-args (val)
431 "If the macro value VAL has an argument list, return the arglist."
432 (when (and val
(consp val
) (consp (car val
))
433 (eq 'spp-arg-list
(car (car val
))))
434 (car (cdr (car val
)))))
436 (defun semantic-lex-spp-token-macro-to-macro-stream (val beg end argvalues
)
437 "Convert lexical macro contents VAL into a macro expansion stream.
438 Argument VAL is the value of some macro to be converted into a stream.
439 BEG and END are the token bounds of the macro to be expanded
440 that will somehow gain a much longer token stream.
441 ARGVALUES are values for any arg list, or nil.
442 See comments in code for information about how token streams are processed
443 and what valid VAL values are."
445 ;; A typical VAL value might be either a stream of tokens.
446 ;; Tokens saved into a macro stream always includes the text from the
447 ;; buffer, since the locations specified probably don't represent
448 ;; that text anymore, or even the same buffer.
450 ;; CASE 1: Simple token stream
452 ;; #define SUPER mysuper::
454 ;;((symbol "mysuper" 480 . 487)
455 ;; (punctuation ":" 487 . 488)
456 ;; (punctuation ":" 488 . 489))
458 ;; CASE 2: Token stream with argument list
460 ;; #define INT_FCN(name) int name (int in)
462 ;; ((spp-arg-list ("name") 558 . 564)
463 ;; (INT "int" 565 . 568)
464 ;; (symbol "name" 569 . 573)
465 ;; (semantic-list "(int in)" 574 . 582))
467 ;; In the second case, a macro with an argument list as the a rgs as the
470 ;; CASE 3: Symbol text merge
472 ;; #define TMP(a) foo_ ## a
474 ;; ((spp-arg-list ("a") 20 . 23)
475 ;; (spp-symbol-merge ((symbol "foo_" 24 . 28) (symbol "a" 32 . 33))
478 ;; Usually in conjunction with a macro with an argument, merging symbol
479 ;; parts is a way of fabricating new symbols from pieces inside the macro.
480 ;; These macros use `spp-symbol-merge' tokens whose TEXT part is another
481 ;; token stream. This sub-stream ought to consist of only 2 SYMBOL pieces,
482 ;; though I suppose keywords might be ok. The end result of this example
483 ;; merge symbol would be (symbol "foo_A" 24 . 33) where A is the symbol
484 ;; passed in from the arg list "a".
486 ;; CASE 4: Nested token streams
489 ;; #define BLA bla FOO(foo)
491 ;; ((INT "int" 82 . 85)
492 ;; (symbol "FOO" 86 . 89)
493 ;; (semantic-list "(foo)" 89 . 94))
495 ;; Nested token FOO shows up in the table of macros, and gets replace
496 ;; inline. This is the same as case 2.
498 (let ((arglist (semantic-lex-spp-macro-with-args val
))
503 ;; CASE 2: Dealing with the arg list.
505 ;; Skip the arg list.
508 ;; Push args into the replacement list.
509 (let ((AV argvalues
))
511 (let* ((argval (car AV
)))
513 (semantic-lex-spp-symbol-push A argval
)
514 (setq argalist
(cons (cons A argval
) argalist
))
515 (setq AV
(cdr AV
)))))
518 ;; Set val-tmp after stripping arguments.
521 ;; CASE 1: Push everything else onto the list.
522 ;; Once the arg list is stripped off, CASE 2 is the same
525 (setq v
(car val-tmp
))
526 (setq val-tmp
(cdr val-tmp
))
528 (let* (;; The text of the current lexical token.
530 ;; Try to convert txt into a macro declaration. If it is
531 ;; not a macro, use nil.
532 (txt-macro-or-nil (semantic-lex-spp-symbol txt
))
533 ;; If our current token is a macro, then pull off the argument
536 (when txt-macro-or-nil
537 (semantic-lex-spp-macro-with-args (symbol-value txt-macro-or-nil
)))
539 ;; We need to peek at the next token when testing for
540 ;; used macros with arg lists.
541 (next-tok-class (semantic-lex-token-class (car val-tmp
)))
545 ;; CASE 3: Merge symbols together.
546 ((eq (semantic-lex-token-class v
) 'spp-symbol-merge
)
547 ;; We need to merge the tokens in the 'text segement together,
548 ;; and produce a single symbol from it.
550 (mapconcat (lambda (tok)
551 (semantic-lex-spp-one-token-to-txt tok
))
554 (semantic-lex-push-token
555 (semantic-lex-token 'symbol beg end newsym
))
558 ;; CASE 2: Argument replacement. If a discovered symbol is in
559 ;; the active list of arguments, then we need to substitute
561 ((and (eq (semantic-lex-token-class v
) 'symbol
) txt-macro-or-nil
562 (or (and macro-and-args
(eq next-tok-class
'semantic-list
))
563 (not macro-and-args
))
568 (semantic-lex-spp-stream-for-arglist (car val-tmp
)))
569 ;; We used up these args. Pull from the stream.
570 (setq val-tmp
(cdr val-tmp
))
573 (semantic-lex-with-macro-used txt
574 ;; Don't recurse directly into this same fcn, because it is
575 ;; convenient to have plain string replacements too.
576 (semantic-lex-spp-macro-to-macro-stream
577 (symbol-value txt-macro-or-nil
)
581 ;; This is a HACK for the C parser. The 'macros text
582 ;; property is some storage so that the parser can do
583 ;; some C specific text manipulations.
584 ((eq (semantic-lex-token-class v
) 'semantic-list
)
585 ;; Push our arg list onto the semantic list.
587 (setq txt
(concat txt
)) ; Copy the text.
588 (put-text-property 0 1 'macros argalist txt
))
589 (semantic-lex-push-token
590 (semantic-lex-token (semantic-lex-token-class v
) beg end txt
))
593 ;; CASE 1: Just another token in the stream.
596 (semantic-lex-push-token
597 (semantic-lex-token (semantic-lex-token-class v
) beg end txt
))
601 ;; CASE 2: The arg list we pushed onto the symbol table
602 ;; must now be removed.
604 (semantic-lex-spp-symbol-pop A
))
609 ;; Used when token streams from different macros include eachother.
610 ;; Merged macro streams perform in place replacements.
612 (defun semantic-lex-spp-merge-streams (raw-stream)
613 "Merge elements from the RAW-STREAM together.
614 Handle spp-concat symbol concatenation.
615 Handle Nested macro replacements.
616 Return the cooked stream."
617 (let ((cooked-stream nil
))
620 (cond ((eq (semantic-lex-token-class (car raw-stream
)) 'spp-concat
)
621 ;; handle hashhash, by skipping it.
622 (setq raw-stream
(cdr raw-stream
))
623 ;; Now merge the symbols.
624 (let ((prev-tok (car cooked-stream
))
625 (next-tok (car raw-stream
)))
626 (setq cooked-stream
(cdr cooked-stream
))
627 (push (semantic-lex-token
629 (semantic-lex-token-start prev-tok
)
630 (semantic-lex-token-end next-tok
)
631 (list prev-tok next-tok
))
635 (push (car raw-stream
) cooked-stream
))
637 (setq raw-stream
(cdr raw-stream
))
640 (nreverse cooked-stream
))
645 ;; There are two types of expansion.
647 ;; 1. Expansion using a value made up of lexical tokens.
648 ;; 2. User input replacement from a plain string.
650 (defun semantic-lex-spp-macro-to-macro-stream (val beg end argvalues
)
651 "Convert lexical macro contents VAL into a macro expansion stream.
652 Argument VAL is the value of some macro to be converted into a stream.
653 BEG and END are the token bounds of the macro to be expanded
654 that will somehow gain a much longer token stream.
655 ARGVALUES are values for any arg list, or nil."
657 ;; If val is nil, then just skip it.
659 ;; If it is a token, then return that token rebuilt.
660 ((and (consp val
) (car val
) (symbolp (car val
)))
661 (semantic-lex-push-token
662 (semantic-lex-token (car val
) beg end
(semantic-lex-token-text val
))))
663 ;; Test for a token list.
664 ((and (consp val
) (consp (car val
)) (car (car val
))
665 (symbolp (car (car val
))))
666 (semantic-lex-spp-token-macro-to-macro-stream val beg end argvalues
))
667 ;; Test for miscellaneous strings.
669 (semantic-lex-spp-simple-macro-to-macro-stream val beg end argvalues
))
672 ;;; --------------------------------------------------------
679 ;; An analyser that will push tokens from a macro in place
680 ;; of the macro symbol.
682 (defun semantic-lex-spp-anlyzer-do-replace (sym val beg end
)
683 "Do the lexical replacement for SYM with VAL.
684 Argument BEG and END specify the bounds of SYM in the buffer."
686 (setq semantic-lex-end-point end
)
692 ;; Check for arguments.
693 (setq arg-in
(semantic-lex-spp-macro-with-args val
))
699 (semantic-lex-spp-one-token-and-move-for-macro
700 ;; NOTE: This used to be (point-at-eol), but
701 ;; that was too close for multi-line arguments
702 ;; to a macro. Point max may be too far if there
703 ;; is a typo in the buffer.
705 ;; Look here for performance issues while a user is typing
708 (setq end
(semantic-lex-token-end arg-parsed
))
710 (when (and (listp arg-parsed
) (eq (car arg-parsed
) 'semantic-list
))
712 ;; Use lex to split up the contents of the argument list.
713 (semantic-lex-spp-stream-for-arglist arg-parsed
)
717 ;; if we have something to sub in, then do it.
718 (semantic-lex-spp-macro-to-macro-stream val beg end arg-split
)
719 (setq semantic-lex-end-point end
)
723 (defvar semantic-lex-spp-replacements-enabled t
724 "Non-nil means do replacements when finding keywords.
725 Disable this only to prevent recursive expansion issues.")
727 (defun semantic-lex-spp-analyzer-push-tokens-for-symbol (str beg end
)
728 "Push lexical tokens for the symbol or keyword STR.
729 STR occurs in the current buffer between BEG and END."
733 ;; It is a macro. Prepare for a replacement.
734 ((and semantic-lex-spp-replacements-enabled
735 (semantic-lex-spp-symbol-p str
))
736 (setq sym
(semantic-lex-spp-symbol str
)
737 val
(symbol-value sym
)
740 (let ((semantic-lex-spp-expanded-macro-stack
741 semantic-lex-spp-expanded-macro-stack
))
743 (semantic-lex-with-macro-used str
744 ;; Do direct replacements of single value macros of macros.
745 ;; This solves issues with a macro containing one symbol that
746 ;; is another macro, and get arg lists passed around.
747 (while (and val
(consp val
)
748 (semantic-lex-token-p (car val
))
750 (eq (semantic-lex-token-class (car val
)) 'symbol
)
751 (semantic-lex-spp-symbol-p (semantic-lex-token-text (car val
)))
754 (setq str
(semantic-lex-token-text (car val
)))
755 (setq sym
(semantic-lex-spp-symbol str
)
756 val
(symbol-value sym
))
758 (setq count
(1+ count
))
759 ;; This prevents a different kind of recursion.
760 (push str semantic-lex-spp-expanded-macro-stack
)
763 (semantic-lex-spp-anlyzer-do-replace sym val beg end
))
768 ;; A regular keyword.
769 (semantic-lex-push-token
770 (semantic-lex-token (or (semantic-lex-keyword-p str
) 'symbol
)
774 (define-lex-regex-analyzer semantic-lex-spp-replace-or-symbol-or-keyword
775 "Like 'semantic-lex-symbol-or-keyword' plus preprocessor macro replacement."
777 (let ((str (match-string 0))
778 (beg (match-beginning 0))
780 (semantic-lex-spp-analyzer-push-tokens-for-symbol str beg end
)))
782 ;;; ANALYZERS FOR NEW MACROS
784 ;; These utilities and analyzer declaration function are for
785 ;; creating an analyzer which produces new macros in the macro table.
787 ;; There are two analyzers. One for new macros, and one for removing
790 (defun semantic-lex-spp-first-token-arg-list (token)
791 "If TOKEN is a semantic-list, turn it into a an SPP ARG LIST."
792 (when (and (consp token
)
793 (symbolp (car token
))
794 (eq 'semantic-list
(car token
)))
795 ;; Convert TOKEN in place.
796 (let ((argsplit (split-string (semantic-lex-token-text token
)
798 (setcar token
'spp-arg-list
)
799 (setcar (nthcdr 1 token
) argsplit
))
802 (defun semantic-lex-spp-one-token-and-move-for-macro (max)
803 "Lex up one token, and move to end of that token.
805 (let ((ans (semantic-lex (point) max
0 0)))
807 (progn (goto-char max
)
809 (when (> (semantic-lex-token-end (car ans
)) max
)
810 (let ((bounds (semantic-lex-token-bounds (car ans
))))
811 (setcdr bounds max
)))
812 (goto-char (semantic-lex-token-end (car ans
)))
816 (defun semantic-lex-spp-stream-for-arglist (token)
817 "Lex up the contents of the arglist TOKEN.
818 Parsing starts inside the parens, and ends at the end of TOKEN."
819 (let ((end (semantic-lex-token-end token
))
824 (if (stringp (nth 1 token
))
825 ;; If the 2nd part of the token is a string, then we have
826 ;; a token specifically extracted from a buffer. Possibly
827 ;; a different buffer. This means we need to do something
828 ;; nice to parse its contents.
829 (let ((txt (semantic-lex-token-text token
)))
830 (semantic-lex-spp-lex-text-string
831 (substring txt
1 (1- (length txt
)))))
833 ;; This part is like the original
834 (goto-char (semantic-lex-token-start token
))
835 ;; A cheat for going into the semantic list.
837 (setq fresh-toks
(semantic-lex-spp-stream-for-macro (1- end
)))
838 (dolist (tok fresh-toks
)
839 (when (memq (semantic-lex-token-class tok
) '(symbol semantic-list
))
840 (setq toks
(cons tok toks
))))
844 (defvar semantic-lex-spp-hack-depth
0
845 "Current depth of recursive calls to `semantic-lex-spp-lex-text-string'.")
847 (defun semantic-lex-spp-lex-text-string (text)
848 "Lex the text string TEXT using the current buffer's state.
849 Use this to parse text extracted from a macro as if it came from
850 the current buffer. Since the lexer is designed to only work in
851 a buffer, we need to create a new buffer, and populate it with rules
852 and variable state from the current buffer."
853 (let* ((semantic-lex-spp-hack-depth (1+ semantic-lex-spp-hack-depth
))
854 (buf (get-buffer-create (format " *SPP parse hack %d*"
855 semantic-lex-spp-hack-depth
)))
859 (origbuff (current-buffer))
860 (important-vars '(semantic-lex-spp-macro-symbol-obarray
861 semantic-lex-spp-project-macro-symbol-obarray
862 semantic-lex-spp-dynamic-macro-symbol-obarray
863 semantic-lex-spp-dynamic-macro-symbol-obarray-stack
864 semantic-lex-spp-expanded-macro-stack
870 ;; Below is a painful hack to make sure everything is setup correctly.
871 (when (not (eq major-mode mode
))
874 ;; Protect against user-hooks that throw errors.
879 ;; Hack in mode-local
880 (activate-mode-local-bindings)
881 ;; CHEATER! The following 3 lines are from
882 ;; `semantic-new-buffer-fcn', but we don't want to turn
883 ;; on all the other annoying modes for this little task.
884 (setq semantic-new-buffer-fcn-was-run t
)
886 (semantic-clear-toplevel-cache)
887 (remove-hook 'semantic-lex-reset-hooks
'semantic-lex-spp-reset-hook
891 ;; Second Cheat: copy key variables regarding macro state from the
892 ;; the originating buffer we are parsing. We need to do this every time
893 ;; since the state changes.
894 (dolist (V important-vars
)
895 (set V
(semantic-buffer-local-value V origbuff
)))
897 (goto-char (point-min))
899 (setq fresh-toks
(semantic-lex-spp-stream-for-macro (point-max))))
901 (dolist (tok fresh-toks
)
902 (when (memq (semantic-lex-token-class tok
) '(symbol semantic-list
))
903 (setq toks
(cons tok toks
))))
908 ;; This is the fist version of semantic-lex-spp-stream-for-arglist
909 ;; that worked pretty well. It doesn't work if the TOKEN was derived
910 ;; from some other buffer, in which case it can get the wrong answer
911 ;; or throw an error if the token location in the originating buffer is
912 ;; larger than the current buffer.
913 ;;(defun semantic-lex-spp-stream-for-arglist-orig (token)
914 ;; "Lex up the contents of the arglist TOKEN.
915 ;; Parsing starts inside the parens, and ends at the end of TOKEN."
917 ;; (let ((end (semantic-lex-token-end token))
920 ;; (goto-char (semantic-lex-token-start token))
921 ;; ;; A cheat for going into the semantic list.
923 ;; (setq fresh-toks (semantic-lex-spp-stream-for-macro (1- end)))
924 ;; (dolist (tok fresh-toks)
925 ;; (when (memq (semantic-lex-token-class tok) '(symbol semantic-list))
926 ;; (setq toks (cons tok toks))))
931 ;; This doesn't work, because some arguments passed into a macro
932 ;; might contain non-simple symbol words, which this doesn't handle.
934 ;; Thus, you need a full lex to occur.
935 ;; (defun semantic-lex-spp-stream-for-arglist-split (token)
936 ;; "Lex up the contents of the arglist TOKEN.
937 ;; Parsing starts inside the parens, and ends at the end of TOKEN."
938 ;; (let* ((txt (semantic-lex-token-text token))
939 ;; (split (split-string (substring txt 1 (1- (length txt)))
941 ;; ;; Hack for lexing.
942 ;; (semantic-lex-spp-analyzer-push-tokens-for-symbol nil))
944 ;; (semantic-lex-spp-analyzer-push-tokens-for-symbol S 0 1))
945 ;; (reverse semantic-lex-spp-analyzer-push-tokens-for-symbol)))
948 (defun semantic-lex-spp-stream-for-macro (eos)
949 "Lex up a stream of tokens for a #define statement.
950 Parsing starts at the current point location.
951 EOS is the end of the stream to lex for this macro."
953 (while (< (point) eos
)
954 (let* ((tok (semantic-lex-spp-one-token-and-move-for-macro eos
))
956 (semantic-lex-token-text tok
)))
959 (push (semantic-lex-token (semantic-lex-token-class tok
)
960 (semantic-lex-token-start tok
)
961 (semantic-lex-token-end tok
)
971 (defmacro define-lex-spp-macro-declaration-analyzer
(name doc regexp tokidx
973 "Define a lexical analyzer for defining new MACROS.
974 NAME is the name of the analyzer.
975 DOC is the documentation for the analyzer.
976 REGEXP is a regular expression for the analyzer to match.
977 See `define-lex-regex-analyzer' for more on regexp.
978 TOKIDX is an index into REGEXP for which a new lexical token
979 of type `spp-macro-def' is to be created.
980 VALFORM are forms that return the value to be saved for this macro, or nil.
981 When implementing a macro, you can use `semantic-lex-spp-stream-for-macro'
982 to convert text into a lexical stream for storage in the macro."
983 (let ((start (make-symbol "start"))
984 (end (make-symbol "end"))
985 (val (make-symbol "val"))
986 (startpnt (make-symbol "startpnt"))
987 (endpnt (make-symbol "endpnt")))
988 `(define-lex-regex-analyzer ,name
991 (let ((,start
(match-beginning ,tokidx
))
992 (,end
(match-end ,tokidx
))
993 (,startpnt semantic-lex-end-point
)
994 (,val
(save-match-data ,@valform
))
995 (,endpnt semantic-lex-end-point
))
996 (semantic-lex-spp-symbol-set
997 (buffer-substring-no-properties ,start
,end
)
999 (semantic-lex-push-token
1000 (semantic-lex-token 'spp-macro-def
1002 ;; Preserve setting of the end point from the calling macro.
1003 (when (and (/= ,startpnt
,endpnt
)
1004 (/= ,endpnt semantic-lex-end-point
))
1005 (setq semantic-lex-end-point
,endpnt
))
1008 (defmacro define-lex-spp-macro-undeclaration-analyzer
(name doc regexp tokidx
)
1009 "Undefine a lexical analyzer for defining new MACROS.
1010 NAME is the name of the analyzer.
1011 DOC is the documentation for the analyzer.
1012 REGEXP is a regular expression for the analyzer to match.
1013 See `define-lex-regex-analyzer' for more on regexp.
1014 TOKIDX is an index into REGEXP for which a new lexical token
1015 of type `spp-macro-undef' is to be created."
1016 (let ((start (make-symbol "start"))
1017 (end (make-symbol "end")))
1018 `(define-lex-regex-analyzer ,name
1021 (let ((,start
(match-beginning ,tokidx
))
1022 (,end
(match-end ,tokidx
))
1024 (semantic-lex-spp-symbol-remove
1025 (buffer-substring-no-properties ,start
,end
))
1026 (semantic-lex-push-token
1027 (semantic-lex-token 'spp-macro-undef
1033 ;; These analyzers help a language define how include files
1034 ;; are identified. These are ONLY for languages that perform
1035 ;; an actual textual includesion, and not for imports.
1037 ;; This section is supposed to allow the macros from the headers to be
1038 ;; added to the local dynamic macro table, but that hasn't been
1041 (defcustom semantic-lex-spp-use-headers-flag nil
1042 "*Non-nil means to pre-parse headers as we go.
1043 For languages that use the Semantic pre-processor, this can
1044 improve the accuracy of parsed files where include files
1045 can change the state of what's parsed in the current file.
1047 Note: Note implemented yet"
1051 (defun semantic-lex-spp-merge-header (name)
1052 "Extract and merge any macros from the header with NAME.
1053 Finds the header file belonging to NAME, gets the macros
1054 from that file, and then merge the macros with our current
1056 (when semantic-lex-spp-use-headers-flag
1057 ;; @todo - do this someday, ok?
1060 (defmacro define-lex-spp-include-analyzer
(name doc regexp tokidx
1062 "Define a lexical analyzer for defining a new INCLUDE lexical token.
1063 Macros defined in the found include will be added to our running table
1064 at the time the include statement is found.
1065 NAME is the name of the analyzer.
1066 DOC is the documentation for the analyzer.
1067 REGEXP is a regular expression for the analyzer to match.
1068 See `define-lex-regex-analyzer' for more on regexp.
1069 TOKIDX is an index into REGEXP for which a new lexical token
1070 of type `spp-macro-include' is to be created.
1071 VALFORM are forms that return the name of the thing being included, and the
1072 type of include. The return value should be of the form:
1074 where NAME is the name of the include, and TYPE is the type of the include,
1075 where a valid symbol is 'system, or nil."
1076 (let ((start (make-symbol "start"))
1077 (end (make-symbol "end"))
1078 (val (make-symbol "val"))
1079 (startpnt (make-symbol "startpnt"))
1080 (endpnt (make-symbol "endpnt")))
1081 `(define-lex-regex-analyzer ,name
1084 (let ((,start
(match-beginning ,tokidx
))
1085 (,end
(match-end ,tokidx
))
1086 (,startpnt semantic-lex-end-point
)
1087 (,val
(save-match-data ,@valform
))
1088 (,endpnt semantic-lex-end-point
))
1089 ;;(message "(car ,val) -> %S" (car ,val))
1090 (semantic-lex-spp-merge-header (car ,val
))
1091 (semantic-lex-push-token
1092 (semantic-lex-token (if (eq (cdr ,val
) 'system
)
1097 ;; Preserve setting of the end point from the calling macro.
1098 (when (and (/= ,startpnt
,endpnt
)
1099 (/= ,endpnt semantic-lex-end-point
))
1100 (setq semantic-lex-end-point
,endpnt
))
1105 ;; Semanticdb can save off macro tables for quick lookup later.
1107 ;; These routines are for saving macro lists into an EIEIO persistent
1109 (defvar semantic-lex-spp-macro-max-length-to-save
200
1110 "*Maximum length of an SPP macro before we opt to not save it.")
1113 (defun semantic-lex-spp-table-write-slot-value (value)
1114 "Write out the VALUE of a slot for EIEIO.
1115 The VALUE is a spp lexical table."
1123 (let* ((first (car (cdr sym
)))
1125 (when (not (listp first
))
1126 (error "Error in macro \"%s\"" (car sym
)))
1127 (when (eq (car first
) 'spp-arg-list
)
1130 (setq rest
(cdr rest
))
1135 (let ((len (length (cdr rest
))))
1140 (princ "nil ;; Error writing macro\n"))))
1141 ((< len semantic-lex-spp-macro-max-length-to-save
)
1146 (princ "nil ;; Error writing macro\n ")))
1149 (princ "nil ;; Too Long!\n ")
1157 ;;; MACRO TABLE DEBUG
1159 (defun semantic-lex-spp-describe (&optional buffer
)
1160 "Describe the current list of spp macros for BUFFER.
1161 If BUFFER is not provided, use the current buffer."
1163 (let ((syms (save-excursion
1164 (if buffer
(set-buffer buffer
))
1165 (semantic-lex-spp-macros)))
1167 (with-output-to-temp-buffer "*SPP MACROS*"
1168 (princ "Macro\t\tValue\n")
1170 (setq sym
(car syms
)
1172 (princ (symbol-name sym
))
1174 (if (< (length (symbol-name sym
)) 8)
1176 (prin1 (symbol-value sym
))
1186 (def-edebug-spec define-lex-spp-macro-declaration-analyzer
1187 (&define name stringp stringp form def-body
)
1190 (def-edebug-spec define-lex-spp-macro-undeclaration-analyzer
1191 (&define name stringp stringp form
)
1194 (def-edebug-spec define-lex-spp-include-analyzer
1195 (&define name stringp stringp form def-body
))))
1197 (provide 'semantic
/lex-spp
)
1200 ;; generated-autoload-file: "loaddefs.el"
1201 ;; generated-autoload-feature: semantic/loaddefs
1202 ;; generated-autoload-load-name: "semantic/lex-spp"
1205 ;; arch-tag: 8877d83e-07ea-4d86-a960-e3562138d8a5
1206 ;;; semantic-lex-spp.el ends here