keywords.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004,
  2. * 2006, 2008, 2009, 2011, 2013, 2015 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <string.h>
  23. #include <stdarg.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/async.h"
  26. #include "libguile/ports.h"
  27. #include "libguile/root.h"
  28. #include "libguile/smob.h"
  29. #include "libguile/hashtab.h"
  30. #include "libguile/validate.h"
  31. #include "libguile/keywords.h"
  32. #include "libguile/strings.h"
  33. static SCM keyword_obarray;
  34. #define SCM_KEYWORDP(x) (SCM_HAS_TYP7 (x, scm_tc7_keyword))
  35. #define SCM_KEYWORD_SYMBOL(x) (SCM_CELL_OBJECT_1 (x))
  36. SCM_DEFINE (scm_keyword_p, "keyword?", 1, 0, 0,
  37. (SCM obj),
  38. "Return @code{#t} if the argument @var{obj} is a keyword, else\n"
  39. "@code{#f}.")
  40. #define FUNC_NAME s_scm_keyword_p
  41. {
  42. return scm_from_bool (SCM_KEYWORDP (obj));
  43. }
  44. #undef FUNC_NAME
  45. SCM_DEFINE (scm_symbol_to_keyword, "symbol->keyword", 1, 0, 0,
  46. (SCM symbol),
  47. "Return the keyword with the same name as @var{symbol}.")
  48. #define FUNC_NAME s_scm_symbol_to_keyword
  49. {
  50. SCM keyword;
  51. SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, 0, NULL, "symbol");
  52. SCM_CRITICAL_SECTION_START;
  53. /* Note: `scm_cell' and `scm_hashq_set_x' can raise an out-of-memory
  54. error. */
  55. keyword = scm_hashq_ref (keyword_obarray, symbol, SCM_BOOL_F);
  56. if (scm_is_false (keyword))
  57. {
  58. keyword = scm_cell (scm_tc7_keyword, SCM_UNPACK (symbol));
  59. scm_hashq_set_x (keyword_obarray, symbol, keyword);
  60. }
  61. SCM_CRITICAL_SECTION_END;
  62. return keyword;
  63. }
  64. #undef FUNC_NAME
  65. SCM_DEFINE (scm_keyword_to_symbol, "keyword->symbol", 1, 0, 0,
  66. (SCM keyword),
  67. "Return the symbol with the same name as @var{keyword}.")
  68. #define FUNC_NAME s_scm_keyword_to_symbol
  69. {
  70. SCM_VALIDATE_KEYWORD (1, keyword);
  71. return SCM_KEYWORD_SYMBOL (keyword);
  72. }
  73. #undef FUNC_NAME
  74. int
  75. scm_is_keyword (SCM val)
  76. {
  77. return SCM_KEYWORDP (val);
  78. }
  79. SCM
  80. scm_from_locale_keyword (const char *name)
  81. {
  82. return scm_symbol_to_keyword (scm_from_locale_symbol (name));
  83. }
  84. SCM
  85. scm_from_locale_keywordn (const char *name, size_t len)
  86. {
  87. return scm_symbol_to_keyword (scm_from_locale_symboln (name, len));
  88. }
  89. SCM
  90. scm_from_latin1_keyword (const char *name)
  91. {
  92. return scm_symbol_to_keyword (scm_from_latin1_symbol (name));
  93. }
  94. SCM
  95. scm_from_utf8_keyword (const char *name)
  96. {
  97. return scm_symbol_to_keyword (scm_from_utf8_symbol (name));
  98. }
  99. SCM_SYMBOL (scm_keyword_argument_error, "keyword-argument-error");
  100. void
  101. scm_c_bind_keyword_arguments (const char *subr, SCM rest,
  102. scm_t_keyword_arguments_flags flags, ...)
  103. {
  104. va_list va;
  105. if (SCM_UNLIKELY (!(flags & SCM_ALLOW_NON_KEYWORD_ARGUMENTS)
  106. && scm_ilength (rest) % 2 != 0))
  107. scm_error (scm_keyword_argument_error,
  108. subr, "Odd length of keyword argument list",
  109. SCM_EOL, SCM_BOOL_F);
  110. while (scm_is_pair (rest))
  111. {
  112. SCM kw_or_arg = SCM_CAR (rest);
  113. SCM tail = SCM_CDR (rest);
  114. if (scm_is_keyword (kw_or_arg) && scm_is_pair (tail))
  115. {
  116. SCM kw;
  117. SCM *arg_p;
  118. va_start (va, flags);
  119. for (;;)
  120. {
  121. kw = va_arg (va, SCM);
  122. if (SCM_UNBNDP (kw))
  123. {
  124. /* KW_OR_ARG is not in the list of expected keywords. */
  125. if (!(flags & SCM_ALLOW_OTHER_KEYS))
  126. scm_error_scm (scm_keyword_argument_error,
  127. scm_from_locale_string (subr),
  128. scm_from_latin1_string
  129. ("Unrecognized keyword"),
  130. SCM_EOL, scm_list_1 (kw_or_arg));
  131. break;
  132. }
  133. arg_p = va_arg (va, SCM *);
  134. if (scm_is_eq (kw_or_arg, kw))
  135. {
  136. /* We found the matching keyword. Store the
  137. associated value and break out of the loop. */
  138. *arg_p = SCM_CAR (tail);
  139. break;
  140. }
  141. }
  142. va_end (va);
  143. /* Advance REST. */
  144. rest = SCM_CDR (tail);
  145. }
  146. else
  147. {
  148. /* The next argument is not a keyword, or is a singleton
  149. keyword at the end of REST. */
  150. if (!(flags & SCM_ALLOW_NON_KEYWORD_ARGUMENTS))
  151. scm_error_scm (scm_keyword_argument_error,
  152. scm_from_locale_string (subr),
  153. scm_from_latin1_string ("Invalid keyword"),
  154. SCM_EOL, scm_list_1 (kw_or_arg));
  155. /* Advance REST. */
  156. rest = tail;
  157. }
  158. }
  159. }
  160. void
  161. scm_init_keywords ()
  162. {
  163. keyword_obarray = scm_c_make_hash_table (0);
  164. #include "libguile/keywords.x"
  165. }
  166. /*
  167. Local Variables:
  168. c-file-style: "gnu"
  169. End:
  170. */