Skip to content

Commit

Permalink
feat: update tree-sitter (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nsidorenco authored Sep 5, 2024
1 parent 7dcac54 commit e0ab53d
Show file tree
Hide file tree
Showing 36 changed files with 589 additions and 687 deletions.
99 changes: 6 additions & 93 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ jobs:
- name: test language
uses: tree-sitter/parser-test-action@v2
with:
test-parser-cmd: cd ${{matrix.language}} && tree-sitter test
test-rust: ${{ runner.os == 'Linux' }}
test-node: true
test-python: true
test-go: true
test-swift: ${{ runner.os == 'macOS' }}

parse-examples:
name: Parse examples
runs-on: ${{matrix.os}}
Expand All @@ -51,98 +56,6 @@ jobs:
name: failures-${{runner.os}}
path: ${{steps.parse-files.outputs.failures}}

test-go:
name: Test Go binding
runs-on: ${{matrix.os}}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.22"
cache-dependency-path: bindings/go/go.mod
- name: Get dependencies
working-directory: bindings/go
run: go get -t .
- name: Test
working-directory: bindings/go
run: go test -v

test-node:
name: Test Node binding
runs-on: ${{matrix.os}}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Node
uses: actions/setup-node@v4
- name: Install dependencies
run: npm install
- name: Test
run: npm run test-binding

test-python:
name: Test Python binding
runs-on: ${{matrix.os}}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
cache: pip
python-version: 3.12
cache-dependency-path: pyproject.toml
- name: Build
run: pip install -e .[core]
- name: Test
run: python -munittest discover -v -s bindings/python/tests

test-rust:
name: Test Rust binding
runs-on: ${{matrix.os}}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Build
run: cargo build -v
- name: Test
run: cargo test -v

# test-swift:
# name: Test Swift binding
# runs-on: ${{matrix.os}}
# strategy:
# fail-fast: false
# matrix:
# os: [ubuntu-latest, macos-latest]
# steps:
# - name: Checkout
# uses: actions/checkout@v4
# - name: Build
# run: swift build
# - name: Test
# run: swift test

fuzz:
name: Fuzz fsharp
runs-on: ubuntu-latest
Expand Down
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ include = [
path = "bindings/rust/lib.rs"

[dependencies]
tree-sitter = ">=0.21.0"
tree-sitter-language = "0.1.0"

[build-dependencies]
cc = "1.0.89"
cc = "1.0.104"

[dev-dependencies]
tree-sitter = "0.23"
27 changes: 24 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,30 @@ let package = Package(
targets: [
.target(
name: "TreeSitterFSharp",
dependencies: [],
path: ".",
exclude: [
"Cargo.toml",
"Makefile",
"binding.gyp",
"bindings/c",
"bindings/go",
"bindings/node",
"bindings/python",
"bindings/rust",
"prebuilds",
"grammar.js",
"package.json",
"package-lock.json",
"pyproject.toml",
"setup.py",
"test",
"examples",
".editorconfig",
".github",
".gitignore",
".gitattributes",
".gitmodules",
],
sources: [
"fsharp/src/parser.c",
"fsharp/src/scanner.c",
Expand All @@ -34,7 +56,6 @@ let package = Package(
],
path: "bindings/swift/TreeSitterFSharpTests"
)
],
cLanguageStandard: .c11
]
)

2 changes: 1 addition & 1 deletion bindings/go/binding_fsharp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import "C"
import "unsafe"

// Get the tree-sitter Language for FSharp.
func FSharp() unsafe.Pointer {
func LanguageFSharp() unsafe.Pointer {
return unsafe.Pointer(C.tree_sitter_fsharp())
}
2 changes: 1 addition & 1 deletion bindings/go/binding_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import "C"
import "unsafe"

// Get the tree-sitter Language for FSharp signature.
func FSharpSignature() unsafe.Pointer {
func LanguageFSharpSignature() unsafe.Pointer {
return unsafe.Pointer(C.tree_sitter_fsharp_signature())
}
23 changes: 14 additions & 9 deletions bindings/go/binding_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
package tree_sitter_fsharp_test

import (
"context"
"testing"

tree_sitter "github.com/smacker/go-tree-sitter"
tree_sitter_fsharp "github.com/ionide/tree-sitter-fsharp"
tree_sitter "github.com/tree-sitter/go-tree-sitter"
tree_sitter_fsharp "github.com/tree-sitter/tree-sitter-fsharp/bindings/go"
)

func TestFSharpGrammar(t *testing.T) {
language := tree_sitter.NewLanguage(tree_sitter_fsharp.FSharp())
language := tree_sitter.NewLanguage(tree_sitter_fsharp.LanguageFSharp())
if language == nil {
t.Errorf("Error loading FSharp grammar")
}

sourceCode := []byte("module M = ()")
parser := tree_sitter.NewParser()
defer parser.Close()
parser.SetLanguage(language)

node, err := tree_sitter.ParseCtx(context.Background(), sourceCode, language)
if err != nil || node.HasError() {
tree := parser.Parse(sourceCode, nil)
if tree == nil || tree.RootNode().HasError() {
t.Errorf("Error parsing FSharp")
}
}

func TestFSharpSignatureGrammar(t *testing.T) {
language := tree_sitter.NewLanguage(tree_sitter_fsharp.FSharpSignature())
language := tree_sitter.NewLanguage(tree_sitter_fsharp.LanguageFSharpSignature())
if language == nil {
t.Errorf("Error loading FSharpSignature grammar")
}

sourceCode := []byte("val x : int -> int")
parser := tree_sitter.NewParser()
defer parser.Close()
parser.SetLanguage(language)

node, err := tree_sitter.ParseCtx(context.Background(), sourceCode, language)
if err != nil || node.HasError() {
tree := parser.Parse(sourceCode, nil)
if tree == nil || tree.RootNode().HasError() {
t.Errorf("Error parsing FSharpSignature")
}
}
5 changes: 0 additions & 5 deletions bindings/go/go.mod

This file was deleted.

2 changes: 1 addition & 1 deletion bindings/python/tree_sitter_fsharp/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static PyMethodDef methods[] = {
{"fsharp", _binding_language_fsharp, METH_NOARGS,
"Get the tree-sitter language for FSharp."},
{"signature", _binding_language_fsharp_signature, METH_NOARGS,
"Get the tree-sitter language for FSharp signature."},
"Get the tree-sitter language for FSharp interfaces."},
{NULL, NULL, 0, NULL}};

static struct PyModuleDef module = {.m_base = PyModuleDef_HEAD_INIT,
Expand Down
72 changes: 25 additions & 47 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -1,78 +1,57 @@

//! This crate provides FSharp language support for the [tree-sitter][] parsing
//! library. There are separate languages for implementation, (`.fs`),
//! signature (`.fsi`).
//! This crate provides Fsharp language support for the [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language_fsharp][language func] function to add
//! this language to a tree-sitter [Parser][], and then use the parser to parse
//! some code:
//! 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 = r#"
//! module M =
//! let x = 0
//! "#;
//! let mut parser = tree_sitter::Parser::new();
//! let language = tree_sitter_fsharp::LANGUAGE_FSHARP;
//! parser
//! .set_language(&tree_sitter_fsharp::language_fsharp())
//! .expect("Error loading FSharp grammar");
//! .set_language(&language.into())
//! .expect("Error loading Fsharp parser");
//! 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_fsharp.html
//! [language func]: fn.language.html
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/

use tree_sitter::Language;
use tree_sitter_language::LanguageFn;

extern "C" {
fn tree_sitter_fsharp() -> Language;
fn tree_sitter_fsharp_signature() -> Language;
}

/// Get the tree-sitter [Language][] for FSharp.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language_fsharp() -> Language {
unsafe { tree_sitter_fsharp() }
fn tree_sitter_fsharp() -> *const ();
fn tree_sitter_fsharp_signature() -> *const ();
}

/// Get the tree-sitter [Language][] for FSharp signature.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language_fsharp_signature() -> Language {
unsafe { tree_sitter_fsharp_signature() }
}
/// The tree-sitter [`LanguageFn`] for this grammar.
pub const LANGUAGE_FSHARP: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_fsharp) };
pub const LANGUAGE_SIGNATURE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_fsharp_signature) };

