rego-cpp 0.4.5
A C++ implementation of the Rego language and runtime
Loading...
Searching...
No Matches
rego Namespace Reference

Classes

class  BigInt
 
struct  BuiltInDef
 
class  BuiltInsDef
 
class  Interpreter
 
class  UnwrapOpt
 
struct  UnwrapResult
 

Typedefs

using BuiltIn = std::shared_ptr<BuiltInDef>
 
using BuiltInBehavior = std::function<Node(const Nodes&)>
 
using BuiltIns = std::shared_ptr<BuiltInsDef>
 

Enumerations

enum class  LogLevel : char {
  None = REGO_LOG_LEVEL_NONE , Error = REGO_LOG_LEVEL_ERROR , Output = REGO_LOG_LEVEL_OUTPUT , Warn = REGO_LOG_LEVEL_WARN ,
  Info = REGO_LOG_LEVEL_INFO , Debug = REGO_LOG_LEVEL_DEBUG , Trace = REGO_LOG_LEVEL_TRACE
}
 

Functions

Node unwrap_arg (const Nodes &args, const UnwrapOpt &options)
 
UnwrapResult unwrap (const Node &term, const Token &type)
 
UnwrapResult unwrap (const Node &term, const std::set< Token > &types)
 
BigInt get_int (const Node &node)
 
double get_double (const Node &node)
 
std::optional< BigInttry_get_int (const Node &node)
 
std::string get_string (const Node &node)
 
bool get_bool (const Node &node)
 
std::optional< Node > try_get_item (const Node &node, const std::string_view &key)
 
Node scalar (BigInt value)
 
Node scalar (double value)
 
Node scalar (bool value)
 
Node scalar (const char *value)
 
Node scalar (const std::string &value)
 
Node scalar ()
 
Node object_item (const Node &key_term, const Node &val_term)
 
Node object (const Nodes &object_items)
 
Node array (const Nodes &array_members)
 
Node set (const Nodes &set_members)
 
void set_tzdata_path (const std::filesystem::path &path)
 
Node err (NodeRange &r, const std::string &msg, const std::string &code=UnknownError)
 
Node err (Node node, const std::string &msg, const std::string &code=UnknownError)
 
Node version ()
 
std::string to_key (const trieste::Node &node, bool set_as_array=false, bool sort_arrays=false)
 
void set_log_level (LogLevel level)
 
std::string set_log_level_from_string (const std::string &level)
 
Reader reader (bool v1_compatible=false)
 
Rewriter to_input ()
 
Rewriter unify (BuiltIns builtins=BuiltInsDef::create())
 
Rewriter from_json (bool as_term=false)
 
Rewriter to_json ()
 
Rewriter to_yaml ()
 

Variables

const auto Module = TokenDef("rego-module", flag::symtab)
 
const auto Package = TokenDef("rego-package")
 
const auto Policy = TokenDef("rego-policy")
 
const auto Rule = TokenDef("rego-rule", flag::symtab)
 
const auto RuleHead = TokenDef("rego-rulehead")
 
const auto RuleHeadComp = TokenDef("rego-ruleheadcomp")
 
const auto RuleHeadFunc = TokenDef("rego-ruleheadfunc")
 
const auto RuleHeadSet = TokenDef("rego-ruleheadset")
 
const auto RuleHeadObj = TokenDef("rego-ruleheadobj")
 
const auto RuleArgs = TokenDef("rego-ruleargs")
 
const auto Query = TokenDef("rego-query")
 
const auto Literal = TokenDef("rego-literal")
 
const auto Expr = TokenDef("rego-expr")
 
const auto ExprInfix = TokenDef("rego-exprinfix")
 
const auto ExprCall = TokenDef("rego-exprcall")
 
const auto ExprEvery = TokenDef("rego-exprevery")
 
const auto ExprParens = TokenDef("rego-exprparens")
 
const auto UnaryExpr = TokenDef("rego-unaryexpr")
 
const auto NotExpr = TokenDef("rego-not-expr")
 
const auto Term = TokenDef("rego-term")
 
const auto InfixOperator = TokenDef("rego-infixoperator")
 
const auto BoolOperator = TokenDef("rego-booloperator")
 
const auto ArithOperator = TokenDef("rego-arithoperator")
 
const auto AssignOperator = TokenDef("rego-assignoperator")
 
const auto BinOperator = TokenDef("rego-binoperator")
 
const auto Ref = TokenDef("rego-ref")
 
const auto RefArgBrack = TokenDef("rego-refargbrack")
 
const auto RefArgDot = TokenDef("rego-refargdot")
 
