Skip to content

Commit

Permalink
feat: support chatl files
Browse files Browse the repository at this point in the history
TODO: also support chatette
  • Loading branch information
ObserverOfTime committed Feb 18, 2024
1 parent 308b591 commit a18f1c8
Show file tree
Hide file tree
Showing 31 changed files with 4,668 additions and 171 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
max_line_length = 120
insert_final_newline = true
trim_trailing_whitespace = true

[grammar.js]
quote_type = single

[*.rs]
indent_size = 4
3 changes: 0 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
* text eol=lf

src/** linguist-generated
bindings/** linguist-generated
binding.gyp linguist-generated
Cargo.toml linguist-generated
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ on:
- src/**
- bindings/**
- binding.gyp
- extensions/**
pull_request:
branches: ["*"]
paths:
- grammar.js
- src/**
- bindings/**
- binding.gyp
- extensions/**

jobs:
test:
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tree-sitter-chatito"
description = "chatito grammar for the tree-sitter parsing library"
version = "0.2.1"
version = "0.3.0"
keywords = ["incremental", "parsing", "chatito"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter-grammars/tree-sitter-chatito"
Expand All @@ -14,6 +14,8 @@ include = [
"grammar.js",
"queries/*",
"src/*",
"extensions/*/grammar.js",
"extensions/*/src/*",
]

[lib]
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

