poshtree
Parse PowerShell into a syntax tree, walk it, and turn it back into source.
poshtree is a small PowerShell front-end for Rust. Under the hood it is a
lexer feeding a recursive-descent parser, with an unparser for going the other
way. It handles messy and deliberately obfuscated input without panicking,
which makes it a useful base for formatters, linters, refactoring tools, or an
editor integration that needs to know the shape of a script. The only
dependency is regex.
Install
[]
= "0.1"
Or point at a local checkout:
[]
= { = "../poshtree" }
Parse and walk
parse returns the parsed script block together with any recoverable errors.
A malformed construct becomes an error node instead of aborting the parse, so
you always get a tree back.
use ;
let = parse;
assert!;
let root = ScriptBlock;
let mut count = 0;
root.walk;
println!;
Unparse (tree back to source)
let source = unparse_source;
assert!;
The result is meant to re-parse cleanly rather than match the original byte for byte. Whitespace and comments are not preserved.
Tokenize only
If you just want the token stream, skip the parser:
use tokenize;
let tokens = tokenize;
println!;
What is in the box
The common items are re-exported at the crate root: parse, parse_tokens,
tokenize, AstNode, NodeInfo, ScriptBlock, Token, unparse,
unparse_source, and dump_ast_to_ps1. Everything else lives under the v1
module (v1::ast, v1::lexer, v1::parser, v1::tokens).
Versioning
The tree types live under a v1 module. If the tree ever needs an
incompatible redesign, it will arrive as a v2 module and v1 will keep
working, so pinning to v1 is safe.
License
MIT. See LICENSE.