1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
; ABNF (Augmented Backus-Naur Form) Library
;
; Copyright (C) 2022 Kestrel Institute (http://www.kestrel.edu)
;
; License: A 3-clause BSD license. See the LICENSE file distributed with ACL2.
;
; Author: Alessandro Coglio (coglio@kestrel.edu)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(in-package "ABNF")
; Added by Matt K. 12/31/2021 to avoid ACL2(r) failure:
; cert_param: (non-acl2r)
(include-book "../notation/semantics")
(include-book "kestrel/fty/nat-result" :dir :system)
(include-book "kestrel/fty/nat-list-result" :dir :system)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defxdoc+ parsing-primitives-defresult
:parents (abnf)
:short "Some basic ABNF parsing functions
for use with @(tsee fty::defresult)."
:long
(xdoc::topstring
(xdoc::p
"These functions may be be useful when writing
@(tsee fty::defresult)-based parsers for
languages specified via ABNF grammars.
These functions take lists of natural numbers as inputs
and return two outputs:
(i) a @(tsee fty::defresult) value (e.g. tree or error)
obtained from the consumed input natural numbers, and
(ii) the remaining unconsumed input natural numbers.")
(xdoc::p
"These are somewhat similar to the "
(xdoc::seetopic "parsing-primitives-seq"
"<i>Seq</i>-based parsing primitives")
", but they have an interface suitable for @(tsee fty::defresult)
instead of an interface suitable for <i>Seq</i>."))
:order-subtopics t
:default-parent t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define parse-next ((input nat-listp))
:returns (mv (nat nat-resultp)
(rest-input nat-listp))
:short "Obtain the next natural number from the input."
(if (consp input)
(mv (lnfix (car input))
(nat-list-fix (cdr input)))
(mv (reserrf :end-of-input)
nil))
:hooks (:fix)
///
(defret len-of-parse-next-<=
(<= (len rest-input)
(len input))
:rule-classes :linear)
(defret len-of-parse-next-<
(implies (not (reserrp nat))
(< (len rest-input)
(len input)))
:rule-classes :linear))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define parse-direct ((nats nat-listp) (input nat-listp))
:returns (mv (tree tree-resultp)
(rest-input nat-listp))
:short "Parse a direct numeric value consisting of given natural numbers."
(b* (((mv nats input) (parse-direct-aux nats input))
((when (reserrp nats)) (mv (reserrf-push nats) input)))
(mv (tree-leafterm nats)
input))
:hooks (:fix)
:prepwork
((define parse-direct-aux ((nats nat-listp) (input nat-listp))
:returns
(mv (nats1
nat-list-resultp
:hints
(("Goal"
:in-theory
(enable
acl2::nat-listp-when-nat-list-resultp-and-not-reserrp))))
(rest-input nat-listp))
:parents nil
(b* (((when (endp nats)) (mv nil (nat-list-fix input)))
((mv nat? input1) (parse-next input))
((when (reserrp nat?)) (mv (reserrf-push nat?) (nat-list-fix input)))
(nat nat?)
((unless (equal nat (lnfix (car nats))))
(mv (reserrf (list :found nat :required (lnfix (car nats))))
(nat-list-fix input)))
((mv nats? input2) (parse-direct-aux (cdr nats) input1))
((when (reserrp nats?)) (mv (reserrf-push nats?) input1))
(nats nats?))
(mv (cons nat nats) input2))
:hooks (:fix)
///
(defret len-of-parse-direct-aux-<=
(<= (len rest-input)
(len input))
:rule-classes :linear)
(defret len-of-parse-direct-aux-<
(implies (and (not (reserrp nats1))
(consp nats))
(< (len rest-input)
(len input)))
:rule-classes :linear)))
///
(defret len-of-parse-direct-<=
(<= (len rest-input)
(len input))
:rule-classes :linear)
(defret len-of-parse-direct-<
(implies (and (not (reserrp tree))
(consp nats))
(< (len rest-input)
(len input)))
:rule-classes :linear))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define parse-range ((min natp) (max natp) (input nat-listp))
:guard (<= min max)
:returns (mv (tree tree-resultp)
(rest-input nat-listp))
:short "Parse a range numeric value consisting of given minimum and maximum."
(b* (((mv nat? input1) (parse-next input))
((when (reserrp nat?)) (mv (reserrf-push nat?) (nat-list-fix input)))
(nat nat?)
((unless (and (<= (lnfix min)
nat)
(<= nat
(lnfix max))))
(mv (reserrf (list :found nat :required (lnfix min) (lnfix max)))
(nat-list-fix input))))
(mv (tree-leafterm (list nat))
input1))
:hooks (:fix)
///
(defret len-of-parse-range-<=
(<= (len rest-input)
(len input))
:rule-classes :linear)
(defret len-of-parse-range-<
(implies (not (reserrp tree))
(< (len rest-input)
(len input)))
:rule-classes :linear))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define parse-ichars ((chars acl2::stringp) (input nat-listp))
:returns (mv (tree tree-resultp)
(rest-input nat-listp))
:short "Parse a case-insensitive character value consisting of
a given string of characters."
(b* (((mv nats input) (parse-ichars-aux (str::explode chars) input))
((when (reserrp nats)) (mv (reserrf-push nats) input)))
(mv (tree-leafterm nats)
input))
:hooks (:fix)
:prepwork
((define parse-ichars-aux ((chars character-listp) (input nat-listp))
:returns
(mv (nats
nat-list-resultp
:hints
(("Goal"
:in-theory
(enable
acl2::natp-when-nat-resultp-and-not-reserrp
acl2::nat-listp-when-nat-list-resultp-and-not-reserrp))))
(rest-input nat-listp))
:parents nil
(b* (((when (endp chars)) (mv nil (nat-list-fix input)))
((mv nat? input1) (parse-next input))
((when (reserrp nat?)) (mv (reserrf-push nat?) (nat-list-fix input)))
(nat nat?)
((unless (nat-match-insensitive-char-p nat (car chars)))
(mv (reserrf (list :found nat :required (acl2::char-fix (car chars))))
(nat-list-fix input)))
((mv nats? input2) (parse-ichars-aux (cdr chars) input1))
((when (reserrp nats?)) (mv (reserrf-push nats?) input1))
(nats nats?))
(mv (cons nat nats) input2))
:hooks (:fix)
///
(defret len-of-parse-ichars-aux-<=
(<= (len rest-input)
(len input))
:rule-classes :linear)
(defret len-of-parse-ichars-aux-<
(implies (and (not (reserrp nats))
(consp chars))
(< (len rest-input)
(len input)))
:rule-classes :linear)))
///
(defret len-of-parse-ichars-<=
(<= (len rest-input)
(len input))
:rule-classes :linear)
(defret len-of-parse-ichars-<
(implies (and (not (reserrp tree))
(consp (str::explode chars)))
(< (len rest-input)
(len input)))
:rule-classes :linear))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define parse-schars ((chars acl2::stringp) (input nat-listp))
:returns (mv (tree tree-resultp)
(rest-input nat-listp))
:short "Parse a case-sensitive character value consisting of
a given string of characters."
(b* (((mv nats input) (parse-schars-aux (str::explode chars) input))
((when (reserrp nats)) (mv (reserrf-push nats) input)))
(mv (tree-leafterm nats)
input))
:hooks (:fix)
:prepwork
((define parse-schars-aux ((chars character-listp) (input nat-listp))
:returns
(mv (nats
nat-list-resultp
:hints
(("Goal"
:in-theory
(enable
acl2::natp-when-nat-resultp-and-not-reserrp
acl2::nat-listp-when-nat-list-resultp-and-not-reserrp))))
(rest-input nat-listp))
:parents nil
(b* (((when (endp chars)) (mv nil (nat-list-fix input)))
((mv nat? input1) (parse-next input))
((when (reserrp nat?)) (mv (reserrf-push nat?) (nat-list-fix input)))
(nat nat?)
((unless (nat-match-sensitive-char-p nat (car chars)))
(mv (reserrf (list :found nat :required (acl2::char-fix (car chars))))
(nat-list-fix input)))
((mv nats? input2) (parse-schars-aux (cdr chars) input1))
((when (reserrp nats?)) (mv (reserrf-push nats?) input1))
(nats nats?))
(mv (cons nat nats) input2))
:hooks (:fix)
///
(defret len-of-parse-schars-aux-<=
(<= (len rest-input)
(len input))
:rule-classes :linear)
(defret len-of-parse-schars-aux-<
(implies (and (not (reserrp nats))
(consp chars))
(< (len rest-input)
(len input)))
:rule-classes :linear)))
///
(defret len-of-parse-schars-<=
(<= (len rest-input)
(len input))
:rule-classes :linear)
(defret len-of-parse-schars-<
(implies (and (not (reserrp tree))
(consp (str::explode chars)))
(< (len rest-input)
(len input)))
:rule-classes :linear))
|