Skip to content

Commit 5594caa

Browse files
committed
Initial commit
0 parents  commit 5594caa

File tree

16 files changed

+1114
-0
lines changed

16 files changed

+1114
-0
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: vigoux

.github/workflows/main.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This is a basic workflow to help you get started with Actions
2+
3+
name: Run tree-sitter tests
4+
5+
# Controls when the workflow will run
6+
on:
7+
# Triggers the workflow on push or pull request events but only for the master branch
8+
push:
9+
branches: [ master ]
10+
pull_request:
11+
branches: [ master ]
12+
13+
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
14+
jobs:
15+
# This workflow contains a single job called "build"
16+
test:
17+
name: Tree-sitter tests
18+
# The type of runner that the job will run on
19+
runs-on: ubuntu-latest
20+
21+
# Steps represent a sequence of tasks that will be executed as part of the job
22+
steps:
23+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
24+
- uses: actions/checkout@v2
25+
26+
- uses: actions/[email protected]
27+
- name: Install Dependencies
28+
run: npm install
29+
30+
- name: Run tests
31+
run: npm test

Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "tree-sitter-help"
3+
description = "help grammar for the tree-sitter parsing library"
4+
version = "0.0.1"
5+
keywords = ["incremental", "parsing", "help"]
6+
categories = ["parsing", "text-editors"]
7+
repository = "https://github.com/tree-sitter/tree-sitter-help"
8+
edition = "2018"
9+
license = "MIT"
10+
11+
build = "bindings/rust/build.rs"
12+
include = [
13+
"bindings/rust/*",
14+
"grammar.js",
15+
"queries/*",
16+
"src/*",
17+
]
18+
19+
[lib]
20+
path = "bindings/rust/lib.rs"
21+
22+
[dependencies]
23+
tree-sitter = "~0.20.0"
24+
25+
[build-dependencies]
26+
cc = "1.0"

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
all:
2+
tree-sitter generate
3+
tree-sitter test

bindings/node/binding.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "tree_sitter/parser.h"
2+
#include <node.h>
3+
#include "nan.h"
4+
5+
using namespace v8;
6+
7+
extern "C" TSLanguage * tree_sitter_help();
8+
9+
namespace {
10+
11+
NAN_METHOD(New) {}
12+
13+
void Init(Local<Object> exports, Local<Object> module) {
14+
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
15+
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
16+
tpl->InstanceTemplate()->SetInternalFieldCount(1);
17+
18+
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
19+
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
20+
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_help());
21+
22+
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("help").ToLocalChecked());
23+
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
24+
}
25+
26+
NODE_MODULE(tree_sitter_help_binding, Init)
27+
28+
} // namespace

bindings/node/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
try {
2+
module.exports = require("../../build/Release/tree_sitter_help_binding");
3+
} catch (error1) {
4+
if (error1.code !== 'MODULE_NOT_FOUND') {
5+
throw error1;
6+
}
7+
try {
8+
module.exports = require("../../build/Debug/tree_sitter_help_binding");
9+
} catch (error2) {
10+
if (error2.code !== 'MODULE_NOT_FOUND') {
11+
throw error2;
12+
}
13+
throw error1
14+
}
15+
}
16+
17+
try {
18+
module.exports.nodeTypeInfo = require("../../src/node-types.json");
19+
} catch (_) {}

bindings/rust/build.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
fn main() {
2+
let src_dir = std::path::Path::new("src");
3+
4+
let mut c_config = cc::Build::new();
5+
c_config.include(&src_dir);
6+
c_config
7+
.flag_if_supported("-Wno-unused-parameter")
8+
.flag_if_supported("-Wno-unused-but-set-variable")
9+
.flag_if_supported("-Wno-trigraphs");
10+
let parser_path = src_dir.join("parser.c");
11+
c_config.file(&parser_path);
12+
13+
// If your language uses an external scanner written in C,
14+
// then include this block of code:
15+
16+
/*
17+
let scanner_path = src_dir.join("scanner.c");
18+
c_config.file(&scanner_path);
19+
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
20+
*/
21+
22+
c_config.compile("parser");
23+
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
24+
25+
// If your language uses an external scanner written in C++,
26+
// then include this block of code:
27+
28+
/*
29+
let mut cpp_config = cc::Build::new();
30+
cpp_config.cpp(true);
31+
cpp_config.include(&src_dir);
32+
cpp_config
33+
.flag_if_supported("-Wno-unused-parameter")
34+
.flag_if_supported("-Wno-unused-but-set-variable");
35+
let scanner_path = src_dir.join("scanner.cc");
36+
cpp_config.file(&scanner_path);
37+
cpp_config.compile("scanner");
38+
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
39+
*/
40+
}

bindings/rust/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! This crate provides help language support for the [tree-sitter][] parsing library.
2+
//!
3+
//! Typically, you will use the [language][language func] function to add this language to a
4+
//! tree-sitter [Parser][], and then use the parser to parse some code:
5+
//!
6+
//! ```
7+
//! let code = "";
8+
//! let mut parser = tree_sitter::Parser::new();
9+
//! parser.set_language(tree_sitter_help::language()).expect("Error loading help grammar");
10+
//! let tree = parser.parse(code, None).unwrap();
11+
//! ```
12+
//!
13+
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
14+
//! [language func]: fn.language.html
15+
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
16+
//! [tree-sitter]: https://tree-sitter.github.io/
17+
18+
use tree_sitter::Language;
19+
20+
extern "C" {
21+
fn tree_sitter_help() -> Language;
22+
}
23+
24+
/// Get the tree-sitter [Language][] for this grammar.
25+
///
26+
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
27+
pub fn language() -> Language {
28+
unsafe { tree_sitter_help() }
29+
}
30+
31+
/// The content of the [`node-types.json`][] file for this grammar.
32+
///
33+
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
34+
pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");
35+
36+
// Uncomment these to include any queries that this grammar contains
37+
38+
// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
39+
// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
40+
// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
41+
// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");
42+
43+
#[cfg(test)]
44+
mod tests {
45+
#[test]
46+
fn test_can_load_grammar() {
47+
let mut parser = tree_sitter::Parser::new();
48+
parser
49+
.set_language(super::language())
50+
.expect("Error loading help language");
51+
}
52+
}

corpus/header.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
================================================================================
2+
Simple file with text
3+
================================================================================
4+
Simple text
5+
--------------------------------------------------------------------------------
6+
7+
(help_file
8+
(line
9+
(word)
10+
(word)))

grammar.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = grammar({
2+
name: 'help', // The actual language name is help
3+
4+
extras: ($) => [/[\t ]/],
5+
6+
rules: {
7+
// TODO: add the actual grammar rules
8+
help_file: ($) => seq($.line, repeat(seq('\n', $.line))),
9+
10+
header: ($) => seq($.tag),
11+
12+
line: ($) => repeat1($._atom),
13+
14+
_atom: ($) => choice($.word, $.tag),
15+
16+
word: ($) => /[^*'|\n]\S*[^*'|\n]/,
17+
tag: ($) => wrapped_word($, '*', 'name'),
18+
option: ($) => /'[a-z]+'/,
19+
hotlink: ($) => /|[a-z-]+|/,
20+
},
21+
});
22+
23+
function wrapped_word($, char, fname) {
24+
return seq(char, field(fname, $.word), char);
25+
}

0 commit comments

Comments
 (0)