feat: improve language bindings
Co-authored-by: ObserverOfTime <chronobserver@disroot.org>
This commit is contained in:
parent
d0d349c02b
commit
9e5bf6591f
32 changed files with 1132 additions and 195 deletions
|
|
@ -32,7 +32,9 @@ clap.workspace = true
|
|||
ctrlc.workspace = true
|
||||
difference.workspace = true
|
||||
dirs.workspace = true
|
||||
filetime.workspace = true
|
||||
glob.workspace = true
|
||||
heck.workspace = true
|
||||
html-escape.workspace = true
|
||||
indexmap.workspace = true
|
||||
indoc.workspace = true
|
||||
|
|
|
|||
21
cli/build.rs
21
cli/build.rs
|
|
@ -1,6 +1,10 @@
|
|||
use std::ffi::OsStr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{env, fs};
|
||||
use std::{
|
||||
env,
|
||||
ffi::OsStr,
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
time::SystemTime,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
if let Some(git_sha) = read_git_sha() {
|
||||
|
|
@ -10,6 +14,12 @@ fn main() {
|
|||
if web_playground_files_present() {
|
||||
println!("cargo:rustc-cfg=TREE_SITTER_EMBED_WASM_BINDING");
|
||||
}
|
||||
|
||||
let build_time = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs_f64();
|
||||
println!("cargo:rustc-env=BUILD_TIME={build_time}");
|
||||
}
|
||||
|
||||
fn web_playground_files_present() -> bool {
|
||||
|
|
@ -30,7 +40,8 @@ fn read_git_sha() -> Option<String> {
|
|||
git_path = repo_path.join(".git");
|
||||
if git_path.exists() {
|
||||
break;
|
||||
} else if !repo_path.pop() {
|
||||
}
|
||||
if !repo_path.pop() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
|
@ -93,7 +104,7 @@ fn read_git_sha() -> Option<String> {
|
|||
return fs::read_to_string(&ref_filename).ok();
|
||||
}
|
||||
// If we're on a detached commit, then the `HEAD` file itself contains the sha.
|
||||
else if head_content.len() == 40 {
|
||||
if head_content.len() == 40 {
|
||||
return Some(head_content);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
460
cli/src/generate/grammar_files.rs
Normal file
460
cli/src/generate/grammar_files.rs
Normal file
|
|
@ -0,0 +1,460 @@
|
|||
use super::write_file;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use filetime::FileTime;
|
||||
use heck::{ToKebabCase, ToShoutySnakeCase, ToSnakeCase, ToUpperCamelCase};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Map, Value};
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, SystemTime};
|
||||
use std::{fs, str};
|
||||
|
||||
const BUILD_TIME: &str = env!("BUILD_TIME");
|
||||
|
||||
const CLI_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
const CLI_VERSION_PLACEHOLDER: &str = "CLI_VERSION";
|
||||
|
||||
const PARSER_NAME_PLACEHOLDER: &str = "PARSER_NAME";
|
||||
const CAMEL_PARSER_NAME_PLACEHOLDER: &str = "CAMEL_PARSER_NAME";
|
||||
const UPPER_PARSER_NAME_PLACEHOLDER: &str = "UPPER_PARSER_NAME";
|
||||
const LOWER_PARSER_NAME_PLACEHOLDER: &str = "LOWER_PARSER_NAME";
|
||||
|
||||
const DSL_D_TS_FILE: &str = include_str!("../../npm/dsl.d.ts");
|
||||
const GRAMMAR_JS_TEMPLATE: &str = include_str!("./templates/grammar.js");
|
||||
const PACKAGE_JSON_TEMPLATE: &str = include_str!("./templates/package.json");
|
||||
const GITIGNORE_TEMPLATE: &str = include_str!("./templates/gitignore");
|
||||
const GITATTRIBUTES_TEMPLATE: &str = include_str!("./templates/gitattributes");
|
||||
const EDITORCONFIG_TEMPLATE: &str = include_str!("./templates/gitattributes");
|
||||
|
||||
const RUST_BINDING_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
const RUST_BINDING_VERSION_PLACEHOLDER: &str = "RUST_BINDING_VERSION";
|
||||
|
||||
const LIB_RS_TEMPLATE: &str = include_str!("./templates/lib.rs");
|
||||
const BUILD_RS_TEMPLATE: &str = include_str!("./templates/build.rs");
|
||||
const CARGO_TOML_TEMPLATE: &str = include_str!("./templates/cargo.toml");
|
||||
|
||||
const INDEX_JS_TEMPLATE: &str = include_str!("./templates/index.js");
|
||||
const JS_BINDING_CC_TEMPLATE: &str = include_str!("./templates/js-binding.cc");
|
||||
const BINDING_GYP_TEMPLATE: &str = include_str!("./templates/binding.gyp");
|
||||
|
||||
const MAKEFILE_TEMPLATE: &str = include_str!("./templates/makefile");
|
||||
const PARSER_NAME_H_TEMPLATE: &str = include_str!("./templates/PARSER_NAME.h");
|
||||
const PARSER_NAME_PC_IN_TEMPLATE: &str = include_str!("./templates/PARSER_NAME.pc.in");
|
||||
|
||||
const GO_MOD_TEMPLATE: &str = include_str!("./templates/go.mod");
|
||||
const BINDING_GO_TEMPLATE: &str = include_str!("./templates/binding.go");
|
||||
const BINDING_GO_TEST_TEMPLATE: &str = include_str!("./templates/binding_test.go");
|
||||
|
||||
const SETUP_PY_TEMPLATE: &str = include_str!("./templates/setup.py");
|
||||
const INIT_PY_TEMPLATE: &str = include_str!("./templates/__init__.py");
|
||||
const INIT_PYI_TEMPLATE: &str = include_str!("./templates/__init__.pyi");
|
||||
const PYPROJECT_TOML_TEMPLATE: &str = include_str!("./templates/pyproject.toml");
|
||||
const PY_BINDING_C_TEMPLATE: &str = include_str!("./templates/py-binding.c");
|
||||
|
||||
const PACKAGE_SWIFT_TEMPLATE: &str = include_str!("./templates/Package.swift");
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct LanguageConfiguration {}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct PackageJSON {
|
||||
#[serde(rename = "tree-sitter")]
|
||||
tree_sitter: Option<Vec<LanguageConfiguration>>,
|
||||
}
|
||||
|
||||
pub fn path_in_ignore(repo_path: &Path) -> bool {
|
||||
[
|
||||
"bindings",
|
||||
"build",
|
||||
"examples",
|
||||
"node_modules",
|
||||
"queries",
|
||||
"script",
|
||||
"src",
|
||||
"target",
|
||||
"test",
|
||||
"types",
|
||||
]
|
||||
.iter()
|
||||
.any(|dir| repo_path.ends_with(dir))
|
||||
}
|
||||
|
||||
pub fn generate_grammar_files(
|
||||
repo_path: &Path,
|
||||
language_name: &str,
|
||||
generate_bindings: bool,
|
||||
) -> Result<()> {
|
||||
let dashed_language_name = language_name.to_kebab_case();
|
||||
|
||||
// Create package.json, or update it with new binding path
|
||||
let package_json_path_state = missing_path_else(
|
||||
repo_path.join("package.json"),
|
||||
|path| generate_file(path, PACKAGE_JSON_TEMPLATE, dashed_language_name.as_str()),
|
||||
|path| {
|
||||
let package_json_str =
|
||||
fs::read_to_string(path).with_context(|| "Failed to read package.json")?;
|
||||
let mut package_json = serde_json::from_str::<Map<String, Value>>(&package_json_str)
|
||||
.with_context(|| "Failed to parse package.json")?;
|
||||
let package_json_main = package_json.get("main");
|
||||
let package_json_needs_update = package_json_main.map_or(true, |v| {
|
||||
let main_string = v.as_str();
|
||||
main_string == Some("index.js") || main_string == Some("./index.js")
|
||||
});
|
||||
if package_json_needs_update {
|
||||
eprintln!("Updating package.json with new binding path");
|
||||
package_json.insert(
|
||||
"main".to_string(),
|
||||
Value::String("bindings/node".to_string()),
|
||||
);
|
||||
let mut package_json_str = serde_json::to_string_pretty(&package_json)?;
|
||||
package_json_str.push('\n');
|
||||
write_file(path, package_json_str)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
|
||||
let (_, package_json) = lookup_package_json_for_path(package_json_path_state.as_path())?;
|
||||
|
||||
// Do not create a grammar.js file in a repo with multiple language configs
|
||||
if !package_json.has_multiple_language_configs() {
|
||||
missing_path(repo_path.join("grammar.js"), |path| {
|
||||
generate_file(path, GRAMMAR_JS_TEMPLATE, language_name)
|
||||
})?;
|
||||
}
|
||||
|
||||
// Rewrite dsl.d.ts only if its mtime differs from what was set on its creation
|
||||
missing_path(repo_path.join("types"), create_dir)?.apply(|path| {
|
||||
missing_path(path.join("dsl.d.ts"), |path| {
|
||||
write_file(path, DSL_D_TS_FILE)
|
||||
})?
|
||||
.apply_state(|state| {
|
||||
let build_time =
|
||||
SystemTime::UNIX_EPOCH + Duration::from_secs_f64(BUILD_TIME.parse::<f64>()?);
|
||||
|
||||
match state {
|
||||
PathState::Exists(path) => {
|
||||
let mtime = fs::metadata(path)?.modified()?;
|
||||
if mtime != build_time {
|
||||
write_file(path, DSL_D_TS_FILE)?;
|
||||
filetime::set_file_mtime(path, FileTime::from_system_time(build_time))?;
|
||||
}
|
||||
}
|
||||
PathState::Missing(path) => {
|
||||
filetime::set_file_mtime(path, FileTime::from_system_time(build_time))?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
// Write .gitignore file
|
||||
missing_path(repo_path.join(".gitignore"), |path| {
|
||||
generate_file(path, GITIGNORE_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
// Write .gitattributes file
|
||||
missing_path(repo_path.join(".gitattributes"), |path| {
|
||||
generate_file(path, GITATTRIBUTES_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
// Write .editorconfig file
|
||||
missing_path(repo_path.join(".editorconfig"), |path| {
|
||||
generate_file(path, EDITORCONFIG_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
if generate_bindings {
|
||||
let bindings_dir = repo_path.join("bindings");
|
||||
|
||||
// Generate Rust bindings
|
||||
missing_path(bindings_dir.join("rust"), create_dir)?.apply(|path| {
|
||||
missing_path(path.join("lib.rs"), |path| {
|
||||
generate_file(path, LIB_RS_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
missing_path(path.join("build.rs"), |path| {
|
||||
generate_file(path, BUILD_RS_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
missing_path(repo_path.join("Cargo.toml"), |path| {
|
||||
generate_file(path, CARGO_TOML_TEMPLATE, dashed_language_name.as_str())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
// Generate Node bindings
|
||||
missing_path(bindings_dir.join("node"), create_dir)?.apply(|path| {
|
||||
missing_path(path.join("index.js"), |path| {
|
||||
generate_file(path, INDEX_JS_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
missing_path(path.join("binding.cc"), |path| {
|
||||
generate_file(path, JS_BINDING_CC_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
// Create binding.gyp, or update it with new binding path.
|
||||
missing_path_else(
|
||||
repo_path.join("binding.gyp"),
|
||||
|path| generate_file(path, BINDING_GYP_TEMPLATE, language_name),
|
||||
|path| {
|
||||
let binding_gyp =
|
||||
fs::read_to_string(path).with_context(|| "Failed to read binding.gyp")?;
|
||||
let old_path = "\"src/binding.cc\"";
|
||||
if binding_gyp.contains(old_path) {
|
||||
eprintln!("Updating binding.gyp with new binding path");
|
||||
let binding_gyp =
|
||||
binding_gyp.replace(old_path, "\"bindings/node/binding.cc\"");
|
||||
write_file(path, binding_gyp)?;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
|
||||
// Remove files from old node binding paths.
|
||||
existing_path(repo_path.join("index.js"), remove_file)?;
|
||||
existing_path(repo_path.join("src").join("binding.cc"), remove_file)?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
// Generate C bindings
|
||||
missing_path(bindings_dir.join("c"), create_dir)?.apply(|path| {
|
||||
missing_path(
|
||||
path.join(format!("tree-sitter-{language_name}.h")),
|
||||
|path| generate_file(path, PARSER_NAME_H_TEMPLATE, language_name),
|
||||
)?;
|
||||
|
||||
missing_path(
|
||||
path.join(format!("tree-sitter-{language_name}.pc.in")),
|
||||
|path| generate_file(path, PARSER_NAME_PC_IN_TEMPLATE, language_name),
|
||||
)?;
|
||||
|
||||
missing_path(repo_path.join("Makefile"), |path| {
|
||||
generate_file(path, MAKEFILE_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
// Generate Go bindings
|
||||
missing_path(bindings_dir.join("go"), create_dir)?.apply(|path| {
|
||||
missing_path(path.join("binding.go"), |path| {
|
||||
generate_file(path, BINDING_GO_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
missing_path(path.join("binding_test.go"), |path| {
|
||||
generate_file(path, BINDING_GO_TEST_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
missing_path(path.join("go.mod"), |path| {
|
||||
generate_file(path, GO_MOD_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
// Generate Python bindings
|
||||
missing_path(
|
||||
bindings_dir
|
||||
.join("python")
|
||||
.join(format!("tree_sitter_{}", language_name.to_snake_case())),
|
||||
create_dir,
|
||||
)?
|
||||
.apply(|path| {
|
||||
missing_path(path.join("binding.c"), |path| {
|
||||
generate_file(path, PY_BINDING_C_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
missing_path(path.join("__init__.py"), |path| {
|
||||
generate_file(path, INIT_PY_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
missing_path(path.join("__init__.pyi"), |path| {
|
||||
generate_file(path, INIT_PYI_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
missing_path(path.join("py.typed"), |path| {
|
||||
generate_file(path, "", language_name) // py.typed is empty
|
||||
})?;
|
||||
|
||||
missing_path(repo_path.join("setup.py"), |path| {
|
||||
generate_file(path, SETUP_PY_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
missing_path(repo_path.join("pyproject.toml"), |path| {
|
||||
generate_file(path, PYPROJECT_TOML_TEMPLATE, dashed_language_name.as_str())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
// Generate Swift bindings
|
||||
missing_path(
|
||||
bindings_dir
|
||||
.join("swift")
|
||||
.join(format!("TreeSitter{}", language_name.to_upper_camel_case())),
|
||||
create_dir,
|
||||
)?
|
||||
.apply(|path| {
|
||||
missing_path(path.join(format!("{language_name}.h")), |path| {
|
||||
generate_file(path, PARSER_NAME_H_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
missing_path(repo_path.join("Package.swift"), |path| {
|
||||
generate_file(path, PACKAGE_SWIFT_TEMPLATE, language_name)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn lookup_package_json_for_path(path: &Path) -> Result<(PathBuf, PackageJSON)> {
|
||||
let mut pathbuf = path.to_owned();
|
||||
loop {
|
||||
let package_json = pathbuf
|
||||
.exists()
|
||||
.then(|| -> Result<PackageJSON> {
|
||||
let file =
|
||||
File::open(pathbuf.as_path()).with_context(|| "Failed to open package.json")?;
|
||||
let package_json: PackageJSON = serde_json::from_reader(BufReader::new(file))?;
|
||||
Ok(package_json)
|
||||
})
|
||||
.transpose()?;
|
||||
if let Some(package_json) = package_json {
|
||||
if package_json.tree_sitter.is_some() {
|
||||
return Ok((pathbuf, package_json));
|
||||
}
|
||||
}
|
||||
pathbuf.pop(); // package.json
|
||||
if !pathbuf.pop() {
|
||||
return Err(anyhow!(concat!(
|
||||
"Failed to locate a package.json file that has a \"tree-sitter\" section,",
|
||||
" please ensure you have one, and if you don't then consult the docs",
|
||||
)));
|
||||
}
|
||||
pathbuf.push("package.json");
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_file(path: &Path, template: &str, language_name: &str) -> Result<()> {
|
||||
write_file(
|
||||
path,
|
||||
template
|
||||
.replace(
|
||||
CAMEL_PARSER_NAME_PLACEHOLDER,
|
||||
&language_name.to_upper_camel_case(),
|
||||
)
|
||||
.replace(
|
||||
UPPER_PARSER_NAME_PLACEHOLDER,
|
||||
&language_name.to_shouty_snake_case(),
|
||||
)
|
||||
.replace(
|
||||
LOWER_PARSER_NAME_PLACEHOLDER,
|
||||
&language_name.to_snake_case(),
|
||||
)
|
||||
.replace(PARSER_NAME_PLACEHOLDER, language_name)
|
||||
.replace(CLI_VERSION_PLACEHOLDER, CLI_VERSION)
|
||||
.replace(RUST_BINDING_VERSION_PLACEHOLDER, RUST_BINDING_VERSION),
|
||||
)
|
||||
}
|
||||
|
||||
fn create_dir(path: &Path) -> Result<()> {
|
||||
fs::create_dir_all(path)
|
||||
.with_context(|| format!("Failed to create {:?}", path.to_string_lossy()))
|
||||
}
|
||||
|
||||
fn remove_file(path: &Path) -> Result<()> {
|
||||
fs::remove_file(path).ok();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
enum PathState {
|
||||
Exists(PathBuf),
|
||||
Missing(PathBuf),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl PathState {
|
||||
fn exists(&self, mut action: impl FnMut(&Path) -> Result<()>) -> Result<&Self> {
|
||||
if let Self::Exists(path) = self {
|
||||
action(path.as_path())?;
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn missing(&self, mut action: impl FnMut(&Path) -> Result<()>) -> Result<&Self> {
|
||||
if let Self::Missing(path) = self {
|
||||
action(path.as_path())?;
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn apply(&self, mut action: impl FnMut(&Path) -> Result<()>) -> Result<&Self> {
|
||||
action(self.as_path())?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn apply_state(&self, mut action: impl FnMut(&Self) -> Result<()>) -> Result<&Self> {
|
||||
action(self)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn as_path(&self) -> &Path {
|
||||
match self {
|
||||
Self::Exists(path) | Self::Missing(path) => path.as_path(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn existing_path<F>(path: PathBuf, mut action: F) -> Result<PathState>
|
||||
where
|
||||
F: FnMut(&Path) -> Result<()>,
|
||||
{
|
||||
if path.exists() {
|
||||
action(path.as_path())?;
|
||||
Ok(PathState::Exists(path))
|
||||
} else {
|
||||
Ok(PathState::Missing(path))
|
||||
}
|
||||
}
|
||||
|
||||
fn missing_path<F>(path: PathBuf, mut action: F) -> Result<PathState>
|
||||
where
|
||||
F: FnMut(&Path) -> Result<()>,
|
||||
{
|
||||
if !path.exists() {
|
||||
action(path.as_path())?;
|
||||
Ok(PathState::Missing(path))
|
||||
} else {
|
||||
Ok(PathState::Exists(path))
|
||||
}
|
||||
}
|
||||
|
||||
fn missing_path_else<T, F>(path: PathBuf, mut action: T, mut else_action: F) -> Result<PathState>
|
||||
where
|
||||
T: FnMut(&Path) -> Result<()>,
|
||||
F: FnMut(&Path) -> Result<()>,
|
||||
{
|
||||
if !path.exists() {
|
||||
action(path.as_path())?;
|
||||
Ok(PathState::Missing(path))
|
||||
} else {
|
||||
else_action(path.as_path())?;
|
||||
Ok(PathState::Exists(path))
|
||||
}
|
||||
}
|
||||
|
||||
impl PackageJSON {
|
||||
fn has_multiple_language_configs(&self) -> bool {
|
||||
self.tree_sitter.as_ref().is_some_and(|c| c.len() > 1)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,25 @@
|
|||
mod binding_files;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::{env, fs};
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::{Regex, RegexBuilder};
|
||||
use semver::Version;
|
||||
|
||||
use build_tables::build_tables;
|
||||
use grammar_files::path_in_ignore;
|
||||
use grammars::{InlinedProductionMap, LexicalGrammar, SyntaxGrammar};
|
||||
use parse_grammar::parse_grammar;
|
||||
use prepare_grammar::prepare_grammar;
|
||||
use render::render_c_code;
|
||||
use rules::AliasMap;
|
||||
|
||||
mod build_tables;
|
||||
mod char_tree;
|
||||
mod dedup;
|
||||
mod grammar_files;
|
||||
mod grammars;
|
||||
mod nfa;
|
||||
mod node_types;
|
||||
|
|
@ -11,21 +29,6 @@ mod render;
|
|||
mod rules;
|
||||
mod tables;
|
||||
|
||||
use self::build_tables::build_tables;
|
||||
use self::grammars::{InlinedProductionMap, LexicalGrammar, SyntaxGrammar};
|
||||
use self::parse_grammar::parse_grammar;
|
||||
use self::prepare_grammar::prepare_grammar;
|
||||
use self::render::render_c_code;
|
||||
use self::rules::AliasMap;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::{Regex, RegexBuilder};
|
||||
use semver::Version;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::{env, fs};
|
||||
|
||||
lazy_static! {
|
||||
static ref JSON_COMMENT_REGEX: Regex = RegexBuilder::new("^\\s*//.*")
|
||||
.multi_line(true)
|
||||
|
|
@ -46,8 +49,35 @@ pub fn generate_parser_in_directory(
|
|||
report_symbol_name: Option<&str>,
|
||||
js_runtime: Option<&str>,
|
||||
) -> Result<()> {
|
||||
let src_path = repo_path.join("src");
|
||||
let header_path = src_path.join("tree_sitter");
|
||||
let mut repo_path = repo_path.to_owned();
|
||||
let mut grammar_path = grammar_path;
|
||||
|
||||
// Populate a new empty grammar directory.
|
||||
if let Some(path) = grammar_path {
|
||||
let path = PathBuf::from(path);
|
||||
if !path
|
||||
.try_exists()
|
||||
.with_context(|| "Some error with specified path")?
|
||||
{
|
||||
fs::create_dir_all(&path)?;
|
||||
grammar_path = None;
|
||||
repo_path = path;
|
||||
}
|
||||
}
|
||||
|
||||
if repo_path.is_dir() && !repo_path.join("grammar.js").exists() && !path_in_ignore(&repo_path) {
|
||||
if let Some(dir_name) = repo_path
|
||||
.file_name()
|
||||
.map(|x| x.to_string_lossy().to_ascii_lowercase())
|
||||
{
|
||||
if let Some(language_name) = dir_name
|
||||
.strip_prefix("tree-sitter-")
|
||||
.or_else(|| Some(dir_name.as_ref()))
|
||||
{
|
||||
grammar_files::generate_grammar_files(&repo_path, language_name, false)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Read the grammar.json.
|
||||
let grammar_json = if let Some(path) = grammar_path {
|
||||
|
|
@ -58,6 +88,9 @@ pub fn generate_parser_in_directory(
|
|||
load_grammar_file(&grammar_js_path, js_runtime)?
|
||||
};
|
||||
|
||||
let src_path = repo_path.join("src");
|
||||
let header_path = src_path.join("tree_sitter");
|
||||
|
||||
// Ensure that the output directories exist.
|
||||
fs::create_dir_all(&src_path)?;
|
||||
fs::create_dir_all(&header_path)?;
|
||||
|
|
@ -91,8 +124,8 @@ pub fn generate_parser_in_directory(
|
|||
write_file(&src_path.join("node-types.json"), node_types_json)?;
|
||||
write_file(&header_path.join("parser.h"), tree_sitter::PARSER_HEADER)?;
|
||||
|
||||
if generate_bindings {
|
||||
binding_files::generate_binding_files(repo_path, &language_name)?;
|
||||
if !path_in_ignore(&repo_path) {
|
||||
grammar_files::generate_grammar_files(&repo_path, &language_name, generate_bindings)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
16
cli/src/generate/templates/PARSER_NAME.h
Normal file
16
cli/src/generate/templates/PARSER_NAME.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef TREE_SITTER_UPPER_PARSER_NAME_H_
|
||||
#define TREE_SITTER_UPPER_PARSER_NAME_H_
|
||||
|
||||
typedef struct TSLanguage TSLanguage;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern const TSLanguage *tree_sitter_PARSER_NAME(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_UPPER_PARSER_NAME_H_
|
||||
11
cli/src/generate/templates/PARSER_NAME.pc.in
Normal file
11
cli/src/generate/templates/PARSER_NAME.pc.in
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
prefix=@PREFIX@
|
||||
libdir=@LIBDIR@
|
||||
includedir=@INCLUDEDIR@
|
||||
|
||||
Name: tree-sitter-PARSER_NAME
|
||||
Description: PARSER_NAME grammar for tree-sitter
|
||||
URL: @URL@
|
||||
Version: @VERSION@
|
||||
Requires: @REQUIRES@
|
||||
Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-PARSER_NAME
|
||||
Cflags: -I${includedir}
|
||||
24
cli/src/generate/templates/Package.swift
Normal file
24
cli/src/generate/templates/Package.swift
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// swift-tools-version:5.3
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "TreeSitterCAMEL_PARSER_NAME",
|
||||
platforms: [.macOS(.v10_13), .iOS(.v11)],
|
||||
products: [
|
||||
.library(name: "TreeSitterCAMEL_PARSER_NAME", targets: ["TreeSitterCAMEL_PARSER_NAME"]),
|
||||
],
|
||||
dependencies: [],
|
||||
targets: [
|
||||
.target(name: "TreeSitterCAMEL_PARSER_NAME",
|
||||
path: ".",
|
||||
sources: [
|
||||
"src/parser.c",
|
||||
// NOTE: if your language has an external scanner, add it here.
|
||||
],
|
||||
resources: [
|
||||
.copy("queries")
|
||||
],
|
||||
publicHeadersPath: "bindings/swift",
|
||||
cSettings: [.headerSearchPath("src")])
|
||||
]
|
||||
)
|
||||
3
cli/src/generate/templates/__init__.py
Normal file
3
cli/src/generate/templates/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
"CAMEL_PARSER_NAME grammar for tree-sitter"
|
||||
|
||||
from ._binding import language
|
||||
1
cli/src/generate/templates/__init__.pyi
Normal file
1
cli/src/generate/templates/__init__.pyi
Normal file
|
|
@ -0,0 +1 @@
|
|||
def language() -> int: ...
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
#include "tree_sitter/parser.h"
|
||||
#include <node.h>
|
||||
#include "nan.h"
|
||||
|
||||
using namespace v8;
|
||||
|
||||
extern "C" TSLanguage * tree_sitter_PARSER_NAME();
|
||||
|
||||
namespace {
|
||||
|
||||
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_PARSER_NAME());
|
||||
|
||||
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("PARSER_NAME").ToLocalChecked());
|
||||
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
|
||||
}
|
||||
|
||||
NODE_MODULE_CONTEXT_AWARE(tree_sitter_PARSER_NAME_binding, Init)
|
||||
|
||||
} // namespace
|
||||
13
cli/src/generate/templates/binding.go
Normal file
13
cli/src/generate/templates/binding.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package tree_sitter_PARSER_NAME
|
||||
|
||||
// #cgo CFLAGS: -std=c11 -fPIC
|
||||
// #include "../../src/parser.c"
|
||||
// // NOTE: if your language has an external scanner, add it here.
|
||||
import "C"
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// Get the tree-sitter Language for this grammar.
|
||||
func Language() unsafe.Pointer {
|
||||
return unsafe.Pointer(C.tree_sitter_LOWER_PARSER_NAME())
|
||||
}
|
||||
|
|
@ -4,15 +4,18 @@
|
|||
"target_name": "tree_sitter_PARSER_NAME_binding",
|
||||
"include_dirs": [
|
||||
"<!(node -e \"require('nan')\")",
|
||||
"src"
|
||||
"src",
|
||||
],
|
||||
"sources": [
|
||||
"bindings/node/binding.cc",
|
||||
"src/parser.c",
|
||||
# If your language uses an external scanner, add it here.
|
||||
# NOTE: if your language has an external scanner, add it here.
|
||||
],
|
||||
"cflags_c": [
|
||||
"-std=c99",
|
||||
"-std=c11",
|
||||
],
|
||||
"cflags_cc": [
|
||||
"-Wno-cast-function-type",
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
|
|||
15
cli/src/generate/templates/binding_test.go
Normal file
15
cli/src/generate/templates/binding_test.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package tree_sitter_PARSER_NAME_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
tree_sitter "github.com/smacker/go-tree-sitter"
|
||||
"github.com/tree-sitter/tree-sitter-PARSER_NAME"
|
||||
)
|
||||
|
||||
func TestCanLoadGrammar(t *testing.T) {
|
||||
language := tree_sitter.NewLanguage(tree_sitter_PARSER_NAME.Language())
|
||||
if language == nil {
|
||||
t.Errorf("Error loading CAMEL_PARSER_NAME grammar")
|
||||
}
|
||||
}
|
||||
|
|
@ -2,16 +2,11 @@ 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("-Wno-unused-parameter")
|
||||
.flag_if_supported("-Wno-unused-but-set-variable")
|
||||
.flag_if_supported("-Wno-trigraphs");
|
||||
c_config.include(src_dir);
|
||||
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:
|
||||
// NOTE: if your language uses an external scanner, uncomment this block:
|
||||
|
||||
/*
|
||||
let scanner_path = src_dir.join("scanner.c");
|
||||
|
|
@ -19,22 +14,6 @@ fn main() {
|
|||
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
|
||||
*/
|
||||
|
||||
c_config.compile("parser");
|
||||
c_config.compile("tree-sitter-PARSER_NAME");
|
||||
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());
|
||||
*/
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,23 @@
|
|||
[package]
|
||||
name = "tree-sitter-PARSER_NAME"
|
||||
description = "PARSER_NAME grammar for the tree-sitter parsing library"
|
||||
description = "PARSER_NAME grammar for tree-sitter"
|
||||
version = "0.0.1"
|
||||
license = "MIT"
|
||||
readme = "bindings/rust/README.md"
|
||||
keywords = ["incremental", "parsing", "PARSER_NAME"]
|
||||
categories = ["parsing", "text-editors"]
|
||||
repository = "https://github.com/tree-sitter/tree-sitter-PARSER_NAME"
|
||||
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"
|
||||
|
||||
[dependencies]
|
||||
tree-sitter = "~RUST_BINDING_VERSION"
|
||||
tree-sitter = ">=RUST_BINDING_VERSION"
|
||||
|
||||
[build-dependencies]
|
||||
cc = "1.0"
|
||||
cc = "1.0.87"
|
||||
|
|
|
|||
41
cli/src/generate/templates/editorconfig
Normal file
41
cli/src/generate/templates/editorconfig
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.{json,toml,yml}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.js]
|
||||
quote_type = double
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.rs]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[*.{c,cc,h}]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[*.{py,pyi}]
|
||||
quote_type = double
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[*.swift]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[*.go]
|
||||
indent_style = tab
|
||||
indent_size = 8
|
||||
|
||||
[Makefile]
|
||||
indent_style = tab
|
||||
indent_size = 8
|
||||
10
cli/src/generate/templates/gitattributes
Normal file
10
cli/src/generate/templates/gitattributes
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
* text eol=lf
|
||||
|
||||
src/*.json linguist-generated
|
||||
src/parser.c linguist-generated
|
||||
src/tree_sitter/* linguist-generated
|
||||
|
||||
bindings/** linguist-generated
|
||||
binding.gyp linguist-generated
|
||||
setup.py linguist-generated
|
||||
Makefile linguist-generated
|
||||
36
cli/src/generate/templates/gitignore
Normal file
36
cli/src/generate/templates/gitignore
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
# Rust artifacts
|
||||
/Cargo.lock
|
||||
/target/
|
||||
|
||||
# Node artifacts
|
||||
/build/
|
||||
/node_modules/
|
||||
|
||||
# Swift artifacts
|
||||
/.build/
|
||||
|
||||
# Python artifacts
|
||||
/dist/
|
||||
*.egg-info
|
||||
*.whl
|
||||
|
||||
# Zig artifacts
|
||||
/zig-cache/
|
||||
/zig-out/
|
||||
|
||||
# C artifacts
|
||||
*.a
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
*.dll
|
||||
*.pc
|
||||
|
||||
# Example dirs
|
||||
/examples/*/
|
||||
|
||||
# Grammar volatiles
|
||||
dsl.d.ts
|
||||
*.wasm
|
||||
*.obj
|
||||
*.o
|
||||
5
cli/src/generate/templates/go.mod
Normal file
5
cli/src/generate/templates/go.mod
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
module github.com/tree-sitter/tree-sitter-PARSER_NAME
|
||||
|
||||
go 1.22
|
||||
|
||||
require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8
|
||||
11
cli/src/generate/templates/grammar.js
Normal file
11
cli/src/generate/templates/grammar.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/// <reference types="./types/dsl" />
|
||||
// @ts-check
|
||||
|
||||
module.exports = grammar({
|
||||
name: "LOWER_PARSER_NAME",
|
||||
|
||||
rules: {
|
||||
// TODO: add the actual grammar rules
|
||||
source_file: $ => "hello"
|
||||
}
|
||||
});
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
try {
|
||||
module.exports = require("../../build/Release/tree_sitter_PARSER_NAME_binding");
|
||||
} catch (error1) {
|
||||
if (error1.code !== 'MODULE_NOT_FOUND') {
|
||||
if (error1.code !== "MODULE_NOT_FOUND") {
|
||||
throw error1;
|
||||
}
|
||||
try {
|
||||
module.exports = require("../../build/Debug/tree_sitter_PARSER_NAME_binding");
|
||||
} catch (error2) {
|
||||
if (error2.code !== 'MODULE_NOT_FOUND') {
|
||||
if (error2.code !== "MODULE_NOT_FOUND") {
|
||||
throw error2;
|
||||
}
|
||||
throw error1
|
||||
|
|
|
|||
29
cli/src/generate/templates/js-binding.cc
Normal file
29
cli/src/generate/templates/js-binding.cc
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include "nan.h"
|
||||
#include <node.h>
|
||||
|
||||
using namespace v8;
|
||||
|
||||
typedef struct TSLanguage TSLanguage;
|
||||
|
||||
extern "C" const TSLanguage *tree_sitter_PARSER_NAME(void);
|
||||
|
||||
namespace {
|
||||
|
||||
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, (void *)tree_sitter_PARSER_NAME());
|
||||
|
||||
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("PARSER_NAME").ToLocalChecked());
|
||||
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
|
||||
}
|
||||
|
||||
NODE_MODULE_CONTEXT_AWARE(tree_sitter_PARSER_NAME_binding, Init)
|
||||
|
||||
} // namespace
|
||||
|
|
@ -1,13 +1,15 @@
|
|||
//! This crate provides PARSER_NAME language support for the [tree-sitter][] parsing library.
|
||||
//! This crate provides CAMEL_PARSER_NAME 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_PARSER_NAME::language()).expect("Error loading PARSER_NAME grammar");
|
||||
//! parser.set_language(&tree_sitter_PARSER_NAME::language()).expect("Error loading CAMEL_PARSER_NAME 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
|
||||
|
|
@ -31,14 +33,14 @@ 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 {
|
||||
|
|
@ -46,7 +48,7 @@ mod tests {
|
|||
fn test_can_load_grammar() {
|
||||
let mut parser = tree_sitter::Parser::new();
|
||||
parser
|
||||
.set_language(super::language())
|
||||
.expect("Error loading PARSER_NAME language");
|
||||
.set_language(&super::language())
|
||||
.expect("Error loading CAMEL_PARSER_NAME language");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
94
cli/src/generate/templates/makefile
Normal file
94
cli/src/generate/templates/makefile
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
VERSION := 0.0.1
|
||||
|
||||
LANGUAGE_NAME := tree-sitter-PARSER_NAME
|
||||
|
||||
# repository
|
||||
SRC_DIR := src
|
||||
|
||||
PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin 2>/dev/null)
|
||||
|
||||
ifeq ($(PARSER_URL),)
|
||||
PARSER_URL := $(subst .git,,$(PARSER_REPO_URL))
|
||||
ifeq ($(shell echo $(PARSER_URL) | grep '^[a-z][-+.0-9a-z]*://'),)
|
||||
PARSER_URL := $(subst :,/,$(PARSER_URL))
|
||||
PARSER_URL := $(subst git@,https://,$(PARSER_URL))
|
||||
endif
|
||||
endif
|
||||
|
||||
# ABI versioning
|
||||
SONAME_MAJOR := $(word 1,$(subst ., ,$(VERSION)))
|
||||
SONAME_MINOR := $(word 2,$(subst ., ,$(VERSION)))
|
||||
|
||||
# install directory layout
|
||||
PREFIX ?= /usr/local
|
||||
INCLUDEDIR ?= $(PREFIX)/include
|
||||
LIBDIR ?= $(PREFIX)/lib
|
||||
PCLIBDIR ?= $(LIBDIR)/pkgconfig
|
||||
|
||||
# object files
|
||||
OBJS := $(patsubst %.c,%.o,$(wildcard $(SRC_DIR)/*.c))
|
||||
|
||||
# flags
|
||||
ARFLAGS := rcs
|
||||
override CFLAGS += -I$(SRC_DIR) -std=c11
|
||||
|
||||
# OS-specific bits
|
||||
ifeq ($(shell uname),Darwin)
|
||||
SOEXT = dylib
|
||||
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
|
||||
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
|
||||
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl,
|
||||
ifneq ($(ADDITIONAL_LIBS),)
|
||||
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS),
|
||||
endif
|
||||
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks
|
||||
else ifneq ($(filter $(shell uname),Linux FreeBSD NetBSD DragonFly),)
|
||||
SOEXT = so
|
||||
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
|
||||
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
|
||||
LINKSHARED := $(LINKSHARED)-shared -Wl,
|
||||
ifneq ($(ADDITIONAL_LIBS),)
|
||||
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS)
|
||||
endif
|
||||
LINKSHARED := $(LINKSHARED)-soname,lib$(LANGUAGE_NAME).so.$(SONAME_MAJOR)
|
||||
else ifeq ($(OS),Windows_NT)
|
||||
$(error "Windows is not supported")
|
||||
endif
|
||||
ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),)
|
||||
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
|
||||
endif
|
||||
|
||||
all: lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT) $(LANGUAGE_NAME).pc
|
||||
|
||||
$(SRC_DIR)/%.o: $(SRC_DIR)/%.c
|
||||
$(CC) -c $^ -o $@
|
||||
|
||||
lib$(LANGUAGE_NAME).a: $(OBJS)
|
||||
$(AR) $(ARFLAGS) $@ $^
|
||||
|
||||
lib$(LANGUAGE_NAME).$(SOEXT): $(OBJS)
|
||||
$(CC) -fPIC $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
|
||||
|
||||
$(LANGUAGE_NAME).pc:
|
||||
sed > $@ bindings/c/$(LANGUAGE_NAME).pc.in \
|
||||
-e 's|@URL@|$(PARSER_URL)|' \
|
||||
-e 's|@VERSION@|$(VERSION)|' \
|
||||
-e 's|@LIBDIR@|$(LIBDIR)|;' \
|
||||
-e 's|@INCLUDEDIR@|$(INCLUDEDIR)|;' \
|
||||
-e 's|=$(PREFIX)|=$${prefix}|' \
|
||||
-e 's|@PREFIX@|$(PREFIX)|' \
|
||||
-e 's|@REQUIRES@|$(REQUIRES)|' \
|
||||
-e 's|@ADDITIONAL_LIBS@|$(ADDITIONAL_LIBS)|'
|
||||
|
||||
install: all
|
||||
install -Dm644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h
|
||||
install -Dm644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
|
||||
install -Dm755 lib$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a
|
||||
install -Dm755 lib$(LANGUAGE_NAME).$(SOEXT) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER)
|
||||
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR)
|
||||
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT)
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJS) $(LANGUAGE_NAME).pc lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT)
|
||||
|
||||
.PHONY: all install clean
|
||||
|
|
@ -1,19 +1,31 @@
|
|||
{
|
||||
"name": "tree-sitter-PARSER_NAME",
|
||||
"version": "0.0.1",
|
||||
"description": "PARSER_NAME grammar for tree-sitter",
|
||||
"description": "CAMEL_PARSER_NAME grammar for tree-sitter",
|
||||
"repository": "github:tree-sitter/tree-sitter-PARSER_NAME",
|
||||
"license": "MIT",
|
||||
"main": "bindings/node",
|
||||
"keywords": [
|
||||
"parsing",
|
||||
"incremental"
|
||||
"incremental",
|
||||
"LOWER_PARSER_NAME"
|
||||
],
|
||||
"dependencies": {
|
||||
"nan": "^2.12.1"
|
||||
"nan": "^2.18.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tree-sitter-cli": "^CLI_VERSION"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "tree-sitter test"
|
||||
}
|
||||
"build": "tree-sitter generate --no-bindings",
|
||||
"build-wasm": "tree-sitter build-wasm",
|
||||
"test": "tree-sitter test",
|
||||
"parse": "tree-sitter parse"
|
||||
},
|
||||
"tree-sitter": [
|
||||
{
|
||||
"scope": "source.LOWER_PARSER_NAME",
|
||||
"injection-regex": "LOWER_PARSER_NAME"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
27
cli/src/generate/templates/py-binding.c
Normal file
27
cli/src/generate/templates/py-binding.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <Python.h>
|
||||
|
||||
typedef struct TSLanguage TSLanguage;
|
||||
|
||||
extern const TSLanguage *tree_sitter_LOWER_PARSER_NAME(void);
|
||||
|
||||
static PyObject* _binding_language(PyObject *self, PyObject *args) {
|
||||
return PyLong_FromVoidPtr((void *)tree_sitter_LOWER_PARSER_NAME());
|
||||
}
|
||||
|
||||
static PyMethodDef methods[] = {
|
||||
{"language", _binding_language, METH_NOARGS,
|
||||
"Get the tree-sitter language for this grammar."},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
static struct PyModuleDef module = {
|
||||
.m_base = PyModuleDef_HEAD_INIT,
|
||||
.m_name = "_binding",
|
||||
.m_doc = NULL,
|
||||
.m_size = -1,
|
||||
.m_methods = methods
|
||||
};
|
||||
|
||||
PyMODINIT_FUNC PyInit__binding(void) {
|
||||
return PyModule_Create(&module);
|
||||
}
|
||||
26
cli/src/generate/templates/pyproject.toml
Normal file
26
cli/src/generate/templates/pyproject.toml
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
[build-system]
|
||||
requires = ["setuptools>=42", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "tree-sitter-PARSER_NAME"
|
||||
description = "CAMEL_PARSER_NAME grammar for tree-sitter"
|
||||
version = "0.0.1"
|
||||
keywords = ["parsing", "incremental", "PARSER_NAME"]
|
||||
classifiers = [
|
||||
"Development Status :: 4 - Beta",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Topic :: Software Development :: Compilers",
|
||||
"Topic :: Text Processing :: Linguistic",
|
||||
]
|
||||
requires-python = ">=3.8"
|
||||
license.file = "LICENSE"
|
||||
readme = "README.md"
|
||||
|
||||
[project.optional-dependencies]
|
||||
core = ["tree-sitter~=0.21"]
|
||||
|
||||
[tool.cibuildwheel]
|
||||
build = "cp38-*"
|
||||
build-frontend = "build"
|
||||
49
cli/src/generate/templates/setup.py
Normal file
49
cli/src/generate/templates/setup.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
from os.path import join
|
||||
from setuptools import Extension, find_packages, setup
|
||||
from setuptools.command.build import build
|
||||
from wheel.bdist_wheel import bdist_wheel
|
||||
|
||||
|
||||
class Build(build):
|
||||
def run(self):
|
||||
dest = join(self.build_lib, "tree_sitter_PARSER_NAME", "queries")
|
||||
try:
|
||||
self.copy_tree("queries", dest)
|
||||
except:
|
||||
pass
|
||||
super().run()
|
||||
|
||||
|
||||
class BdistWheel(bdist_wheel):
|
||||
def get_tag(self):
|
||||
python, abi, platform = super().get_tag()
|
||||
if python.startswith("cp"):
|
||||
python, abi = "cp38", "abi3"
|
||||
return python, abi, platform
|
||||
|
||||
|
||||
setup(
|
||||
packages=find_packages("bindings/python"),
|
||||
package_dir={"": "bindings/python"},
|
||||
package_data={
|
||||
"tree_sitter_LOWER_PARSER_NAME": ["*.pyi", "py.typed"],
|
||||
"tree_sitter_LOWER_PARSER_NAME.queries": ["*.scm"],
|
||||
},
|
||||
ext_package="tree_sitter_LOWER_PARSER_NAME",
|
||||
ext_modules=[
|
||||
Extension(
|
||||
name="_binding",
|
||||
sources=[
|
||||
"bindings/python/tree_sitter_LOWER_PARSER_NAME/binding.c",
|
||||
"src/parser.c",
|
||||
# NOTE: if your language uses an external scanner, add it here.
|
||||
],
|
||||
extra_compile_args=["-std=c11"],
|
||||
define_macros=[("Py_LIMITED_API", "0x03080000"), ("PY_SSIZE_T_CLEAN", None)],
|
||||
include_dirs=["src"],
|
||||
py_limited_api=True,
|
||||
)
|
||||
],
|
||||
cmdclass={"build": Build, "bdist_wheel": BdistWheel},
|
||||
zip_safe=False,
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue