import { COORDINATION_RULES, FLOWCHART_RULES } from "#configuration/strings/bnf.fragment.strings"; import type { Section, Subsection } from "#types/document.types"; import { BNF_LANGUAGE } from "#configuration/constants/code.constants"; import { BNF_SECTION_ICON } from "#configuration/icons/grammar.icons"; import { BNF_SECTION_ID } from "#core/ids/grammar.ids"; import type { GrammarRule } from "#types/grammar.types"; import { joinRules } from "#core/converters/grammar.converter"; const BNF_TITLE = "BNF grammar"; const PLANNING_RULES: readonly GrammarRule[] = [ { grammar: ' ::= \n ::= "---" "---"\n ::= | ε\n ::= | ε\n ::= "THIS" \n ::= "AGENT" | "WORKFLOW" | "PROTOCOL" | "POLICY" | "CHECKLIST" | "TEMPLATE" | "TASK" | "INSTRUCTION" | "PROMPT" | "COMMAND" | "TEST"\n| "DEBUG" | "VERIFICATION" | "DISTILLATION" | "AUDIT" | "TRANSLATION" | "COMPOSITION"\n ::= "IS" | "ENFORCES" | "EXECUTES" | "HAS" | "PERFORMS" | "PROVIDES" | "IMPLEMENTS" | "DEFINES" | "MANAGES" | "COORDINATES" | "GENERATES"\n| "RESOLVES" | "FINDS" | "FIXES" | "VERIFIES" | "CLASSIFIES" | "DISTILLS" | "ABSTRACTS" | "ELIMINATES" | "AUDITS" | "MEASURES" | "SCORES" | "CORRECTS" | "RENDERS" | "COMPOSES" | "FOLDS"\n ::= \n ::= + * \n ::= | ε\n ::= | ε', title: "Instruction", }, { grammar: ' ::= "%%" "META" "%%" ":" +\n ::= "objective" ":" \n| "priority" ":" \n| "trust" ":" ("," )*\n| "jurisdiction" ":" "|" "external" ":" \n| "recursion_limit" ":" \n ::= (">" )*\n ::= "=" ("TRUSTED" | "UNTRUSTED" | )\n ::= ', title: "Meta block", }, { grammar: ' ::= * \n ::= "#" "NODE" "—" "[" "·" "·" "·" "yields:" "]"\n ::= +\n ::= \n ::= "epistemic" | "conative" | "evaluative"\n ::= "ontology" | "teleology" | "analysis" | "reasoning" | "formalisation" | "verification" | "representation" | "termination"\n ::= "set-theory" | "logic" | "graph" | "algebra" | "analysis" | "optimisation" | "topology" | "computation" | "probability" | "information-theory" | "dynamical-systems"\n ::= "set" | "boolean" | "edge-list" | "ranking" | "procedure" | "artifact" | \n ::= "@purpose" ":" \n| "@axis_question" ":" \n| "@cue" ":" \n| "@genesis" ":" \n| "@mandatory"\n ::= "existence" | "difference" | "relation" | "structure" | "transformation" | "constraint" | "emergence"', title: "Node", }, { grammar: ' ::= "CONTRACT" ":" "input" ":" "transform" ":" + "output" ":" \n ::= "NODE" | | | \n ::= ("preserves" ":" ) | ε\n ::= ("," )*\n ::= ("constraints" ":" ) | ε\n ::= ("freshness" ":" "+" ) | ε\n ::= \n ::= ("handoff" ":" ) | ε\n ::= ("#" "OUTPUT" "CONTRACT" +) | ε\n ::= "{" ("." )* "}"\n ::= ', title: "Contract", }, { grammar: ' ::= "HANDOFF" "GATE" ":" + \n ::= ("(" ")") | ε\n ::= ("rule_id" ":" "yields" ":" ) | ε\n ::= "(" "evidence" ":" ")" \n ::= "[check]" | "ASSERT" | "REQUIRE"\n ::= \n ::= | ε\n ::= "over" ":" "measured" ":" "/" \n ::= \n ::= | \n ::= ("refuse" ":" "before" ) | ε\n ::= | \n ::= ("standing" ":" "moved-set" ) | ε\n ::= "result" ":" "pass" "->" ("|" "->" "REPAIR" "(" "owner" ":" ")")+ "|" "unknown" "->" "BLOCKED"\n ::= "pass" | "fail" | "unknown"\n ::= "NODE" | "TERMINATE"\n ::= \n ::= "NODE" | \n ::= "#" "REPAIR" "EDGE" ', title: "Handoff gate", }, { grammar: ' ::= "#" "CROSS-NODE" "INVARIANTS" +\n ::= "INVARIANT" ":" "over" ":" "binds" ":" "objector" ":" ( | "none")\n ::= \n ::= \n ::= \n ::= "[check]" ', title: "Invariant block", }, { grammar: ' ::= "REPORT" ":" +\n ::= "subject" ":" \n| "verdict" ":" \n| "domain" ":" "declared" "measured" \n| "populations" ":" \n| "inputs" ":" \n| "code" ":" \n| "output" ":" \n| "refusals" ":" \n| "unresolved" ":" \n| "completion" ":" "saturated" "complete" "verified" \n ::= "NODE" \n ::= ("," )*\n ::= ("," )*\n ::= "[" ("," )* "]"', title: "Report block", }, { grammar: ' ::= "USE" "TEMPLATE" \n ::= ', title: "Macro" }, { grammar: ' ::= "RULE" ":" \n ::= \n ::= * +\n ::= "WHEN" ":" +', title: "Rule", }, ]; const STATEMENT_RULES: readonly GrammarRule[] = [ { grammar: ' ::= \n ::= \n| \n| \n| \n| \n| \n| \n| \n| \n| \n| \n| \n| \n| \n| \n| \n| \n| \n ::= | ε\n ::= "[" "]"\n ::= " " | "x" | ">"\n ::= | ε\n ::= ("@cue" ":" ) | ε', title: "Directive", }, { grammar: ' ::= * \n ::= \n| "EXECUTE" | "READ" | "WRITE" | "DELETE" | "REMOVE" | "ANALYZE"\n| "CREATE" | "FIND" | "REPORT" | "VALIDATE" | "VERIFY"\n| "COLLECT" | "EXTRACT" | "LINK" | "DETERMINE" | "CLASSIFY"\n| "INVESTIGATE" | "FILTER" | "COMPARE" | "CONVERT" | "MERGE" | "SPLIT" | "MARK"\n| "SORT" | "RANK" | "ORDER" | "INSERT" | "APPEND"\n| "ADD" | "MOVE" | "COPY" | "BACKUP" | "RESTORE" | "LOAD"\n| "ITERATE" | "ATTEMPT" | "ENFORCE" | "RENAME"\n| "FAIL" | "EXIT" | "RETURN" | "WAIT" | "SEND"\n| "REDUCE" | "PROPAGATE" | "FINALIZE" | "EVIDENCE"\n| "PREDICT" | "EXPLAIN" | "REFLECT" | "ABSTRACT" | "GENERALISE" | "DESCRIBE" | "FORMALISE"\n ::= "DISCOVER_RESOURCES" | "READ_RESOURCE" | "SEARCH_CONTENT" | "ANALYZE_CONTENT" | "EXTRACT_FACTS" | "CALCULATE_METRIC"\n| "COMPOSE_ARTIFACT" | "VALIDATE_ARTIFACT" | "PERSIST_ARTIFACT" | "EXECUTE_TOOL" | "REQUEST_DECISION" | "REPORT_RESULT"\n ::= "MUST" | "NEVER" | "ALWAYS" | "REQUIRED" | "MANDATORY"\n ::= | | \n ::= "SYSTEM" | "USER" | "SERVICE"\n ::= | | ε\n ::= "(" ")"\n ::= \n ::= ("," )*\n ::= | | ', title: "Action expression", }, { grammar: ' ::= \n| \n| \n| \n| \n| \n| \n ::= "IF" ":" +\n("ELSE" "IF" ":" +)*\n\n ::= ("ELSE" ":" +) | ε\n ::= "FOR" "EACH" "IN" ":" +\n ::= "WHILE" ":" +\n ::= "TRY" ":" +\n"CATCH" ":" +\n ::= | ε\n ::= \n ::= "GOTO" \n ::= ":"\n ::= \n ::= "START" \n| "LOOP" \n| "END"\n| "STOP"\n ::= | ε\n ::= ("BACKTO" ) | ε', title: "Control flow", }, { grammar: ' ::= "SET" "=" \n| "DECLARE" ":" \n ::= "string" | "number" | "boolean" | "array" | "object" | "file" | "context"', title: "Declaration statement", }, { grammar: ' ::= \n ::= "BACKUP" "TO" \n| "COPY" "TO" \n ::= "PERSIST_ARTIFACT" "TO" \n| "WRITE" \n| "EXECUTE_TOOL" "WITH" "timeout" ":" \n ::= \n ::= \n ::= "VALIDATE_ARTIFACT" "AGAINST" \n ::= | ε\n ::= \n ::= "CONTENT" \n| "FROM" \n| "INTO" \n| \n ::= \n ::= \n ::= ', title: "Transform statement", }, { grammar: ' ::= \n ::= "DISCOVER_RESOURCES" "INTO" \n| "SEARCH_CONTENT" "FOR" "INTO" \n| "FIND" "IN" \n ::= "IF" "exists" ":" +\n| "ANALYZE" \n ::= | ε\n ::= "IN" | ε\n ::= \n ::= \n ::= \n ::= ', title: "Discovery statement", }, ]; const EXPRESSION_RULES: readonly GrammarRule[] = [ { grammar: ' ::= \n ::= ("|>" )*\n ::= ("OR" )*\n ::= ("AND" )*\n ::= (("===" | "!==") )*\n ::= (("<" | ">" | "<=" | ">=") )*\n| "MATCHES" \n| "FOR" \n| "BETWEEN" \n ::= (("+" | "-") )*\n ::= (("*" | "/" | "%") )*\n ::= ("!" | "NOT" | "-" | "+") \n| \n ::= *\n ::= "[" "]"\n| "." \n| "(" ")"\n ::= \n| \n| \n| \n| "(" ")"\n ::= "[" "]"\n ::= "{" "}"\n ::= ("," )*\n ::= | ε\n ::= ":" \n ::= | | \n ::= ("," )*\n ::= | ε\n ::= "(" ")"\n ::= | ε\n ::= \n ::= \n ::= \n ::= \n ::= \n ::= | \n ::= "/" "/" \n ::= | ε\n ::= +\n ::= +\n ::= "===" | "!==" | "<" | ">" | "<=" | ">="', title: "Expressions", }, { grammar: ' ::= ( | | "_" | "-")*\n ::= \n ::= \n ::= \n ::= | \n ::= ("/" )* "/"\n ::= ("/" )* \n ::= \n ::= ("." ) | ε\n ::= | | \n ::= + \n ::= ("." +) | ε\n ::= \'"\' * \'"\' | "\'" * "\'"\n ::= "true" | "false"\n ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"\n ::= "a".."z" | "A".."Z"\n ::= \n ::= +\n ::= \n ::= | | | \n ::= "_" | "-" | "." | "," | ":" | ";" | "!" | "?" | "@" | "#" | "$" | "%" | "^" | "&" | "*" | "(" | ")" | "[" | "]" | "{" | "}" | "<" | ">" | "/" | "\\" | "|" | "=" | "+" | "`" | "~"\n ::= | | \n ::= " " | "\t" | "\n" | "\r"', title: "Lexical", }, { grammar: ' ::= \n ::= "FOR" "EACH" "IN" \n ::= ":" + \n ::= "APPEND" "TO" \n| "CREATE" "FROM" \n| "EXTRACT" "INTO" \n| "WRITE" "INTO" \n| "COLLECT" "INTO" \n| "SET" "=" \n ::= "recursion_limit" ":" \n ::= | ε\n ::= | ε\n ::= \n ::= | \n ::= \n ::= \n ::= \n ::= \n ::= \n ::= \n ::= \n ::= \n ::= ', title: "Iteration statement", }, { grammar: ' ::= "FUNCTION" "(" ")" ":" +\n ::= | ε\n ::= ("," )*\n ::= ', title: "Function declaration", }, { grammar: ' ::= "REPORT" \n ::= | ', title: "Announcement statement", }, ]; const category = function category(title: string, content: string, rules: readonly GrammarRule[]): Subsection { return { blocks: [{ code: joinRules(rules), kind: "code", language: BNF_LANGUAGE, title: BNF_TITLE }], content, title, }; }; export const BNF_SECTION: Section = { icon: BNF_SECTION_ICON, id: BNF_SECTION_ID, intro: "PAG is defined by a context-free grammar expressed in BNF notation, organised into five rule categories. The planning and coordination productions are the grammar's own records, each grounded to a reasoning record; the statement, expression and flowchart rules expand the terminals those records leave open. The scan under well-formedness reads tokens rather than patterns; the grammar itself admits a pattern literal in a condition.", subsections: [ category( "Planning rules", "Grammar for document structure, the meta block, nodes, contracts, handoff gates, invariant records and the report.", PLANNING_RULES, ), category("Statement rules", "Grammar for directives, actions, control flow and declarations.", STATEMENT_RULES), category( "Expression rules", "Grammar for expressions, operators, literals and lexical elements.", EXPRESSION_RULES, ), category( "Coordination rules", "Grammar for the structure declarations, the shared surface and the semantic operations.", COORDINATION_RULES, ), category("Flowchart rules", "Grammar for visual flow definitions in various formats.", FLOWCHART_RULES), ], title: "BNF grammar", };