Prefer directed to neutral quotes
[emacs.git] / lisp / cedet / srecode / dictionary.el
blobb95d45ebc864c45bfbdd75c21f488a6dafbcf350
1 ;;; srecode/dictionary.el --- Dictionary code for the semantic recoder.
3 ;; Copyright (C) 2007-2015 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/>.
22 ;;; Commentary:
24 ;; Dictionaries contain lists of names and their associated values.
25 ;; These dictionaries are used to fill in macros from recoder templates.
27 ;;; Code:
29 ;;; CLASSES
31 (eval-when-compile (require 'cl))
32 (require 'eieio)
33 (require 'cl-generic)
34 (require 'srecode)
35 (require 'srecode/table)
36 (eval-when-compile (require 'semantic))
38 (declare-function srecode-compile-parse-inserter "srecode/compile")
39 (declare-function srecode-dump-code-list "srecode/compile")
40 (declare-function srecode-load-tables-for-mode "srecode/find")
41 (declare-function srecode-template-table-in-project-p "srecode/find")
42 (declare-function srecode-insert-code-stream "srecode/insert")
43 (declare-function data-debug-new-buffer "data-debug")
44 (declare-function data-debug-insert-object-slots "eieio-datadebug")
45 (declare-function srecode-field "srecode/fields")
47 (defclass srecode-dictionary ()
48 ((namehash :initarg :namehash
49 :documentation
50 "Hash table containing the names of all the templates.")
51 (buffer :initarg :buffer
52 :documentation
53 "The buffer this dictionary was initialized with.")
54 (parent :initarg :parent
55 :type (or null srecode-dictionary)
56 :documentation
57 "The parent dictionary.
58 Symbols not appearing in this dictionary will be checked against the
59 parent dictionary.")
60 (origin :initarg :origin
61 :type string
62 :documentation
63 "A string representing the origin of this dictionary.
64 Useful only while debugging.")
66 "Dictionary of symbols and what they mean.
67 Dictionaries are used to look up named symbols from
68 templates to decide what to do with those symbols.")
70 (defclass srecode-dictionary-compound-value ()
72 "A compound dictionary value.
73 Values stored in a dictionary must be a STRING,
74 a dictionary for showing sections, or an instance of a subclass
75 of this class.
77 Compound dictionary values derive from this class, and must
78 provide a sequence of method implementations to convert into
79 a string."
80 :abstract t)
82 (defclass srecode-dictionary-compound-variable
83 (srecode-dictionary-compound-value)
84 ((value :initarg :value
85 :documentation
86 "The value of this template variable.
87 Variables in template files are usually a single string
88 which can be inserted into a dictionary directly.
90 Some variables may be more complex and involve dictionary
91 lookups, strings, concatenation, or the like.
93 The format of VALUE is determined by current template
94 formatting rules.")
95 (compiled :initarg :compiled
96 :type list
97 :documentation
98 "The compiled version of VALUE.")
100 "A compound dictionary value for template file variables.
101 You can declare a variable in a template like this:
103 set NAME \"str\" macro \"OTHERNAME\"
105 with appending various parts together in a list.")
107 (cl-defmethod initialize-instance ((this srecode-dictionary-compound-variable)
108 &optional fields)
109 "Initialize the compound variable THIS.
110 Makes sure that :value is compiled."
111 (let ((newfields nil)
112 (state nil))
113 (while fields
114 ;; Strip out :state
115 (if (eq (car fields) :state)
116 (setq state (car (cdr fields)))
117 (setq newfields (cons (car (cdr fields))
118 (cons (car fields) newfields))))
119 (setq fields (cdr (cdr fields))))
121 ;;(when (not state)
122 ;; (error "Cannot create compound variable outside of sectiondictionary"))
124 (cl-call-next-method this (nreverse newfields))
125 (when (not (slot-boundp this 'compiled))
126 (let ((val (oref this :value))
127 (comp nil))
128 (while val
129 (let ((nval (car val))
131 (cond ((stringp nval)
132 (setq comp (cons nval comp)))
133 ((and (listp nval)
134 (equal (car nval) 'macro))
135 (require 'srecode/compile)
136 (setq comp (cons
137 (srecode-compile-parse-inserter
138 (cdr nval)
139 state)
140 comp)))
142 (error "Don't know how to handle variable value %S" nval)))
144 (setq val (cdr val)))
145 (oset this :compiled (nreverse comp))))))
147 ;;; DICTIONARY METHODS
150 (defun srecode-create-dictionary (&optional buffer-or-parent)
151 "Create a dictionary for BUFFER.
152 If BUFFER-OR-PARENT is not specified, assume a buffer, and
153 use the current buffer.
154 If BUFFER-OR-PARENT is another dictionary, then remember the
155 parent within the new dictionary, and assume that BUFFER
156 is the same as belongs to the parent dictionary.
157 The dictionary is initialized with variables setup for that
158 buffer's table.
159 If BUFFER-OR-PARENT is t, then this dictionary should not be
160 associated with a buffer or parent."
161 (save-excursion
162 ;; Handle the parent
163 (let ((parent nil)
164 (buffer nil)
165 (origin nil)
166 (initfrombuff nil))
167 (cond
168 ;; Parent is a buffer
169 ((bufferp buffer-or-parent)
170 (set-buffer buffer-or-parent)
171 (setq buffer buffer-or-parent
172 origin (buffer-name buffer-or-parent)
173 initfrombuff t))
175 ;; Parent is another dictionary
176 ((srecode-dictionary-child-p buffer-or-parent)
177 (setq parent buffer-or-parent
178 buffer (oref buffer-or-parent buffer)
179 origin (concat (eieio-object-name buffer-or-parent) " in "
180 (if buffer (buffer-name buffer)
181 "no buffer")))
182 (when buffer
183 (set-buffer buffer)))
185 ;; No parent
186 ((eq buffer-or-parent t)
187 (setq buffer nil
188 origin "Unspecified Origin"))
190 ;; Default to unspecified parent
192 (setq buffer (current-buffer)
193 origin (concat "Unspecified. Assume "
194 (buffer-name buffer))
195 initfrombuff t)))
197 ;; Create the new dictionary object.
198 (let ((dict (make-instance
199 'srecode-dictionary
200 :buffer buffer
201 :parent parent
202 :namehash (make-hash-table :test 'equal
203 :size 20)
204 :origin origin)))
205 ;; Only set up the default variables if we are being built
206 ;; directly for a particular buffer.
207 (when initfrombuff
208 ;; Variables from the table we are inserting from.
209 ;; @todo - get a better tree of tables.
210 (let ((mt (srecode-get-mode-table major-mode))
211 (def (srecode-get-mode-table 'default)))
212 ;; Each table has multiple template tables.
213 ;; Do DEF first so that MT can override any values.
214 (srecode-dictionary-add-template-table dict def)
215 (srecode-dictionary-add-template-table dict mt)
217 dict))))
219 (cl-defmethod srecode-dictionary-add-template-table ((dict srecode-dictionary)
220 tpl)
221 "Insert into DICT the variables found in table TPL.
222 TPL is an object representing a compiled template file."
223 (when tpl
224 ;; Tables are sorted with highest priority first, useful for looking
225 ;; up templates, but this means we need to install the variables in
226 ;; reverse order so higher priority variables override lower ones.
227 (let ((tabs (reverse (oref tpl :tables))))
228 (require 'srecode/find) ; For srecode-template-table-in-project-p
229 (while tabs
230 (when (srecode-template-table-in-project-p (car tabs))
231 (let ((vars (oref (car tabs) variables)))
232 (while vars
233 (srecode-dictionary-set-value
234 dict (car (car vars)) (cdr (car vars)))
235 (setq vars (cdr vars)))))
236 (setq tabs (cdr tabs))))))
239 (cl-defmethod srecode-dictionary-set-value ((dict srecode-dictionary)
240 name value)
241 "In dictionary DICT, set NAME to have VALUE."
242 ;; Validate inputs
243 (unless (stringp name)
244 (signal 'wrong-type-argument (list name 'stringp)))
246 ;; Add the value.
247 (with-slots (namehash) dict
248 (puthash name value namehash))
251 (cl-defmethod srecode-dictionary-add-section-dictionary ((dict srecode-dictionary)
252 name &optional show-only force)
253 "In dictionary DICT, add a section dictionary for section macro NAME.
254 Return the new dictionary.
256 You can add several dictionaries to the same section entry.
257 For each dictionary added to a variable, the block of codes in
258 the template will be repeated.
260 If optional argument SHOW-ONLY is non-nil, then don't add a new dictionary
261 if there is already one in place. Also, don't add FIRST/LAST entries.
262 These entries are not needed when we are just showing a section.
264 Each dictionary added will automatically get values for positional macros
265 which will enable SECTIONS to be enabled.
267 * FIRST - The first entry in the table.
268 * NOTFIRST - Not the first entry in the table.
269 * LAST - The last entry in the table
270 * NOTLAST - Not the last entry in the table.
272 Adding a new dictionary will alter these values in previously
273 inserted dictionaries."
274 ;; Validate inputs
275 (unless (stringp name)
276 (signal 'wrong-type-argument (list name 'stringp)))
278 (let ((new (srecode-create-dictionary dict))
279 (ov (srecode-dictionary-lookup-name dict name t)))
281 (when (not show-only)
282 ;; Setup the FIRST/NOTFIRST and LAST/NOTLAST entries.
283 (if (null ov)
284 (progn
285 (srecode-dictionary-show-section new "FIRST")
286 (srecode-dictionary-show-section new "LAST"))
287 ;; Not the very first one. Let's clean up CAR.
288 (let ((tail (car (last ov))))
289 (srecode-dictionary-hide-section tail "LAST")
290 (srecode-dictionary-show-section tail "NOTLAST")
292 (srecode-dictionary-show-section new "NOTFIRST")
293 (srecode-dictionary-show-section new "LAST"))
296 (when (or force
297 (not show-only)
298 (null ov))
299 (srecode-dictionary-set-value dict name (append ov (list new))))
300 ;; Return the new sub-dictionary.
301 new))
303 (cl-defmethod srecode-dictionary-show-section ((dict srecode-dictionary) name)
304 "In dictionary DICT, indicate that the section NAME should be exposed."
305 ;; Validate inputs
306 (unless (stringp name)
307 (signal 'wrong-type-argument (list name 'stringp)))
309 ;; Showing a section is just like making a section dictionary, but
310 ;; with no dictionary values to add.
311 (srecode-dictionary-add-section-dictionary dict name t)
312 nil)
314 (cl-defmethod srecode-dictionary-hide-section ((dict srecode-dictionary) name)
315 "In dictionary DICT, indicate that the section NAME should be hidden."
316 ;; We need to find the has value, and then delete it.
317 ;; Validate inputs
318 (unless (stringp name)
319 (signal 'wrong-type-argument (list name 'stringp)))
321 ;; Add the value.
322 (with-slots (namehash) dict
323 (remhash name namehash))
324 nil)
326 (cl-defmethod srecode-dictionary-add-entries ((dict srecode-dictionary)
327 entries &optional state)
328 "Add ENTRIES to DICT.
330 ENTRIES is a list of even length of dictionary entries to
331 add. ENTRIES looks like this:
333 (NAME_1 VALUE_1 NAME_2 VALUE_2 ...)
335 The following rules apply:
336 * NAME_N is a string
337 and for values
338 * If VALUE_N is t, the section NAME_N is shown.
339 * If VALUE_N is a string, an ordinary value is inserted.
340 * If VALUE_N is a dictionary, it is inserted as entry NAME_N.
341 * Otherwise, a compound variable is created for VALUE_N.
343 The optional argument STATE has to non-nil when compound values
344 are inserted. An error is signaled if ENTRIES contains compound
345 values but STATE is nil."
346 (while entries
347 (let ((name (nth 0 entries))
348 (value (nth 1 entries)))
349 (cond
350 ;; Value is t; show a section.
351 ((eq value t)
352 (srecode-dictionary-show-section dict name))
354 ;; Value is a simple string; create an ordinary dictionary
355 ;; entry
356 ((stringp value)
357 (srecode-dictionary-set-value dict name value))
359 ;; Value is a dictionary; insert as child dictionary.
360 ((srecode-dictionary-child-p value)
361 (srecode-dictionary-merge
362 (srecode-dictionary-add-section-dictionary dict name)
363 value t))
365 ;; Value is some other object; create a compound value.
367 (unless state
368 (error "Cannot insert compound values without state."))
370 (srecode-dictionary-set-value
371 dict name
372 (srecode-dictionary-compound-variable
373 name :value value :state state)))))
374 (setq entries (nthcdr 2 entries)))
375 dict)
377 (cl-defmethod srecode-dictionary-merge ((dict srecode-dictionary) otherdict
378 &optional force)
379 "Merge into DICT the dictionary entries from OTHERDICT.
380 Unless the optional argument FORCE is non-nil, values in DICT are
381 not modified, even if there are values of the same names in
382 OTHERDICT."
383 (when otherdict
384 (maphash
385 (lambda (key entry)
386 ;; The new values is only merged in if there was no old value
387 ;; or FORCE is non-nil.
389 ;; This protects applications from being whacked, and basically
390 ;; makes these new section dictionary entries act like
391 ;; "defaults" instead of overrides.
392 (when (or force
393 (not (srecode-dictionary-lookup-name dict key t)))
394 (cond
395 ;; A list of section dictionaries. We need to merge them in.
396 ((and (listp entry)
397 (srecode-dictionary-p (car entry)))
398 (dolist (sub-dict entry)
399 (srecode-dictionary-merge
400 (srecode-dictionary-add-section-dictionary
401 dict key t t)
402 sub-dict force)))
404 ;; Other values can be set directly.
406 (srecode-dictionary-set-value dict key entry)))))
407 (oref otherdict namehash))))
409 (cl-defmethod srecode-dictionary-lookup-name ((dict srecode-dictionary)
410 name &optional non-recursive)
411 "Return information about DICT's value for NAME.
412 DICT is a dictionary, and NAME is a string that is treated as the
413 name of an entry in the dictionary. If such an entry exists, its
414 value is returned. Otherwise, nil is returned. Normally, the
415 lookup is recursive in the sense that the parent of DICT is
416 searched for NAME if it is not found in DICT. This recursive
417 lookup can be disabled by the optional argument NON-RECURSIVE.
419 This function derives values for some special NAMEs, such as
420 `FIRST' and `LAST'."
421 (if (not (slot-boundp dict 'namehash))
423 ;; Get the value of this name from the dictionary or its parent
424 ;; unless the lookup should be non-recursive.
425 (with-slots (namehash parent) dict
426 (or (gethash name namehash)
427 (and (not non-recursive)
428 (not (member name '("FIRST" "LAST" "NOTFIRST" "NOTLAST")))
429 parent
430 (srecode-dictionary-lookup-name parent name)))))
433 (cl-defmethod srecode-root-dictionary ((dict srecode-dictionary))
434 "For dictionary DICT, return the root dictionary.
435 The root dictionary is usually for a current or active insertion."
436 (let ((ans dict))
437 (while (oref ans parent)
438 (setq ans (oref ans parent)))
439 ans))
441 ;;; COMPOUND VALUE METHODS
443 ;; Compound values must provide at least the toString method
444 ;; for use in converting the compound value into something insertable.
446 (cl-defmethod srecode-compound-toString ((cp srecode-dictionary-compound-value)
447 function
448 dictionary)
449 "Convert the compound dictionary value CP to a string.
450 If FUNCTION is non-nil, then FUNCTION is somehow applied to an aspect
451 of the compound value. The FUNCTION could be a fraction
452 of some function symbol with a logical prefix excluded.
454 If you subclass `srecode-dictionary-compound-value' then this
455 method could return nil, but if it does that, it must insert
456 the value itself using `princ', or by detecting if the current
457 standard out is a buffer, and using `insert'."
458 (eieio-object-name cp))
460 (cl-defmethod srecode-dump ((cp srecode-dictionary-compound-value)
461 &optional indent)
462 "Display information about this compound value."
463 (princ (eieio-object-name cp))
466 (cl-defmethod srecode-compound-toString ((cp srecode-dictionary-compound-variable)
467 function
468 dictionary)
469 "Convert the compound dictionary variable value CP into a string.
470 FUNCTION and DICTIONARY are as for the baseclass."
471 (require 'srecode/insert)
472 (srecode-insert-code-stream (oref cp compiled) dictionary))
475 (cl-defmethod srecode-dump ((cp srecode-dictionary-compound-variable)
476 &optional indent)
477 "Display information about this compound value."
478 (require 'srecode/compile)
479 (princ "# Compound Variable #\n")
480 (let ((indent (+ 4 (or indent 0)))
481 (cmp (oref cp compiled))
483 (srecode-dump-code-list cmp (make-string indent ? ))
486 ;;; FIELD EDITING COMPOUND VALUE
488 ;; This is an interface to using field-editing objects
489 ;; instead of asking questions. This provides the basics
490 ;; behind this compound value.
492 (defclass srecode-field-value (srecode-dictionary-compound-value)
493 ((firstinserter :initarg :firstinserter
494 :documentation
495 "The inserter object for the first occurrence of this field.")
496 (defaultvalue :initarg :defaultvalue
497 :documentation
498 "The default value for this inserter.")
500 "When inserting values with editable field mode, a dictionary value.
501 Compound values allow a field to be stored in the dictionary for when
502 it is referenced a second time. This compound value can then be
503 inserted with a new editable field.")
505 (cl-defmethod srecode-compound-toString((cp srecode-field-value)
506 function
507 dictionary)
508 "Convert this field into an insertable string."
509 (require 'srecode/fields)
510 ;; If we are not in a buffer, then this is not supported.
511 (when (not (bufferp standard-output))
512 (error "FIELDS invoked while inserting template to non-buffer"))
514 (if function
515 (error "@todo: Cannot mix field insertion with functions")
517 ;; No function. Perform a plain field insertion.
518 ;; We know we are in a buffer, so we can perform the insertion.
519 (let* ((dv (oref cp defaultvalue))
520 (sti (oref cp firstinserter))
521 (start (point))
522 (name (oref sti :object-name)))
524 (cond
525 ;; No default value.
526 ((not dv) (insert name))
527 ;; A compound value as the default? Recurse.
528 ((srecode-dictionary-compound-value-child-p dv)
529 (srecode-compound-toString dv function dictionary))
530 ;; A string that is empty? Use the name.
531 ((and (stringp dv) (string= dv ""))
532 (insert name))
533 ;; Insert strings
534 ((stringp dv) (insert dv))
535 ;; Some other issue
537 (error "Unknown default value for value %S" name)))
539 ;; Create a field from the inserter.
540 (srecode-field name :name name
541 :start start
542 :end (point)
543 :prompt (oref sti prompt)
544 :read-fcn (oref sti read-fcn)
547 ;; Returning nil is a signal that we have done the insertion ourselves.
548 nil)
551 ;;; Higher level dictionary functions
553 (defun srecode-create-dictionaries-from-tags (tags state)
554 "Create a dictionary with entries according to TAGS.
556 TAGS should be in the format produced by the template file
557 grammar. That is
559 TAGS = (ENTRY_1 ENTRY_2 ...)
561 where
563 ENTRY_N = (NAME ENTRY_N_1 ENTRY_N_2 ...) | TAG
565 where TAG is a semantic tag of class 'variable. The (NAME ... )
566 form creates a child dictionary which is stored under the name
567 NAME. The TAG form creates a value entry or section dictionary
568 entry whose name is the name of the tag.
570 STATE is the current compiler state."
571 (let ((dict (srecode-create-dictionary t))
572 (entries (apply #'append
573 (mapcar
574 (lambda (entry)
575 (cond
576 ;; Entry is a tag
577 ((semantic-tag-p entry)
578 (let ((name (semantic-tag-name entry))
579 (value (semantic-tag-variable-default entry)))
580 (list name
581 (if (and (listp value)
582 (= (length value) 1)
583 (stringp (car value)))
584 (car value)
585 value))))
587 ;; Entry is a nested dictionary
589 (let ((name (car entry))
590 (entries (cdr entry)))
591 (list name
592 (srecode-create-dictionaries-from-tags
593 entries state))))))
594 tags))))
595 (srecode-dictionary-add-entries
596 dict entries state)
597 dict)
600 ;;; DUMP DICTIONARY
602 ;; Make a dictionary, and dump it's contents.
604 (defun srecode-adebug-dictionary ()
605 "Run data-debug on this mode's dictionary."
606 (interactive)
607 (require 'eieio-datadebug)
608 (require 'srecode/find)
609 (let* ((modesym major-mode)
610 (start (current-time))
611 (junk (or (progn (srecode-load-tables-for-mode modesym)
612 (srecode-get-mode-table modesym))
613 (error "No table found for mode %S" modesym)))
614 (dict (srecode-create-dictionary (current-buffer)))
615 (end (current-time))
617 (message "Creating a dictionary took %.2f seconds."
618 (semantic-elapsed-time start end))
619 (data-debug-new-buffer "*SRECODE ADEBUG*")
620 (data-debug-insert-object-slots dict "*")))
622 (defun srecode-dictionary-dump ()
623 "Dump a typical fabricated dictionary."
624 (interactive)
625 (require 'srecode/find)
626 (let ((modesym major-mode))
627 ;; This load allows the dictionary access to inherited
628 ;; and stacked dictionary entries.
629 (srecode-load-tables-for-mode modesym)
630 (let ((tmp (srecode-get-mode-table modesym))
632 (if (not tmp)
633 (error "No table found for mode %S" modesym))
634 ;; Now make the dictionary.
635 (let ((dict (srecode-create-dictionary (current-buffer))))
636 (with-output-to-temp-buffer "*SRECODE DUMP*"
637 (princ "DICTIONARY FOR ")
638 (princ major-mode)
639 (princ "\n--------------------------------------------\n")
640 (srecode-dump dict))
641 ))))
643 (cl-defmethod srecode-dump ((dict srecode-dictionary) &optional indent)
644 "Dump a dictionary."
645 (if (not indent) (setq indent 0))
646 (maphash (lambda (key entry)
647 (princ (make-string indent ? ))
648 (princ " ")
649 (princ key)
650 (princ " ")
651 (cond ((and (listp entry)
652 (srecode-dictionary-p (car entry)))
653 (let ((newindent (if indent
654 (+ indent 4)
655 4)))
656 (while entry
657 (princ " --> SUBDICTIONARY ")
658 (princ (eieio-object-name dict))
659 (princ "\n")
660 (srecode-dump (car entry) newindent)
661 (setq entry (cdr entry))
663 (princ "\n")
665 ((srecode-dictionary-compound-value-child-p entry)
666 (srecode-dump entry indent)
667 (princ "\n")
670 (prin1 entry)
671 ;(princ "\n")
673 (terpri)
675 (oref dict namehash))
678 (provide 'srecode/dictionary)
680 ;;; srecode/dictionary.el ends here