atom

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2021 License: Unlicense Imports: 5 Imported by: 0

Documentation

Overview

Package atom provides primitives to describe and get the properties of an atomic part of a PKGBUILD file.

A PKGBUILD can contain:

  • Blank lines,
  • Comment lines,
  • Variable declarations (with distinction between string & array variables) containing themselves a name and a list of values and inner comments,
  • Function declarations containing themselves a name and a body,
  • Trailing comments after variables/functions declarations.

There should be only one root atom on a line of the file. For this reason, declarations and trailing comments are grouped in an atom of type group.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(a Atom) string

Debug returns a string representation of the atom (for debugging only).

func GetBegin

func GetBegin(a Atom) position.Position

GetBegin returns the begin position of the atom.

func GetEnd

func GetEnd(a Atom) position.Position

GetEnd returns the end position of the atom.

func IncrementPosition

func IncrementPosition(a Atom, inc position.Increment, recursive ...bool) (newBegin, newEnd position.Position)

IncrementPosition moves the given atom in incrementing the beginning and the end positions with the given increment. If recursive is needed, it moves the inner atoms too. It returns the new begin/end positions of the atom.

func MoveAtom

func MoveAtom(a Atom, begin position.Position, recursive ...bool) (newBegin, newEnd position.Position)

MoveAtom moves the atom to the given position. If recursive is needed, it moves the inner atoms too. It returns the new begin/end positions of the atom.

func RecomputeAllPositions

func RecomputeAllPositions(a Atom, optBegin ...position.Position) (newBegin, newEnd position.Position)

RecomputeAllPositions is same as RecomputePosition but it recomputes the inner atoms too. It returns the new begin/end positions of the atom.

func RecomputePosition

func RecomputePosition(a Atom, optBegin ...position.Position) (newBegin, newEnd position.Position)

RecomputePosition recomputes the end position of the atom according to the raw string. If a begin position is given, it moves the atom first before recomputing. It returns the new begin/end positions of the atom.

func SetBegin

func SetBegin(a Atom, b position.Position) position.Position

SetBegin put the begin position of the atom.

func SetEnd

func SetEnd(a Atom, e position.Position) position.Position

SetEnd put the end position of the atom.

Types

type Atom

type Atom interface {
	GetType() AtomType
	GetRaw() string
	GetPositions() (begin, end position.Position)
	SetType(AtomType)
	SetRaw(string)
	SetPositions(begin, end position.Position)
	Clone() Atom
}

Atom represents an atomic part of a PKGBUILD.

func NewBlank

func NewBlank() Atom

NewBlank returns an atom of type blank.

func NewComment

func NewComment() Atom

NewComment returns an atom of type comment.

type AtomCheckerFunc

type AtomCheckerFunc func(Atom) bool

func AtomCheckAll

func AtomCheckAll(checkers ...AtomCheckerFunc) AtomCheckerFunc

AtomCheckAll returns a callback checker which applies all given checkers.

func NewMatcher

func NewMatcher(types ...AtomType) AtomCheckerFunc

NewMatcher returns a callback checking if an atom has a supported type.

func NewRevMatcher

func NewRevMatcher(types ...AtomType) AtomCheckerFunc

NewRevMatcher returns a callback checking if an atom has not a supported type.

func RevCheck

func RevCheck(cb AtomCheckerFunc) AtomCheckerFunc

RevCheck returns the inverse of the given callback.

type AtomFunc

type AtomFunc struct {
	// contains filtered or unexported fields
}

AtomFunc is a function declaration.

func NewFunction

func NewFunction() *AtomFunc

NewFunction returns a new function declaration.

func (*AtomFunc) Clone

func (a *AtomFunc) Clone() Atom

func (*AtomFunc) Copy

func (a *AtomFunc) Copy() *AtomFunc

func (*AtomFunc) FormatSpaces

func (a *AtomFunc) FormatSpaces(recomputeRaw bool)

