1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "language/lexer/token.h"
25 #include "data/identifier.h"
26 #include "libpspp/assertion.h"
27 #include "libpspp/cast.h"
29 #include "gl/ftoastr.h"
30 #include "gl/xalloc.h"
32 /* Initializes TOKEN with an arbitrary type, number 0, and a null string. */
34 token_init (struct token *token)
38 token->string = ss_empty ();
41 /* Frees the string that TOKEN contains. */
43 token_destroy (struct token *token)
46 ss_dealloc (&token->string);
50 number_token_to_string (const struct token *token)
52 char buffer[DBL_BUFSIZE_BOUND];
54 dtoastr (buffer, sizeof buffer, 0, 0, fabs (token->number));
55 return (token->type == T_POS_NUM
57 : xasprintf ("-%s", buffer));
61 quoted_string_representation (struct substring ss, size_t n_quotes)
67 p = rep = xmalloc (1 + ss.length + n_quotes + 1 + 1);
69 for (i = 0; i < ss.length; i++)
71 uint8_t c = ss.string[i];
83 hex_string_representation (struct substring ss)
89 p = rep = xmalloc (2 + 2 * ss.length + 1 + 1);
92 for (i = 0; i < ss.length; i++)
94 static const char hex_digits[] = "0123456789abcdef";
95 uint8_t c = ss.string[i];
96 *p++ = hex_digits[c >> 4];
97 *p++ = hex_digits[c & 15];
106 string_representation (struct substring ss)
113 for (ofs = 0; ofs < ss.length; ofs += mblen)
117 mblen = u8_mbtoucr (&uc,
118 CHAR_CAST (const uint8_t *, ss.string + ofs),
120 if (mblen < 0 || !uc_is_print (uc))
121 return hex_string_representation (ss);
125 return quoted_string_representation (ss, n_quotes);
128 /* Returns a UTF-8 string that would yield TOKEN if it appeared in a syntax
129 file. The caller should free the returned string, with free(), when it is
132 The T_STOP token has no representation, so this function returns NULL. */
134 token_to_string (const struct token *token)
142 return number_token_to_string (token);
145 return ss_xstrdup (token->string);
148 return string_representation (token->string);
151 name = token_type_to_name (token->type);
152 return name != NULL ? xstrdup (name) : NULL;
156 /* Prints TOKEN on STREAM, for debugging. */
158 token_print (const struct token *token, FILE *stream)
160 fputs (token_type_to_name (token->type), stream);
161 if (token->type == T_POS_NUM || token->type == T_NEG_NUM
162 || token->number != 0.0)
164 char s[DBL_BUFSIZE_BOUND];
166 dtoastr (s, sizeof s, 0, 0, token->number);
167 fprintf (stream, "\t%s", s);
169 if (token->type == T_ID || token->type == T_STRING || token->string.length)
170 fprintf (stream, "\t\"%.*s\"",
171 (int) token->string.length, token->string.string);