| 1 |
From Coq Require Import Ascii String List. |
| 2 |
Require Import Crypto.Util.Option. |
| 3 |
Require Import Crypto.Util.OptionList. |
| 4 |
Require Import Crypto.Util.Strings.String. |
| 5 |
Require Import Crypto.Util.Strings.Ascii. |
| 6 |
Require Import Crypto.Util.Notations. |
| 7 |
Import ListNotations. |
| 8 |
Local Open Scope list_scope. |
| 9 |
Local Open Scope option_scope. |
| 10 |
Local Open Scope char_scope. |
| 11 |
Local Open Scope string_scope. |
| 12 |
|
| 13 |
Definition ParserAction T := string -> list (T * string). |
| 14 |
Declare Scope parse_scope. |
| 15 |
Delimit Scope parse_scope with parse. |
| 16 |
Bind Scope parse_scope with ParserAction. |
| 17 |
Definition parse_impossible {T} : ParserAction T |
| 18 |
:= fun s => []. |
| 19 |
Definition parse_empty : ParserAction unit |
| 20 |
:= fun s |
| 21 |
=> if s =? "" |
| 22 |
then [(tt, "")] |
| 23 |
else []. |
| 24 |
Definition parse_seq_gen {A B C} (f : A -> B -> C) (p1 : ParserAction A) (p2 : ParserAction B) : ParserAction C |
| 25 |
:= fun s |
| 26 |
=> flat_map |
| 27 |
(fun '(a, s) => List.map (fun '(b, s) => (f a b, s)) (p2 s)) |
| 28 |
(p1 s). |
| 29 |
Definition parse_alt_gen {A B C} (f : A + B -> C) (p1 : ParserAction A) (p2 : ParserAction B) : ParserAction C |
| 30 |
:= fun s |
| 31 |
=> ((List.map (fun '(v, s) => (f (inl v), s)) (p1 s)) |
| 32 |
++ List.map (fun '(v, s) => (f (inr v), s)) (p2 s))%list. |
| 33 |
Definition parse_alt {A} (p1 p2 : ParserAction A) : ParserAction A |
| 34 |
:= parse_alt_gen (fun aa => match aa with inl a => a | inr a => a end) p1 p2. |
| 35 |
(* variant of alt which only parses the second branch if there are no matches on the first branch *) |
| 36 |
Definition parse_or_else_gen {A B C} (f : A + B -> C) (p1 : ParserAction A) (p2 : ParserAction B) : ParserAction C |
| 37 |
:= fun s |
| 38 |
=> match p1 s with |
| 39 |
| [] => List.map (fun '(v, s) => (f (inr v), s)) (p2 s) |
| 40 |
| ls => List.map (fun '(v, s) => (f (inl v), s)) ls |
| 41 |
end. |
| 42 |
Definition parse_or_else {A} (p1 p2 : ParserAction A) : ParserAction A |
| 43 |
:= parse_or_else_gen (fun aa => match aa with inl a => a | inr a => a end) p1 p2. |
| 44 |
|
| 45 |
Notation ε := parse_empty. |
| 46 |
Infix "||" := parse_alt : parse_scope. |
| 47 |
Infix "||->{ f }" := (parse_alt_gen f) : parse_scope. |
| 48 |
Infix ";;->{ f }" := (parse_seq_gen f) : parse_scope. |
| 49 |
Infix ";;" := (parse_seq_gen (@pair _ _)) : parse_scope. |
| 50 |
Infix ";;L" := (parse_seq_gen (fun l r => l)) : parse_scope. |
| 51 |
Infix ";;R" := (parse_seq_gen (fun l r => r)) : parse_scope. |
| 52 |
|
| 53 |
Definition parse_alpha_char (lower : bool) (upper : bool) : ParserAction ascii |
| 54 |
:= fun s |
| 55 |
=> match s with |
| 56 |
| EmptyString => [] |
| 57 |
| String ch s' |
| 58 |
=> if ((lower && ("a" <=? ch) && (ch <=? "z")) || (upper && ("A" <=? ch) && (ch <=? "Z")))%char%bool |
| 59 |
then [(ch, s')] |
| 60 |
else [] |
| 61 |
end. |
| 62 |
|
| 63 |
Notation "[A-Z]" := (parse_alpha_char false true) : parse_scope. |
| 64 |
Notation "[a-z]" := (parse_alpha_char true false) : parse_scope. |
| 65 |
Notation "[a-zA-Z]" := (parse_alpha_char true true) : parse_scope. |
| 66 |
|
| 67 |
Definition parse_ascii (prefix : ascii) : ParserAction ascii |
| 68 |
:= fun s |
| 69 |
=> match s with |
| 70 |
| EmptyString => [] |
| 71 |
| String ch s' => if (ch =? prefix)%char |
| 72 |
then [(ch, s')] |
| 73 |
else [] |
| 74 |
end. |
| 75 |
|
| 76 |
Coercion parse_ascii : ascii >-> ParserAction. |
| 77 |
|
| 78 |
Definition parse_ascii_case_insensitive (prefix : ascii) : ParserAction ascii |
| 79 |
:= fun s |
| 80 |
=> match s with |
| 81 |
| EmptyString => [] |
| 82 |
| String ch s' => if (Ascii.to_lower ch =? Ascii.to_lower prefix)%char |
| 83 |
then [(ch, s')] |
| 84 |
else [] |
| 85 |
end. |
| 86 |
|
| 87 |
Definition parse_str (prefix : string) : ParserAction string |
| 88 |
:= fun s |
| 89 |
=> if startswith prefix s |
| 90 |
then [(prefix, substring (String.length prefix) (String.length s) s)] |
| 91 |
else []. |
| 92 |
|
| 93 |
Coercion parse_str : string >-> ParserAction. |
| 94 |
|
| 95 |
Definition parse_str_case_insensitive (prefix : string) : ParserAction string |
| 96 |
:= fun s |
| 97 |
=> let '(s1, s2) := (substring 0 (String.length prefix) s, |
| 98 |
substring (String.length prefix) (String.length s) s) in |
| 99 |
if (String.to_lower prefix =? String.to_lower s1)%string |
| 100 |
then [(s1, s2)] |
| 101 |
else []. |
| 102 |
|
| 103 |
Notation casefold := parse_str_case_insensitive (only parsing). |
| 104 |
|
| 105 |
Definition parse_map {A B} (f : A -> B) : ParserAction A -> ParserAction B |
| 106 |
:= fun p s |
| 107 |
=> List.map (fun '(v, s) => (f v, s)) (p s). |
| 108 |
|
| 109 |
Definition parse_filter {A} (f : A -> bool) : ParserAction A -> ParserAction A |
| 110 |
:= fun p s |
| 111 |
=> List.filter (fun '(v, s) => f v) (p s). |
| 112 |
|
| 113 |
Definition parse_flat_map {A B} (f : A -> list B) : ParserAction A -> ParserAction B |
| 114 |
:= fun p s |
| 115 |
=> List.flat_map (fun '(v, s) => List.map (fun fv => (fv, s)) (f v)) (p s). |
| 116 |
|
| 117 |
Definition parse_option_list_map {A B} (f : A -> option B) : ParserAction A -> ParserAction B |
| 118 |
:= parse_flat_map (fun a => match f a with Some v => [v] | None => [] end). |
| 119 |
|
| 120 |
Definition parse_maybe {A} (p : ParserAction A) : ParserAction (option A) |
| 121 |
:= (p ||->{ fun v => match v with inl v => Some v | inr _ => None end } "")%parse. |
| 122 |
|
| 123 |
Notation "f ?" := (parse_maybe f) : parse_scope. |
| 124 |
|
| 125 |
Definition option_to_list {A} (v : option A) : list A |
| 126 |
:= match v with |
| 127 |
| Some v => [v] |
| 128 |
| None => [] |
| 129 |
end. |
| 130 |
|
| 131 |
Definition option_list_to_list {A} (v : option (list A)) : list A |
| 132 |
:= match v with |
| 133 |
| Some v => v |
| 134 |
| None => [] |
| 135 |
end. |
| 136 |
|
| 137 |
Definition fuel {A} (p : nat -> ParserAction A) : ParserAction A |
| 138 |
:= fun s => p (S (String.length s)) s. |
| 139 |
|
| 140 |
Fixpoint parse_plus_fueled {A} (p : ParserAction A) (fuel : nat) : ParserAction (list A) |
| 141 |
:= (p ;;->{ @cons _ } (match fuel with |
| 142 |
| O => fun s => [] |
| 143 |
| S fuel => parse_map option_list_to_list ((parse_plus_fueled p fuel)?) |
| 144 |
end)). |
| 145 |
|
| 146 |
Definition parse_plus {A} (p : ParserAction A) : ParserAction (list A) |
| 147 |
:= fuel (parse_plus_fueled p). |
| 148 |
|
| 149 |
Definition parse_star {A} (p : ParserAction A) : ParserAction (list A) |
| 150 |
:= parse_map option_list_to_list ((parse_plus p)?). |
| 151 |
|
| 152 |
Definition parse_lookahead {A} (p : ParserAction A) : ParserAction A |
| 153 |
:= fun s => List.map (fun '(a, _) => (a, s)) (p s). |
| 154 |
|
| 155 |
Definition parse_lookahead_not {A} (p : ParserAction A) : ParserAction unit |
| 156 |
:= fun s => match p s with |
| 157 |
| [] => [(tt, s)] |
| 158 |
| _ => [] |
| 159 |
end. |
| 160 |
|
| 161 |
Notation "p +" := (parse_plus p) : parse_scope. |
| 162 |
Notation "p *" := (parse_star p) : parse_scope. |
| 163 |
|
| 164 |
Definition whitespace : list ascii |
| 165 |
:= [" "; "009" (* \t *); "010" (* \n *); "013" (* \r *); "012" (* \f *); "011" (* \v *)]%char. |
| 166 |
Definition whitespace_strs : list string |
| 167 |
:= Eval compute in List.map (fun ch => String ch "") whitespace. |
| 168 |
|
| 169 |
Definition parse_alt_list {T} (ls : list (ParserAction T)) : ParserAction T |
| 170 |
:= List.fold_left ((fun p2 p1 => p1 ||->{ fun v => match v with inl v => v | inr v => v end } p2)%parse) |
| 171 |
ls |
| 172 |
parse_impossible. |
| 173 |
|
| 174 |
Definition parse_strs {T} (ls : list (string * T)) : ParserAction T |
| 175 |
:= parse_alt_list (List.map (fun '(s, v) => parse_map (fun _ => v) (s:string)) ls). |
| 176 |
|
| 177 |
Definition parse_strs_case_insensitive {T} (ls : list (string * T)) : ParserAction T |
| 178 |
:= parse_alt_list (List.map (fun '(s, v) => parse_map (fun _ => v) (casefold s)) ls). |
| 179 |
|
| 180 |
Notation parse_strs_casefold := parse_strs_case_insensitive (only parsing). |
| 181 |
|
| 182 |
Definition parse_one_whitespace : ParserAction string |
| 183 |
:= Eval cbv [List.fold_right List.fold_left whitespace whitespace_strs List.tl List.hd parse_strs List.combine] in |
| 184 |
(List.fold_left (B:=string) parse_alt (tl whitespace_strs) (hd " " whitespace_strs)). |
| 185 |
|
| 186 |
Notation "[\s]" := parse_one_whitespace : parse_scope. |
| 187 |
|
| 188 |
Definition parse_any_whitespace : ParserAction (list string) |
| 189 |
:= Eval cbv [parse_one_whitespace] in |
| 190 |
(parse_one_whitespace)*. |
| 191 |
|
| 192 |
Definition strip_whitespace_around {A} (p : ParserAction A) : ParserAction A |
| 193 |
:= parse_any_whitespace ;;->{ fun _ v => v } |
| 194 |
p ;;->{ fun v _ => v } |
| 195 |
parse_any_whitespace. |
| 196 |
|
| 197 |
Definition strip_whitespace_before {A} (p : ParserAction A) : ParserAction A |
| 198 |
:= parse_any_whitespace ;;->{ fun _ v => v } |
| 199 |
p. |
| 200 |
|
| 201 |
Definition strip_whitespace_after {A} (p : ParserAction A) : ParserAction A |
| 202 |
:= p ;;->{ fun v _ => v } |
| 203 |
parse_any_whitespace. |
| 204 |
|
| 205 |
Definition parse_list_gen {A} (leftbr sep rightbr : string) (parse : ParserAction A) : ParserAction (list A) |
| 206 |
:= (strip_whitespace_around leftbr) |
| 207 |
;;->{ fun _ v => v } |
| 208 |
((parse ;;->{ @cons _ } |
| 209 |
(strip_whitespace_around sep ;;->{ fun _ tl => tl } parse)* ))? |
| 210 |
;;->{ fun v _ => match v with None => [] | Some ls => ls end } |
| 211 |
strip_whitespace_around rightbr. |
| 212 |
|
| 213 |
Definition parse_list_gen_no_leading_trailing_space {A} (leftbr sep rightbr : string) (parse : ParserAction A) : ParserAction (list A) |
| 214 |
:= (strip_whitespace_after leftbr) |
| 215 |
;;->{ fun _ v => v } |
| 216 |
((parse ;;->{ @cons _ } |
| 217 |
(strip_whitespace_around sep ;;->{ fun _ tl => tl } parse)* ))? |
| 218 |
;;->{ fun v _ => match v with None => [] | Some ls => ls end } |
| 219 |
strip_whitespace_before rightbr. |
| 220 |
|
| 221 |
Definition parse_list {A} (parse : ParserAction A) : ParserAction (list A) |
| 222 |
:= parse_list_gen "[" ";" "]" parse. |
| 223 |
|
| 224 |
Definition parse_list_Python_style {A} (parse : ParserAction A) : ParserAction (list A) |
| 225 |
:= parse_list_gen "[" "," "]" parse. |
| 226 |
|
| 227 |
Definition parse_list_Mathematica_style {A} (parse : ParserAction A) : ParserAction (list A) |
| 228 |
:= parse_list_gen "{" "," "}" parse. |
| 229 |
|
| 230 |
Definition parse_comma_list {A} (parse : ParserAction A) : ParserAction (list A) |
| 231 |
:= parse_list_gen "" "," "" parse. |
| 232 |
|
| 233 |
Definition parse_semicolon_list {A} (parse : ParserAction A) : ParserAction (list A) |
| 234 |
:= parse_list_gen "" ";" "" parse. |
| 235 |
|
| 236 |
Definition parse_any_list {A} (parse : ParserAction A) : ParserAction (list A) |
| 237 |
:= parse_list parse || parse_list_Python_style parse || parse_list_Mathematica_style parse || parse_comma_list parse || parse_semicolon_list parse. |
| 238 |
|
| 239 |
Fixpoint split_before_first (f : ascii -> bool) (s : string) : string * string |
| 240 |
:= match s with |
| 241 |
| EmptyString => (EmptyString, EmptyString) |
| 242 |
| String ch rest |
| 243 |
=> if f ch |
| 244 |
then (EmptyString, s) |
| 245 |
else let '(s1, s2) := split_before_first f rest in |
| 246 |
(String ch s1, s2) |
| 247 |
end. |
| 248 |
|
| 249 |
Fixpoint split_before_first_skip (f : ascii -> bool) (s : string) (skip : nat) : string * string |
| 250 |
:= match skip, s with |
| 251 |
| 0, _ => split_before_first f s |
| 252 |
| _, EmptyString => (EmptyString, EmptyString) |
| 253 |
| S skip', String ch rest |
| 254 |
=> let '(s1, s2) := split_before_first_skip f rest skip' in |
| 255 |
(String ch s1, s2) |
| 256 |
end. |
| 257 |
|
| 258 |
Definition finalize {A} (p : ParserAction A) : string -> option A |
| 259 |
:= fun s => match (p ;;->{fun v _ => v} ε)%parse s with |
| 260 |
| [(v, _)] => Some v |
| 261 |
| _ => None |
| 262 |
end. |
| 263 |
|
| 264 |
(* |
| 265 |
Definition parse_ch {T} (ls : list (ascii * T)) (s : string) : option (T * string) |
| 266 |
:= match s with |
| 267 |
| EmptyString => None |
| 268 |
| String ch s |
| 269 |
=> List.fold_right |
| 270 |
(fun '(ch', t) default |
| 271 |
=> if ascii_beq ch ch' then Some (t, s) else default) |
| 272 |
None |
| 273 |
ls |
| 274 |
end. |
| 275 |
*) |