FormatSpaces removes useless space before, between and after the name and the body of the function declaration. If recomputeRaw is true, the raw value of the atom is recomputed.

func (*AtomFunc) GetBody

func (a *AtomFunc) GetBody() string

func (*AtomFunc) GetBodyPositions

func (a *AtomFunc) GetBodyPositions() (b, e position.Position)

func (AtomFunc) GetName

func (a AtomFunc) GetName() string

func (AtomFunc) GetNamePositions

func (a AtomFunc) GetNamePositions() (b, e position.Position)

func (*AtomFunc) RecomputeRaw

func (a *AtomFunc) RecomputeRaw()

RecomputeRaw recomputes the raw value of the function declaration

func (*AtomFunc) SetBody

func (a *AtomFunc) SetBody(b string)

func (*AtomFunc) SetBodyPositions

func (a *AtomFunc) SetBodyPositions(b, e position.Position)

func (AtomFunc) SetName

func (a AtomFunc) SetName(n string)

func (AtomFunc) SetNamePositions

func (a AtomFunc) SetNamePositions(b, e position.Position)

type AtomGroup

type AtomGroup struct {
	Childs Slice
	// contains filtered or unexported fields
}

AtomGroup is a group of atoms It should contains: - one declaration atom (variable or function), - one comment.

func NewGroup

func NewGroup() *AtomGroup

NewGroup returns a new group atom.

func (*AtomGroup) Clone

func (a *AtomGroup) Clone() Atom

func (*AtomGroup) Copy

func (a *AtomGroup) Copy() *AtomGroup

func (*AtomGroup) FormatSpaces

func (a *AtomGroup) FormatSpaces(recomputeRaw bool)

FormatSpaces removes useless spaces before, between and after each childs of the group. If recomputeRaw is true, it recomputes the raw string of the group.

func (AtomGroup) GetPositions

func (a AtomGroup) GetPositions() (b, e position.Position)

func (AtomGroup) GetRaw

func (a AtomGroup) GetRaw() string

func (AtomGroup) GetType

func (a AtomGroup) GetType() AtomType

func (*AtomGroup) RecomputRaw

func (a *AtomGroup) RecomputRaw()

RecomputeRaw recompute the raw string of the group.

func (AtomGroup) SetPositions

func (a AtomGroup) SetPositions(b, e position.Position)

func (AtomGroup) SetRaw

func (a AtomGroup) SetRaw(r string)

func (AtomGroup) SetType

func (a AtomGroup) SetType(t AtomType)

type AtomNamed

type AtomNamed interface {
	Atom
	GetName() string
	GetNamePositions() (begin, end position.Position)
	SetName(string)
	SetNamePositions(begin, end position.Position)
}

AtomNamed represents an atom of type declaration (function or variable).

func GetNamed

func GetNamed(a Atom) (n AtomNamed, exists bool, isParent bool)

GetNamed returns the named atom of the given atom if possible. If the atom is a named atom, it returns it. If the atom is a group, it returns the first named atom child and put isParent to true. Otherwise, exists is false.

type AtomType

type AtomType int

Type is a type of atom.

const (
	Unknown AtomType = iota
	Blank
	Comment
	Name
	Value
	Body
	Function
	VarString
	VarArray
	Group
)

type AtomValue

type AtomValue struct {
	// contains filtered or unexported fields
}

AtomValue is an atom containing a value of a variable declaration.

func NewValue

func NewValue() *AtomValue

NewValue returns an otom of type value.

func (*AtomValue) Clone

func (a *AtomValue) Clone() Atom

func (*AtomValue) Copy

func (a *AtomValue) Copy() *AtomValue

func (*AtomValue) GetDepends

func (a *AtomValue) GetDepends() []string

GetDepends returns the needed variable to fully compute the value.

func (*AtomValue) GetFormatted

func (a *AtomValue) GetFormatted(quoteNeeded bool) string

GetFormatted formats a value with or without quote depending of the given parameter.

