Skip to content

Commit

Permalink
Update publish workflow to go to crates.io too
Browse files Browse the repository at this point in the history
Also update the `README` to lead with instructions for using the crate
and the npm package. This is the future!
  • Loading branch information
alex-pinkus committed Jan 2, 2022
1 parent 96d199a commit e00de7b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 27 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: NPM publish
name: Publish to `npm` and `crates.io`

on:
workflow_run:
Expand All @@ -7,7 +7,7 @@ on:
- completed

jobs:
publish:
npm-publish:
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
steps:
Expand All @@ -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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ tree-sitter = "~0.20.0"

[build-dependencies]
cc = "1.0"

[dev-dependencies]
anyhow = "1.0"
50 changes: 45 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
16 changes: 0 additions & 16 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
*/
}
26 changes: 22 additions & 4 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

0 comments on commit e00de7b

Please sign in to comment.