| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Regular expressions acting on strings, using unsafe features |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --with-K #-} |
| 8 |
|
| 9 |
module Text.Regex.String.Unsafe where |
| 10 |
|
| 11 |
open import Data.String.Base using (String; toList; fromList) |
| 12 |
import Data.String.Unsafe as Stringₚ |
| 13 |
open import Function.Base using (_on_; id) |
| 14 |
open import Level using (0ℓ) |
| 15 |
open import Relation.Binary.Core using (Rel) |
| 16 |
open import Relation.Binary.Definitions using (Decidable) |
| 17 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; sym; subst) |
| 18 |
open import Relation.Nullary.Decidable using (map′) |
| 19 |
|
| 20 |
------------------------------------------------------------------------ |
| 21 |
-- Re-exporting safe definitions |
| 22 |
|
| 23 |
open import Text.Regex.String as Regex public |
| 24 |
hiding (_∈_; _∉_; _∈?_; _∉?_; Span; Match; search) |
| 25 |
|
| 26 |
------------------------------------------------------------------------ |
| 27 |
-- Specialised definitions |
| 28 |
|
| 29 |
infix 4 _∈_ _∉_ _∈?_ _∉?_ |
| 30 |
|
| 31 |
_∈_ : String → Exp → Set |
| 32 |
str ∈ e = toList str Regex.∈ e |
| 33 |
|
| 34 |
_∉_ : String → Exp → Set |
| 35 |
str ∉ e = toList str Regex.∉ e |
| 36 |
|
| 37 |
_∈?_ : Decidable _∈_ |
| 38 |
str ∈? e = toList str Regex.∈? e |
| 39 |
|
| 40 |
_∉?_ : Decidable _∉_ |
| 41 |
str ∉? e = toList str Regex.∉? e |
| 42 |
|
| 43 |
Span : Regex → Rel String 0ℓ |
| 44 |
Span e = Regex.Span e _≡_ on toList |
| 45 |
|
| 46 |
-- A match is a string, a proof it matches the regular expression, |
| 47 |
-- and a proof it appears as the right sort of substring. |
| 48 |
record Match (str : String) (e : Regex) : Set where |
| 49 |
constructor mkMatch |
| 50 |
field |
| 51 |
string : String |
| 52 |
match : string ∈ Regex.expression e |
| 53 |
related : Span e string str |
| 54 |
open Match public |
| 55 |
|
| 56 |
search : Decidable Match |
| 57 |
search str e = map′ from to (Regex.search input e) where |
| 58 |
|
| 59 |
input = toList str |
| 60 |
exp = Regex.expression e |
| 61 |
|
| 62 |
from : Regex.Match (Regex.Span e _≡_) input exp → Match str e |
| 63 |
from (Regex.mkMatch list match related) = |
| 64 |
let eq = sym (Stringₚ.toList∘fromList list) in |
| 65 |
mkMatch (fromList list) |
| 66 |
(subst (Regex._∈ exp) eq match) |
| 67 |
(subst (λ str → Regex.Span e _≡_ str input) eq related) |
| 68 |
|
| 69 |
to : Match str e → Regex.Match (Regex.Span e _≡_) input exp |
| 70 |
to (mkMatch string match related) = |
| 71 |
Regex.mkMatch (toList string) match related |