Skip to content

Commit

Permalink
refactor: use C for the external scanner (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq authored Jun 16, 2023
1 parent 399605a commit 24ed4fd
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 27 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",
# If your language uses an external scanner, add it here.
"src/scanner.c",
],
"cflags_c": [
"-std=c99",
Expand Down
21 changes: 1 addition & 20 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,18 @@ 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: 2 additions & 6 deletions src/scanner.cc → src/scanner.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#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 (std::iswspace(lexer->lookahead)) {
while (iswspace(lexer->lookahead)) {
skip(lexer);
}
}
Expand Down Expand Up @@ -72,8 +71,6 @@ 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 @@ -87,4 +84,3 @@ 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 24ed4fd

Please sign in to comment.