func (*AtomValue) GetParsed

func (a *AtomValue) GetParsed(depends map[string]string) string

GetParsed returns the real value, replacing the referenced variables by their values.

func (AtomValue) GetPositions

func (a AtomValue) GetPositions() (b, e position.Position)

func (AtomValue) GetRaw

func (a AtomValue) GetRaw() string

func (AtomValue) GetType

func (a AtomValue) GetType() AtomType

func (*AtomValue) GetValue

func (a *AtomValue) GetValue() string

GetValue returns the string representation of the formatted value

func (*AtomValue) SetFormat

func (a *AtomValue) SetFormat(f ValueFormatter)

SetFormat puts the format to use to compute and format the value.

func (AtomValue) SetPositions

func (a AtomValue) SetPositions(b, e position.Position)

func (AtomValue) SetRaw

func (a AtomValue) SetRaw(r string)

func (AtomValue) SetType

func (a AtomValue) SetType(t AtomType)

type AtomVar

type AtomVar struct {
	// contains filtered or unexported fields
}

AtomVar is a variable declaration.

func NewArrayVar

func NewArrayVar() *AtomVar

NewArrayVar returns a variable of type array.

func NewStringVar

func NewStringVar() *AtomVar

NewStringVar returns a variable of type string.

func (*AtomVar) Clone

func (a *AtomVar) Clone() Atom

func (*AtomVar) Copy

func (a *AtomVar) Copy() *AtomVar

func (*AtomVar) FormatSpaces

func (a *AtomVar) FormatSpaces(recomputeRaw bool)

FormatSpaces removes useless spaces of the atom. If recomputeRaw is true, the raw string is recomputed.

func (*AtomVar) FormatVariables

func (a *AtomVar) FormatVariables(recomputeRaw, quoteNeeded bool, optType ...AtomType)

FormatVariables reformats the values of the declaration, with or without quote depending to the value of the quoteNeeded parameter. If a type is given (should be of VarString or VarArray), the type of of the variable is forced.That means if an array variable becones a string variable, only the first value is kept. If recomputeRaw is true, the raw string of the declaration is recomputed.

func (*AtomVar) GetArrayFormatted

func (a *AtomVar) GetArrayFormatted(quoteNeeded bool) []string

GetArrayFormatted returns the list of the beautified values with or without quotes depending of the given parameter.

func (*AtomVar) GetArrayParsed

func (a *AtomVar) GetArrayParsed(depends map[string]string) []string

GetArrayParsed returns the list of real values computed with the known values of the depending variables.

func (*AtomVar) GetArrayValue

func (a *AtomVar) GetArrayValue() []string

GetArrayValue returns the list of the raw string of the values.

func (*AtomVar) GetDepends

func (a *AtomVar) GetDepends() map[string]bool

GetDepends returns the list of the needed variables to compute the real values of the declaration.

func (AtomVar) GetName

func (a AtomVar) GetName() string

func (AtomVar) GetNamePositions

func (a AtomVar) GetNamePositions() (b, e position.Position)

func (*AtomVar) GetStringFormatted

func (a *AtomVar) GetStringFormatted(quoteNeeded bool) string

GetStringFormatted is same as GetArrayFormatted but returns only the first entry or an empty string if no value).

func (*AtomVar) GetStringParsed

func (a *AtomVar) GetStringParsed(depends map[string]string) string

GetStringParsed is same as GetArrayParsed but returns only the first entry or an empty string if no value).

func (*AtomVar) GetStringValue

func (a *AtomVar) GetStringValue() string

GetStringValue is same as GetArrayValue but returns only the first entry or an empty string if no value).

func (*AtomVar) GetValue

func (a *AtomVar) GetValue() (e *AtomValue, exists bool)

GetValue returns the first value otom or false if the declaration is empty of values.

func (*AtomVar) GetValues

func (a *AtomVar) GetValues() []*AtomValue

GetValues returns the value otams only (without comments).

func (*AtomVar) RecomputRaw

