lex-spp.el 41 KB

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