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)", escaped quotes "", # in interpolated strings, 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 ??/??=, logical (-and/-or/-xor), bitwise (-band/-bor/-bxor), 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 double-backtick arity List1 ``
  • Variables$var, $scope:var, ${braced} with backtick escapes, @splatted, and special vars $$ $^ $? $_
  • Unicode-aware source — Unicode identifiers, variables, parameters, type/class/enum names, and decimal digits
  • 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.38.0.tar.gz (465.1 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.38.0-cp310-abi3-win_arm64.whl (215.7 kB view details)

Uploaded CPython 3.10+Windows ARM64

tree_sitter_pwsh-0.38.0-cp310-abi3-win_amd64.whl (228.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_pwsh-0.38.0-cp310-abi3-musllinux_1_2_x86_64.whl (246.7 kB view details)

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

tree_sitter_pwsh-0.38.0-cp310-abi3-musllinux_1_2_aarch64.whl (236.7 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tree_sitter_pwsh-0.38.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (237.4 kB view details)

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

tree_sitter_pwsh-0.38.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (247.7 kB view details)

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

tree_sitter_pwsh-0.38.0-cp310-abi3-macosx_11_0_arm64.whl (222.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_pwsh-0.38.0-cp310-abi3-macosx_10_9_x86_64.whl (213.7 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pwsh-0.38.0.tar.gz
  • Upload date:
  • Size: 465.1 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.38.0.tar.gz
Algorithm Hash digest
SHA256 65e42dea93c6ddf82b3a463b5e771f77e36deb9abf01a5a3c147489a486bd709
MD5 7fb9adf16cc8414855607af9f5cfbdac
BLAKE2b-256 4e05f2cef1aecd94408e05763784d6e4e6b2a21dce69ee7cd743d2f5bad99732

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.38.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 fee07643af92915ee9549df0174cd0ba0c1044e8e9fa144585c21a2f8813cf07
MD5 59601c459492da90b2c974b1e0bc5105
BLAKE2b-256 8c7bf07c37bb617146cbd7c333ed2ed3c77bfee976c26d3f1fc416491798f619

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.38.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 53f6c3cf07af2b081aa88b10abfefadaad0e2d9f3738d8479be665dc19ac8028
MD5 ebec062341bb8b8529182454af7e78dd
BLAKE2b-256 de2474dbdb6c7e96b6cae5d6fc8b1c3dd55cde9db8f987d72e202cb01b316b35

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.38.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5a913860ffae1f1295151e56f45421dcb57d8875c202ec24e2bd4bea928b3f18
MD5 a136913944b05172320bacf62b2ecc37
BLAKE2b-256 66c10535b0c3bc34d325037d797d760c59066db9712c8d55b4b5fe80428a1621

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.38.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 320fea7b4d109f8d09b2f83e4cb316dc439099ae144879529f3e47a15ef7c62f
MD5 f27ab1561b1e84ae5eaf1c4f1dfd0d5c
BLAKE2b-256 ccbb56a282539b3ef2cc7ab858155a8afa7dff28ce3da22e6e3e16475deb0885

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.38.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 0e088de365d87dfccc056bbbef6c939fd633f9fa0ce118cd4c122b3317673258
MD5 89e3521dc07434c2cd3e56077bdeb251
BLAKE2b-256 500fb8f6c17f9377b5a219d194f85cf6afe9a93b3a0234b7f7df9c46b14129ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.38.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.38.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.38.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 6f5acea33c5d2050d8da40bc46247945b1b2704476ecd5d69fabfc8a21b99c5d
MD5 c9d722b0917c8958c627fe8d7556e3ae
BLAKE2b-256 ac69b423295ec46b14590904a802f16f7ee3ed66c3369e98a2742e6488ddd0e5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.38.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 229937d5cf2c1b4551b12d51d31a770ad6ee1fb91779684d9ec8d890a6bd528b
MD5 1f95ad17e87bb1d329024e94c7d9b28f
BLAKE2b-256 257281db74a9607ae46e9dca63e217cd1048cdb583f4777e801935f0376b7a56

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.38.0-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0c293f9a44c390cb3f06d8fd5151dc9c1574ccd9c3c11e16f3cf9377c583ea05
MD5 a8dead90e568cd52614ac90daa315dd8
BLAKE2b-256 c7d7d88db985edcccaf226113e4b265115626657f27adb893a62cf96dcee0c14

See more details on using hashes here.

Provenance

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