| 1 |
/* |
| 2 |
* Copyright © 2012 Ran Benita <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include <assert.h> |
| 9 |
#include <stdbool.h> |
| 10 |
#include <stdint.h> |
| 11 |
|
| 12 |
#include "scanner-utils.h" |
| 13 |
#include "xkbcomp-priv.h" |
| 14 |
#include "parser-priv.h" |
| 15 |
|
| 16 |
const char DECIMAL_SEPARATOR = '.'; |
| 17 |
|
| 18 |
static bool |
| 19 |
number(struct scanner *s, int64_t *out, int *out_tok) |
| 20 |
{ |
| 21 |
if (scanner_lit(s, "0x")) { |
| 22 |
switch (scanner_hex_int64(s, out)) { |
| 23 |
case -1: |
| 24 |
*out_tok = ERROR_TOK; |
| 25 |
return true; |
| 26 |
case 0: |
| 27 |
return false; |
| 28 |
default: |
| 29 |
*out_tok = INTEGER; |
| 30 |
return true; |
| 31 |
} |
| 32 |
} |
| 33 |
else { |
| 34 |
bool is_digit = false; |
| 35 |
switch (scanner_dec_int64(s, out)) { |
| 36 |
case -1: |
| 37 |
*out_tok = ERROR_TOK; |
| 38 |
return true; |
| 39 |
case 0: |
| 40 |
return false; |
| 41 |
case 1: |
| 42 |
is_digit = true; |
| 43 |
break; |
| 44 |
default: |
| 45 |
; |
| 46 |
} |
| 47 |
if (scanner_chr(s, DECIMAL_SEPARATOR)) { |
| 48 |
/* |
| 49 |
* Since the parser does not use floats, we do not care of the |
| 50 |
* actual floating-point number value: we just need to ensure that |
| 51 |
* the syntax is correct. So just truncate it! |
| 52 |
* |
| 53 |
* Note that `strtold` would not work reliably in our context, |
| 54 |
* because it depends on the locale for e.g. the decimal separator |
| 55 |
* (e.g. a period or a comma) and requires a null-terminated string, |
| 56 |
* which we cannot guarantee. |
| 57 |
*/ |
| 58 |
int64_t dec; |
| 59 |
if (scanner_dec_int64(s, &dec) < 0) { |
| 60 |
*out_tok = ERROR_TOK; |
| 61 |
return true; |
| 62 |
} |
| 63 |
*out_tok = FLOAT; |
| 64 |
} else if (is_digit) { |
| 65 |
*out_tok = DECIMAL_DIGIT; |
| 66 |
} else { |
| 67 |
*out_tok = INTEGER; |
| 68 |
} |
| 69 |
return true; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
int |
| 74 |
_xkbcommon_lex(YYSTYPE *yylval, struct scanner *s) |
| 75 |
{ |
| 76 |
skip_more_whitespace_and_comments: |
| 77 |
/* Skip spaces. */ |
| 78 |
while (is_space(scanner_peek(s))) scanner_next(s); |
| 79 |
|
| 80 |
/* |
| 81 |
* Skip U+200E LEFT-TO-RIGHT MARK and U+200F RIGHT-TO-LEFT MARK, assuming |
| 82 |
* UTF-8 encoding. These Unicode code points are useful for forcing the text |
| 83 |
* directionality when displaying/editing an XKB file. |
| 84 |
*/ |
| 85 |
if (scanner_lit(s, u8"\u200E") || scanner_lit(s, u8"\u200F")) |
| 86 |
goto skip_more_whitespace_and_comments; |
| 87 |
|
| 88 |
/* Skip comments. */ |
| 89 |
if (scanner_lit(s, "//") || scanner_chr(s, '#')) { |
| 90 |
scanner_skip_to_eol(s); |
| 91 |
goto skip_more_whitespace_and_comments; |
| 92 |
} |
| 93 |
|
| 94 |
/* See if we're done. */ |
| 95 |
if (scanner_eof(s)) return END_OF_FILE; |
| 96 |
|
| 97 |
/* New token. */ |
| 98 |
s->token_pos = s->pos; |
| 99 |
s->buf_pos = 0; |
| 100 |
|
| 101 |
/* String literal. */ |
| 102 |
if (scanner_chr(s, '\"')) { |
| 103 |
while (!scanner_eof(s) && !scanner_eol(s) && scanner_peek(s) != '\"') { |
| 104 |
if (scanner_chr(s, '\\')) { |
| 105 |
uint8_t o; |
| 106 |
const size_t start_pos = s->pos; |
| 107 |
if (scanner_chr(s, '\\')) scanner_buf_append(s, '\\'); |
| 108 |
else if (scanner_chr(s, '"')) scanner_buf_append(s, '"'); |
| 109 |
else if (scanner_chr(s, 'n')) scanner_buf_append(s, '\n'); |
| 110 |
else if (scanner_chr(s, 't')) scanner_buf_append(s, '\t'); |
| 111 |
else if (scanner_chr(s, 'r')) scanner_buf_append(s, '\r'); |
| 112 |
else if (scanner_chr(s, 'b')) scanner_buf_append(s, '\b'); |
| 113 |
else if (scanner_chr(s, 'f')) scanner_buf_append(s, '\f'); |
| 114 |
else if (scanner_chr(s, 'v')) scanner_buf_append(s, '\v'); |
| 115 |
else if (scanner_chr(s, 'e')) scanner_buf_append(s, '\x1b'); |
| 116 |
else if (scanner_chr(s, 'u')) { |
| 117 |
/* Unicode escape sequence */ |
| 118 |
uint32_t cp = 0; |
| 119 |
if (scanner_unicode_code_point(s, &cp) && |
| 120 |
is_valid_char(cp)) { |
| 121 |
/* Encode code point using UTF-8 */ |
| 122 |
scanner_buf_appends_code_point(s, cp); |
| 123 |
} else { |
| 124 |
scanner_warn( |
| 125 |
s, XKB_WARNING_INVALID_UNICODE_ESCAPE_SEQUENCE, |
| 126 |
"invalid Unicode escape sequence \"%.*s\" " |
| 127 |
"in string literal", |
| 128 |
(int) (s->pos - start_pos + 1), |
| 129 |
&s->s[start_pos - 1] |
| 130 |
); |
| 131 |
/* Ignore. */ |
| 132 |
} |
| 133 |
} |
| 134 |
else if (scanner_oct(s, &o) && is_valid_char((uint32_t) o)) |
| 135 |
scanner_buf_append(s, (char) o); |
| 136 |
else if (s->pos > start_pos) { |
| 137 |
scanner_warn(s, XKB_WARNING_INVALID_ESCAPE_SEQUENCE, |
| 138 |
"invalid octal escape sequence \"%.*s\" " |
| 139 |
"in string literal", |
| 140 |
(int) (s->pos - start_pos + 1), |
| 141 |
&s->s[start_pos - 1]); |
| 142 |
/* Ignore. */ |
| 143 |
} |
| 144 |
else { |
| 145 |
scanner_warn(s, XKB_WARNING_UNKNOWN_CHAR_ESCAPE_SEQUENCE, |
| 146 |
"unknown escape sequence \"\\%c\" in string literal", |
| 147 |
scanner_peek(s)); |
| 148 |
/* Ignore. */ |
| 149 |
} |
| 150 |
} else { |
| 151 |
scanner_buf_append(s, scanner_next(s)); |
| 152 |
} |
| 153 |
} |
| 154 |
if (!scanner_buf_append(s, '\0') || !scanner_chr(s, '\"')) { |
| 155 |
scanner_err(s, XKB_LOG_MESSAGE_NO_ID, |
| 156 |
"unterminated string literal"); |
| 157 |
return ERROR_TOK; |
| 158 |
} |
| 159 |
yylval->str = strdup(s->buf); |
| 160 |
if (!yylval->str) |
| 161 |
return ERROR_TOK; |
| 162 |
return STRING; |
| 163 |
} |
| 164 |
|
| 165 |
/* Key name literal. */ |
| 166 |
if (scanner_chr(s, '<')) { |
| 167 |
while (is_graph(scanner_peek(s)) && scanner_peek(s) != '>') |
| 168 |
scanner_next(s); |
| 169 |
if (!scanner_chr(s, '>')) { |
| 170 |
scanner_err(s, XKB_LOG_MESSAGE_NO_ID, |
| 171 |
"unterminated key name literal"); |
| 172 |
return ERROR_TOK; |
| 173 |
} |
| 174 |
/* Empty key name literals are allowed. */ |
| 175 |
const char *start = s->s + s->token_pos + 1; |
| 176 |
const size_t len = s->pos - s->token_pos - 2; |
| 177 |
yylval->atom = xkb_atom_intern(s->ctx, start, len); |
| 178 |
return KEYNAME; |
| 179 |
} |
| 180 |
|
| 181 |
/* Operators and punctuation. */ |
| 182 |
if (scanner_chr(s, ';')) return SEMI; |
| 183 |
if (scanner_chr(s, '{')) return OBRACE; |
| 184 |
if (scanner_chr(s, '}')) return CBRACE; |
| 185 |
if (scanner_chr(s, '=')) return EQUALS; |
| 186 |
if (scanner_chr(s, '[')) return OBRACKET; |
| 187 |
if (scanner_chr(s, ']')) return CBRACKET; |
| 188 |
if (scanner_chr(s, '(')) return OPAREN; |
| 189 |
if (scanner_chr(s, ')')) return CPAREN; |
| 190 |
if (scanner_chr(s, '.')) return DOT; |
| 191 |
if (scanner_chr(s, ',')) return COMMA; |
| 192 |
if (scanner_chr(s, '+')) return PLUS; |
| 193 |
if (scanner_chr(s, '-')) return MINUS; |
| 194 |
if (scanner_chr(s, '*')) return TIMES; |
| 195 |
if (scanner_chr(s, '/')) return DIVIDE; |
| 196 |
if (scanner_chr(s, '!')) return EXCLAM; |
| 197 |
if (scanner_chr(s, '~')) return INVERT; |
| 198 |
|
| 199 |
int tok = ERROR_TOK; |
| 200 |
|
| 201 |
/* Identifier. */ |
| 202 |
if (is_alpha(scanner_peek(s)) || scanner_peek(s) == '_') { |
| 203 |
while (is_alnum(scanner_peek(s)) || scanner_peek(s) == '_') |
| 204 |
scanner_next(s); |
| 205 |
|
| 206 |
const char *start = s->s + s->token_pos; |
| 207 |
const size_t len = s->pos - s->token_pos; |
| 208 |
|
| 209 |
/* Keyword. */ |
| 210 |
tok = keyword_to_token(start, len); |
| 211 |
if (tok >= 0) return tok; |
| 212 |
|
| 213 |
yylval->sval = SVAL(start, len); |
| 214 |
return IDENT; |
| 215 |
} |
| 216 |
|
| 217 |
/* Number literal (hexadecimal / decimal / float). */ |
| 218 |
if (number(s, &yylval->num, &tok)) { |
| 219 |
if (tok == ERROR_TOK) { |
| 220 |
scanner_err(s, XKB_ERROR_MALFORMED_NUMBER_LITERAL, |
| 221 |
"malformed number literal"); |
| 222 |
return ERROR_TOK; |
| 223 |
} |
| 224 |
return tok; |
| 225 |
} |
| 226 |
|
| 227 |
scanner_err(s, XKB_LOG_MESSAGE_NO_ID, |
| 228 |
"unrecognized token"); |
| 229 |
return ERROR_TOK; |
| 230 |
} |
| 231 |
|
| 232 |
bool |
| 233 |
XkbParseStringInit(struct xkb_context *ctx, struct scanner *scanner, |
| 234 |
const char *string, size_t len, |
| 235 |
const char *file_name, const char *map) |
| 236 |
{ |
| 237 |
scanner_init(scanner, ctx, string, len, file_name, NULL); |
| 238 |
|
| 239 |
/* Basic detection of wrong character encoding. |
| 240 |
The first character relevant to the grammar must be ASCII: |
| 241 |
whitespace, section, comment */ |
| 242 |
if (!scanner_check_supported_char_encoding(scanner)) { |
| 243 |
scanner_err(scanner, XKB_ERROR_INVALID_FILE_ENCODING, |
| 244 |
"This could be a file encoding issue. " |
| 245 |
"Supported encodings must be backward compatible with ASCII."); |
| 246 |
scanner_err(scanner, XKB_ERROR_INVALID_FILE_ENCODING, |
| 247 |
"E.g. ISO/CEI 8859 and UTF-8 are supported " |
| 248 |
"but UTF-16, UTF-32 and CP1026 are not."); |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
return true; |
| 253 |
} |
| 254 |
|
| 255 |
XkbFile * |
| 256 |
XkbParseString(struct xkb_context *ctx, const char *string, size_t len, |
| 257 |
const char *file_name, const char *map) |
| 258 |
{ |
| 259 |
struct scanner scanner; |
| 260 |
if (!XkbParseStringInit(ctx, &scanner, string, len, file_name, map)) |
| 261 |
return NULL; |
| 262 |
|
| 263 |
return parse(ctx, &scanner, map); |
| 264 |
} |
| 265 |
|
| 266 |
bool |
| 267 |
XkbParseStringNext(struct xkb_context *ctx, struct scanner *scanner, |
| 268 |
const char *map, XkbFile **out) |
| 269 |
{ |
| 270 |
if (map) { |
| 271 |
*out = parse(ctx, scanner, map); |
| 272 |
return !!(*out); |
| 273 |
} else { |
| 274 |
return parse_next(ctx, scanner, out); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
XkbFile * |
| 279 |
XkbParseFile(struct xkb_context *ctx, FILE *file, |
| 280 |
const char *file_name, const char *map) |
| 281 |
{ |
| 282 |
bool ok; |
| 283 |
XkbFile *xkb_file; |
| 284 |
char *string; |
| 285 |
size_t size; |
| 286 |
|
| 287 |
ok = map_file(file, &string, &size); |
| 288 |
if (!ok) { |
| 289 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 290 |
"Couldn't read XKB file %s: %s\n", |
| 291 |
file_name, strerror(errno)); |
| 292 |
return NULL; |
| 293 |
} |
| 294 |
|
| 295 |
xkb_file = XkbParseString(ctx, string, size, file_name, map); |
| 296 |
unmap_file(string, size); |
| 297 |
return xkb_file; |
| 298 |
} |