Commit | Line | Data |
---|---|---|
dea4d055 MW |
1 | ;;; -*-lisp-*- |
2 | ;;; | |
3 | ;;; Implementation for property sets | |
4 | ;;; | |
5 | ;;; (c) 2009 Straylight/Edgeware | |
6 | ;;; | |
7 | ||
8 | ;;;----- Licensing notice --------------------------------------------------- | |
9 | ;;; | |
e0808c47 | 10 | ;;; This file is part of the Sensible Object Design, an object system for C. |
dea4d055 MW |
11 | ;;; |
12 | ;;; SOD is free software; you can redistribute it and/or modify | |
13 | ;;; it under the terms of the GNU General Public License as published by | |
14 | ;;; the Free Software Foundation; either version 2 of the License, or | |
15 | ;;; (at your option) any later version. | |
16 | ;;; | |
17 | ;;; SOD is distributed in the hope that it will be useful, | |
18 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;;; GNU General Public License for more details. | |
21 | ;;; | |
22 | ;;; You should have received a copy of the GNU General Public License | |
23 | ;;; along with SOD; if not, write to the Free Software Foundation, | |
24 | ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | ||
26 | (cl:in-package #:sod) | |
27 | ||
4ee476bc MW |
28 | ;;;-------------------------------------------------------------------------- |
29 | ;;; Conversion utilities. | |
30 | ||
31 | (defun string-to-symbol | |
32 | (string &key (package *package*) (swap-case t) (swap-hyphen t)) | |
33 | "Convert STRING to a symbol in PACKAGE. | |
34 | ||
35 | Parse off a `PACKAGE:' prefix from STRING if necessary, to identify the | |
36 | package; PACKAGE is used if there isn't a prefix. A doubled colon allows | |
37 | access to internal symbols, and will intern if necessary. Note that | |
38 | escape characters are /not/ processed; don't put colons in package names | |
39 | if you want to use them from SOD property sets. | |
40 | ||
41 | The portions of the string are modified by `frob-identifier'; the | |
42 | arguments SWAP-CASE and SWAP-HYPHEN are passed to `frob-identifier' to | |
43 | control this process." | |
44 | ||
45 | (let* ((length (length string)) | |
46 | (colon (position #\: string))) | |
47 | (multiple-value-bind (start internalp) | |
48 | (cond ((not colon) (values 0 t)) | |
49 | ((and (< (1+ colon) length) | |
50 | (char= (char string (1+ colon)) #\:)) | |
51 | (values (+ colon 2) t)) | |
52 | (t | |
53 | (values (1+ colon) nil))) | |
54 | (when colon | |
55 | (let* ((package-name (if (zerop colon) "KEYWORD" | |
56 | (frob-identifier (subseq string 0 colon) | |
57 | :swap-case swap-case | |
58 | :swap-hyphen swap-hyphen))) | |
59 | (found (find-package package-name))) | |
60 | (unless found | |
61 | (error "Unknown package `~A'" package-name)) | |
62 | (setf package found))) | |
63 | (let ((name (frob-identifier (subseq string start) | |
64 | :swap-case swap-case | |
65 | :swap-hyphen swap-hyphen))) | |
66 | (multiple-value-bind (symbol status) | |
67 | (funcall (if internalp #'intern #'find-symbol) name package) | |
68 | (cond ((or internalp (eq status :external)) | |
69 | symbol) | |
70 | ((not status) | |
71 | (error "Symbol `~A' not found in package `~A'" | |
72 | name (package-name package))) | |
73 | (t | |
74 | (error "Symbol `~A' not external in package `~A'" | |
75 | name (package-name package))))))))) | |
76 | ||
5abdef28 MW |
77 | (let ((truish '("true" "t" "yes" "on" "verily")) |
78 | (falsish '("false" "nil" "no" "off" "nowise"))) | |
d448780e MW |
79 | (defun truishp (string) |
80 | "Convert STRING to a boolean." | |
5abdef28 MW |
81 | (cond ((member string truish :test #'string-equal) t) |
82 | ((member string falsish :test #'string-equal) nil) | |
d448780e MW |
83 | (t (error "Unrecognized boolean value `~A'" string))))) |
84 | ||
dea4d055 MW |
85 | ;;;-------------------------------------------------------------------------- |
86 | ;;; Property representation. | |
87 | ||
88 | (defmethod file-location ((prop property)) | |
89 | (file-location (p-location prop))) | |
90 | ||
48eb81ca MW |
91 | ;;; Input conversions. |
92 | ||
93 | (defmethod decode-property ((raw symbol)) (values :symbol raw)) | |
94 | (defmethod decode-property ((raw integer)) (values :int raw)) | |
95 | (defmethod decode-property ((raw string)) (values :string raw)) | |
96 | (defmethod decode-property ((raw character)) (values :char raw)) | |
97 | (defmethod decode-property ((raw function)) (values :func raw)) | |
98 | (defmethod decode-property ((raw c-type)) (values :type raw)) | |
99 | ||
dea4d055 MW |
100 | ;;; Keywords. |
101 | ||
102 | (defmethod coerce-property-value | |
103 | ((value symbol) (type (eql :symbol)) (wanted (eql :keyword))) | |
104 | value) | |
105 | ||
106 | (defmethod coerce-property-value | |
107 | ((value string) (type (eql :id)) (wanted (eql :keyword))) | |
108 | (string-to-symbol value :package :keyword)) | |
109 | ||
110 | (defmethod coerce-property-value | |
111 | ((value string) (type (eql :string)) (wanted (eql :keyword))) | |
112 | (string-to-symbol value :package :keyword :swap-hyphen nil)) | |
113 | ||
114 | ;;; Symbols. | |
115 | ||
116 | (defmethod coerce-property-value | |
117 | ((value string) (type (eql :id)) (wanted (eql :symbol))) | |
118 | (string-to-symbol value)) | |
119 | ||
120 | (defmethod coerce-property-value | |
121 | ((value string) (type (eql :string)) (wanted (eql :symbol))) | |
122 | (string-to-symbol value :swap-hyphen nil)) | |
123 | ||
124 | ;;; Identifiers. | |
125 | ||
126 | (defmethod coerce-property-value | |
127 | ((value string) (type (eql :string)) (wanted (eql :id))) | |
128 | value) | |
129 | ||
130 | (defmethod coerce-property-value | |
131 | ((value symbol) (type (eql :symbol)) (wanted (eql :id))) | |
132 | (frob-identifier (symbol-name value))) | |
133 | ||
d448780e MW |
134 | ;;; Boolean. |
135 | ||
136 | (defmethod coerce-property-value | |
137 | ((value symbol) (type (eql :symbol)) (wanted (eql :boolean))) | |
138 | value) | |
139 | ||
140 | (defmethod coerce-property-value | |
141 | ((value string) (type (eql :id)) (wanted (eql :boolean))) | |
142 | (truishp value)) | |
143 | ||
144 | (defmethod coerce-property-value | |
145 | ((value integer) (type (eql :int)) (wanted (eql :boolean))) | |
146 | (not (zerop value))) | |
147 | ||
6ee19709 MW |
148 | ;;; Types. |
149 | ||
150 | (defmethod coerce-property-value | |
151 | ((value string) (type (eql :id)) (wanted (eql :type))) | |
ff344fb7 MW |
152 | (or (and (boundp '*module-type-map*) |
153 | (gethash value *module-type-map*)) | |
14adef2f | 154 | (find-simple-c-type value) |
a1985b3c | 155 | (error "Unknown type `~A'" value))) |
6ee19709 | 156 | |
dea4d055 MW |
157 | ;;;-------------------------------------------------------------------------- |
158 | ;;; Property sets. | |
159 | ||
160 | (defmethod print-object ((pset pset) stream) | |
161 | (print-unreadable-object (pset stream :type t) | |
162 | (pprint-logical-block (stream nil) | |
163 | (let ((firstp t)) | |
164 | (pset-map (lambda (prop) | |
165 | (cond (firstp (setf firstp nil)) | |
166 | (t (write-char #\space stream) | |
167 | (pprint-newline :linear stream))) | |
168 | (format stream "~:@<~S ~@_~S ~@_~S~:>" | |
169 | (p-name prop) (p-type prop) (p-value prop))) | |
170 | pset))))) | |
171 | ||
172 | ;;;----- That's all, folks -------------------------------------------------- |