package ini
import "github.com/aws/aws-sdk-go-v2/internal/ini"
Package ini is an LL(1) parser for configuration files.
Example: sections, err := ini.OpenFile("/path/to/file") if err != nil { panic(err) } profile := "foo" section, ok := sections.GetSection(profile) if !ok { fmt.Printf("section %q could not be found", profile) }
Below is the BNF that describes this parser
Grammar: stmt -> value stmt' stmt' -> epsilon | op stmt value -> number | string | boolean | quoted_string section -> [ section' section' -> value section_close section_close -> ] SkipState will skip (NL WS)+ comment -> # comment' | ; comment' comment' -> epsilon | value
Index ¶
- Constants
- Variables
- func EqualExprKey(ast AST) string
- func Walk(tree []AST, v Visitor) error
- type AST
- func ParseAST(r io.Reader) ([]AST, error)
- func ParseASTBytes(b []byte) ([]AST, error)
- func (a *AST) AppendChild(child AST)
- func (a *AST) GetChildren() []AST
- func (a *AST) GetRoot() AST
- func (a *AST) SetChildren(children []AST)
- type ASTKind
- type DefaultVisitor
- func NewDefaultVisitor() *DefaultVisitor
- func (v *DefaultVisitor) VisitExpr(expr AST) error
- func (v *DefaultVisitor) VisitStatement(stmt AST) error
- type ParseError
- func NewParseError(message string) *ParseError
- func (err *ParseError) Code() string
- func (err *ParseError) Error() string
- func (err *ParseError) Message() string
- func (err *ParseError) OrigError() error
- type ParseStack
- func (s *ParseStack) Len() int
- func (s ParseStack) List() []AST
- func (s *ParseStack) MarkComplete(ast AST)
- func (s *ParseStack) Pop() AST
- func (s *ParseStack) Push(ast AST)
- func (s ParseStack) String() string
- type Section
- func (t Section) Bool(k string) bool
- func (t Section) Float64(k string) float64
- func (t Section) Has(k string) bool
- func (t Section) Int(k string) int64
- func (t Section) String(k string) string
- func (t Section) ValueType(k string) (ValueType, bool)
- type Sections
- func OpenFile(path string) (Sections, error)
- func Parse(f io.Reader) (Sections, error)
- func ParseBytes(b []byte) (Sections, error)
- func (t Sections) GetSection(p string) (Section, bool)
- func (t Sections) List() []string
- type Token
- type TokenType
- type Value
- func (v *Value) Append(tok Token)
- func (v Value) BoolValue() bool
- func (v Value) FloatValue() float64
- func (v Value) IntValue() int64
- func (v Value) String() string
- func (v Value) StringValue() string
- type ValueType
- type Visitor
Constants ¶
const ( ASTKindNone = ASTKind(iota) ASTKindStart ASTKindExpr ASTKindEqualExpr ASTKindStatement ASTKindSkipStatement ASTKindExprStatement ASTKindSectionStatement ASTKindNestedSectionStatement ASTKindCompletedNestedSectionStatement ASTKindCommentStatement ASTKindCompletedSectionStatement )
ASTKind* is used in the parse table to transition between the different states
const ( TokenNone = TokenType(iota) TokenLit TokenSep TokenComma TokenOp TokenWS TokenNL TokenComment )
TokenType enums
const ( InvalidState = iota // stmt -> value stmt' StatementState // stmt' -> MarkComplete | op stmt StatementPrimeState // value -> number | string | boolean | quoted_string ValueState // section -> [ section' OpenScopeState // section' -> value section_close SectionState // section_close -> ] CloseScopeState // SkipState will skip (NL WS)+ SkipState // SkipTokenState will skip any token and push the previous // state onto the stack. SkipTokenState // comment -> # comment' | ; comment' // comment' -> MarkComplete | value CommentState // MarkComplete state will complete statements and move that // to the completed AST list MarkCompleteState // TerminalState signifies that the tokens have been fully parsed TerminalState )
State enums for the parse table
ValueType enums
const ( // ErrCodeParseError is returned when a parsing error // has occurred. ErrCodeParseError = "INIParseError" )
const ( // ErrCodeUnableToReadFile is used when a file is failed to be // opened or read from. ErrCodeUnableToReadFile = "FailedRead" )
Variables ¶
var Start = newAST(ASTKindStart, AST{})
Start is used to indicate the starting state of the parse table.
Functions ¶
func EqualExprKey ¶
EqualExprKey will return a LHS value in the equal expr
func Walk ¶
Walk will traverse the AST using the v, the Visitor.
Types ¶
type AST ¶
AST interface allows us to determine what kind of node we are on and casting may not need to be necessary.
The root is always the first node in Children
func ParseAST ¶
ParseAST will parse input from an io.Reader using an LL(1) parser.
func ParseASTBytes ¶
ParseASTBytes will parse input from a byte slice using an LL(1) parser.
func (*AST) AppendChild ¶
AppendChild will append to the list of children an AST has.
func (*AST) GetChildren ¶
GetChildren will return the current AST's list of children
func (*AST) GetRoot ¶
GetRoot will return the root AST which can be the first entry in the children list or a token.
func (*AST) SetChildren ¶
SetChildren will set and override all children of the AST.
type ASTKind ¶
type ASTKind int
ASTKind represents different states in the parse table and the type of AST that is being constructed
func (ASTKind) String ¶
type DefaultVisitor ¶
type DefaultVisitor struct { Sections Sections // contains filtered or unexported fields }
DefaultVisitor is used to visit statements and expressions and ensure that they are both of the correct format. In addition, upon visiting this will build sections and populate the Sections field which can be used to retrieve profile configuration.
func NewDefaultVisitor ¶
func NewDefaultVisitor() *DefaultVisitor
NewDefaultVisitor return a DefaultVisitor
func (*DefaultVisitor) VisitExpr ¶
func (v *DefaultVisitor) VisitExpr(expr AST) error
VisitExpr visits expressions...
func (*DefaultVisitor) VisitStatement ¶
func (v *DefaultVisitor) VisitStatement(stmt AST) error
VisitStatement visits statements...
type ParseError ¶
type ParseError struct {
// contains filtered or unexported fields
}
ParseError is an error which is returned during any part of the parsing process.
func NewParseError ¶
func NewParseError(message string) *ParseError
NewParseError will return a new ParseError where message is the description of the error.
func (*ParseError) Code ¶
func (err *ParseError) Code() string
Code will return the ErrCodeParseError
func (*ParseError) Error ¶
func (err *ParseError) Error() string
func (*ParseError) Message ¶
func (err *ParseError) Message() string
Message returns the error's message
func (*ParseError) OrigError ¶
func (err *ParseError) OrigError() error
OrigError return nothing since there will never be any original error.
type ParseStack ¶
type ParseStack struct {
// contains filtered or unexported fields
}
ParseStack is a stack that contains a container, the stack portion, and the list which is the list of ASTs that have been successfully parsed.
func (*ParseStack) Len ¶
func (s *ParseStack) Len() int
Len will return the length of the container
func (ParseStack) List ¶
func (s ParseStack) List() []AST
List will return the completed statements
func (*ParseStack) MarkComplete ¶
func (s *ParseStack) MarkComplete(ast AST)
MarkComplete will append the AST to the list of completed statements
func (*ParseStack) Pop ¶
func (s *ParseStack) Pop() AST
Pop will return and truncate the last container element.
func (*ParseStack) Push ¶
func (s *ParseStack) Push(ast AST)
Push will add the new AST to the container
func (ParseStack) String ¶
func (s ParseStack) String() string
type Section ¶
type Section struct { Name string // contains filtered or unexported fields }
Section contains a name and values. This represent a sectioned entry in a configuration file.
func (Section) Bool ¶
Bool returns a bool value at k
func (Section) Float64 ¶
Float64 returns a float value at k
func (Section) Has ¶
Has will return whether or not an entry exists in a given section
func (Section) Int ¶
Int returns an integer value at k
func (Section) String ¶
String returns the string value at k
func (Section) ValueType ¶
ValueType will returned what type the union is set to. If k was not found, the NoneType will be returned.
type Sections ¶
type Sections struct {
// contains filtered or unexported fields
}
Sections is a map of Section structures that represent a configuration.
func OpenFile ¶
OpenFile takes a path to a given file, and will open and parse that file.
func Parse ¶
Parse will parse the given file using the shared config visitor.
func ParseBytes ¶
ParseBytes will parse the given bytes and return the parsed sections.
func (Sections) GetSection ¶
GetSection will return section p. If section p does not exist, false will be returned in the second parameter.
func (Sections) List ¶
List will return a list of all sections that were successfully parsed.
type Token ¶
type Token struct { ValueType ValueType // contains filtered or unexported fields }
Token indicates a metadata about a given value.
func (Token) Raw ¶
Raw return the raw runes that were consumed
func (Token) Type ¶
Type returns the token type
type TokenType ¶
type TokenType int
TokenType represents the various different tokens types
func (TokenType) String ¶
type Value ¶
type Value struct { Type ValueType // contains filtered or unexported fields }
Value is a union container
func (*Value) Append ¶
Append will append values and change the type to a string type.
func (Value) BoolValue ¶
BoolValue returns a bool value
func (Value) FloatValue ¶
FloatValue returns a float value
func (Value) IntValue ¶
IntValue returns an integer value
func (Value) String ¶
func (Value) StringValue ¶
StringValue returns the string value
type ValueType ¶
type ValueType int
ValueType is an enum that will signify what type the Value is
func (ValueType) String ¶
type Visitor ¶
Visitor is an interface used by walkers that will traverse an array of ASTs.
Source Files ¶
ast.go comma_token.go comment_token.go doc.go empty_token.go expression.go ini.go ini_lexer.go ini_parser.go literal_tokens.go newline_token.go number_helper.go op_tokens.go parse_error.go parse_stack.go sep_tokens.go skipper.go statement.go value_util.go visitor.go walker.go ws_token.go
- Version
- v0.17.0
- Published
- Nov 21, 2019
- Platform
- windows/amd64
- Imports
- 10 packages
- Last checked
- 5 minutes ago –
Tools for package owners.