* cedet/semantic.el (semantic-new-buffer-setup-functions): New
[emacs.git] / lisp / cedet / srecode / compile.el
blob626d8c8d0d05086979fca687d3a45941775a28d0
1 ;;; srecode/compile --- Compilation of srecode template files.
3 ;; Copyright (C) 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: codegeneration
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;; Compile a Semantic Recoder template file.
27 ;; Template files are parsed using a Semantic/Wisent parser into
28 ;; a tag table. The code therein is then further parsed down using
29 ;; a regular expression parser.
31 ;; The output are a series of EIEIO objects which represent the
32 ;; templates in a way that could be inserted later.
34 (require 'semantic)
35 (require 'eieio)
36 (require 'eieio-base)
37 (require 'srecode)
38 (require 'srecode/table)
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")
45 ;;; Code:
47 ;;; Template Class
49 ;; Templatets describe a patter of text that can be inserted into a
50 ;; buffer.
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.")
84 "Class defines storage for semantic recoder templates.")
86 (defun srecode-flush-active-templates ()
87 "Flush the active template storage.
88 Useful if something goes wrong in SRecode, and the active template
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."))
98 ;;; Inserters
100 ;; Each inserter object manages a different thing that
101 ;; might be inserted into a template output stream.
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.
107 ;; See srecode-insert.el for the specialized classes.
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)
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)
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)
131 (defmethod srecode-inserter-apply-state ((ins srecode-template-inserter) STATE)
132 "For the template inserter INS, apply information from STATE."
133 nil)
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)
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.")
160 "Current state of the compile.")
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)))
180 ;;; TEMPLATE COMPILER
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 (unless (semantic-active-p)
192 (semantic-new-buffer-fcn))
193 (srecode-compile-templates)
194 ;; Trash the buffer if we had to read it in.
195 (if (not peb)
196 (kill-buffer (current-buffer)))
199 ;;;###autoload
200 (defun srecode-compile-templates ()
201 "Compile a semantic recode template file into a mode-local variable."
202 (interactive)
203 (require 'srecode/insert)
204 (message "Compiling template %s..."
205 (file-name-nondirectory (buffer-file-name)))
206 (let ((tags (semantic-fetch-tags))
207 (tag nil)
208 (class nil)
209 (table nil)
210 (STATE (srecode-compile-state (file-name-nondirectory
211 (buffer-file-name))))
212 (mode nil)
213 (application nil)
214 (priority nil)
215 (vars nil)
219 ;; COMPILE
221 (while tags
222 (setq tag (car tags)
223 class (semantic-tag-class tag))
224 ;; What type of item is it?
225 (cond
226 ;; CONTEXT tags specify the context all future tags
227 ;; belong to.
228 ((eq class 'context)
229 (oset STATE context (semantic-tag-name tag))
232 ;; PROMPT tags specify prompts for dictionary ? inserters
233 ;; which appear in the following templates
234 ((eq class 'prompt)
235 (srecode-compile-add-prompt STATE tag)
238 ;; VARIABLE tags can specify operational control
239 ((eq class 'variable)
240 (let* ((name (semantic-tag-name tag))
241 (value (semantic-tag-variable-default tag))
242 (firstvalue (car value)))
243 ;; If it is a single string, and one value, then
244 ;; look to see if it is one of our special variables.
245 (if (and (= (length value) 1) (stringp firstvalue))
246 (cond ((string= name "mode")
247 (setq mode (intern firstvalue)))
248 ((string= name "escape_start")
249 (oset STATE escape_start firstvalue)
251 ((string= name "escape_end")
252 (oset STATE escape_end firstvalue)
254 ((string= name "application")
255 (setq application (read firstvalue)))
256 ((string= name "priority")
257 (setq priority (read firstvalue)))
259 ;; Assign this into some table of variables.
260 (setq vars (cons (cons name firstvalue) vars))
262 ;; If it isn't a single string, then the value of the
263 ;; variable belongs to a compound dictionary value.
265 ;; Create a compound dictionary value from "value".
266 (require 'srecode/dictionary)
267 (let ((cv (srecode-dictionary-compound-variable
268 name :value value)))
269 (setq vars (cons (cons name cv) vars)))
273 ;; FUNCTION tags are really templates.
274 ((eq class 'function)
275 (setq table (cons (srecode-compile-one-template-tag tag STATE)
276 table))
279 ;; Ooops
280 (t (error "Unknown TAG class %s" class))
282 ;; Continue
283 (setq tags (cdr tags)))
285 ;; MSG - Before install since nreverse whacks our list.
286 (message "%d templates compiled for %s"
287 (length table) mode)
290 ;; APPLY TO MODE
292 (if (not mode)
293 (error "You must specify a MODE for your templates"))
296 ;; Calculate priority
298 (if (not priority)
299 (let ((d (file-name-directory (buffer-file-name)))
300 (sd (file-name-directory (locate-library "srecode")))
301 (defaultdelta (if (eq mode 'default) 20 0)))
302 (if (string= d sd)
303 (setq priority (+ 80 defaultdelta))
304 (setq priority (+ 30 defaultdelta)))
305 (message "Templates %s has estimated priority of %d"
306 (file-name-nondirectory (buffer-file-name))
307 priority))
308 (message "Compiling templates %s priority %d... done!"
309 (file-name-nondirectory (buffer-file-name))
310 priority))
312 ;; Save it up!
313 (srecode-compile-template-table table mode priority application vars)
317 (defun srecode-compile-one-template-tag (tag STATE)
318 "Compile a template tag TAG into an srecode template class.
319 STATE is the current compile state as an object `srecode-compile-state'."
320 (require 'srecode/dictionary)
321 (let* ((context (oref STATE context))
322 (codeout (srecode-compile-split-code
323 tag (semantic-tag-get-attribute tag :code)
324 STATE))
325 (code (cdr codeout))
326 (args (semantic-tag-function-arguments tag))
327 (binding (semantic-tag-get-attribute tag :binding))
328 (rawdicts (semantic-tag-get-attribute tag :dictionaries))
329 (sdicts (srecode-create-section-dictionary rawdicts STATE))
330 (addargs nil)
332 ; (message "Compiled %s to %d codes with %d args and %d prompts."
333 ; (semantic-tag-name tag)
334 ; (length code)
335 ; (length args)
336 ; (length prompts))
337 (while args
338 (setq addargs (cons (intern (car args)) addargs))
339 (when (eq (car addargs) :blank)
340 ;; If we have a wrap, then put wrap inserters on both
341 ;; ends of the code.
342 (setq code (append
343 (list (srecode-compile-inserter "BLANK"
344 "\r"
345 STATE
346 :secondname nil
347 :where 'begin))
348 code
349 (list (srecode-compile-inserter "BLANK"
350 "\r"
351 STATE
352 :secondname nil
353 :where 'end))
355 (setq args (cdr args)))
356 (srecode-template (semantic-tag-name tag)
357 :context context
358 :args (nreverse addargs)
359 :dictionary sdicts
360 :binding binding
361 :code code)
364 (defun srecode-compile-do-hard-newline-p (comp)
365 "Examine COMP to decide if the upcoming newline should be hard.
366 It is hard if the previous inserter is a newline object."
367 (while (and comp (stringp (car comp)))
368 (setq comp (cdr comp)))
369 (or (not comp)
370 (require 'srecode/insert)
371 (srecode-template-inserter-newline-child-p (car comp))))
373 (defun srecode-compile-split-code (tag str STATE
374 &optional end-name)
375 "Split the code for TAG into something templatable.
376 STR is the string of code from TAG to split.
377 STATE is the current compile state.
378 ESCAPE_START and ESCAPE_END are regexps that indicate the beginning
379 escape character, and end escape character pattern for expandable
380 macro names.
381 Optional argument END-NAME specifies the name of a token upon which
382 parsing should stop.
383 If END-NAME is specified, and the input string"
384 (let* ((what str)
385 (end-token nil)
386 (comp nil)
387 (regex (concat "\n\\|" (regexp-quote (oref STATE escape_start))))
388 (regexend (regexp-quote (oref STATE escape_end)))
390 (while (and what (not end-token))
391 (cond
392 ((string-match regex what)
393 (let* ((prefix (substring what 0 (match-beginning 0)))
394 (match (substring what
395 (match-beginning 0)
396 (match-end 0)))
397 (namestart (match-end 0))
398 (junk (string-match regexend what namestart))
399 end tail name key)
400 ;; Add string to compiled output
401 (when (> (length prefix) 0)
402 (setq comp (cons prefix comp)))
403 (if (string= match "\n")
404 ;; Do newline thingy.
405 (let ((new-inserter
406 (srecode-compile-inserter
407 "INDENT"
408 "\n"
409 STATE
410 :secondname nil
411 ;; This newline is "hard" meaning ALWAYS do it
412 ;; if the previous entry is also a newline.
413 ;; Without it, user entered blank lines will be
414 ;; ignored.
415 :hard (srecode-compile-do-hard-newline-p comp)
417 ;; Trim WHAT back.
418 (setq what (substring what namestart))
419 (when (> (length what) 0)
420 ;; make the new inserter, but only if we aren't last.
421 (setq comp (cons new-inserter comp))
423 ;; Regular inserter thingy.
424 (setq end (if junk
425 (match-beginning 0)
426 (error "Could not find end escape for %s"
427 (semantic-tag-name tag)))
428 tail (match-end 0))
429 (cond ((not end)
430 (error "No matching escape end for %s"
431 (semantic-tag-name tag)))
432 ((<= end namestart)
433 (error "Stray end escape for %s"
434 (semantic-tag-name tag)))
436 ;; Add string to compiled output
437 (setq name (substring what namestart end)
438 key nil)
439 ;; Trim WHAT back.
440 (setq what (substring what tail))
441 ;; Get the inserter
442 (let ((new-inserter
443 (srecode-compile-parse-inserter name STATE))
445 ;; If this is an end inserter, then assign into
446 ;; the end-token.
447 (if (srecode-match-end new-inserter end-name)
448 (setq end-token new-inserter))
449 ;; Add the inserter to our compilation stream.
450 (setq comp (cons new-inserter comp))
451 ;; Allow the inserter an opportunity to modify
452 ;; the input stream.
453 (setq what (srecode-parse-input new-inserter tag what
454 STATE))
458 (if end-name
459 (error "Unmatched section end %s" end-name))
460 (setq comp (cons what comp)
461 what nil))))
462 (cons what (nreverse comp))))
464 (defun srecode-compile-parse-inserter (txt STATE)
465 "Parse the inserter TXT with the current STATE.
466 Return an inserter object."
467 (let ((key (aref txt 0))
468 name
470 (if (and (or (< key ?A) (> key ?Z))
471 (or (< key ?a) (> key ?z)) )
472 (setq name (substring txt 1))
473 (setq name txt
474 key nil))
475 (let* ((junk (string-match ":" name))
476 (namepart (if junk
477 (substring name 0 (match-beginning 0))
478 name))
479 (secondname (if junk
480 (substring name (match-end 0))
481 nil))
482 (new-inserter (srecode-compile-inserter
483 namepart key STATE
484 :secondname secondname
486 ;; Return the new inserter
487 new-inserter)))
489 (defun srecode-compile-inserter (name key STATE &rest props)
490 "Create an srecode inserter object for some macro NAME.
491 KEY indicates a single character key representing a type
492 of inserter to create.
493 STATE is the current compile state.
494 PROPS are additional properties that might need to be passed
495 to the inserter constructor."
496 ;;(message "Compile: %s %S" name props)
497 (if (not key)
498 (apply 'srecode-template-inserter-variable name props)
499 (let ((classes (class-children srecode-template-inserter))
500 (new nil))
501 ;; Loop over the various subclasses and
502 ;; create the correct inserter.
503 (while (and (not new) classes)
504 (setq classes (append classes (class-children (car classes))))
505 ;; Do we have a match?
506 (when (and (not (class-abstract-p (car classes)))
507 (equal (oref (car classes) key) key))
508 ;; Create the new class, and apply state.
509 (setq new (apply (car classes) name props))
510 (srecode-inserter-apply-state new STATE)
512 (setq classes (cdr classes)))
513 (if (not new) (error "SRECODE: Unknown macro code %S" key))
514 new)))
516 (defun srecode-compile-template-table (templates mode priority application vars)
517 "Compile a list of TEMPLATES into an semantic recode table.
518 The table being compiled is for MODE, or the string \"default\".
519 PRIORITY is a numerical value that indicates this tables location
520 in an ordered search.
521 APPLICATION is the name of the application these templates belong to.
522 A list of defined variables VARS provides a variable table."
523 (let ((namehash (make-hash-table :test 'equal
524 :size (length templates)))
525 (contexthash (make-hash-table :test 'equal :size 10))
526 (lp templates)
529 (while lp
531 (let* ((objname (oref (car lp) :object-name))
532 (context (oref (car lp) :context))
533 (globalname (concat context ":" objname))
536 ;; Place this template object into the global name hash.
537 (puthash globalname (car lp) namehash)
539 ;; Place this template into the specific context name hash.
540 (let ((hs (gethash context contexthash)))
541 ;; Make a new context if none was available.
542 (when (not hs)
543 (setq hs (make-hash-table :test 'equal :size 20))
544 (puthash context hs contexthash))
545 ;; Put into that contenxt's hash.
546 (puthash objname (car lp) hs)
549 (setq lp (cdr lp))))
551 (let* ((table (srecode-mode-table-new mode (buffer-file-name)
552 :templates (nreverse templates)
553 :namehash namehash
554 :contexthash contexthash
555 :variables vars
556 :major-mode mode
557 :priority priority
558 :application application))
559 (tmpl (oref table templates)))
560 ;; Loop over all the templates, and xref.
561 (while tmpl
562 (oset (car tmpl) :table table)
563 (setq tmpl (cdr tmpl))))
568 ;;; DEBUG
570 ;; Dump out information about the current srecoder compiled templates.
573 (defmethod srecode-dump ((tmp srecode-template))
574 "Dump the contents of the SRecode template tmp."
575 (princ "== Template \"")
576 (princ (object-name-string tmp))
577 (princ "\" in context ")
578 (princ (oref tmp context))
579 (princ "\n")
580 (when (oref tmp args)
581 (princ " Arguments: ")
582 (prin1 (oref tmp args))
583 (princ "\n"))
584 (when (oref tmp dictionary)
585 (princ " Section Dictionaries:\n")
586 (srecode-dump (oref tmp dictionary) 4)
587 ;(princ "\n")
589 (when (and (slot-boundp tmp 'binding) (oref tmp binding))
590 (princ " Binding: ")
591 (prin1 (oref tmp binding))
592 (princ "\n"))
593 (princ " Compiled Codes:\n")
594 (srecode-dump-code-list (oref tmp code) " ")
595 (princ "\n\n")
598 (defun srecode-dump-code-list (code indent)
599 "Dump the CODE from a template code list to standard output.
600 Argument INDENT specifies the indentation level for the list."
601 (let ((i 1))
602 (while code
603 (princ indent)
604 (prin1 i)
605 (princ ") ")
606 (cond ((stringp (car code))
607 (prin1 (car code)))
608 ((srecode-template-inserter-child-p (car code))
609 (srecode-dump (car code) indent))
611 (princ "Unknown Code: ")
612 (prin1 (car code))))
613 (setq code (cdr code)
614 i (1+ i))
615 (when code
616 (princ "\n"))))
619 (defmethod srecode-dump ((ins srecode-template-inserter) indent)
620 "Dump the state of the SRecode template inserter INS."
621 (princ "INS: \"")
622 (princ (object-name-string ins))
623 (when (oref ins :secondname)
624 (princ "\" : \"")
625 (princ (oref ins :secondname)))
626 (princ "\" type \"")
627 (let* ((oc (symbol-name (object-class ins)))
628 (junk (string-match "srecode-template-inserter-" oc))
629 (on (if junk
630 (substring oc (match-end 0))
631 oc)))
632 (princ on))
633 (princ "\"")
636 (provide 'srecode/compile)
638 ;; Local variables:
639 ;; generated-autoload-file: "loaddefs.el"
640 ;; generated-autoload-feature: srecode/loaddefs
641 ;; generated-autoload-load-name: "srecode/compile"
642 ;; End:
644 ;; arch-tag: d993ffab-2704-4bb2-bd92-eafe803af3be
645 ;;; srecode/compile.el ends here