diff --git a/corpus/comment/comment.nu b/corpus/comment/comment.nu new file mode 100644 index 0000000..5547eeb --- /dev/null +++ b/corpus/comment/comment.nu @@ -0,0 +1,98 @@ +===== +comment-001-single +===== + +# comment + +----- + +(nu_script + (comment)) + +===== +comment-002-before-command +===== + +# comment +echo hello + +----- + +(nu_script + (comment) + (pipeline + (pipe_element + (command + (cmd_identifier) + (val_string))))) + +===== +comment-003-empty-before-command +===== + +# +echo hello + +----- + +(nu_script + (comment) + (pipeline + (pipe_element + (command + (cmd_identifier) + (val_string))))) + +===== +comment-003-end-of-line +===== + +echo hello # comment + +----- + +(nu_script + (pipeline + (pipe_element + (command + (cmd_identifier) + (val_string))) + (comment))) + +===== +comment-003-empty-end-of-line +===== + +echo hello # + +----- + +(nu_script + (pipeline + (pipe_element + (command + (cmd_identifier) + (val_string))) + (comment))) + +===== +comment-003-empty-end-of-line-before-command +===== + +echo hello # +echo world + +----- + +(nu_script + (pipeline + (pipe_element + (command + (cmd_identifier) + (val_string))) + (comment)) + (pipeline + (pipe_element + (command + (cmd_identifier) + (val_string))))) diff --git a/grammar.js b/grammar.js index f55874b..c8ee8ff 100644 --- a/grammar.js +++ b/grammar.js @@ -928,7 +928,7 @@ module.exports = grammar({ /// Comments - comment: ($) => seq(PUNC().hash, /.*\n/), + comment: ($) => seq(PUNC().hash, /.*/), }, }); diff --git a/src/grammar.json b/src/grammar.json index ad3926d..d025020 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -7049,7 +7049,7 @@ }, { "type": "PATTERN", - "value": ".*\\n" + "value": ".*" } ] } diff --git a/src/parser.c b/src/parser.c index 3fe1e11..820f4aa 100644 --- a/src/parser.c +++ b/src/parser.c @@ -16,9 +16,9 @@ #define LANGUAGE_VERSION 14 #define STATE_COUNT 3764 #define LARGE_STATE_COUNT 989 -#define SYMBOL_COUNT 408 +#define SYMBOL_COUNT 409 #define ALIAS_COUNT 0 -#define TOKEN_COUNT 267 +#define TOKEN_COUNT 268 #define EXTERNAL_TOKEN_COUNT 0 #define FIELD_COUNT 68 #define MAX_ALIAS_SEQUENCE_LENGTH 15 @@ -291,147 +291,148 @@ enum { aux_sym_long_flag_token1 = 264, aux_sym_unquoted_token1 = 265, anon_sym_POUND = 266, - sym_nu_script = 267, - sym_shebang = 268, - sym__block_body_statement = 269, - sym__declaration = 270, - sym_decl_alias = 271, - sym_stmt_let = 272, - sym_stmt_mut = 273, - sym_stmt_const = 274, - sym__assignment_pattern = 275, - sym__statement = 276, - sym_pipeline = 277, - sym__block_body_statement_last = 278, - sym__declaration_last = 279, - sym_decl_alias_last = 280, - sym_stmt_let_last = 281, - sym_stmt_mut_last = 282, - sym_stmt_const_last = 283, - sym__assignment_pattern_last = 284, - sym__statement_last = 285, - sym_pipeline_last = 286, - sym__block_body = 287, - sym__command_name = 288, - sym__variable_name = 289, - sym__terminator = 290, - sym_decl_def = 291, - sym_decl_export = 292, - sym_decl_extern = 293, - sym_decl_module = 294, - sym_decl_use = 295, - sym_returns = 296, - sym__one_type = 297, - sym__multiple_types = 298, - sym_parameter_parens = 299, - sym_parameter_bracks = 300, - sym_parameter_pipes = 301, - sym_parameter = 302, - sym__param_name = 303, - sym_param_type = 304, - sym_param_value = 305, - sym__type_annotation = 306, - sym_flat_type = 307, - sym_collection_type = 308, - sym_list_type = 309, - sym_param_cmd = 310, - sym_param_rest = 311, - sym_param_opt = 312, - sym_param_long_flag = 313, - sym_flag_capsule = 314, - sym_param_short_flag = 315, - sym__control = 316, - sym__ctrl_statement = 317, - sym__ctrl_expression = 318, - sym_ctrl_for = 319, - sym_ctrl_loop = 320, - sym_ctrl_error = 321, - sym_ctrl_while = 322, - sym_ctrl_do = 323, - sym_ctrl_if = 324, - sym_ctrl_match = 325, - sym_match_arm = 326, - sym_default_arm = 327, - sym__match_expression = 328, - sym_match_pattern = 329, - sym__match_or_pattern = 330, - sym_match_guard = 331, - sym__match_list_destructure_pattern = 332, - sym_ctrl_try = 333, - sym_ctrl_return = 334, - sym_pipe_element = 335, - sym_pipe_element_last = 336, - sym_stmt_source = 337, - sym_stmt_register = 338, - sym__stmt_hide = 339, - sym_hide_mod = 340, - sym_hide_env = 341, - sym__stmt_overlay = 342, - sym_overlay_list = 343, - sym_overlay_hide = 344, - sym_overlay_new = 345, - sym_overlay_use = 346, - sym_scope_pattern = 347, - sym_wild_card = 348, - sym_command_list = 349, - sym_assignment = 350, - sym_block = 351, - sym__blosure = 352, - sym_where_command = 353, - sym__where_predicate = 354, - sym__expression = 355, - sym_expr_unary = 356, - sym_expr_binary = 357, - sym_expr_parenthesized = 358, - sym_val_range = 359, - sym__value = 360, - sym_val_bool = 361, - sym_val_variable = 362, - sym__var = 363, - sym_val_number = 364, - sym_val_duration = 365, - sym_val_filesize = 366, - sym_val_binary = 367, - sym_val_string = 368, - sym__str_double_quotes = 369, - sym_val_interpolated = 370, - sym__inter_single_quotes = 371, - sym__inter_double_quotes = 372, - sym_expr_interpolated = 373, - sym_val_list = 374, - sym_val_record = 375, - sym_record_entry = 376, - sym_val_table = 377, - sym_val_closure = 378, - sym_cell_path = 379, - sym_path = 380, - sym_command = 381, - sym__cmd_arg = 382, - sym_redirection = 383, - sym__flag = 384, - sym_long_flag = 385, - sym_unquoted = 386, - sym_comment = 387, - aux_sym_pipeline_repeat1 = 388, - aux_sym__block_body_repeat1 = 389, - aux_sym__block_body_repeat2 = 390, - aux_sym__multiple_types_repeat1 = 391, - aux_sym_parameter_parens_repeat1 = 392, - aux_sym_collection_type_repeat1 = 393, - aux_sym_ctrl_match_repeat1 = 394, - aux_sym__match_or_pattern_repeat1 = 395, - aux_sym_pipe_element_repeat1 = 396, - aux_sym_overlay_use_repeat1 = 397, - aux_sym_command_list_repeat1 = 398, - aux_sym_val_binary_repeat1 = 399, - aux_sym__str_double_quotes_repeat1 = 400, - aux_sym__inter_single_quotes_repeat1 = 401, - aux_sym__inter_double_quotes_repeat1 = 402, - aux_sym_val_list_repeat1 = 403, - aux_sym_val_record_repeat1 = 404, - aux_sym_val_table_repeat1 = 405, - aux_sym_cell_path_repeat1 = 406, - aux_sym_command_repeat1 = 407, + aux_sym_comment_token1 = 267, + sym_nu_script = 268, + sym_shebang = 269, + sym__block_body_statement = 270, + sym__declaration = 271, + sym_decl_alias = 272, + sym_stmt_let = 273, + sym_stmt_mut = 274, + sym_stmt_const = 275, + sym__assignment_pattern = 276, + sym__statement = 277, + sym_pipeline = 278, + sym__block_body_statement_last = 279, + sym__declaration_last = 280, + sym_decl_alias_last = 281, + sym_stmt_let_last = 282, + sym_stmt_mut_last = 283, + sym_stmt_const_last = 284, + sym__assignment_pattern_last = 285, + sym__statement_last = 286, + sym_pipeline_last = 287, + sym__block_body = 288, + sym__command_name = 289, + sym__variable_name = 290, + sym__terminator = 291, + sym_decl_def = 292, + sym_decl_export = 293, + sym_decl_extern = 294, + sym_decl_module = 295, + sym_decl_use = 296, + sym_returns = 297, + sym__one_type = 298, + sym__multiple_types = 299, + sym_parameter_parens = 300, + sym_parameter_bracks = 301, + sym_parameter_pipes = 302, + sym_parameter = 303, + sym__param_name = 304, + sym_param_type = 305, + sym_param_value = 306, + sym__type_annotation = 307, + sym_flat_type = 308, + sym_collection_type = 309, + sym_list_type = 310, + sym_param_cmd = 311, + sym_param_rest = 312, + sym_param_opt = 313, + sym_param_long_flag = 314, + sym_flag_capsule = 315, + sym_param_short_flag = 316, + sym__control = 317, + sym__ctrl_statement = 318, + sym__ctrl_expression = 319, + sym_ctrl_for = 320, + sym_ctrl_loop = 321, + sym_ctrl_error = 322, + sym_ctrl_while = 323, + sym_ctrl_do = 324, + sym_ctrl_if = 325, + sym_ctrl_match = 326, + sym_match_arm = 327, + sym_default_arm = 328, + sym__match_expression = 329, + sym_match_pattern = 330, + sym__match_or_pattern = 331, + sym_match_guard = 332, + sym__match_list_destructure_pattern = 333, + sym_ctrl_try = 334, + sym_ctrl_return = 335, + sym_pipe_element = 336, + sym_pipe_element_last = 337, + sym_stmt_source = 338, + sym_stmt_register = 339, + sym__stmt_hide = 340, + sym_hide_mod = 341, + sym_hide_env = 342, + sym__stmt_overlay = 343, + sym_overlay_list = 344, + sym_overlay_hide = 345, + sym_overlay_new = 346, + sym_overlay_use = 347, + sym_scope_pattern = 348, + sym_wild_card = 349, + sym_command_list = 350, + sym_assignment = 351, + sym_block = 352, + sym__blosure = 353, + sym_where_command = 354, + sym__where_predicate = 355, + sym__expression = 356, + sym_expr_unary = 357, + sym_expr_binary = 358, + sym_expr_parenthesized = 359, + sym_val_range = 360, + sym__value = 361, + sym_val_bool = 362, + sym_val_variable = 363, + sym__var = 364, + sym_val_number = 365, + sym_val_duration = 366, + sym_val_filesize = 367, + sym_val_binary = 368, + sym_val_string = 369, + sym__str_double_quotes = 370, + sym_val_interpolated = 371, + sym__inter_single_quotes = 372, + sym__inter_double_quotes = 373, + sym_expr_interpolated = 374, + sym_val_list = 375, + sym_val_record = 376, + sym_record_entry = 377, + sym_val_table = 378, + sym_val_closure = 379, + sym_cell_path = 380, + sym_path = 381, + sym_command = 382, + sym__cmd_arg = 383, + sym_redirection = 384, + sym__flag = 385, + sym_long_flag = 386, + sym_unquoted = 387, + sym_comment = 388, + aux_sym_pipeline_repeat1 = 389, + aux_sym__block_body_repeat1 = 390, + aux_sym__block_body_repeat2 = 391, + aux_sym__multiple_types_repeat1 = 392, + aux_sym_parameter_parens_repeat1 = 393, + aux_sym_collection_type_repeat1 = 394, + aux_sym_ctrl_match_repeat1 = 395, + aux_sym__match_or_pattern_repeat1 = 396, + aux_sym_pipe_element_repeat1 = 397, + aux_sym_overlay_use_repeat1 = 398, + aux_sym_command_list_repeat1 = 399, + aux_sym_val_binary_repeat1 = 400, + aux_sym__str_double_quotes_repeat1 = 401, + aux_sym__inter_single_quotes_repeat1 = 402, + aux_sym__inter_double_quotes_repeat1 = 403, + aux_sym_val_list_repeat1 = 404, + aux_sym_val_record_repeat1 = 405, + aux_sym_val_table_repeat1 = 406, + aux_sym_cell_path_repeat1 = 407, + aux_sym_command_repeat1 = 408, }; static const char * const ts_symbol_names[] = { @@ -702,6 +703,7 @@ static const char * const ts_symbol_names[] = { [aux_sym_long_flag_token1] = "long_flag_token1", [aux_sym_unquoted_token1] = "unquoted_token1", [anon_sym_POUND] = "#", + [aux_sym_comment_token1] = "comment_token1", [sym_nu_script] = "nu_script", [sym_shebang] = "shebang", [sym__block_body_statement] = "_block_body_statement", @@ -1113,6 +1115,7 @@ static const TSSymbol ts_symbol_map[] = { [aux_sym_long_flag_token1] = aux_sym_long_flag_token1, [aux_sym_unquoted_token1] = aux_sym_unquoted_token1, [anon_sym_POUND] = anon_sym_POUND, + [aux_sym_comment_token1] = aux_sym_comment_token1, [sym_nu_script] = sym_nu_script, [sym_shebang] = sym_shebang, [sym__block_body_statement] = sym__block_body_statement, @@ -2325,6 +2328,10 @@ static const TSSymbolMetadata ts_symbol_metadata[] = { .visible = true, .named = false, }, + [aux_sym_comment_token1] = { + .visible = false, + .named = false, + }, [sym_nu_script] = { .visible = true, .named = true, @@ -37234,6 +37241,25 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { ACCEPT_TOKEN(anon_sym_POUND); if (!aux_sym_unquoted_token1_character_set_1(lookahead)) ADVANCE(1849); END_STATE(); + case 1854: + ACCEPT_TOKEN(anon_sym_POUND); + if (lookahead != 0 && + lookahead != '\n') ADVANCE(1856); + END_STATE(); + case 1855: + ACCEPT_TOKEN(aux_sym_comment_token1); + if (lookahead == '#') ADVANCE(1854); + if (lookahead == '\t' || + lookahead == '\r' || + lookahead == ' ') ADVANCE(1855); + if (lookahead != 0 && + lookahead != '\n') ADVANCE(1856); + END_STATE(); + case 1856: + ACCEPT_TOKEN(aux_sym_comment_token1); + if (lookahead != 0 && + lookahead != '\n') ADVANCE(1856); + END_STATE(); default: return false; } @@ -41403,7 +41429,7 @@ static const TSLexMode ts_lex_modes[STATE_COUNT] = { [3629] = {.lex_state = 343}, [3630] = {.lex_state = 52}, [3631] = {.lex_state = 33}, - [3632] = {.lex_state = 2}, + [3632] = {.lex_state = 1855}, [3633] = {.lex_state = 343}, [3634] = {.lex_state = 343}, [3635] = {.lex_state = 343}, @@ -244133,7 +244159,7 @@ static const uint16_t ts_small_parse_table[] = { ACTIONS(3), 1, anon_sym_POUND, ACTIONS(5618), 1, - aux_sym_shebang_token1, + aux_sym_comment_token1, STATE(3632), 1, sym_comment, [145670] = 3, @@ -250519,7 +250545,7 @@ static const TSParseActionEntry ts_parse_actions[] = { [5612] = {.entry = {.count = 1, .reusable = true}}, SHIFT(2079), [5614] = {.entry = {.count = 1, .reusable = true}}, SHIFT(2722), [5616] = {.entry = {.count = 1, .reusable = true}}, SHIFT(3676), - [5618] = {.entry = {.count = 1, .reusable = true}}, SHIFT(3763), + [5618] = {.entry = {.count = 1, .reusable = false}}, SHIFT(3763), [5620] = {.entry = {.count = 1, .reusable = true}}, SHIFT(239), [5622] = {.entry = {.count = 1, .reusable = true}}, SHIFT(1158), [5624] = {.entry = {.count = 1, .reusable = true}}, SHIFT(1891),