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