Change how print(c) formats reported characters

Printable ASCII becomes single-quoted, using backslash
escapes if necessary. Unprintable characters use 0xNN
formatting, without quotes.
This commit is contained in:
Rangi
2021-04-09 14:20:54 -04:00
committed by Eldred Habert
parent 850c78aaf4
commit ab79e6bede
5 changed files with 26 additions and 18 deletions

View File

@@ -1172,7 +1172,7 @@ static void readLineContinuation(void)
} else if (c == ';') { } else if (c == ';') {
discardComment(); discardComment();
} else { } else {
error("Begun line continuation, but encountered character '%s'\n", error("Begun line continuation, but encountered character %s\n",
print(c)); print(c));
return; return;
} }
@@ -1631,7 +1631,7 @@ static void readString(void)
break; break;
default: default:
error("Illegal character escape '%s'\n", print(c)); error("Illegal character escape %s\n", print(c));
shiftChars(1); shiftChars(1);
break; break;
} }
@@ -1781,7 +1781,7 @@ static size_t appendStringLiteral(size_t i)
break; break;
default: default:
error("Illegal character escape '%s'\n", print(c)); error("Illegal character escape %s\n", print(c));
shiftChars(1); shiftChars(1);
break; break;
} }
@@ -2069,7 +2069,7 @@ static int yylex_NORMAL(void)
/* Do not report weird characters when capturing, it'll be done later */ /* Do not report weird characters when capturing, it'll be done later */
if (!lexerState->capturing) { if (!lexerState->capturing) {
/* TODO: try to group reportings */ /* TODO: try to group reportings */
error("Unknown character '%s'\n", print(c)); error("Unknown character %s\n", print(c));
} }
} }
lexerState->atLineStart = false; lexerState->atLineStart = false;
@@ -2157,7 +2157,7 @@ static int yylex_RAW(void)
*/ */
default: default:
error("Illegal character escape '%s'\n", print(c)); error("Illegal character escape %s\n", print(c));
break; break;
} }
/* fallthrough */ /* fallthrough */

View File

@@ -32,35 +32,43 @@ uint32_t calchash(const char *s)
char const *print(int c) char const *print(int c)
{ {
static char buf[5]; /* '\xNN' + '\0' */ // "'A'" + '\0': 4 bytes
// "'\\n'" + '\0': 5 bytes
// "0xFF" + '\0': 5 bytes
static char buf[5];
if (c == EOF) if (c == EOF)
return "EOF"; return "EOF";
if (isprint(c)) { if (isprint(c)) {
buf[0] = c; buf[0] = '\'';
buf[1] = '\0'; buf[1] = c;
buf[2] = '\'';
buf[3] = '\0';
return buf; return buf;
} }
buf[0] = '\\';
switch (c) { switch (c) {
case '\n': case '\n':
buf[1] = 'n'; buf[2] = 'n';
break; break;
case '\r': case '\r':
buf[1] = 'r'; buf[2] = 'r';
break; break;
case '\t': case '\t':
buf[1] = 't'; buf[2] = 't';
break; break;
default: /* Print as hex */ default: /* Print as hex */
buf[0] = '0';
buf[1] = 'x'; buf[1] = 'x';
sprintf(&buf[2], "%02hhx", (uint8_t)c); snprintf(&buf[2], 3, "%02hhX", (uint8_t)c); // includes the '\0'
return buf; return buf;
} }
buf[2] = '\0'; buf[0] = '\'';
buf[1] = '\\';
buf[3] = '\'';
buf[4] = '\0';
return buf; return buf;
} }

View File

@@ -1,3 +1,3 @@
ERROR: garbage_char.asm(1): ERROR: garbage_char.asm(1):
Unknown character '\xff' Unknown character 0xFF
error: Assembly aborted (1 error)! error: Assembly aborted (1 error)!

View File

@@ -1,5 +1,5 @@
ERROR: invalid-utf-8.asm(6) -> invalid-utf-8.asm::m(4): ERROR: invalid-utf-8.asm(6) -> invalid-utf-8.asm::m(4):
Unknown character '\xcf' Unknown character 0xCF
ERROR: invalid-utf-8.asm(6) -> invalid-utf-8.asm::m(4): ERROR: invalid-utf-8.asm(6) -> invalid-utf-8.asm::m(4):
Unknown character '\xd3' Unknown character 0xD3
error: Assembly aborted (2 errors)! error: Assembly aborted (2 errors)!

View File

@@ -1,3 +1,3 @@
ERROR: null-in-macro.asm(4) -> null-in-macro.asm::foo(2): ERROR: null-in-macro.asm(4) -> null-in-macro.asm::foo(2):
Unknown character '\x00' Unknown character 0x00
error: Assembly aborted (1 error)! error: Assembly aborted (1 error)!