parser

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrVariableInFact = errors.New("parser: a fact cannot contain any variables")
	ErrVariableInSet  = errors.New("parser: a set cannot contain any variables")
)
View Source
var BiscuitLexerRules = []lexer.Rule{
	{Name: "Keyword", Pattern: `check if|allow if|deny if`, Action: nil},
	{Name: "Function", Pattern: `prefix|suffix|matches|length|contains`, Action: nil},
	{Name: "Hex", Pattern: `hex:`, Action: nil},
	{Name: "Dot", Pattern: `\.`, Action: nil},
	{Name: "Arrow", Pattern: `<-`, Action: nil},
	{Name: "Or", Pattern: `\|\|`, Action: nil},
	{Name: "Operator", Pattern: `==|>=|<=|>|<|not|in`, Action: nil},
	{Name: "Comment", Pattern: `//[^\n]*`, Action: nil},
	{Name: "String", Pattern: `\"[^\"]*\"`, Action: nil},
	{Name: "Variable", Pattern: `\$[a-zA-Z0-9_]+`, Action: nil},
	{Name: "DateTime", Pattern: `\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(Z|([-+]\d\d:\d\d))?`},
	{Name: "Int", Pattern: `[0-9]+`, Action: nil},
	{Name: "Bool", Pattern: `true|false`, Action: nil},
	{Name: "Ident", Pattern: `[a-zA-Z0-9_:]+`, Action: nil},
	{Name: "Whitespace", Pattern: `[ \t]+`, Action: nil},
	{Name: "EOL", Pattern: `[\n\r]+`, Action: nil},
	{Name: "Punct", Pattern: `[-[!@%^&#$*()+_={}\|:;"'<,>.?/]|]`, Action: nil},
}
View Source
var DefaultParserOptions = []participle.Option{
	participle.Lexer(lexer.MustSimple(BiscuitLexerRules)),
	participle.UseLookahead(1),
	participle.Elide("Whitespace", "EOL"),
	participle.Unquote("String"),
}

Functions

func FromStringCheck

func FromStringCheck(input string) (biscuit.Check, error)

func FromStringFact

func FromStringFact(input string) (biscuit.Fact, error)

func FromStringPolicy

func FromStringPolicy(input string) (biscuit.Policy, error)

func FromStringRule

func FromStringRule(input string) (biscuit.Rule, error)

Types

type Allow

type Allow struct {
	Queries []*CheckQuery `"allow if" @@ ( "or" @@ )*`
}

type Bool

type Bool bool

func (*Bool) Capture

func (b *Bool) Capture(values []string) error

type Check

type Check struct {
	Queries []*CheckQuery `"check if" @@ ( "or" @@ )*`
}

func (*Check) ToBiscuit

func (c *Check) ToBiscuit() (*biscuit.Check, error)

type CheckQuery

type CheckQuery struct {
	Body []*RuleElement `@@ ("," @@)*`
}

func (*CheckQuery) ToBiscuit

func (r *CheckQuery) ToBiscuit() (*biscuit.Rule, error)

type Comment

type Comment string

func (*Comment) Capture

func (c *Comment) Capture(values []string) error

type Deny

type Deny struct {
	Queries []*CheckQuery `"deny if" @@ ( "or" @@ )*`
}

type Expr1

type Expr1 struct {
	Left  *Expr2     `@@`
	Right []*OpExpr2 `@@*`
}

func (*Expr1) ToExpr

func (e *Expr1) ToExpr(expr *biscuit.Expression)

type Expr2

type Expr2 struct {
	Left  *Expr3     `@@`
	Right []*OpExpr3 `@@*`
}

func (*Expr2) ToExpr

func (e *Expr2) ToExpr(expr *biscuit.Expression)

type Expr3

type Expr3 struct {
	Left  *Expr4     `@@`
	Right []*OpExpr4 `@@*`
}

func (*Expr3) ToExpr

func (e *Expr3) ToExpr(expr *biscuit.Expression)

type Expr4

type Expr4 struct {
	Left  *Expr5     `@@`
	Right []*OpExpr5 `@@*`
}

func (*Expr4) ToExpr

func (e *Expr4) ToExpr(expr *biscuit.Expression)

type Expr5

type Expr5 struct {
	Left  *ExprTerm  `@@`
	Right []*OpExpr5 `@@*`
}

func (*Expr5) ToExpr

func (e *Expr5) ToExpr(expr *biscuit.Expression)

type ExprTerm

type ExprTerm struct {
	Unary *Unary `@@`
	Term  *Term  `|@@`
}

func (*ExprTerm) ToExpr

func (e *ExprTerm) ToExpr(expr *biscuit.Expression)

type Expression

type Expression struct {
	Left  *Expr1     `@@`
	Right []*OpExpr1 `@@*`
}

func (*Expression) ToExpr

func (e *Expression) ToExpr(expr *biscuit.Expression)

type HexString

type HexString string

func (*HexString) Decode

func (h *HexString) Decode() ([]byte, error)

func (*HexString) Parse

