1 ;;; srecode-dictionary.el --- Dictionary code for the semantic recoder.
3 ;; Copyright (C) 2007, 2008, 2009, 2010 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.
31 (eval-when-compile (require 'cl
))
34 (require 'srecode
/table
)
35 (eval-when-compile (require 'semantic
))
37 (declare-function srecode-compile-parse-inserter
"srecode/compile")
38 (declare-function srecode-dump-code-list
"srecode/compile")
39 (declare-function srecode-load-tables-for-mode
"srecode/find")
40 (declare-function srecode-insert-code-stream
"srecode/insert")
41 (declare-function data-debug-new-buffer
"data-debug")
42 (declare-function data-debug-insert-object-slots
"eieio-datadebug")
43 (declare-function srecode-field
"srecode/fields")
45 (defclass srecode-dictionary
()
46 ((namehash :initarg
:namehash
48 "Hash table containing the names of all the templates.")
49 (buffer :initarg
:buffer
51 "The buffer this dictionary was initialized with.")
52 (parent :initarg
:parent
53 :type
(or null srecode-dictionary
)
55 "The parent dictionary.
56 Symbols not appearing in this dictionary will be checked against the
58 (origin :initarg
:origin
61 "A string representing the origin of this dictionary.
62 Useful only while debugging.")
64 "Dictionary of symbols and what they mean.
65 Dictionaries are used to look up named symbols from
66 templates to decide what to do with those symbols.")
68 (defclass srecode-dictionary-compound-value
()
70 "A compound dictionary value.
71 Values stored in a dictionary must be a STRING,
72 a dictionary for showing sections, or an instance of a subclass
75 Compound dictionary values derive from this class, and must
76 provide a sequence of method implementations to convert into
80 (defclass srecode-dictionary-compound-variable
81 (srecode-dictionary-compound-value)
82 ((value :initarg
:value
84 "The value of this template variable.
85 Variables in template files are usually a single string
86 which can be inserted into a dictionary directly.
88 Some variables may be more complex and involve dictionary
89 lookups, strings, concatenation, or the like.
91 The format of VALUE is determined by current template
93 (compiled :initarg
:compiled
96 "The compiled version of VALUE.")
98 "A compound dictionary value for template file variables.
99 You can declare a variable in a template like this:
101 set NAME \"str\" macro \"OTHERNAME\"
103 with appending various parts together in a list.")
105 (defmethod initialize-instance ((this srecode-dictionary-compound-variable
)
107 "Initialize the compound variable THIS.
108 Makes sure that :value is compiled."
109 (let ((newfields nil
)
113 (if (eq (car fields
) :state
)
114 (setq state
(car (cdr fields
)))
115 (setq newfields
(cons (car (cdr fields
))
116 (cons (car fields
) newfields
))))
117 (setq fields
(cdr (cdr fields
))))
120 (error "Cannot create compound variable without :state"))
122 (call-next-method this
(nreverse newfields
))
123 (when (not (slot-boundp this
'compiled
))
124 (let ((val (oref this
:value
))
127 (let ((nval (car val
))
129 (cond ((stringp nval
)
130 (setq comp
(cons nval comp
)))
132 (equal (car nval
) 'macro
))
133 (require 'srecode
/compile
)
135 (srecode-compile-parse-inserter
140 (error "Don't know how to handle variable value %S" nval
)))
142 (setq val
(cdr val
)))
143 (oset this
:compiled
(nreverse comp
))))))
145 ;;; DICTIONARY METHODS
148 (defun srecode-create-dictionary (&optional buffer-or-parent
)
149 "Create a dictionary for BUFFER.
150 If BUFFER-OR-PARENT is not specified, assume a buffer, and
151 use the current buffer.
152 If BUFFER-OR-PARENT is another dictionary, then remember the
153 parent within the new dictionary, and assume that BUFFER
154 is the same as belongs to the parent dictionary.
155 The dictionary is initialized with variables setup for that
157 If BUFFER-OR-PARENT is t, then this dictionary should not be
158 associated with a buffer or parent."
164 (cond ((bufferp buffer-or-parent
)
165 (set-buffer buffer-or-parent
)
166 (setq buffer buffer-or-parent
167 origin
(buffer-name buffer-or-parent
)
169 ((srecode-dictionary-child-p buffer-or-parent
)
170 (setq parent buffer-or-parent
171 buffer
(oref buffer-or-parent buffer
)
172 origin
(concat (object-name buffer-or-parent
) " in "
173 (if buffer
(buffer-name buffer
)
176 (set-buffer buffer
)))
177 ((eq buffer-or-parent t
)
179 origin
"Unspecified Origin"))
181 (setq buffer
(current-buffer)
182 origin
(concat "Unspecified. Assume "
183 (buffer-name buffer
))
187 (let ((dict (srecode-dictionary
191 :namehash
(make-hash-table :test
'equal
194 ;; Only set up the default variables if we are being built
195 ;; directroy for a particular buffer.
197 ;; Variables from the table we are inserting from.
198 ;; @todo - get a better tree of tables.
199 (let ((mt (srecode-get-mode-table major-mode
))
200 (def (srecode-get-mode-table 'default
)))
201 ;; Each table has multiple template tables.
202 ;; Do DEF first so that MT can override any values.
203 (srecode-dictionary-add-template-table dict def
)
204 (srecode-dictionary-add-template-table dict mt
)
208 (defmethod srecode-dictionary-add-template-table ((dict srecode-dictionary
)
210 "Insert into DICT the variables found in table TPL.
211 TPL is an object representing a compiled template file."
213 (let ((tabs (oref tpl
:tables
)))
215 (let ((vars (oref (car tabs
) variables
)))
217 (srecode-dictionary-set-value
218 dict
(car (car vars
)) (cdr (car vars
)))
219 (setq vars
(cdr vars
))))
220 (setq tabs
(cdr tabs
))))))
223 (defmethod srecode-dictionary-set-value ((dict srecode-dictionary
)
225 "In dictionary DICT, set NAME to have VALUE."
227 (if (not (stringp name
))
228 (signal 'wrong-type-argument
(list name
'stringp
)))
230 (with-slots (namehash) dict
231 (puthash name value namehash
))
234 (defmethod srecode-dictionary-add-section-dictionary ((dict srecode-dictionary
)
235 name
&optional show-only
)
236 "In dictionary DICT, add a section dictionary for section macro NAME.
237 Return the new dictionary.
239 You can add several dictionaries to the same section macro.
240 For each dictionary added to a macro, the block of codes in the
241 template will be repeated.
243 If optional argument SHOW-ONLY is non-nil, then don't add a new dictionary
244 if there is already one in place. Also, don't add FIRST/LAST entries.
245 These entries are not needed when we are just showing a section.
247 Each dictionary added will automatically get values for positional macros
248 which will enable SECTIONS to be enabled.
250 * FIRST - The first entry in the table.
251 * NOTFIRST - Not the first entry in the table.
252 * LAST - The last entry in the table
253 * NOTLAST - Not the last entry in the table.
255 Adding a new dictionary will alter these values in previously
256 inserted dictionaries."
258 (if (not (stringp name
))
259 (signal 'wrong-type-argument
(list name
'stringp
)))
260 (let ((new (srecode-create-dictionary dict
))
261 (ov (srecode-dictionary-lookup-name dict name
)))
263 (when (not show-only
)
264 ;; Setup the FIRST/NOTFIRST and LAST/NOTLAST entries.
267 (srecode-dictionary-show-section new
"FIRST")
268 (srecode-dictionary-show-section new
"LAST"))
269 ;; Not the very first one. Lets clean up CAR.
270 (let ((tail (car (last ov
))))
271 (srecode-dictionary-hide-section tail
"LAST")
272 (srecode-dictionary-show-section tail
"NOTLAST")
274 (srecode-dictionary-show-section new
"NOTFIRST")
275 (srecode-dictionary-show-section new
"LAST"))
278 (when (or (not show-only
) (null ov
))
279 (srecode-dictionary-set-value dict name
(append ov
(list new
))))
280 ;; Return the new sub-dictionary.
283 (defmethod srecode-dictionary-show-section ((dict srecode-dictionary
) name
)
284 "In dictionary DICT, indicate that the section NAME should be exposed."
286 (if (not (stringp name
))
287 (signal 'wrong-type-argument
(list name
'stringp
)))
288 ;; Showing a section is just like making a section dictionary, but
289 ;; with no dictionary values to add.
290 (srecode-dictionary-add-section-dictionary dict name t
)
293 (defmethod srecode-dictionary-hide-section ((dict srecode-dictionary
) name
)
294 "In dictionary DICT, indicate that the section NAME should be hidden."
295 ;; We need to find the has value, and then delete it.
297 (if (not (stringp name
))
298 (signal 'wrong-type-argument
(list name
'stringp
)))
300 (with-slots (namehash) dict
301 (remhash name namehash
))
304 (defmethod srecode-dictionary-merge ((dict srecode-dictionary
) otherdict
)
305 "Merge into DICT the dictionary entries from OTHERDICT."
309 ;; Only merge in the new values if there was no old value.
310 ;; This protects applications from being whacked, and basically
311 ;; makes these new section dictionary entries act like
312 ;; "defaults" instead of overrides.
313 (when (not (srecode-dictionary-lookup-name dict key
))
314 (cond ((and (listp entry
) (srecode-dictionary-p (car entry
)))
315 ;; A list of section dictionaries.
316 ;; We need to merge them in.
319 (srecode-dictionary-add-section-dictionary
321 (srecode-dictionary-merge new-sub-dict
(car entry
)))
322 (setq entry
(cdr entry
)))
326 (srecode-dictionary-set-value dict key entry
)))
328 (oref otherdict namehash
))))
330 (defmethod srecode-dictionary-lookup-name ((dict srecode-dictionary
)
332 "Return information about the current DICT's value for NAME."
333 (if (not (slot-boundp dict
'namehash
))
335 ;; Get the value of this name from the dictionary
336 (or (with-slots (namehash) dict
337 (gethash name namehash
))
338 (and (not (member name
'("FIRST" "LAST" "NOTFIRST" "NOTLAST")))
340 (srecode-dictionary-lookup-name (oref dict parent
) name
))
343 (defmethod srecode-root-dictionary ((dict srecode-dictionary
))
344 "For dictionary DICT, return the root dictionary.
345 The root dictionary is usually for a current or active insertion."
347 (while (oref ans parent
)
348 (setq ans
(oref ans parent
)))
351 ;;; COMPOUND VALUE METHODS
353 ;; Compound values must provide at least the toStriong method
354 ;; for use in converting the compound value into sometehing insertable.
356 (defmethod srecode-compound-toString ((cp srecode-dictionary-compound-value
)
359 "Convert the compound dictionary value CP to a string.
360 If FUNCTION is non-nil, then FUNCTION is somehow applied to an aspect
361 of the compound value. The FUNCTION could be a fraction
362 of some function symbol with a logical prefix excluded.
364 If you subclass `srecode-dictionary-compound-value' then this
365 method could return nil, but if it does that, it must insert
366 the value itself using `princ', or by detecting if the current
367 standard out is a buffer, and using `insert'."
370 (defmethod srecode-dump ((cp srecode-dictionary-compound-value
)
372 "Display information about this compound value."
373 (princ (object-name cp
))
376 (defmethod srecode-compound-toString ((cp srecode-dictionary-compound-variable
)
379 "Convert the compound dictionary variable value CP into a string.
380 FUNCTION and DICTIONARY are as for the baseclass."
381 (require 'srecode
/insert
)
382 (srecode-insert-code-stream (oref cp compiled
) dictionary
))
385 (defmethod srecode-dump ((cp srecode-dictionary-compound-variable
)
387 "Display information about this compound value."
388 (require 'srecode
/compile
)
389 (princ "# Compound Variable #\n")
390 (let ((indent (+ 4 (or indent
0)))
391 (cmp (oref cp compiled
))
393 (srecode-dump-code-list cmp
(make-string indent ?
))
396 ;;; FIELD EDITING COMPOUND VALUE
398 ;; This is an interface to using field-editing objects
399 ;; instead of asking questions. This provides the basics
400 ;; behind this compound value.
402 (defclass srecode-field-value
(srecode-dictionary-compound-value)
403 ((firstinserter :initarg
:firstinserter
405 "The inserter object for the first occurrence of this field.")
406 (defaultvalue :initarg
:defaultvalue
408 "The default value for this inserter.")
410 "When inserting values with editable field mode, a dictionary value.
411 Compound values allow a field to be stored in the dictionary for when
412 it is referenced a second time. This compound value can then be
413 inserted with a new editable field.")
415 (defmethod srecode-compound-toString((cp srecode-field-value
)
418 "Convert this field into an insertable string."
419 (require 'srecode
/fields
)
420 ;; If we are not in a buffer, then this is not supported.
421 (when (not (bufferp standard-output
))
422 (error "FIELDS invoked while inserting template to non-buffer"))
425 (error "@todo: Cannot mix field insertion with functions")
427 ;; No function. Perform a plain field insertion.
428 ;; We know we are in a buffer, so we can perform the insertion.
429 (let* ((dv (oref cp defaultvalue
))
430 (sti (oref cp firstinserter
))
432 (name (oref sti
:object-name
)))
434 (if (or (not dv
) (string= dv
""))
438 (srecode-field name
:name name
441 :prompt
(oref sti prompt
)
442 :read-fcn
(oref sti read-fcn
)
445 ;; Returning nil is a signal that we have done the insertion ourselves.
449 ;;; Higher level dictionary functions
451 (defun srecode-create-section-dictionary (sectiondicts STATE
)
452 "Create a dictionary with section entries for a template.
453 The format for SECTIONDICTS is what is emitted from the template parsers.
454 STATE is the current compiler state."
456 (let ((new (srecode-create-dictionary t
)))
457 ;; Loop over each section. The section is a macro w/in the
460 (let* ((sect (car (car sectiondicts
)))
461 (entries (cdr (car sectiondicts
)))
462 (subdict (srecode-dictionary-add-section-dictionary new sect
))
464 ;; Loop over each entry. This is one variable in the
465 ;; section dictionary.
467 (let ((tname (semantic-tag-name (car entries
)))
468 (val (semantic-tag-variable-default (car entries
))))
470 (srecode-dictionary-show-section subdict tname
)
472 ((and (stringp (car val
))
474 (setq val
(car val
)))
476 (setq val
(srecode-dictionary-compound-variable
477 tname
:value val
:state STATE
))))
478 (srecode-dictionary-set-value
480 (setq entries
(cdr entries
))))
482 (setq sectiondicts
(cdr sectiondicts
)))
487 ;; Make a dictionary, and dump it's contents.
489 (defun srecode-adebug-dictionary ()
490 "Run data-debug on this mode's dictionary."
492 (require 'eieio-datadebug
)
494 (require 'srecode
/find
)
495 (let* ((modesym major-mode
)
496 (start (current-time))
497 (junk (or (progn (srecode-load-tables-for-mode modesym
)
498 (srecode-get-mode-table modesym
))
499 (error "No table found for mode %S" modesym
)))
500 (dict (srecode-create-dictionary (current-buffer)))
503 (message "Creating a dictionary took %.2f seconds."
504 (semantic-elapsed-time start end
))
505 (data-debug-new-buffer "*SRECODE ADEBUG*")
506 (data-debug-insert-object-slots dict
"*")))
508 (defun srecode-dictionary-dump ()
509 "Dump a typical fabricated dictionary."
511 (require 'srecode
/find
)
512 (let ((modesym major-mode
))
513 ;; This load allows the dictionary access to inherited
514 ;; and stacked dictionary entries.
515 (srecode-load-tables-for-mode modesym
)
516 (let ((tmp (srecode-get-mode-table modesym
))
519 (error "No table found for mode %S" modesym
))
520 ;; Now make the dictionary.
521 (let ((dict (srecode-create-dictionary (current-buffer))))
522 (with-output-to-temp-buffer "*SRECODE DUMP*"
523 (princ "DICTIONARY FOR ")
525 (princ "\n--------------------------------------------\n")
529 (defmethod srecode-dump ((dict srecode-dictionary
) &optional indent
)
531 (if (not indent
) (setq indent
0))
532 (maphash (lambda (key entry
)
533 (princ (make-string indent ?
))
537 (cond ((and (listp entry
)
538 (srecode-dictionary-p (car entry
)))
539 (let ((newindent (if indent
543 (princ " --> SUBDICTIONARY ")
544 (princ (object-name dict
))
546 (srecode-dump (car entry
) newindent
)
547 (setq entry
(cdr entry
))
551 ((srecode-dictionary-compound-value-child-p entry
)
552 (srecode-dump entry indent
)
561 (oref dict namehash
))
564 (provide 'srecode
/dictionary
)
566 ;; arch-tag: c664179c-171c-4709-9b56-d5a2fd30e457
567 ;;; srecode/dictionary.el ends here