diff --git a/grammar.js b/grammar.js index 296e152..5bbe8ad 100644 --- a/grammar.js +++ b/grammar.js @@ -10,6 +10,8 @@ /// // @ts-check +const ESCAPE_SEQUENCE = token(/\\[nrt"\\]/); + /** Comma separated list with at least one item */ function comma_sep1(item) { return seq(item, repeat(seq(",", item))); @@ -314,10 +316,20 @@ module.exports = grammar({ ), _raw_string_indented: (_) => seq("'''", repeat(/./), "'''"), - _string: ($) => seq('"', repeat(choice($.string_escape, /[^\\"]+/)), '"'), + _string: ($) => + seq( + '"', + repeat(choice($.escape_sequence, /[^\\"]+/)), + '"', + ), _string_indented: ($) => - seq('"""', repeat(choice($.string_escape, /[^\\"]+/)), '"""'), - string_escape: (_) => /\\[nrt"\\]/, + seq( + '"""', + repeat(choice($.escape_sequence, /[^\\"]+/)), + '"""', + ), + + escape_sequence: (_) => ESCAPE_SEQUENCE, _backticked: (_) => seq("`", repeat(/./), "`"), _indented_backticked: (_) => seq("```", repeat(/./), "```"), diff --git a/queries-src/highlights.scm b/queries-src/highlights.scm index 095289d..be5a812 100644 --- a/queries-src/highlights.scm +++ b/queries-src/highlights.scm @@ -71,7 +71,7 @@ (dependency_expression recipe: (identifier) @function) ; handle escape sequences -(string_literal (string_escape) @constant.character.escape) +(string_literal (escape_sequence) @constant.character.escape) (string_literal) @string (comment) @comment.line diff --git a/queries/just/highlights.scm b/queries/just/highlights.scm index 7719f80..27eb24e 100644 --- a/queries/just/highlights.scm +++ b/queries/just/highlights.scm @@ -73,7 +73,7 @@ (dependency_expression recipe: (identifier) @function) ; handle escape sequences -(string_literal (string_escape) @string.escape) +(string_literal (escape_sequence) @string.escape) (string_literal) @string (comment) @comment diff --git a/src/grammar.json b/src/grammar.json index 90704cd..f61d323 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -1350,7 +1350,7 @@ "members": [ { "type": "SYMBOL", - "name": "string_escape" + "name": "escape_sequence" }, { "type": "PATTERN", @@ -1379,7 +1379,7 @@ "members": [ { "type": "SYMBOL", - "name": "string_escape" + "name": "escape_sequence" }, { "type": "PATTERN", @@ -1394,9 +1394,12 @@ } ] }, - "string_escape": { - "type": "PATTERN", - "value": "\\\\[nrt\"\\\\]" + "escape_sequence": { + "type": "TOKEN", + "content": { + "type": "PATTERN", + "value": "\\\\[nrt\"\\\\]" + } }, "_backticked": { "type": "SEQ", @@ -1474,3 +1477,4 @@ ], "supertypes": [] } + diff --git a/src/node-types.json b/src/node-types.json index 55c972a..f7fbfd5 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -682,7 +682,7 @@ "required": false, "types": [ { - "type": "string_escape", + "type": "escape_sequence", "named": true } ] @@ -861,6 +861,10 @@ "type": "else", "named": false }, + { + "type": "escape_sequence", + "named": true + }, { "type": "export", "named": false @@ -893,10 +897,6 @@ "type": "shell", "named": false }, - { - "type": "string_escape", - "named": true - }, { "type": "text", "named": true @@ -921,4 +921,4 @@ "type": "}}", "named": false } -] +] \ No newline at end of file diff --git a/src/parser.c b/src/parser.c index 585ab7e..b05c097 100644 --- a/src/parser.c +++ b/src/parser.c @@ -61,7 +61,7 @@ enum { anon_sym_DQUOTE = 42, aux_sym__string_token1 = 43, anon_sym_DQUOTE_DQUOTE_DQUOTE = 44, - sym_string_escape = 45, + sym_escape_sequence = 45, anon_sym_BQUOTE = 46, anon_sym_BQUOTE_BQUOTE_BQUOTE = 47, sym_text = 48, @@ -167,7 +167,7 @@ static const char * const ts_symbol_names[] = { [anon_sym_DQUOTE] = "\"", [aux_sym__string_token1] = "_string_token1", [anon_sym_DQUOTE_DQUOTE_DQUOTE] = "\"\"\"", - [sym_string_escape] = "string_escape", + [sym_escape_sequence] = "escape_sequence", [anon_sym_BQUOTE] = "`", [anon_sym_BQUOTE_BQUOTE_BQUOTE] = "```", [sym_text] = "text", @@ -273,7 +273,7 @@ static const TSSymbol ts_symbol_map[] = { [anon_sym_DQUOTE] = anon_sym_DQUOTE, [aux_sym__string_token1] = aux_sym__string_token1, [anon_sym_DQUOTE_DQUOTE_DQUOTE] = anon_sym_DQUOTE_DQUOTE_DQUOTE, - [sym_string_escape] = sym_string_escape, + [sym_escape_sequence] = sym_escape_sequence, [anon_sym_BQUOTE] = anon_sym_BQUOTE, [anon_sym_BQUOTE_BQUOTE_BQUOTE] = anon_sym_BQUOTE_BQUOTE_BQUOTE, [sym_text] = sym_text, @@ -514,7 +514,7 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { .visible = true, .named = false, }, - [sym_string_escape] = { + [sym_escape_sequence] = { .visible = true, .named = true, }, @@ -1811,7 +1811,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { ACCEPT_TOKEN(anon_sym_DQUOTE_DQUOTE_DQUOTE); END_STATE(); case 77: - ACCEPT_TOKEN(sym_string_escape); + ACCEPT_TOKEN(sym_escape_sequence); END_STATE(); case 78: ACCEPT_TOKEN(anon_sym_BQUOTE); @@ -2432,7 +2432,7 @@ static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { [anon_sym_SQUOTE_SQUOTE_SQUOTE] = ACTIONS(1), [anon_sym_DQUOTE] = ACTIONS(1), [anon_sym_DQUOTE_DQUOTE_DQUOTE] = ACTIONS(1), - [sym_string_escape] = ACTIONS(1), + [sym_escape_sequence] = ACTIONS(1), [anon_sym_BQUOTE] = ACTIONS(1), [anon_sym_BQUOTE_BQUOTE_BQUOTE] = ACTIONS(1), [sym__indent] = ACTIONS(1), @@ -5731,7 +5731,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(597), 1, aux_sym__string_token1, ACTIONS(599), 1, - sym_string_escape, + sym_escape_sequence, STATE(209), 1, aux_sym__string_repeat1, [4273] = 4, @@ -5740,7 +5740,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(603), 1, anon_sym_DQUOTE_DQUOTE_DQUOTE, ACTIONS(605), 1, - sym_string_escape, + sym_escape_sequence, STATE(207), 1, aux_sym__string_repeat1, [4286] = 1, @@ -5775,7 +5775,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(597), 1, aux_sym__string_token1, ACTIONS(599), 1, - sym_string_escape, + sym_escape_sequence, ACTIONS(603), 1, anon_sym_DQUOTE, STATE(209), 1, @@ -5807,7 +5807,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(613), 1, anon_sym_DQUOTE_DQUOTE_DQUOTE, ACTIONS(615), 1, - sym_string_escape, + sym_escape_sequence, STATE(163), 1, aux_sym__string_repeat1, [4375] = 4, @@ -5816,7 +5816,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(617), 1, aux_sym__string_token1, ACTIONS(619), 1, - sym_string_escape, + sym_escape_sequence, STATE(168), 1, aux_sym__string_repeat1, [4388] = 4, @@ -5825,14 +5825,14 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(623), 1, aux_sym__string_token1, ACTIONS(625), 1, - sym_string_escape, + sym_escape_sequence, STATE(162), 1, aux_sym__string_repeat1, [4401] = 4, ACTIONS(601), 1, aux_sym__string_token1, ACTIONS(605), 1, - sym_string_escape, + sym_escape_sequence, ACTIONS(627), 1, anon_sym_DQUOTE_DQUOTE_DQUOTE, STATE(207), 1, @@ -5841,7 +5841,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(597), 1, aux_sym__string_token1, ACTIONS(599), 1, - sym_string_escape, + sym_escape_sequence, ACTIONS(627), 1, anon_sym_DQUOTE, STATE(209), 1, @@ -5870,7 +5870,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(631), 1, anon_sym_DQUOTE_DQUOTE_DQUOTE, ACTIONS(633), 1, - sym_string_escape, + sym_escape_sequence, STATE(175), 1, aux_sym__string_repeat1, [4466] = 1, @@ -5891,7 +5891,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(637), 1, aux_sym__string_token1, ACTIONS(639), 1, - sym_string_escape, + sym_escape_sequence, STATE(176), 1, aux_sym__string_repeat1, [4493] = 4, @@ -5900,7 +5900,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(641), 1, aux_sym__string_token1, ACTIONS(643), 1, - sym_string_escape, + sym_escape_sequence, STATE(191), 1, aux_sym__string_repeat1, [4506] = 2, @@ -5958,7 +5958,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(601), 1, aux_sym__string_token1, ACTIONS(605), 1, - sym_string_escape, + sym_escape_sequence, STATE(207), 1, aux_sym__string_repeat1, [4582] = 4, @@ -6077,7 +6077,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(658), 1, anon_sym_DQUOTE_DQUOTE_DQUOTE, ACTIONS(660), 1, - sym_string_escape, + sym_escape_sequence, STATE(207), 1, aux_sym__string_repeat1, [4740] = 4, @@ -6095,7 +6095,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(663), 1, aux_sym__string_token1, ACTIONS(666), 1, - sym_string_escape, + sym_escape_sequence, STATE(209), 1, aux_sym__string_repeat1, [4766] = 1, diff --git a/test/corpus/statements.txt b/test/corpus/statements.txt index 7d51018..744043b 100644 --- a/test/corpus/statements.txt +++ b/test/corpus/statements.txt @@ -131,8 +131,8 @@ baz := "a\"\t" (expression (value (string_literal - (string_escape) - (string_escape)))) + (escape_sequence) + (escape_sequence)))) (eol))) (item (eol))) @@ -175,8 +175,8 @@ def \" (expression (value (string_literal - (string_escape) - (string_escape)))) + (escape_sequence) + (escape_sequence)))) (eol))) (item (eol)))