1 ;;; srecode/compile --- Compilation of srecode template files.
3 ;; Copyright (C) 2005, 2007-2013 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
))
38 (require 'srecode
/table
)
39 (require 'srecode
/dictionary
)
41 (declare-function srecode-template-inserter-newline-child-p
"srecode/insert"
48 ;; Templates describe a pattern of text that can be inserted into a
51 (defclass srecode-template
(eieio-named)
52 ((context :initarg
:context
55 "Context this template belongs to.")
58 "List of arguments that this template requires.")
61 "Compiled text from the template.")
62 (dictionary :initarg
:dictionary
63 :type
(or null srecode-dictionary
)
65 "List of section dictionaries.
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 incoming dictionaries values.")
70 (binding :initarg
:binding
72 "Preferred keybinding for this template in `srecode-minor-mode-map'.")
73 (active :allocation
:class
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
81 "The table this template lives in.")
83 "Class defines storage for semantic recoder templates.")
85 (defun srecode-flush-active-templates ()
86 "Flush the active template storage.
87 Useful if something goes wrong in SRecode, and the active template
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."))
99 ;; Each inserter object manages a different thing that
100 ;; might be inserted into a template output stream.
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.
106 ;; See srecode/insert.el for the specialized classes.
108 (defclass srecode-template-inserter
(eieio-named)
109 ((secondname :initarg
:secondname
110 :type
(or null string
)
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."
118 (defmethod srecode-parse-input ((ins srecode-template-inserter
)
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."
126 (defmethod srecode-match-end ((ins srecode-template-inserter
) name
)
127 "For the template inserter INS, do I end a section called NAME?"
130 (defmethod srecode-inserter-apply-state ((ins srecode-template-inserter
) STATE
)
131 "For the template inserter INS, apply information from STATE."
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."
140 (when (and (slot-exists-p ins
'key
) (oref ins key
))
141 (princ (format "%c" (oref ins key
))))
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.")
159 "Current state of the compile.")
161 (defmethod srecode-compile-add-prompt ((state srecode-compile-state
)
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
))
169 (setq newprompts nil
)
171 (when (not (string= (car (car tmp
))
173 (setq newprompts
(cons (car tmp
)
175 (setq tmp
(cdr tmp
)))))
176 (setq prompts
(cons prompttag newprompts
)))
179 ;;; TEMPLATE COMPILER
181 (defun srecode-compile-file (fname)
182 "Compile the templates from the file FNAME."
183 (let ((peb (get-file-buffer fname
)))
185 ;; Make whatever it is local.
187 (set-buffer (semantic-find-file-noselect fname
))
190 (unless (semantic-active-p)
191 (semantic-new-buffer-fcn))
192 (srecode-compile-templates)
193 ;; Trash the buffer if we had to read it in.
195 (kill-buffer (current-buffer)))
199 (defun srecode-compile-templates ()
200 "Compile a semantic recode template file into a mode-local variable."
202 (unless (semantic-active-p)
203 (error "You have to activate semantic-mode to compile SRecode templates"))
204 (require 'srecode
/insert
)
205 (when (called-interactively-p 'interactive
)
206 (message "Compiling template %s..."
207 (file-name-nondirectory (buffer-file-name))))
208 (let ((tags (semantic-fetch-tags))
212 (STATE (srecode-compile-state (file-name-nondirectory
213 (buffer-file-name))))
227 class
(semantic-tag-class tag
))
228 ;; What type of item is it?
230 ;; CONTEXT tags specify the context all future tags
233 (oset STATE context
(semantic-tag-name tag
))
236 ;; PROMPT tags specify prompts for dictionary ? inserters
237 ;; which appear in the following templates
239 (srecode-compile-add-prompt STATE tag
)
242 ;; VARIABLE tags can specify operational control
243 ((eq class
'variable
)
244 (let* ((name (semantic-tag-name tag
))
245 (value (semantic-tag-variable-default tag
))
246 (firstvalue (car value
)))
247 ;; If it is a single string, and one value, then
248 ;; look to see if it is one of our special variables.
249 (if (and (= (length value
) 1) (stringp firstvalue
))
250 (cond ((string= name
"mode")
251 (setq mode
(intern firstvalue
)))
252 ((string= name
"escape_start")
253 (oset STATE escape_start firstvalue
)
255 ((string= name
"escape_end")
256 (oset STATE escape_end firstvalue
)
258 ((string= name
"application")
259 (setq application
(read firstvalue
)))
260 ((string= name
"framework")
261 (setq framework
(read firstvalue
)))
262 ((string= name
"priority")
263 (setq priority
(read firstvalue
)))
264 ((string= name
"project")
265 (setq project firstvalue
))
267 ;; Assign this into some table of variables.
268 (setq vars
(cons (cons name firstvalue
) vars
))
270 ;; If it isn't a single string, then the value of the
271 ;; variable belongs to a compound dictionary value.
273 ;; Create a compound dictionary value from "value".
274 (require 'srecode
/dictionary
)
275 (let ((cv (srecode-dictionary-compound-variable
277 (setq vars
(cons (cons name cv
) vars
)))
281 ;; FUNCTION tags are really templates.
282 ((eq class
'function
)
283 (setq table
(cons (srecode-compile-one-template-tag tag STATE
)
288 (t (error "Unknown TAG class %s" class
))
291 (setq tags
(cdr tags
)))
293 ;; MSG - Before install since nreverse whacks our list.
294 (when (called-interactively-p 'interactive
)
295 (message "%d templates compiled for %s"
296 (length table
) mode
))
302 (error "You must specify a MODE for your templates"))
305 ;; Calculate priority
308 (let ((d (expand-file-name (file-name-directory (buffer-file-name))))
309 (sd (expand-file-name (file-name-directory (locate-library "srecode"))))
310 (defaultdelta (if (eq mode
'default
) 0 10)))
311 ;; @TODO : WHEN INTEGRATING INTO EMACS
312 ;; The location of Emacs default templates needs to be specified
313 ;; here to also have a lower priority.
314 (if (string-match (concat "^" sd
) d
)
315 (setq priority
(+ 30 defaultdelta
))
316 ;; If the user created template is for a project, then
317 ;; don't add as much as if it is unique to just some user.
318 (if (stringp project
)
319 (setq priority
(+ 50 defaultdelta
))
320 (setq priority
(+ 80 defaultdelta
))))
321 (when (called-interactively-p 'interactive
)
322 (message "Templates %s has estimated priority of %d"
323 (file-name-nondirectory (buffer-file-name))
325 (when (called-interactively-p 'interactive
)
326 (message "Compiling templates %s priority %d... done!"
327 (file-name-nondirectory (buffer-file-name))
331 (srecode-compile-template-table table mode priority application framework project vars
)
335 (defun srecode-compile-one-template-tag (tag state
)
336 "Compile a template tag TAG into a srecode template object.
337 STATE is the current compile state as an object of class
338 `srecode-compile-state'."
339 (let* ((context (oref state context
))
340 (code (cdr (srecode-compile-split-code
341 tag
(semantic-tag-get-attribute tag
:code
)
343 (args (semantic-tag-function-arguments tag
))
344 (binding (semantic-tag-get-attribute tag
:binding
))
345 (dict-tags (semantic-tag-get-attribute tag
:dictionaries
))
346 (root-dict (when dict-tags
347 (srecode-create-dictionaries-from-tags
350 ;; Examine arguments.
352 (let ((symbol (intern arg
)))
353 (push symbol addargs
)
355 ;; If we have a wrap, then put wrap inserters on both ends of
357 (when (eq symbol
:blank
)
359 (list (srecode-compile-inserter
366 (list (srecode-compile-inserter
373 ;; Construct and return the template object.
374 (srecode-template (semantic-tag-name tag
)
376 :args
(nreverse addargs
)
377 :dictionary root-dict
382 (defun srecode-compile-do-hard-newline-p (comp)
383 "Examine COMP to decide if the upcoming newline should be hard.
384 It is hard if the previous inserter is a newline object."
385 (while (and comp
(stringp (car comp
)))
386 (setq comp
(cdr comp
)))
388 (progn (require 'srecode
/insert
)
389 (srecode-template-inserter-newline-child-p (car comp
)))))
391 (defun srecode-compile-split-code (tag str STATE
393 "Split the code for TAG into something templatable.
394 STR is the string of code from TAG to split.
395 STATE is the current compile state.
396 ESCAPE_START and ESCAPE_END are regexps that indicate the beginning
397 escape character, and end escape character pattern for expandable
399 Optional argument END-NAME specifies the name of a token upon which
401 If END-NAME is specified, and the input string"
405 (regex (concat "\n\\|" (regexp-quote (oref STATE escape_start
))))
406 (regexend (regexp-quote (oref STATE escape_end
)))
408 (while (and what
(not end-token
))
410 ((string-match regex what
)
411 (let* ((prefix (substring what
0 (match-beginning 0)))
412 (match (substring what
415 (namestart (match-end 0))
416 (junk (string-match regexend what namestart
))
418 ;; Add string to compiled output
419 (when (> (length prefix
) 0)
420 (setq comp
(cons prefix comp
)))
421 (if (string= match
"\n")
422 ;; Do newline thingy.
424 (srecode-compile-inserter
429 ;; This newline is "hard" meaning ALWAYS do it
430 ;; if the previous entry is also a newline.
431 ;; Without it, user entered blank lines will be
433 :hard
(srecode-compile-do-hard-newline-p comp
)
436 (setq what
(substring what namestart
))
437 (when (> (length what
) 0)
438 ;; make the new inserter, but only if we aren't last.
439 (setq comp
(cons new-inserter comp
))
441 ;; Regular inserter thingy.
444 (error "Could not find end escape for %s"
445 (semantic-tag-name tag
)))
448 (error "No matching escape end for %s"
449 (semantic-tag-name tag
)))
451 (error "Stray end escape for %s"
452 (semantic-tag-name tag
)))
454 ;; Add string to compiled output
455 (setq name
(substring what namestart end
)
458 (setq what
(substring what tail
))
461 (srecode-compile-parse-inserter name STATE
))
463 ;; If this is an end inserter, then assign into
465 (if (srecode-match-end new-inserter end-name
)
466 (setq end-token new-inserter
))
467 ;; Add the inserter to our compilation stream.
468 (setq comp
(cons new-inserter comp
))
469 ;; Allow the inserter an opportunity to modify
471 (setq what
(srecode-parse-input new-inserter tag what
477 (error "Unmatched section end %s" end-name
))
478 (setq comp
(cons what comp
)
480 (cons what
(nreverse comp
))))
482 (defun srecode-compile-parse-inserter (txt STATE
)
483 "Parse the inserter TXT with the current STATE.
484 Return an inserter object."
485 (let ((key (aref txt
0))
488 (if (and (or (< key ?A
) (> key ?Z
))
489 (or (< key ?a
) (> key ?z
)) )
490 (setq name
(substring txt
1))
493 (let* ((junk (string-match ":" name
))
495 (substring name
0 (match-beginning 0))
498 (substring name
(match-end 0))
500 (new-inserter (srecode-compile-inserter
502 :secondname secondname
504 ;; Return the new inserter
507 (defun srecode-compile-inserter (name key STATE
&rest props
)
508 "Create an srecode inserter object for some macro NAME.
509 KEY indicates a single character key representing a type
510 of inserter to create.
511 STATE is the current compile state.
512 PROPS are additional properties that might need to be passed
513 to the inserter constructor."
514 ;;(message "Compile: %s %S" name props)
516 (apply 'srecode-template-inserter-variable name props
)
517 (let ((classes (eieio-class-children srecode-template-inserter
))
519 ;; Loop over the various subclasses and
520 ;; create the correct inserter.
521 (while (and (not new
) classes
)
522 (setq classes
(append classes
(eieio-class-children (car classes
))))
523 ;; Do we have a match?
524 (when (and (not (class-abstract-p (car classes
)))
525 (equal (oref (car classes
) key
) key
))
526 ;; Create the new class, and apply state.
527 (setq new
(apply (car classes
) name props
))
528 (srecode-inserter-apply-state new STATE
)
530 (setq classes
(cdr classes
)))
531 (if (not new
) (error "SRECODE: Unknown macro code %S" key
))
534 (defun srecode-compile-template-table (templates mode priority application framework project vars
)
535 "Compile a list of TEMPLATES into an semantic recode table.
536 The table being compiled is for MODE, or the string \"default\".
537 PRIORITY is a numerical value that indicates this tables location
538 in an ordered search.
539 APPLICATION is the name of the application these templates belong to.
540 FRAMEWORK is the name of the framework these templates belong to.
541 PROJECT is a directory name which these templates scope to.
542 A list of defined variables VARS provides a variable table."
543 (let ((namehash (make-hash-table :test
'equal
544 :size
(length templates
)))
545 (contexthash (make-hash-table :test
'equal
:size
10))
551 (let* ((objname (oref (car lp
) :object-name
))
552 (context (oref (car lp
) :context
))
553 (globalname (concat context
":" objname
))
556 ;; Place this template object into the global name hash.
557 (puthash globalname
(car lp
) namehash
)
559 ;; Place this template into the specific context name hash.
560 (let ((hs (gethash context contexthash
)))
561 ;; Make a new context if none was available.
563 (setq hs
(make-hash-table :test
'equal
:size
20))
564 (puthash context hs contexthash
))
565 ;; Put into that context's hash.
566 (puthash objname
(car lp
) hs
)
571 (when (stringp project
)
572 (setq project
(expand-file-name project
)))
574 (let* ((table (srecode-mode-table-new mode
(buffer-file-name)
575 :templates
(nreverse templates
)
577 :contexthash contexthash
581 :application application
584 (tmpl (oref table templates
)))
585 ;; Loop over all the templates, and xref.
587 (oset (car tmpl
) :table table
)
588 (setq tmpl
(cdr tmpl
))))
595 ;; Dump out information about the current srecoder compiled templates.
598 (defmethod srecode-dump ((tmp srecode-template
))
599 "Dump the contents of the SRecode template tmp."
600 (princ "== Template \"")
601 (princ (eieio-object-name-string tmp
))
602 (princ "\" in context ")
603 (princ (oref tmp context
))
605 (when (oref tmp args
)
606 (princ " Arguments: ")
607 (prin1 (oref tmp args
))
609 (when (oref tmp dictionary
)
610 (princ " Section Dictionaries:\n")
611 (srecode-dump (oref tmp dictionary
) 4)
614 (when (and (slot-boundp tmp
'binding
) (oref tmp binding
))
616 (prin1 (oref tmp binding
))
618 (princ " Compiled Codes:\n")
619 (srecode-dump-code-list (oref tmp code
) " ")
623 (defun srecode-dump-code-list (code indent
)
624 "Dump the CODE from a template code list to standard output.
625 Argument INDENT specifies the indentation level for the list."
631 (cond ((stringp (car code
))
633 ((srecode-template-inserter-child-p (car code
))
634 (srecode-dump (car code
) indent
))
636 (princ "Unknown Code: ")
638 (setq code
(cdr code
)
644 (defmethod srecode-dump ((ins srecode-template-inserter
) indent
)
645 "Dump the state of the SRecode template inserter INS."
647 (princ (eieio-object-name-string ins
))
648 (when (oref ins
:secondname
)
650 (princ (oref ins
:secondname
)))
652 (let* ((oc (symbol-name (eieio-object-class ins
)))
653 (junk (string-match "srecode-template-inserter-" oc
))
655 (substring oc
(match-end 0))
661 (provide 'srecode
/compile
)
664 ;; generated-autoload-file: "loaddefs.el"
665 ;; generated-autoload-load-name: "srecode/compile"
668 ;;; srecode/compile.el ends here