|
| 1 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | +# SPDX-FileCopyrightText: 2025 Phronesis Contributors |
| 3 | + |
| 4 | +defmodule Phronesis.AST do |
| 5 | + @moduledoc """ |
| 6 | + Abstract Syntax Tree node definitions for Phronesis. |
| 7 | +
|
| 8 | + The AST follows the grammar specification with these node types: |
| 9 | +
|
| 10 | + ## Top-Level Declarations |
| 11 | +
|
| 12 | + - `{:policy, name, condition, action, metadata}` - Policy declaration |
| 13 | + - `{:import, module_path, alias}` - Import statement |
| 14 | + - `{:const, name, expression}` - Constant declaration |
| 15 | +
|
| 16 | + ## Expressions |
| 17 | +
|
| 18 | + - `{:binary_op, op, left, right}` - Binary operations (+, -, *, /, AND, OR) |
| 19 | + - `{:unary_op, op, operand}` - Unary operations (NOT, -) |
| 20 | + - `{:comparison, op, left, right}` - Comparisons (==, !=, >, <, etc.) |
| 21 | + - `{:module_call, path, args}` - Module function call |
| 22 | + - `{:identifier, name}` - Variable reference |
| 23 | + - `{:literal, type, value}` - Literal values |
| 24 | +
|
| 25 | + ## Actions |
| 26 | +
|
| 27 | + - `{:execute, function, args}` - Execute action |
| 28 | + - `{:report, message}` - Report action |
| 29 | + - `{:reject, reason}` - Reject action |
| 30 | + - `{:accept, reason}` - Accept action |
| 31 | + - `{:block, actions}` - Compound action (BEGIN...END) |
| 32 | + - `{:conditional, condition, then_action, else_action}` - IF/THEN/ELSE |
| 33 | +
|
| 34 | + ## Metadata |
| 35 | +
|
| 36 | + - `%{priority: int, expires: :never | datetime, created_by: identifier}` |
| 37 | + """ |
| 38 | + |
| 39 | + @type name :: String.t() |
| 40 | + @type module_path :: [name()] |
| 41 | + |
| 42 | + @type literal_type :: :integer | :float | :string | :boolean | :ip_address | :datetime |
| 43 | + @type literal :: {:literal, literal_type(), any()} |
| 44 | + |
| 45 | + @type comparison_op :: :eq | :neq | :gt | :gte | :lt | :lte | :in |
| 46 | + @type binary_op :: :add | :sub | :mul | :div | :and | :or |
| 47 | + @type unary_op :: :not | :neg |
| 48 | + |
| 49 | + @type expression :: |
| 50 | + {:binary_op, binary_op(), expression(), expression()} |
| 51 | + | {:unary_op, unary_op(), expression()} |
| 52 | + | {:comparison, comparison_op(), expression(), expression()} |
| 53 | + | {:module_call, module_path(), [expression()]} |
| 54 | + | {:identifier, name()} |
| 55 | + | literal() |
| 56 | + |
| 57 | + @type action :: |
| 58 | + {:execute, name(), [expression()]} |
| 59 | + | {:report, expression()} |
| 60 | + | {:reject, expression() | nil} |
| 61 | + | {:accept, expression() | nil} |
| 62 | + | {:block, [action()]} |
| 63 | + | {:conditional, expression(), action(), action() | nil} |
| 64 | + |
| 65 | + @type metadata :: %{ |
| 66 | + priority: non_neg_integer(), |
| 67 | + expires: :never | String.t(), |
| 68 | + created_by: name() | nil |
| 69 | + } |
| 70 | + |
| 71 | + @type policy :: {:policy, name(), expression(), action(), metadata()} |
| 72 | + @type import_decl :: {:import, module_path(), name() | nil} |
| 73 | + @type const_decl :: {:const, name(), expression()} |
| 74 | + |
| 75 | + @type declaration :: policy() | import_decl() | const_decl() |
| 76 | + @type program :: [declaration()] |
| 77 | + |
| 78 | + @doc """ |
| 79 | + Create a policy node. |
| 80 | + """ |
| 81 | + @spec policy(name(), expression(), action(), metadata()) :: policy() |
| 82 | + def policy(name, condition, action, metadata) do |
| 83 | + {:policy, name, condition, action, metadata} |
| 84 | + end |
| 85 | + |
| 86 | + @doc """ |
| 87 | + Create an import node. |
| 88 | + """ |
| 89 | + @spec import_decl(module_path(), name() | nil) :: import_decl() |
| 90 | + def import_decl(path, alias_name \\ nil) do |
| 91 | + {:import, path, alias_name} |
| 92 | + end |
| 93 | + |
| 94 | + @doc """ |
| 95 | + Create a constant declaration node. |
| 96 | + """ |
| 97 | + @spec const_decl(name(), expression()) :: const_decl() |
| 98 | + def const_decl(name, value) do |
| 99 | + {:const, name, value} |
| 100 | + end |
| 101 | + |
| 102 | + @doc """ |
| 103 | + Create a binary operation node. |
| 104 | + """ |
| 105 | + @spec binary_op(binary_op(), expression(), expression()) :: expression() |
| 106 | + def binary_op(op, left, right) do |
| 107 | + {:binary_op, op, left, right} |
| 108 | + end |
| 109 | + |
| 110 | + @doc """ |
| 111 | + Create a comparison node. |
| 112 | + """ |
| 113 | + @spec comparison(comparison_op(), expression(), expression()) :: expression() |
| 114 | + def comparison(op, left, right) do |
| 115 | + {:comparison, op, left, right} |
| 116 | + end |
| 117 | + |
| 118 | + @doc """ |
| 119 | + Create a unary operation node. |
| 120 | + """ |
| 121 | + @spec unary_op(unary_op(), expression()) :: expression() |
| 122 | + def unary_op(op, operand) do |
| 123 | + {:unary_op, op, operand} |
| 124 | + end |
| 125 | + |
| 126 | + @doc """ |
| 127 | + Create a module call node. |
| 128 | + """ |
| 129 | + @spec module_call(module_path(), [expression()]) :: expression() |
| 130 | + def module_call(path, args) do |
| 131 | + {:module_call, path, args} |
| 132 | + end |
| 133 | + |
| 134 | + @doc """ |
| 135 | + Create an identifier node. |
| 136 | + """ |
| 137 | + @spec identifier(name()) :: expression() |
| 138 | + def identifier(name) do |
| 139 | + {:identifier, name} |
| 140 | + end |
| 141 | + |
| 142 | + @doc """ |
| 143 | + Create a literal node. |
| 144 | + """ |
| 145 | + @spec literal(literal_type(), any()) :: literal() |
| 146 | + def literal(type, value) do |
| 147 | + {:literal, type, value} |
| 148 | + end |
| 149 | + |
| 150 | + @doc """ |
| 151 | + Create an execute action node. |
| 152 | + """ |
| 153 | + @spec execute(name(), [expression()]) :: action() |
| 154 | + def execute(function, args \\ []) do |
| 155 | + {:execute, function, args} |
| 156 | + end |
| 157 | + |
| 158 | + @doc """ |
| 159 | + Create a report action node. |
| 160 | + """ |
| 161 | + @spec report(expression()) :: action() |
| 162 | + def report(message) do |
| 163 | + {:report, message} |
| 164 | + end |
| 165 | + |
| 166 | + @doc """ |
| 167 | + Create a reject action node. |
| 168 | + """ |
| 169 | + @spec reject(expression() | nil) :: action() |
| 170 | + def reject(reason \\ nil) do |
| 171 | + {:reject, reason} |
| 172 | + end |
| 173 | + |
| 174 | + @doc """ |
| 175 | + Create an accept action node. |
| 176 | + """ |
| 177 | + @spec accept(expression() | nil) :: action() |
| 178 | + def accept(reason \\ nil) do |
| 179 | + {:accept, reason} |
| 180 | + end |
| 181 | + |
| 182 | + @doc """ |
| 183 | + Create a block action node. |
| 184 | + """ |
| 185 | + @spec block([action()]) :: action() |
| 186 | + def block(actions) do |
| 187 | + {:block, actions} |
| 188 | + end |
| 189 | + |
| 190 | + @doc """ |
| 191 | + Create a conditional action node. |
| 192 | + """ |
| 193 | + @spec conditional(expression(), action(), action() | nil) :: action() |
| 194 | + def conditional(condition, then_action, else_action \\ nil) do |
| 195 | + {:conditional, condition, then_action, else_action} |
| 196 | + end |
| 197 | + |
| 198 | + @doc """ |
| 199 | + Create policy metadata. |
| 200 | + """ |
| 201 | + @spec metadata(keyword()) :: metadata() |
| 202 | + def metadata(opts \\ []) do |
| 203 | + %{ |
| 204 | + priority: Keyword.get(opts, :priority, 0), |
| 205 | + expires: Keyword.get(opts, :expires, :never), |
| 206 | + created_by: Keyword.get(opts, :created_by) |
| 207 | + } |
| 208 | + end |
| 209 | +end |
0 commit comments