const auto Var = TokenDef("rego-var", flag::print)
 
const auto Scalar = TokenDef("rego-scalar")
 
const auto String = TokenDef("rego-string")
 
const auto Array = TokenDef("rego-array")
 
const auto Object = TokenDef("rego-object")
 
const auto Set = TokenDef("rego-set")
 
const auto ObjectItem = TokenDef("rego-objectitem")
 
const auto RawString = TokenDef("rego-rawstring", flag::print)
 
const auto JSONString = TokenDef("rego-STRING", flag::print)
 
const auto Int = TokenDef("rego-INT", flag::print)
 
const auto Float = TokenDef("rego-FLOAT", flag::print)
 
const auto True = TokenDef("rego-true")
 
const auto False = TokenDef("rego-false")
 
const auto Null = TokenDef("rego-null")
 
const auto Equals = TokenDef("rego-equals")
 
const auto NotEquals = TokenDef("rego-notequals")
 
const auto LessThan = TokenDef("rego-lessthan")
 
const auto GreaterThan = TokenDef("rego-greaterthan")
 
const auto LessThanOrEquals = TokenDef("rego-lessthanorequals")
 
const auto GreaterThanOrEquals = TokenDef("rego-greaterthanorequals")
 
const auto Add = TokenDef("rego-add")
 
const auto Subtract = TokenDef("rego-subtract")
 
const auto Multiply = TokenDef("rego-multiply")
 
const auto Divide = TokenDef("rego-divide")
 
const auto Modulo = TokenDef("rego-modulo")
 
const auto And = TokenDef("rego-and")
 
const auto Or = TokenDef("rego-or")
 
const auto Assign = TokenDef("rego-assign")
 
const auto Unify = TokenDef("rego-unify")
 
const auto Default = TokenDef("rego-default")
 
const auto Some = TokenDef("rego-some")
 
const auto SomeDecl = TokenDef("rego-somedecl")
 
const auto If = TokenDef("rego-if")
 
const auto IsIn = TokenDef("rego-in")
 
const auto Contains = TokenDef("rego-contains")
 
const auto Else = TokenDef("rego-else")
 
const auto As = TokenDef("rego-as")
 
const auto With = TokenDef("rego-with")
 
const auto Every = TokenDef("rego-every")
 
const auto ArrayCompr = TokenDef("rego-arraycompr")
 
const auto ObjectCompr = TokenDef("rego-objectcompr")
 
const auto SetCompr = TokenDef("rego-setcompr")
 
const auto Membership = TokenDef("rego-membership")
 
const auto Not = TokenDef("rego-not")
 
const auto Import
 
const auto Placeholder = TokenDef("rego-placeholder")
 
const auto Version = TokenDef("rego-version", flag::print)
 
const auto RuleBodySeq = TokenDef("rego-rulebodyseq")
 
const auto ImportSeq = TokenDef("rego-importseq")
 
const auto RuleRef = TokenDef("rego-ruleref")
 
const auto RuleHeadType = TokenDef("rego-ruleheadtype")
 
const auto WithSeq = TokenDef("rego-withseq")
 
const auto TermSeq = TokenDef("rego-termseq")
 
const auto ExprSeq = TokenDef("rego-exprseq")
 
const auto VarSeq = TokenDef("rego-varseq")
 
const auto RefHead = TokenDef("rego-refhead")
 
const auto RefArgSeq = TokenDef("rego-refargseq")
 
const auto Key = TokenDef("rego-key", flag::print)
 
const auto Val = TokenDef("rego-value")
 
const auto Undefined = TokenDef("rego-undefined")
 
const auto Results = TokenDef("rego-results", flag::symtab)
 
const auto Result = TokenDef("rego-result", flag::symtab)
 
const auto Bindings = TokenDef("rego-bindings")
 
const auto Terms = TokenDef("rego-terms")
 
const auto Binding = TokenDef("rego-binding", flag::lookdown)
 
const auto ErrorCode = TokenDef("rego-errorcode")
 
const auto ErrorSeq = TokenDef("rego-errorseq")
 
const auto wf_assign_op = Assign | Unify
 
const auto wf_arith_op = Add | Subtract | Multiply | Divide | Modulo
 
const auto wf_bin_op = And | Or | Subtract
 
const auto wf_bool_op
 
const auto wf_exprs
 
const auto wf
 
const auto wf_result
 
const std::size_t AnyArity = std::numeric_limits<std::size_t>::max()
 
const std::string UnknownError = "unknown_error"
 
const std::string EvalTypeError = "eval_type_error"
 
const std::string EvalBuiltInError = "eval_builtin_error"
 
