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.30.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.30.0-cp310-abi3-win_arm64.whl (143.0 kB view details)

Uploaded CPython 3.10+Windows ARM64

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

Uploaded CPython 3.10+Windows x86-64

tree_sitter_pwsh-0.30.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.30.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.30.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.30.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.30.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.30.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.30.0.tar.gz.

File metadata

  • Download URL: tree_sitter_pwsh-0.30.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.30.0.tar.gz
Algorithm Hash digest
SHA256 d87bfecaa4deaf1fb220752fa8d5698bf9bbbe9c149150355673103793f6f36f
MD5 02d7c02d2a1f8e3238257d775aa495f7
BLAKE2b-256 02de3f5ebbdcebe868546f48fb846d97361895dc5a6d9fa9def434e4e6401723

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.30.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 762e4364c798fe9f516154fd76d36439b444b1266eb9afe5b331e3b36a66d7a7
MD5 7e80b24bf9bf5f1586cdf397bd532183
BLAKE2b-256 fe58f4485ed5e2f6d88e9b013ec7610bd888080317d89cf281a206f5ce2b651c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.30.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 75a7fcdf2c6a38f09e18409122afb543d2cf09635706002aea5b6e53bae6c745
MD5 fbf7228172ef6d8cdd6af3ce14d60347
BLAKE2b-256 da8b112e7bfdbb230dd01ce88e26816e637805a5b6c1f0c7110ce400ccc15011

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.30.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c02fc4a98acdc9afb89743e45e7da4a5ce7b355042896aa58da84424480cba91
MD5 4d78122cb0ad70d589a02e74b38b8fab
BLAKE2b-256 7bcc1abe40493c8d4a4b7bd6f479d47513a9ab8ebfb5e296be78c1e5334d14bc

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.30.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 d71708bce0fbbe2b678b7156235e1bca0a39f9a51cabaf0613b29b4e2890e0b7
MD5 4f1f7b62251f4dce49fb57d2023a3d1e
BLAKE2b-256 30c2040369969ecb3667365d5eaa8831871fa24066ee06fe5496555fc23c4915

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.30.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a226737e73cc8d082c91150a4a409ce6a8bf582ee914a65985f3f245e6d4b9cd
MD5 6f8bd05054206fc301fb9f1b741bd7dc
BLAKE2b-256 3bf0e4e2096dc90d9bedc5661b5c7f4a57cfe08ec1fdfdb647c7525a553a4e34

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.30.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.30.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.30.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 e94be8358bd26c41196d789e9feb590598494f955dc0b449b26ae04ff4e48f5a
MD5 ef06415e546153e1559ca29b16ba7336
BLAKE2b-256 227c25a5efbfa4bb285041bfa75f0124fa154a3474b2d7acdf4d82355dac1099

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.30.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c0de1ba12e0f411cf07df61f7b828d53cead82aa92d7f75adde9aaff45929581
MD5 cde35fbfc447e5eba4e9800524624aec
BLAKE2b-256 43833041ba356b6ef72eba98beb9c2d9d12664e00c63a0cdc0f3c0488857e3ed

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.30.0-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6f2681310429d7d1dcd5932d48f778ced9d429e08040f8b3dd2dacdc646d5e1b
MD5 f0bbbb62c2714b7832dfdea774dbe740
BLAKE2b-256 aa81e6b966be3b6520acc846a63aec162b49bc88b62bb75dafca290241b594cd

See more details on using hashes here.

Provenance

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