Library iris.proofmode.sel_patterns

From stdpp Require Export strings.
From iris.proofmode Require Import base tokens.
From iris.prelude Require Import options.

Inductive sel_pat :=
  | SelPure
  | SelIntuitionistic
  | SelSpatial
  | SelIdent : ident sel_pat.

Fixpoint sel_pat_pure (ps : list sel_pat) : bool :=
  match ps with
  | []false
  | SelPure :: pstrue
  | _ :: pssel_pat_pure ps
  end.

Module sel_pat.
Fixpoint parse_go (ts : list token) (k : list sel_pat) : option (list sel_pat) :=
  match ts with
  | []Some (reverse k)
  | TName s :: tsparse_go ts (SelIdent s :: k)
  | TPure None :: tsparse_go ts (SelPure :: k)
  | TIntuitionistic :: tsparse_go ts (SelIntuitionistic :: k)
  | TSep :: tsparse_go ts (SelSpatial :: k)
  | _None
  end.
Definition parse (s : string) : option (list sel_pat) :=
  parse_go (tokenize s) [].

Ltac parse s :=
  lazymatch type of s with
  | sel_patconstr:([s])
  | list sel_pats
  | identconstr:([SelIdent s])
  | list identeval vm_compute in (SelIdent <$> s)
  | list stringeval vm_compute in (SelIdent INamed <$> s)
  | string
     lazymatch eval vm_compute in (parse s) with
     | Some ?patspats
     | _fail "sel_pat.parse: cannot parse" s "as a selection pattern"
     end
  | ?Xfail "sel_pat.parse: the term" s
     "is expected to be a selection pattern"
     "(usually a string),"
     "but has unexpected type" X
  end.
End sel_pat.