Skip to content

Commit

Permalink
start signature grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
Nsidorenco committed Aug 31, 2024
1 parent 9bc4042 commit 4043417
Show file tree
Hide file tree
Showing 42 changed files with 802,759 additions and 806,707 deletions.
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ description = "fsharp grammar for the tree-sitter parsing library"
version = "0.0.1"
keywords = ["incremental", "parsing", "fsharp"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/nsidorenco/tree-sitter-fsharp"
repository = "https://github.com/ionide/tree-sitter-fsharp"
edition = "2021"
license = "MIT"

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

[lib]
Expand Down
22 changes: 13 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ VERSION := 0.0.1
LANGUAGE_NAME := tree-sitter-fsharp

# repository
SRC_DIR := src
FSHARP_DIR := grammars/fsharp
SIGNATURE_DIR := grammars/signature
SRC_DIR := grammars/fsharp/src

PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin 2>/dev/null)
PARSER_REPO_URL := $(shell git remote get-url origin 2>/dev/null)

ifeq ($(PARSER_URL),)
PARSER_URL := $(subst .git,,$(PARSER_REPO_URL))
Expand All @@ -28,7 +30,7 @@ LIBDIR ?= $(PREFIX)/lib
PCLIBDIR ?= $(LIBDIR)/pkgconfig

