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.31.0.tar.gz (319.4 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.31.0-cp310-abi3-win_arm64.whl (143.0 kB view details)

Uploaded CPython 3.10+Windows ARM64

tree_sitter_pwsh-0.31.0-cp310-abi3-win_amd64.whl (149.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_pwsh-0.31.0-cp310-abi3-musllinux_1_2_x86_64.whl (165.5 kB view details)

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

tree_sitter_pwsh-0.31.0-cp310-abi3-musllinux_1_2_aarch64.whl (161.5 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tree_sitter_pwsh-0.31.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (162.1 kB view details)

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

tree_sitter_pwsh-0.31.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (167.4 kB view details)

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

tree_sitter_pwsh-0.31.0-cp310-abi3-macosx_11_0_arm64.whl (153.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_pwsh-0.31.0-cp310-abi3-macosx_10_9_x86_64.whl (143.3 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pwsh-0.31.0.tar.gz
  • Upload date:
  • Size: 319.4 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.31.0.tar.gz
Algorithm Hash digest
SHA256 fb307a16199a050195273032b4bfd957dc8c77d4a59a28a5a4af7c52e882f9bd
MD5 b7c754ed5680ab666916d394836508cc
BLAKE2b-256 0e7f55e337f332c9a27e6092d43ae4c0bd0f7eff28513d7a4c1677db446feec1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.31.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.31.0-cp310-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.31.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 080c277c01eef3f9abb130038d2baa52d9c6c0c15d9c2789e0b33f01da3f3717
MD5 8d14c4dca1925cbddf1acab6fc6d2dee
BLAKE2b-256 263a3f69d02a1828645ddfa46aba3d396f7ae4a4e62819a04aafddcbfe20b6a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.31.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.31.0-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.31.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 12bae7158f349ff206f03ddba75fb56ea74181ae068e5648580e6d731236ff1a
MD5 3ea34b6053e89bce54ab41642aa5d607
BLAKE2b-256 9f39559e5d531b11f5f674d7204d4f897c5a7ad0cb783c8289443bad4b70e6d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.31.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.31.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.31.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 53981f74a253ecaa4bc4d998700f1658a4757e1dae831644d039e96c1b84e087
MD5 d9f8656309edbb84736646322d489fe0
BLAKE2b-256 06ef52c6c560193f497a1842cdca5433f3a66f6fa445875573ddf8a566ac5bb3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.31.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.31.0-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.31.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 3403ef596b7953ab74ebbfc43fb43ee380252442c4bef0245a911596ad6438ac
MD5 579c3747b5c52b3cb857fe928d992298
BLAKE2b-256 7913f518ba3b917083675806ef9f3456c94a788397a29aa6dc0c6995ea2a9e3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.31.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.31.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.31.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 20c86b09841affd95ba967ab1f349ed48b86d7d3aaab9e3384e0e9393d95d8a5
MD5 f0f7ad9df284df189f9ed92b6fe20691
BLAKE2b-256 794991606e9a3d295717b20e9ed83cfe9a99cc49e21b3eefa680c06172fbc1fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.31.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.31.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.31.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 f1c3fdd307147153bac3152d900492639a70eb6c5bc63a80e89149da4ea68f5c
MD5 c84bbe79ab1301e7e3755202377480d6
BLAKE2b-256 05eec5b4f07e4752da66c94a15a9a351e56d50c1b6107f3d6ad0a0515817e196

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.31.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.31.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.31.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ace12abcf3af4352b25d89fb454d33aae74ebaa2e9a8976264c65607137ca3ff
MD5 637698e93d6cb07e32613c3223aade47
BLAKE2b-256 f867b2ab2dd431657b97dec948700a7a83dbbb2e7eb43d562ab2b3dda57efb51

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.31.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.31.0-cp310-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.31.0-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6924cdfaf3223720ade8ed90a16cdac68d343a5928d2a51ffd3eceb387ed3041
MD5 de3310412c91619436f1a28c07768f4b
BLAKE2b-256 9f72d14ac2e1f48d87b993b5822e57b948295892bb638849778561058569a618

See more details on using hashes here.

Provenance

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