/// The content of the [`node-types.json`][] file for FSharp.
/// 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 FSHARP_NODE_TYPES: &'static str = include_str!("../../fsharp/src/node-types.json");
pub const FSHARP_NODE_TYPES: &str = include_str!("../../fsharp/src/node-types.json");
pub const SIGNATURE_NODE_TYPES: &str = include_str!("../../fsharp_signature/src/node-types.json");

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

/// The syntax highlighting query for FSharp.
pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
// NOTE: uncomment these to include any queries that this grammar contains:

/// The local-variable syntax highlighting query for FSharp.
pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");

/// The symbol tagging query for FSharp.
pub const TAGGING_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_fsharp() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(&super::language_fsharp())
.expect("Error loading FSharp grammar");
.set_language(&super::LANGUAGE_FSHARP.into())
.expect("Error loading Fsharp parser");

let code = r#"
module M =
Expand All @@ -88,8 +67,8 @@ mod tests {
fn test_fsharp_signature() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(&super::language_fsharp_signature())
.expect("Error loading FSharp signature grammar");
.set_language(&super::LANGUAGE_SIGNATURE.into())
.expect("Error loading Fsharp parser");

let code = r#"
module M =
Expand All @@ -101,4 +80,3 @@ mod tests {
assert!(!root.has_error());
}
}

10 changes: 2 additions & 8 deletions bindings/swift/TreeSitterFSharpTests/TreeSitterFSharpTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ final class TreeSitterFSharpTests: XCTestCase {
let parser = Parser()
try parser.setLanguage(language)

let source = """
module M =
let x = 0
"""
let source = "module M = ()"

let tree = try XCTUnwrap(parser.parse(source))
let root = try XCTUnwrap(tree.rootNode)
Expand All @@ -27,10 +24,7 @@ final class TreeSitterFSharpTests: XCTestCase {
let parser = Parser()
try parser.setLanguage(language)

let source = """
module M =
val x : int -> int
"""
let source = "val x : int -> int"

let tree = try XCTUnwrap(parser.parse(source))
let root = try XCTUnwrap(tree.rootNode)
Expand Down
Loading

0 comments on commit e0ab53d

Please sign in to comment.