Skip to content

Commit

Permalink
Update to latest dhall-lang
Browse files Browse the repository at this point in the history
* Implement Text/show builtin
  dhall-lang/dhall-lang#365
* Don't tag encoded expressions with their version
  dhall-lang/dhall-lang#362
* Rename grammar rule for natural-raw:
  dhall-lang/dhall-lang#368
  • Loading branch information
f-f committed Feb 16, 2019
1 parent b3c2aa1 commit 3e70f09
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 49 deletions.
2 changes: 1 addition & 1 deletion dhall-lang
Submodule dhall-lang updated 73 files
+2 −0 Prelude/Text/package.dhall
+13 −0 Prelude/Text/show
+11 −4 README.md
+11 −4 nixops/logical.nix
+8 −53 standard/binary.md
+10 −7 standard/dhall.abnf
+81 −10 standard/semantics.md
+2 −23 standard/versioning.md
+2 −2 tests/import/success/fieldOrderA.dhall
+1 −1 tests/import/success/issue553B.dhall
+1 −0 tests/normalization/success/prelude/Text/show/0A.dhall
+1 −0 tests/normalization/success/prelude/Text/show/0B.dhall
+1 −0 tests/normalization/success/prelude/Text/show/1A.dhall
+1 −0 tests/normalization/success/prelude/Text/show/1B.dhall
+4 −4 tests/parser/success/annotationsA.dhall
+38 −30 tests/parser/success/annotationsB.json
+1 −1 tests/parser/success/asTextB.json
+0 −0 tests/parser/success/blockCommentB.json
+0 −0 tests/parser/success/builtinsB.json
+0 −0 tests/parser/success/collectionImportTypeB.json
+0 −0 tests/parser/success/constructorsB.json
+0 −0 tests/parser/success/doubleB.json
+0 −0 tests/parser/success/doubleQuotedStringB.json
+0 −0 tests/parser/success/environmentVariablesB.json
+0 −0 tests/parser/success/escapedDoubleQuotedStringB.json
+0 −0 tests/parser/success/escapedSingleQuotedStringB.json
+0 −0 tests/parser/success/fieldsB.json
+0 −0 tests/parser/success/forallB.json
+0 −0 tests/parser/success/functionTypeB.json
+0 −0 tests/parser/success/identifierB.json
+0 −0 tests/parser/success/ifThenElseB.json
+0 −0 tests/parser/success/importAltB.json
+1 −1 tests/parser/success/interpolatedDoubleQuotedStringA.dhall
+1 −1 tests/parser/success/interpolatedDoubleQuotedStringB.json
+1 −1 tests/parser/success/interpolatedSingleQuotedStringA.dhall
+1 −1 tests/parser/success/interpolatedSingleQuotedStringB.json
+0 −0 tests/parser/success/labelB.json
+0 −0 tests/parser/success/lambdaB.json
+0 −0 tests/parser/success/largeExpressionB.json
+1 −1 tests/parser/success/letA.dhall
+1 −1 tests/parser/success/letB.json
+0 −0 tests/parser/success/lineCommentB.json
+2 −2 tests/parser/success/listA.dhall
+0 −40 tests/parser/success/listB.dhall
+48 −0 tests/parser/success/listB.json
+0 −0 tests/parser/success/mergeB.json
+0 −0 tests/parser/success/multiletB.json
+1 −1 tests/parser/success/naturalA.dhall
+4 −4 tests/parser/success/naturalB.json
+0 −0 tests/parser/success/nestedBlockCommentB.json
+1 −1 tests/parser/success/operatorsA.dhall
+3 −3 tests/parser/success/operatorsB.json
+1 −1 tests/parser/success/parenthesizeUsingB.json
+7 −7 tests/parser/success/pathTerminationB.json
+0 −0 tests/parser/success/pathsB.json
+0 −0 tests/parser/success/quotedLabelB.json
+1 −1 tests/parser/success/quotedPathsB.json
+2 −2 tests/parser/success/recordA.dhall
+8 −8 tests/parser/success/recordB.json
+1 −1 tests/parser/success/reservedPrefixA.dhall
+1 −1 tests/parser/success/reservedPrefixB.json
+0 −0 tests/parser/success/singleQuotedStringB.json
+0 −0 tests/parser/success/sortB.json
+2 −2 tests/parser/success/templateB.json
+0 −0 tests/parser/success/unicodeCommentB.json
+0 −0 tests/parser/success/unicodeDoubleQuotedStringB.json
+1 −0 tests/parser/success/unicodePathsA.dhall
+8 −0 tests/parser/success/unicodePathsB.json
+0 −0 tests/parser/success/unionB.json
+9 −9 tests/parser/success/urlsB.json
+0 −0 tests/parser/success/whitespaceB.json
+1 −1 tests/parser/success/whitespaceBuffetA.dhall
+1 −1 tests/parser/success/whitespaceBuffetB.json
3 changes: 3 additions & 0 deletions src/dhall_clj/alpha_normalize.clj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
(alpha-normalize [this]
(map-chunks this alpha-normalize))