# object files
OBJS := $(patsubst %.c,%.o,$(wildcard $(SRC_DIR)/*.c))
OBJS := $(patsubst %.c,%.o,$(wildcard grammars/*/src/*.c))

# flags
ARFLAGS := rcs
Expand Down Expand Up @@ -81,14 +83,16 @@ $(LANGUAGE_NAME).pc: bindings/c/$(LANGUAGE_NAME).pc.in
-e 's|=$(PREFIX)|=$${prefix}|' \
-e 's|@PREFIX@|$(PREFIX)|' $< > $@

$(SRC_DIR)/parser.c: grammar.js
$(TS) generate --no-bindings
$(FSHARP_DIR)/src/parser.c: $(FSHARP_DIR)/grammar.js
cd $(FSHARP_DIR) && $(TS) generate --no-bindings

$(SIGNATURE_DIR)/src/parser.c: $(FSHARP_DIR)/grammar.js $(SIGNATURE_DIR)/grammar.js
cd $(SIGNATURE_DIR) && $(TS) generate --no-bindings

install: all
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)'
install -m644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h
install -m644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
install -m644 lib$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a
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 -m755 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)
Expand Down
68 changes: 30 additions & 38 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,39 @@
import PackageDescription

let package = Package(
name: "TreeSitterFsharp",
platforms: [.macOS(.v10_13), .iOS(.v11)],
name: "TreeSitterFSharp",
products: [
.library(name: "TreeSitterFsharp", targets: ["TreeSitterFsharp"]),
.library(name: "TreeSitterFSharp", targets: ["TreeSitterFSharp"]),
],
dependencies: [
.package(url: "https://github.com/ChimeHQ/SwiftTreeSitter", from: "0.8.0"),
],
dependencies: [],
targets: [
.target(name: "TreeSitterFsharp",
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: [
"src/parser.c",
// NOTE: if your language has an external scanner, add it here.
],
resources: [
.copy("queries")
],
publicHeadersPath: "bindings/swift",
cSettings: [.headerSearchPath("src")])
.target(
name: "TreeSitterFSharp",
dependencies: [],
path: ".",
sources: [
"grammars/fsharp/src/parser.c",
"grammars/fsharp/src/scanner.c",
"grammars/signature/src/parser.c",
"grammars/signature/src/scanner.c",
],
resources: [
.copy("queries")
],
publicHeadersPath: "bindings/swift",
cSettings: [.headerSearchPath("grammars/fsharp/src")]
),
.testTarget(
name: "TreeSitterFSharpTests",
dependencies: [
"SwiftTreeSitter",
"TreeSitterFSharp",
],
path: "bindings/swift/TreeSitterFSharpTests"
)
],
cLanguageStandard: .c11
)

17 changes: 14 additions & 3 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@
"bindings/node/binding.cc",
"grammars/fsharp/src/parser.c",
"grammars/fsharp/src/scanner.c",
"grammars/signature/src/parser.c",
"grammars/signature/src/scanner.c"
],
"cflags_c": [
"-std=c11",
"conditions": [
["OS!='win'", {
"cflags_c": [
"-std=c11",
],
}, { # OS == "win"
"cflags_c": [
"/std:c11",
"/utf-8",
],
}],
],
}
},
]
}
1 change: 1 addition & 0 deletions bindings/c/tree-sitter-fsharp.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
#endif

const TSLanguage *tree_sitter_fsharp(void);
const TSLanguage *tree_sitter_fsharp_signature(void);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/tree-sitter-fsharp.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ libdir=@LIBDIR@
includedir=@INCLUDEDIR@

Name: tree-sitter-fsharp
Description: Fsharp grammar for tree-sitter
Description: FSharp grammar for tree-sitter
URL: @URL@
Version: @VERSION@
Requires: @REQUIRES@
Expand Down
1 change: 0 additions & 1 deletion bindings/go/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tree_sitter_fsharp

// #cgo CFLAGS: -std=c11 -fPIC
// #include "../../src/parser.c"
// // NOTE: if your language has an external scanner, add it here.
import "C"

import "unsafe"
Expand Down
13 changes: 13 additions & 0 deletions bindings/go/binding_fsharp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tree_sitter_fsharp

// #cgo CFLAGS: -I../../grammars/fsharp/src -std=c11 -fPIC
// #include "../../grammars/fsharp/src/parser.c"
// #include "../../grammars/fsharp/src/scanner.c"
import "C"

import "unsafe"

// Get the tree-sitter Language for FSharp.
func FSharp() unsafe.Pointer {
return unsafe.Pointer(C.tree_sitter_fsharp())
}
13 changes: 13 additions & 0 deletions bindings/go/binding_signature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tree_sitter_fsharp

// #cgo CFLAGS: -I../../grammars/fsharp/src -std=c11 -fPIC
// #include "../../grammars/fsharp/src/parser.c"
// #include "../../grammars/fsharp/src/scanner.c"
import "C"

import "unsafe"

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

import (
"context"
"testing"

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

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

sourceCode := []byte("module M = ()")

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

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

sourceCode := []byte("val x : int -> int")

node, err := tree_sitter.ParseCtx(context.Background(), sourceCode, language)
if err != nil || node.HasError() {
t.Errorf("Error parsing FSharpSignature")
}
}
2 changes: 1 addition & 1 deletion bindings/go/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/tree-sitter/tree-sitter-fsharp
module github.com/ionide/tree-sitter-fsharp

go 1.22

Expand Down
2 changes: 1 addition & 1 deletion bindings/node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ type Language = {
};

export const fsharp: Language;
export const interface: Language;
export const signature: Language;
2 changes: 1 addition & 1 deletion bindings/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module.exports = require("node-gyp-build")(root);

try {
module.exports.fsharp.nodeTypeInfo = require("../../grammars/fsharp/src/node-types.json");
module.exports.interface.nodeTypeInfo = require("../../grammars/interface/src/node-types.json");
module.exports.signature.nodeTypeInfo = require("../../grammars/signature/src/node-types.json");
} catch (_) {}
28 changes: 28 additions & 0 deletions bindings/python/tests/test_binding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from unittest import TestCase

import tree_sitter_fsharp
from tree_sitter import Language, Parser


class TestLanguage(TestCase):
def test_fsharp_grammar(self):
language = Language(tree_sitter_fsharp.fsharp())
parser = Parser(language)
tree = parser.parse(
b"""
module M =
let x = 0
"""
)
self.assertFalse(tree.root_node.has_error)

def test_signature_grammar(self):
language = Language(tree_sitter_fsharp.signature())
parser = Parser(language)
tree = parser.parse(
b"""
module M =
val x : int -> int
"""
)
self.assertFalse(tree.root_node.has_error)
48 changes: 46 additions & 2 deletions bindings/python/tree_sitter_fsharp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
"Fsharp grammar for tree-sitter"

from ._binding import language
from importlib.resources import files as _files

__all__ = ["language"]
from ._binding import fsharp, signature


def _get_query(name, file):
query = _files(f"{__package__}.queries") / file
globals()[name] = query.read_text()
return globals()[name]


def __getattr__(name):
if name == "HIGHLIGHTS_QUERY":
return _get_query("HIGHLIGHTS_QUERY", "highlights.scm")
if name == "LOCALS_QUERY":
return _get_query("LOCALS_QUERY", "locals.scm")
if name == "TAGS_QUERY":
return _get_query("TAGS_QUERY", "tags.scm")

raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


__all__ = [
"fsharp",
"signature",
"HIGHLIGHTS_QUERY",
"LOCALS_QUERY",
"TAGS_QUERY",
]


def __dir__():
return sorted(
__all__
+ [
"__all__",
"__builtins__",
"__cached__",
"__doc__",
"__file__",
"__loader__",
"__name__",
"__package__",
"__path__",
"__spec__",
]
)
11 changes: 10 additions & 1 deletion bindings/python/tree_sitter_fsharp/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
def language() -> int: ...
from typing import Final


HIGHLIGHTS_QUERY: Final[str]
LOCALS_QUERY: Final[str]
TAGS_QUERY: Final[str]


def fsharp() -> int: ...
def signature() -> int: ...
Loading

0 comments on commit 4043417

Please sign in to comment.