1 ;;; srecode/compile --- Compilation of srecode template files.
3 ;; Copyright (C) 2005, 2007, 2008, 2009, 2010 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/>.
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 (eval-when-compile (require 'cl
))
39 (require 'srecode
/table
)
41 (declare-function srecode-template-inserter-newline-child-p
"srecode/insert"
43 (declare-function srecode-create-section-dictionary
"srecode/dictionary")
44 (declare-function srecode-dictionary-compound-variable
"srecode/dictionary")
50 ;; Templatets describe a patter of text that can be inserted into a
53 (defclass srecode-template
(eieio-named)
54 ((context :initarg
:context
57 "Context this template belongs to.")
60 "List of arguments that this template requires.")
63 "Compiled text from the template.")
64 (dictionary :initarg
:dictionary
65 :type
(or null srecode-dictionary
)
67 "List of section dictinaries.
68 The compiled template can contain lists of section dictionaries,
69 or values that are expected to be passed down into different
70 section macros. The template section dictionaries are merged in with
71 any incomming dictionaries values.")
72 (binding :initarg
:binding
74 "Preferred keybinding for this template in `srecode-minor-mode-map'.")
75 (active :allocation
:class
78 "During template insertion, this is the stack of active templates.
79 The top-most template is the 'active' template. Use the accessor methods
80 for push, pop, and peek for the active template.")
81 (table :initarg
:table
83 "The table this template lives in.")
85 "Class defines storage for semantic recoder templates.")
87 (defun srecode-flush-active-templates ()
88 "Flush the active template storage.
89 Useful if something goes wrong in SRecode, and the active template
92 (if (oref srecode-template active
)
93 (when (y-or-n-p (format "%d active templates. Flush? "
94 (length (oref srecode-template active
))))
95 (oset-default srecode-template active nil
))
96 (message "No active templates to flush."))
101 ;; Each inserter object manages a different thing that
102 ;; might be inserted into a template output stream.
104 ;; The 'srecode-insert-method' on each inserter does the actual
105 ;; work, and the smaller, simple inserter object is saved in
106 ;; the compiled templates.
108 ;; See srecode-insert.el for the specialized classes.
110 (defclass srecode-template-inserter
(eieio-named)
111 ((secondname :initarg
:secondname
112 :type
(or null string
)
114 "If there is a colon in the inserter's name, it represents
115 additional static argument data."))
116 "This represents an item to be inserted via a template macro.
117 Plain text strings are not handled via this baseclass."
120 (defmethod srecode-parse-input ((ins srecode-template-inserter
)
122 "For the template inserter INS, parse INPUT.
123 Shorten input only by the amount needed.
124 Return the remains of INPUT.
125 STATE is the current compilation state."
128 (defmethod srecode-match-end ((ins srecode-template-inserter
) name
)
129 "For the template inserter INS, do I end a section called NAME?"
132 (defmethod srecode-inserter-apply-state ((ins srecode-template-inserter
) STATE
)
133 "For the template inserter INS, apply information from STATE."
136 (defmethod srecode-inserter-prin-example :STATIC
((ins srecode-template-inserter
)
137 escape-start escape-end
)
138 "Insert an example using inserter INS.
139 Arguments ESCAPE-START and ESCAPE-END are the current escape sequences in use."
142 (when (and (slot-exists-p ins
'key
) (oref ins key
))
143 (princ (format "%c" (oref ins key
))))
151 (defclass srecode-compile-state
()
152 ((context :initform
"declaration"
153 :documentation
"The active context.")
154 (prompts :initform nil
155 :documentation
"The active prompts.")
156 (escape_start :initform
"{{"
157 :documentation
"The starting escape sequence.")
158 (escape_end :initform
"}}"
159 :documentation
"The ending escape sequence.")
161 "Current state of the compile.")
163 (defmethod srecode-compile-add-prompt ((state srecode-compile-state
)
165 "Add PROMPTTAG to the current list of prompts."
166 (with-slots (prompts) state
167 (let ((match (assoc (semantic-tag-name prompttag
) prompts
))
168 (newprompts prompts
))
171 (setq newprompts nil
)
173 (when (not (string= (car (car tmp
))
175 (setq newprompts
(cons (car tmp
)
177 (setq tmp
(cdr tmp
)))))
178 (setq prompts
(cons prompttag newprompts
)))
181 ;;; TEMPLATE COMPILER
183 (defun srecode-compile-file (fname)
184 "Compile the templates from the file FNAME."
185 (let ((peb (get-file-buffer fname
)))
187 ;; Make whatever it is local.
189 (set-buffer (semantic-find-file-noselect fname
))
192 (unless (semantic-active-p)
193 (semantic-new-buffer-fcn))
194 (srecode-compile-templates)
195 ;; Trash the buffer if we had to read it in.
197 (kill-buffer (current-buffer)))
201 (defun srecode-compile-templates ()
202 "Compile a semantic recode template file into a mode-local variable."
204 (require 'srecode
/insert
)
205 (message "Compiling template %s..."
206 (file-name-nondirectory (buffer-file-name)))
207 (let ((tags (semantic-fetch-tags))
211 (STATE (srecode-compile-state (file-name-nondirectory
212 (buffer-file-name))))
224 class
(semantic-tag-class tag
))
225 ;; What type of item is it?
227 ;; CONTEXT tags specify the context all future tags
230 (oset STATE context
(semantic-tag-name tag
))
233 ;; PROMPT tags specify prompts for dictionary ? inserters
234 ;; which appear in the following templates
236 (srecode-compile-add-prompt STATE tag
)
239 ;; VARIABLE tags can specify operational control
240 ((eq class
'variable
)
241 (let* ((name (semantic-tag-name tag
))
242 (value (semantic-tag-variable-default tag
))
243 (firstvalue (car value
)))
244 ;; If it is a single string, and one value, then
245 ;; look to see if it is one of our special variables.
246 (if (and (= (length value
) 1) (stringp firstvalue
))
247 (cond ((string= name
"mode")
248 (setq mode
(intern firstvalue
)))
249 ((string= name
"escape_start")
250 (oset STATE escape_start firstvalue
)
252 ((string= name
"escape_end")
253 (oset STATE escape_end firstvalue
)
255 ((string= name
"application")
256 (setq application
(read firstvalue
)))
257 ((string= name
"priority")
258 (setq priority
(read firstvalue
)))
260 ;; Assign this into some table of variables.
261 (setq vars
(cons (cons name firstvalue
) vars
))
263 ;; If it isn't a single string, then the value of the
264 ;; variable belongs to a compound dictionary value.
266 ;; Create a compound dictionary value from "value".
267 (require 'srecode
/dictionary
)
268 (let ((cv (srecode-dictionary-compound-variable
270 (setq vars
(cons (cons name cv
) vars
)))
274 ;; FUNCTION tags are really templates.
275 ((eq class
'function
)
276 (setq table
(cons (srecode-compile-one-template-tag tag STATE
)
281 (t (error "Unknown TAG class %s" class
))
284 (setq tags
(cdr tags
)))
286 ;; MSG - Before install since nreverse whacks our list.
287 (message "%d templates compiled for %s"
294 (error "You must specify a MODE for your templates"))
297 ;; Calculate priority
300 (let ((d (file-name-directory (buffer-file-name)))
301 (sd (file-name-directory (locate-library "srecode")))
302 (defaultdelta (if (eq mode
'default
) 20 0)))
304 (setq priority
(+ 80 defaultdelta
))
305 (setq priority
(+ 30 defaultdelta
)))
306 (message "Templates %s has estimated priority of %d"
307 (file-name-nondirectory (buffer-file-name))
309 (message "Compiling templates %s priority %d... done!"
310 (file-name-nondirectory (buffer-file-name))
314 (srecode-compile-template-table table mode priority application vars
)
318 (defun srecode-compile-one-template-tag (tag STATE
)
319 "Compile a template tag TAG into an srecode template class.
320 STATE is the current compile state as an object `srecode-compile-state'."
321 (require 'srecode
/dictionary
)
322 (let* ((context (oref STATE context
))
323 (codeout (srecode-compile-split-code
324 tag
(semantic-tag-get-attribute tag
:code
)
327 (args (semantic-tag-function-arguments tag
))
328 (binding (semantic-tag-get-attribute tag
:binding
))
329 (rawdicts (semantic-tag-get-attribute tag
:dictionaries
))
330 (sdicts (srecode-create-section-dictionary rawdicts STATE
))
333 ; (message "Compiled %s to %d codes with %d args and %d prompts."
334 ; (semantic-tag-name tag)
339 (setq addargs
(cons (intern (car args
)) addargs
))
340 (when (eq (car addargs
) :blank
)
341 ;; If we have a wrap, then put wrap inserters on both
344 (list (srecode-compile-inserter "BLANK"
350 (list (srecode-compile-inserter "BLANK"
356 (setq args
(cdr args
)))
357 (srecode-template (semantic-tag-name tag
)
359 :args
(nreverse addargs
)
365 (defun srecode-compile-do-hard-newline-p (comp)
366 "Examine COMP to decide if the upcoming newline should be hard.
367 It is hard if the previous inserter is a newline object."
368 (while (and comp
(stringp (car comp
)))
369 (setq comp
(cdr comp
)))
371 (require 'srecode
/insert
)
372 (srecode-template-inserter-newline-child-p (car comp
))))
374 (defun srecode-compile-split-code (tag str STATE
376 "Split the code for TAG into something templatable.
377 STR is the string of code from TAG to split.
378 STATE is the current compile state.
379 ESCAPE_START and ESCAPE_END are regexps that indicate the beginning
380 escape character, and end escape character pattern for expandable
382 Optional argument END-NAME specifies the name of a token upon which
384 If END-NAME is specified, and the input string"
388 (regex (concat "\n\\|" (regexp-quote (oref STATE escape_start
))))
389 (regexend (regexp-quote (oref STATE escape_end
)))
391 (while (and what
(not end-token
))
393 ((string-match regex what
)
394 (let* ((prefix (substring what
0 (match-beginning 0)))
395 (match (substring what
398 (namestart (match-end 0))
399 (junk (string-match regexend what namestart
))
401 ;; Add string to compiled output
402 (when (> (length prefix
) 0)
403 (setq comp
(cons prefix comp
)))
404 (if (string= match
"\n")
405 ;; Do newline thingy.
407 (srecode-compile-inserter
412 ;; This newline is "hard" meaning ALWAYS do it
413 ;; if the previous entry is also a newline.
414 ;; Without it, user entered blank lines will be
416 :hard
(srecode-compile-do-hard-newline-p comp
)
419 (setq what
(substring what namestart
))
420 (when (> (length what
) 0)
421 ;; make the new inserter, but only if we aren't last.
422 (setq comp
(cons new-inserter comp
))
424 ;; Regular inserter thingy.
427 (error "Could not find end escape for %s"
428 (semantic-tag-name tag
)))
431 (error "No matching escape end for %s"
432 (semantic-tag-name tag
)))
434 (error "Stray end escape for %s"
435 (semantic-tag-name tag
)))
437 ;; Add string to compiled output
438 (setq name
(substring what namestart end
)
441 (setq what
(substring what tail
))
444 (srecode-compile-parse-inserter name STATE
))
446 ;; If this is an end inserter, then assign into
448 (if (srecode-match-end new-inserter end-name
)
449 (setq end-token new-inserter
))
450 ;; Add the inserter to our compilation stream.
451 (setq comp
(cons new-inserter comp
))
452 ;; Allow the inserter an opportunity to modify
454 (setq what
(srecode-parse-input new-inserter tag what
460 (error "Unmatched section end %s" end-name
))
461 (setq comp
(cons what comp
)
463 (cons what
(nreverse comp
))))
465 (defun srecode-compile-parse-inserter (txt STATE
)
466 "Parse the inserter TXT with the current STATE.
467 Return an inserter object."
468 (let ((key (aref txt
0))
471 (if (and (or (< key ?A
) (> key ?Z
))
472 (or (< key ?a
) (> key ?z
)) )
473 (setq name
(substring txt
1))
476 (let* ((junk (string-match ":" name
))
478 (substring name
0 (match-beginning 0))
481 (substring name
(match-end 0))
483 (new-inserter (srecode-compile-inserter
485 :secondname secondname
487 ;; Return the new inserter
490 (defun srecode-compile-inserter (name key STATE
&rest props
)
491 "Create an srecode inserter object for some macro NAME.
492 KEY indicates a single character key representing a type
493 of inserter to create.
494 STATE is the current compile state.
495 PROPS are additional properties that might need to be passed
496 to the inserter constructor."
497 ;;(message "Compile: %s %S" name props)
499 (apply 'srecode-template-inserter-variable name props
)
500 (let ((classes (class-children srecode-template-inserter
))
502 ;; Loop over the various subclasses and
503 ;; create the correct inserter.
504 (while (and (not new
) classes
)
505 (setq classes
(append classes
(class-children (car classes
))))
506 ;; Do we have a match?
507 (when (and (not (class-abstract-p (car classes
)))
508 (equal (oref (car classes
) key
) key
))
509 ;; Create the new class, and apply state.
510 (setq new
(apply (car classes
) name props
))
511 (srecode-inserter-apply-state new STATE
)
513 (setq classes
(cdr classes
)))
514 (if (not new
) (error "SRECODE: Unknown macro code %S" key
))
517 (defun srecode-compile-template-table (templates mode priority application vars
)
518 "Compile a list of TEMPLATES into an semantic recode table.
519 The table being compiled is for MODE, or the string \"default\".
520 PRIORITY is a numerical value that indicates this tables location
521 in an ordered search.
522 APPLICATION is the name of the application these templates belong to.
523 A list of defined variables VARS provides a variable table."
524 (let ((namehash (make-hash-table :test
'equal
525 :size
(length templates
)))
526 (contexthash (make-hash-table :test
'equal
:size
10))
532 (let* ((objname (oref (car lp
) :object-name
))
533 (context (oref (car lp
) :context
))
534 (globalname (concat context
":" objname
))
537 ;; Place this template object into the global name hash.
538 (puthash globalname
(car lp
) namehash
)
540 ;; Place this template into the specific context name hash.
541 (let ((hs (gethash context contexthash
)))
542 ;; Make a new context if none was available.
544 (setq hs
(make-hash-table :test
'equal
:size
20))
545 (puthash context hs contexthash
))
546 ;; Put into that contenxt's hash.
547 (puthash objname
(car lp
) hs
)
552 (let* ((table (srecode-mode-table-new mode
(buffer-file-name)
553 :templates
(nreverse templates
)
555 :contexthash contexthash
559 :application application
))
560 (tmpl (oref table templates
)))
561 ;; Loop over all the templates, and xref.
563 (oset (car tmpl
) :table table
)
564 (setq tmpl
(cdr tmpl
))))
571 ;; Dump out information about the current srecoder compiled templates.
574 (defmethod srecode-dump ((tmp srecode-template
))
575 "Dump the contents of the SRecode template tmp."
576 (princ "== Template \"")
577 (princ (object-name-string tmp
))
578 (princ "\" in context ")
579 (princ (oref tmp context
))
581 (when (oref tmp args
)
582 (princ " Arguments: ")
583 (prin1 (oref tmp args
))
585 (when (oref tmp dictionary
)
586 (princ " Section Dictionaries:\n")
587 (srecode-dump (oref tmp dictionary
) 4)
590 (when (and (slot-boundp tmp
'binding
) (oref tmp binding
))
592 (prin1 (oref tmp binding
))
594 (princ " Compiled Codes:\n")
595 (srecode-dump-code-list (oref tmp code
) " ")
599 (defun srecode-dump-code-list (code indent
)
600 "Dump the CODE from a template code list to standard output.
601 Argument INDENT specifies the indentation level for the list."
607 (cond ((stringp (car code
))
609 ((srecode-template-inserter-child-p (car code
))
610 (srecode-dump (car code
) indent
))
612 (princ "Unknown Code: ")
614 (setq code
(cdr code
)
620 (defmethod srecode-dump ((ins srecode-template-inserter
) indent
)
621 "Dump the state of the SRecode template inserter INS."
623 (princ (object-name-string ins
))
624 (when (oref ins
:secondname
)
626 (princ (oref ins
:secondname
)))
628 (let* ((oc (symbol-name (object-class ins
)))
629 (junk (string-match "srecode-template-inserter-" oc
))
631 (substring oc
(match-end 0))
637 (provide 'srecode
/compile
)
640 ;; generated-autoload-file: "loaddefs.el"
641 ;; generated-autoload-load-name: "srecode/compile"
644 ;; arch-tag: d993ffab-2704-4bb2-bd92-eafe803af3be
645 ;;; srecode/compile.el ends here