|
| 1 | +// RUN: %verify "%s" |
| 2 | + |
| 3 | +include "Grammar.dfy" |
| 4 | + |
| 5 | +module {:options "-functionSyntax:4"} JSON.ConcreteSyntax.Spec { |
| 6 | + import opened BoundedInts |
| 7 | + |
| 8 | + import Vs = Utils.Views.Core |
| 9 | + import opened Grammar |
| 10 | + |
| 11 | + function View(v: Vs.View) : bytes { |
| 12 | + v.Bytes() |
| 13 | + } |
| 14 | + |
| 15 | + function Structural<T>(self: Structural<T>, fT: T -> bytes): bytes { |
| 16 | + View(self.before) + fT(self.t) + View(self.after) |
| 17 | + } |
| 18 | + |
| 19 | + function StructuralView(self: Structural<Vs.View>): bytes { |
| 20 | + Structural<Vs.View>(self, View) |
| 21 | + } |
| 22 | + |
| 23 | + function Maybe<T>(self: Maybe<T>, fT: T -> bytes): (bs: bytes) |
| 24 | + ensures self.Empty? ==> bs == [] |
| 25 | + ensures self.NonEmpty? ==> bs == fT(self.t) |
| 26 | + { |
| 27 | + if self.Empty? then [] else fT(self.t) |
| 28 | + } |
| 29 | + |
| 30 | + function ConcatBytes<T>(ts: seq<T>, fT: T --> bytes) : bytes |
| 31 | + requires forall d | d in ts :: fT.requires(d) |
| 32 | + { |
| 33 | + if |ts| == 0 then [] |
| 34 | + else fT(ts[0]) + ConcatBytes(ts[1..], fT) |
| 35 | + } |
| 36 | + |
| 37 | + function Bracketed<D, S>(self: Bracketed<Vs.View, D, S, Vs.View>, fDatum: Suffixed<D, S> --> bytes): bytes |
| 38 | + requires forall d | d < self :: fDatum.requires(d) |
| 39 | + { |
| 40 | + StructuralView(self.l) + |
| 41 | + ConcatBytes(self.data, fDatum) + |
| 42 | + StructuralView(self.r) |
| 43 | + } |
| 44 | + |
| 45 | + function KeyValue(self: jKeyValue): bytes { |
| 46 | + String(self.k) + StructuralView(self.colon) + Value(self.v) |
| 47 | + } |
| 48 | + |
| 49 | + function Frac(self: jfrac): bytes { |
| 50 | + View(self.period) + View(self.num) |
| 51 | + } |
| 52 | + |
| 53 | + function Exp(self: jexp): bytes { |
| 54 | + View(self.e) + View(self.sign) + View(self.num) |
| 55 | + } |
| 56 | + |
| 57 | + function Number(self: jnumber): bytes { |
| 58 | + View(self.minus) + View(self.num) + Maybe(self.frac, Frac) + Maybe(self.exp, Exp) |
| 59 | + } |
| 60 | + |
| 61 | + function String(self: jstring): bytes { |
| 62 | + View(self.lq) + View(self.contents) + View(self.rq) |
| 63 | + } |
| 64 | + |
| 65 | + function CommaSuffix(c: Maybe<Structural<jcomma>>): bytes { |
| 66 | + // BUG(https://github.com/dafny-lang/dafny/issues/2179) |
| 67 | + Maybe<Structural<Vs.View>>(c, StructuralView) |
| 68 | + } |
| 69 | + |
| 70 | + function Member(self: jmember) : bytes { |
| 71 | + KeyValue(self.t) + CommaSuffix(self.suffix) |
| 72 | + } |
| 73 | + |
| 74 | + function Item(self: jitem) : bytes { |
| 75 | + Value(self.t) + CommaSuffix(self.suffix) |
| 76 | + } |
| 77 | + |
| 78 | + function Object(obj: jobject) : bytes { |
| 79 | + Bracketed(obj, (d: jmember) requires d < obj => Member(d)) |
| 80 | + } |
| 81 | + |
| 82 | + function Array(arr: jarray) : bytes { |
| 83 | + Bracketed(arr, (d: jitem) requires d < arr => Item(d)) |
| 84 | + } |
| 85 | + |
| 86 | + function Value(self: Value) : bytes { |
| 87 | + match self { |
| 88 | + case Null(n) => View(n) |
| 89 | + case Bool(b) => View(b) |
| 90 | + case String(str) => String(str) |
| 91 | + case Number(num) => Number(num) |
| 92 | + case Object(obj) => Object(obj) |
| 93 | + case Array(arr) => Array(arr) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + lemma UnfoldValueNumber(v: Value) |
| 98 | + requires v.Number? |
| 99 | + ensures Value(v) == Number(v.num) |
| 100 | + { |
| 101 | + assert Value(v) == match v { case Number(num) => Number(num) case _ => []}; |
| 102 | + } |
| 103 | + |
| 104 | + lemma UnfoldValueObject(v: Value) |
| 105 | + requires v.Object? |
| 106 | + ensures Value(v) == Object(v.obj) |
| 107 | + { |
| 108 | + assert Value(v) == match v { case Object(obj) => Object(obj) case _ => []}; |
| 109 | + } |
| 110 | + |
| 111 | + lemma UnfoldValueArray(v: Value) |
| 112 | + requires v.Array? |
| 113 | + ensures Value(v) == Array(v.arr) |
| 114 | + { |
| 115 | + assert Value(v) == match v { case Array(arr) => Array(arr) case _ => []}; |
| 116 | + } |
| 117 | + |
| 118 | + function JSON(js: JSON) : bytes { |
| 119 | + Structural(js, Value) |
| 120 | + } |
| 121 | +} |
0 commit comments