From 27ac51f5fecc444663e8a0e50dfed8dc05624194 Mon Sep 17 00:00:00 2001 From: Robert Liebowitz Date: Tue, 26 Mar 2024 23:27:24 -0400 Subject: [PATCH] fix: allow closing flow seq/map at same indentation The commit as written does not include running tree-sitter generate. Happy to run that if someone can point me to the documentation for how that can be done for the current iteration of the repo. Resolves: https://github.com/ikatyang/tree-sitter-yaml/issues/29 Relates-to: https://github.com/tree-sitter-grammars/tree-sitter-yaml/issues/2 --- grammar.js | 12 +++---- src/scanner.c | 14 ++++++-- test/corpus/spec.txt | 82 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 8 deletions(-) diff --git a/grammar.js b/grammar.js index f6ffd0a..8d9b19c 100644 --- a/grammar.js +++ b/grammar.js @@ -22,9 +22,9 @@ module.exports = grammar({ $._r_blk_fld_bgn, $._br_blk_fld_bgn, // > $._br_blk_str_ctn, // block scalar content $._r_flw_seq_bgn, $._br_flw_seq_bgn, $._b_flw_seq_bgn, // [ - $._r_flw_seq_end, $._br_flw_seq_end, // ] + $._r_flw_seq_end, $._br_flw_seq_end, $._b_flw_seq_end, // ] $._r_flw_map_bgn, $._br_flw_map_bgn, $._b_flw_map_bgn, // { - $._r_flw_map_end, $._br_flw_map_end, // } + $._r_flw_map_end, $._br_flw_map_end, $._b_flw_map_end, // } $._r_flw_sep_bgn, $._br_flw_sep_bgn, // , $._r_flw_key_bgn, $._br_flw_key_bgn, // ? $._r_flw_jsv_bgn, $._br_flw_jsv_bgn, // : (json key) @@ -298,7 +298,7 @@ module.exports = grammar({ _br_sgl_flw_seq: $ => seq($._br_flw_seq_bgn, $._sgl_flw_seq_tal), _b_sgl_flw_seq: $ => seq($._b_flw_seq_bgn, $._sgl_flw_seq_tal), - _flw_seq_tal: $ => seq(optional(choice($._r_flw_seq_dat, $._br_flw_seq_dat)), choice($._r_flw_seq_end, $._br_flw_seq_end)), + _flw_seq_tal: $ => seq(optional(choice($._r_flw_seq_dat, $._br_flw_seq_dat)), choice($._r_flw_seq_end, $._br_flw_seq_end, $._b_flw_seq_end)), _sgl_flw_seq_tal: $ => seq(optional($._r_sgl_flw_col_dat), $._r_flw_seq_end), // flow mapping @@ -317,7 +317,7 @@ module.exports = grammar({ _br_sgl_flw_map: $ => seq($._br_flw_map_bgn, $._sgl_flw_map_tal), _b_sgl_flw_map: $ => seq($._b_flw_map_bgn, $._sgl_flw_map_tal), - _flw_map_tal: $ => seq(optional(choice($._r_flw_map_dat, $._br_flw_map_dat)), choice($._r_flw_map_end, $._br_flw_map_end)), + _flw_map_tal: $ => seq(optional(choice($._r_flw_map_dat, $._br_flw_map_dat)), choice($._r_flw_map_end, $._br_flw_map_end, $._b_flw_map_end)), _sgl_flw_map_tal: $ => seq(optional($._r_sgl_flw_col_dat), $._r_flw_map_end), // flow collection data @@ -529,9 +529,9 @@ module.exports.grammar = global_alias(global_alias(module.exports.grammar, { ..._("|", "_r_blk_lit_bgn", "_br_blk_lit_bgn"), ..._(">", "_r_blk_fld_bgn", "_br_blk_fld_bgn"), ..._("[", "_r_flw_seq_bgn", "_br_flw_seq_bgn", "_b_flw_seq_bgn"), - ..._("]", "_r_flw_seq_end", "_br_flw_seq_end"), + ..._("]", "_r_flw_seq_end", "_br_flw_seq_end", "_b_flw_seq_end"), ..._("{", "_r_flw_map_bgn", "_br_flw_map_bgn", "_b_flw_map_bgn"), - ..._("}", "_r_flw_map_end", "_br_flw_map_end"), + ..._("}", "_r_flw_map_end", "_br_flw_map_end", "_b_flw_map_end"), ..._(",", "_r_flw_sep_bgn", "_br_flw_sep_bgn"), ..._("?", "_r_flw_key_bgn", "_br_flw_key_bgn"), ..._(":", "_r_flw_jsv_bgn", "_br_flw_jsv_bgn"), diff --git a/src/scanner.c b/src/scanner.c index 35e32a9..59a5b75 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -22,9 +22,9 @@ typedef enum { R_BLK_FLD_BGN, BR_BLK_FLD_BGN, BR_BLK_STR_CTN, R_FLW_SEQ_BGN, BR_FLW_SEQ_BGN, B_FLW_SEQ_BGN, - R_FLW_SEQ_END, BR_FLW_SEQ_END, + R_FLW_SEQ_END, BR_FLW_SEQ_END, B_FLW_SEQ_END, R_FLW_MAP_BGN, BR_FLW_MAP_BGN, B_FLW_MAP_BGN, - R_FLW_MAP_END, BR_FLW_MAP_END, + R_FLW_MAP_END, BR_FLW_MAP_END, B_FLW_MAP_END, R_FLW_SEP_BGN, BR_FLW_SEP_BGN, R_FLW_KEY_BGN, BR_FLW_KEY_BGN, R_FLW_JSV_BGN, BR_FLW_JSV_BGN, @@ -977,6 +977,11 @@ bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) { mrk_end(scanner, lexer); RET_SYM(BR_FLW_SEQ_END) } + if (valid_symbols[B_FLW_SEQ_END] && is_b) { + adv(scanner, lexer); + mrk_end(scanner, lexer); + RET_SYM(BR_FLW_SEQ_END) + } } else if (lexer->lookahead == '{') { if (valid_symbols[R_FLW_MAP_BGN] && is_r) { MAY_UPD_IMP_COL(); @@ -1007,6 +1012,11 @@ bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) { mrk_end(scanner, lexer); RET_SYM(BR_FLW_MAP_END) } + if (valid_symbols[B_FLW_MAP_END] && is_b) { + adv(scanner, lexer); + mrk_end(scanner, lexer); + RET_SYM(BR_FLW_MAP_END) + } } else if (lexer->lookahead == ',') { if (valid_symbols[R_FLW_SEP_BGN] && is_r) { adv(scanner, lexer); diff --git a/test/corpus/spec.txt b/test/corpus/spec.txt index ca75d12..3ed4e8b 100644 --- a/test/corpus/spec.txt +++ b/test/corpus/spec.txt @@ -1664,6 +1664,49 @@ Flow Mapping (plain_scalar (string_scalar)))))))) +================================================================================ +Flow Mapping Closed at Original Indentation +================================================================================ +first: { + foo: you, + bar: far +} +second: 2 + +-------------------------------------------------------------------------------- + +(stream + (document + (block_node + (block_mapping + (block_mapping_pair + key: (flow_node + (plain_scalar + (string_scalar))) + value: (flow_node + (flow_mapping + (flow_pair + key: (flow_node + (plain_scalar + (string_scalar))) + value: (flow_node + (plain_scalar + (string_scalar)))) + (flow_pair + key: (flow_node + (plain_scalar + (string_scalar))) + value: (flow_node + (plain_scalar + (string_scalar))))))) + (block_mapping_pair + key: (flow_node + (plain_scalar + (string_scalar))) + value: (flow_node + (plain_scalar + (integer_scalar)))))))) + ================================================================================ Flow mapping colon on line after key ================================================================================ @@ -1761,6 +1804,45 @@ Flow Sequence (plain_scalar (integer_scalar))))))) +================================================================================ +Flow Sequence Closed at Original Indentation +================================================================================ +first: [ + foo, + b, + 42 +] +second: 2 + +-------------------------------------------------------------------------------- + +(stream + (document + (block_node + (block_mapping + (block_mapping_pair + key: (flow_node + (plain_scalar + (string_scalar))) + value: (flow_node + (flow_sequence + (flow_node + (plain_scalar + (string_scalar))) + (flow_node + (plain_scalar + (string_scalar))) + (flow_node + (plain_scalar + (integer_scalar)))))) + (block_mapping_pair + key: (flow_node + (plain_scalar + (string_scalar))) + value: (flow_node + (plain_scalar + (integer_scalar)))))))) + ================================================================================ Flow Sequence in Block Mapping ================================================================================