A Tree-sitter grammar for the JASS programming language.
JASS (Just Another Scripting Syntax) is a scripting language used in Warcraft III for creating custom maps and modifications. This grammar provides full syntax analysis and incremental parsing capabilities for pure JASS code ( without vJASS extensions).
- 🌳 Complete syntax parsing for JASS code
- 🚀 Incremental parsing for fast change processing
- 🔍 Syntax highlighting support
- 📦 Available for Node.js and Rust
- 🎯 Accurate scope and structure detection
- ⚡ High-performance parsing with Tree-sitter
npm install tree-sitter-jass[dependencies]
tree-sitter-jass = "0.1.0"const Parser = require('tree-sitter');
const JASS = require('tree-sitter-jass');
const parser = new Parser();
parser.setLanguage(JASS);
const sourceCode = `
globals
integer myVar = 0
endglobals
function HelloWorld takes nothing returns nothing
call BJDebugMsg("Hello, World!")
endfunction
`;
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());use tree_sitter::Parser;
use tree_sitter_jass::language;
fn main() {
let mut parser = Parser::new();
parser.set_language(&language()).expect("Error loading JASS grammar");
let source_code = r#"
globals
integer myVar = 0
endglobals
"#;
let tree = parser.parse(source_code, None).unwrap();
println!("{}", tree.root_node().to_sexp());
}This section provides a complete description of the JASS programming language syntax and features.
JASS supports two types of comments:
// This is a single-line comment
JASS has several built-in primitive types:
integer- 32-bit signed integerreal- floating-point numberboolean- true/false valuestring- text stringcode- function referencehandle- base type for game objects
Handle types represent game objects and extend from the base handle type:
unit- game unitplayer- player referencelocation- map locationtrigger- event triggertimer- timer objectgroup- unit group- And many more...
Arrays in JASS are declared with the array keyword:
integer array myNumbers
unit array myUnits
Arrays are 1-dimensional and 0-indexed with a fixed size of 8192 elements.
integer count
real distance
boolean isAlive
string message
unit myUnit
local- declares a local variable inside a functionconstant- declares a constant (read-only) variablearray- declares an array variable
local integer i = 0
constant real PI = 3.14159
integer array myData
constant integer MAX_UNITS = 100
set count = 10
set distance = 150.5
set isAlive = true
set message = "Hello"
42 // Decimal
0x2A // Hexadecimal
0b101010 // Binary
1_000_000 // With separators
Integer literals can have suffixes:
lorL- long integeruorU- unsigned integerulorUL- unsigned long
3.14 // Decimal point
2.5e10 // Scientific notation
1.0f // Float suffix
.5 // Leading decimal point
true
false
"Hello, World!"
"Escape sequences: \n \t \\ \""
null // Null handle reference
nothing // Used in function signatures
a + b // Addition
a - b // Subtraction
a * b // Multiplication
a / b // Division
-a // Unary negation
+a // Unary plus
a == b // Equal
a != b // Not equal
a < b // Less than
a > b // Greater than
a <= b // Less than or equal
a >= b // Greater than or equal
a and b // Logical AND
a or b // Logical OR
not a // Logical NOT
set a = b // Simple assignment
a++ // Postfix increment
a-- // Postfix decrement
++a // Prefix increment
--a // Prefix decrement
array[index] // Array subscript
call FunctionName(arg1, arg2)
From highest to lowest precedence:
- Postfix:
++,--,.,[],() - Prefix:
++,--,not,-,+ - Multiplicative:
*,/ - Additive:
+,- - Relational:
<,>,<=,>= - Equality:
==,!= - Logical OR:
or - Logical AND:
and - Assignment:
= - Comma:
,
Important note: In JASS, or has higher precedence than and. This means:
// false and true or true evaluates as: false and (true or true) = false
// NOT as: (false and true) or true = true
if condition then
// code
endif
if condition then
// code
else
// code
endif
if condition1 then
// code
elseif condition2 then
// code
else
// code
endif
loop
// code
exitwhen condition
endloop
exitwhen condition // Exit loop when condition is true
return // Return from function
function FunctionName takes ParameterType paramName returns ReturnType
// function body
endfunction
function HelloWorld takes nothing returns nothing
call BJDebugMsg("Hello, World!")
endfunction
function Add takes integer a, integer b returns integer
return a + b
endfunction
Standard JASS supports only single return values.
call FunctionName(arg1, arg2)
set result = FunctionName(arg1, arg2)
Native functions are declared but not defined (implemented in game engine):
native FunctionName takes ParameterTypes returns ReturnType
Example:
native CreateUnit takes player id, integer unitid, real x, real y, real face returns unit
Global variables are declared in a globals block:
globals
integer count = 0
real PI = 3.14159
constant integer MAX_PLAYERS = 12
unit array playerUnits
boolean gameStarted = false
endglobals
Features of globals:
- Can be declared with or without initialization
- Support
constantmodifier for read-only values - Support
arraymodifier for array declarations - Accessible from any function in the script
JASS allows creating type aliases for existing types:
type mytype extends handle
type unitcode extends integer
Type extensions create new type names that inherit from base types. This is mainly used for type safety and code organization.
Here's a complete JASS script demonstrating various language features:
//============================================================================
// Complete JASS Example
//============================================================================
globals
// Constants
constant real PI = 3.14159
constant integer MAX_UNITS = 100
// Variables
integer unitCount = 0
unit array playerUnits
boolean gameStarted = false
endglobals
//============================================================================
// Native function declarations
//============================================================================
native CreateUnit takes player id, integer unitid, real x, real y, real face returns unit
native BJDebugMsg takes string msg returns nothing
//============================================================================
// Helper function to calculate distance
//============================================================================
function GetDistance takes real x1, real y1, real x2, real y2 returns real
local real dx = x2 - x1
local real dy = y2 - y1
return SquareRoot(dx * dx + dy * dy)
endfunction
//============================================================================
// Function with control flow
//============================================================================
function ProcessUnits takes nothing returns nothing
local integer i = 0
loop
exitwhen i >= unitCount
if playerUnits[i] != null then
// Process unit
call BJDebugMsg("Processing unit: " + I2S(i))
endif
set i = i + 1
endloop
endfunction
//============================================================================
// Type extensions
//============================================================================
type unitcode extends integer
type destructablecode extends integer
//============================================================================
// Function using custom types
//============================================================================
function CreateUnitAtLoc takes player whichPlayer, unitcode unitId, location loc returns unit
return CreateUnit(whichPlayer, unitId, GetLocationX(loc), GetLocationY(loc), 0.0)
endfunction
//============================================================================
// Main initialization
//============================================================================
function InitGame takes nothing returns nothing
set gameStarted = true
set unitCount = 0
call BJDebugMsg("Game initialized!")
endfunction
- Node.js >= 14
- Rust >= 1.70 (optional, for Rust bindings)
- tree-sitter-cli
npm installnpm run generate
# or
./generate.shnpm startThis opens an interactive playground for testing the grammar in your browser.
npm testtree-sitter-jass/
├── grammar.js # Grammar definition
├── src/
│ ├── parser.c # Generated parser (C)
│ ├── grammar.json # Intermediate grammar representation
│ └── node-types.json # AST node types
├── bindings/
│ ├── node/ # Node.js bindings
│ └── rust/ # Rust bindings
├── queries/ # Syntax highlighting queries
├── package.json # npm package metadata
└── Cargo.toml # Rust crate metadata
MIT
nazarpunk <[email protected]>