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/>.
25 ;; Compile a Semantic Recoder template file.
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.
31 ;; The output are a series of EIEIO objects which represent the
32 ;; templates in a way that could be inserted later.
38 (require 'srecode
/table
)
40 (declare-function srecode-template-inserter-newline-child-p
"srecode/insert"
42 (declare-function srecode-create-section-dictionary
"srecode/dictionary")
43 (declare-function srecode-dictionary-compound-variable
"srecode/dictionary")
49 ;; Templatets describe a patter of text that can be inserted into a
52 (defclass srecode-template
(eieio-named)
53 ((context :initarg
:context
56 "Context this template belongs to.")
59 "List of arguments that this template requires.")
62 "Compiled text from the template.")
63 (dictionary :initarg
:dictionary
64 :type
(or null srecode-dictionary
)
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
73 "Preferred keybinding for this template in `srecode-minor-mode-map'.")
74 (active :allocation
:class
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
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 tempalte
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."))
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
)
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."
119 (defmethod srecode-parse-input ((ins srecode-template-inserter
)
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."
127 (defmethod srecode-match-end ((ins srecode-template-inserter
) name
)
128 "For the template inserter INS, do I end a section called NAME?"
131 (defmethod srecode-inserter-apply-state ((ins srecode-template-inserter
) STATE
)
132 "For the template inserter INS, apply information from STATE."
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."
141 (when (and (slot-exists-p ins
'key
) (oref ins key
))
142 (princ (format "%c" (oref ins key
))))
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
)
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
))
170 (setq newprompts nil
)
172 (when (not (string= (car (car tmp
))
174 (setq newprompts
(cons (car tmp
)
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
)))
186 ;; Make whatever it is local.
188 (set-buffer (semantic-find-file-noselect fname
))
191 (srecode-compile-templates)
192 ;; Trash the buffer if we had to read it in.
194 (kill-buffer (current-buffer)))
198 (defun srecode-compile-templates ()
199 "Compile a semantic recode template file into a mode-local variable."
201 (require 'srecode
/insert
)
202 (message "Compiling template %s..."
203 (file-name-nondirectory (buffer-file-name)))
204 (let ((tags (semantic-fetch-tags))
208 (STATE (srecode-compile-state (file-name-nondirectory
209 (buffer-file-name))))
221 class
(semantic-tag-class tag
))
222 ;; What type of item is it?
224 ;; CONTEXT tags specify the context all future tags
227 (oset STATE context
(semantic-tag-name tag
))
230 ;; PROMPT tags specify prompts for dictionary ? inserters
231 ;; which appear in the following templates
233 (srecode-compile-add-prompt STATE tag
)
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
)
249 ((string= name
"escape_end")
250 (oset STATE escape_end firstvalue
)
252 ((string= name
"application")
253 (setq application
(read firstvalue
)))
254 ((string= name
"priority")
255 (setq priority
(read firstvalue
)))
257 ;; Assign this into some table of variables.
258 (setq vars
(cons (cons name firstvalue
) vars
))
260 ;; If it isn't a single string, then the value of the
261 ;; variable belongs to a compound dictionary value.
263 ;; Create a compound dictionary value from "value".
264 (require 'srecode
/dictionary
)
265 (let ((cv (srecode-dictionary-compound-variable
267 (setq vars
(cons (cons name cv
) vars
)))
271 ;; FUNCTION tags are really templates.
272 ((eq class
'function
)
273 (setq table
(cons (srecode-compile-one-template-tag tag STATE
)
278 (t (error "Unknown TAG class %s" class
))
281 (setq tags
(cdr tags
)))
283 ;; MSG - Before install since nreverse whacks our list.
284 (message "%d templates compiled for %s"
291 (error "You must specify a MODE for your templates"))
294 ;; Calculate 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)))
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))
306 (message "Compiling templates %s priority %d... done!"
307 (file-name-nondirectory (buffer-file-name))
311 (srecode-compile-template-table table mode priority application vars
)
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
)
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
))
330 ; (message "Compiled %s to %d codes with %d args and %d prompts."
331 ; (semantic-tag-name tag)
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
341 (list (srecode-compile-inserter "BLANK"
347 (list (srecode-compile-inserter "BLANK"
353 (setq args
(cdr args
)))
354 (srecode-template (semantic-tag-name tag
)
356 :args
(nreverse addargs
)
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
)))
368 (require 'srecode
/insert
)
369 (srecode-template-inserter-newline-child-p (car comp
))))
371 (defun srecode-compile-split-code (tag str STATE
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
379 Optional argument END-NAME specifies the name of a token upon which
381 If END-NAME is specified, and the input string"
385 (regex (concat "\n\\|" (regexp-quote (oref STATE escape_start
))))
386 (regexend (regexp-quote (oref STATE escape_end
)))
388 (while (and what
(not end-token
))
390 ((string-match regex what
)
391 (let* ((prefix (substring what
0 (match-beginning 0)))
392 (match (substring what
395 (namestart (match-end 0))
396 (junk (string-match regexend what namestart
))
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.
404 (srecode-compile-inserter
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
413 :hard
(srecode-compile-do-hard-newline-p comp
)
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
))
421 ;; Regular inserter thingy.
424 (error "Could not find end escape for %s"
425 (semantic-tag-name tag
)))
428 (error "No matching escape end for %s"
429 (semantic-tag-name tag
)))
431 (error "Stray end escape for %s"
432 (semantic-tag-name tag
)))
434 ;; Add string to compiled output
435 (setq name
(substring what namestart end
)
438 (setq what
(substring what tail
))
441 (srecode-compile-parse-inserter name STATE
))
443 ;; If this is an end inserter, then assign into
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
451 (setq what
(srecode-parse-input new-inserter tag what
457 (error "Unmatched section end %s" end-name
))
458 (setq comp
(cons what comp
)
460 (cons what
(nreverse comp
))))
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))
468 (if (and (or (< key ?A
) (> key ?Z
))
469 (or (< key ?a
) (> key ?z
)) )
470 (setq name
(substring txt
1))
473 (let* ((junk (string-match ":" name
))
475 (substring name
0 (match-beginning 0))
478 (substring name
(match-end 0))
480 (new-inserter (srecode-compile-inserter
482 :secondname secondname
484 ;; Return the new inserter
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)
496 (apply 'srecode-template-inserter-variable name props
)
497 (let ((classes (class-children srecode-template-inserter
))
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
)
510 (setq classes
(cdr classes
)))
511 (if (not new
) (error "SRECODE: Unknown macro code %S" key
))
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))
529 (let* ((objname (oref (car lp
) :object-name
))
530 (context (oref (car lp
) :context
))
531 (globalname (concat context
":" objname
))
534 ;; Place this template object into the global name hash.
535 (puthash globalname
(car lp
) namehash
)
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.
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
)
549 (let* ((table (srecode-mode-table-new mode
(buffer-file-name)
550 :templates
(nreverse templates
)
552 :contexthash contexthash
556 :application application
))
557 (tmpl (oref table templates
)))
558 ;; Loop over all the templates, and xref.
560 (oset (car tmpl
) :table table
)
561 (setq tmpl
(cdr tmpl
))))
568 ;; Dump out information about the current srecoder compiled templates.
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
))
578 (when (oref tmp args
)
579 (princ " Arguments: ")
580 (prin1 (oref tmp args
))
582 (when (oref tmp dictionary
)
583 (princ " Section Dictionaries:\n")
584 (srecode-dump (oref tmp dictionary
) 4)
587 (when (and (slot-boundp tmp
'binding
) (oref tmp binding
))
589 (prin1 (oref tmp binding
))
591 (princ " Compiled Codes:\n")
592 (srecode-dump-code-list (oref tmp code
) " ")
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."
604 (cond ((stringp (car code
))
606 ((srecode-template-inserter-child-p (car code
))
607 (srecode-dump (car code
) indent
))
609 (princ "Unknown Code: ")
611 (setq code
(cdr code
)
617 (defmethod srecode-dump ((ins srecode-template-inserter
) indent
)
618 "Dump the state of the SRecode template inserter INS."
620 (princ (object-name-string ins
))
621 (when (oref ins
:secondname
)
623 (princ (oref ins
:secondname
)))
625 (let* ((oc (symbol-name (object-class ins
)))
626 (junk (string-match "srecode-template-inserter-" oc
))
628 (substring oc
(match-end 0))
634 (provide 'srecode
/compile
)
637 ;; generated-autoload-file: "loaddefs.el"
638 ;; generated-autoload-feature: srecode/loaddefs
639 ;; generated-autoload-load-name: "srecode/compile"
642 ;;; srecode/compile.el ends here