// Package cmpd contains utilities for working with compound nodes.
package cmpd import ( ) // Primary returns a primary node and true if that's the only child of the // compound node. Otherwise it returns nil and false. func ( *parse.Compound) (*parse.Primary, bool) { if len(.Indexings) == 1 && len(.Indexings[0].Indicies) == 0 { return .Indexings[0].Head, true } return nil, false } // StringLiteral returns the value of a string literal and true if that's the // only child of the compound node. Otherwise it returns "" and false. func ( *parse.Compound) (string, bool) { if , := Primary(); { switch .Type { case parse.Bareword, parse.SingleQuoted, parse.DoubleQuoted: return .Value, true } } return "", false } // Lambda returns a lambda primary node and true if that's the only child of the // compound node. Otherwise it returns nil and false. func ( *parse.Compound) (*parse.Primary, bool) { if , := Primary(); { if .Type == parse.Lambda { return , true } } return nil, false } // StringLiteralOrError is like StringLiteral, but returns an error suitable as // a compiler error when StringLiteral would return false. func ( *parse.Compound, string) (string, error) { , := StringLiteral() if ! { return "", fmt.Errorf("%s must be string literal, found %s", , Shape()) } return , nil } // Shape describes the shape of the compound node. func ( *parse.Compound) string { if len(.Indexings) == 0 { return "empty expression" } if len(.Indexings) > 1 { return "compound expression" } := .Indexings[0] if len(.Indicies) > 0 { return "indexing expression" } := .Head return "primary expression of type " + .Type.String() }