Skip to content

Commit

Permalink
Make handling of non-standard escape codes more lenient
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Aug 15, 2020
1 parent 6f3e5c9 commit 4f4c3b9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 4 additions & 3 deletions scripts/diagnostics.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ error ExpectedIntegerBaseAfterSigned "expected integer base specifier after sign
error MissingFractionalDigits "expected fractional digits after decimal"
error OctalEscapeCodeTooBig "octal escape code is too large to be an ASCII character"
error InvalidHexEscapeCode "invalid hexadecimal number"
error UnknownEscapeCode "unknown character escape sequence"
error MissingExponentDigits "expected exponent digits"
error DigitsLeadingUnderscore "numeric literals must not start with a leading underscore"
error DecimalDigitMultipleUnknown "decimal literals cannot have multiple digits if at least one of them is X or Z"
Expand All @@ -34,6 +33,8 @@ error BadOctalDigit "expected octal digit"
error BadDecimalDigit "expected decimal digit"
error BadHexDigit "expected hexadecimal digit"
error TooManyLexerErrors "lexer has encountered too many errors (input is a binary file?)"
warning unknown-escape-code UnknownEscapeCode "unknown character escape sequence '\\\\{}'"
warning nonstandard-escape-code NonstandardEscapeCode "non-standard character escape sequence '\\\\{}'"

subsystem Numeric
error LiteralSizeIsZero "size of vector literal cannot be zero"
Expand Down Expand Up @@ -423,8 +424,8 @@ group default = { real-underflow real-overflow literal-overflow ignored-macro-pa
redef-macro nonstandard-generate pointless-void-cast unused-result
not-supported unknown-pragma extra-pragma-args expected-diag-arg unknown-diag-arg
pragma-diag-level explicit-static static-skipped format-real elem-not-found
dynarray-index dynarray-range queue-range empty-queue }
dynarray-index dynarray-range queue-range empty-queue unknown-escape-code }

group extra = { empty-member empty-stmt dup-import }

group pedantic = { empty-pattern implicit-net-port }
group pedantic = { empty-pattern implicit-net-port nonstandard-escape-code }
9 changes: 7 additions & 2 deletions source/parsing/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,15 @@ Token Lexer::lexStringLiteral() {
stringBuffer.append((char)charCode);
}
break;
default:
addDiag(diag::UnknownEscapeCode, offset);
default: {
// '\%' is not an actual escape code but other tools silently allow it
// and major UVM headers use it, so we'll issue a (fairly quiet) warning about it.
// Otherwise issue a louder warning (on by default).
DiagCode code = c == '%' ? diag::NonstandardEscapeCode : diag::UnknownEscapeCode;
addDiag(code, offset) << c;
stringBuffer.append(c);
break;
}
}
}
else if (c == '"') {
Expand Down
13 changes: 12 additions & 1 deletion tests/unittests/LexerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,17 @@ TEST_CASE("String literal (unknown escape)") {
CHECK(diagnostics.back().code == diag::UnknownEscapeCode);
}

TEST_CASE("String literal (nonstandard escape)") {
auto& text = "\"literal\\%\"";
Token token = lexToken(text);

CHECK(token.kind == TokenKind::StringLiteral);
CHECK(token.toString() == text);
CHECK(token.valueText() == "literal%");
REQUIRE(!diagnostics.empty());
CHECK(diagnostics.back().code == diag::NonstandardEscapeCode);
}

TEST_CASE("Integer literal") {
auto& text = "19248";
Token token = lexToken(text);
Expand Down Expand Up @@ -1116,4 +1127,4 @@ TEST_CASE("Test text utilities") {
CHECK(utf8SeqBytes('\xe0') == 2);
CHECK(utf8SeqBytes('\xf0') == 3);
CHECK(utf8SeqBytes('\xff') == 0);
}
}

0 comments on commit 4f4c3b9

Please sign in to comment.