Documentation
¶
Overview ¶
Package parser turns shell input into a validated AST of pipeline segments.
Package parser provides a safe PowerShell dialect parser using participle.
The grammar defines a restricted subset of PowerShell that covers ~90% of diagnostic value while rejecting dangerous constructs (variables, script blocks, redirections, etc.). Security is enforced by the grammar itself: anything not in the grammar is rejected by the parser.
Index ¶
- Constants
- type PSAddExpr
- type PSAddTail
- type PSAndExpr
- type PSAndTail
- type PSArgument
- type PSAtom
- type PSCmpExpr
- type PSCmpTail
- type PSCommand
- type PSExprBlock
- type PSFlag
- type PSHashEntry
- type PSHashtable
- type PSIdentish
- type PSLiteral
- type PSMulExpr
- type PSMulTail
- type PSOrExpr
- type PSOrTail
- type PSPipeVar
- type PSPiped
- type PSPipeline
- type PSPositional
- type PSSafeExpr
- type PSStaticArgList
- type PSStaticCall
- type PSUnary
- type PSValue
- type ParseError
- type Pipeline
- type PipelineSegment
Constants ¶
const ( MaxCommandLength = 65536 // 64KB max total command length MaxPipeSegments = 32 // max pipeline segments (|, &&, ||) MaxArgsPerSegment = 1024 // max arguments per command segment )
Input size limits.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PSArgument ¶ added in v0.7.0
type PSArgument struct {
Flag *PSFlag `parser:" @@"`
Positional *PSPositional `parser:"| @@"`
}
type PSAtom ¶ added in v0.7.0
type PSAtom struct {
StaticCall *PSStaticCall `parser:" @@"`
PipeVar *PSPipeVar `parser:"| @@"`
EnvRef *string `parser:"| @EnvRef"`
Size *string `parser:"| @SizeLiteral"`
Number *string `parser:"| @Number"`
String *string `parser:"| @String"`
DQString *string `parser:"| @DQString"`
Paren *PSSafeExpr `parser:"| '(' @@ ')'"`
}
type PSCommand ¶ added in v0.7.0
type PSCommand struct {
Name string `parser:"@Ident ( '-' @Ident )?"`
Args []*PSArgument `parser:"@@*"`
}
type PSExprBlock ¶ added in v0.7.0
type PSExprBlock struct {
Expr *PSSafeExpr `parser:"LBrace @@ HashClose"`
}
PSExprBlock is a safely-scoped script block: only a constrained expression grammar is accepted inside. Used for calculated properties (@{E={...}}) and Where/Sort/Group-Object script-block arguments.
type PSHashEntry ¶ added in v0.7.0
type PSHashtable ¶ added in v0.7.0
type PSHashtable struct {
Entries []*PSHashEntry `parser:"HashOpen @@* HashClose"`
}
type PSIdentish ¶ added in v0.7.0
type PSIdentish struct {
Head string `parser:"@Ident ( '-' @Ident )?"`
}
type PSLiteral ¶ added in v0.7.0
type PSLiteral struct {
Hashtable *PSHashtable `parser:" @@"`
Block *PSExprBlock `parser:"| @@"`
String *string `parser:"| @String"`
DQString *string `parser:"| @DQString"`
EnvRef *string `parser:"| @EnvRef"`
Size *string `parser:"| @SizeLiteral"`
Number *string `parser:"| @Number"`
Ident *PSIdentish `parser:"| @@"`
}
type PSPipeVar ¶ added in v0.7.0
type PSPipeVar struct {
Ident string `parser:"Dollar @Ident"`
}
PSPipeVar matches `$_` optionally followed by dotted property accessors. The Ident pattern includes `.` in its inner char class, so `$_.Status.Foo` lexes as Dollar + Ident("_.Status.Foo") — captured wholesale here.
type PSPipeline ¶ added in v0.7.0
PSPipeline is the top-level grammar rule: one or more commands separated by |. Struct tags use participle's `parser:"..."` form (valid Go tag syntax); single quotes delimit literal tokens inside the grammar.
type PSPositional ¶ added in v0.7.0
type PSPositional struct {
Value PSValue `parser:"@@"`
}
type PSSafeExpr ¶ added in v0.7.0
type PSSafeExpr struct {
Or *PSOrExpr `parser:"@@"`
}
PSSafeExpr is the root of the safe-expression sub-grammar. Deliberately narrow: no assignment, no call syntax outside the type-whitelisted static call form, no arbitrary subexpressions beyond grouping parens.
type PSStaticArgList ¶ added in v0.7.0
type PSStaticArgList struct {
Args []*PSSafeExpr `parser:"'(' ( @@ ( ',' @@ )* )? ')'"`
}
type PSStaticCall ¶ added in v0.7.0
type PSStaticCall struct {
Type string `parser:"'[' @Ident ']' '::'"`
Member string `parser:"@Ident"`
Call *PSStaticArgList `parser:"@@?"`
}
PSStaticCall matches `[Type]::Member` optionally followed by `(args)`. Type and Member identifiers are whitelisted during post-parse validation; the grammar only bounds the shape, not which types/members are safe.
type PSValue ¶ added in v0.7.0
PSValue is a comma-separated list of literals. PowerShell treats `a, b, c` as an array argument; we flatten it into a single comma-joined token string. A single value (no commas) has an empty Tail slice.
type ParseError ¶
type ParseError struct {
Message string
}
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type Pipeline ¶
type Pipeline struct {
Segments []PipelineSegment
}
func ParsePowerShell ¶ added in v0.7.0
ParsePowerShell parses a PowerShell command string into a Pipeline using the safe PowerShell dialect grammar. Returns actionable error messages for rejected constructs.