Skip to content

erasin/tree-sitter-bsn

Repository files navigation

tree-sitter-bsn

中文 | A Tree-sitter grammar for Bevy Scene Notation (BSN).

Installation

Helix Editor

Add the following configuration to ~/.config/helix/languages.toml:

# ==================== BSN (Bevy Scene Notation) ====================
[[language]]
name = "bsn"
scope = "source.bsn"
injection-regex = "bsn"
file-types = ["bsn"]
roots = []
auto-format = false
comment-token = "//"
block-comment-tokens = { start = "/*", end = "*/" }
indent = { tab-width = 2, unit = "  " }

[[grammar]]
name = "bsn"
source = { git = "https://github.com/erasin/tree-sitter-bsn", rev = "HEAD" }
hx --grammar fetch
hx --grammar build

Restart Helix and open a .bsn file to use the grammar.

Syntax Support

:"player.bsn"           # inheritance declaration
Player                   # entity
Sprite { image: "player.png" }  # struct body
Health(10)              # tuple body
Transform {             # nested struct
    translation: Vec3 { y: 4.0 }
}
Children [               # block structure
    (Hat)
    (Sprite { image: "hat.png" })
]
#Root                  # name shorthand
@mytemplate { value: 10 }  # template patch
Emotion::Happy { amount: 10 }  # scoped identifier
Transform::IDENTITY     # scoped constant

Note: Rust closure syntax (e.g. on(|jump: On<Jump>| { ... })) is not part of BSN format, it only exists in Rust code's bsn! macro invocations.

Project Structure

├── grammar.js          # Tree-sitter grammar definition
├── src/                # Generated parser code
├── queries/            # Helix query files
│   ├── highlights.scm  # syntax highlighting
│   ├── indents.scm     # indentation rules
│   ├── textobjects.scm  # text objects
│   ├── tags.scm        # code navigation
│   ├── rainbows.scm    # rainbow brackets
│   └── injections.scm  # language injection
└── test.bsn            # test file

Local Development

# Generate parser
tree-sitter generate

# Test parsing
tree-sitter parse test.bsn

# Run tests
tree-sitter test

BSN! Macro Highlighting in Rust

When using bsn! or bsn_list! macros in Rust files, you can get BSN syntax highlighting via language injection.

Configuration Steps

  1. Create/edit the Rust injection query file:
mkdir -p ~/.config/helix/runtime/queries/rust
cat >> ~/.config/helix/runtime/queries/rust/injections.scm << 'EOF'
; Inject BSN highlighting for bsn! and bsn_list! macros in Rust
((macro_invocation
    macro: (identifier) @_macro
    (token_tree) @injection.content)
  (#match? @_macro "^(bsn|bsn_list)$")
  (#set! injection.language "bsn"))
EOF
  1. Restart Helix and open a Rust file to see highlighting for bsn! macro content.

Example

fn player() -> impl Scene {
    bsn! {
        Player
        Sprite { image: "player.png" }
        Transform {
            translation: Vec3 { y: 4.0 }
        }
    }
}

Links

About

tree-sitter-bsn for bevy

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors