Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
aryx committed Nov 8, 2023
1 parent 9a4b74d commit 7d29122
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
53 changes: 40 additions & 13 deletions rule_schema_v2.atd
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ type options <ocaml from="Rule_options" t="t"> = abstract
*)

type formula = {
(* alt: have ?all: ... ?any: ... ?regex: ... ?pattern: ... with only one *)
f: formula_bis;
(* alt: we could instead do 'All of formula list * condition list' below
* but syntactically we also allow 'where' with pattern:, regex:, etc.
Expand Down Expand Up @@ -263,18 +264,34 @@ type formula_bis = [
(* TODO? Taint of taint_spec *)
]

(* TODO *)
type condition = raw_json

type condition_TODO = [
(* Just like for formula, we're using an adapter to transform
* conditions in YAML like:
*
* where:
* - metavariable: $X
* regex: $Z
*
* which when turned into JSON gives:
*
* XXX
*
* which we must transform in an ATD-compliant:
*
* YYY
*)
type condition = [
(* either a single string or an array in JSON, that is
* {focus: "$FOO"}, but also {focus: ["$FOO", "$BAR"]}
*)
| Focus of mvar list
| Comparison of comparison
| Metavariable of (mvar * metavariable_cond)
| Focus <json name="F"> of focus
| Comparison <json name="C"> of comparison
| Metavariable <json name="M"> of metavariable_cond
]
<json adapter.ocaml="Rule_schema_v2_adapter.Where">
<json adapter.ocaml="Rule_schema_v2_adapter.Condition">

type focus = {
focus: mvar list;
}

type mvar = string

Expand All @@ -284,11 +301,21 @@ type comparison = {
~strip: bool;
}

type metavariable_cond = [
| Type of string
(* TODO: for metavariable-regex, can also enable constant_propagation *)
| Formula of formula
| Analyzer of string
type metavariable_cond = {
metavariable: mvar;
(* alt: have ?type: ... ?types:... ?regex: ... *)
c: metavariable_cond_bis;
}

type metavariable_cond_bis = [
| Type <json name="type"> of string
| Types <json name="types"> of string list
(* alt: we could remove Regex as Formula itself as a Regex
* TODO: for metavariable-regex, can also enable constant_propagation
*)
| Regex <json name="regex"> of regex
| Formula <json name="F"> of formula
| Analyzer <json name="analyzer"> of string
]

(*****************************************************************************)
Expand Down
35 changes: 31 additions & 4 deletions rule_schema_v2_adapter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,41 @@ module Formula = struct
failwith "Rule_schema_v2_adapter.Formula.restore not implemented"
end

module Where = struct
module Condition = struct

(** Convert from original json to ATD-compatible json *)
let normalize (_orig : Yojson.Safe.t ) : Yojson.Safe.t =
failwith "TODO: Where.normalize"
let normalize (orig : Yojson.Safe.t ) : Yojson.Safe.t =
match orig with
| `Assoc (("comparison", cmp)::rest) ->
`List [`String "C";
`Assoc (("comparison", cmp)::rest)]
| `Assoc [("metavariable", mvar); ("regex", reg)] ->
`List [`String "M";
`Assoc [("metavariable", mvar);
("c", `List [`String "regex"; reg])]]
| `Assoc [("metavariable", mvar); ("type", ty)] ->
`List [`String "M";
`Assoc [("metavariable", mvar);
("c", `List [`String "type"; ty])]]
| `Assoc [("metavariable", mvar); ("types", tys)] ->
`List [`String "M";
`Assoc [("metavariable", mvar);
("c", `List [`String "types"; tys])]]
| `Assoc [("metavariable", _mvar); ("pattern", _p)] ->
Common.pr2_gen orig;
failwith "TODO"
(*
`List [`String "M";
`Assoc [("metavariable", mvar);
("c", `List [`String "F"; `Assoc [("pattern", p)]])]]
*)
| x ->
Common.pr2_gen x;
failwith "TODO2"


(** Convert from ATD-compatible json to original json *)
let restore (_atd : Yojson.Safe.t) : Yojson.Safe.t =
(* not needed for now; we care just about parsing *)
failwith "Rule_schema_v2_adapter.Where.restore not implemented"
failwith "Rule_schema_v2_adapter.Condition.restore not implemented"
end

0 comments on commit 7d29122

Please sign in to comment.