1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
;;; srecode/compile --- Compilation of srecode template files.
;; Copyright (C) 2005, 2007-2011 Free Software Foundation, Inc.
;; Author: Eric M. Ludlam <zappo@gnu.org>
;; Keywords: codegeneration
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Compile a Semantic Recoder template file.
;;
;; Template files are parsed using a Semantic/Wisent parser into
;; a tag table. The code therein is then further parsed down using
;; a regular expression parser.
;;
;; The output are a series of EIEIO objects which represent the
;; templates in a way that could be inserted later.
(eval-when-compile (require 'cl))
(require 'semantic)
(require 'eieio)
(require 'eieio-base)
(require 'srecode/table)
(require 'srecode/dictionary)
(declare-function srecode-template-inserter-newline-child-p "srecode/insert"
t t)
;;; Code:
;;; Template Class
;;
;; Templates describe a pattern of text that can be inserted into a
;; buffer.
;;
(defclass srecode-template (eieio-named)
((context :initarg :context
:initform nil
:documentation
"Context this template belongs to.")
(args :initarg :args
:documentation
"List of arguments that this template requires.")
(code :initarg :code
:documentation
"Compiled text from the template.")
(dictionary :initarg :dictionary
:type (or null srecode-dictionary)
:documentation
"List of section dictionaries.
The compiled template can contain lists of section dictionaries,
or values that are expected to be passed down into different
section macros. The template section dictionaries are merged in with
any incoming dictionaries values.")
(binding :initarg :binding
:documentation
"Preferred keybinding for this template in `srecode-minor-mode-map'.")
(active :allocation :class
:initform nil
:documentation
"During template insertion, this is the stack of active templates.
The top-most template is the 'active' template. Use the accessor methods
for push, pop, and peek for the active template.")
(table :initarg :table
:documentation
"The table this template lives in.")
)
"Class defines storage for semantic recoder templates.")
(defun srecode-flush-active-templates ()
"Flush the active template storage.
Useful if something goes wrong in SRecode, and the active template
stack is broken."
(interactive)
(if (oref srecode-template active)
(when (y-or-n-p (format "%d active templates. Flush? "
(length (oref srecode-template active))))
(oset-default srecode-template active nil))
(message "No active templates to flush."))
)
;;; Inserters
;;
;; Each inserter object manages a different thing that
;; might be inserted into a template output stream.
;;
;; The 'srecode-insert-method' on each inserter does the actual
;; work, and the smaller, simple inserter object is saved in
;; the compiled templates.
;;
;; See srecode/insert.el for the specialized classes.
;;
(defclass srecode-template-inserter (eieio-named)
((secondname :initarg :secondname
:type (or null string)
:documentation
"If there is a colon in the inserter's name, it represents
additional static argument data."))
"This represents an item to be inserted via a template macro.
Plain text strings are not handled via this baseclass."
:abstract t)
(defmethod srecode-parse-input ((ins srecode-template-inserter)
tag input STATE)
"For the template inserter INS, parse INPUT.
Shorten input only by the amount needed.
Return the remains of INPUT.
STATE is the current compilation state."
input)
(defmethod srecode-match-end ((ins srecode-template-inserter) name)
"For the template inserter INS, do I end a section called NAME?"
nil)
(defmethod srecode-inserter-apply-state ((ins srecode-template-inserter) STATE)
"For the template inserter INS, apply information from STATE."
nil)
(defmethod srecode-inserter-prin-example :STATIC ((ins srecode-template-inserter)
escape-start escape-end)
"Insert an example using inserter INS.
Arguments ESCAPE-START and ESCAPE-END are the current escape sequences in use."
(princ " ")
(princ escape-start)
(when (and (slot-exists-p ins 'key) (oref ins key))
(princ (format "%c" (oref ins key))))
(princ "VARNAME")
(princ escape-end)
(terpri)
)
;;; Compile State
(defclass srecode-compile-state ()
((context :initform "declaration"
:documentation "The active context.")
(prompts :initform nil
:documentation "The active prompts.")
(escape_start :initform "{{"
:documentation "The starting escape sequence.")
(escape_end :initform "}}"
:documentation "The ending escape sequence.")
)
"Current state of the compile.")
(defmethod srecode-compile-add-prompt ((state srecode-compile-state)
prompttag)
"Add PROMPTTAG to the current list of prompts."
(with-slots (prompts) state
(let ((match (assoc (semantic-tag-name prompttag) prompts))
(newprompts prompts))
(when match
(let ((tmp prompts))
(setq newprompts nil)
(while tmp
(when (not (string= (car (car tmp))
(car prompttag)))
(setq newprompts (cons (car tmp)
newprompts)))
(setq tmp (cdr tmp)))))
(setq prompts (cons prompttag newprompts)))
))
;;; TEMPLATE COMPILER
;;
(defun srecode-compile-file (fname)
"Compile the templates from the file FNAME."
(let ((peb (get-file-buffer fname)))
(save-excursion
;; Make whatever it is local.
(if (not peb)
(set-buffer (semantic-find-file-noselect fname))
(set-buffer peb))
;; Do the compile.
(unless (semantic-active-p)
(semantic-new-buffer-fcn))
(srecode-compile-templates)
;; Trash the buffer if we had to read it in.
(if (not peb)
(kill-buffer (current-buffer)))
)))
;;;###autoload
(defun srecode-compile-templates ()
"Compile a semantic recode template file into a mode-local variable."
(interactive)
(require 'srecode/insert)
(message "Compiling template %s..."
(file-name-nondirectory (buffer-file-name)))
(let ((tags (semantic-fetch-tags))
(tag nil)
(class nil)
(table nil)
(STATE (srecode-compile-state (file-name-nondirectory
(buffer-file-name))))
(mode nil)
(application nil)
(priority nil)
(project nil)
(vars nil)
)
;;
;; COMPILE
;;
(while tags
(setq tag (car tags)
class (semantic-tag-class tag))
;; What type of item is it?
(cond
;; CONTEXT tags specify the context all future tags
;; belong to.
((eq class 'context)
(oset STATE context (semantic-tag-name tag))
)
;; PROMPT tags specify prompts for dictionary ? inserters
;; which appear in the following templates
((eq class 'prompt)
(srecode-compile-add-prompt STATE tag)
)
;; VARIABLE tags can specify operational control
((eq class 'variable)
(let* ((name (semantic-tag-name tag))
(value (semantic-tag-variable-default tag))
(firstvalue (car value)))
;; If it is a single string, and one value, then
;; look to see if it is one of our special variables.
(if (and (= (length value) 1) (stringp firstvalue))
(cond ((string= name "mode")
(setq mode (intern firstvalue)))
((string= name "escape_start")
(oset STATE escape_start firstvalue)
)
((string= name "escape_end")
(oset STATE escape_end firstvalue)
)
((string= name "application")
(setq application (read firstvalue)))
((string= name "priority")
(setq priority (read firstvalue)))
((string= name "project")
(setq project firstvalue))
(t
;; Assign this into some table of variables.
(setq vars (cons (cons name firstvalue) vars))
))
;; If it isn't a single string, then the value of the
;; variable belongs to a compound dictionary value.
;;
;; Create a compound dictionary value from "value".
(require 'srecode/dictionary)
(let ((cv (srecode-dictionary-compound-variable
name :value value)))
(setq vars (cons (cons name cv) vars)))
))
)
;; FUNCTION tags are really templates.
((eq class 'function)
(setq table (cons (srecode-compile-one-template-tag tag STATE)
table))
)
;; Ooops
(t (error "Unknown TAG class %s" class))
)
;; Continue
(setq tags (cdr tags)))
;; MSG - Before install since nreverse whacks our list.
(message "%d templates compiled for %s"
(length table) mode)
;;
;; APPLY TO MODE
;;
(if (not mode)
(error "You must specify a MODE for your templates"))
;;
;; Calculate priority
;;
(if (not priority)
(let ((d (expand-file-name (file-name-directory (buffer-file-name))))
(sd (expand-file-name (file-name-directory (locate-library "srecode"))))
(defaultdelta (if (eq mode 'default) 0 10)))
;; @TODO : WHEN INTEGRATING INTO EMACS
;; The location of Emacs default templates needs to be specified
;; here to also have a lower priority.
(if (string-match (concat "^" sd) d)
(setq priority (+ 30 defaultdelta))
;; If the user created template is for a project, then
;; don't add as much as if it is unique to just some user.
(if (stringp project)
(setq priority (+ 50 defaultdelta))
(setq priority (+ 80 defaultdelta))))
(message "Templates %s has estimated priority of %d"
(file-name-nondirectory (buffer-file-name))
priority))
(message "Compiling templates %s priority %d... done!"
(file-name-nondirectory (buffer-file-name))
priority))
;; Save it up!
(srecode-compile-template-table table mode priority application project vars)
)
)
(defun srecode-compile-one-template-tag (tag state)
"Compile a template tag TAG into a srecode template object.
STATE is the current compile state as an object of class
`srecode-compile-state'."
(let* ((context (oref state context))
(code (cdr (srecode-compile-split-code
tag (semantic-tag-get-attribute tag :code)
state)))
(args (semantic-tag-function-arguments tag))
(binding (semantic-tag-get-attribute tag :binding))
(dict-tags (semantic-tag-get-attribute tag :dictionaries))
(root-dict (when dict-tags
(srecode-create-dictionaries-from-tags
dict-tags state)))
(addargs))
;; Examine arguments.
(dolist (arg args)
(let ((symbol (intern arg)))
(push symbol addargs)
;; If we have a wrap, then put wrap inserters on both ends of
;; the code.
(when (eq symbol :blank)
(setq code (append
(list (srecode-compile-inserter
"BLANK"
"\r"
state
:secondname nil
:where 'begin))
code
(list (srecode-compile-inserter
"BLANK"
"\r"
state
:secondname nil
:where 'end)))))))
;; Construct and return the template object.
(srecode-template (semantic-tag-name tag)
:context context
:args (nreverse addargs)
:dictionary root-dict
:binding binding
:code code))
)
(defun srecode-compile-do-hard-newline-p (comp)
"Examine COMP to decide if the upcoming newline should be hard.
It is hard if the previous inserter is a newline object."
(while (and comp (stringp (car comp)))
(setq comp (cdr comp)))
(or (not comp)
(require 'srecode/insert)
(srecode-template-inserter-newline-child-p (car comp))))
(defun srecode-compile-split-code (tag str STATE
&optional end-name)
"Split the code for TAG into something templatable.
STR is the string of code from TAG to split.
STATE is the current compile state.
ESCAPE_START and ESCAPE_END are regexps that indicate the beginning
escape character, and end escape character pattern for expandable
macro names.
Optional argument END-NAME specifies the name of a token upon which
parsing should stop.
If END-NAME is specified, and the input string"
(let* ((what str)
(end-token nil)
(comp nil)
(regex (concat "\n\\|" (regexp-quote (oref STATE escape_start))))
(regexend (regexp-quote (oref STATE escape_end)))
)
(while (and what (not end-token))
(cond
((string-match regex what)
(let* ((prefix (substring what 0 (match-beginning 0)))
(match (substring what
(match-beginning 0)
(match-end 0)))
(namestart (match-end 0))
(junk (string-match regexend what namestart))
end tail name key)
;; Add string to compiled output
(when (> (length prefix) 0)
(setq comp (cons prefix comp)))
(if (string= match "\n")
;; Do newline thingy.
(let ((new-inserter
(srecode-compile-inserter
"INDENT"
"\n"
STATE
:secondname nil
;; This newline is "hard" meaning ALWAYS do it
;; if the previous entry is also a newline.
;; Without it, user entered blank lines will be
;; ignored.
:hard (srecode-compile-do-hard-newline-p comp)
)))
;; Trim WHAT back.
(setq what (substring what namestart))
(when (> (length what) 0)
;; make the new inserter, but only if we aren't last.
(setq comp (cons new-inserter comp))
))
;; Regular inserter thingy.
(setq end (if junk
(match-beginning 0)
(error "Could not find end escape for %s"
(semantic-tag-name tag)))
tail (match-end 0))
(cond ((not end)
(error "No matching escape end for %s"
(semantic-tag-name tag)))
((<= end namestart)
(error "Stray end escape for %s"
(semantic-tag-name tag)))
)
;; Add string to compiled output
(setq name (substring what namestart end)
key nil)
;; Trim WHAT back.
(setq what (substring what tail))
;; Get the inserter
(let ((new-inserter
(srecode-compile-parse-inserter name STATE))
)
;; If this is an end inserter, then assign into
;; the end-token.
(if (srecode-match-end new-inserter end-name)
(setq end-token new-inserter))
;; Add the inserter to our compilation stream.
(setq comp (cons new-inserter comp))
;; Allow the inserter an opportunity to modify
;; the input stream.
(setq what (srecode-parse-input new-inserter tag what
STATE))
)
)))
(t
(if end-name
(error "Unmatched section end %s" end-name))
(setq comp (cons what comp)
what nil))))
(cons what (nreverse comp))))
(defun srecode-compile-parse-inserter (txt STATE)
"Parse the inserter TXT with the current STATE.
Return an inserter object."
(let ((key (aref txt 0))
name
)
(if (and (or (< key ?A) (> key ?Z))
(or (< key ?a) (> key ?z)) )
(setq name (substring txt 1))
(setq name txt
key nil))
(let* ((junk (string-match ":" name))
(namepart (if junk
(substring name 0 (match-beginning 0))
name))
(secondname (if junk
(substring name (match-end 0))
nil))
(new-inserter (srecode-compile-inserter
namepart key STATE
:secondname secondname
)))
;; Return the new inserter
new-inserter)))
(defun srecode-compile-inserter (name key STATE &rest props)
"Create an srecode inserter object for some macro NAME.
KEY indicates a single character key representing a type
of inserter to create.
STATE is the current compile state.
PROPS are additional properties that might need to be passed
to the inserter constructor."
;;(message "Compile: %s %S" name props)
(if (not key)
(apply 'srecode-template-inserter-variable name props)
(let ((classes (class-children srecode-template-inserter))
(new nil))
;; Loop over the various subclasses and
;; create the correct inserter.
(while (and (not new) classes)
(setq classes (append classes (class-children (car classes))))
;; Do we have a match?
(when (and (not (class-abstract-p (car classes)))
(equal (oref (car classes) key) key))
;; Create the new class, and apply state.
(setq new (apply (car classes) name props))
(srecode-inserter-apply-state new STATE)
)
(setq classes (cdr classes)))
(if (not new) (error "SRECODE: Unknown macro code %S" key))
new)))
(defun srecode-compile-template-table (templates mode priority application project vars)
"Compile a list of TEMPLATES into an semantic recode table.
The table being compiled is for MODE, or the string \"default\".
PRIORITY is a numerical value that indicates this tables location
in an ordered search.
APPLICATION is the name of the application these templates belong to.
PROJECT is a directory name which these templates scope to.
A list of defined variables VARS provides a variable table."
(let ((namehash (make-hash-table :test 'equal
:size (length templates)))
(contexthash (make-hash-table :test 'equal :size 10))
(lp templates)
)
(while lp
(let* ((objname (oref (car lp) :object-name))
(context (oref (car lp) :context))
(globalname (concat context ":" objname))
)
;; Place this template object into the global name hash.
(puthash globalname (car lp) namehash)
;; Place this template into the specific context name hash.
(let ((hs (gethash context contexthash)))
;; Make a new context if none was available.
(when (not hs)
(setq hs (make-hash-table :test 'equal :size 20))
(puthash context hs contexthash))
;; Put into that context's hash.
(puthash objname (car lp) hs)
)
(setq lp (cdr lp))))
(when (stringp project)
(setq project (expand-file-name project)))
(let* ((table (srecode-mode-table-new mode (buffer-file-name)
:templates (nreverse templates)
:namehash namehash
:contexthash contexthash
:variables vars
:major-mode mode
:priority priority
:application application
:project project))
(tmpl (oref table templates)))
;; Loop over all the templates, and xref.
(while tmpl
(oset (car tmpl) :table table)
(setq tmpl (cdr tmpl))))
))
;;; DEBUG
;;
;; Dump out information about the current srecoder compiled templates.
;;
(defmethod srecode-dump ((tmp srecode-template))
"Dump the contents of the SRecode template tmp."
(princ "== Template \"")
(princ (object-name-string tmp))
(princ "\" in context ")
(princ (oref tmp context))
(princ "\n")
(when (oref tmp args)
(princ " Arguments: ")
(prin1 (oref tmp args))
(princ "\n"))
(when (oref tmp dictionary)
(princ " Section Dictionaries:\n")
(srecode-dump (oref tmp dictionary) 4)
;(princ "\n")
)
(when (and (slot-boundp tmp 'binding) (oref tmp binding))
(princ " Binding: ")
(prin1 (oref tmp binding))
(princ "\n"))
(princ " Compiled Codes:\n")
(srecode-dump-code-list (oref tmp code) " ")
(princ "\n\n")
)
(defun srecode-dump-code-list (code indent)
"Dump the CODE from a template code list to standard output.
Argument INDENT specifies the indentation level for the list."
(let ((i 1))
(while code
(princ indent)
(prin1 i)
(princ ") ")
(cond ((stringp (car code))
(prin1 (car code)))
((srecode-template-inserter-child-p (car code))
(srecode-dump (car code) indent))
(t
(princ "Unknown Code: ")
(prin1 (car code))))
(setq code (cdr code)
i (1+ i))
(when code
(princ "\n"))))
)
(defmethod srecode-dump ((ins srecode-template-inserter) indent)
"Dump the state of the SRecode template inserter INS."
(princ "INS: \"")
(princ (object-name-string ins))
(when (oref ins :secondname)
(princ "\" : \"")
(princ (oref ins :secondname)))
(princ "\" type \"")
(let* ((oc (symbol-name (object-class ins)))
(junk (string-match "srecode-template-inserter-" oc))
(on (if junk
(substring oc (match-end 0))
oc)))
(princ on))
(princ "\"")
)
(provide 'srecode/compile)
;; Local variables:
;; generated-autoload-file: "loaddefs.el"
;; generated-autoload-load-name: "srecode/compile"
;; End:
;;; srecode/compile.el ends here
|