Add string literals

Once again, taking the rules more or less straight from tree-sitter-go.
This commit is contained in:
Bruno BELANYI 2024-04-07 23:16:31 +01:00
parent 57b38c01c0
commit f548b1d5ad
5 changed files with 712 additions and 181 deletions

View file

@ -31,6 +31,7 @@ module.exports = grammar({
// Literals
$.identifier,
$.integer_literal,
$._string_literal,
),
// The Blueprint scanner makes use of Go's lexer, so copy their rule
@ -38,6 +39,39 @@ module.exports = grammar({
integer_literal: (_) => seq(optional("-"), /[0-9]+/),
// The Blueprint scanner makes use of Go's lexer, so copy their rule
_string_literal: ($) => choice(
$.raw_string_literal,
$.interpreted_string_literal,
),
raw_string_literal: (_) => token(seq(
"`",
/[^`]+/,
"`",
)),
interpreted_string_literal: $ => seq(
'"',
repeat(choice(
// Allow all characters without special meaning, disallow newlines
/[^"\n\\]+/,
$.escape_sequence,
)),
token.immediate('"'),
),
escape_sequence: (_) => token.immediate(seq(
'\\',
choice(
/[^xuU]/,
/\d{2,3}/,
/x[0-9a-fA-F]{2,}/,
/u[0-9a-fA-F]{4}/,
/U[0-9a-fA-F]{8}/,
),
)),
// }}}
}
});