From 5c457d613b950cf2290b0f9aea90a019b4ceeef1 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 18 Apr 2021 21:45:26 +0200 Subject: [PATCH] Allow ~...fF and set aliases for some injected symbols --- grammar.js | 10 +++--- src/grammar.json | 15 ++++++--- src/node-types.json | 46 ++++++++++++++++++++++----- src/parser.c | 74 ++++++++++++++++--------------------------- test/corpus/basic.txt | 6 ++-- 5 files changed, 84 insertions(+), 67 deletions(-) diff --git a/grammar.js b/grammar.js index 49872d065..252bf5abf 100644 --- a/grammar.js +++ b/grammar.js @@ -80,7 +80,7 @@ module.exports = grammar(clojure, { repeat(choice(field('value', $._form), $._gap)), field('close', ")")), - _format_token: $ => choice($.num_lit, seq("'", /./)), + _format_token: $ => choice($.num_lit, seq("'", alias(/./, $.char_lit))), // https://en.wikipedia.org/wiki/Format_Common_Lisp) format_prefix_parameters: _ => choice('v', 'V', '#'), format_modifiers: $ => seq(repeat(choice($._format_token, ',')), choice('@', '@:', ':', ':@')), @@ -105,7 +105,7 @@ module.exports = grammar(clojure, { seq('/', choice($._package_lit_without_slash, $._sym_lit_without_slash), '/'), '?', "Newline", - seq(repeat(choice($._format_token, ',')), /[$rRbBdDgGxXeEoOsS]/), + seq(repeat(choice($._format_token, ',')), /[$rRbBdDgGxXeEoOsStTfF]/), ), format_specifier: $ => prec.left(seq( @@ -228,7 +228,7 @@ module.exports = grammar(clojure, { field('package', $._sym_lit_without_slash), // Make optional, instead of keywords? choice(':', '::'), field('symbol', $._sym_lit_without_slash) - )), 'package_lit'), + )), $.package_lit), kwd_lit: $ => prec(PREC.KWD_LIT, seq( choice(':', '::'), @@ -238,8 +238,8 @@ module.exports = grammar(clojure, { sym_lit: _ => seq(SYMBOL), - _sym_lit_without_slash: _ => - alias(repeat1(SYMBOL_WITHOUT_SLASH), 'sym_lit'), + _sym_lit_without_slash: $ => + alias(repeat1(SYMBOL_WITHOUT_SLASH), $.sym_lit), kwd_symbol: _ => seq(SYMBOL), diff --git a/src/grammar.json b/src/grammar.json index 81fa33553..a346d238e 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -1807,8 +1807,13 @@ "value": "'" }, { - "type": "PATTERN", - "value": "." + "type": "ALIAS", + "content": { + "type": "PATTERN", + "value": "." + }, + "named": true, + "value": "char_lit" } ] } @@ -2046,7 +2051,7 @@ }, { "type": "PATTERN", - "value": "[$rRbBdDgGxXeEoOsS]" + "value": "[$rRbBdDgGxXeEoOsStTfF]" } ] } @@ -3584,7 +3589,7 @@ ] } }, - "named": false, + "named": true, "value": "package_lit" }, "_sym_lit_without_slash": { @@ -3596,7 +3601,7 @@ "value": "[^:\\f\\n\\r\\t ()\\[\\]{}\"@^;/`\\\\,#'\\u000B\\u001C\\u001D\\u001E\\u001F\\u2028\\u2029\\u1680\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2008\\u2009\\u200a\\u205f\\u3000]" } }, - "named": false, + "named": true, "value": "sym_lit" }, "kwd_symbol": { diff --git a/src/node-types.json b/src/node-types.json index 6d88058cd..507e84bdb 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -1473,13 +1473,17 @@ "named": true, "fields": { "numberOfArgs": { - "multiple": false, + "multiple": true, "required": false, "types": [ { "type": "'", "named": false }, + { + "type": "char_lit", + "named": true + }, { "type": "num_lit", "named": true @@ -1492,18 +1496,22 @@ "types": [ { "type": "package_lit", - "named": false + "named": true } ] }, "repetitions": { - "multiple": false, + "multiple": true, "required": false, "types": [ { "type": "'", "named": false }, + { + "type": "char_lit", + "named": true + }, { "type": "num_lit", "named": true @@ -1516,7 +1524,7 @@ "types": [ { "type": "package_lit", - "named": false + "named": true } ] } @@ -1525,9 +1533,21 @@ "multiple": true, "required": false, "types": [ + { + "type": "char_lit", + "named": true + }, { "type": "num_lit", "named": true + }, + { + "type": "package_lit", + "named": true + }, + { + "type": "sym_lit", + "named": true } ] } @@ -1540,6 +1560,10 @@ "multiple": true, "required": false, "types": [ + { + "type": "char_lit", + "named": true + }, { "type": "num_lit", "named": true @@ -2433,6 +2457,16 @@ } ] } + }, + "children": { + "multiple": false, + "required": false, + "types": [ + { + "type": "sym_lit", + "named": true + } + ] } }, { @@ -4702,10 +4736,6 @@ "type": "on", "named": false }, - { - "type": "package_lit", - "named": false - }, { "type": "repeat", "named": false diff --git a/src/parser.c b/src/parser.c index 5f60dda05..41986d6b5 100644 --- a/src/parser.c +++ b/src/parser.c @@ -17,7 +17,7 @@ #define STATE_COUNT 3021 #define LARGE_STATE_COUNT 680 #define SYMBOL_COUNT 186 -#define ALIAS_COUNT 2 +#define ALIAS_COUNT 0 #define TOKEN_COUNT 117 #define EXTERNAL_TOKEN_COUNT 0 #define FIELD_COUNT 21 @@ -210,8 +210,6 @@ enum { aux_sym_do_clause_repeat1 = 183, aux_sym_loop_macro_repeat1 = 184, aux_sym__sym_lit_without_slash_repeat1 = 185, - anon_alias_sym_package_lit = 186, - anon_alias_sym_sym_lit = 187, }; static const char *ts_symbol_names[] = { @@ -255,7 +253,7 @@ static const char *ts_symbol_names[] = { [anon_sym_COMMA] = ",", [sym_block_comment] = "block_comment", [sym_fancy_literal] = "fancy_literal", - [aux_sym__format_token_token1] = "_format_token_token1", + [aux_sym__format_token_token1] = "char_lit", [anon_sym_v] = "v", [anon_sym_V] = "V", [anon_sym_AT_COLON] = "@:", @@ -401,8 +399,6 @@ static const char *ts_symbol_names[] = { [aux_sym_do_clause_repeat1] = "do_clause_repeat1", [aux_sym_loop_macro_repeat1] = "loop_macro_repeat1", [aux_sym__sym_lit_without_slash_repeat1] = "_sym_lit_without_slash_repeat1", - [anon_alias_sym_package_lit] = "package_lit", - [anon_alias_sym_sym_lit] = "sym_lit", }; static TSSymbol ts_symbol_map[] = { @@ -446,7 +442,7 @@ static TSSymbol ts_symbol_map[] = { [anon_sym_COMMA] = anon_sym_COMMA, [sym_block_comment] = sym_block_comment, [sym_fancy_literal] = sym_fancy_literal, - [aux_sym__format_token_token1] = aux_sym__format_token_token1, + [aux_sym__format_token_token1] = sym_char_lit, [anon_sym_v] = anon_sym_v, [anon_sym_V] = anon_sym_V, [anon_sym_AT_COLON] = anon_sym_AT_COLON, @@ -592,8 +588,6 @@ static TSSymbol ts_symbol_map[] = { [aux_sym_do_clause_repeat1] = aux_sym_do_clause_repeat1, [aux_sym_loop_macro_repeat1] = aux_sym_loop_macro_repeat1, [aux_sym__sym_lit_without_slash_repeat1] = aux_sym__sym_lit_without_slash_repeat1, - [anon_alias_sym_package_lit] = anon_alias_sym_package_lit, - [anon_alias_sym_sym_lit] = anon_alias_sym_sym_lit, }; static const TSSymbolMetadata ts_symbol_metadata[] = { @@ -758,8 +752,8 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { .named = true, }, [aux_sym__format_token_token1] = { - .visible = false, - .named = false, + .visible = true, + .named = true, }, [anon_sym_v] = { .visible = true, @@ -1341,14 +1335,6 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { .visible = false, .named = false, }, - [anon_alias_sym_package_lit] = { - .visible = true, - .named = false, - }, - [anon_alias_sym_sym_lit] = { - .visible = true, - .named = false, - }, }; enum { @@ -1836,41 +1822,37 @@ static const TSFieldMapEntry ts_field_map_entries[] = { static TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGTH] = { [0] = {0}, [29] = { - [0] = anon_alias_sym_sym_lit, + [0] = sym_sym_lit, }, [52] = { - [0] = anon_alias_sym_package_lit, - [1] = anon_alias_sym_package_lit, - [2] = anon_alias_sym_package_lit, + [0] = sym_package_lit, + [1] = sym_package_lit, + [2] = sym_package_lit, }, }; static uint16_t ts_non_terminal_alias_map[] = { sym__sym_lit_without_slash, 2, sym__sym_lit_without_slash, - anon_alias_sym_package_lit, + sym_package_lit, aux_sym__sym_lit_without_slash_repeat1, 2, aux_sym__sym_lit_without_slash_repeat1, - anon_alias_sym_sym_lit, + sym_sym_lit, 0, }; static inline bool aux_sym_str_lit_token1_character_set_1(int32_t c) { return (c < 'X' - ? (c < 'G' + ? (c < 'O' ? (c < 'B' ? c == '$' - : c <= 'E') - : (c <= 'G' || (c < 'R' - ? c == 'O' - : c <= 'S'))) - : (c <= 'X' || (c < 'o' - ? (c < 'g' - ? (c >= 'b' && c <= 'e') - : c <= 'g') - : (c <= 'o' || (c < 'x' - ? (c >= 'r' && c <= 's') - : c <= 'x'))))); + : c <= 'G') + : (c <= 'O' || (c >= 'R' && c <= 'T'))) + : (c <= 'X' || (c < 'r' + ? (c < 'o' + ? (c >= 'b' && c <= 'g') + : c <= 'o') + : (c <= 't' || c == 'x')))); } static inline bool aux_sym_char_lit_token1_character_set_1(int32_t c) { @@ -2113,20 +2095,18 @@ static inline bool sym_fancy_literal_character_set_1(int32_t c) { static inline bool aux_sym_format_directive_type_token11_character_set_1(int32_t c) { return (c < 'X' - ? (c < 'G' + ? (c < 'D' ? (c < 'B' ? c == '$' - : (c <= 'B' || (c >= 'D' && c <= 'E'))) + : c <= 'B') : (c <= 'G' || (c < 'R' ? c == 'O' - : c <= 'S'))) - : (c <= 'X' || (c < 'o' - ? (c < 'g' - ? (c >= 'b' && c <= 'e') - : c <= 'g') - : (c <= 'o' || (c < 'x' - ? (c >= 'r' && c <= 's') - : c <= 'x'))))); + : c <= 'T'))) + : (c <= 'X' || (c < 'r' + ? (c < 'o' + ? (c >= 'b' && c <= 'g') + : c <= 'o') + : (c <= 't' || c == 'x')))); } static inline bool aux_sym__sym_lit_without_slash_token1_character_set_1(int32_t c) { diff --git a/test/corpus/basic.txt b/test/corpus/basic.txt index c88adca99..8f853c207 100644 --- a/test/corpus/basic.txt +++ b/test/corpus/basic.txt @@ -883,12 +883,14 @@ Format (str_lit (format_specifier (format_prefix_parameters) - (format_directive_type))) + (format_directive_type + (char_lit)))) (num_lit) (num_lit)) (list_lit (sym_lit) (str_lit (format_specifier - (format_directive_type)))) + (format_directive_type + (sym_lit))))) (num_lit))