Skip to content

Commit

Permalink
Revert "refactor: use C for the external scanner (#13)" (#15)
Browse files Browse the repository at this point in the history
This reverts commit 24ed4fd.
  • Loading branch information
uyha authored Jun 18, 2023
1 parent 24ed4fd commit 4e210a9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 20 additions & 1 deletion bindings/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,37 @@ 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")
.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());
}
8 changes: 6 additions & 2 deletions src/scanner.c → src/scanner.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <cwctype>
#include <tree_sitter/parser.h>
#include <wctype.h>

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);
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
}

0 comments on commit 4e210a9

Please sign in to comment.