parser

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: Apache-2.0, BSD-3-Clause Imports: 5 Imported by: 0

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

View Source
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 PSAddExpr added in v0.7.0

type PSAddExpr struct {
	First *PSMulExpr   `parser:"@@"`
	Rest  []*PSAddTail `parser:"@@*"`
}

type PSAddTail added in v0.7.0

type PSAddTail struct {
	Op   string     `parser:"@('+'|'-')"`
	Expr *PSMulExpr `parser:"@@"`
}

type PSAndExpr added in v0.7.0

type PSAndExpr struct {
	First *PSCmpExpr   `parser:"@@"`
	Rest  []*PSAndTail `parser:"@@*"`
}

type PSAndTail added in v0.7.0

type PSAndTail struct {
	Op   string     `parser:"@('-and')"`
	Expr *PSCmpExpr `parser:"@@"`
}

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 PSCmpExpr added in v0.7.0

type PSCmpExpr struct {
	Left *PSAddExpr `parser:"@@"`
	Tail *PSCmpTail `parser:"@@?"`
}

type PSCmpTail added in v0.7.0

type PSCmpTail struct {
	Op    string     `parser:"@('-eq'|'-ne'|'-gt'|'-lt'|'-ge'|'-le'|'-like'|'-notlike'|'-match'|'-notmatch')"`
	Right *PSAddExpr `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 PSFlag added in v0.7.0

type PSFlag struct {
	Name  string   `parser:"@Flag"`
	Value *PSValue `parser:"@@?"`
}

type PSHashEntry added in v0.7.0

type PSHashEntry struct {
	Key   string  `parser:"@Ident '='"`
	Value PSValue `parser:"@@"`
	Semi  *string `parser:"@Semi?"`
}

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 PSMulExpr added in v0.7.0

type PSMulExpr struct {
	First *PSUnary     `parser:"@@"`
	Rest  []*PSMulTail `parser:"@@*"`
}

type PSMulTail added in v0.7.0

type PSMulTail struct {
	Op   string   `parser:"@('/'|'%')"`
	Expr *PSUnary `parser:"@@"`
}

type PSOrExpr added in v0.7.0

type PSOrExpr struct {
	First *PSAndExpr  `parser:"@@"`
	Rest  []*PSOrTail `parser:"@@*"`
}

type PSOrTail added in v0.7.0

type PSOrTail struct {
	Op   string     `parser:"@('-or')"`
	Expr *PSAndExpr `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 PSPiped added in v0.7.0

type PSPiped struct {
	Command PSCommand `parser:"'|' @@"`
}

type PSPipeline added in v0.7.0

type PSPipeline struct {
	First PSCommand  `parser:"@@"`
	Rest  []*PSPiped `parser:"@@*"`
}

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 PSUnary added in v0.7.0

type PSUnary struct {
	Neg  *string `parser:"@'-'?"`
	Atom *PSAtom `parser:"@@"`
}

type PSValue added in v0.7.0

type PSValue struct {
	First *PSLiteral   `parser:"@@"`
	Tail  []*PSLiteral `parser:"( ',' @@ )*"`
}

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 Parse

func Parse(command string) (*Pipeline, error)

func ParsePowerShell added in v0.7.0

func ParsePowerShell(command string) (*Pipeline, error)

ParsePowerShell parses a PowerShell command string into a Pipeline using the safe PowerShell dialect grammar. Returns actionable error messages for rejected constructs.

type PipelineSegment

type PipelineSegment struct {
	Command  string
	Args     []string
	Operator string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL