Skip to main content

A Windows Batch/CMD grammar for tree-sitter

Project description

tree-sitter-batch

Windows Batch/CMD grammar for tree-sitter.

Parses .bat and .cmd files into a concrete syntax tree for syntax highlighting, code navigation, and analysis.

Features

  • Control flowIF/ELSE (EXIST, DEFINED, ERRORLEVEL, comparison with NOT), FOR (/D /R /L /F), GOTO, CALL, EXIT /B
  • VariablesSET (plain, /A arithmetic, /P prompt, display-only without =), %VAR%, !VAR!, %%i, %~dp0, %VAR:old=new%, subscripted delayed expansion !ARR[%%i]!, for-variable modifiers %%~dpnxf %%~zS %%~$PATH:F, escaped forms %%VAR%% %%%%i
  • Echo — free-form text with literal ( ) ! %, inline strings, and variable references
  • Operators — pipes |, redirects > >> < 2> 2>&1 (fds 0-9, including variable handles >&%FD% / <&%FD%), conditional && ||, separator &
  • Line continuation — trailing caret ^ joins the current line with the next (e.g. "%JAVACMD%" ^ followed by indented arguments)
  • Commands — bare names, variable references as command, and quoted paths ("C:\path\app.exe" args, call "%SCRIPT%" %*)
  • Structure — labels :name, comments REM ::, parenthesized blocks (including @(...)), @ECHO OFF, macro invocations, DosTips idioms (call,) / (call;) / (call) for ERRORLEVEL manipulation
  • ScopeSETLOCAL/ENDLOCAL with ENABLEDELAYEDEXPANSION
  • Polyglot headers — tolerates batch/PowerShell <# ... #> header lines and batch/VBScript lines marked with a trailing 'VBS so SysToolsLib-style dual-language scripts parse cleanly
  • Case-insensitive — all keywords match regardless of casing

Example

@echo off
REM Build script
setlocal enabledelayedexpansion

set "PROJECT=MyApp"
set /a VERSION=1

if not exist "dist" (
  mkdir dist
)

for %%f in (src\*.txt) do (
  copy "%%f" "dist\"
)

if %ERRORLEVEL% == 0 (
  echo Build successful
) else (
  echo Build failed
  exit /b 1
)

exit /b 0

Parsed tree:

(program
  (echo_off)
  (comment)
  (setlocal_stmt)
  (variable_assignment
    (set_keyword) (variable_name) (assignment_value))
  (variable_assignment
    (set_keyword) (arithmetic_assignment (set_option) (arithmetic_expression)))
  (if_stmt
    (string)
    (parenthesized
      (cmd (command_name) (argument_list (argument_value)))))
  (for_stmt
    (for_variable)
    (for_set (for_set_literal))
    (parenthesized
      (cmd (command_name) (argument_list (string) (string)))))
  (if_stmt
    (variable_reference)
    (comparison_op)
    (argument_value)
    (parenthesized
      (cmd (command_name) (argument_list (argument_value) (argument_value))))
    (else_clause
      (parenthesized
        (cmd (command_name) (argument_list (argument_value) (argument_value)))
        (exit_stmt (integer)))))
  (exit_stmt (integer)))

Installation

npm

npm install tree-sitter-batch

Cargo

cargo add tree-sitter-batch

PyPI

pip install tree-sitter-batch

Go

import tree_sitter_batch "github.com/wharflab/tree-sitter-batch/bindings/go"

The root package also exports the bundled queries/highlights.scm via go:embed:

import batch "github.com/wharflab/tree-sitter-batch"

lang := batch.GetLanguage()
query, _ := batch.GetHighlightsQuery()
// or access the raw .scm source:
// raw := batch.HighlightsQuery

Usage

Node.js

import Parser from "tree-sitter";
import Batch from "tree-sitter-batch";

const parser = new Parser();
parser.setLanguage(Batch);

const tree = parser.parse(`@echo off\necho Hello World\n`);
console.log(tree.rootNode.toString());

Rust

let mut parser = tree_sitter::Parser::new();
let language = tree_sitter_batch::LANGUAGE;
parser.set_language(&language.into()).unwrap();

let tree = parser.parse("@echo off\necho Hello\n", None).unwrap();
println!("{}", tree.root_node().to_sexp());

Python

from tree_sitter import Language, Parser
import tree_sitter_batch

parser = Parser(Language(tree_sitter_batch.language()))
tree = parser.parse(b"@echo off\necho Hello\n")
print(tree.root_node.sexp())

Syntax Highlighting

The grammar ships with a queries/highlights.scm file for use in editors that support tree-sitter highlighting (Neovim, Helix, Zed, etc.).