func (h *HexString) Parse(lex *lexer.PeekingLexer) error

func (*HexString) String

func (h *HexString) String() string

type Length

type Length struct {
	Term *Term `@@ Dot "length()"`
}

type MustParser

type MustParser interface {
	Fact(fact string) biscuit.Fact
	Rule(rule string) biscuit.Rule
	Check(check string) biscuit.Check
	Policy(policy string) biscuit.Policy
}

type Negate

type Negate struct {
	Expr5 *Expr5 `"!" @@`
}

type OpExpr1

type OpExpr1 struct {
	Operator Operator `@("&&" | "||")`
	Expr2    *Expr2   `@@`
}

func (*OpExpr1) ToExpr

func (e *OpExpr1) ToExpr(expr *biscuit.Expression)

type OpExpr2

type OpExpr2 struct {
	Operator Operator `@("<=" | ">=" | "<" | ">" | "==")`
	Expr3    *Expr3   `@@`
}

func (*OpExpr2) ToExpr

func (e *OpExpr2) ToExpr(expr *biscuit.Expression)

type OpExpr3

type OpExpr3 struct {
	Operator Operator `@("+" | "-")`
	Expr4    *Expr4   `@@`
}

func (*OpExpr3) ToExpr

func (e *OpExpr3) ToExpr(expr *biscuit.Expression)

type OpExpr4

type OpExpr4 struct {
	Operator Operator `@("*" | "/")`
	Expr5    *Expr5   `@@`
}

func (*OpExpr4) ToExpr

func (e *OpExpr4) ToExpr(expr *biscuit.Expression)

type OpExpr5

type OpExpr5 struct {
	Operator   Operator      `Dot @("contains" | "starts_with" | "ends_with" | "matches" | "intersection" | "union" | "length")`
	Expression []*Expression `"("  @@* ")"`
}

func (*OpExpr5) ToExpr

func (e *OpExpr5) ToExpr(expr *biscuit.Expression)

type Operator

type Operator int
const (
	OpMul Operator = iota
	OpDiv
	OpAdd
	OpSub
	OpAnd
	OpOr
	OpLessOrEqual
	OpGreaterOrEqual
	OpLessThan
	OpGreaterThan
	OpEqual
	OpContains
	OpPrefix
	OpSuffix
	OpMatches
	OpIntersection
	OpUnion
	OpLength
)

func (*Operator) Capture

func (o *Operator) Capture(s []string) error

func (*Operator) ToExpr

func (op *Operator) ToExpr(expr *biscuit.Expression)

type Parens

type Parens struct {
	Expression *Expression `"("  @@ ")"`
}

type Parser

type Parser interface {
	Fact(fact string) (biscuit.Fact, error)
	Rule(rule string) (biscuit.Rule, error)
	Check(check string) (biscuit.Check, error)
	Policy(policy string) (biscuit.Policy, error)

	Must() MustParser
}

func New

func New() Parser

type Policy

type Policy struct {
	Allow *Allow `@@`
	Deny  *Deny  `|@@`
}

func (*Policy) ToBiscuit

func (p *Policy) ToBiscuit() (*biscuit.Policy, error)

type Predicate

type Predicate struct {
	Name *string `@Ident`
	IDs  []*Term `"(" (@@ ("," @@)*)* ")"`
}

func (*Predicate) ToBiscuit

func (p *Predicate) ToBiscuit() (*biscuit.Predicate, error)

type Rule

type Rule struct {
	Comments []*Comment     `@Comment*`
	Head     *Predicate     `@@`
	Body     []*RuleElement `"<-" @@ ("," @@)*`
}

func (*Rule) ToBiscuit

func (r *Rule) ToBiscuit() (*biscuit.Rule, error)

type RuleElement

type RuleElement struct {
	Predicate  *Predicate  `@@`
	Expression *Expression `|@@`
}

type Set

type Set struct {
	Not    bool        `@"not"? "in"`
	Bytes  []HexString `("[" ( @@ ("," @@)*)+ "]"`
	String []string    `| "[" (@String ("," @String)*)+ "]"`
	Int    []int64     `| "[" (@Int ("," @Int)*)+ "]")`
}

type Term

type Term struct {
	Variable *Variable  `@Variable`
	Bytes    *HexString `| @@`
	String   *string    `| @String`
	Date     *string    `| @DateTime`
	Integer  *int64     `| @Int`
	Bool     *Bool      `| @Bool`
	Set      []*Term    `| "[" @@ ("," @@)* "]"`
}

func (*Term) ToBiscuit

func (a *Term) ToBiscuit() (biscuit.Term, error)

type Unary

type Unary struct {
	Negate *Negate `@@`
	Parens *Parens `|@@`
}

type Value

type Value struct {
	Number        *float64    `  @(Float|Int)`
	Variable      *string     `| @Ident`
	Subexpression *Expression `| "(" @@ ")"`
}

type Variable

type Variable string

func (*Variable) Capture

func (v *Variable) Capture(values []string) error

Jump to

Keyboard shortcuts

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