const std::string RegoTypeError = "rego_type_error"
 
const std::string RegoParseError = "rego_parse_error"
 
const std::string RegoCompileError = "rego_compile_error"
 
const std::string EvalConflictError = "eval_conflict_error"
 
const std::string WellFormedError = "wellformed_error"
 
const std::string RuntimeError = "runtime_error"
 
const std::string RecursionError = "rego_recursion_error"
 
const std::string DefaultVersion = "v0"
 

Detailed Description

This namespace provides the C++ API for the library. It includes all the token types for nodes in the AST, the well-formedness definitions for each pass, the built-in system and custom types for handling various kinds of data (e.g. the BigInt class).

Typedef Documentation

◆ BuiltIn

using rego::BuiltIn = std::shared_ptr<BuiltInDef>

A pointer to a BuiltInDef.

◆ BuiltInBehavior

using rego::BuiltInBehavior = std::function<Node(const Nodes&)>

The function pointer to the behavior of the built-in.

Enumeration Type Documentation

◆ LogLevel

enum class rego::LogLevel : char
strong

The logging level.

Function Documentation

◆ array()

Node rego::array ( const Nodes & array_members)

Converts the value to an array node.

Parameters
array_membersThe members of the array.
Returns
The array node (array (term) (term) ...)

◆ err() [1/2]

Node rego::err ( Node node,
const std::string & msg,
const std::string & code = UnknownError )

Generates an error node.

Parameters
nodeThe node for which the error occurred.
msgThe error message.
codeThe error code.

◆ err() [2/2]

Node rego::err ( NodeRange & r,
const std::string & msg,
const std::string & code = UnknownError )

Generates an error node.

Parameters
rThe range of nodes over which the error occurred.
msgThe error message.
codeThe error code.

◆ from_json()

Rewriter rego::from_json ( bool as_term = false)

Rewrites a JSON AST to a Rego data input AST.

◆ get_bool()

bool rego::get_bool ( const Node & node)

Extracts the value of a node as a boolean.

Parameters
nodeThe node to extract from.
Returns
The boolean value of the node.

◆ get_double()

double rego::get_double ( const Node & node)

Extracts the value of a node as a double.

Parameters
nodeThe node to extract from.
Returns
The double value of the node.

◆ get_int()

BigInt rego::get_int ( const Node & node)

Extracts the value of a node as an integer.

Parameters
nodeThe node to extract from.
Returns
The integer value of the node.

◆ get_string()

std::string rego::get_string ( const Node & node)

Extracts the value of a node as a string.

The resulting string will have any enclosing quotes removed.

Parameters
nodeThe node to extract from.
Returns
The string value of the node.

◆ object()

Node rego::object ( const Nodes & object_items)

Converts the value to an object node.

Parameters
object_itemsThe object items of the object
Returns
The object node (object (object_item) (object_item) ...)

◆ object_item()

Node rego::object_item ( const Node & key_term,
const Node & val_term )

Converts the key and val terms to an object item.

Parameters
key_termThe key term.
val_termThe value term.
Returns
The object item node (object_item (key_term) (val_term))

◆ reader()

Reader rego::reader ( bool v1_compatible = false)

Parses Rego queries and virtual documents.

◆ scalar() [1/6]

Node rego::scalar ( )

Converts the value to a scalar node.

Parameters
valueThe value to convert.
Returns
The scalar node (scalar (NULL)))

◆ scalar() [2/6]

Node rego::scalar ( BigInt value)

Converts the value to a scalar node.

Parameters
valueThe value to convert.
Returns
The scalar node (scalar (INT

)))

◆ scalar() [3/6]

Node rego::scalar ( bool value)

Converts the value to a scalar node.

Parameters
valueThe value to convert.
Returns
The scalar node (scalar (TRUE|FALSE)))

◆ scalar() [4/6]

Node rego::scalar ( const char * value)

Converts the value to a scalar node.

Parameters
valueThe value to convert.
Returns
The scalar node (scalar (STRING

)))

◆ scalar() [5/6]

Node rego::scalar ( const std::string & value)

Converts the value to a scalar node.

Parameters
valueThe value to convert.
Returns
The scalar node (scalar (STRING

)))

◆ scalar() [6/6]

Node rego::scalar ( double value)

Converts the value to a scalar node.

Parameters
valueThe value to convert.
Returns
The scalar node (scalar (FLOAT

)))

◆ set()

Node rego::set ( const Nodes & set_members)

Converts the value to a set node.

Parameters
set_membersThe members of the set.
Returns
The set node (set (term) (term) ...)

◆ set_log_level()

void rego::set_log_level ( LogLevel level)

