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

  • Control flowif/elseif/else, switch (-Regex, -Wildcard, -Exact, -CaseSensitive), foreach, for, while, do/while/until
  • Functionsfunction, filter, workflow with param() blocks, attributes, validation, named blocks (begin/process/end/clean)
  • Classes & enums — properties, methods, constructors with : base()/: this() chaining, hidden/static attributes, inheritance
  • Error handlingtry/catch/finally with typed catch clauses, trap, throw, break, continue, return, exit
  • Pipelines — pipes |, chain operators && ||, redirections > >> 2>&1, invocation & ., splatting @params
  • Expressions — ternary ? :, null-coalescing ??/??=, comparison/string/type/containment operators, -f format, range ..
  • Types — full .NET type system: generics [Dictionary[string, int]], arrays [int[]], nested types Array+Enumerator, backtick-arity Dictionary`2
  • Strings — expandable "$var $(expr)", verbatim 'literal', here-strings @" / @', escape sequences
  • Numbers — decimal, hex 0x, scientific 1.5e10, all suffixes (u, ul, s, us, y, uy, n, l, d), multipliers (kb/mb/gb/tb/pb)
  • using directivesusing namespace, using module (with hashtable specs), using assembly, using static
  • Variables$var, $scope:var, ${braced} (with backtick escapes), @splatted, special vars $$ $^ $? $_
  • Case-insensitive — all keywords and operators match regardless of casing

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.28.0.tar.gz (249.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.28.0-cp310-abi3-win_arm64.whl (121.8 kB view details)

Uploaded CPython 3.10+Windows ARM64

tree_sitter_pwsh-0.28.0-cp310-abi3-win_amd64.whl (127.7 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_pwsh-0.28.0-cp310-abi3-musllinux_1_2_x86_64.whl (143.4 kB view details)

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

tree_sitter_pwsh-0.28.0-cp310-abi3-musllinux_1_2_aarch64.whl (139.9 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tree_sitter_pwsh-0.28.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (140.7 kB view details)

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

tree_sitter_pwsh-0.28.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (144.6 kB view details)

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

tree_sitter_pwsh-0.28.0-cp310-abi3-macosx_11_0_arm64.whl (130.8 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_pwsh-0.28.0-cp310-abi3-macosx_10_9_x86_64.whl (122.2 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_pwsh-0.28.0.tar.gz
  • Upload date:
  • Size: 249.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.28.0.tar.gz
Algorithm Hash digest
SHA256 8ea9580dbfb471daa64789cf5ea7c92510dd784e7d63308dac6c82dd035ffa6d
MD5 2c037c75c15d68c3931d388ce665ad49
BLAKE2b-256 8effcdcc80c46338f6b9bd140babdbfa9cdd2b6d6e06215f7a98a9626d4e3fac

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.28.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 6018c75e0a463e34ef482c2d122282c95630380c52d08e80ad696854b0a45d0d
MD5 3a1fc565210ef2de5965128729a9b966
BLAKE2b-256 552bf437705b8dc72143e08ac7254a84047e1928f450c84beef29daad9522e47

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.28.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2e54fd148e8c08706c80f2df03fd305e106990a90f7671df0afc01a5049457ab
MD5 a1f3274d9a6ad3e0812189598b5d164d
BLAKE2b-256 fc141977fb8541f99362c6200f29ec7be3832b8fe741531a655c4ba89f005047

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.28.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c5057a3bae903b7133c2f1fa29507f124ff04cb518f75e877a97b74d893560ad
MD5 8cbfd5ceb6bdc8e7c026f741bff9bb8d
BLAKE2b-256 675805a74bdd7b7932ee37090156ebeeb5978fbc5d575be4af2049a2f5e49feb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.28.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 2eb542b64424286a7d8aaaa2026ddb20625912ff05a7bd692efce595ac92e44a
MD5 05473bff27b14e3417e596095f9b96e3
BLAKE2b-256 bee0faf8d2376b4cfac2c7fffd6be8916944778cd3596aefca0d0cac74488e61

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.28.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b5c4adcb5a0df4b9d918abe4f386bcc3a80b099bb992c16709001d7a4465ef7a
MD5 0e8a4933f2804dece9b7c246f7c75e0c
BLAKE2b-256 e1f6859caf7f3a8b01c69bc4f07bd4844c69ee66b630834d5b4f36b909bfc20c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_pwsh-0.28.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.28.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.28.0-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 592f69cdd5f666ca3483a33911ff585698a4eb6861d720dd674581db5cb17443
MD5 cc6a26af239d5dc3f6e23313331b12b7
BLAKE2b-256 0669fc20c46ef581a2d7b10e5bb61cbd7095f446f18d2d94af0967dca73bbdfe

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.28.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c84bb4f211a7b0efaaf12c8a33c27c3c9f2b1d13262dbd797a27cd1ec07e441f
MD5 63c28d07ecc92b5128228f145d464cc5
BLAKE2b-256 667f24ccbd15bd187a90cfac75d81b4fa64b82505ad344e4d6b574017d3f8efa

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_pwsh-0.28.0-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6cf0fa334e97582a4cc9c20cca4087cda848bc8b66178a60dbf535d902b2ef1c
MD5 c9ca17954bf5cce50c018f074ff97401
BLAKE2b-256 6ac2a68d5c7a0ab8348ce72dcf65981c38336ec5b5e64a0fd6a3ca4d06c5bdb0

See more details on using hashes here.

Provenance

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