From 4e210a9ca6ffd6c87ca5fbf82c485f5753a56452 Mon Sep 17 00:00:00 2001 From: Beartama <8091245+uyha@users.noreply.github.com> Date: Sun, 18 Jun 2023 15:52:52 +0300 Subject: [PATCH] Revert "refactor: use C for the external scanner (#13)" (#15) This reverts commit 24ed4fd565cd95fcba4f42e995cf186cab180ccb. --- binding.gyp | 2 +- bindings/rust/build.rs | 21 ++++++++++++++++++++- src/{scanner.c => scanner.cc} | 8 ++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) rename src/{scanner.c => scanner.cc} (95%) diff --git a/binding.gyp b/binding.gyp index 382a0c9c89..a2beb8a058 100644 --- a/binding.gyp +++ b/binding.gyp @@ -9,7 +9,7 @@ "sources": [ "bindings/node/binding.cc", "src/parser.c", - "src/scanner.c", + # If your language uses an external scanner, add it here. ], "cflags_c": [ "-std=c99", diff --git a/bindings/rust/build.rs b/bindings/rust/build.rs index 8851fed13a..618e90a3ac 100644 --- a/bindings/rust/build.rs +++ b/bindings/rust/build.rs @@ -2,7 +2,7 @@ fn main() { let src_dir = std::path::Path::new("src"); let mut c_config = cc::Build::new(); - c_config.include(src_dir); + c_config.include(&src_dir); c_config .flag_if_supported("-Wno-unused-parameter") .flag_if_supported("-Wno-unused-but-set-variable") @@ -10,10 +10,29 @@ fn main() { 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/src/scanner.c b/src/scanner.cc similarity index 95% rename from src/scanner.c rename to src/scanner.cc index 93651805a9..886bb3317f 100644 --- a/src/scanner.c +++ b/src/scanner.cc @@ -1,11 +1,12 @@ +#include #include -#include +namespace { enum TokenType { BRACKET_ARGUMENT, BRACKET_COMMENT, LINE_COMMENT }; void skip(TSLexer *lexer) { lexer->advance(lexer, true); } void advance(TSLexer *lexer) { lexer->advance(lexer, false); } void skip_wspace(TSLexer *lexer) { - while (iswspace(lexer->lookahead)) { + while (std::iswspace(lexer->lookahead)) { skip(lexer); } } @@ -71,6 +72,8 @@ bool scan(void *payload, TSLexer *lexer, bool const *valid_symbols) { return false; } +} // namespace +extern "C" { void *tree_sitter_cmake_external_scanner_create() { return NULL; } void tree_sitter_cmake_external_scanner_destroy(void *payload) {} unsigned tree_sitter_cmake_external_scanner_serialize(void *payload, @@ -84,3 +87,4 @@ bool tree_sitter_cmake_external_scanner_scan(void *payload, TSLexer *lexer, bool const *valid_symbols) { return scan(payload, lexer, valid_symbols); } +}