Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust crate #57

Merged
merged 12 commits into from
Nov 21, 2020
20 changes: 20 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Publish release

on:
pull_request
# create:
# tags:
# - v*

jobs:
cargo:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: cargo login ${CRATES_IO_TOKEN}
env:
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
harrysarson marked this conversation as resolved.
Show resolved Hide resolved
- run: cargo publish --dry-run
24 changes: 23 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Test OSX and Linux
on: [push, pull_request]

jobs:
build:
node:
runs-on: ${{ matrix.os }}

strategy:
Expand All @@ -27,3 +27,25 @@ jobs:
- name: Test examples
run: |
script/parse-examples

rust:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: lint
uses: actions-rs/cargo@v1
with:
command: clippy
- name: test
uses: actions-rs/cargo@v1
with:
command: test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package-lock.json
Cargo.lock
node_modules
build
*.log
prebuilds
.idea
.vscode/ipch
target/

# Examples generated during automated tests
examples/**
Expand Down
35 changes: 35 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "tree-sitter-elm"
description = "Rust bindings for the tree sitter elm language parser"
authors = [
"Harry Sarson <[email protected]>",
"Razze"

]
homepage = "https://tree-sitter.github.io/tree-sitter/"
repository = "https://github.com/Razzeee/tree-sitter-elm"
keywords = ["elm", "tree", "sitter", "parsing", "incremental"]
categories = ["parser-implementations", "api-bindings", "text-editors", "parsing"]
edition = "2018"
license = "MIT"
build = "rust/build.rs"

# Keep in sync with package.json
version = "4.3.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tree-sitter = "0.17.1"

[build-dependencies]
cc = "1.0.61"


[lib]
path = "rust/lib.rs" # The source file of the target.

[dev-dependencies]
serde = { version = "1.0.117", features = ["derive"] }
serde_json = "1.0.59"
toml = "0.5.7"
2 changes: 1 addition & 1 deletion HOW_TO_RELEASE.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
1. Increase the version number in the package.json
1. Increase the version number in the package.json and the Cargo.toml
2. Push the code to master
3. Run `npm publish`
4. Create a release on github with the name being the version number from before prefixed with `v` for e.g. `v1.1.0`
Expand Down
17 changes: 17 additions & 0 deletions rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::path::Path;

fn main() {
let dir = Path::new("src");

cc::Build::new()
.include(&dir)
.file(dir.join("parser.c"))
.flag_if_supported("-Wno-unused-but-set-variable")
.compile("tree-sitter-parser");

cc::Build::new()
.cpp(true)
.include(&dir)
.file(dir.join("scanner.cc"))
.compile("tree-sitter-scanner");
razzeee marked this conversation as resolved.
Show resolved Hide resolved
}
52 changes: 52 additions & 0 deletions rust/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![warn(clippy::pedantic)]

use tree_sitter::Language;

extern "C" {
#[link_name = "tree_sitter_elm"]
fn raw_tree_sitter_elm() -> Language;
}

#[must_use]
pub fn tree_sitter_elm() -> Language {
unsafe { raw_tree_sitter_elm() }
}

#[cfg(test)]
mod tests {
use std::fs;
use tree_sitter::Parser;

#[test]
fn smoke() {
let source_code = "test : Test.Test";
let tree = {
let mut parser = Parser::new();
let language = super::tree_sitter_elm();
parser.set_language(language).unwrap();
parser.parse(source_code, None).unwrap()
};
let root_node = tree.root_node();

assert_eq!(root_node.kind(), "file");
assert_eq!(root_node.start_position().column, 0);
assert_eq!(root_node.end_position().column, 16);
}

#[test]
fn versions() {
#[derive(serde::Deserialize)]
struct Package {
version: String,
}
#[derive(serde::Deserialize)]
struct CargoManifest {
package: Package,
}
let npm: Package =
serde_json::from_str(&fs::read_to_string("package.json").unwrap()).unwrap();
let cargo: CargoManifest =
toml::from_str(&fs::read_to_string("Cargo.toml").unwrap()).unwrap();
assert_eq!(npm.version, cargo.package.version);
}
razzeee marked this conversation as resolved.
Show resolved Hide resolved
}