declare smobs in alloc.c
[bpt/emacs.git] / lisp / cedet / srecode / compile.el
CommitLineData
4d902e6f
CY
1;;; srecode/compile --- Compilation of srecode template files.
2
ba318903 3;; Copyright (C) 2005, 2007-2014 Free Software Foundation, Inc.
4d902e6f
CY
4
5;; Author: Eric M. Ludlam <zappo@gnu.org>
6;; Keywords: codegeneration
7
8;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23;;; Commentary:
24;;
25;; Compile a Semantic Recoder template file.
26;;
27;; Template files are parsed using a Semantic/Wisent parser into
2f10955c 28;; a tag table. The code therein is then further parsed down using
4d902e6f
CY
29;; a regular expression parser.
30;;
31;; The output are a series of EIEIO objects which represent the
32;; templates in a way that could be inserted later.
33
67d3ffe4 34(eval-when-compile (require 'cl))
4d902e6f
CY
35(require 'semantic)
36(require 'eieio)
37(require 'eieio-base)
4d902e6f 38(require 'srecode/table)
b9749554 39(require 'srecode/dictionary)
4d902e6f 40
84c23041
GM
41(declare-function srecode-template-inserter-newline-child-p "srecode/insert"
42 t t)
4d902e6f
CY
43
44;;; Code:
45
46;;; Template Class
47;;
b9749554 48;; Templates describe a pattern of text that can be inserted into a
4d902e6f
CY
49;; buffer.
50;;
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
4c36be58 65 "List of section dictionaries.
4d902e6f
CY
66The compiled template can contain lists of section dictionaries,
67or values that are expected to be passed down into different
68section macros. The template section dictionaries are merged in with
da6062e6 69any incoming dictionaries values.")
4d902e6f
CY
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.
77The top-most template is the 'active' template. Use the accessor methods
78for push, pop, and peek for the active template.")
79 (table :initarg :table
80 :documentation
81 "The table this template lives in.")
82 )
83 "Class defines storage for semantic recoder templates.")
84
85(defun srecode-flush-active-templates ()
86 "Flush the active template storage.
2f10955c 87Useful if something goes wrong in SRecode, and the active template
4d902e6f
CY
88stack 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."))
95 )
96
97;;; Inserters
98;;
99;; Each inserter object manages a different thing that
100;; might be inserted into a template output stream.
101;;
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.
105;;
07a79ce4 106;; See srecode/insert.el for the specialized classes.
4d902e6f
CY
107;;
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
113additional static argument data."))
114 "This represents an item to be inserted via a template macro.
115Plain text strings are not handled via this baseclass."
116 :abstract t)
117
118(defmethod srecode-parse-input ((ins srecode-template-inserter)
119 tag input STATE)
120 "For the template inserter INS, parse INPUT.
121Shorten input only by the amount needed.
122Return the remains of INPUT.
123STATE is the current compilation state."
124 input)
125
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)
129
130(defmethod srecode-inserter-apply-state ((ins srecode-template-inserter) STATE)
131 "For the template inserter INS, apply information from STATE."
132 nil)
133
134(defmethod srecode-inserter-prin-example :STATIC ((ins srecode-template-inserter)
135 escape-start escape-end)
136 "Insert an example using inserter INS.
137Arguments 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)
145 )
146
147
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.")
158 )
159 "Current state of the compile.")
160
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)))
177 ))
178
179;;; TEMPLATE COMPILER
180;;
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.
82481502
CY
190 (unless (semantic-active-p)
191 (semantic-new-buffer-fcn))
4d902e6f
CY
192 (srecode-compile-templates)
193 ;; Trash the buffer if we had to read it in.
194 (if (not peb)
195 (kill-buffer (current-buffer)))
196 )))
197
198;;;###autoload
199(defun srecode-compile-templates ()
200 "Compile a semantic recode template file into a mode-local variable."
201 (interactive)
3f2a848d 202 (unless (semantic-active-p)
890f7890 203 (error "You have to activate semantic-mode to compile SRecode templates"))
b82525f2 204 (require 'srecode/insert)
890f7890
DE
205 (when (called-interactively-p 'interactive)
206 (message "Compiling template %s..."
207 (file-name-nondirectory (buffer-file-name))))
4d902e6f
CY
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)
62a81506 216 (framework nil)
4d902e6f 217 (priority nil)
b9749554 218 (project nil)
4d902e6f
CY
219 (vars nil)
220 )
221
222 ;;
223 ;; COMPILE
224 ;;
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))
234 )
235
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)
240 )
241
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)
254 )
255 ((string= name "escape_end")
256 (oset STATE escape_end firstvalue)
257 )
258 ((string= name "application")
259 (setq application (read firstvalue)))
62a81506
CY
260 ((string= name "framework")
261 (setq framework (read firstvalue)))
4d902e6f
CY
262 ((string= name "priority")
263 (setq priority (read firstvalue)))
b9749554
EL
264 ((string= name "project")
265 (setq project firstvalue))
4d902e6f
CY
266 (t
267 ;; Assign this into some table of variables.
268 (setq vars (cons (cons name firstvalue) vars))
269 ))
270 ;; If it isn't a single string, then the value of the
271 ;; variable belongs to a compound dictionary value.
272 ;;
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)))
278 ))
279 )
280
281 ;; FUNCTION tags are really templates.
282 ((eq class 'function)
283 (setq table (cons (srecode-compile-one-template-tag tag STATE)
284 table))
285 )
286
287 ;; Ooops
288 (t (error "Unknown TAG class %s" class))
289 )
290 ;; Continue
291 (setq tags (cdr tags)))
890f7890 292
4d902e6f 293 ;; MSG - Before install since nreverse whacks our list.
890f7890
DE
294 (when (called-interactively-p 'interactive)
295 (message "%d templates compiled for %s"
296 (length table) mode))
4d902e6f
CY
297
298 ;;
299 ;; APPLY TO MODE
300 ;;
301 (if (not mode)
302 (error "You must specify a MODE for your templates"))
303
304 ;;
305 ;; Calculate priority
306 ;;
307 (if (not priority)
b9749554
EL
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))))
890f7890
DE
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)))
4d902e6f
CY
329
330 ;; Save it up!
62a81506 331 (srecode-compile-template-table table mode priority application framework project vars)
4d902e6f
CY
332 )
333)
334
b9749554
EL
335(defun srecode-compile-one-template-tag (tag state)
336 "Compile a template tag TAG into a srecode template object.
337STATE 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)
354
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)))))))
372
373 ;; Construct and return the template object.
4d902e6f 374 (srecode-template (semantic-tag-name tag)
b9749554
EL
375 :context context
376 :args (nreverse addargs)
377 :dictionary root-dict
378 :binding binding
379 :code code))
380 )
4d902e6f
CY
381
382(defun srecode-compile-do-hard-newline-p (comp)
383 "Examine COMP to decide if the upcoming newline should be hard.
384It 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)
62a81506
CY
388 (progn (require 'srecode/insert)
389 (srecode-template-inserter-newline-child-p (car comp)))))
4d902e6f
CY
390
391(defun srecode-compile-split-code (tag str STATE
392 &optional end-name)
393 "Split the code for TAG into something templatable.
394STR is the string of code from TAG to split.
395STATE is the current compile state.
396ESCAPE_START and ESCAPE_END are regexps that indicate the beginning
397escape character, and end escape character pattern for expandable
398macro names.
399Optional argument END-NAME specifies the name of a token upon which
400parsing should stop.
401If 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)))
407 )
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))
6564b177 417 end tail name key)
4d902e6f
CY
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)
434 )))
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))
440 ))
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)))
453 )
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))
462 )
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))
473 )
474 )))
475 (t
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))))
481
482(defun srecode-compile-parse-inserter (txt STATE)
483 "Parse the inserter TXT with the current STATE.
484Return an inserter object."
485 (let ((key (aref txt 0))
6564b177 486 name
4d902e6f
CY
487 )
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
503 )))
504 ;; Return the new inserter
505 new-inserter)))
506
507(defun srecode-compile-inserter (name key STATE &rest props)
508 "Create an srecode inserter object for some macro NAME.
509KEY indicates a single character key representing a type
510of inserter to create.
511STATE is the current compile state.
512PROPS are additional properties that might need to be passed
513to the inserter constructor."
514 ;;(message "Compile: %s %S" name props)
515 (if (not key)
516 (apply 'srecode-template-inserter-variable name props)
e8cc7880 517 (let ((classes (eieio-class-children srecode-template-inserter))
4d902e6f
CY
518 (new nil))
519 ;; Loop over the various subclasses and
520 ;; create the correct inserter.
521 (while (and (not new) classes)
e8cc7880 522 (setq classes (append classes (eieio-class-children (car classes))))
4d902e6f
CY
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)
529 )
530 (setq classes (cdr classes)))
531 (if (not new) (error "SRECODE: Unknown macro code %S" key))
532 new)))
533
62a81506 534(defun srecode-compile-template-table (templates mode priority application framework project vars)
4d902e6f
CY
535 "Compile a list of TEMPLATES into an semantic recode table.
536The table being compiled is for MODE, or the string \"default\".
537PRIORITY is a numerical value that indicates this tables location
538in an ordered search.
539APPLICATION is the name of the application these templates belong to.
62a81506 540FRAMEWORK is the name of the framework these templates belong to.
b9749554 541PROJECT is a directory name which these templates scope to.
4d902e6f
CY
542A 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)
547 )
548
549 (while lp
550
551 (let* ((objname (oref (car lp) :object-name))
552 (context (oref (car lp) :context))
553 (globalname (concat context ":" objname))
554 )
555
556 ;; Place this template object into the global name hash.
557 (puthash globalname (car lp) namehash)
558
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))
da6062e6 565 ;; Put into that context's hash.
4d902e6f
CY
566 (puthash objname (car lp) hs)
567 )
568
569 (setq lp (cdr lp))))
570
b9749554
EL
571 (when (stringp project)
572 (setq project (expand-file-name project)))
573
4d902e6f
CY
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
b9749554 581 :application application
62a81506 582 :framework framework
b9749554 583 :project project))
4d902e6f
CY
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))))
589 ))
590
591
592
593;;; DEBUG
594;;
595;; Dump out information about the current srecoder compiled templates.
596;;
597
598(defmethod srecode-dump ((tmp srecode-template))
599 "Dump the contents of the SRecode template tmp."
600 (princ "== Template \"")
e8cc7880 601 (princ (eieio-object-name-string tmp))
4d902e6f
CY
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")
613 )
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")
621 )
622
623(defun srecode-dump-code-list (code indent)
624 "Dump the CODE from a template code list to standard output.
625Argument 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))
635 (t
636 (princ "Unknown Code: ")
637 (prin1 (car code))))
638 (setq code (cdr code)
639 i (1+ i))
640 (when code
641 (princ "\n"))))
642 )
643
644(defmethod srecode-dump ((ins srecode-template-inserter) indent)
645 "Dump the state of the SRecode template inserter INS."
646 (princ "INS: \"")
e8cc7880 647 (princ (eieio-object-name-string ins))
4d902e6f
CY
648 (when (oref ins :secondname)
649 (princ "\" : \"")
650 (princ (oref ins :secondname)))
651 (princ "\" type \"")
e8cc7880 652 (let* ((oc (symbol-name (eieio-object-class ins)))
4d902e6f
CY
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 "\"")
659 )
660
661(provide 'srecode/compile)
662
663;; Local variables:
664;; generated-autoload-file: "loaddefs.el"
4d902e6f
CY
665;; generated-autoload-load-name: "srecode/compile"
666;; End:
667
668;;; srecode/compile.el ends here