Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add more string litral formats (from postgres) and numbers foramts #215

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3106,6 +3106,8 @@ module.exports = grammar({
$._integer,
$._decimal_number,
$._literal_string,
$._bit_string,
$._string_casting,
$.keyword_true,
$.keyword_false,
$.keyword_null,
Expand All @@ -3114,7 +3116,7 @@ module.exports = grammar({
_double_quote_string: _ => /"[^"]*"/,
// The norm specify that between two consecutive string must be a return,
// but this is good enough.
_single_quote_string: _ => repeat1(/'([^']|'')*'/),
_single_quote_string: _ => seq(/([uU]&)?'([^']|'')*'/, repeat(/'([^']|'')*'/)),
_literal_string: $ => prec(
1,
choice(
Expand All @@ -3124,8 +3126,18 @@ module.exports = grammar({
),
),
_natural_number: _ => /\d+/,
_integer: $ => seq(optional("-"), $._natural_number),
_decimal_number: $ => seq(optional("-"), /(\d*[.]\d+)|(\d+[.])/),
_integer: $ => seq(
optional(choice("-", "+")),
/(0[xX][0-9A-Fa-f]+(_[0-9A-Fa-f]+)*)|(0[oO][0-7]+(_[0-7]+)*)|(0[bB][01]+(_[01]+)*)|(\d+(_\d+)*(e[+-]?\d+(_\d+)*)?)/
),
_decimal_number: $ => seq(
optional(
choice("-", "+")),
/((\d+(_\d+)*)?[.]\d+(_\d+)*(e[+-]?\d+(_\d+)*)?)|(\d+(_\d+)*[.](e[+-]?\d+(_\d+)*)?)/
),
_bit_string: $ => seq(/[bBxX]'([^']|'')*'/, repeat(/'([^']|'')*'/)),
// The identifier should be followed by a string (no parenthesis allowed)
_string_casting: $ => seq($.identifier, $._single_quote_string),
matthias-Q marked this conversation as resolved.
Show resolved Hide resolved

bang: _ => '!',

Expand Down
178 changes: 178 additions & 0 deletions test/corpus/literals.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
================================================================================
Hexadecimal number
================================================================================

SELECT 0xAA;

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal))))))

================================================================================
binary number
================================================================================

SELECT 0b1010;

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal))))))

================================================================================
big integer with _ separators
================================================================================

SELECT 1_000_000_000;

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal))))))

================================================================================
integer with exponent with _ separators
================================================================================

SELECT 12e1_000;

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal))))))

================================================================================
decimal with exponent
================================================================================

SELECT .2e-1_000;

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal))))))

================================================================================
positive number
================================================================================

SELECT +1;

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal))))))

================================================================================
bit string
================================================================================

SELECT b'1010';

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal))))))

================================================================================
bit string (hexa)
================================================================================

SELECT x'AF';

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal))))))

================================================================================
multi line string
================================================================================

SELECT 'hello '
'world';

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal))))))

================================================================================
unicode string
================================================================================

SELECT u&'\041f\0440\0438\0432\0456\0442 '
'\0421\0432\0456\0442';

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal))))))

================================================================================
string casting
================================================================================

SELECT int '123';

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
(literal
(identifier)))))))