Skip to content

Commit

Permalink
feat!: remove C++ support for external scanners
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Sep 8, 2024
1 parent fd190f1 commit 9301d38
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 33 deletions.
27 changes: 4 additions & 23 deletions cli/loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,15 +609,10 @@ impl Loader {
.host(BUILD_HOST)
.debug(self.debug_build)
.file(&config.parser_path)
.includes(&config.header_paths);
.includes(&config.header_paths)
.std("c11");

if let Some(scanner_path) = config.scanner_path.as_ref() {
if scanner_path.extension() != Some("c".as_ref()) {
cc_config.cpp(true);
eprintln!("Warning: Using a C++ scanner is now deprecated. Please migrate your scanner code to C, as C++ support will be removed in the near future.");
} else {
cc_config.std("c11");
}
cc_config.file(scanner_path);
}

Expand Down Expand Up @@ -882,14 +877,6 @@ impl Loader {
]);

if let Some(scanner_filename) = scanner_filename {
if scanner_filename
.extension()
.and_then(|ext| ext.to_str())
.map_or(false, |ext| ["cc", "cpp"].contains(&ext))
{
eprintln!("Warning: Using a C++ scanner is now deprecated. Please migrate your scanner code to C, as C++ support will be removed in the near future.");
command.arg("-xc++");
}
command.arg(scanner_filename);
}

Expand Down Expand Up @@ -1204,14 +1191,8 @@ impl Loader {

#[must_use]
pub fn get_scanner_path(&self, src_path: &Path) -> Option<PathBuf> {
let mut path = src_path.join("scanner.c");
for extension in ["c", "cc", "cpp"] {
path.set_extension(extension);
if path.exists() {
return Some(path);
}
}
None
let path = src_path.join("scanner.c");
path.exists().then_some(path)
}
}

Expand Down
13 changes: 3 additions & 10 deletions docs/section-3-creating-parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Developing Tree-sitter grammars can have a difficult learning curve, but once yo
In order to develop a Tree-sitter parser, there are two dependencies that you need to install:

* **Node.js** - Tree-sitter grammars are written in JavaScript, and Tree-sitter uses [Node.js][node.js] to interpret JavaScript files. It requires the `node` command to be in one of the directories in your [`PATH`][path-env]. You'll need Node.js version 6.0 or greater.
* **A C Compiler** - Tree-sitter creates parsers that are written in C. In order to run and test these parsers with the `tree-sitter parse` or `tree-sitter test` commands, you must have a C/C++ compiler installed. Tree-sitter will try to look for these compilers in the standard places for each platform.
* **A C Compiler** - Tree-sitter creates parsers that are written in C. In order to run and test these parsers with the `tree-sitter parse` or `tree-sitter test` commands, you must have a C compiler installed. Tree-sitter will try to look for these compilers in the standard places for each platform.

### Installation

Expand Down Expand Up @@ -766,14 +766,7 @@ grammar({
});
```

Then, add another C or C++ source file to your project. Currently, its path must be `src/scanner.c` or `src/scanner.cc` for the CLI to recognize it. Be sure to add this file to the `sources` section of your `binding.gyp` file so that it will be included when your project is compiled by Node.js and uncomment the appropriate block in your `bindings/rust/build.rs` file so that it will be included in your Rust crate.

> **Note**
>
> C++ scanners are now deprecated and will be removed in the near future.
> While it is currently possible to write an external scanner in C++, it can be difficult
> to get working cross-platform and introduces extra requirements; therefore it
> is *greatly* preferred to use C.
Then, add another C source file to your project. Currently, its path must be `src/scanner.c` for the CLI to recognize it. Be sure to add this file to the `sources` section of your `binding.gyp` file so that it will be included when your project is compiled by Node.js and uncomment the appropriate block in your `bindings/rust/build.rs` file so that it will be included in your Rust crate.

In this new source file, define an [`enum`][enum] type containing the names of all of your external tokens. The ordering of this enum must match the order in your grammar's `externals` array; the actual names do not matter.

Expand All @@ -789,7 +782,7 @@ enum TokenType {
}
```

Finally, you must define five functions with specific names, based on your language's name and five actions: *create*, *destroy*, *serialize*, *deserialize*, and *scan*. These functions must all use [C linkage][c-linkage], so if you're writing the scanner in C++, you need to declare them with the `extern "C"` qualifier.
Finally, you must define five functions with specific names, based on your language's name and five actions: *create*, *destroy*, *serialize*, *deserialize*, and *scan*.

#### Create

Expand Down

0 comments on commit 9301d38

Please sign in to comment.