Skip to content

Commit

Permalink
Golang driver written by Claude-3
Browse files Browse the repository at this point in the history
  • Loading branch information
slimslenderslacks committed Oct 9, 2024
1 parent 970db3a commit 6fd4da1
Show file tree
Hide file tree
Showing 25 changed files with 2,118 additions and 72 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
.lsp
/prompts/stable-diffusion/*.png
**/.DS_Store
/functions/tree-sitter/.cpcache/
/functions/tree-sitter/.nrepl-port
/functions/tree-sitter/hs_err_*.log
/functions/tree-sitter/main
/functions/tree-sitter-clj/target/
/functions/tree-sitter-clj/.cpcache/
1 change: 1 addition & 0 deletions functions/tree-sitter-clj/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
32 changes: 32 additions & 0 deletions functions/tree-sitter-clj/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# syntax = docker/dockerfile:1.4
FROM nixos/nix:2.21.1@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder

WORKDIR /tmp/build
RUN mkdir /tmp/nix-store-closure

# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
# hadolint ignore=SC2046
RUN --mount=type=cache,target=/nix,from=nixos/nix:2.21.1,source=/nix \
--mount=type=cache,target=/root/.cache \
--mount=type=bind,target=/tmp/build \
<<EOF
nix \
--extra-experimental-features "nix-command flakes" \
--option filter-syscalls false \
--extra-trusted-substituters "https://cache.iog.io" \
--extra-trusted-public-keys "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" \
--show-trace \
--log-format raw \
build . --out-link /tmp/output/result
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
EOF

FROM scratch

WORKDIR /app

COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/output/ /app/

ENTRYPOINT ["/app/result/bin/entrypoint"]
24 changes: 24 additions & 0 deletions functions/tree-sitter-clj/build.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns build
(:require [clojure.tools.build.api :as b]))

(def class-dir "target/classes")
(def basis (b/create-basis {:project "deps.edn"}))

(defn compile-java [_]
(b/delete {:path "target"})
(b/javac {:src-dirs ["src/java"]
:class-dir class-dir
:basis basis
:javac-opts ["-source" "8" "-target" "8"]}))

(defn uber [_]
(compile-java nil) ; Compile Java first
(b/copy-dir {:src-dirs ["src/clojure" "resources"]
:target-dir class-dir})
(b/compile-clj {:basis basis ; compile clojure code
:src-dirs ["src/clojure"]
:class-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file "target/my-project.jar"
:basis basis
:main 'docker.ts}))
86 changes: 86 additions & 0 deletions functions/tree-sitter-clj/cmd/ts/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
"io/ioutil"
"os"

sitter "github.com/smacker/go-tree-sitter"
"github.com/smacker/go-tree-sitter/python"
"github.com/smacker/go-tree-sitter/yaml"
"github.com/smacker/go-tree-sitter/sql"
"github.com/smacker/go-tree-sitter/bash"
"github.com/smacker/go-tree-sitter/html"
"github.com/smacker/go-tree-sitter/dockerfile"
)

func main() {
// Check if both language and query string are provided as arguments
if len(os.Args) < 3 {
fmt.Println("Usage: ./program <language> <query_string>")
return
}
language := os.Args[1]
queryString := os.Args[2]

// Create a parser
parser := sitter.NewParser()

// Set the language based on the first argument
switch language {
case "python":
parser.SetLanguage(python.GetLanguage())
case "yaml":
parser.SetLanguage(yaml.GetLanguage())
case "sql":
parser.SetLanguage(sql.GetLanguage())
case "bash":
parser.SetLanguage(bash.GetLanguage())
case "html":
parser.SetLanguage(html.GetLanguage())
case "dockerfile":
parser.SetLanguage(dockerfile.GetLanguage())
default:
fmt.Printf("Unsupported language: %s\n", language)
return
}

// Read source code from stdin
sourceCode, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Println("Error reading from stdin:", err)
return
}

// Parse the source code
tree := parser.Parse(nil, sourceCode)
defer tree.Close()

// Create a query
query, err := sitter.NewQuery([]byte(queryString), python.GetLanguage())
if err != nil {
fmt.Println("Error creating query:", err)
return
}
defer query.Close()

// Execute the query
qc := sitter.NewQueryCursor()
defer qc.Close()

qc.Exec(query, tree.RootNode())

// Iterate over the query results
for {
match, ok := qc.NextMatch()
if !ok {
break
}

for _, capture := range match.Captures {
captureName := query.CaptureNameForId(capture.Index)
nodeText := capture.Node.Content(sourceCode)
fmt.Printf("Capture: %s, Node: %s\n", captureName, nodeText)
}
}
}
Loading

0 comments on commit 6fd4da1

Please sign in to comment.