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 && ||, PowerShell 7 line-initial pipe continuation (\n| Cmd), redirections > >> 2>&1, invocation & ., splatting @params, barewords (including expandable $var.suffix forms), verbatim arguments --%
  • Directivesusing namespace, using module (type names, relative bareword paths like .\Foo.psm1, and hashtable specs), using assembly, using static, and top-of-file #Requires
  • Strings and interpolation — expandable strings "$var $(expr)", verbatim strings, here-strings @" / @', sub-expressions $(...), array sub-expressions @(...), hashtable literals @{}, 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
  • Workflow and data blocksdata sections, inlinescript, parallel, and sequence statement blocks
  • 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 — attribute-decorated declarations ([Flags()] enum, [Attr()] class), properties, methods, constructors with : base()/: this() chaining, hidden/static modifiers, and inheritance clauses with generic bases (: IComparer[Object])
  • 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.32.0.tar.gz (323.8 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.32.0-cp310-abi3-win_arm64.whl (147.0 kB view details)

Uploaded CPython 3.10+Windows ARM64

tree_sitter_pwsh-0.32.0-cp310-abi3-win_amd64.whl (154.3 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_pwsh-0.32.0-cp310-abi3-musllinux_1_2_x86_64.whl (171.6 kB view details)

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

tree_sitter_pwsh-0.32.0-cp310-abi3-musllinux_1_2_aarch64.whl (167.4 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tree_sitter_pwsh-0.32.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (168.2 kB view details)

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

tree_sitter_pwsh-0.32.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (173.5 kB view details)

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

tree_sitter_pwsh-0.32.0-cp310-abi3-macosx_11_0_arm64.whl (158.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_pwsh-0.32.0-cp310-abi3-macosx_10_9_x86_64.whl (149.3 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pwsh-0.32.0.tar.gz
  • Upload date:
  • Size: 323.8 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.32.0.tar.gz
Algorithm Hash digest
SHA256 1bcdda3e4346368af2c36637b1b7ff90edd4c49f22cba045b08a8f2df707f560
MD5 bbec69233587662a76284ff9179a926a
BLAKE2b-256 412355452c77291c9a27fc2cc9006e01d35b97a6bdb0a61fc00dbbc9aea5a0bf

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.32.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 2676d8fe5c0c88cf3aa1597a30a4ccc249d228dd1713e395f930c41635e955a2
MD5 ec36d717d273c967e0d16d44f048c55d
BLAKE2b-256 f0c4fb7fcb78277d461b8a6e82e55f5be11521ba65320a66865a37a81f92cd70

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.32.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 53c24c3bf4d4096ee6ca7070a1a6bc9c8dd9fae44cdea20cd3a54b87a77135b0
MD5 c63b105d572a9b910650bf9ef7a6e855
BLAKE2b-256 9a27af721d0e0c821a938d24a8cb19a89294998e7093ecab3bc92253d9fbbb75

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.32.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4b25f79a5acd8e19dd3149219f5e6dbb8e004ab9dee4c06a2b3d00b49579f8bc
MD5 05dc78caac78c25c1b377dac8d8f3ebb
BLAKE2b-256 e5545bfea9287965286535052a14728efefae702d6068650c8d235cb25df2956

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.32.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 75bbb398aaebb1bbd856a9d04ffdd5dded010ff153fe30b66064903378da411b
MD5 b7d4773077f7fceca670fdc83ed44792
BLAKE2b-256 08194a53bf60b31656a44b64c015caf7fc09f779880583c6ce3340b9500bf88d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.32.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 cde61798c2a1ba85d07c9f7e2b742e74bb87c4c44d6f9d34f9bc1e604abdb74c
MD5 46c7013048b5c444ef34074f0aa19e94
BLAKE2b-256 cbbc3b43318f2ac50cb5141bb7ae7ee112ac8ae91ff28645af7535ca89e89b4d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.32.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.32.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.32.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 3781543f9cff8efe7b0011cebe34ad639899db06c211e109abfeddd43b0c3ec0
MD5 cb9c91c9c4e073447dc0e706a9a48f51
BLAKE2b-256 4efdb43a973d7b09b76dfa387e83416a0c873dfe04236c3425f6d45f1696c049

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.32.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6275d8dd3545a914eb3033ce68f3d685a284bd1a4e0f3b1146e0b4316a877bf9
MD5 4bf80a8c74673714b9fce3ad27721c66
BLAKE2b-256 9574c8db28f9201b3c1396596884a9e3c47a6442dfb932e093e9235f442591a3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.32.0-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e5116d5adbdd82a946ac9a58dcb40d5e96e70532c602c07babb399f838ce62d7
MD5 d7580b7e8d3ae979feaffb87e825b724
BLAKE2b-256 8e666b87ca42f1465f89acd54e3cb3c9bb602c30219a574ec0c662ce94312904

See more details on using hashes here.

Provenance

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