PowerShell grammar for tree-sitter.
Parses .ps1 and .psm1 files into a concrete syntax tree for syntax highlighting, code navigation, and analysis.
- Pipelines and commands — pipes
|, chain operators&&||, redirections>>>2>&1, invocation&., splatting@params - Directives —
using namespace,using module(including hashtable specs),using assembly,using static, and top-of-file#Requires - Strings and interpolation — expandable strings
"$var $(expr)", verbatim strings, here-strings@"/@', and PowerShell-style escaping - Functions and named blocks —
function,filter,workflowwithparam()blocks, attributes, validation, andbegin/process/end/clean - Control flow —
if/elseif/else,switch(-Regex,-Wildcard,-Exact,-CaseSensitive),foreach,for,while,do/while/until - Error handling —
try/catch/finallywith typed catch clauses,trap,throw,break,continue,return,exit - Expressions — ternary
? :, null-coalescing??/??=, comparison/string/type/containment operators,-fformat, range.. - Types — common .NET type forms including generics
[Dictionary[string, int]], arrays[int[]], nested typesArray+Enumerator, and backtick arityDictionary`2 - Variables —
$var,$scope:var,${braced}with backtick escapes,@splatted, and special vars$$$^$?$_ - Classes and enums — properties, methods, constructors with
: base()/: this()chaining,hidden/staticattributes, and inheritance clauses - Numbers — decimal, hex
0x, scientific1.5e10, numeric suffixes (u,ul,s,us,y,uy,n,l,d), and size multipliers (kb/mb/gb/tb/pb) - Case-insensitive keywords and operators — parses PowerShell casing variations without normalization
using namespace System.IO
function Get-FileSize {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Path
)
begin { $total = 0 }
process {
$size = (Get-Item $Path).Length
$total += $size
}
end { $total }
clean { Remove-Variable total }
}
$result = Get-FileSize -Path ".\README.md"
$message = $result -gt 1kb ? "Large file" : "Small file"
Write-Host $messageParsed tree:
(program
(using_directive_list
(using_statement (type_name (type_name (type_identifier)) (type_identifier))))
(statement_list
(function_statement
(function_name)
(script_block
(param_block
(attribute_list
(attribute (attribute_name (type_spec (type_name (type_identifier))))))
(parameter_list
(script_parameter
(attribute_list
(attribute (attribute_name (type_spec (type_name (type_identifier))))))
(variable))))
(script_block_body
(named_block_list
(named_block (block_name) (statement_block ...))
(named_block (block_name) (statement_block ...))
(named_block (block_name) (statement_block ...))
(named_block (block_name) (statement_block ...))))))
(pipeline
(assignment_expression ...))
(pipeline
(assignment_expression ...))
(pipeline
(pipeline_chain
(command (command_name) (command_elements ...))))))
npm install tree-sitter-pwshcargo add tree-sitter-pwshpip install tree-sitter-pwshimport tree_sitter_powershell "github.com/wharflab/tree-sitter-powershell/bindings/go"The root package also exports the bundled queries/highlights.scm via go:embed:
import powershell "github.com/wharflab/tree-sitter-powershell"
lang := powershell.GetLanguage()
query, _ := powershell.GetHighlightsQuery()import Parser from "tree-sitter";
import PowerShell from "tree-sitter-pwsh";
const parser = new Parser();
parser.setLanguage(PowerShell);
const tree = parser.parse(`Get-Process | Where-Object { $_.CPU -gt 10 }\n`);
console.log(tree.rootNode.toString());let mut parser = tree_sitter::Parser::new();
let language = tree_sitter_pwsh::LANGUAGE;
parser.set_language(&language.into()).unwrap();
let tree = parser.parse("Get-Process | Sort-Object CPU\n", None).unwrap();
println!("{}", tree.root_node().to_sexp());from tree_sitter import Language, Parser
import tree_sitter_pwsh
parser = Parser(Language(tree_sitter_pwsh.language()))
tree = parser.parse(b"Get-Process | Sort-Object CPU\n")
print(tree.root_node.sexp())The grammar ships with a queries/highlights.scm file for use in editors that support tree-sitter highlighting (Neovim, Helix, Zed, etc.).
- PowerShell Language Specification
- PowerShell/EditorSyntax — used as a real-world parsing benchmark
- PowerShell/tree-sitter-PowerShell — archived PowerShell grammar repo; provided a significant part of the corpus coverage and the
TheBigTestFile.ps1benchmark target - Originally forked from airbus-cert/tree-sitter-powershell