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