Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up tree-sitter
uses: tree-sitter/setup-action/cli@v1
uses: tree-sitter/setup-action/cli@v2
- name: Run tests
uses: tree-sitter/parser-test-action@v2
with:
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ include = [
path = "bindings/rust/lib.rs"

[dependencies]
tree-sitter = "~0.20.10"
tree-sitter-language = "0.1"

[build-dependencies]
cc = "1.0"

[dev-dependencies]
tree-sitter = "0.25"
Comment on lines 22 to +29
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,44 @@
[![matrix][matrix]](https://matrix.to/#/#tree-sitter-chat:matrix.org)
[![npm][npm]](https://www.npmjs.com/package/tree-sitter-glimmer-typescript)

A tree-sitter grammar for Glimmer-flavored JavaScript (`.gjs`). Wraps
[tree-sitter-javascript][ts-js] and adds the `<template>...</template>`
syntax used by Ember/Glimmer.

## Source vs. generated files

The single source of truth for the grammar is `grammar.js`. Most of the
files under `src/` are produced by `tree-sitter generate` and **must not
be edited by hand** — any change there will be overwritten on the next
generation, and CI verifies that committed artifacts match what
generation produces.

**Hand-written (edit these):**

- `grammar.js` — grammar definition
- `src/scanner.c` — external scanner (handles `<template>` raw text and
custom ASI behavior)
- `queries/**/*.scm` — highlight, locals, and tags queries
- `test/corpus/**/*.txt` — parser test fixtures
- `bindings/**` — language binding code (Node, Rust, Python, Go, Swift, C)
- `binding.gyp`, `Cargo.toml`, `package.json`, `tree-sitter.json` — build
and package metadata
- `README.md`

**Auto-generated by `tree-sitter generate` (do not edit):**

- `src/parser.c` — the parse table (the `/* Automatically @generated by
tree-sitter */` header is the canonical marker)
- `src/grammar.json` — JSON form of the evaluated grammar
- `src/node-types.json` — node type schema
- `src/tree_sitter/parser.h`, `array.h`, `alloc.h` — vendored tree-sitter
runtime headers

If you change `grammar.js` or bump the `tree-sitter` CLI, run
`npm run build` (or `tree-sitter generate`) and commit the regenerated
files alongside your source change.

[ci]: https://github.com/ember-tooling/tree-sitter-glimmer-javascript/actions/workflows/ci.yml/badge.svg
[matrix]: https://img.shields.io/matrix/tree-sitter-chat%3Amatrix.org?label=matrix
[npm]: https://img.shields.io/npm/v/tree-sitter-glimmer-javascript
[ts-js]: https://github.com/tree-sitter/tree-sitter-javascript
2 changes: 1 addition & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"sources": [
"bindings/node/binding.cc",
"src/parser.c",
# NOTE: if your language has an external scanner, add it here.
"src/scanner.c",
],
"conditions": [
["OS!='win'", {
Expand Down
41 changes: 18 additions & 23 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,47 @@
//! This crate provides glimmer_javascript language support for the [tree-sitter][] parsing library.
//! This crate provides Glimmer-flavored JavaScript language support for the
//! [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language][language func] function to add this language to a
//! Typically, you will use the [LANGUAGE] constant to add this language to a
//! tree-sitter [Parser][], and then use the parser to parse some code:
//!
//! ```
//! let code = "";
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_glimmer_javascript::language()).expect("Error loading glimmer_javascript grammar");
//! use tree_sitter::Parser;
//!
//! let code = "const x = 1;";
//! let mut parser = Parser::new();
//! parser
//! .set_language(&tree_sitter_glimmer_javascript::LANGUAGE.into())
//! .expect("Error loading glimmer_javascript parser");
//! let tree = parser.parse(code, None).unwrap();
//! assert!(!tree.root_node().has_error());
//! ```
//!
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
//! [language func]: fn.language.html
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/

use tree_sitter::Language;
use tree_sitter_language::LanguageFn;

extern "C" {
fn tree_sitter_glimmer_javascript() -> Language;
fn tree_sitter_glimmer_javascript() -> *const ();
}

/// Get the tree-sitter [Language][] for this grammar.
/// The tree-sitter [`LanguageFn`] for Glimmer-flavored JavaScript.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language() -> Language {
unsafe { tree_sitter_glimmer_javascript() }
}
/// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html
pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_glimmer_javascript) };

/// The content of the [`node-types.json`][] file for this grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");

// Uncomment these to include any queries that this grammar contains

// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");

#[cfg(test)]
mod tests {
#[test]
fn test_can_load_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(super::language())
.set_language(&super::LANGUAGE.into())
.expect("Error loading glimmer_javascript language");
}
}
Loading
Loading