tree-sitter-powershell

raw JSON →
0.26.4 verified Sat May 09 auth: no python

A PowerShell grammar for tree-sitter, providing robust parsing of PowerShell scripts (including PowerShell 5.1 and PowerShell 7+). Current version 0.26.4, requires Python >=3.10. Released from https://github.com/airbus-cert/tree-sitter-powershell.

pip install tree-sitter-powershell
error TypeError: expected Language, got function
cause Passing the `language` function directly to `Language()` instead of calling it.
fix
Use Language(language()).
error ImportError: cannot import name 'Language' from 'tree_sitter_powershell'
cause The `Language` class was removed in v0.20+.
fix
Use from tree_sitter_powershell import language and then Language(language()).
gotcha The grammar object is a callable that returns a language pointer. Use `language()` (not just `language`) when passing to `Language()` constructor.
fix Use `Language(language())` instead of `Language(language)`.
breaking In version 0.20+, the import path changed: `from tree_sitter_powershell import language` replaces the old `from tree_sitter_powershell import Language` (where Language was a class).
fix Update imports to `from tree_sitter_powershell import language`.
deprecated The old `Language` class from tree-sitter-powershell <=0.19 is deprecated and may be removed.
fix Upgrade to >=0.20.0 and use the new `language()` function.

Parses a PowerShell snippet and prints the s-expression of the AST.

from tree_sitter import Language, Parser
from tree_sitter_powershell import language

PS_LANGUAGE = Language(language())
parser = Parser(PS_LANGUAGE)

tree = parser.parse(b"Write-Host 'Hello, World!'")
print(tree.root_node.sexp())