tree_sitter_batch

package module
v0.11.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 3 Imported by: 0

README

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

Documentation

Index

Constants

View Source
const (
	NodeArgumentList               = "argument_list"
	NodeArgumentValue              = "argument_value"
	NodeArithmeticAssignment       = "arithmetic_assignment"
	NodeArithmeticExpression       = "arithmetic_expression"
	NodeAssignmentLiteral          = "assignment_literal"
	NodeAssignmentParenGroup       = "assignment_paren_group"
	NodeAssignmentValue            = "assignment_value"
	NodeBracketedLiteral           = "bracketed_literal"
	NodeBracketedValue             = "bracketed_value"
	NodeCallStmt                   = "call_stmt"
	NodeCaretQuotedAssignmentValue = "caret_quoted_assignment_value"
	NodeCmd                        = "cmd"
	NodeCommandName                = "command_name"
	NodeCommandOption              = "command_option"
	NodeCommandSep                 = "command_sep"
	NodeComment                    = "comment"
	NodeComparisonOp               = "comparison_op"
	NodeCondExec                   = "cond_exec"
	NodeEchoOff                    = "echo_off"
	NodeElseClause                 = "else_clause"
	NodeEndlocalStmt               = "endlocal_stmt"
	NodeExitStmt                   = "exit_stmt"
	NodeFdRedirect                 = "fd_redirect"
	NodeFdRedirectOp               = "fd_redirect_op"
	NodeForOptions                 = "for_options"
	NodeForSet                     = "for_set"
	NodeForSetGroup                = "for_set_group"
	NodeForSetLiteral              = "for_set_literal"
	NodeForStmt                    = "for_stmt"
	NodeForVariable                = "for_variable"
	NodeGotoStmt                   = "goto_stmt"
	NodeIfOption                   = "if_option"
	NodeIfStmt                     = "if_stmt"
	NodeInteger                    = "integer"
	NodeLabel                      = "label"
	NodeMacroInvocation            = "macro_invocation"
	NodeParenExpression            = "paren_expression"
	NodeParenthesized              = "parenthesized"
	NodePipeStmt                   = "pipe_stmt"
	NodeProgram                    = "program"
	NodePromptAssignment           = "prompt_assignment"
	NodeQuotedAssignmentValue      = "quoted_assignment_value"
	NodeRedirectOp                 = "redirect_op"
	NodeRedirectStmt               = "redirect_stmt"
	NodeRedirectTarget             = "redirect_target"
	NodeRedirection                = "redirection"
	NodeSetKeyword                 = "set_keyword"
	NodeSetOption                  = "set_option"
	NodeSetlocalStmt               = "setlocal_stmt"
	NodeString                     = "string"
	NodeVariableAssignment         = "variable_assignment"
	NodeVariableName               = "variable_name"
	NodeVariableReference          = "variable_reference"
)

Node kind constants for the batch grammar.

Variables

View Source
var HighlightsQuery string

Functions

func GetHighlightsQuery

func GetHighlightsQuery() (*sitter.Query, *sitter.QueryError)

GetHighlightsQuery compiles and returns the bundled highlights query.

func GetLanguage

func GetLanguage() *sitter.Language

GetLanguage returns the tree-sitter Language for Batch.

Types

This section is empty.

Directories

Path Synopsis
bindings
go
go-compat
smoke command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL