let visit_ast ?(special_k = fun _ -> assert false) k =
let rec aux = function
-
| Appl terms -> Appl (List.map k terms)
| Binder (kind, var, body) ->
Binder (kind, aux_capture_variable var, k body)
| Ident (name, Some substs) -> Ident (name, Some (aux_substs substs))
| Uri (name, Some substs) -> Uri (name, Some (aux_substs substs))
| Meta (index, substs) -> Meta (index, List.map aux_opt substs)
-
| (AttributedTerm _
| Layout _
| Literal _
| Magic _
| Variable _) as t -> special_k t
-
| (Ident _
| Implicit
| Num _
| Symbol _
| Uri _
| UserInput) as t -> t
-
and aux_opt = function
| None -> None
| Some term -> Some (k term)
-
and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt
-
and aux_patterns patterns = List.map aux_pattern patterns
-
and aux_pattern ((head, vars), term) =
((head, List.map aux_capture_variable vars), k term)
-
and aux_subst (name, term) = (name, k term)
-
and aux_substs substs = List.map aux_subst substs
-
in
-
aux
let visit_layout k = function
| Root (arg, index) -> Root (k arg, k index)
| Break -> Break
| Box (kind, terms) -> Box (kind, List.map k terms)
+ | Group terms -> Group (List.map k terms)
let visit_magic k = function
| List0 (t, l) -> List0 (k t, l)
in
List.map aux (variables_of_term t)
+let keywords_of_term t =
+ let rec keywords = ref [] in
+ let add_keyword k = keywords := k :: !keywords in
+ let rec aux = function
+ | AttributedTerm (_, t) -> aux t
+ | Layout l -> Layout (visit_layout aux l)
+ | Literal (`Keyword k) as t ->
+ add_keyword k;
+ t
+ | Literal _ as t -> t
+ | Magic m -> Magic (visit_magic aux m)
+ | Variable _ as v -> v
+ | t -> visit_ast aux t
+ in
+ ignore (aux t) ;
+ !keywords
+
let rec strip_attributes t =
let special_k = function
| AttributedTerm (_, term) -> strip_attributes term
| [ a ] -> a
| l -> Layout (Box ((H, false, false), l))
+let group = function
+ | [ a ] -> a
+ | l -> Layout (Group l)
+
+let ungroup =
+ let rec aux acc =
+ function
+ [] -> List.rev acc
+ | Layout (Group terms) :: terms' -> aux acc (terms @ terms')
+ | term :: terms -> aux (term :: acc) terms
+ in
+ aux []
+
+let dress sauce =
+ let rec aux =
+ function
+ | [] -> []
+ | [hd] -> [hd]
+ | hd :: tl -> hd :: sauce :: aux tl
+ in
+ aux
+
let find_appl_pattern_uris ap =
let rec aux acc =
function
- | UriPattern uri ->
+ | GrafiteAst.UriPattern uri ->
(try
ignore (List.find (fun uri' -> UriManager.eq uri uri') acc);
acc
with Not_found -> uri :: acc)
- | VarPattern _ -> acc
- | ApplPattern apl -> List.fold_left aux acc apl
+ | GrafiteAst.VarPattern _ -> acc
+ | GrafiteAst.ApplPattern apl -> List.fold_left aux acc apl
in
aux [] ap