References

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tree_sitter_batch-0.11.1.tar.gz (54.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tree_sitter_batch-0.11.1-cp310-abi3-win_arm64.whl (44.3 kB view details)

Uploaded CPython 3.10+Windows ARM64

tree_sitter_batch-0.11.1-cp310-abi3-win_amd64.whl (46.3 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_batch-0.11.1-cp310-abi3-musllinux_1_2_x86_64.whl (54.5 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

tree_sitter_batch-0.11.1-cp310-abi3-musllinux_1_2_aarch64.whl (54.4 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tree_sitter_batch-0.11.1-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (54.8 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

tree_sitter_batch-0.11.1-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (55.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

tree_sitter_batch-0.11.1-cp310-abi3-macosx_11_0_arm64.whl (45.0 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_batch-0.11.1-cp310-abi3-macosx_10_9_x86_64.whl (43.1 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

Details for the file tree_sitter_batch-0.11.1.tar.gz.

File metadata

  • Download URL: tree_sitter_batch-0.11.1.tar.gz
  • Upload date:
  • Size: 54.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tree_sitter_batch-0.11.1.tar.gz
Algorithm Hash digest
SHA256 752ddd015d9412ba2d4df058388dae84cd2c905c67057a039c0628425b4967ea
MD5 9f40db7db70eaff6baf1d7dc08f6a56f
BLAKE2b-256 876c5a296d17eb24270b68270afc70f1842ee626b8c7c71403aec2d88aeb1fd8

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.1.tar.gz:

Publisher: publish_pypi.yml on wharflab/tree-sitter-batch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_batch-0.11.1-cp310-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.1-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 8997ca43a5d29e654546a72a5f70a0c2a6a606ca2bbe7538fda7d4a70d4b4e6d
MD5 c1cd20de364d33b10f0ef89ddbeadc2b
BLAKE2b-256 17250abbe6239285d848b5e41ba6fa8436504c2752cf8e6f590b91281790fa65

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.1-cp310-abi3-win_arm64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-batch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_batch-0.11.1-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 755ca1e2dcbc06e62e7e254fd99b40bcef8d35a927fb8f123c72c60baf4bc6f5
MD5 609ab1e8cb90d5aae64fecaf8afd739e
BLAKE2b-256 7a9096feb639738854a4da7e4296bf6abec6b700b60b05d56238207b60fe1861

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.1-cp310-abi3-win_amd64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-batch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_batch-0.11.1-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.1-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6633f51111cd439324d835284d742ca4ce12eb865372b006eb9857f6e73946f0
MD5 a59b74fe5cdcfca9909a3f11a9d08eff
BLAKE2b-256 35136e48f98733d12dc324225f7b85c858cbe3e5b455f0b60671a381619b6d00

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.1-cp310-abi3-musllinux_1_2_x86_64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-batch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_batch-0.11.1-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.1-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 eea9c860e05e970f8b6405e27f0511eddf30fe64a5d6325bf619284314826373
MD5 e467c65d978c0e9cac22bc3975912bcd
BLAKE2b-256 adff2df1be60d74e59e2b149dad2c14a6b18764fceee2bc81208fb71dbd222c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.1-cp310-abi3-musllinux_1_2_aarch64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-batch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_batch-0.11.1-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.1-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 cba9429978825a55e6b1acae9775b748c3cffbe4e3187ef41f72e1adf28877eb
MD5 5e35a47ce0b3f5e63da42696190bb491
BLAKE2b-256 b1b673351c80ae8a539c82a07cf1e6baf10d13da9d82ded4a3b2c1ffbb0d7417

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.1-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-batch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_batch-0.11.1-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.1-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 03957e781f130e76843531719d731519db9eb1e8981c5ded44923b872b87f4b9
MD5 bd447159f5588489fec6a88d50a79aad
BLAKE2b-256 cbd7ed1ef6c38d9021c2eb78d67c8c27a5c382b0fa0794f7a7477f9c2bd2cf56

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.1-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-batch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_batch-0.11.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 38c56f96a5ed8ecc498da52e67fd1cfd4409e67270e9242d1c8a52cb55b9dfaf
MD5 1f8ce88979a8f33597b8526c59f49c7f
BLAKE2b-256 104beb0597855e7cd89c41e9214153fc03bb198f6e5689feba340b4e94963681

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.1-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-batch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_batch-0.11.1-cp310-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.1-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 274097c4d0c1991f8ecec8af237526bc04543d232ad545d1f47b7090dc41d74f
MD5 4b4066b958f9044b89fbd77c4b16b3fb
BLAKE2b-256 2d557f7857ca78c1ec6b50bfac0209e95060c5986a01886f1ec6b5f4a8bdd88a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.1-cp310-abi3-macosx_10_9_x86_64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-batch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page