| 1 |
/* rocq-elpi: Helpers common to synterp and interp */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
shorten std.{fatal-error, fatal-error-w-data, debug-print}. |
| 6 |
|
| 7 |
:before "default-fatal-error" |
| 8 |
fatal-error M :- !, stop M. |
| 9 |
|
| 10 |
:before "default-fatal-error-w-data" |
| 11 |
fatal-error-w-data Msg Data :- !, |
| 12 |
term_to_string Data DataS, |
| 13 |
M is Msg ^ ": " ^ DataS, stop M. |
| 14 |
|
| 15 |
:before "default-debug-print" |
| 16 |
debug-print M Data :- !, coq.debug M Data. |
| 17 |
|
| 18 |
% HACK: elpi's stop has no argument |
| 19 |
func stop string. |
| 20 |
:name "stop:begin" |
| 21 |
stop S :- coq.error S. % halt S |
| 22 |
|
| 23 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 24 |
|
| 25 |
func coq.parse-attributes list attribute, list attribute-signature -> list prop. |
| 26 |
|
| 27 |
% Coq attribute parser, eg [#[attribute=value] Command] |
| 28 |
% |
| 29 |
% Usage: |
| 30 |
% main _ :- |
| 31 |
% attributes A, % fetch |
| 32 |
% coq.parse-attributes A Spec Opts, % parse/validate |
| 33 |
% Opts => (mycode, get-option "foo" V, mycode). % use |
| 34 |
% |
| 35 |
% where [Opts] is a list of clauses [get-option StringName Value], where value |
| 36 |
% can have any type and [Spec] is a list of [attribute-sigmature]. |
| 37 |
% Example of an attribute signature: |
| 38 |
% [ |
| 39 |
% att "this" bool, |
| 40 |
% att "that.thing" int, |
| 41 |
% att "algebraic" (oneof ["foo" `-> foo-thing, "bar" `-> barbar]), |
| 42 |
% ] |
| 43 |
% |
| 44 |
% Env variable COQ_ELPI_ATTRIBUTES can be used to pass attributes to all |
| 45 |
% commands. These attributes names are prefixed by 'elpi.' and are of type |
| 46 |
% string. |
| 47 |
% |
| 48 |
% Eg. |
| 49 |
% COQ_ELPI_ATTRIBUTES=test=yes,str="some-string" coqc foo.v |
| 50 |
% results in commands in foo.v to receive |
| 51 |
% [ attribute "elpi.test" (leaf "yes") , |
| 52 |
% attribute "elpi.str" (leaf "some-string") | ...] |
| 53 |
% which are automatically accepted and give rise to |
| 54 |
% get-option "elpi.test" "yes" |
| 55 |
% get-option "elpi.str" "some-string" |
| 56 |
|
| 57 |
kind attribute-signature type. |
| 58 |
type att string -> attribute-type -> attribute-signature. |
| 59 |
type att-ignore-unknown attribute-signature. |
| 60 |
|
| 61 |
type supported-attribute attribute-signature -> prop. |
| 62 |
supported-attribute (att "elpi.loc" loc). |
| 63 |
supported-attribute (att Name string) :- rex_match "^elpi\\." Name. |
| 64 |
|
| 65 |
kind attribute-type type. |
| 66 |
type int attribute-type. |
| 67 |
type string attribute-type. |
| 68 |
type bool attribute-type. |
| 69 |
type oneof list attribute-mapping -> attribute-type. |
| 70 |
type attmap attribute-type. % #[map(k1="v1",k2="v2")] |
| 71 |
type attlist attribute-type. % #[set(b1,b2,b3)] |
| 72 |
type attlabel attribute-type. % #[label( a(..), b, .. )] if #[label(a, b), a(..), ..] |
| 73 |
type loc attribute-type. |
| 74 |
|
| 75 |
kind attribute-mapping type. |
| 76 |
type (`->) string -> any -> attribute-mapping. |
| 77 |
|
| 78 |
func coq.valid-str-attribute string, string -> option any, diagnostic. |
| 79 |
coq.valid-str-attribute Name Value V Diag :- |
| 80 |
if (supported-attribute (att Name Type)) |
| 81 |
(coq.typecheck-attribute Name Type Value LPV Diag, V = some LPV) |
| 82 |
(if (supported-attribute att-ignore-unknown) (V = none, Diag = ok) |
| 83 |
(Diag = error {calc ( "Attribute " ^ Name ^ " is not supported")})). |
| 84 |
func coq.valid-loc-attribute string, loc -> diagnostic. |
| 85 |
coq.valid-loc-attribute Name Loc Diag :- |
| 86 |
if (supported-attribute (att Name loc)) |
| 87 |
(if (primitive? Loc "loc") (Diag = ok) (Diag = error {calc ( "Attribute " ^ Name ^ " takes a loc, got " ^ {std.any->string Loc} ) } )) |
| 88 |
(if (supported-attribute att-ignore-unknown) (Diag = ok) |
| 89 |
(Diag = error {calc ( "Attribute " ^ Name ^ " is not supported")})). |
| 90 |
|
| 91 |
:index (_ 1 1) |
| 92 |
func coq.typecheck-attribute string, attribute-type, string -> any, diagnostic. |
| 93 |
|
| 94 |
coq.typecheck-attribute _ int Value V ok :- V is string_to_int Value, !. |
| 95 |
coq.typecheck-attribute N int Value _ (error Msg) :- |
| 96 |
Msg is "Attribute " ^ N ^ " takes an integer, got: " ^ Value. |
| 97 |
|
| 98 |
coq.typecheck-attribute _ string V V ok. |
| 99 |
|
| 100 |
coq.typecheck-attribute _ bool "true" tt ok :- !. |
| 101 |
coq.typecheck-attribute _ bool "tt" tt ok :- !. |
| 102 |
coq.typecheck-attribute _ bool "True" tt ok :- !. |
| 103 |
coq.typecheck-attribute _ bool "on" tt ok :- !. |
| 104 |
coq.typecheck-attribute _ bool "yes" tt ok :- !. |
| 105 |
coq.typecheck-attribute _ bool "" tt ok :- !. |
| 106 |
coq.typecheck-attribute _ bool "false" ff ok :- !. |
| 107 |
coq.typecheck-attribute _ bool "False" ff ok :- !. |
| 108 |
coq.typecheck-attribute _ bool "off" ff ok :- !. |
| 109 |
coq.typecheck-attribute _ bool "ff" ff ok :- !. |
| 110 |
coq.typecheck-attribute _ bool "no" ff ok :- !. |
| 111 |
coq.typecheck-attribute N bool Value _ (error Msg) :- |
| 112 |
Msg is "Attribute " ^ N ^ " takes an boolean, got: " ^ Value. |
| 113 |
|
| 114 |
:functional |
| 115 |
pred coq.is-one-of i:string, o:any, i:attribute-mapping. |
| 116 |
coq.is-one-of K V (K `-> V). |
| 117 |
|
| 118 |
coq.typecheck-attribute _ (oneof L) K V ok :- std.exists L (coq.is-one-of K V), !. |
| 119 |
coq.typecheck-attribute N (oneof L) K _ (error Msg) :- |
| 120 |
std.map L (x\r\ sigma tmp\ x = r `-> tmp) S, |
| 121 |
std.fold S "" (s\ a\ calc (a ^ " " ^ s)) OneOf, |
| 122 |
Msg is "Attribute " ^ N ^ " takes one of " ^ OneOf ^ ", got: " ^ K. |
| 123 |
|
| 124 |
func append-string string, string -> string. |
| 125 |
append-string "" A A :- !. |
| 126 |
append-string A B R :- R is A ^ "." ^ B. |
| 127 |
|
| 128 |
func keep-only-label attribute -> attribute. |
| 129 |
keep-only-label (attribute L _) (attribute L (leaf-str "")). |
| 130 |
|
| 131 |
coq.parse-attributes L S O :- |
| 132 |
std.map S (x\r\ r = supported-attribute x) CS, |
| 133 |
CS => parse-attributes.aux L "" O, !. |
| 134 |
|
| 135 |
:index (1) |
| 136 |
func parse-attributes.aux list attribute, string -> list prop. |
| 137 |
parse-attributes.aux [] _ []. |
| 138 |
parse-attributes.aux [attribute S (node L)|AS] Prefix R :- |
| 139 |
append-string Prefix S PS, supported-attribute (att PS attlist), !, |
| 140 |
parse-attributes.aux AS Prefix R1, |
| 141 |
((pi x\ supported-attribute (att x bool) :- !) ==> parse-attributes.aux L "" Map), |
| 142 |
std.append R1 [get-option PS Map] R. |
| 143 |
parse-attributes.aux [attribute S (node L)|AS] Prefix R :- |
| 144 |
append-string Prefix S PS, supported-attribute (att PS attmap), !, |
| 145 |
parse-attributes.aux AS Prefix R1, |
| 146 |
((pi x\ supported-attribute (att x string) :- !) ==> parse-attributes.aux L "" Map), |
| 147 |
std.append R1 [get-option PS Map] R. |
| 148 |
parse-attributes.aux [attribute S (node L)|AS] Prefix R :- |
| 149 |
append-string Prefix S PS, supported-attribute (att PS attlabel), !, |
| 150 |
parse-attributes.aux AS Prefix R1, |
| 151 |
std.map L keep-only-label Ll, |
| 152 |
((pi x\ supported-attribute (att x bool) :- !) ==> parse-attributes.aux Ll "" Map), |
| 153 |
parse-attributes.aux L Prefix R2, |
| 154 |
std.append R1 [get-option PS Map|R2] R. |
| 155 |
parse-attributes.aux [attribute S (node L)|AS] Prefix R :- !, |
| 156 |
parse-attributes.aux AS Prefix R1, |
| 157 |
append-string Prefix S PS, |
| 158 |
parse-attributes.aux L PS R2, |
| 159 |
std.append R1 R2 R. |
| 160 |
parse-attributes.aux [attribute S (leaf-str V)|AS] Prefix CLS :- !, |
| 161 |
append-string Prefix S PS, |
| 162 |
coq.valid-str-attribute PS V V1 Diag, |
| 163 |
if (Diag = error Msg) (coq.error Msg) true, |
| 164 |
if (V1 = some Val) (CLS = [get-option PS Val|R]) (CLS = R), % ignored |
| 165 |
parse-attributes.aux AS Prefix R. |
| 166 |
parse-attributes.aux [attribute S (leaf-loc V)|AS] Prefix CLS :- !, |
| 167 |
append-string Prefix S PS, |
| 168 |
coq.valid-loc-attribute PS V Diag, |
| 169 |
if (Diag = error Msg) (coq.error Msg) true, |
| 170 |
CLS = [get-option PS V|R], |
| 171 |
parse-attributes.aux AS Prefix R. |
| 172 |
|