diff --git a/.gitignore b/.gitignore index 39e4380..7d5b7a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,2 @@ /build /node_modules - -/bindings/rust -/Cargo.toml diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a12eebf --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "tree-sitter-yaml" +description = "yaml grammar for the tree-sitter parsing library" +version = "0.0.1" +keywords = ["incremental", "parsing", "yaml"] +categories = ["parsing", "text-editors"] +repository = "https://github.com/tree-sitter/tree-sitter-yaml" +edition = "2018" +license = "MIT" + +build = "bindings/rust/build.rs" +include = [ + "bindings/rust/*", + "grammar.js", + "queries/*", + "src/*", +] + +[lib] +path = "bindings/rust/lib.rs" + +[dependencies] +tree-sitter = "~0.20" + +[build-dependencies] +cc = "1.0" diff --git a/bindings/rust/build.rs b/bindings/rust/build.rs new file mode 100644 index 0000000..618e90a --- /dev/null +++ b/bindings/rust/build.rs @@ -0,0 +1,38 @@ +fn main() { + let src_dir = std::path::Path::new("src"); + + let mut c_config = cc::Build::new(); + c_config.include(&src_dir); + c_config + .flag_if_supported("-Wno-unused-parameter") + .flag_if_supported("-Wno-unused-but-set-variable") + .flag_if_supported("-Wno-trigraphs"); + let parser_path = src_dir.join("parser.c"); + c_config.file(&parser_path); + + // If your language uses an external scanner written in C, + // then include this block of code: + + /* + let scanner_path = src_dir.join("scanner.c"); + c_config.file(&scanner_path); + println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); + */ + + c_config.compile("parser"); + println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap()); + + // If your language uses an external scanner written in C++, + // then include this block of code: + + let mut cpp_config = cc::Build::new(); + cpp_config.cpp(true); + cpp_config.include(&src_dir); + cpp_config + .flag_if_supported("-Wno-unused-parameter") + .flag_if_supported("-Wno-unused-but-set-variable"); + let scanner_path = src_dir.join("scanner.cc"); + cpp_config.file(&scanner_path); + cpp_config.compile("scanner"); + println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); +} diff --git a/bindings/rust/lib.rs b/bindings/rust/lib.rs new file mode 100644 index 0000000..6fc1f62 --- /dev/null +++ b/bindings/rust/lib.rs @@ -0,0 +1,52 @@ +//! This crate provides yaml language support for the [tree-sitter][] parsing library. +//! +//! Typically, you will use the [language][language func] function to add this language to a +//! tree-sitter [Parser][], and then use the parser to parse some code: +//! +//! ``` +//! let code = ""; +//! let mut parser = tree_sitter::Parser::new(); +//! parser.set_language(tree_sitter_yaml::language()).expect("Error loading yaml grammar"); +//! let tree = parser.parse(code, None).unwrap(); +//! ``` +//! +//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html +//! [language func]: fn.language.html +//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html +//! [tree-sitter]: https://tree-sitter.github.io/ + +use tree_sitter::Language; + +extern "C" { + fn tree_sitter_yaml() -> Language; +} + +/// Get the tree-sitter [Language][] for this grammar. +/// +/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html +pub fn language() -> Language { + unsafe { tree_sitter_yaml() } +} + +/// The content of the [`node-types.json`][] file for this grammar. +/// +/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types +pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json"); + +// Uncomment these to include any queries that this grammar contains + +// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm"); +// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm"); +// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm"); +// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm"); + +#[cfg(test)] +mod tests { + #[test] + fn test_can_load_grammar() { + let mut parser = tree_sitter::Parser::new(); + parser + .set_language(super::language()) + .expect("Error loading yaml language"); + } +} diff --git a/src/parser.c b/src/parser.c index 24f8363..02b85f7 100644 --- a/src/parser.c +++ b/src/parser.c @@ -311,7 +311,7 @@ enum { aux_sym__br_mtl_sqt_ctn_repeat1 = 292, }; -static const char *ts_symbol_names[] = { +static const char * const ts_symbol_names[] = { [ts_builtin_sym_end] = "end", [sym__eof] = "_eof", [sym__s_dir_yml_bgn] = "_s_dir_yml_bgn", @@ -607,7 +607,7 @@ static const char *ts_symbol_names[] = { [aux_sym__br_mtl_sqt_ctn_repeat1] = "_br_mtl_sqt_ctn_repeat1", }; -static TSSymbol ts_symbol_map[] = { +static const TSSymbol ts_symbol_map[] = { [ts_builtin_sym_end] = ts_builtin_sym_end, [sym__eof] = sym__eof, [sym__s_dir_yml_bgn] = sym__s_dir_yml_bgn, @@ -2083,7 +2083,7 @@ enum { field_value = 2, }; -static const char *ts_field_names[] = { +static const char * const ts_field_names[] = { [0] = NULL, [field_key] = "key", [field_value] = "value", @@ -2125,7 +2125,7 @@ static const TSFieldMapEntry ts_field_map_entries[] = { {field_value, 1, .inherited = true}, }; -static TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGTH] = { +static const TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGTH] = { [0] = {0}, [6] = { [0] = sym__r_flw_exp_par, @@ -2138,7 +2138,7 @@ static TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGT }, }; -static uint16_t ts_non_terminal_alias_map[] = { +static const uint16_t ts_non_terminal_alias_map[] = { sym__bgn_imp_doc, 2, sym__bgn_imp_doc, sym__drs_doc_end, @@ -2194,7 +2194,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) { } } -static TSLexMode ts_lex_modes[STATE_COUNT] = { +static const TSLexMode ts_lex_modes[STATE_COUNT] = { [0] = {.lex_state = 0, .external_lex_state = 1}, [1] = {.lex_state = 0, .external_lex_state = 2}, [2] = {.lex_state = 0, .external_lex_state = 3}, @@ -3884,7 +3884,7 @@ enum { ts_external_token_comment = 104, }; -static TSSymbol ts_external_scanner_symbol_map[EXTERNAL_TOKEN_COUNT] = { +static const TSSymbol ts_external_scanner_symbol_map[EXTERNAL_TOKEN_COUNT] = { [ts_external_token__eof] = sym__eof, [ts_external_token__s_dir_yml_bgn] = sym__s_dir_yml_bgn, [ts_external_token__r_dir_yml_ver] = sym__r_dir_yml_ver, @@ -3992,7 +3992,7 @@ static TSSymbol ts_external_scanner_symbol_map[EXTERNAL_TOKEN_COUNT] = { [ts_external_token_comment] = sym_comment, }; -static bool ts_external_scanner_states[110][EXTERNAL_TOKEN_COUNT] = { +static const bool ts_external_scanner_states[110][EXTERNAL_TOKEN_COUNT] = { [1] = { [ts_external_token__eof] = true, [ts_external_token__s_dir_yml_bgn] = true, @@ -6255,7 +6255,7 @@ static bool ts_external_scanner_states[110][EXTERNAL_TOKEN_COUNT] = { }, }; -static uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { +static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { [0] = { [ts_builtin_sym_end] = ACTIONS(1), [sym__eof] = ACTIONS(1), @@ -14181,7 +14181,7 @@ static uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { }, }; -static uint16_t ts_small_parse_table[] = { +static const uint16_t ts_small_parse_table[] = { [0] = 37, ACTIONS(3), 1, sym_comment, @@ -35923,7 +35923,7 @@ static uint16_t ts_small_parse_table[] = { sym__r_acr_ctn, }; -static uint32_t ts_small_parse_table_map[] = { +static const uint32_t ts_small_parse_table_map[] = { [SMALL_STATE(88)] = 0, [SMALL_STATE(89)] = 139, [SMALL_STATE(90)] = 278, @@ -37417,7 +37417,7 @@ static uint32_t ts_small_parse_table_map[] = { [SMALL_STATE(1578)] = 27583, }; -static TSParseActionEntry ts_parse_actions[] = { +static const TSParseActionEntry ts_parse_actions[] = { [0] = {.entry = {.count = 0, .reusable = false}}, [1] = {.entry = {.count = 1, .reusable = false}}, RECOVER(), [3] = {.entry = {.count = 1, .reusable = true}}, SHIFT_EXTRA(), @@ -38709,7 +38709,7 @@ void tree_sitter_yaml_external_scanner_deserialize(void *, const char *, unsigne #endif extern const TSLanguage *tree_sitter_yaml(void) { - static TSLanguage language = { + static const TSLanguage language = { .version = LANGUAGE_VERSION, .symbol_count = SYMBOL_COUNT, .alias_count = ALIAS_COUNT, @@ -38720,22 +38720,22 @@ extern const TSLanguage *tree_sitter_yaml(void) { .production_id_count = PRODUCTION_ID_COUNT, .field_count = FIELD_COUNT, .max_alias_sequence_length = MAX_ALIAS_SEQUENCE_LENGTH, - .parse_table = (const uint16_t *)ts_parse_table, - .small_parse_table = (const uint16_t *)ts_small_parse_table, - .small_parse_table_map = (const uint32_t *)ts_small_parse_table_map, + .parse_table = &ts_parse_table[0][0], + .small_parse_table = ts_small_parse_table, + .small_parse_table_map = ts_small_parse_table_map, .parse_actions = ts_parse_actions, .symbol_names = ts_symbol_names, .field_names = ts_field_names, - .field_map_slices = (const TSFieldMapSlice *)ts_field_map_slices, - .field_map_entries = (const TSFieldMapEntry *)ts_field_map_entries, + .field_map_slices = ts_field_map_slices, + .field_map_entries = ts_field_map_entries, .symbol_metadata = ts_symbol_metadata, .public_symbol_map = ts_symbol_map, .alias_map = ts_non_terminal_alias_map, - .alias_sequences = (const TSSymbol *)ts_alias_sequences, + .alias_sequences = &ts_alias_sequences[0][0], .lex_modes = ts_lex_modes, .lex_fn = ts_lex, .external_scanner = { - (const bool *)ts_external_scanner_states, + &ts_external_scanner_states[0][0], ts_external_scanner_symbol_map, tree_sitter_yaml_external_scanner_create, tree_sitter_yaml_external_scanner_destroy, diff --git a/src/tree_sitter/parser.h b/src/tree_sitter/parser.h index a3a87bd..cbbc7b4 100644 --- a/src/tree_sitter/parser.h +++ b/src/tree_sitter/parser.h @@ -102,8 +102,8 @@ struct TSLanguage { const uint16_t *small_parse_table; const uint32_t *small_parse_table_map; const TSParseActionEntry *parse_actions; - const char **symbol_names; - const char **field_names; + const char * const *symbol_names; + const char * const *field_names; const TSFieldMapSlice *field_map_slices; const TSFieldMapEntry *field_map_entries; const TSSymbolMetadata *symbol_metadata;