From 0a21eee3f0d7771e1ad90c2c8392b8798860f8c8 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 8 May 2014 13:14:45 -0700 Subject: [PATCH] Remove magic number from generated symbols enums The symbol numbers 0 and 1 are reserved for 'error' and 'eof', so the grammar's start symbol is always 2. --- examples/parsers/arithmetic.c | 32 ++--- examples/parsers/golang.c | 128 ++++++++++---------- examples/parsers/javascript.c | 170 +++++++++++++-------------- examples/parsers/json.c | 32 ++--- include/tree_sitter/runtime.h | 1 + src/compiler/generate_code/c_code.cc | 11 +- 6 files changed, 190 insertions(+), 184 deletions(-) diff --git a/examples/parsers/arithmetic.c b/examples/parsers/arithmetic.c index f2526d80..c5e871be 100644 --- a/examples/parsers/arithmetic.c +++ b/examples/parsers/arithmetic.c @@ -4,22 +4,22 @@ #define SYMBOL_COUNT 18 enum { - ts_sym_expression = 2, - ts_sym_sum = 3, - ts_sym_difference = 4, - ts_sym_product = 5, - ts_sym_quotient = 6, - ts_sym_exponent = 7, - ts_sym_group = 8, - ts_sym_number = 9, - ts_sym_variable = 10, - ts_aux_sym_token0 = 11, - ts_aux_sym_token1 = 12, - ts_aux_sym_token2 = 13, - ts_aux_sym_token3 = 14, - ts_aux_sym_token4 = 15, - ts_aux_sym_token5 = 16, - ts_aux_sym_token6 = 17, + ts_sym_expression = ts_start_sym, + ts_sym_sum, + ts_sym_difference, + ts_sym_product, + ts_sym_quotient, + ts_sym_exponent, + ts_sym_group, + ts_sym_number, + ts_sym_variable, + ts_aux_sym_token0, + ts_aux_sym_token1, + ts_aux_sym_token2, + ts_aux_sym_token3, + ts_aux_sym_token4, + ts_aux_sym_token5, + ts_aux_sym_token6, }; SYMBOL_NAMES = { diff --git a/examples/parsers/golang.c b/examples/parsers/golang.c index a5534063..ffda1661 100644 --- a/examples/parsers/golang.c +++ b/examples/parsers/golang.c @@ -4,70 +4,70 @@ #define SYMBOL_COUNT 66 enum { - ts_sym_program = 2, - ts_sym_package_directive = 3, - ts_sym_imports_block = 4, - ts_sym_package_import = 5, - ts_sym_declaration = 6, - ts_sym_type_declaration = 7, - ts_sym_var_declaration = 8, - ts_sym_func_declaration = 9, - ts_sym_statement_block = 10, - ts_sym_type_expression = 11, - ts_sym_pointer_type = 12, - ts_sym_map_type = 13, - ts_sym_slice_type = 14, - ts_sym_struct_type = 15, - ts_sym_interface_type = 16, - ts_sym_expression = 17, - ts_sym_math_op = 18, - ts_sym_bool_op = 19, - ts_sym__func_signature = 20, - ts_sym_package_name = 21, - ts_sym_var_name = 22, - ts_sym_type_name = 23, - ts_sym_string = 24, - ts_sym__identifier = 25, - ts_sym_number = 26, - ts_sym_comment = 27, - ts_aux_sym_program_repeat0 = 28, - ts_aux_sym_program_repeat1 = 29, - ts_aux_sym_imports_block_repeat0 = 30, - ts_aux_sym_struct_type_repeat0 = 31, - ts_aux_sym_interface_type_repeat0 = 32, - ts_aux_sym__func_signature_repeat0 = 33, - ts_aux_sym__func_signature_repeat1 = 34, - ts_aux_sym__func_signature_repeat2 = 35, - ts_aux_sym__func_signature_repeat3 = 36, - ts_aux_sym__func_signature_repeat4 = 37, - ts_aux_sym_token0 = 38, - ts_aux_sym_token1 = 39, - ts_aux_sym_token2 = 40, - ts_aux_sym_token3 = 41, - ts_aux_sym_token4 = 42, - ts_aux_sym_token5 = 43, - ts_aux_sym_token6 = 44, - ts_aux_sym_token7 = 45, - ts_aux_sym_token8 = 46, - ts_aux_sym_token9 = 47, - ts_aux_sym_token10 = 48, - ts_aux_sym_token11 = 49, - ts_aux_sym_token12 = 50, - ts_aux_sym_token13 = 51, - ts_aux_sym_token14 = 52, - ts_aux_sym_token15 = 53, - ts_aux_sym_token16 = 54, - ts_aux_sym_token17 = 55, - ts_aux_sym_token18 = 56, - ts_aux_sym_token19 = 57, - ts_aux_sym_token20 = 58, - ts_aux_sym_token21 = 59, - ts_aux_sym_token22 = 60, - ts_aux_sym_token23 = 61, - ts_aux_sym_token24 = 62, - ts_aux_sym_token25 = 63, - ts_aux_sym_token26 = 64, - ts_aux_sym_token27 = 65, + ts_sym_program = ts_start_sym, + ts_sym_package_directive, + ts_sym_imports_block, + ts_sym_package_import, + ts_sym_declaration, + ts_sym_type_declaration, + ts_sym_var_declaration, + ts_sym_func_declaration, + ts_sym_statement_block, + ts_sym_type_expression, + ts_sym_pointer_type, + ts_sym_map_type, + ts_sym_slice_type, + ts_sym_struct_type, + ts_sym_interface_type, + ts_sym_expression, + ts_sym_math_op, + ts_sym_bool_op, + ts_sym__func_signature, + ts_sym_package_name, + ts_sym_var_name, + ts_sym_type_name, + ts_sym_string, + ts_sym__identifier, + ts_sym_number, + ts_sym_comment, + ts_aux_sym_program_repeat0, + ts_aux_sym_program_repeat1, + ts_aux_sym_imports_block_repeat0, + ts_aux_sym_struct_type_repeat0, + ts_aux_sym_interface_type_repeat0, + ts_aux_sym__func_signature_repeat0, + ts_aux_sym__func_signature_repeat1, + ts_aux_sym__func_signature_repeat2, + ts_aux_sym__func_signature_repeat3, + ts_aux_sym__func_signature_repeat4, + ts_aux_sym_token0, + ts_aux_sym_token1, + ts_aux_sym_token2, + ts_aux_sym_token3, + ts_aux_sym_token4, + ts_aux_sym_token5, + ts_aux_sym_token6, + ts_aux_sym_token7, + ts_aux_sym_token8, + ts_aux_sym_token9, + ts_aux_sym_token10, + ts_aux_sym_token11, + ts_aux_sym_token12, + ts_aux_sym_token13, + ts_aux_sym_token14, + ts_aux_sym_token15, + ts_aux_sym_token16, + ts_aux_sym_token17, + ts_aux_sym_token18, + ts_aux_sym_token19, + ts_aux_sym_token20, + ts_aux_sym_token21, + ts_aux_sym_token22, + ts_aux_sym_token23, + ts_aux_sym_token24, + ts_aux_sym_token25, + ts_aux_sym_token26, + ts_aux_sym_token27, }; SYMBOL_NAMES = { diff --git a/examples/parsers/javascript.c b/examples/parsers/javascript.c index 6767f4ea..48f63269 100644 --- a/examples/parsers/javascript.c +++ b/examples/parsers/javascript.c @@ -4,91 +4,91 @@ #define SYMBOL_COUNT 87 enum { - ts_sym_program = 2, - ts_sym_statement = 3, - ts_sym_statement_block = 4, - ts_sym_for_statement = 5, - ts_sym_if_statement = 6, - ts_sym_switch_statement = 7, - ts_sym_switch_case = 8, - ts_sym_break_statement = 9, - ts_sym_var_declaration = 10, - ts_sym_expression_statement = 11, - ts_sym_return_statement = 12, - ts_sym_delete_statement = 13, - ts_sym_expression = 14, - ts_sym_math_op = 15, - ts_sym_bool_op = 16, - ts_sym_ternary = 17, - ts_sym_assignment = 18, - ts_sym_function_expression = 19, - ts_sym_function_call = 20, - ts_sym_constructor_call = 21, - ts_sym_property_access = 22, - ts_sym_formal_parameters = 23, - ts_sym_object = 24, - ts_sym_array = 25, - ts_sym_comment = 26, - ts_sym__terminator = 27, - ts_sym_regex = 28, - ts_sym_string = 29, - ts_sym_identifier = 30, - ts_sym_number = 31, - ts_sym_null = 32, - ts_sym_true = 33, - ts_sym_false = 34, - ts_aux_sym_program_repeat0 = 35, - ts_aux_sym_statement_block_repeat0 = 36, - ts_aux_sym_switch_statement_repeat0 = 37, - ts_aux_sym_switch_case_repeat0 = 38, - ts_aux_sym_var_declaration_repeat0 = 39, - ts_aux_sym_function_call_repeat0 = 40, - ts_aux_sym_formal_parameters_repeat0 = 41, - ts_aux_sym_object_repeat0 = 42, - ts_aux_sym_array_repeat0 = 43, - ts_aux_sym_token0 = 44, - ts_aux_sym_token1 = 45, - ts_aux_sym_token2 = 46, - ts_aux_sym_token3 = 47, - ts_aux_sym_token4 = 48, - ts_aux_sym_token5 = 49, - ts_aux_sym_token6 = 50, - ts_aux_sym_token7 = 51, - ts_aux_sym_token8 = 52, - ts_aux_sym_token9 = 53, - ts_aux_sym_token10 = 54, - ts_aux_sym_token11 = 55, - ts_aux_sym_token12 = 56, - ts_aux_sym_token13 = 57, - ts_aux_sym_token14 = 58, - ts_aux_sym_token15 = 59, - ts_aux_sym_token16 = 60, - ts_aux_sym_token17 = 61, - ts_aux_sym_token18 = 62, - ts_aux_sym_token19 = 63, - ts_aux_sym_token20 = 64, - ts_aux_sym_token21 = 65, - ts_aux_sym_token22 = 66, - ts_aux_sym_token23 = 67, - ts_aux_sym_token24 = 68, - ts_aux_sym_token25 = 69, - ts_aux_sym_token26 = 70, - ts_aux_sym_token27 = 71, - ts_aux_sym_token28 = 72, - ts_aux_sym_token29 = 73, - ts_aux_sym_token30 = 74, - ts_aux_sym_token31 = 75, - ts_aux_sym_token32 = 76, - ts_aux_sym_token33 = 77, - ts_aux_sym_token34 = 78, - ts_aux_sym_token35 = 79, - ts_aux_sym_token36 = 80, - ts_aux_sym_token37 = 81, - ts_aux_sym_token38 = 82, - ts_aux_sym_token39 = 83, - ts_aux_sym_token40 = 84, - ts_aux_sym_token41 = 85, - ts_aux_sym_token42 = 86, + ts_sym_program = ts_start_sym, + ts_sym_statement, + ts_sym_statement_block, + ts_sym_for_statement, + ts_sym_if_statement, + ts_sym_switch_statement, + ts_sym_switch_case, + ts_sym_break_statement, + ts_sym_var_declaration, + ts_sym_expression_statement, + ts_sym_return_statement, + ts_sym_delete_statement, + ts_sym_expression, + ts_sym_math_op, + ts_sym_bool_op, + ts_sym_ternary, + ts_sym_assignment, + ts_sym_function_expression, + ts_sym_function_call, + ts_sym_constructor_call, + ts_sym_property_access, + ts_sym_formal_parameters, + ts_sym_object, + ts_sym_array, + ts_sym_comment, + ts_sym__terminator, + ts_sym_regex, + ts_sym_string, + ts_sym_identifier, + ts_sym_number, + ts_sym_null, + ts_sym_true, + ts_sym_false, + ts_aux_sym_program_repeat0, + ts_aux_sym_statement_block_repeat0, + ts_aux_sym_switch_statement_repeat0, + ts_aux_sym_switch_case_repeat0, + ts_aux_sym_var_declaration_repeat0, + ts_aux_sym_function_call_repeat0, + ts_aux_sym_formal_parameters_repeat0, + ts_aux_sym_object_repeat0, + ts_aux_sym_array_repeat0, + ts_aux_sym_token0, + ts_aux_sym_token1, + ts_aux_sym_token2, + ts_aux_sym_token3, + ts_aux_sym_token4, + ts_aux_sym_token5, + ts_aux_sym_token6, + ts_aux_sym_token7, + ts_aux_sym_token8, + ts_aux_sym_token9, + ts_aux_sym_token10, + ts_aux_sym_token11, + ts_aux_sym_token12, + ts_aux_sym_token13, + ts_aux_sym_token14, + ts_aux_sym_token15, + ts_aux_sym_token16, + ts_aux_sym_token17, + ts_aux_sym_token18, + ts_aux_sym_token19, + ts_aux_sym_token20, + ts_aux_sym_token21, + ts_aux_sym_token22, + ts_aux_sym_token23, + ts_aux_sym_token24, + ts_aux_sym_token25, + ts_aux_sym_token26, + ts_aux_sym_token27, + ts_aux_sym_token28, + ts_aux_sym_token29, + ts_aux_sym_token30, + ts_aux_sym_token31, + ts_aux_sym_token32, + ts_aux_sym_token33, + ts_aux_sym_token34, + ts_aux_sym_token35, + ts_aux_sym_token36, + ts_aux_sym_token37, + ts_aux_sym_token38, + ts_aux_sym_token39, + ts_aux_sym_token40, + ts_aux_sym_token41, + ts_aux_sym_token42, }; SYMBOL_NAMES = { diff --git a/examples/parsers/json.c b/examples/parsers/json.c index bb40c3b0..db4fb1f4 100644 --- a/examples/parsers/json.c +++ b/examples/parsers/json.c @@ -4,22 +4,22 @@ #define SYMBOL_COUNT 18 enum { - ts_sym_value = 2, - ts_sym_object = 3, - ts_sym_array = 4, - ts_sym_string = 5, - ts_sym_number = 6, - ts_sym_null = 7, - ts_sym_true = 8, - ts_sym_false = 9, - ts_aux_sym_object_repeat0 = 10, - ts_aux_sym_array_repeat0 = 11, - ts_aux_sym_token0 = 12, - ts_aux_sym_token1 = 13, - ts_aux_sym_token2 = 14, - ts_aux_sym_token3 = 15, - ts_aux_sym_token4 = 16, - ts_aux_sym_token5 = 17, + ts_sym_value = ts_start_sym, + ts_sym_object, + ts_sym_array, + ts_sym_string, + ts_sym_number, + ts_sym_null, + ts_sym_true, + ts_sym_false, + ts_aux_sym_object_repeat0, + ts_aux_sym_array_repeat0, + ts_aux_sym_token0, + ts_aux_sym_token1, + ts_aux_sym_token2, + ts_aux_sym_token3, + ts_aux_sym_token4, + ts_aux_sym_token5, }; SYMBOL_NAMES = { diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index f2dbb09f..d460f868 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -10,6 +10,7 @@ extern "C" { typedef unsigned short ts_symbol; #define ts_builtin_sym_error 0 #define ts_builtin_sym_end 1 +#define ts_start_sym 2 typedef struct ts_tree ts_tree; ts_tree * ts_tree_make_leaf(ts_symbol symbol, size_t size, size_t offset); diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index 805bf4d9..c8829c1e 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -204,10 +204,15 @@ namespace tree_sitter { string symbol_enum() { string result = "enum {\n"; - size_t index = 2; + bool at_start = true; for (auto symbol : parse_table.symbols) - if (!symbol.is_built_in()) - result += indent(symbol_id(symbol)) + " = " + to_string(index++) + ",\n"; + if (!symbol.is_built_in()) { + if (at_start) + result += indent(symbol_id(symbol)) + " = ts_start_sym,\n"; + else + result += indent(symbol_id(symbol)) + ",\n"; + at_start = false; + } return result + "};"; }