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.34.2.tar.gz (336.0 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.34.2-cp310-abi3-win_arm64.whl (160.9 kB view details)

Uploaded CPython 3.10+Windows ARM64

tree_sitter_pwsh-0.34.2-cp310-abi3-win_amd64.whl (173.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_pwsh-0.34.2-cp310-abi3-musllinux_1_2_x86_64.whl (185.4 kB view details)

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

tree_sitter_pwsh-0.34.2-cp310-abi3-musllinux_1_2_aarch64.whl (175.8 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tree_sitter_pwsh-0.34.2-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (176.7 kB view details)

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

tree_sitter_pwsh-0.34.2-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (186.4 kB view details)

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

tree_sitter_pwsh-0.34.2-cp310-abi3-macosx_11_0_arm64.whl (165.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_pwsh-0.34.2-cp310-abi3-macosx_10_9_x86_64.whl (161.5 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pwsh-0.34.2.tar.gz
  • Upload date:
  • Size: 336.0 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.34.2.tar.gz
Algorithm Hash digest
SHA256 257fa2a912ea698c9f08f924613415b6bb9824f10e9d27aa3d7204cecedc1708
MD5 d2e4b85fc2d0835334d2234583c81765
BLAKE2b-256 4a2df8d1c5335f9088f196ea9a4696855a4c893a27f610e2c88b296d690c024b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.34.2-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 ecfbe28a43592cbe2d0ee80ea9ac9fb108d5708ecd3a2d8ac1d07ba2617be81e
MD5 e9c270b919b0901d99b4280424b99ecc
BLAKE2b-256 aae190b66ad53f0aa3ea897a1d1ee85f740a8820eded74a08d658e6dd6f26c89

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.34.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 44160ae45e485c894593a13ef7e734a1791e1adbd116b855d9df3fd124f84e58
MD5 c4eed6201fc7f8581dc3b1149f1d8b02
BLAKE2b-256 f81e68268f25a67b498d53b59b9bfb36f28eb6e841742da18e4995c0a45deb52

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.34.2-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7793f2b145e0e1bf8e9b17d6ab9c8d2fd695bf5f2e99d076b06c65d9ccdcd2ad
MD5 28a43f6a13b50cedd517558a7d3b3b17
BLAKE2b-256 8af55a8b8885073d2cc3d2bc77e7b391943f02e95b2bfb0b1e7b357df25fbddb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.34.2-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 859f251a11d4dd75c71351b3c148b717eec5195416aafaed2a306b69eddab2e4
MD5 52f112f291df2d91a135b81600e096bc
BLAKE2b-256 0996fe38ab1f2614325192e0b2ecd7e2b084acf91de7be0a719929a04bc98898

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.34.2-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 cd93df6ad3af2f85a9f5ecbe7bb51a1a1f943811123c8519f9fca3e479d964d7
MD5 dfc9fc5389f1aea2efad9d039614456e
BLAKE2b-256 1f88452730eb91c507a7fc865f012605ff5c61ec6a9b4e518b2267adf212b616

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.34.2-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.34.2-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.34.2-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 6cddb0fb697266a84277fd6141e57b2687d3c9c83ba3c9de72cc32f2d53c14ea
MD5 adc8e7d2bdfde28eac3c06ffcd52bdc8
BLAKE2b-256 4224a84792e7293c6ec4e750ba3b3fa7dbf41e56b7660237ab70dee0adf82e4e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.34.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d293987b923915c60ccceca046e4943f7dfeefbe2260a810bbb47815bd9856ae
MD5 c36e4e414bec0b5e34e1bb876031484c
BLAKE2b-256 117f44e21a6b75f55017a162945e44180cdba87f5a798374b1a5ad5da9f47ce7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.34.2-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b4ea34baf3fd7cb284e2b4ae90908bf0a4a2f631ef2b461472706c42827a8194
MD5 7d53b5f7740283d754e619ad1276ffd1
BLAKE2b-256 a9b0514a9053a0696b9b1e2bad0de8d3ee07c6942fceee38376bf19df29024a4

See more details on using hashes here.

Provenance

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