Support GnuTLS v3 and set up its audit logging properly.
[emacs.git] / lisp / cedet / srecode / compile.el
blob542fd49f8e57b2c02fa1c1dc3717220a6a603cb2
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/>.
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 (eval-when-compile (require 'cl))
35 (require 'semantic)
36 (require 'eieio)
37 (require 'eieio-base)
38 (require 'srecode/table)
39 (require 'srecode/dictionary)
41 (declare-function srecode-template-inserter-newline-child-p "srecode/insert"
42 t t)
44 ;;; Code:
46 ;;; Template Class
48 ;; Templates describe a pattern of text that can be inserted into a
49 ;; buffer.
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 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
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.")
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
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."))
97 ;;; Inserters
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)
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)
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)
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)
130 (defmethod srecode-inserter-apply-state ((ins srecode-template-inserter) STATE)
131 "For the template inserter INS, apply information from STATE."
132 nil)
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)
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.")
159 "Current state of the compile.")
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)))
179 ;;; TEMPLATE COMPILER
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 (unless (semantic-active-p)
191 (semantic-new-buffer-fcn))
192 (srecode-compile-templates)
193 ;; Trash the buffer if we had to read it in.
194 (if (not peb)
195 (kill-buffer (current-buffer)))
198 ;;;###autoload
199 (defun srecode-compile-templates ()
200 "Compile a semantic recode template file into a mode-local variable."
201 (interactive)
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))
209 (tag nil)
210 (class nil)
211 (table nil)
212 (STATE (srecode-compile-state (file-name-nondirectory
213 (buffer-file-name))))
214 (mode nil)
215 (application nil)
216 (framework nil)
217 (priority nil)
218 (project nil)
219 (vars nil)
223 ;; COMPILE
225 (while tags
226 (setq tag (car tags)
227 class (semantic-tag-class tag))
228 ;; What type of item is it?
229 (cond
230 ;; CONTEXT tags specify the context all future tags
231 ;; belong to.
232 ((eq class 'context)
233 (oset STATE context (semantic-tag-name tag))
236 ;; PROMPT tags specify prompts for dictionary ? inserters
237 ;; which appear in the following templates
238 ((eq class 'prompt)
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
276 name :value value)))
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)
284 table))
287 ;; Ooops
288 (t (error "Unknown TAG class %s" class))
290 ;; Continue
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))
299 ;; APPLY TO MODE
301 (if (not mode)
302 (error "You must specify a MODE for your templates"))
305 ;; Calculate priority
307 (if (not 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))
324 priority)))
325 (when (called-interactively-p 'interactive)
326 (message "Compiling templates %s priority %d... done!"
327 (file-name-nondirectory (buffer-file-name))
328 priority)))
330 ;; Save it up!
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)
342 state)))
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
348 dict-tags state)))
349 (addargs))
350 ;; Examine arguments.
351 (dolist (arg args)
352 (let ((symbol (intern arg)))
353 (push symbol addargs)
355 ;; If we have a wrap, then put wrap inserters on both ends of
356 ;; the code.
357 (when (eq symbol :blank)
358 (setq code (append
359 (list (srecode-compile-inserter
360 "BLANK"
361 "\r"
362 state
363 :secondname nil
364 :where 'begin))
365 code
366 (list (srecode-compile-inserter
367 "BLANK"
368 "\r"
369 state
370 :secondname nil
371 :where 'end)))))))
373 ;; Construct and return the template object.
374 (srecode-template (semantic-tag-name tag)
375 :context context
376 :args (nreverse addargs)
377 :dictionary root-dict
378 :binding binding
379 :code code))
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)))
387 (or (not comp)
388 (progn (require 'srecode/insert)
389 (srecode-template-inserter-newline-child-p (car comp)))))
391 (defun srecode-compile-split-code (tag str STATE
392 &optional end-name)
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
398 macro names.
399 Optional argument END-NAME specifies the name of a token upon which
400 parsing should stop.
401 If END-NAME is specified, and the input string"
402 (let* ((what str)
403 (end-token nil)
404 (comp nil)
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))
409 (cond
410 ((string-match regex what)
411 (let* ((prefix (substring what 0 (match-beginning 0)))
412 (match (substring what
413 (match-beginning 0)
414 (match-end 0)))
415 (namestart (match-end 0))
416 (junk (string-match regexend what namestart))
417 end tail name key)
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.
423 (let ((new-inserter
424 (srecode-compile-inserter
425 "INDENT"
426 "\n"
427 STATE
428 :secondname nil
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
432 ;; ignored.
433 :hard (srecode-compile-do-hard-newline-p comp)
435 ;; Trim WHAT back.
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.
442 (setq end (if junk
443 (match-beginning 0)
444 (error "Could not find end escape for %s"
445 (semantic-tag-name tag)))
446 tail (match-end 0))
447 (cond ((not end)
448 (error "No matching escape end for %s"
449 (semantic-tag-name tag)))
450 ((<= end namestart)
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)
456 key nil)
457 ;; Trim WHAT back.
458 (setq what (substring what tail))
459 ;; Get the inserter
460 (let ((new-inserter
461 (srecode-compile-parse-inserter name STATE))
463 ;; If this is an end inserter, then assign into
464 ;; the end-token.
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
470 ;; the input stream.
471 (setq what (srecode-parse-input new-inserter tag what
472 STATE))
476 (if end-name
477 (error "Unmatched section end %s" end-name))
478 (setq comp (cons what comp)
479 what nil))))
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))
486 name
488 (if (and (or (< key ?A) (> key ?Z))
489 (or (< key ?a) (> key ?z)) )
490 (setq name (substring txt 1))
491 (setq name txt
492 key nil))
493 (let* ((junk (string-match ":" name))
494 (namepart (if junk
495 (substring name 0 (match-beginning 0))
496 name))
497 (secondname (if junk
498 (substring name (match-end 0))
499 nil))
500 (new-inserter (srecode-compile-inserter
501 namepart key STATE
502 :secondname secondname
504 ;; Return the new inserter
505 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)
515 (if (not key)
516 (apply 'srecode-template-inserter-variable name props)
517 (let ((classes (eieio-class-children srecode-template-inserter))
518 (new nil))
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))
532 new)))
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))
546 (lp templates)
549 (while lp
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.
562 (when (not hs)
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)
569 (setq lp (cdr lp))))
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)
576 :namehash namehash
577 :contexthash contexthash
578 :variables vars
579 :major-mode mode
580 :priority priority
581 :application application
582 :framework framework
583 :project project))
584 (tmpl (oref table templates)))
585 ;; Loop over all the templates, and xref.
586 (while tmpl
587 (oset (car tmpl) :table table)
588 (setq tmpl (cdr tmpl))))
593 ;;; DEBUG
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))
604 (princ "\n")
605 (when (oref tmp args)
606 (princ " Arguments: ")
607 (prin1 (oref tmp args))
608 (princ "\n"))
609 (when (oref tmp dictionary)
610 (princ " Section Dictionaries:\n")
611 (srecode-dump (oref tmp dictionary) 4)
612 ;(princ "\n")
614 (when (and (slot-boundp tmp 'binding) (oref tmp binding))
615 (princ " Binding: ")
616 (prin1 (oref tmp binding))
617 (princ "\n"))
618 (princ " Compiled Codes:\n")
619 (srecode-dump-code-list (oref tmp code) " ")
620 (princ "\n\n")
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."
626 (let ((i 1))
627 (while code
628 (princ indent)
629 (prin1 i)
630 (princ ") ")
631 (cond ((stringp (car code))
632 (prin1 (car code)))
633 ((srecode-template-inserter-child-p (car code))
634 (srecode-dump (car code) indent))
636 (princ "Unknown Code: ")
637 (prin1 (car code))))
638 (setq code (cdr code)
639 i (1+ i))
640 (when code
641 (princ "\n"))))
644 (defmethod srecode-dump ((ins srecode-template-inserter) indent)
645 "Dump the state of the SRecode template inserter INS."
646 (princ "INS: \"")
647 (princ (eieio-object-name-string ins))
648 (when (oref ins :secondname)
649 (princ "\" : \"")
650 (princ (oref ins :secondname)))
651 (princ "\" type \"")
652 (let* ((oc (symbol-name (eieio-object-class ins)))
653 (junk (string-match "srecode-template-inserter-" oc))
654 (on (if junk
655 (substring oc (match-end 0))
656 oc)))
657 (princ on))
658 (princ "\"")
661 (provide 'srecode/compile)
663 ;; Local variables:
664 ;; generated-autoload-file: "loaddefs.el"
665 ;; generated-autoload-load-name: "srecode/compile"
666 ;; End:
668 ;;; srecode/compile.el ends here