Skip to main content

PowerShell grammar for tree-sitter

Project description

tree-sitter-pwsh

PowerShell grammar for tree-sitter.

Parses .ps1 and .psm1 files into a concrete syntax tree for syntax highlighting, code navigation, and analysis.

Features

  • Pipelines and commands — pipes |, chain operators && ||, redirections > >> 2>&1, invocation & ., splatting @params
  • Directivesusing 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 blocksfunction, filter, workflow with param() blocks, attributes, validation, and begin/process/end/clean
  • Control flowif/elseif/else, switch (-Regex, -Wildcard, -Exact, -CaseSensitive), foreach, for, while, do/while/until
  • Error handlingtry/catch/finally with typed catch clauses, trap, throw, break, continue, return, exit
  • Expressions — ternary ? :, null-coalescing ??/??=, comparison/string/type/containment operators, -f format, range ..
  • Types — common .NET type forms including generics [Dictionary[string, int]], arrays [int[]], nested types Array+Enumerator, and backtick arity Dictionary`2
  • Variables$var, $scope:var, ${braced} with backtick escapes, @splatted, and special vars $$ $^ $? $_
  • Classes and enums — properties, methods, constructors with : base()/: this() chaining, hidden/static attributes, and inheritance clauses
  • Numbers — decimal, hex 0x, scientific 1.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

Example

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 $message

Parsed 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 ...))))))

Installation

npm

npm install tree-sitter-pwsh

Cargo

cargo add tree-sitter-pwsh

PyPI

pip install tree-sitter-pwsh

Go

import 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()

Usage

Node.js

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());

Rust

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());

Python

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())

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_pwsh-0.29.0.tar.gz (310.2 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_pwsh-0.29.0-cp310-abi3-win_arm64.whl (137.7 kB view details)

Uploaded CPython 3.10+Windows ARM64

tree_sitter_pwsh-0.29.0-cp310-abi3-win_amd64.whl (143.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_pwsh-0.29.0-cp310-abi3-musllinux_1_2_x86_64.whl (159.5 kB view details)

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

tree_sitter_pwsh-0.29.0-cp310-abi3-musllinux_1_2_aarch64.whl (156.5 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tree_sitter_pwsh-0.29.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (157.0 kB view details)

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

tree_sitter_pwsh-0.29.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (161.1 kB view details)

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

tree_sitter_pwsh-0.29.0-cp310-abi3-macosx_11_0_arm64.whl (148.8 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_pwsh-0.29.0-cp310-abi3-macosx_10_9_x86_64.whl (137.7 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

Details for the file tree_sitter_pwsh-0.29.0.tar.gz.

File metadata

  • Download URL: tree_sitter_pwsh-0.29.0.tar.gz
  • Upload date:
  • Size: 310.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tree_sitter_pwsh-0.29.0.tar.gz
Algorithm Hash digest
SHA256 b7effd54fe52e7876df11ab0cac45214c3e1df0ed8c5af9611d4b76770e42663
MD5 fb1369ef45e1d821ff9eb5057032853f
BLAKE2b-256 24e23a1188f6cb63caf3fea3b078be2deee0c226610672e5a2a6ed5c109a2f6c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.29.0.tar.gz:

Publisher: publish_pypi.yml on wharflab/tree-sitter-powershell

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_pwsh-0.29.0-cp310-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.29.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 8cee3d148e11d415f194d3b312495eee173607705cb58b8b3dd2bfc005c24460
MD5 e4c275b83e03ff531b0c4fc52a68a67b
BLAKE2b-256 3052387e0683f00a93842fa2f9f6af8655f0f9dbbcf4452c44a566d09f49c436

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.29.0-cp310-abi3-win_arm64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-powershell

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_pwsh-0.29.0-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.29.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 be86d9cbcd03ce613857608a15bc05843949af028e9fecdbfb56546790d66f9e
MD5 d26a4c6248ad947361dc8bc065debd95
BLAKE2b-256 28c796de6b44a9f343145ca648a44742a4a6d4a1deed9d2f5f9fe229af7aaf8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.29.0-cp310-abi3-win_amd64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-powershell

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_pwsh-0.29.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.29.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2557a62e5d4153206efe137a8eb4a20b16eda70ba6359f3a5efb0a7bccd6237e
MD5 4114817dd665a45419ea69e5adace143
BLAKE2b-256 655143797a10d9f8bc751fad117a1d3f632f42477beb36f30c92f7967c50f1c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.29.0-cp310-abi3-musllinux_1_2_x86_64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-powershell

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_pwsh-0.29.0-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.29.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 14fe142a9920f3b6319109a013b34f8b69dc5c1f4fa3461349c9f138d1099141
MD5 7002792c39f03fde7708a64a0e7ea952
BLAKE2b-256 07caa4ae00bd9afe4a4582309f3fe710824667717646dbead757e525c7baf42a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.29.0-cp310-abi3-musllinux_1_2_aarch64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-powershell

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_pwsh-0.29.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.29.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c87a654fd8a7d257c6cabfbe07894a2d04a401ef5072c509a11e2459d4b55181
MD5 64bf09f8b3c30d942eb0f470a1bd55f7
BLAKE2b-256 3d87367017a557416515404f352ef7cb0ce19bc0d36efbf42d3acd39ea609ac2

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.29.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-powershell

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_pwsh-0.29.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_pwsh-0.29.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 89bb7b6575d33711ca3dfe949d885894daa96809480aa4a2bb1c2f63e9df0ee5
MD5 14626d6ffcfc1bb379e2d8403dfead7f
BLAKE2b-256 23d6e308b113e03a136562ddc29b3e945ce0a62e7184d63db8bbcea93a7a025a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.29.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-powershell

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_pwsh-0.29.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.29.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ba6ac2ce230584a81788118f657892e198d2c3d135f1bafb22b01d9bd56a113e
MD5 8cca762e023dccabac213cd44d960f6a
BLAKE2b-256 89cd9fe856c8e17c0f5942abd93898d55aea01b8bcac6c1a678e7f85ccc65095

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.29.0-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-powershell

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_pwsh-0.29.0-cp310-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.29.0-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5ed79f97c0fadb69ae020e4e8ae26bac1b74688bcfb87ae0ec231572c6360d82
MD5 187a2302b4e932ef8e1c77a51114e00c
BLAKE2b-256 7a3da45b0e30f503af72c5ca1a4bcc126921a9b2f3a5390f749ce8bff46b5e74

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.29.0-cp310-abi3-macosx_10_9_x86_64.whl:

Publisher: publish_pypi.yml on wharflab/tree-sitter-powershell

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