Skip to content

Commit

Permalink
Show invalid escape sequences
Browse files Browse the repository at this point in the history
It is easier to debug when the message actually display the offending
escape sequence.
  • Loading branch information
wismill committed Sep 19, 2023
1 parent 6b7ed04 commit dd68f0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
28 changes: 16 additions & 12 deletions src/compose/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ lex(struct scanner *s, union lvalue *val)
while (!scanner_eof(s) && !scanner_eol(s) && scanner_peek(s) != '\"') {
if (scanner_chr(s, '\\')) {
uint8_t o;
size_t start_pos = s->pos;
if (scanner_chr(s, '\\')) {
scanner_buf_append(s, '\\');
}
Expand All @@ -184,24 +185,27 @@ lex(struct scanner *s, union lvalue *val)
if (scanner_hex(s, &o) && is_valid_char((char) o)) {
scanner_buf_append(s, (char) o);
} else {
// [TODO] actually show the sequence
scanner_warn_with_code(s,
XKB_WARNING_INVALID_ESCAPE_SEQUENCE,
"illegal hexadecimal escape sequence in string literal");
"illegal hexadecimal escape sequence (%.*s) in string literal",
(int) (s->pos - start_pos + 1), &s->s[start_pos - 1]);
}
}
else if (scanner_oct(s, &o)) {
if (is_valid_char((char) o)) {
scanner_buf_append(s, (char) o);
} else {
// [TODO] actually show the sequence
scanner_warn_with_code(s,
XKB_WARNING_INVALID_ESCAPE_SEQUENCE,
"illegal octal escape sequence in string literal");
}
else if (scanner_oct(s, &o) && is_valid_char((char) o)) {
scanner_buf_append(s, (char) o);
}
else if (s->pos > start_pos) {
scanner_warn_with_code(s,
XKB_WARNING_INVALID_ESCAPE_SEQUENCE,
"illegal octal escape sequence (%.*s) in string literal",
(int) (s->pos - start_pos + 1), &s->s[start_pos - 1]);
/* Ignore. */
}
else {
scanner_warn(s, "unknown escape sequence (%c) in string literal", scanner_peek(s));
scanner_warn_with_code(s,
XKB_WARNING_UNKNOWN_CHAR_ESCAPE_SEQUENCE,
"unknown escape sequence (\\%c) in string literal",
scanner_peek(s));
/* Ignore. */
}
} else {
Expand Down
26 changes: 13 additions & 13 deletions src/xkbcomp/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ _xkbcommon_lex(YYSTYPE *yylval, struct scanner *s)
while (!scanner_eof(s) && !scanner_eol(s) && scanner_peek(s) != '\"') {
if (scanner_chr(s, '\\')) {
uint8_t o;
size_t start_pos = s->pos;
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');
Expand All @@ -98,20 +99,19 @@ _xkbcommon_lex(YYSTYPE *yylval, struct scanner *s)
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, '\033');
else if (scanner_oct(s, &o)) {
if (is_valid_char((char) o)) {
scanner_buf_append(s, (char) o);
} else {
scanner_warn_with_code(s,
XKB_WARNING_INVALID_ESCAPE_SEQUENCE,
"invalid octal escape sequence: \\%o", o);
}
}
else if (scanner_oct(s, &o) && is_valid_char((char) o))
scanner_buf_append(s, (char) o);
else if (s->pos > start_pos)
scanner_warn_with_code(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 {
// TODO: display actual sequence! See: scanner_peek(s).
// require escaping any potential control character
scanner_warn_with_code(s, XKB_WARNING_UNKNOWN_CHAR_ESCAPE_SEQUENCE,
"unknown escape sequence in string literal");
scanner_warn_with_code(s,
XKB_WARNING_UNKNOWN_CHAR_ESCAPE_SEQUENCE,
"unknown escape sequence (\\%c) in string literal",
scanner_peek(s));
/* Ignore. */
}
} else {
Expand Down

0 comments on commit dd68f0f

Please sign in to comment.