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.0.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.0-cp310-abi3-win_arm64.whl (44.5 kB view details)

Uploaded CPython 3.10+Windows ARM64

tree_sitter_batch-0.11.0-cp310-abi3-win_amd64.whl (46.6 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_batch-0.11.0-cp310-abi3-musllinux_1_2_x86_64.whl (54.9 kB view details)

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

tree_sitter_batch-0.11.0-cp310-abi3-musllinux_1_2_aarch64.whl (54.8 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tree_sitter_batch-0.11.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (55.3 kB view details)

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

tree_sitter_batch-0.11.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (55.5 kB view details)

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

tree_sitter_batch-0.11.0-cp310-abi3-macosx_11_0_arm64.whl (45.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_batch-0.11.0-cp310-abi3-macosx_10_9_x86_64.whl (43.3 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_batch-0.11.0.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.0.tar.gz
Algorithm Hash digest
SHA256 3bdbcc3eabde43446c340344adc8999bb940ed1ca122be55c1d27790be2fc3c7
MD5 2b52a188cc0b16ed425c6bf20e5593ad
BLAKE2b-256 c496d45d55b0a560c50b5ef5bd65015813af4ab0a644299851258e87450a8dbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.0.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.0-cp310-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 4f3fed99f82acbebd6d963bcc92026de73f91e0f624d3d6dfbd7e52057e0727f
MD5 ac3ab4fa59ed24f94d40faa09c920f9f
BLAKE2b-256 d6f48c29c9c8fb1264e5daabacf2811cbfdedd23e7f2f871db65ec34430388c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.0-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.0-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 07f9fe0fc1c09f83433286f3afe50fd4088debdedead4814eca0caf82dbf4f5c
MD5 2a322d273685ac0fc5f72d4da30ef87f
BLAKE2b-256 6a37348b3013551e4986ba5a097bb2293feaacbd8c11ad3c67eb1e936569760d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.0-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.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b313d60587fd561a36e5e36c7e506cb9f0c5ff1a93ca7ae6a74fe55ac22f33bf
MD5 3765b7c853fafb8d634e80b7c0f89ab2
BLAKE2b-256 d3a39b8b8d36e1639efd31499a1b23ebac653c38b1d24cc7c0f665d788679ba9

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.0-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.0-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 a0f5cd5cc2af4eeaf37f9efac68d1ff7f7694f010d663ac193b750e2679b9ecd
MD5 f65cded9208e16eb79ca36a8fa90842a
BLAKE2b-256 ac72314c9615946b1088f9957676a4d7978edbf30db86dcffb2956863888dc6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.0-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.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 cff4b16ce42941681b1b857f37a19939584cbc15b0666dd0df2c698abccc7ecf
MD5 c58cd9345d07203101b4f24d7ab97b0a
BLAKE2b-256 2dba43e5e1b16829d9f2ff9f05d72227af97e0035967ecd7efe5f090d9183e50

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.0-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.0-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.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 1c2887062889bc7ab7df83a8109cdc947c634232c1322241ac5f51f07d14af42
MD5 fa4eda9530b615672833d3c9b10b0d5b
BLAKE2b-256 f01e2f93e6dae8401e2570015c641b0ad66e16b0ebe88f25cd34039c83199b85

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.0-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.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 09f9908d7c08658bbb0c14a348cb5854e94fb983bb9e4733dabaa3261024fb7a
MD5 5d4e153a12c8aed29c61c8c907c32623
BLAKE2b-256 d2b4ac0472b8b1d9ecf33ecb8512fe6990c6668195f3c43513bd791ad56fee68

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.0-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.0-cp310-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_batch-0.11.0-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ce79a078c92697bee4e99b153b9321473e1d179c220492f28b3103577210d271
MD5 f16d46eb0c22b6b72444a1c381d04331
BLAKE2b-256 c05ca2f3640297f5ca05769fde4c9697e7a5cbb8c00ec56f0360e87fb5a646ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_batch-0.11.0-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