[![CI][ci]](https://github.com/tree-sitter-grammars/tree-sitter-chatito/actions/workflows/ci.yml)
[![discord][discord]](https://discord.gg/w7nTvsVJhm)
[![matrix][matrix]](https://matrix.to/#/#nvim-treesitter:matrix.org)
[![matrix][matrix]](https://matrix.to/#/#tree-sitter-chat:matrix.org)

A tree-sitter parser for Chatito files.
A tree-sitter parser for chatito & chatl files.

## References

* [Chatito Spec](https://github.com/rodrigopivi/Chatito/blob/master/spec.md)
* [DSL specifications (chatl)](https://github.com/atlassistant/chatl#dsl-specifications)

## Editors

Expand All @@ -18,5 +19,5 @@ A tree-sitter parser for Chatito files.
- [ ] Zed

[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter-grammars/tree-sitter-chatito/ci.yml?logo=github&label=CI
[discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=tree-sitter
[matrix]: https://img.shields.io/matrix/nvim-treesitter%3Amatrix.org?logo=matrix&label=nvim-treesitter
[discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord
[matrix]: https://img.shields.io/matrix/tree-sitter-chat%3Amatrix.org?logo=matrix&label=matrix
2 changes: 2 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"sources": [
"bindings/node/binding.cc",
"src/parser.c",
"extensions/chatl/src/parser.c",
# "extensions/chatette/src/parser.c",
],
"cflags_c": [
"-std=c99",
Expand Down
41 changes: 30 additions & 11 deletions bindings/node/binding.cc
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
#include "nan.h"
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"

using namespace v8;

extern "C" TSLanguage * tree_sitter_chatito();
extern "C" TSLanguage * tree_sitter_chatl();
// extern "C" TSLanguage * tree_sitter_chatette();

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_chatito());

Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("chatito").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
Local<FunctionTemplate> chatito_tpl = Nan::New<FunctionTemplate>(New);
chatito_tpl->SetClassName(Nan::New("Language").ToLocalChecked());
chatito_tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> chatito_constructor = Nan::GetFunction(chatito_tpl).ToLocalChecked();
Local<Object> chatito_instance = chatito_constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(chatito_instance, 0, tree_sitter_chatito());
Nan::Set(chatito_instance, Nan::New("name").ToLocalChecked(), Nan::New("chatito").ToLocalChecked());

Local<FunctionTemplate> chatl_tpl = Nan::New<FunctionTemplate>(New);
chatl_tpl->SetClassName(Nan::New("Language").ToLocalChecked());
chatl_tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> chatl_constructor = Nan::GetFunction(chatl_tpl).ToLocalChecked();
Local<Object> chatl_instance = chatl_constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(chatl_instance, 0, tree_sitter_chatl());
Nan::Set(chatl_instance, Nan::New("name").ToLocalChecked(), Nan::New("chatl").ToLocalChecked());

// Local<FunctionTemplate> chatette_tpl = Nan::New<FunctionTemplate>(New);
// chatette_tpl->SetClassName(Nan::New("Language").ToLocalChecked());
// chatette_tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Local<Function> chatette_constructor = Nan::GetFunction(chatette_tpl).ToLocalChecked();
// Local<Object> chatette_instance = chatette_constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
// Nan::SetInternalFieldPointer(chatette_instance, 0, tree_sitter_chatette());
// Nan::Set(chatette_instance, Nan::New("name").ToLocalChecked(), Nan::New("chatette").ToLocalChecked());

Nan::Set(exports, Nan::New("chatito").ToLocalChecked(), chatito_instance);
Nan::Set(exports, Nan::New("chatl").ToLocalChecked(), chatl_instance);
// Nan::Set(exports, Nan::New("chatette").ToLocalChecked(), chatette_instance);
}

NODE_MODULE(tree_sitter_chatito_binding, Init)
Expand Down
1 change: 1 addition & 0 deletions bindings/node/chatito.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('.').chatito;
1 change: 1 addition & 0 deletions bindings/node/chatl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('.').chatl;
10 changes: 6 additions & 4 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
try {
module.exports = require("../../build/Release/tree_sitter_chatito_binding");
module.exports = require('../../build/Release/tree_sitter_chatito_binding');
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_chatito_binding");
module.exports = require('../../build/Debug/tree_sitter_chatito_binding');
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
Expand All @@ -15,5 +15,7 @@ try {
}

try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
} catch (_) {}
module.exports.chatito.nodeTypeInfo = require('../../chatito/src/node-types.json');
module.exports.chatl.nodeTypeInfo = require('../../chatl/src/node-types.json');
// module.exports.chatette.nodeTypeInfo = require('../../chatette/src/node-types.json');
} catch (_) { }
25 changes: 17 additions & 8 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
fn main() {
let src_dir = std::path::Path::new("src");
let chatito_dir = std::path::Path::new("src");
let ext_dir = std::path::Path::new("extensions");
let chatl_dir = ext_dir.join("chatl").join("src");
// let chatette_dir = ext_dir.join("chatette").join("src");

let mut c_config = cc::Build::new();
c_config.include(src_dir);
c_config
let mut config = cc::Build::new();
config.include(chatito_dir);
config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable");
let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);

c_config.compile("parser");
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
for path in &[
chatito_dir.join("parser.c"),
chatl_dir.join("parser.c"),
// chatette_dir.join("parser.c")
] {
config.file(path);
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
}

config.compile("parser");
}
58 changes: 50 additions & 8 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,59 @@
//! synonym 2
//! "#;
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_chatito::language()).expect("Error loading chatito grammar");
//! parser.set_language(tree_sitter_chatito::language_chatito()).expect("Error loading chatito 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
//! [language func]: fn.language.html
//! [language func]: fn.language_chatito.html
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/

use tree_sitter::Language;

extern "C" {
fn tree_sitter_chatito() -> Language;
fn tree_sitter_chatl() -> Language;
// fn tree_sitter_chatette() -> Language;
}

/// Get the tree-sitter [Language][] for this grammar.
/// Get the tree-sitter [Language][] for the chatito grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language() -> Language {
pub fn language_chatito() -> Language {
unsafe { tree_sitter_chatito() }
}

/// The content of the [`node-types.json`][] file for this grammar.
/// Get the tree-sitter [Language][] for the chatl grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language_chatl() -> Language {
unsafe { tree_sitter_chatl() }
}

/// Get the tree-sitter [Language][] for the chatette grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
// pub fn language_chatette() -> Language {
// unsafe { tree_sitter_chatette() }
// }

/// The content of the [`node-types.json`][] file for the chatito grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const CHATITO_NODE_TYPES: &str = include_str!("../../src/node-types.json");

/// The content of the [`node-types.json`][] file for the chatl grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
pub const CHATL_NODE_TYPES: &str = include_str!("../../extensions/chatl/src/node-types.json");

/// The content of the [`node-types.json`][] file for the chatette grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
// pub const CHATETTE_NODE_TYPES: &str = include_str!("../../extensions/chatette/src/node-types.json");

/// The syntax highlighting queries.
pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
Expand All @@ -53,10 +79,26 @@ pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
#[cfg(test)]
mod tests {
#[test]
fn test_can_load_grammar() {
fn test_can_load_chatito_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(super::language())
.set_language(super::language_chatito())
.expect("Error loading chatito language");
}

#[test]
fn test_can_load_chatl_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(super::language_chatl())
.expect("Error loading chatl language");
}

// #[test]
// fn test_can_load_chatette_grammar() {
// let mut parser = tree_sitter::Parser::new();
// parser
// .set_language(super::language_chatette())
// .expect("Error loading chatette language");
// }
}
15 changes: 15 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference types="node"/>

const {execSync} = require('node:child_process');
const {join} = require('node:path');

console.log('building chatito...');
execSync('tree-sitter generate --no-bindings', {stdio: 'inherit'});

console.log('building chatl...');
process.chdir(join(__dirname, 'extensions', 'chatl'));
execSync('tree-sitter generate --no-bindings', {stdio: 'inherit'});

// console.log('building chatette...');
// process.chdir(join(__dirname, 'extensions', 'chatette'));
// execSync('tree-sitter generate --no-bindings', {stdio: 'inherit'});
34 changes: 34 additions & 0 deletions extensions/chatl/grammar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @file Tree-sitter grammar definition
* @author ObserverOfTime
* @license MIT
*/

/// <reference types="tree-sitter-cli/dsl"/>

const CHATITO= require('../../grammar');

module.exports = grammar(CHATITO, {
name: 'chatl',

rules: {
eq: _ => '=',

string: $ => choice(
field('content', repeat1(/\S/)),
seq(
field('quote', '`'),
field(
'content',
repeat(choice(
/[^`\\\r\n]/,
$.escape
))
),
field('quote', '`')
)
),

_indent: _ => /[ ]{2}|[ ]{4}|\t/,
}
});
3 changes: 3 additions & 0 deletions extensions/chatl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "../../bindings/node/chatl"
}

0 comments on commit a18f1c8

Please sign in to comment.