fig

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2022 License: MIT Imports: 17 Imported by: 1

README

fig

todos/nice to have

  • expression evaluation: TBD
  • user defined functions inside fig file: TBD

spec

comment

fig has two kind of comments:

  • single line comment: a # symbol marks the rest of the line as a comment.
  • multiline line comment: everything between /* and */ is a comment. nesting multiline comments is allowed but is not recommended.
types

fig supports the following primitive types:

  • string
  • literal
  • integer
  • float
  • boolean
options

options are defined as a pair key/value pair separated by a = symbol. Each option is defined on its own line. A line ends with with a \n or, optionally, with a ; symbol followed by \n.

values can be of any of the primitive types defined above and can also be an array of these values.

key = value

an option without value is allowed. if such an option is present in a document, the parser/decoder should choose the more suitable zero-value for this field.

key = # empty value is allowed

repeating multiple times the same key is also allowed. it has as effect to create an option with the given key as identifier and an array as value with the defined values. there is no check on the type of values set in the created array.

key = 1
key = 2
key = true
key = foobar
# is equivalent to
key = [1, 2, true, foobar]
objects
arrays
variables

in fig, variables can take two forms:

  • local variables are identifier that reference an option defined in an object at the higher level that the object that used the variable
  • environment variables are variable that are defined outside the document and pass to the parser/decoder in order to be available in the document (in its "environment")

example

answer = 42
hitchhiker {
    answer   = $answer # a local variable
    question = @question # an environmnet variable
}
functions
macros

macros are a simple way to modify the parsed document. The supported macros can created new options from an external file, repeated the same objects multiple times, include another fig file into the current one,...

macros in fig are a bit like function called in other programming language. Indeed, some take positional arguments (but can also be defined as keyword arguments), some not. Some macros needs an object (like the body of function), some not.

the following section describes each macros supported currently by fig as well as their arguments.

include
define
apply
extend
repeat
readfile
register

the register macro allows to register a variable that will be given to the decoder.

ifeq
ifneq
ifdef
ifndef

Documentation

Index

Constants

View Source
const (
	EOF rune = -(iota + 1)
	Ident
	Comment
	Macro
	Boolean
	Heredoc
	String
	Template
	Integer
	Float
	LocalVar
	EnvVar
	BegArr
	EndArr
	BegObj
	EndObj
	BegGrp
	EndGrp
	Comma
	Slice
	Assign
	EOL
	Invalid
)

Variables

View Source
var (
	ErrUnexpected = errors.New("unexpected token")
	ErrSyntax     = errors.New("syntax error")
	ErrAllow      = errors.New("not allowed")
)

Functions

func Apply added in v0.1.0

func Apply(root, _ Node, env *Env, args []Node, kwargs map[string]Node) error

func Debug

func Debug(r io.Reader, w io.Writer) error

func Define added in v0.1.0

func Define(root, nest Node, env *Env, args []Node, kwargs map[string]Node) error

func Extend added in v0.1.2

func Extend(root, nest Node, env *Env, args []Node, kwargs map[string]Node) error

func IfDef added in v0.4.0

func IfDef(root, nest Node, env *Env, args []Node, kwargs map[string]Node) error

func IfEq added in v0.3.2

func IfEq(root, nest Node, env *Env, args []Node, kwargs map[string]Node) error

func IfNotDef added in v0.4.0

func IfNotDef(root, nest Node, env *Env, args []Node, kwargs map[string]Node) error

func IfNotEq added in v0.3.2

func IfNotEq(root, nest Node, env *Env, args []Node, kwargs map[string]Node) error

func Include added in v0.1.0

func Include(root, _ Node, env *Env, args []Node, kwargs map[string]Node) error

func ReadFile added in v0.1.5

func ReadFile(root, _ Node, env *Env, args []Node, kwargs map[string]Node) error

func Register added in v0.4.0

func Register(root, _ Node, env *Env, args []Node, kwargs map[string]Node) error

func Repeat added in v0.1.3

func Repeat(root, nest Node, env *Env, args []Node, kwargs map[string]Node) error

func Script added in v0.5.0

func Script(root, _ Node, env *Env, args []Node, kwargs map[string]Node) error

Types

type Argument

type Argument interface {
	GetString() (string, error)
	GetFloat() (float64, error)
	GetInt() (int64, error)
	GetUint() (uint64, error)
	GetBool() (bool, error)
}

type Decoder added in v0.1.0

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

func NewDecoder added in v0.1.4

func NewDecoder(r io.Reader) *Decoder

func (*Decoder) Decode added in v0.1.4

func (d *Decoder) Decode(v interface{}) error

func (*Decoder) Define added in v0.1.5

func (d *Decoder) Define(ident string, value interface{})

func (*Decoder) Funcs added in v0.1.4

func (d *Decoder) Funcs(set FuncMap)

type Env

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

func EmptyEnv

func EmptyEnv() *Env

func EnclosedEnv

func EnclosedEnv(parent *Env) *Env

func (*Env) Define

func (e *Env) Define(ident string, value interface{})

func (*Env) Resolve

func (e *Env) Resolve(ident string) (interface{}, error)

type FuncMap added in v0.1.4

type FuncMap map[string]interface{}

type Node

type Node interface {
	fmt.Stringer
	Type() NodeType
	// contains filtered or unexported methods
}

func Parse

func Parse(r io.Reader) (Node, error)

func ParseWithEnv added in v0.4.0

func ParseWithEnv(r io.Reader, env *Env) (Node, error)

type NodeType added in v0.1.0

type NodeType int8
const (
	TypeLiteral NodeType = iota
	TypeTemplate
	TypeVariable
	TypeSlice
	TypeOption
	TypeArray
	TypeObject
	TypeEqual
	TypeCall
)

type Parser

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

func NewParser

func NewParser(r io.Reader) (*Parser, error)

func (*Parser) Parse

func (p *Parser) Parse() (Node, error)

type Position

type Position struct {
	Line int
	Col  int
}

func (Position) String

func (p Position) String() string

type Resolver added in v0.4.0

type Resolver interface {
	Resolve(string) (interface{}, error)
}

type Scanner

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

func Scan

func Scan(r io.Reader) (*Scanner, error)

func (*Scanner) Scan

func (s *Scanner) Scan() Token

type Setter

type Setter interface {
	Set(string) error
}

type Token

type Token struct {
	Literal     string
	Type        rune
	Interpolate bool
	Position
}

func (Token) Equal

func (t Token) Equal(other Token) bool

func (Token) String

func (t Token) String() string

type Updater added in v0.2.0

type Updater interface {
	Update(Resolver) error
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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