dhall_clj.ast.TextShow
(alpha-normalize [this] this)

dhall_clj.ast.TextAppend
(alpha-normalize [this]
(-> this
Expand Down
7 changes: 7 additions & 0 deletions src/dhall_clj/ast.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
(defrecord DoubleShow [])
(defrecord TextT [])
(defrecord TextLit [chunks])
(defrecord TextShow [])
(defrecord TextAppend [a b])
(defrecord ListT [])
(defrecord ListLit [type? exprs])
Expand Down Expand Up @@ -292,6 +293,9 @@
(shift [this diff var]
(map-chunks this (fn [c] (shift c diff var))))

TextShow
(shift [this diff var] this)

TextAppend
(shift [this diff var]
(-> this
Expand Down Expand Up @@ -596,6 +600,9 @@
(subst [this var e]
(map-chunks this (fn [c] (subst c var e))))

TextShow
(subst [this var e] this)

TextAppend
(subst [this var e]
(-> this
Expand Down
31 changes: 30 additions & 1 deletion src/dhall_clj/beta_normalize.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
(ns dhall-clj.beta-normalize
(:require [dhall-clj.alpha-normalize :refer [alpha-normalize]]
[dhall-clj.ast :refer :all]
[clojure.string :as string]
[medley.core :refer [map-vals]])
(:import [dhall_clj.ast NaturalLit TextLit BoolLit Lam App ListBuild ListFold
NaturalBuild NaturalFold OptionalBuild OptionalFold NaturalIsZero
NaturalEven NaturalOdd NaturalToInteger NaturalShow IntegerLit
IntegerShow IntegerToDouble DoubleLit DoubleShow ListLit ListLength
ListHead ListLast ListIndexed ListReverse RecordLit
ListHead ListLast ListIndexed ListReverse RecordLit TextShow
UnionLit RecordT UnionT Some None]))

(defprotocol IBetaNormalize
Expand All @@ -23,6 +24,24 @@
(ab-normalize b))))


(defn text-show [text]
(let [char-map {\" "\\\""
\$ "\\u0024"
\\ "\\\\"
\backspace "\\b"
\newline "\\n"
\return "\\r"
\tab "\\t"}
escape-control (fn [char]
(if (<= (int char) 30)
(format "\\u%04x" (int char))
char))]
(str "\""
(apply str (map escape-control
(seq (string/escape text char-map))))
"\"")))


(extend-protocol IBetaNormalize

dhall_clj.ast.Const
Expand Down Expand Up @@ -266,6 +285,13 @@
val (:e (:b (:a (:a f'))))]
(beta-normalize (->App just val)))

;; TextShow
(and (instance? TextShow f')
(instance? TextLit a')
(= 1 (count (:chunks a')))
(string? (first (:chunks a'))))
(beta-normalize (update a' :chunks (partial map text-show)))

:else (->App f' a'))))))

dhall_clj.ast.Let
Expand Down Expand Up @@ -469,6 +495,9 @@
:else (->TextAppend l r)))]
(decide (beta-normalize a) (beta-normalize b))))

dhall_clj.ast.TextShow
(beta-normalize [this] this)

dhall_clj.ast.ListT
(beta-normalize [this] this)

Expand Down
27 changes: 9 additions & 18 deletions src/dhall_clj/binary.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,17 @@
(declare cbor)
(declare decbor)

(def protocol-version
"The currently supported version for the binary protocol"
"5.0.0")

(def supported-versions
"A list of versions that will be accepted for deserialization"
[protocol-version])

(defn encode
"Encode `e` (which should be Dhall AST) into its binary form.
`version` is one of `supported-versions`. Will return a `ByteArray`."
([e] (encode e protocol-version))
([e version]
(if (some #{version} supported-versions)
(cbor/encode [version (cbor e)])
(fail/unsupported-version-encoding! version supported-versions))))
Will return a `ByteArray`."
[e]
(cbor/encode (cbor e)))

(defn decode
"Takes a bytearray `binary-expr`, and tries to decode it into Dhall AST.
Will throw an exception of type `dhall-clj.fail/binary` on malformed input."
[^bytes binary-expr]
(let [[version expression] (cbor/decode binary-expr)]
(if (some #{version} supported-versions)
(decbor expression)
(fail/unsupported-version-decoding! version supported-versions))))
(decbor (cbor/decode binary-expr)))


;;;; CBOR -> Dhall AST
Expand Down Expand Up @@ -78,6 +64,7 @@
"List/reverse" (->ListReverse)
"Optional/fold" (->OptionalFold)
"Optional/build" (->OptionalBuild)
"Text/show" (->TextShow)
"Bool" (->BoolT)
"Optional" (->OptionalT)
"None" (->None)
Expand Down Expand Up @@ -382,6 +369,10 @@
(cbor [{:keys [chunks]}]
(into [] (concat [18] (map #(if (string? %) % (cbor %)) chunks))))

dhall_clj.ast.TextShow
(cbor [this]
"Text/show")

dhall_clj.ast.TextAppend
(cbor [{:keys [a b]}]
[3 6 (cbor a) (cbor b)])
Expand Down
4 changes: 4 additions & 0 deletions src/dhall_clj/emit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@
(apply str chunks')
`(apply str ~chunks'))))

dhall_clj.ast.TextShow
(emit [this]
`str) ;; TODO

dhall_clj.ast.TextAppend
(emit [{:keys [a b]}]
`(str ~(emit a) ~(emit b)))
Expand Down
18 changes: 0 additions & 18 deletions src/dhall_clj/fail.clj
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,6 @@
;; Serialization
;;

(defn unsupported-version-encoding!
"Throws an ex-info if the version we are trying to encode with is not supported"
[version versions]
(throw-data
"Serialization error: version not supported"
{:type ::unsupported-version-encoding
:supported-versions versions
:current-version version}))

(defn unsupported-version-decoding!
"Throws an ex-info if the version we are trying to decode with is not supported"
[version versions]
(throw-data
"Deserialization error: version not supported"
{:type ::unsupported-version-decoding
:supported-versions versions
:current-version version}))

(defn vector-too-short!
"Throws an ex-info if the vector `e` has less than `n` elems"
[e n]
Expand Down
3 changes: 3 additions & 0 deletions src/dhall_clj/import.clj
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@
(resolve-imports [this state]
(map-chunks this (fn [c] (resolve-imports c state))))

dhall_clj.ast.TextShow
(resolve-imports [this state] this)

dhall_clj.ast.TextAppend
(resolve-imports [this state]
(-> this
Expand Down
11 changes: 6 additions & 5 deletions src/dhall_clj/parse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"List/reverse"
"Optional/fold"
"Optional/build"
"Text/show"
"if"
"then"
"else"
Expand Down Expand Up @@ -348,7 +349,8 @@
:List-indexed-raw (->ListIndexed)
:List-reverse-raw (->ListReverse)
:Optional-fold-raw (->OptionalFold)
:Optional-build-raw (->OptionalBuild))))
:Optional-build-raw (->OptionalBuild)
:Text-show-raw (->TextShow))))

(defn identifier [e]
(let [children (:c e)
Expand All @@ -359,7 +361,7 @@
(->> children first :c first :c (apply str)))
;; at the end of `children` there might be a DeBrujin index
maybe-index (-> children butlast last)
index? (= :natural-raw (:t maybe-index))
index? (= :natural-literal-raw (:t maybe-index))
index (if index?
(-> maybe-index :c first :c first read-string)
0)
Expand Down Expand Up @@ -622,9 +624,8 @@
"t" "\t"
;; Otherwise we're reading in a \uXXXX char
(->> (nthrest content 2)
(mapv (fn [{:keys [c]}]
(-> c first :c first)))
(apply str "0x")
compact
(str "0x")
hex->num
char))]
(recur (rest children)
Expand Down
4 changes: 4 additions & 0 deletions src/dhall_clj/typecheck.clj
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@
chunks)
(->TextT))

dhall_clj.ast.TextShow
(typecheck [this _ctx]
(->Pi "_" (->TextT) (->TextT)))

dhall_clj.ast.TextAppend
(typecheck [{:keys [a b] :as this} ctx]
(typecheck-binary this ctx TextT fail/cant-text-append!))
Expand Down
1 change: 1 addition & 0 deletions test/dhall_clj/binary_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
(->DoubleT)
(->DoubleShow)
(->TextT)
(->TextShow)
(->ListT)
(->ListBuild)
(->ListFold)
Expand Down
2 changes: 1 addition & 1 deletion test/dhall_clj/import_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[me.raynes.fs :as fs]))


(def prelude-hash "26e13b153cb428366610110d4d8f0c135e22b20179d5478bb16b1b83b3f2ca13")
(def prelude-hash "e3be3dba308637ad7ab6d4ce9a11a342b087efbf2aa801c88a05a6babaae8e48")

(def simple-success-cases
{"Prelude import with hash"
Expand Down
6 changes: 2 additions & 4 deletions test/dhall_clj/parser_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,6 @@
"Here we list all the tests that blow up, so we categorize and exclude them.
Note: they are vectors because the path creation is platform-sensitive."
[
;; Waiting for https://github.com/dhall-lang/dhall-haskell/pull/779
["dhall-lang" "tests" "parser" "success" "annotations"]
["dhall-lang" "tests" "parser" "success" "list"]
;; No CBOR for imports yet
["dhall-lang" "tests" "parser" "success" "collectionImportType"]
["dhall-lang" "tests" "parser" "success" "parenthesizeUsing"]
Expand All @@ -324,7 +321,8 @@
["dhall-lang" "tests" "parser" "success" "pathTermination"]
["dhall-lang" "tests" "parser" "success" "importAlt"]
["dhall-lang" "tests" "parser" "success" "asText"]
;; Broken operators?
["dhall-lang" "tests" "parser" "success" "unicodePaths"]
;; https://github.com/dhall-lang/dhall-lang/issues/373
["dhall-lang" "tests" "parser" "success" "operators"]
;; Something's broken
["dhall-lang" "tests" "parser" "success" "largeExpression"]
Expand Down
6 changes: 5 additions & 1 deletion test/dhall_clj/test_utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
[test-folder]
(let [files (->> (list-files test-folder)
(remove failure-case?))
map-of-testcases (group-by #(->> % str (drop-last 7) (apply str)) files)]
map-of-testcases (group-by #(-> % str
(string/replace #"B.json" "")
(string/replace #"A.dhall" "")
(string/replace #"B.dhall" ""))
files)]
(map-vals
(fn [a-and-b]
;; We sort so we get the A.dhall file first
Expand Down

0 comments on commit 3e70f09

Please sign in to comment.