func (a *AtomVar) RecomputRaw()

RecomputeRaw recomputes the raw string of the atom.

func (*AtomVar) RemoveComments

func (a *AtomVar) RemoveComments(recomputeRaw bool)

RemoveComments removes all inner comment of the declaration.

func (AtomVar) SetName

func (a AtomVar) SetName(n string)

func (AtomVar) SetNamePositions

func (a AtomVar) SetNamePositions(b, e position.Position)

func (*AtomVar) SetValues

func (a *AtomVar) SetValues(values ...Atom)

SetValues set the values atoms to the given atoms. The atoms must be of type value or comment (if it is an inner comment of the variable declaration).

type Info

type Info struct {
	AtomNamed
	// contains filtered or unexported fields
}

Info packs properties of a named atom.

func NewInfo

func NewInfo(a Atom) (info *Info, ok bool)

NewInfo returns the infos of the given atom. If the atom is not a named atom or doesn’t contain a named atom, it returns false.

func (*Info) ArrayRawValue

func (i *Info) ArrayRawValue() []string

ArrayRawValue returns the raw values of the described atom or an empty array if it’s not a variable.

func (*Info) ArrayValue

func (i *Info) ArrayValue() []string

ArrayValue returns the values of the described atom or an empty array if it’s not a variable.

func (*Info) Begin

func (i *Info) Begin() position.Position

Begin returns the begin position of the named atom or the container.

func (*Info) Body

func (i *Info) Body() string

Body returns the raw content of the body function or an empty string if it’s not a function.

func (*Info) End

func (i *Info) End() position.Position

End returns the end position of the named atom or the container.

func (*Info) Function

func (i *Info) Function() (n *AtomFunc, ok bool)

Function returns the functon atom described by the info, or false if it’s not a function atom.

func (*Info) Index

func (i *Info) Index() int

Index returns the index position of the described atom in the slice.

func (*Info) IsArrayVar

func (i *Info) IsArrayVar() bool

IsArrayVar returns true if the info concerns a variable of type array.

func (*Info) IsFunc

func (i *Info) IsFunc() bool

IsFunction returns true if the info concerns a function.

func (*Info) IsStringVar

func (i *Info) IsStringVar() bool

IsStringVar returns true if the info concerns a variable of type string.

func (*Info) IsVar

func (i *Info) IsVar() bool

IsSVar returns true if the info concerns a variable.

func (*Info) Name

func (i *Info) Name() string

Name returns the name of the named atom.

func (*Info) Raw

func (i *Info) Raw() string

Raw returns the raw string of the described named atom.

func (*Info) StringRawValue

func (i *Info) StringRawValue() string

StringRawValue returns the first raw value of the described atom or an empty string if it’s not a variable.

func (*Info) StringValue

func (i *Info) StringValue() string

StringValue returns the first value of the described atom or an empty string if it’s not a variable.

func (*Info) Variable

func (i *Info) Variable() (n *AtomVar, ok bool)

Variable returns the variable atom described by the info, or false if it’s not a variable atom.

type InfoList

type InfoList struct {
	// contains filtered or unexported fields
}

InfoList is a list of named infos in a slice of atoms.

func NewInfoList

func NewInfoList() *InfoList

NewInfoList returns an empty info list.

func (*InfoList) Filter

func (l *InfoList) Filter(cb NamedCheckerFunc) []*Info

Filter returns the infos of the atoms which pass le callback.

func (*InfoList) FilterFirst

func (l *InfoList) FilterFirst(cb NamedCheckerFunc) (info *Info, exists bool)

FilterFirst returns the first atom which pass the callback or false if no atom was found.

func (*InfoList) Functions

func (l *InfoList) Functions() []*Info

Functions returns all infos of function atoms.

func (*InfoList) Get

func (l *InfoList) Get(a Atom) (info *Info, exists bool)

Get is same as GetNamed but checks the container.

func (*InfoList) GetByIndex

