Skip to content

Latest commit

 

History

63 Commits

Folders and files

Repository files navigation

tree-sitter-powershell

Goal

  • A grammar compatible with PowerShell Core language features.
  • Follow tree-sitter's convention where applicable.

Non-goal

  • Support legacy language features, such as workflow.
  • Strictly follow official grammar specification(which is unmaintained and not always correct).

What are opinionated

  • Everything is expression: anything can evaluate to a assignable value is an "expression", including flow control statement(e.g. if, for statement etc.)
  • Parsing type identifiers: identifiers in type literals like [Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089], [System.String, mscorlib]]] are not part of the language but specific to .NET, they are still parsed structurally.

Known limitations

  • LHS list of elements in the assignment expression are not limited to location values, that means 1,2 = 1,2 also parses.

  • Parenthesized location value list not supported, e.g. ((($foo), ($bar)), $baz) = ....

  • Contextual null-coalescing expression and ternary expression not always correct.

    1??2 # command, good
    1 ?? 2 # binary_expression, good
    1?2:3 # command, good
    1 ?2:3 # conditional_expression, good
    
    $foo = 1??2 # rhs is expected to be command but parsed as binary_expression
    $foo = 1?2:3 # rhs is expected to be command but parsed as conditional_expression
  • using ... declared in non-top-level positions are parsed as command while it's expected to be an error.

    using namespace System.Text # top-level, fine
    
    if (0) {
        # should be error but parsed as command
        using namespace System.Text
    }

Installation

Neovim

With nvim-treesitter:

vim.api.nvim_create_autocmd('User', {
  pattern = 'TSUpdate',
  callback = function()
    require('nvim-treesitter.parsers').powershell = {
      install_info = {
        url = 'https://github.com/sharpchen/tree-sitter-powershell',
        generate = true,
        queries = 'queries/nvim-treesitter',
      },
    }
  end,
})

Then you might need to run :TSInstall powershell.

Acknowledgements

References

Releases

Packages

Contributors

Languages