1 ;;; srecode-dictionary.el --- Dictionary code for the semantic recoder.
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Dictionaries contain lists of names and their associated values.
25 ;; These dictionaries are used to fill in macros from recoder templates.
33 (require 'srecode
/table
)
34 (eval-when-compile (require 'semantic
))
36 (declare-function srecode-compile-parse-inserter
"srecode/compile")
37 (declare-function srecode-dump-code-list
"srecode/compile")
38 (declare-function srecode-load-tables-for-mode
"srecode/find")
39 (declare-function srecode-insert-code-stream
"srecode/insert")
40 (declare-function data-debug-new-buffer
"data-debug")
41 (declare-function data-debug-insert-object-slots
"eieio-datadebug")
42 (declare-function srecode-field
"srecode/fields")
44 (defclass srecode-dictionary
()
45 ((namehash :initarg
:namehash
47 "Hash table containing the names of all the templates.")
48 (buffer :initarg
:buffer
50 "The buffer this dictionary was initialized with.")
51 (parent :initarg
:parent
52 :type
(or null srecode-dictionary
)
54 "The parent dictionary.
55 Symbols not appearing in this dictionary will be checked against the
57 (origin :initarg
:origin
60 "A string representing the origin of this dictionary.
61 Useful only while debugging.")
63 "Dictionary of symbols and what they mean.
64 Dictionaries are used to look up named symbols from
65 templates to decide what to do with those symbols.")
67 (defclass srecode-dictionary-compound-value
()
69 "A compound dictionary value.
70 Values stored in a dictionary must be a STRING,
71 a dictionary for showing sections, or an instance of a subclass
74 Compound dictionary values derive from this class, and must
75 provide a sequence of method implementations to convert into
79 (defclass srecode-dictionary-compound-variable
80 (srecode-dictionary-compound-value)
81 ((value :initarg
:value
83 "The value of this template variable.
84 Variables in template files are usually a single string
85 which can be inserted into a dictionary directly.
87 Some variables may be more complex and involve dictionary
88 lookups, strings, concatenation, or the like.
90 The format of VALUE is determined by current template
92 (compiled :initarg
:compiled
95 "The compiled version of VALUE.")
97 "A compound dictionary value for template file variables.
98 You can declare a variable in a template like this:
100 set NAME \"str\" macro \"OTHERNAME\"
102 with appending various parts together in a list.")
104 (defmethod initialize-instance ((this srecode-dictionary-compound-variable
)
106 "Initialize the compound variable THIS.
107 Makes sure that :value is compiled."
108 (let ((newfields nil
)
112 (if (eq (car fields
) :state
)
113 (setq state
(car (cdr fields
)))
114 (setq newfields
(cons (car (cdr fields
))
115 (cons (car fields
) newfields
))))
116 (setq fields
(cdr (cdr fields
))))
119 (error "Cannot create compound variable without :state"))
121 (call-next-method this
(nreverse newfields
))
122 (when (not (slot-boundp this
'compiled
))
123 (let ((val (oref this
:value
))
126 (let ((nval (car val
))
128 (cond ((stringp nval
)
129 (setq comp
(cons nval comp
)))
131 (equal (car nval
) 'macro
))
132 (require 'srecode
/compile
)
134 (srecode-compile-parse-inserter
139 (error "Don't know how to handle variable value %S" nval
)))
141 (setq val
(cdr val
)))
142 (oset this
:compiled
(nreverse comp
))))))
144 ;;; DICTIONARY METHODS
147 (defun srecode-create-dictionary (&optional buffer-or-parent
)
148 "Create a dictionary for BUFFER.
149 If BUFFER-OR-PARENT is not specified, assume a buffer, and
150 use the current buffer.
151 If BUFFER-OR-PARENT is another dictionary, then remember the
152 parent within the new dictionary, and assume that BUFFER
153 is the same as belongs to the parent dictionary.
154 The dictionary is initialized with variables setup for that
156 If BUFFER-OR-PARENT is t, then this dictionary should not be
157 associated with a buffer or parent."
163 (cond ((bufferp buffer-or-parent
)
164 (set-buffer buffer-or-parent
)
165 (setq buffer buffer-or-parent
166 origin
(buffer-name buffer-or-parent
)
168 ((srecode-dictionary-child-p buffer-or-parent
)
169 (setq parent buffer-or-parent
170 buffer
(oref buffer-or-parent buffer
)
171 origin
(concat (object-name buffer-or-parent
) " in "
172 (if buffer
(buffer-name buffer
)
175 (set-buffer buffer
)))
176 ((eq buffer-or-parent t
)
178 origin
"Unspecified Origin"))
180 (setq buffer
(current-buffer)
181 origin
(concat "Unspecified. Assume "
182 (buffer-name buffer
))
186 (let ((dict (srecode-dictionary
190 :namehash
(make-hash-table :test
'equal
193 ;; Only set up the default variables if we are being built
194 ;; directroy for a particular buffer.
196 ;; Variables from the table we are inserting from.
197 ;; @todo - get a better tree of tables.
198 (let ((mt (srecode-get-mode-table major-mode
))
199 (def (srecode-get-mode-table 'default
)))
200 ;; Each table has multiple template tables.
201 ;; Do DEF first so that MT can override any values.
202 (srecode-dictionary-add-template-table dict def
)
203 (srecode-dictionary-add-template-table dict mt
)
207 (defmethod srecode-dictionary-add-template-table ((dict srecode-dictionary
)
209 "Insert into DICT the variables found in table TPL.
210 TPL is an object representing a compiled template file."
212 (let ((tabs (oref tpl
:tables
)))
214 (let ((vars (oref (car tabs
) variables
)))
216 (srecode-dictionary-set-value
217 dict
(car (car vars
)) (cdr (car vars
)))
218 (setq vars
(cdr vars
))))
219 (setq tabs
(cdr tabs
))))))
222 (defmethod srecode-dictionary-set-value ((dict srecode-dictionary
)
224 "In dictionary DICT, set NAME to have VALUE."
226 (if (not (stringp name
))
227 (signal 'wrong-type-argument
(list name
'stringp
)))
229 (with-slots (namehash) dict
230 (puthash name value namehash
))
233 (defmethod srecode-dictionary-add-section-dictionary ((dict srecode-dictionary
)
234 name
&optional show-only
)
235 "In dictionary DICT, add a section dictionary for section macro NAME.
236 Return the new dictionary.
238 You can add several dictionaries to the same section macro.
239 For each dictionary added to a macro, the block of codes in the
240 template will be repeated.
242 If optional argument SHOW-ONLY is non-nil, then don't add a new dictionarly
243 if there is already one in place. Also, don't add FIRST/LAST entries.
244 These entries are not needed when we are just showing a section.
246 Each dictionary added will automatically get values for positional macros
247 which will enable SECTIONS to be enabled.
249 * FIRST - The first entry in the table.
250 * NOTFIRST - Not the first entry in the table.
251 * LAST - The last entry in the table
252 * NOTLAST - Not the last entry in the table.
254 Adding a new dictionary will alter these values in previously
255 inserted dictionaries."
257 (if (not (stringp name
))
258 (signal 'wrong-type-argument
(list name
'stringp
)))
259 (let ((new (srecode-create-dictionary dict
))
260 (ov (srecode-dictionary-lookup-name dict name
)))
262 (when (not show-only
)
263 ;; Setup the FIRST/NOTFIRST and LAST/NOTLAST entries.
266 (srecode-dictionary-show-section new
"FIRST")
267 (srecode-dictionary-show-section new
"LAST"))
268 ;; Not the very first one. Lets clean up CAR.
269 (let ((tail (car (last ov
))))
270 (srecode-dictionary-hide-section tail
"LAST")
271 (srecode-dictionary-show-section tail
"NOTLAST")
273 (srecode-dictionary-show-section new
"NOTFIRST")
274 (srecode-dictionary-show-section new
"LAST"))
277 (when (or (not show-only
) (null ov
))
278 (srecode-dictionary-set-value dict name
(append ov
(list new
))))
279 ;; Return the new sub-dictionary.
282 (defmethod srecode-dictionary-show-section ((dict srecode-dictionary
) name
)
283 "In dictionary DICT, indicate that the section NAME should be exposed."
285 (if (not (stringp name
))
286 (signal 'wrong-type-argument
(list name
'stringp
)))
287 ;; Showing a section is just like making a section dictionary, but
288 ;; with no dictionary values to add.
289 (srecode-dictionary-add-section-dictionary dict name t
)
292 (defmethod srecode-dictionary-hide-section ((dict srecode-dictionary
) name
)
293 "In dictionary DICT, indicate that the section NAME should be hidden."
294 ;; We need to find the has value, and then delete it.
296 (if (not (stringp name
))
297 (signal 'wrong-type-argument
(list name
'stringp
)))
299 (with-slots (namehash) dict
300 (remhash name namehash
))
303 (defmethod srecode-dictionary-merge ((dict srecode-dictionary
) otherdict
)
304 "Merge into DICT the dictionary entries from OTHERDICT."
308 ;; Only merge in the new values if there was no old value.
309 ;; This protects applications from being whacked, and basically
310 ;; makes these new section dictionary entries act like
311 ;; "defaults" instead of overrides.
312 (when (not (srecode-dictionary-lookup-name dict key
))
313 (cond ((and (listp entry
) (srecode-dictionary-p (car entry
)))
314 ;; A list of section dictionaries.
315 ;; We need to merge them in.
318 (srecode-dictionary-add-section-dictionary
320 (srecode-dictionary-merge new-sub-dict
(car entry
)))
321 (setq entry
(cdr entry
)))
325 (srecode-dictionary-set-value dict key entry
)))
327 (oref otherdict namehash
))))
329 (defmethod srecode-dictionary-lookup-name ((dict srecode-dictionary
)
331 "Return information about the current DICT's value for NAME."
332 (if (not (slot-boundp dict
'namehash
))
334 ;; Get the value of this name from the dictionary
335 (or (with-slots (namehash) dict
336 (gethash name namehash
))
337 (and (not (member name
'("FIRST" "LAST" "NOTFIRST" "NOTLAST")))
339 (srecode-dictionary-lookup-name (oref dict parent
) name
))
342 (defmethod srecode-root-dictionary ((dict srecode-dictionary
))
343 "For dictionary DICT, return the root dictionary.
344 The root dictionary is usually for a current or active insertion."
346 (while (oref ans parent
)
347 (setq ans
(oref ans parent
)))
350 ;;; COMPOUND VALUE METHODS
352 ;; Compound values must provide at least the toStriong method
353 ;; for use in converting the compound value into sometehing insertable.
355 (defmethod srecode-compound-toString ((cp srecode-dictionary-compound-value
)
358 "Convert the compound dictionary value CP to a string.
359 If FUNCTION is non-nil, then FUNCTION is somehow applied to an aspect
360 of the compound value. The FUNCTION could be a fraction
361 of some function symbol with a logical prefix excluded.
363 If you subclass `srecode-dictionary-compound-value' then this
364 method could return nil, but if it does that, it must insert
365 the value itself using `princ', or by detecting if the current
366 standard out is a buffer, and using `insert'."
369 (defmethod srecode-dump ((cp srecode-dictionary-compound-value
)
371 "Display information about this compound value."
372 (princ (object-name cp
))
375 (defmethod srecode-compound-toString ((cp srecode-dictionary-compound-variable
)
378 "Convert the compound dictionary variable value CP into a string.
379 FUNCTION and DICTIONARY are as for the baseclass."
380 (require 'srecode
/insert
)
381 (srecode-insert-code-stream (oref cp compiled
) dictionary
))
384 (defmethod srecode-dump ((cp srecode-dictionary-compound-variable
)
386 "Display information about this compound value."
387 (require 'srecode
/compile
)
388 (princ "# Compound Variable #\n")
389 (let ((indent (+ 4 (or indent
0)))
390 (cmp (oref cp compiled
))
392 (srecode-dump-code-list cmp
(make-string indent ?
))
395 ;;; FIELD EDITING COMPOUND VALUE
397 ;; This is an interface to using field-editing objects
398 ;; instead of asking questions. This provides the basics
399 ;; behind this compound value.
401 (defclass srecode-field-value
(srecode-dictionary-compound-value)
402 ((firstinserter :initarg
:firstinserter
404 "The inserter object for the first occurance of this field.")
405 (defaultvalue :initarg
:defaultvalue
407 "The default value for this inserter.")
409 "When inserting values with editable field mode, a dictionary value.
410 Compound values allow a field to be stored in the dictionary for when
411 it is referenced a second time. This compound value can then be
412 inserted with a new editable field.")
414 (defmethod srecode-compound-toString((cp srecode-field-value
)
417 "Convert this field into an insertable string."
418 (require 'srecode
/fields
)
419 ;; If we are not in a buffer, then this is not supported.
420 (when (not (bufferp standard-output
))
421 (error "FIELDS invoked while inserting template to non-buffer"))
424 (error "@todo: Cannot mix field insertion with functions")
426 ;; No function. Perform a plain field insertion.
427 ;; We know we are in a buffer, so we can perform the insertion.
428 (let* ((dv (oref cp defaultvalue
))
429 (sti (oref cp firstinserter
))
431 (name (oref sti
:object-name
)))
433 (if (or (not dv
) (string= dv
""))
437 (srecode-field name
:name name
440 :prompt
(oref sti prompt
)
441 :read-fcn
(oref sti read-fcn
)
444 ;; Returning nil is a signal that we have done the insertion ourselves.
448 ;;; Higher level dictionary functions
450 (defun srecode-create-section-dictionary (sectiondicts STATE
)
451 "Create a dictionary with section entries for a template.
452 The format for SECTIONDICTS is what is emitted from the template parsers.
453 STATE is the current compiler state."
455 (let ((new (srecode-create-dictionary t
)))
456 ;; Loop over each section. The section is a macro w/in the
459 (let* ((sect (car (car sectiondicts
)))
460 (entries (cdr (car sectiondicts
)))
461 (subdict (srecode-dictionary-add-section-dictionary new sect
))
463 ;; Loop over each entry. This is one variable in the
464 ;; section dictionary.
466 (let ((tname (semantic-tag-name (car entries
)))
467 (val (semantic-tag-variable-default (car entries
))))
469 (srecode-dictionary-show-section subdict tname
)
471 ((and (stringp (car val
))
473 (setq val
(car val
)))
475 (setq val
(srecode-dictionary-compound-variable
476 tname
:value val
:state STATE
))))
477 (srecode-dictionary-set-value
479 (setq entries
(cdr entries
))))
481 (setq sectiondicts
(cdr sectiondicts
)))
486 ;; Make a dictionary, and dump it's contents.
488 (defun srecode-adebug-dictionary ()
489 "Run data-debug on this mode's dictionary."
491 (require 'eieio-datadebug
)
493 (require 'srecode
/find
)
494 (let* ((modesym major-mode
)
495 (start (current-time))
496 (junk (or (progn (srecode-load-tables-for-mode modesym
)
497 (srecode-get-mode-table modesym
))
498 (error "No table found for mode %S" modesym
)))
499 (dict (srecode-create-dictionary (current-buffer)))
502 (message "Creating a dictionary took %.2f seconds."
503 (semantic-elapsed-time start end
))
504 (data-debug-new-buffer "*SRECODE ADEBUG*")
505 (data-debug-insert-object-slots dict
"*")))
507 (defun srecode-dictionary-dump ()
508 "Dump a typical fabricated dictionary."
510 (require 'srecode
/find
)
511 (let ((modesym major-mode
))
512 ;; This load allows the dictionary access to inherited
513 ;; and stacked dictionary entries.
514 (srecode-load-tables-for-mode modesym
)
515 (let ((tmp (srecode-get-mode-table modesym
))
518 (error "No table found for mode %S" modesym
))
519 ;; Now make the dictionary.
520 (let ((dict (srecode-create-dictionary (current-buffer))))
521 (with-output-to-temp-buffer "*SRECODE DUMP*"
522 (princ "DICTIONARY FOR ")
524 (princ "\n--------------------------------------------\n")
528 (defmethod srecode-dump ((dict srecode-dictionary
) &optional indent
)
530 (if (not indent
) (setq indent
0))
531 (maphash (lambda (key entry
)
532 (princ (make-string indent ?
))
536 (cond ((and (listp entry
)
537 (srecode-dictionary-p (car entry
)))
538 (let ((newindent (if indent
542 (princ " --> SUBDICTIONARY ")
543 (princ (object-name dict
))
545 (srecode-dump (car entry
) newindent
)
546 (setq entry
(cdr entry
))
550 ((srecode-dictionary-compound-value-child-p entry
)
551 (srecode-dump entry indent
)
560 (oref dict namehash
))
563 (provide 'srecode
/dictionary
)
565 ;;; srecode/dictionary.el ends here