Sets the logging level for the library.

Parameters
levelThe logging level.

◆ set_log_level_from_string()

std::string rego::set_log_level_from_string ( const std::string & level)

Sets the logging level for the library.

Parameters
levelThe logging level. One of "None", "Error", "Output", "Warn", "Info", "Debug", or "Trace".
Returns
Error message if the level is invalid, otherwise empty string.

◆ set_tzdata_path()

void rego::set_tzdata_path ( const std::filesystem::path & path)

Sets the location where rego-cpp will look for timezone database information.

The timezone database will be interpreted as one obtained from the IANA (https://www.iana.org/time-zones) which has been downloaded and unpacked into at the path provided. If the library was built without manual tzdata support, this function will throw an exception.

Parameters
pathThe path to the timezone database.

◆ to_input()

Rewriter rego::to_input ( )

Rewrites a Query AST to an input term.

◆ to_json()

Rewriter rego::to_json ( )

Rewrites a Rego binding term to a JSON AST.

◆ to_key()

std::string rego::to_key ( const trieste::Node & node,
bool set_as_array = false,
bool sort_arrays = false )

Converts a node to a unique key representation.

Parameters
nodeThe node to convert.
set_as_arrayWhether to represent sets as arrays.

◆ to_yaml()

Rewriter rego::to_yaml ( )

Rewrites a Rego binding term to a YAML AST.

◆ try_get_int()

std::optional< BigInt > rego::try_get_int ( const Node & node)

Attempts to extract the value of a node as an integer. In the case that the node is a double, it will check if the double is integral, i.e. 5.0 will be considered the integer 5.

Parameters
nodeThe node to extract from.
Returns
The integer value of the node

◆ try_get_item()

std::optional< Node > rego::try_get_item ( const Node & node,
const std::string_view & key )

Tries to get an item within an object using its key.

Parameters
nodeThe object to search.
keyThe key to search for.

◆ unify()

Rewriter rego::unify ( BuiltIns builtins = BuiltInsDef::create())

Unifies a set of base and virtual documents, an input term, and a query.

◆ unwrap() [1/2]

UnwrapResult rego::unwrap ( const Node & term,
const std::set< Token > & types )

Attempts to unwrap an argument to one of the specified types.

Parameters
termThe term to unwrap.
typesThe acceptable types.
Returns
An unwrap result.

◆ unwrap() [2/2]

UnwrapResult rego::unwrap ( const Node & term,
const Token & type )

Attempts to unwrap an argument to a specified type.

Parameters
termThe term to unwrap.
typeThe acceptable type.
Returns
An unwrap result.

◆ unwrap_arg()

Node rego::unwrap_arg ( const Nodes & args,
const UnwrapOpt & options )

Unwraps an argument from the provided vector of nodes.

Parameters
argsThe vector of nodes to unwrap from.
optionsThe options for unwrapping.
Returns
The unwrapped argument or an appropriate error node.

◆ version()

Node rego::version ( )

Returns a node representing the version of the library.

The resulting node will be an object containing:

  • The Git commit hash ("commit")
  • The library version ("regocpp_version")
  • The supported OPA Rego version ("version")
  • The environment variables ("env")

Variable Documentation

◆ AnyArity

const std::size_t rego::AnyArity = std::numeric_limits<std::size_t>::max()

This constant indicates that a built-in can receive any number of arguments.

◆ Import

const auto rego::Import
inline
Initial value:
=
TokenDef("rego-import", flag::lookdown | flag::lookup | flag::shadowing)

◆ wf_bool_op

const auto rego::wf_bool_op
inline
Initial value:
= Equals | NotEquals | LessThan |
LessThanOrEquals | GreaterThan | GreaterThanOrEquals | Not

◆ wf_exprs

const auto rego::wf_exprs
inline
Initial value:
=
Term | ExprCall | ExprInfix | ExprEvery | ExprParens | UnaryExpr

◆ wf_result

const auto rego::wf_result
inline
Initial value:
=
(Top <<= Results | Undefined)
| (Results <<= Result++[1])
| (Result <<= Terms * Bindings)
| (Terms <<= Term++)
| (Bindings <<= Binding++)
| (Binding <<= (Key >>= Var) * (Val >>= Term))[Key]
| (Term <<= Scalar | Array | Object | Set)
| (Array <<= Term++)
| (Set <<= Term++)
| (Object <<= ObjectItem++)
| (ObjectItem <<= (Key >>= Term) * (Val >>= Term))
| (Scalar <<= JSONString | Int | Float | True | False | Null)
| (Error <<= ErrorMsg * ErrorAst * ErrorCode)