Mark declarations not understood by check-declare.
[bpt/emacs.git] / lisp / cedet / srecode / compile.el
1 ;;; srecode/compile --- Compilation of srecode template files.
2
3 ;; Copyright (C) 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: codegeneration
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24 ;;
25 ;; Compile a Semantic Recoder template file.
26 ;;
27 ;; Template files are parsed using a Semantic/Wisent parser into
28 ;; a tag table. The code therin is then further parsed down using
29 ;; a regular expression parser.
30 ;;
31 ;; The output are a series of EIEIO objects which represent the
32 ;; templates in a way that could be inserted later.
33
34 (require 'semantic)
35 (require 'eieio)
36 (require 'eieio-base)
37 (require 'srecode)
38 (require 'srecode/table)
39
40 (declare-function srecode-template-inserter-newline-child-p "srecode/insert"
41 t t)
42 (declare-function srecode-create-section-dictionary "srecode/dictionary")
43 (declare-function srecode-dictionary-compound-variable "srecode/dictionary")
44
45 ;;; Code:
46
47 ;;; Template Class
48 ;;
49 ;; Templatets describe a patter of text that can be inserted into a
50 ;; buffer.
51 ;;
52 (defclass srecode-template (eieio-named)
53 ((context :initarg :context
54 :initform nil
55 :documentation
56 "Context this template belongs to.")
57 (args :initarg :args
58 :documentation
59 "List of arguments that this template requires.")
60 (code :initarg :code
61 :documentation
62 "Compiled text from the template.")
63 (dictionary :initarg :dictionary
64 :type (or null srecode-dictionary)
65 :documentation
66 "List of section dictinaries.
67 The compiled template can contain lists of section dictionaries,
68 or values that are expected to be passed down into different
69 section macros. The template section dictionaries are merged in with
70 any incomming dictionaries values.")
71 (binding :initarg :binding
72 :documentation
73 "Preferred keybinding for this template in `srecode-minor-mode-map'.")
74 (active :allocation :class
75 :initform nil
76 :documentation
77 "During template insertion, this is the stack of active templates.
78 The top-most template is the 'active' template. Use the accessor methods
79 for push, pop, and peek for the active template.")
80 (table :initarg :table
81 :documentation
82 "The table this template lives in.")
83 )
84 "Class defines storage for semantic recoder templates.")
85
86 (defun srecode-flush-active-templates ()
87 "Flush the active template storage.
88 Useful if something goes wrong in SRecode, and the active tempalte
89 stack is broken."
90 (interactive)
91 (if (oref srecode-template active)
92 (when (y-or-n-p (format "%d active templates. Flush? "
93 (length (oref srecode-template active))))
94 (oset-default srecode-template active nil))
95 (message "No active templates to flush."))
96 )
97
98 ;;; Inserters
99 ;;
100 ;; Each inserter object manages a different thing that
101 ;; might be inserted into a template output stream.
102 ;;
103 ;; The 'srecode-insert-method' on each inserter does the actual
104 ;; work, and the smaller, simple inserter object is saved in
105 ;; the compiled templates.
106 ;;
107 ;; See srecode-insert.el for the specialized classes.
108 ;;
109 (defclass srecode-template-inserter (eieio-named)
110 ((secondname :initarg :secondname
111 :type (or null string)
112 :documentation
113 "If there is a colon in the inserter's name, it represents
114 additional static argument data."))
115 "This represents an item to be inserted via a template macro.
116 Plain text strings are not handled via this baseclass."
117 :abstract t)
118
119 (defmethod srecode-parse-input ((ins srecode-template-inserter)
120 tag input STATE)
121 "For the template inserter INS, parse INPUT.
122 Shorten input only by the amount needed.
123 Return the remains of INPUT.
124 STATE is the current compilation state."
125 input)
126
127 (defmethod srecode-match-end ((ins srecode-template-inserter) name)
128 "For the template inserter INS, do I end a section called NAME?"
129 nil)
130
131 (defmethod srecode-inserter-apply-state ((ins srecode-template-inserter) STATE)
132 "For the template inserter INS, apply information from STATE."
133 nil)
134
135 (defmethod srecode-inserter-prin-example :STATIC ((ins srecode-template-inserter)
136 escape-start escape-end)
137 "Insert an example using inserter INS.
138 Arguments ESCAPE-START and ESCAPE-END are the current escape sequences in use."
139 (princ " ")
140 (princ escape-start)
141 (when (and (slot-exists-p ins 'key) (oref ins key))
142 (princ (format "%c" (oref ins key))))
143 (princ "VARNAME")
144 (princ escape-end)
145 (terpri)
146 )
147
148
149 ;;; Compile State
150 (defclass srecode-compile-state ()
151 ((context :initform "declaration"
152 :documentation "The active context.")
153 (prompts :initform nil
154 :documentation "The active prompts.")
155 (escape_start :initform "{{"
156 :documentation "The starting escape sequence.")
157 (escape_end :initform "}}"
158 :documentation "The ending escape sequence.")
159 )
160 "Current state of the compile.")
161
162 (defmethod srecode-compile-add-prompt ((state srecode-compile-state)
163 prompttag)
164 "Add PROMPTTAG to the current list of prompts."
165 (with-slots (prompts) state
166 (let ((match (assoc (semantic-tag-name prompttag) prompts))
167 (newprompts prompts))
168 (when match
169 (let ((tmp prompts))
170 (setq newprompts nil)
171 (while tmp
172 (when (not (string= (car (car tmp))
173 (car prompttag)))
174 (setq newprompts (cons (car tmp)
175 newprompts)))
176 (setq tmp (cdr tmp)))))
177 (setq prompts (cons prompttag newprompts)))
178 ))
179
180 ;;; TEMPLATE COMPILER
181 ;;
182 (defun srecode-compile-file (fname)
183 "Compile the templates from the file FNAME."
184 (let ((peb (get-file-buffer fname)))
185 (save-excursion
186 ;; Make whatever it is local.
187 (if (not peb)
188 (set-buffer (semantic-find-file-noselect fname))
189 (set-buffer peb))
190 ;; Do the compile.
191 (srecode-compile-templates)
192 ;; Trash the buffer if we had to read it in.
193 (if (not peb)
194 (kill-buffer (current-buffer)))
195 )))
196
197 ;;;###autoload
198 (defun srecode-compile-templates ()
199 "Compile a semantic recode template file into a mode-local variable."
200 (interactive)
201 (require 'srecode/insert)
202 (message "Compiling template %s..."
203 (file-name-nondirectory (buffer-file-name)))
204 (let ((tags (semantic-fetch-tags))
205 (tag nil)
206 (class nil)
207 (table nil)
208 (STATE (srecode-compile-state (file-name-nondirectory
209 (buffer-file-name))))
210 (mode nil)
211 (application nil)
212 (priority nil)
213 (vars nil)
214 )
215
216 ;;
217 ;; COMPILE
218 ;;
219 (while tags
220 (setq tag (car tags)
221 class (semantic-tag-class tag))
222 ;; What type of item is it?
223 (cond
224 ;; CONTEXT tags specify the context all future tags
225 ;; belong to.
226 ((eq class 'context)
227 (oset STATE context (semantic-tag-name tag))
228 )
229
230 ;; PROMPT tags specify prompts for dictionary ? inserters
231 ;; which appear in the following templates
232 ((eq class 'prompt)
233 (srecode-compile-add-prompt STATE tag)
234 )
235
236 ;; VARIABLE tags can specify operational control
237 ((eq class 'variable)
238 (let* ((name (semantic-tag-name tag))
239 (value (semantic-tag-variable-default tag))
240 (firstvalue (car value)))
241 ;; If it is a single string, and one value, then
242 ;; look to see if it is one of our special variables.
243 (if (and (= (length value) 1) (stringp firstvalue))
244 (cond ((string= name "mode")
245 (setq mode (intern firstvalue)))
246 ((string= name "escape_start")
247 (oset STATE escape_start firstvalue)
248 )
249 ((string= name "escape_end")
250 (oset STATE escape_end firstvalue)
251 )
252 ((string= name "application")
253 (setq application (read firstvalue)))
254 ((string= name "priority")
255 (setq priority (read firstvalue)))
256 (t
257 ;; Assign this into some table of variables.
258 (setq vars (cons (cons name firstvalue) vars))
259 ))
260 ;; If it isn't a single string, then the value of the
261 ;; variable belongs to a compound dictionary value.
262 ;;
263 ;; Create a compound dictionary value from "value".
264 (require 'srecode/dictionary)
265 (let ((cv (srecode-dictionary-compound-variable
266 name :value value)))
267 (setq vars (cons (cons name cv) vars)))
268 ))
269 )
270
271 ;; FUNCTION tags are really templates.
272 ((eq class 'function)
273 (setq table (cons (srecode-compile-one-template-tag tag STATE)
274 table))
275 )
276
277 ;; Ooops
278 (t (error "Unknown TAG class %s" class))
279 )
280 ;; Continue
281 (setq tags (cdr tags)))
282
283 ;; MSG - Before install since nreverse whacks our list.
284 (message "%d templates compiled for %s"
285 (length table) mode)
286
287 ;;
288 ;; APPLY TO MODE
289 ;;
290 (if (not mode)
291 (error "You must specify a MODE for your templates"))
292
293 ;;
294 ;; Calculate priority
295 ;;
296 (if (not priority)
297 (let ((d (file-name-directory (buffer-file-name)))
298 (sd (file-name-directory (locate-library "srecode")))
299 (defaultdelta (if (eq mode 'default) 20 0)))
300 (if (string= d sd)
301 (setq priority (+ 80 defaultdelta))
302 (setq priority (+ 30 defaultdelta)))
303 (message "Templates %s has estimated priority of %d"
304 (file-name-nondirectory (buffer-file-name))
305 priority))
306 (message "Compiling templates %s priority %d... done!"
307 (file-name-nondirectory (buffer-file-name))
308 priority))
309
310 ;; Save it up!
311 (srecode-compile-template-table table mode priority application vars)
312 )
313 )
314
315 (defun srecode-compile-one-template-tag (tag STATE)
316 "Compile a template tag TAG into an srecode template class.
317 STATE is the current compile state as an object `srecode-compile-state'."
318 (require 'srecode/dictionary)
319 (let* ((context (oref STATE context))
320 (codeout (srecode-compile-split-code
321 tag (semantic-tag-get-attribute tag :code)
322 STATE))
323 (code (cdr codeout))
324 (args (semantic-tag-function-arguments tag))
325 (binding (semantic-tag-get-attribute tag :binding))
326 (rawdicts (semantic-tag-get-attribute tag :dictionaries))
327 (sdicts (srecode-create-section-dictionary rawdicts STATE))
328 (addargs nil)
329 )
330 ; (message "Compiled %s to %d codes with %d args and %d prompts."
331 ; (semantic-tag-name tag)
332 ; (length code)
333 ; (length args)
334 ; (length prompts))
335 (while args
336 (setq addargs (cons (intern (car args)) addargs))
337 (when (eq (car addargs) :blank)
338 ;; If we have a wrap, then put wrap inserters on both
339 ;; ends of the code.
340 (setq code (append
341 (list (srecode-compile-inserter "BLANK"
342 "\r"
343 STATE
344 :secondname nil
345 :where 'begin))
346 code
347 (list (srecode-compile-inserter "BLANK"
348 "\r"
349 STATE
350 :secondname nil
351 :where 'end))
352 )))
353 (setq args (cdr args)))
354 (srecode-template (semantic-tag-name tag)
355 :context context
356 :args (nreverse addargs)
357 :dictionary sdicts
358 :binding binding
359 :code code)
360 ))
361
362 (defun srecode-compile-do-hard-newline-p (comp)
363 "Examine COMP to decide if the upcoming newline should be hard.
364 It is hard if the previous inserter is a newline object."
365 (while (and comp (stringp (car comp)))
366 (setq comp (cdr comp)))
367 (or (not comp)
368 (require 'srecode/insert)
369 (srecode-template-inserter-newline-child-p (car comp))))
370
371 (defun srecode-compile-split-code (tag str STATE
372 &optional end-name)
373 "Split the code for TAG into something templatable.
374 STR is the string of code from TAG to split.
375 STATE is the current compile state.
376 ESCAPE_START and ESCAPE_END are regexps that indicate the beginning
377 escape character, and end escape character pattern for expandable
378 macro names.
379 Optional argument END-NAME specifies the name of a token upon which
380 parsing should stop.
381 If END-NAME is specified, and the input string"
382 (let* ((what str)
383 (end-token nil)
384 (comp nil)
385 (regex (concat "\n\\|" (regexp-quote (oref STATE escape_start))))
386 (regexend (regexp-quote (oref STATE escape_end)))
387 )
388 (while (and what (not end-token))
389 (cond
390 ((string-match regex what)
391 (let* ((prefix (substring what 0 (match-beginning 0)))
392 (match (substring what
393 (match-beginning 0)
394 (match-end 0)))
395 (namestart (match-end 0))
396 (junk (string-match regexend what namestart))
397 end tail name key)
398 ;; Add string to compiled output
399 (when (> (length prefix) 0)
400 (setq comp (cons prefix comp)))
401 (if (string= match "\n")
402 ;; Do newline thingy.
403 (let ((new-inserter
404 (srecode-compile-inserter
405 "INDENT"
406 "\n"
407 STATE
408 :secondname nil
409 ;; This newline is "hard" meaning ALWAYS do it
410 ;; if the previous entry is also a newline.
411 ;; Without it, user entered blank lines will be
412 ;; ignored.
413 :hard (srecode-compile-do-hard-newline-p comp)
414 )))
415 ;; Trim WHAT back.
416 (setq what (substring what namestart))
417 (when (> (length what) 0)
418 ;; make the new inserter, but only if we aren't last.
419 (setq comp (cons new-inserter comp))
420 ))
421 ;; Regular inserter thingy.
422 (setq end (if junk
423 (match-beginning 0)
424 (error "Could not find end escape for %s"
425 (semantic-tag-name tag)))
426 tail (match-end 0))
427 (cond ((not end)
428 (error "No matching escape end for %s"
429 (semantic-tag-name tag)))
430 ((<= end namestart)
431 (error "Stray end escape for %s"
432 (semantic-tag-name tag)))
433 )
434 ;; Add string to compiled output
435 (setq name (substring what namestart end)
436 key nil)
437 ;; Trim WHAT back.
438 (setq what (substring what tail))
439 ;; Get the inserter
440 (let ((new-inserter
441 (srecode-compile-parse-inserter name STATE))
442 )
443 ;; If this is an end inserter, then assign into
444 ;; the end-token.
445 (if (srecode-match-end new-inserter end-name)
446 (setq end-token new-inserter))
447 ;; Add the inserter to our compilation stream.
448 (setq comp (cons new-inserter comp))
449 ;; Allow the inserter an opportunity to modify
450 ;; the input stream.
451 (setq what (srecode-parse-input new-inserter tag what
452 STATE))
453 )
454 )))
455 (t
456 (if end-name
457 (error "Unmatched section end %s" end-name))
458 (setq comp (cons what comp)
459 what nil))))
460 (cons what (nreverse comp))))
461
462 (defun srecode-compile-parse-inserter (txt STATE)
463 "Parse the inserter TXT with the current STATE.
464 Return an inserter object."
465 (let ((key (aref txt 0))
466 name
467 )
468 (if (and (or (< key ?A) (> key ?Z))
469 (or (< key ?a) (> key ?z)) )
470 (setq name (substring txt 1))
471 (setq name txt
472 key nil))
473 (let* ((junk (string-match ":" name))
474 (namepart (if junk
475 (substring name 0 (match-beginning 0))
476 name))
477 (secondname (if junk
478 (substring name (match-end 0))
479 nil))
480 (new-inserter (srecode-compile-inserter
481 namepart key STATE
482 :secondname secondname
483 )))
484 ;; Return the new inserter
485 new-inserter)))
486
487 (defun srecode-compile-inserter (name key STATE &rest props)
488 "Create an srecode inserter object for some macro NAME.
489 KEY indicates a single character key representing a type
490 of inserter to create.
491 STATE is the current compile state.
492 PROPS are additional properties that might need to be passed
493 to the inserter constructor."
494 ;;(message "Compile: %s %S" name props)
495 (if (not key)
496 (apply 'srecode-template-inserter-variable name props)
497 (let ((classes (class-children srecode-template-inserter))
498 (new nil))
499 ;; Loop over the various subclasses and
500 ;; create the correct inserter.
501 (while (and (not new) classes)
502 (setq classes (append classes (class-children (car classes))))
503 ;; Do we have a match?
504 (when (and (not (class-abstract-p (car classes)))
505 (equal (oref (car classes) key) key))
506 ;; Create the new class, and apply state.
507 (setq new (apply (car classes) name props))
508 (srecode-inserter-apply-state new STATE)
509 )
510 (setq classes (cdr classes)))
511 (if (not new) (error "SRECODE: Unknown macro code %S" key))
512 new)))
513
514 (defun srecode-compile-template-table (templates mode priority application vars)
515 "Compile a list of TEMPLATES into an semantic recode table.
516 The table being compiled is for MODE, or the string \"default\".
517 PRIORITY is a numerical value that indicates this tables location
518 in an ordered search.
519 APPLICATION is the name of the application these templates belong to.
520 A list of defined variables VARS provides a variable table."
521 (let ((namehash (make-hash-table :test 'equal
522 :size (length templates)))
523 (contexthash (make-hash-table :test 'equal :size 10))
524 (lp templates)
525 )
526
527 (while lp
528
529 (let* ((objname (oref (car lp) :object-name))
530 (context (oref (car lp) :context))
531 (globalname (concat context ":" objname))
532 )
533
534 ;; Place this template object into the global name hash.
535 (puthash globalname (car lp) namehash)
536
537 ;; Place this template into the specific context name hash.
538 (let ((hs (gethash context contexthash)))
539 ;; Make a new context if none was available.
540 (when (not hs)
541 (setq hs (make-hash-table :test 'equal :size 20))
542 (puthash context hs contexthash))
543 ;; Put into that contenxt's hash.
544 (puthash objname (car lp) hs)
545 )
546
547 (setq lp (cdr lp))))
548
549 (let* ((table (srecode-mode-table-new mode (buffer-file-name)
550 :templates (nreverse templates)
551 :namehash namehash
552 :contexthash contexthash
553 :variables vars
554 :major-mode mode
555 :priority priority
556 :application application))
557 (tmpl (oref table templates)))
558 ;; Loop over all the templates, and xref.
559 (while tmpl
560 (oset (car tmpl) :table table)
561 (setq tmpl (cdr tmpl))))
562 ))
563
564
565
566 ;;; DEBUG
567 ;;
568 ;; Dump out information about the current srecoder compiled templates.
569 ;;
570
571 (defmethod srecode-dump ((tmp srecode-template))
572 "Dump the contents of the SRecode template tmp."
573 (princ "== Template \"")
574 (princ (object-name-string tmp))
575 (princ "\" in context ")
576 (princ (oref tmp context))
577 (princ "\n")
578 (when (oref tmp args)
579 (princ " Arguments: ")
580 (prin1 (oref tmp args))
581 (princ "\n"))
582 (when (oref tmp dictionary)
583 (princ " Section Dictionaries:\n")
584 (srecode-dump (oref tmp dictionary) 4)
585 ;(princ "\n")
586 )
587 (when (and (slot-boundp tmp 'binding) (oref tmp binding))
588 (princ " Binding: ")
589 (prin1 (oref tmp binding))
590 (princ "\n"))
591 (princ " Compiled Codes:\n")
592 (srecode-dump-code-list (oref tmp code) " ")
593 (princ "\n\n")
594 )
595
596 (defun srecode-dump-code-list (code indent)
597 "Dump the CODE from a template code list to standard output.
598 Argument INDENT specifies the indentation level for the list."
599 (let ((i 1))
600 (while code
601 (princ indent)
602 (prin1 i)
603 (princ ") ")
604 (cond ((stringp (car code))
605 (prin1 (car code)))
606 ((srecode-template-inserter-child-p (car code))
607 (srecode-dump (car code) indent))
608 (t
609 (princ "Unknown Code: ")
610 (prin1 (car code))))
611 (setq code (cdr code)
612 i (1+ i))
613 (when code
614 (princ "\n"))))
615 )
616
617 (defmethod srecode-dump ((ins srecode-template-inserter) indent)
618 "Dump the state of the SRecode template inserter INS."
619 (princ "INS: \"")
620 (princ (object-name-string ins))
621 (when (oref ins :secondname)
622 (princ "\" : \"")
623 (princ (oref ins :secondname)))
624 (princ "\" type \"")
625 (let* ((oc (symbol-name (object-class ins)))
626 (junk (string-match "srecode-template-inserter-" oc))
627 (on (if junk
628 (substring oc (match-end 0))
629 oc)))
630 (princ on))
631 (princ "\"")
632 )
633
634 (provide 'srecode/compile)
635
636 ;; Local variables:
637 ;; generated-autoload-file: "loaddefs.el"
638 ;; generated-autoload-feature: srecode/loaddefs
639 ;; generated-autoload-load-name: "srecode/compile"
640 ;; End:
641
642 ;;; srecode/compile.el ends here