| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* write_symbol.d - print a symbol |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <ecl/ecl.h> |
| 16 |
#include <ecl/internal.h> |
| 17 |
|
| 18 |
static bool |
| 19 |
potential_number_p(cl_object s, int base) |
| 20 |
{ |
| 21 |
/* See ANSI 2.3.1.1 */ |
| 22 |
cl_index i, l; |
| 23 |
ecl_character c; |
| 24 |
/* A potential number must contain at least one digit */ |
| 25 |
bool some_digit = FALSE; |
| 26 |
|
| 27 |
l = s->base_string.fillp; |
| 28 |
if (l == 0) |
| 29 |
return FALSE; |
| 30 |
c = ecl_char(s, 0); |
| 31 |
|
| 32 |
/* A potential number must begin with a digit, sign or |
| 33 |
extension character (^ _) */ |
| 34 |
if (ecl_digitp(c,base) >= 0) |
| 35 |
some_digit = TRUE; |
| 36 |
else if (c != '+' && c != '-' && c != '^' && c != '_') |
| 37 |
return FALSE; |
| 38 |
|
| 39 |
/* A potential number cannot end with a sign */ |
| 40 |
c = ecl_char(s, l-1); |
| 41 |
if (c == '+' || c == '-') |
| 42 |
return FALSE; |
| 43 |
|
| 44 |
for (i = 1; i < l; i++) { |
| 45 |
c = ecl_char(s, i); |
| 46 |
/* It can only contain digits, signs, ratio markers, |
| 47 |
* extension characters and number markers. Number |
| 48 |
* markers are letters, but two adjacent letters fail |
| 49 |
* to be a number marker. */ |
| 50 |
if (ecl_digitp(c, base) >= 0) { |
| 51 |
some_digit = TRUE; |
| 52 |
} else if (c == '+' || c == '-' || |
| 53 |
c == '/' || c == '.' || c == '^' || c == '_') { |
| 54 |
continue; |
| 55 |
} else if (ecl_alpha_char_p(c) && |
| 56 |
(((i+1) >= l) || !ecl_alpha_char_p(ecl_char(s, i+1)))) { |
| 57 |
continue; |
| 58 |
} else { |
| 59 |
return FALSE; |
| 60 |
} |
| 61 |
} |
| 62 |
return some_digit; |
| 63 |
} |
| 64 |
|
| 65 |
#define needs_to_be_inverted(s) (ecl_string_case(s) != 0) |
| 66 |
|
| 67 |
static bool |
| 68 |
all_dots(cl_object s) |
| 69 |
{ |
| 70 |
cl_index i; |
| 71 |
for (i = 0; i < s->base_string.fillp; i++) |
| 72 |
if (ecl_char(s, i) != '.') |
| 73 |
return 0; |
| 74 |
return 1; |
| 75 |
} |
| 76 |
|
| 77 |
static bool |
| 78 |
needs_to_be_escaped(cl_object s, cl_object readtable, cl_object print_case) |
| 79 |
{ |
| 80 |
int action = readtable->readtable.read_case; |
| 81 |
cl_index i; |
| 82 |
if (potential_number_p(s, ecl_print_base())) |
| 83 |
return 1; |
| 84 |
/* The value of *PRINT-ESCAPE* is T. We need to check whether the |
| 85 |
* symbol name S needs to be escaped. This will happen if it has some |
| 86 |
* strange character, or if it has a lowercase character (because such |
| 87 |
* a character cannot be read with the standard readtable) or if the |
| 88 |
* string has to be escaped according to readtable case and the rules |
| 89 |
* of 22.1.3.3.2. */ |
| 90 |
for (i = 0; i < s->base_string.fillp; i++) { |
| 91 |
int c = ecl_char(s, i); |
| 92 |
int syntax = ecl_readtable_get(readtable, c, 0); |
| 93 |
if (syntax != cat_constituent || |
| 94 |
ecl_invalid_character_p(c) || |
| 95 |
(c) == ':') |
| 96 |
return 1; |
| 97 |
if ((action == ecl_case_downcase) && ecl_upper_case_p(c)) |
| 98 |
return 1; |
| 99 |
if (ecl_lower_case_p(c)) |
| 100 |
return 1; |
| 101 |
} |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
static inline void |
| 106 |
buffer_write_char(ecl_character c, cl_object buffer, cl_object stream, cl_index *buffer_ndx, cl_index buffer_size) { |
| 107 |
ecl_char_set(buffer, (*buffer_ndx)++, c); |
| 108 |
if (*buffer_ndx >= buffer_size) { |
| 109 |
si_fill_pointer_set(buffer, ecl_make_fixnum(buffer_size)); |
| 110 |
si_do_write_sequence(buffer, stream, ecl_make_fixnum(0), ECL_NIL); |
| 111 |
*buffer_ndx = 0; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
static void |
| 116 |
write_symbol_string(cl_object s, int action, cl_object print_case, |
| 117 |
cl_object stream, bool escape) |
| 118 |
{ |
| 119 |
cl_index i; |
| 120 |
bool capitalize; |
| 121 |
if (action == ecl_case_invert) { |
| 122 |
if (!needs_to_be_inverted(s)) |
| 123 |
action = ecl_case_preserve; |
| 124 |
} |
| 125 |
cl_object buffer = si_get_buffer_string(); |
| 126 |
cl_index buffer_size = ecl_fixnum(cl_array_total_size(buffer)); |
| 127 |
cl_index buffer_ndx = 0; |
| 128 |
if (escape) |
| 129 |
buffer_write_char('|', buffer, stream, &buffer_ndx, buffer_size); |
| 130 |
capitalize = 1; |
| 131 |
for (i = 0; i < s->base_string.fillp; i++) { |
| 132 |
ecl_character c = ecl_char(s, i); |
| 133 |
if (escape) { |
| 134 |
if (c == '|' || c == '\\') { |
| 135 |
buffer_write_char('\\', buffer, stream, &buffer_ndx, buffer_size); |
| 136 |
} |
| 137 |
} else if (action != ecl_case_preserve) { |
| 138 |
if (ecl_upper_case_p(c)) { |
| 139 |
if ((action == ecl_case_invert) || |
| 140 |
((action == ecl_case_upcase) && |
| 141 |
((print_case == @':downcase') || |
| 142 |
((print_case == @':capitalize') && !capitalize)))) |
| 143 |
{ |
| 144 |
c = ecl_char_downcase(c); |
| 145 |
} |
| 146 |
capitalize = 0; |
| 147 |
} else if (ecl_lower_case_p(c)) { |
| 148 |
if ((action == ecl_case_invert) || |
| 149 |
((action == ecl_case_downcase) && |
| 150 |
((print_case == @':upcase') || |
| 151 |
((print_case == @':capitalize') && capitalize)))) |
| 152 |
{ |
| 153 |
c = ecl_char_upcase(c); |
| 154 |
} |
| 155 |
capitalize = 0; |
| 156 |
} else { |
| 157 |
capitalize = !ecl_alphanumericp(c); |
| 158 |
} |
| 159 |
} |
| 160 |
buffer_write_char(c, buffer, stream, &buffer_ndx, buffer_size); |
| 161 |
} |
| 162 |
if (escape) |
| 163 |
buffer_write_char('|', buffer, stream, &buffer_ndx, buffer_size); |
| 164 |
si_fill_pointer_set(buffer, ecl_make_fixnum(buffer_ndx)); |
| 165 |
si_do_write_sequence(buffer, stream, ecl_make_fixnum(0), ECL_NIL); |
| 166 |
si_put_buffer_string(buffer); |
| 167 |
} |
| 168 |
|
| 169 |
static bool |
| 170 |
forced_print_package(cl_object package) |
| 171 |
{ |
| 172 |
cl_object print_package = ecl_symbol_value(@'si::*print-package*'); |
| 173 |
return !Null(print_package) && (print_package != package); |
| 174 |
} |
| 175 |
|
| 176 |
void |
| 177 |
_ecl_write_symbol(cl_object x, cl_object stream) |
| 178 |
{ |
| 179 |
cl_object readtable = ecl_current_readtable(); |
| 180 |
cl_object print_case = ecl_print_case(); |
| 181 |
cl_object package; |
| 182 |
cl_object name; |
| 183 |
int intern_flag; |
| 184 |
bool print_readably = ecl_print_readably(); |
| 185 |
bool forced_package = 0; |
| 186 |
|
| 187 |
if (Null(x)) { |
| 188 |
package = ECL_NIL_SYMBOL->symbol.hpack; |
| 189 |
name = ECL_NIL_SYMBOL->symbol.name; |
| 190 |
} else { |
| 191 |
package = x->symbol.hpack; |
| 192 |
name = x->symbol.name; |
| 193 |
} |
| 194 |
|
| 195 |
if (!print_readably && !ecl_print_escape()) { |
| 196 |
write_symbol_string(name, readtable->readtable.read_case, |
| 197 |
print_case, stream, 0); |
| 198 |
return; |
| 199 |
} |
| 200 |
/* From here on, print-escape is true which means that it should |
| 201 |
* be possible to recover the same symbol by reading it with |
| 202 |
* the standard readtable (which has readtable-case = :UPCASE) |
| 203 |
*/ |
| 204 |
if (Null(package)) { |
| 205 |
if (print_readably || ecl_print_gensym()) |
| 206 |
writestr_stream("#:", stream); |
| 207 |
} else if (package == cl_core.keyword_package) { |
| 208 |
ecl_write_char(':', stream); |
| 209 |
} else if ((forced_package = forced_print_package(package)) |
| 210 |
|| ecl_find_symbol(name, ecl_current_package(), &intern_flag) != x |
| 211 |
|| (intern_flag == 0)) |
| 212 |
{ |
| 213 |
cl_object name = package->pack.name; |
| 214 |
write_symbol_string(name, readtable->readtable.read_case, |
| 215 |
print_case, stream, |
| 216 |
needs_to_be_escaped(name, readtable, print_case)); |
| 217 |
if (ecl_find_symbol(ecl_symbol_name(x), package, &intern_flag) != x) |
| 218 |
ecl_internal_error("can't print symbol"); |
| 219 |
if (intern_flag == ECL_INTERNAL || forced_package) { |
| 220 |
writestr_stream("::", stream); |
| 221 |
} else if (intern_flag == ECL_EXTERNAL) { |
| 222 |
ecl_write_char(':', stream); |
| 223 |
} else { |
| 224 |
FEerror("Pathological symbol --- cannot print.", 0); |
| 225 |
} |
| 226 |
} |
| 227 |
write_symbol_string(name, readtable->readtable.read_case, print_case, stream, |
| 228 |
needs_to_be_escaped(name, readtable, print_case) || |
| 229 |
all_dots(name)); |
| 230 |
} |