func (l *InfoList) GetByIndex(idx int) (info *Info, exists bool)

GetByIndex returns the info on the given index.

func (*InfoList) GetDeep

func (l *InfoList) GetDeep(a Atom) (info *Info, exists bool)

GetDeep returns the info of the atom wheter it is a named atom or a container, or false if not found.

func (*InfoList) GetNamed

func (l *InfoList) GetNamed(n AtomNamed) (info *Info, exists bool)

GetNamed returns the info of the given named atom or false if not found.

func (*InfoList) GetValue

func (l *InfoList) GetValue(name string) string

GetValue returns the value of the given variable name or an empty string if not found.

func (*InfoList) GetValues

func (l *InfoList) GetValues() map[string]string

GetValues returns the list of the variable values indexed by the name of the variables.

func (*InfoList) HasValue

func (l *InfoList) HasValue(name string) bool

HasValue returns true if the name is a variable name and if it has a value.

func (*InfoList) Keys

func (l *InfoList) Keys() Slice

Keys returns the ilst of atoms where infos are found.

func (*InfoList) RecomputeValues

func (l *InfoList) RecomputeValues()

RecomputeValues recomputes all values of all variable atoms.

func (*InfoList) Update

func (l *InfoList) Update(a Atom, i int) (t rune)

Update updates the info of the given atom with the specified index and returns a rune code specific to the type of update: - 'A' if the info didn’t exist and was added, - 'D' if the info existed and was deleted, - 'U' if the info was found and some properties were modified, - 'O' if there was no change.

func (*InfoList) UpdateAll

func (l *InfoList) UpdateAll(atoms Slice)

UpdateAll update the infos with the slice of atoms.

func (*InfoList) Variables

func (l *InfoList) Variables() []*Info

Variables returns all infos of variable atoms.

type NamedCheckerFunc

type NamedCheckerFunc func(AtomNamed) bool

func CheckName

func CheckName(name string) NamedCheckerFunc

CheckName returns a callback to check if an atom declaration has the given name.

func NamedCheckAll

func NamedCheckAll(checkers ...NamedCheckerFunc) NamedCheckerFunc

NamedCheckAll returns a callback checker which applies all given checkers.

func NewNameMatcher

func NewNameMatcher(types ...AtomType) NamedCheckerFunc

NewNameMatcher returns a callback checking if an atom declaration has a supported type.

func NewRevNameMatcher

func NewRevNameMatcher(types ...AtomType) NamedCheckerFunc

NewRevNameMatcher returns a callback checking if an atom declaration has not a supported type.

func RevNameCheck

func RevNameCheck(cb NamedCheckerFunc) NamedCheckerFunc

RevNameCheck returns the inverse of the given callback.

type Slice

type Slice []Atom

func (Slice) Clone

func (l Slice) Clone() Slice

Clone makes a copy of the slice.

func (Slice) Filter

func (l Slice) Filter(types ...AtomType) Slice

Filter returns all atoms matching one of the given types.

func (Slice) FilterFirst

func (l Slice) FilterFirst(types ...AtomType) (Atom, bool)

FilterFirst returns the firt atom matching one of the given types. It returns false if no atom was found.

func (Slice) FilterFirstRecursive

func (l Slice) FilterFirstRecursive(types ...AtomType) (Atom, bool)

FilterFirstRecursive is same as FilterFirst but it makes a deep search in searching in the atom containing other atoms.

func (Slice) FilterRecursive

func (l Slice) FilterRecursive(types ...AtomType) Slice

FilterRecursive is same as Filter but it makes a deep search in searching in the atom containing other atoms.

func (*Slice) Insert

func (l *Slice) Insert(idx int, atoms ...Atom)

Insert appends the given atoms at the specified index of the slice. If the index is negative, it is counted from the end. For example, -1 is the last index of the slice.

func (Slice) Map

func (l Slice) Map(cb func(Atom) Atom) Slice

Map apply a transfarmation function to each atom of the slice and returns the result of this transformation.

