Print the faulty object upon invalid-keyword errors.
[bpt/guile.git] / libguile / keywords.c
CommitLineData
e7efe8e7 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2008, 2009 Free Software Foundation, Inc.
fca75708 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
fca75708 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
fca75708 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
fca75708 20\f
dbb605f5
LC
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
fca75708 24
13070bd3 25#include <string.h>
a16d4e82 26#include <stdarg.h>
13070bd3 27
a0599745 28#include "libguile/_scm.h"
4e047c3e 29#include "libguile/async.h"
a0599745
MD
30#include "libguile/ports.h"
31#include "libguile/root.h"
32#include "libguile/smob.h"
00ffa0e7 33#include "libguile/hashtab.h"
a0599745
MD
34
35#include "libguile/validate.h"
36#include "libguile/keywords.h"
cc95e00a
MV
37#include "libguile/strings.h"
38
fca75708
MD
39\f
40
e7efe8e7
AW
41static SCM keyword_obarray;
42
92c2555f 43scm_t_bits scm_tc16_keyword;
e841c3e0 44
265a7997
MV
45#define KEYWORDP(X) (SCM_SMOB_PREDICATE (scm_tc16_keyword, (X)))
46#define KEYWORDSYM(X) (SCM_SMOB_OBJECT (X))
47
fca75708 48static int
e81d98ec 49keyword_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
fca75708
MD
50{
51 scm_puts ("#:", port);
265a7997 52 scm_display (KEYWORDSYM (exp), port);
fca75708
MD
53 return 1;
54}
55
265a7997
MV
56SCM_DEFINE (scm_keyword_p, "keyword?", 1, 0, 0,
57 (SCM obj),
58 "Return @code{#t} if the argument @var{obj} is a keyword, else\n"
59 "@code{#f}.")
60#define FUNC_NAME s_scm_keyword_p
61{
62 return scm_from_bool (KEYWORDP (obj));
63}
64#undef FUNC_NAME
65
66SCM_DEFINE (scm_symbol_to_keyword, "symbol->keyword", 1, 0, 0,
67 (SCM symbol),
68 "Return the keyword with the same name as @var{symbol}.")
69#define FUNC_NAME s_scm_symbol_to_keyword
fca75708 70{
86d31dfe 71 SCM keyword;
fca75708 72
d7212289 73 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, 0, NULL, "symbol");
fca75708 74
9de87eea 75 SCM_CRITICAL_SECTION_START;
a61b2054 76 /* njrev: NEWSMOB and hashq_set_x can raise errors */
e7efe8e7 77 keyword = scm_hashq_ref (keyword_obarray, symbol, SCM_BOOL_F);
7888309b 78 if (scm_is_false (keyword))
fca75708 79 {
54778cd3 80 SCM_NEWSMOB (keyword, scm_tc16_keyword, SCM_UNPACK (symbol));
e7efe8e7 81 scm_hashq_set_x (keyword_obarray, symbol, keyword);
fca75708 82 }
9de87eea 83 SCM_CRITICAL_SECTION_END;
86d31dfe 84 return keyword;
fca75708 85}
1bbd0b84 86#undef FUNC_NAME
fca75708 87
265a7997
MV
88SCM_DEFINE (scm_keyword_to_symbol, "keyword->symbol", 1, 0, 0,
89 (SCM keyword),
90 "Return the symbol with the same name as @var{keyword}.")
91#define FUNC_NAME s_scm_keyword_to_symbol
430a6cc3 92{
265a7997
MV
93 scm_assert_smob_type (scm_tc16_keyword, keyword);
94 return KEYWORDSYM (keyword);
430a6cc3 95}
265a7997 96#undef FUNC_NAME
430a6cc3 97
265a7997
MV
98int
99scm_is_keyword (SCM val)
fca75708 100{
265a7997 101 return KEYWORDP (val);
fca75708
MD
102}
103
265a7997 104SCM
c428e586 105scm_from_locale_keyword (const char *name)
fca75708 106{
c428e586 107 return scm_symbol_to_keyword (scm_from_locale_symbol (name));
fca75708 108}
fca75708 109
265a7997 110SCM
c428e586 111scm_from_locale_keywordn (const char *name, size_t len)
265a7997 112{
c428e586
MW
113 return scm_symbol_to_keyword (scm_from_locale_symboln (name, len));
114}
115
116SCM
117scm_from_latin1_keyword (const char *name)
118{
119 return scm_symbol_to_keyword (scm_from_latin1_symbol (name));
120}
121
122SCM
123scm_from_utf8_keyword (const char *name)
124{
125 return scm_symbol_to_keyword (scm_from_utf8_symbol (name));
265a7997 126}
fca75708 127
a16d4e82
MW
128SCM_SYMBOL (scm_keyword_argument_error, "keyword-argument-error");
129
130void
131scm_c_bind_keyword_arguments (const char *subr, SCM rest,
132 scm_t_keyword_arguments_flags flags, ...)
133{
134 va_list va;
135
136 if (SCM_UNLIKELY (!(flags & SCM_ALLOW_NON_KEYWORD_ARGUMENTS)
137 && scm_ilength (rest) % 2 != 0))
138 scm_error (scm_keyword_argument_error,
139 subr, "Odd length of keyword argument list",
140 SCM_EOL, SCM_BOOL_F);
141
142 while (scm_is_pair (rest))
143 {
144 SCM kw_or_arg = SCM_CAR (rest);
145 SCM tail = SCM_CDR (rest);
146
147 if (scm_is_keyword (kw_or_arg) && scm_is_pair (tail))
148 {
149 SCM kw;
150 SCM *arg_p;
151
152 va_start (va, flags);
153 for (;;)
154 {
155 kw = va_arg (va, SCM);
156 if (SCM_UNBNDP (kw))
157 {
158 /* KW_OR_ARG is not in the list of expected keywords. */
159 if (!(flags & SCM_ALLOW_OTHER_KEYS))
160 scm_error (scm_keyword_argument_error,
161 subr, "Unrecognized keyword",
162 SCM_EOL, SCM_BOOL_F);
163 break;
164 }
165 arg_p = va_arg (va, SCM *);
166 if (scm_is_eq (kw_or_arg, kw))
167 {
168 /* We found the matching keyword. Store the
169 associated value and break out of the loop. */
170 *arg_p = SCM_CAR (tail);
171 break;
172 }
173 }
174 va_end (va);
175
176 /* Advance REST. */
177 rest = SCM_CDR (tail);
178 }
179 else
180 {
181 /* The next argument is not a keyword, or is a singleton
182 keyword at the end of REST. */
183 if (!(flags & SCM_ALLOW_NON_KEYWORD_ARGUMENTS))
184 scm_error (scm_keyword_argument_error,
185 subr, "Invalid keyword",
186 SCM_EOL, SCM_BOOL_F);
187
188 /* Advance REST. */
189 rest = tail;
190 }
191 }
192}
193
a61b2054 194/* njrev: critical sections reviewed so far up to here */
fca75708
MD
195void
196scm_init_keywords ()
197{
e841c3e0 198 scm_tc16_keyword = scm_make_smob_type ("keyword", 0);
e841c3e0 199 scm_set_smob_print (scm_tc16_keyword, keyword_print);
f5f2dcff 200
e7efe8e7 201 keyword_obarray = scm_c_make_hash_table (0);
a0599745 202#include "libguile/keywords.x"
fca75708
MD
203}
204
89e00824
ML
205
206/*
207 Local Variables:
208 c-file-style: "gnu"
209 End:
210*/