From edd79d76b6120186ecc4f47db322f915555555d1 Mon Sep 17 00:00:00 2001 From: Zane Duffield Date: Fri, 6 Oct 2023 16:24:30 +1100 Subject: [PATCH] Correctly parse various unusual integer literals --- CHANGELOG.md | 1 + .../au/com/integradev/delphi/antlr/Delphi.g | 10 ++++---- .../integradev/delphi/antlr/GrammarTest.java | 5 ++++ .../delphi/grammar/UnusualIntegerLiterals.pas | 23 +++++++++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 delphi-frontend/src/test/resources/au/com/integradev/delphi/grammar/UnusualIntegerLiterals.pas diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c52ca03c..55e5d2da0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `add` is no longer colorized as a keyword in the SonarQube web interface. - `remove` is no longer colorized as a keyword in the SonarQube web interface. - `variant` is no longer colorized as a keyword in the SonarQube web interface. +- Parsing errors for unusual escaped character, hex integer, and binary integer literals. ## [0.40.0] - 2023-01-10 diff --git a/delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g b/delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g index 731cb86f8..0ad4b759a 100644 --- a/delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g +++ b/delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g @@ -1201,9 +1201,9 @@ TkIntNumber : DigitSeq ( )? )? ; -TkHexNumber : '$' HexDigitSeq +TkHexNumber : '$' ('_' | HexDigit)* ; -TkBinaryNumber : '%' BinaryDigitSeq +TkBinaryNumber : '%' ('_' | BinaryDigit)* ; TkAsmId : { asmMode }? => '@' '@'? (Alpha | '_' | Digit)+ ; @@ -1213,9 +1213,9 @@ TkQuotedString : '\'' ('\'\'' | ~('\''))* '\'' ; TkAsmDoubleQuotedString : { asmMode }? => '"' (~('\"'))* '"' ; -TkCharacterEscapeCode : '#' DigitSeq - | '#' '$' HexDigitSeq - | '#' '%' BinaryDigitSeq +TkCharacterEscapeCode : '#' ('_' | Digit)+ + | '#' '$' ('_' | HexDigit)+ + | '#' '%' ('_' | BinaryDigit)+ ; //---------------------------------------------------------------------------- // Fragments diff --git a/delphi-frontend/src/test/java/au/com/integradev/delphi/antlr/GrammarTest.java b/delphi-frontend/src/test/java/au/com/integradev/delphi/antlr/GrammarTest.java index b263814ac..cd6cd2a78 100644 --- a/delphi-frontend/src/test/java/au/com/integradev/delphi/antlr/GrammarTest.java +++ b/delphi-frontend/src/test/java/au/com/integradev/delphi/antlr/GrammarTest.java @@ -237,4 +237,9 @@ void testEmptyFileShouldThrow() { DelphiFileUtils.mockConfig())) .isInstanceOf(DelphiFileConstructionException.class); } + + @Test + void testUnusualIntegerLiterals() { + parseFile("UnusualIntegerLiterals.pas"); + } } diff --git a/delphi-frontend/src/test/resources/au/com/integradev/delphi/grammar/UnusualIntegerLiterals.pas b/delphi-frontend/src/test/resources/au/com/integradev/delphi/grammar/UnusualIntegerLiterals.pas new file mode 100644 index 000000000..906dedf0c --- /dev/null +++ b/delphi-frontend/src/test/resources/au/com/integradev/delphi/grammar/UnusualIntegerLiterals.pas @@ -0,0 +1,23 @@ +unit UnusualIntegerLiterals; + +interface + +const + HexWithoutDigits = $; + BinaryWithoutDigits = %; + HexStartingWithUnderscore = $_; + BinaryStartingWithUnderscore = %_; + HexStartingWithManyUnderscores = $__; + BinaryStartingWithManyUnderscores = %__; + + CharHexStartingWithUnderscore = #$_; + CharBinaryStartingWithUnderscore = #%_; + CharHexStartingWithManyUnderscores = #$__; + CharBinaryStartingWithManyUnderscores = #%__; + + CharDecimalStartingWithUnderscore = #_; + CharDecimalStartingWithManyUnderscores = #__; + +implementation + +end.