From e00de7baa5d8eca1c179123d2573338c41654e90 Mon Sep 17 00:00:00 2001 From: Alex Pinkus Date: Sun, 2 Jan 2022 14:37:09 -0800 Subject: [PATCH] Update publish workflow to go to crates.io too Also update the `README` to lead with instructions for using the crate and the npm package. This is the future! --- .github/workflows/publish.yml | 20 ++++++++++++-- Cargo.toml | 3 +++ README.md | 50 +++++++++++++++++++++++++++++++---- bindings/rust/build.rs | 16 ----------- bindings/rust/lib.rs | 26 +++++++++++++++--- 5 files changed, 88 insertions(+), 27 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 8f75b6d..6ac6cc4 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,4 +1,4 @@ -name: NPM publish +name: Publish to `npm` and `crates.io` on: workflow_run: @@ -7,7 +7,7 @@ on: - completed jobs: - publish: + npm-publish: if: ${{ github.ref == 'refs/heads/main' }} runs-on: ubuntu-latest steps: @@ -22,3 +22,19 @@ jobs: - uses: JS-DevTools/npm-publish@v1 with: token: ${{ secrets.NPM_TOKEN }} + crates-io-publish: + if: ${{ github.ref == 'refs/heads/main' }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + - run: npm install + - run: npm run test-ci + - run: cargo test + - uses: katyo/publish-crates@v1 + with: + registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }} + args: --allow-dirty diff --git a/Cargo.toml b/Cargo.toml index c03e9ac..8e0d785 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,6 @@ tree-sitter = "~0.20.0" [build-dependencies] cc = "1.0" + +[dev-dependencies] +anyhow = "1.0" diff --git a/README.md b/README.md index c8644ff..839a964 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,51 @@ programming language. ## Getting started -This grammar is structured similarly to any other tree-sitter grammar. As with most grammars, you can use the cli to -generate or test the parser, and to parse any arbitrary code. An overview of the CLI tool can be found -[here](https://tree-sitter.github.io/tree-sitter/creating-parsers#tool-overview). A moderate amount of work has gone -into building out a corpus, but the corpus mostly encodes the current state of the parser, including a few bugs that it -has. +To use this parser to parse Swift code, you'll want to depend on either the Rust crate or the NPM package. + +### Rust +To use the Rust crate, you'll add this to your `Cargo.toml`: +``` +tree-sitter = "0.20.0" +experimental-tree-sitter-swift = "=0.0.1" +``` + +Then you can use a `tree-sitter` parser with the language declared here: + +``` +let mut parser = tree_sitter::Parser::new(); +parser.set_language(experimental_tree_sitter_swift::language())?; + +// ... + +let tree = parser.parse(&my_source_code, None) + .ok_or_else(|| /* error handling code */)?; +``` + +### Javascript + +To use this from NPM, you'll add similar dependencies to `package.json`: +``` +"dependencies: { + "experimental-tree-sitter-swift": "0.0.1", + "tree-sitter": "^0.20.0" +} +``` + +Your usage of the parser will look like: +``` +const Parser = require("tree-sitter"); +const Swift = require("experimental-tree-sitter-swift"); + +const parser = new Parser(); +parser.setLanguage(Swift); + +// ... + +const tree = parser.parse(mySourceCode); +``` + +### Editing the grammar With this package checked out, a common workflow for editing the grammar will look something like: diff --git a/bindings/rust/build.rs b/bindings/rust/build.rs index 2dd4a7a..8f87ee6 100644 --- a/bindings/rust/build.rs +++ b/bindings/rust/build.rs @@ -16,20 +16,4 @@ fn main() { c_config.compile("parser"); println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap()); - - // If your language uses an external scanner written in C++, - // then include this block of code: - - /* - let mut cpp_config = cc::Build::new(); - cpp_config.cpp(true); - cpp_config.include(&src_dir); - cpp_config - .flag_if_supported("-Wno-unused-parameter") - .flag_if_supported("-Wno-unused-but-set-variable"); - let scanner_path = src_dir.join("scanner.cc"); - cpp_config.file(&scanner_path); - cpp_config.compile("scanner"); - println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); - */ } diff --git a/bindings/rust/lib.rs b/bindings/rust/lib.rs index 14bd07e..632a0ad 100644 --- a/bindings/rust/lib.rs +++ b/bindings/rust/lib.rs @@ -42,11 +42,29 @@ pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json"); #[cfg(test)] mod tests { + use anyhow::{anyhow, Result}; + #[test] - fn test_can_load_grammar() { + fn test_can_load_grammar() -> Result<()> { let mut parser = tree_sitter::Parser::new(); - parser - .set_language(super::language()) - .expect("Error loading swift language"); + parser.set_language(super::language())?; + + Ok(()) + } + + #[test] + fn test_can_parse_basic_file() -> Result<()> { + let mut parser = tree_sitter::Parser::new(); + parser.set_language(super::language())?; + + let tree = parser.parse("_ = \"Hello!\"\n", None) + .ok_or_else(|| anyhow!("Unable to parse!"))?; + + assert_eq!( + "(source_file (assignment (directly_assignable_expression (simple_identifier)) (line_string_literal (line_str_text))))", + tree.root_node().to_sexp(), + ); + + Ok(()) } }