func (*Slice) Pop

func (l *Slice) Pop() (a Atom, exists bool)

Pop removes the last element of the slice and returns it. If the slice is empty, it returns false.

func (*Slice) PopFront

func (l *Slice) PopFront() (a Atom, exists bool)

PopFront removes the first element of the slice and returns it. If the slice is empty, it returns false.

func (*Slice) Push

func (l *Slice) Push(atoms ...Atom)

Push append the given atoms at the end of the slice.

func (*Slice) PushFront

func (l *Slice) PushFront(atoms ...Atom)

PushFront append the given atoms at the beginning of the slice.

func (*Slice) Remove

func (l *Slice) Remove(idx int) (a Atom, exists bool)

Pop removes the element at the given index of the slice and returns it. If the slice is empty or the index is not valid, it returns false.

func (Slice) Search

func (l Slice) Search(cb AtomCheckerFunc, optRecursive ...bool) (result Slice)

Search returns all atoms matching the given checker. If recursive option is given and is true, it makes a deep search in searching in the atom containing other atoms.

func (Slice) SearchFirst

func (l Slice) SearchFirst(cb AtomCheckerFunc, optRecursive ...bool) (result Atom, exists bool)

SearchFirst is same as Search but it returns only the first found atom. It returns false if no atom was found.

type TypeSet

type TypeSet map[AtomType]bool

TypeSet is a set of atoms’ types.

func NewTypeSet

func NewTypeSet(types ...AtomType) TypeSet

func (TypeSet) Match

func (ts TypeSet) Match(a Atom) bool

Match checks if the given atom has a type contained in the set.

func (TypeSet) MatchNamed

func (ts TypeSet) MatchNamed(a AtomNamed) bool

MatchNamed checks if the given atom declaration has a type contained in the set.

type ValueElement

type ValueElement struct {
	// contains filtered or unexported fields
}

ValueElement represents an atomic part of a value It can be a string of the name of a referenced variable.

func (ValueElement) Format

func (e ValueElement) Format(q rune) string

Format formats the element according to the given quote rune. It applies a transformation in escaping characters when needed.

func (ValueElement) Parse

func (e ValueElement) Parse(depends map[string]string) string

Parse returns the computed string of the element using the given mapping (value of a known variable). If the element is a raw string, it returns it. Else it returns the value of the referenced variable (or an empty string if the variable doesn’t exist).

func (ValueElement) String

func (e ValueElement) String() string

String returns the string representation of the part of the value. It can be on the form: - part if the element is a raw string - ${name} if the element is a reference to a variable.

type ValueFormatter

type ValueFormatter []ValueElement

ValueFormatter represent parts of a value.

func NewValueFormatter

func NewValueFormatter(s string) (f ValueFormatter, ok bool)

NewValueFormatter splits the given value in order to distinguish which part is a raw string and which part is a reference to a variable. It returns false if the parse failed.

func (ValueFormatter) Clone

func (f ValueFormatter) Clone() ValueFormatter

Clone returns a copy of the formatter.

func (ValueFormatter) Debug

func (f ValueFormatter) Debug() string

Debug returns a string representation of the formatter (for debugging only).

func (ValueFormatter) Format

func (f ValueFormatter) Format(q rune) string

Format formats the value according to the given quote rune. It escapes chars when needed.

func (ValueFormatter) GetDepends

func (f ValueFormatter) GetDepends() (depends []string)

GetDepends returns the list of the required variables’ names to fully compute the real value.

func (ValueFormatter) HasDep

func (f ValueFormatter) HasDep() bool

HasDep returns true if the formmatter contains referenced variables.

func (ValueFormatter) Parse

func (f ValueFormatter) Parse(depends map[string]string) string

Parse returns the computed value according to the value of the referenced variables.

func (ValueFormatter) String

func (f ValueFormatter) String() string

String returns the string representation of the formatter.

Jump to

Keyboard shortcuts

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