Open Policy Agent (OPA) Grammar

the grammar used in Open Policy Agent (OPA) for writing policies.

Grammar Components

ComponentDescription
moduleA collection of policies, includes package and policy definitions.
packageDefines the namespace for policies. Identified by a reference.
importImports definitions from another package, optionally with an alias.
policyA set of rules that define the policy.
ruleA unit in a policy, can be default, with a head and a body.
rule-headDeclarative part of a rule, includes name, arguments, and assignment.
rule-bodyProcedural part of the rule, consisting of a query.
queryA series of literals representing conditions or facts.
literalA basic unit in a query, can be a declaration, expression, or negation.
exprAn expression, can be a term, function call, or infix operation.
termThe simplest unit, can be various types like reference, array, object.
infix-operatorOperators for boolean, arithmetic, or binary operations.

This grammar is expressed in Extended Backus-Naur Form (EBNF).

  module          = package { import } policy
package         = "package" ref
import          = "import" package [ "as" var ]
policy          = { rule }
rule            = [ "default" ] rule-head { rule-body }
rule-head       = var [ "(" rule-args ")" ] [ "[" term "]" ] [ = term ]
rule-args       = term { "," term }
rule-body       = [ else [ = term ] ] "{" query "}"
query           = literal { ";" | [\r\n] literal }
literal         = ( some-decl | expr | "not" expr ) { with-modifier }
with-modifier   = "with" term "as" term
some-decl       = "some" var { "," var }
expr            = term | expr-built-in | expr-infix
expr-built-in   = var [ "." var ] "(" [ term { , term } ] ")"
expr-infix      = [ term "=" ] term infix-operator term
term            = ref | var | scalar | array | object | set | array-compr | object-compr | set-compr
array-compr     = "[" term "|" rule-body "]"
set-compr       = "{" term "|" rule-body "}"
object-compr    = "{" object-item "|" rule-body "}"
infix-operator  = bool-operator | arith-operator | bin-operator
bool-operator   = "=" | "!=" | "<" | ">" | ">=" | "<="
arith-operator  = "+" | "-" | "*" | "/"
bin-operator    = "&" | "|"
ref             = var { ref-arg }
ref-arg         = ref-arg-dot | ref-arg-brack
ref-arg-brack   = "[" ( scalar | var | array | object | set | "_" ) "]"
ref-arg-dot     = "." var
var             = ( ALPHA | "_" ) { ALPHA | DIGIT | "_" }
scalar          = string | NUMBER | TRUE | FALSE | NULL
string          = STRING | raw-string
raw-string      = "`" { CHAR-"`" } "`"
array           = "[" term { "," term } "]"
object          = "{" object-item { "," object-item } "}"
object-item     = ( scalar | ref | var ) ":" term
set             = empty-set | non-empty-set
non-empty-set   = "{" term { "," term } "}"
empty-set       = "set(" ")"
  

Last updated 05 Oct 2024, 18:17 +0530 . history