parser

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: Apache-2.0 Imports: 9 Imported by: 5

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.SimpleRule{
	{Name: "Keyword", Pattern: `check if|allow if|deny if`},
	{Name: "Function", Pattern: `prefix|suffix|matches|length|contains`},
	{Name: "Hex", Pattern: `hex:([0-9a-fA-F]{2})*`},
	{Name: "Dot", Pattern: `\.`},
	{Name: "Arrow", Pattern: `<-`},
	{Name: "Or", Pattern: `\|\|`},
	{Name: "And", Pattern: `&&`},
	{Name: "Operator", Pattern: `==|>=|<=|>|<|\+|-|\*`},
	{Name: "Comment", Pattern: `//[^\n]*`},
	{Name: "String", Pattern: `\"[^\"]*\"`},
	{Name: "Variable", Pattern: `\$[a-zA-Z0-9_:]+`},
	{Name: "Parameter", Pattern: `\{[a-zA-Z0-9_:]+\}`},
	{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]+`},
	{Name: "Bool", Pattern: `true|false`},
	{Name: "Ident", Pattern: `[a-z][a-zA-Z0-9_:]*`},
	{Name: "Whitespace", Pattern: `[ \t]+`},
	{Name: "EOL", Pattern: `[\n\r]+`},
	{Name: "Punct", Pattern: `[-[!@%^&#$*()+_={}\|:;"'<,>.?/]|]`},
}
View Source
var DefaultParserOptions = []participle.Option{
	participle.Lexer(lexer.MustSimple(BiscuitLexerRules)),
	participle.UseLookahead(1),
	participle.Elide("Whitespace", "EOL"),
	participle.Unquote("String"),
}

Functions

func FromStringAuthorizer added in v2.2.0

func FromStringAuthorizer(input string) (biscuit.ParsedAuthorizer, error)

func FromStringAuthorizerWithParams added in v2.2.0

func FromStringAuthorizerWithParams(input string, parameters ParametersMap) (biscuit.ParsedAuthorizer, error)

func FromStringBlock added in v2.2.0

func FromStringBlock(input string) (biscuit.ParsedBlock, error)

func FromStringBlockWithParams added in v2.2.0

func FromStringBlockWithParams(input string, parameters ParametersMap) (biscuit.ParsedBlock, error)

func FromStringCheck

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

func FromStringCheckWithParams added in v2.2.0

func FromStringCheckWithParams(input string, parameters ParametersMap) (biscuit.Check, error)

func FromStringFact

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

func FromStringFactWithParams added in v2.2.0

func FromStringFactWithParams(input string, parameters ParametersMap) (biscuit.Fact, error)

func FromStringPolicy

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

func FromStringPolicyWithParams added in v2.2.0

func FromStringPolicyWithParams(input string, parameters ParametersMap) (biscuit.Policy, error)

func FromStringRule

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

func FromStringRuleWithParams added in v2.2.0

func FromStringRuleWithParams(input string, parameters ParametersMap) (biscuit.Rule, error)

Types

type Allow

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

type Authorizer added in v2.2.0

type Authorizer struct {
	Comments []*Comment           `@Comment*`
	Body     []*AuthorizerElement `(@@ ";")*`
}

func (*Authorizer) ToBiscuit added in v2.2.0

func (b *Authorizer) ToBiscuit(parameters ParametersMap) (*biscuit.ParsedAuthorizer, error)

type AuthorizerElement added in v2.2.0

type AuthorizerElement struct {
	Policy       *Policy       `@@`
	BlockElement *BlockElement `|@@`
}

type Block added in v2.2.0

type Block struct {
	Comments []*Comment      `@Comment*`
	Body     []*BlockElement `(@@ ";")*`
}

func (*Block) ToBiscuit added in v2.2.0

func (b *Block) ToBiscuit(parameters ParametersMap) (*biscuit.ParsedBlock, error)

type BlockElement added in v2.2.0

type BlockElement struct {
	Check     *Check         `@@`
	Predicate *Predicate     `|@@`
	RuleBody  []*RuleElement `("<-" @@ ("," @@)*)?`
}

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(parameters ParametersMap) (*biscuit.Check, error)

type CheckQuery

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

func (*CheckQuery) ToBiscuit

func (r *CheckQuery) ToBiscuit(parameters ParametersMap) (*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, parameters ParametersMap)

type Expr2

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

func (*Expr2) ToExpr

func (e *Expr2) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type Expr3

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

func (*Expr3) ToExpr

func (e *Expr3) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type Expr4

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

func (*Expr4) ToExpr

func (e *Expr4) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type Expr5

type Expr5 struct {
	Operator *Operator `@("!")?`
	Expr6    *Expr6    `@@`
}

func (*Expr5) ToExpr

func (e *Expr5) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type Expr6 added in v2.2.0

type Expr6 struct {
	Left  *ExprTerm  `@@`
	Right []*OpExpr7 `@@*`
}

func (*Expr6) ToExpr added in v2.2.0

func (e *Expr6) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type ExprTerm

type ExprTerm struct {
	Term       *Term       `@@`
	Expression *Expression `| "(" @@? ")"`
}

func (*ExprTerm) ToExpr

func (e *ExprTerm) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type Expression

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

func (*Expression) ToExpr

func (e *Expression) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

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 MustParser

type MustParser interface {
	Fact(fact string, parameters ParametersMap) biscuit.Fact
	Rule(rule string, parameters ParametersMap) biscuit.Rule
	Check(check string, parameters ParametersMap) biscuit.Check
	Policy(policy string, parameters ParametersMap) biscuit.Policy
	Block(block string, parameters ParametersMap) biscuit.ParsedBlock
	Authorizer(authorizer string, parameters ParametersMap) biscuit.ParsedAuthorizer
}

type OpExpr1

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

func (*OpExpr1) ToExpr

func (e *OpExpr1) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type OpExpr2

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

func (*OpExpr2) ToExpr

func (e *OpExpr2) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type OpExpr3

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

func (*OpExpr3) ToExpr

func (e *OpExpr3) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type OpExpr4

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

func (*OpExpr4) ToExpr

func (e *OpExpr4) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type OpExpr5

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

func (*OpExpr5) ToExpr

func (e *OpExpr5) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

type OpExpr7 added in v2.2.0

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

func (*OpExpr7) ToExpr added in v2.2.0

func (e *OpExpr7) ToExpr(expr *biscuit.Expression, parameters ParametersMap)

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
	OpNegate
)

func (*Operator) Capture

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

func (*Operator) ToExpr

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

type Parameter added in v2.2.0

type Parameter string

func (*Parameter) Capture added in v2.2.0

func (p *Parameter) Capture(values []string) error

type ParametersMap added in v2.2.0

type ParametersMap map[string]biscuit.Term

type Parser

type Parser interface {
	Fact(fact string, parameters ParametersMap) (biscuit.Fact, error)
	Rule(rule string, parameters ParametersMap) (biscuit.Rule, error)
	Check(check string, parameters ParametersMap) (biscuit.Check, error)
	Policy(policy string, parameters ParametersMap) (biscuit.Policy, error)
	Block(block string, parameters ParametersMap) (biscuit.ParsedBlock, error)
	Authorizer(authorizer string, parameters ParametersMap) (biscuit.ParsedAuthorizer, error)

	Must() MustParser
}

func New

func New() Parser

type Policy

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

func (*Policy) ToBiscuit

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

type Predicate

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

func (*Predicate) ToBiscuit

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

type Rule

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

func (*Rule) ToBiscuit

func (r *Rule) ToBiscuit(parameters ParametersMap) (*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 {
	Parameter *Parameter `@Parameter`
	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(parameters ParametersMap) (biscuit.Term, error)

type Value

type Value struct {
	Number        *float64    `  @(Float|Int)`
	Variable      *string     `| @Ident`
	Parameter     *Parameter  `| @Parameter`
	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