Skip to content

Commit

Permalink
prove it works via toml
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Jul 10, 2024
1 parent 2a0cc34 commit 288e1f3
Show file tree
Hide file tree
Showing 12 changed files with 2,277 additions and 3,813 deletions.
19 changes: 8 additions & 11 deletions resources/language-metavariables/tree-sitter-toml/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
[package]
name = "tree-sitter-toml"
description = "toml grammar for the tree-sitter parsing library"
description = "Toml grammar for tree-sitter"
version = "0.0.1"
keywords = ["incremental", "parsing", "toml"]
license = "MIT"
readme = "README.md"
keywords = ["incremental", "parsing", "tree-sitter", "toml"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-toml"
edition = "2018"
license = "MIT"
edition = "2021"
autoexamples = false

build = "bindings/rust/build.rs"
include = [
"bindings/rust/*",
"grammar.js",
"queries/*",
"src/*",
]
include = ["bindings/rust/*", "grammar.js", "queries/*", "src/*"]

[lib]
path = "bindings/rust/lib.rs"
Expand All @@ -23,4 +20,4 @@ path = "bindings/rust/lib.rs"
tree-sitter = "~0.20"

[build-dependencies]
cc = "1.0"
cc = "1.0.87"
25 changes: 18 additions & 7 deletions resources/language-metavariables/tree-sitter-toml/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@
"targets": [
{
"target_name": "tree_sitter_toml_binding",
"dependencies": [
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
],
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
"src",
],
"sources": [
"bindings/node/binding.cc",
"src/parser.c",
"src/scanner.c",
"bindings/node/binding.cc"
# NOTE: if your language has an external scanner, add it here.
],
"conditions": [
["OS!='win'", {
"cflags_c": [
"-std=c11",
],
}, { # OS == "win"
"cflags_c": [
"/std:c11",
"/utf-8",
],
}],
],
"cflags_c": [
"-std=c99",
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
#include <napi.h>

using namespace v8;
typedef struct TSLanguage TSLanguage;

extern "C" TSLanguage * tree_sitter_toml();
extern "C" TSLanguage *tree_sitter_toml();

namespace {
// "tree-sitter", "language" hashed with BLAKE2
const napi_type_tag LANGUAGE_TYPE_TAG = {
0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
};

NAN_METHOD(New) {}

void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_toml());

Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("toml").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["name"] = Napi::String::New(env, "toml");
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_toml());
language.TypeTag(&LANGUAGE_TYPE_TAG);
exports["language"] = language;
return exports;
}

NODE_MODULE(tree_sitter_toml_binding, Init)

} // namespace
NODE_API_MODULE(tree_sitter_toml_binding, Init)
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
try {
module.exports = require("../../build/Release/tree_sitter_toml_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_toml_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}
const root = require("path").join(__dirname, "..", "..");

module.exports = require("node-gyp-build")(root);

try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,21 @@ 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("-w")
.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);
c_config.std("c11").include(src_dir);

let scanner_path = src_dir.join("scanner.c");
c_config.file(&scanner_path);
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
#[cfg(target_env = "msvc")]
c_config.flag("-utf-8");

c_config.compile("parser");
let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);
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:

// NOTE: if your language uses an external scanner, uncomment this block:
/*
let mut cpp_config = cc::Build::new();
cpp_config.cpp(true);
cpp_config.include(&src_dir);
cpp_config
.flag_if_supported("-w")
.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");
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("tree-sitter-toml");
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! This crate provides toml language support for the [tree-sitter][] parsing library.
//! This crate provides Toml 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 code = r#"
//! "#;
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_toml::language()).expect("Error loading toml grammar");
//! parser.set_language(&tree_sitter_toml::language()).expect("Error loading Toml grammar");
//! let tree = parser.parse(code, None).unwrap();
//! assert!(!tree.root_node().has_error());
//! ```
//!
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
Expand All @@ -31,22 +33,22 @@ pub fn language() -> Language {
/// 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");
pub const NODE_TYPES: &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");
// pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
// pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm");
// pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
// pub const TAGS_QUERY: &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 toml language");
.set_language(&super::language())
.expect("Error loading Toml grammar");
}
}
Loading

0 comments on commit 288e1f3

Please sign in to comment.