From a2f8078dcc79d7f4aa0f596b08f4402546df5ddb Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 1 Jul 2017 16:09:02 -0400 Subject: - Text no longer has a codec (because the decoding didn't handle unicode escaping). --- stdlib/source/lux/data/format/json.lux | 22 ++++++------- stdlib/source/lux/data/text.lux | 46 ++++++++------------------- stdlib/source/lux/data/text/format.lux | 2 +- stdlib/source/lux/data/text/lexer.lux | 13 ++++---- stdlib/source/lux/macro/code.lux | 4 ++- stdlib/source/lux/macro/poly/text-encoder.lux | 2 +- stdlib/test/test/lux/data/text.lux | 13 -------- 7 files changed, 35 insertions(+), 67 deletions(-) diff --git a/stdlib/source/lux/data/format/json.lux b/stdlib/source/lux/data/format/json.lux index 573849b9e..0ce1b602a 100644 --- a/stdlib/source/lux/data/format/json.lux +++ b/stdlib/source/lux/data/format/json.lux @@ -109,11 +109,11 @@ ## [Values] (def: #hidden (show-null _) (-> Null Text) "null") (do-template [ ] - [(def: (-> Text) (:: encode))] + [(def: (-> Text) )] - [show-boolean Boolean bool;Codec] - [show-number Number number;Codec] - [show-string String text;Codec]) + [show-boolean Boolean (:: bool;Codec encode)] + [show-number Number (:: number;Codec encode)] + [show-string String text;encode]) (def: (show-array show-json elems) (-> (-> JSON Text) (-> Array Text)) @@ -126,7 +126,7 @@ (format "{" (|> object d;entries - (L/map (function [[key value]] (format (:: text;Codec encode key) ":" (show-json value)))) + (L/map (function [[key value]] (format (show-string key) ":" (show-json value)))) (text;join-with ",")) "}")) @@ -395,7 +395,7 @@ [text Text #String "text" id] ) -(do-template [
]
+(do-template [       
]
   [(def: #export ( test json)
      {#;doc (#;TextA (format "Asks whether a JSON value is a "  "."))}
      (->  (Parser Bool))
@@ -415,15 +415,15 @@
          (if (::  = test value)
            (#R;Success [])
            (#R;Error (format "Value mismatch: "
-                             (::  encode test) "=/=" (::  encode value)))))
+                             ( test) "=/=" ( value)))))
 
        _
        (#R;Error (format "JSON value is not a "  ": " (show-json json)))))]
 
-  [bool? bool! Bool bool;Eq   bool;Codec   #Boolean "boolean" id]
-  [int?  int!  Int  number;Eq  number;Codec  #Number  "number"  real-to-int]
-  [real? real! Real number;Eq number;Codec #Number  "number"  id]
-  [text? text! Text text;Eq   text;Codec   #String  "string"  id]
+  [bool? bool! Bool bool;Eq   (:: bool;Codec encode)   #Boolean "boolean" id]
+  [int?  int!  Int  number;Eq  (:: number;Codec encode)  #Number  "number"  real-to-int]
+  [real? real! Real number;Eq (:: number;Codec encode) #Number  "number"  id]
+  [text? text! Text text;Eq   text;encode                         #String  "string"  id]
   )
 
 (def: #export (char json)
diff --git a/stdlib/source/lux/data/text.lux b/stdlib/source/lux/data/text.lux
index ad33c67ac..dca74423c 100644
--- a/stdlib/source/lux/data/text.lux
+++ b/stdlib/source/lux/data/text.lux
@@ -134,39 +134,19 @@
 
 (open Monoid)
 
-(struct: #export _ (Codec Text Text)
-  (def: (encode original)
-    (let [escaped (|> original
-                      (replace-all "\\" "\\\\")
-                      (replace-all "\t" "\\t")
-                      (replace-all "\v" "\\v")
-                      (replace-all "\b" "\\b")
-                      (replace-all "\n" "\\n")
-                      (replace-all "\r" "\\r")
-                      (replace-all "\f" "\\f")
-                      (replace-all "\"" "\\\"")
-                      )]
-      ($_ append "\"" escaped "\"")))
-
-  (def: (decode input)
-    (if (and (starts-with? "\"" input)
-             (ends-with? "\"" input))
-      (case (clip +1 (n.dec (size input)) input)
-        (#;Some input')
-        (|> input'
-            (replace-all "\\\\" "\\")
-            (replace-all "\\t" "\t")
-            (replace-all "\\v" "\v")
-            (replace-all "\\b" "\b")
-            (replace-all "\\n" "\n")
-            (replace-all "\\r" "\r")
-            (replace-all "\\f" "\f")
-            (replace-all "\\\"" "\"")
-            #;Some)
-
-        #;None
-        (#;Left "Could not decode text"))
-      (#;Left "Could not decode text"))))
+(def: #export (encode original)
+  (-> Text Text)
+  (let [escaped (|> original
+                    (replace-all "\\" "\\\\")
+                    (replace-all "\t" "\\t")
+                    (replace-all "\v" "\\v")
+                    (replace-all "\b" "\\b")
+                    (replace-all "\n" "\\n")
+                    (replace-all "\r" "\\r")
+                    (replace-all "\f" "\\f")
+                    (replace-all "\"" "\\\"")
+                    )]
+    ($_ append "\"" escaped "\"")))
 
 (struct: #export _ (Hash Text)
   (def: eq Eq)
diff --git a/stdlib/source/lux/data/text/format.lux b/stdlib/source/lux/data/text/format.lux
index 127921e41..639a2f39b 100644
--- a/stdlib/source/lux/data/text/format.lux
+++ b/stdlib/source/lux/data/text/format.lux
@@ -39,7 +39,7 @@
   [%d     Deg   (:: number;Codec encode)]
   [%r     Real  (:: number;Codec encode)]
   [%c     Char  (:: char;Codec encode)]
-  [%t     Text  (:: text;Codec encode)]
+  [%t     Text  text;encode]
   [%ident Ident (:: ident;Codec encode)]
   [%code  Code  code;to-text]
   [%type  Type  type;to-text]
diff --git a/stdlib/source/lux/data/text/lexer.lux b/stdlib/source/lux/data/text/lexer.lux
index 8c40af821..3d7423ca2 100644
--- a/stdlib/source/lux/data/text/lexer.lux
+++ b/stdlib/source/lux/data/text/lexer.lux
@@ -59,8 +59,7 @@
       (case (text;split (text;size reference) input)
         #;None              (#R;Error "")
         (#;Some [_ input']) (#R;Success [input' []]))
-      (let [(^open "T/") text;Codec]
-        (#R;Error (format "Invalid match: " (T/encode reference) " @ " (T/encode input)))))))
+      (#R;Error (format "Invalid match: " (text;encode reference) " @ " (text;encode input))))))
 
 (def: #export (this? reference)
   {#;doc "Lex a text if it matches the given sample."}
@@ -79,7 +78,7 @@
   (function [input]
     (case input
       "" (#R;Success [input []])
-      _  (#R;Error (format "The text input has not been fully consumed @ " (:: text;Codec encode input)))
+      _  (#R;Error (format "The text input has not been fully consumed @ " (text;encode input)))
       )))
 
 (def: #export peek
@@ -107,7 +106,7 @@
     [input get-input
      char any
      #let [char' (|> char (text;nth +0) assume)]
-     _ (p;assert (format "Character is not within range: " (C/encode bottom) "-" (C/encode top) " @ " (:: text;Codec encode input))
+     _ (p;assert (format "Character is not within range: " (C/encode bottom) "-" (C/encode top) " @ " (text;encode input))
                  (and (C/>= bottom char')
                       (C/<= top char')))]
     (wrap char)))
@@ -155,7 +154,7 @@
 
           _
           (#R;Error ""))
-        (#R;Error (format "Character (" init ") is not one of: " options " @ " (:: text;Codec encode input))))
+        (#R;Error (format "Character (" init ") is not one of: " options " @ " (text;encode input))))
 
       _
       (#R;Error "Cannot parse character from empty text."))))
@@ -173,7 +172,7 @@
 
           _
           (#R;Error ""))
-        (#R;Error (format "Character (" init ") is one of: " options " @ " (:: text;Codec encode input))))
+        (#R;Error (format "Character (" init ") is one of: " options " @ " (text;encode input))))
 
       _
       (#R;Error "Cannot parse character from empty text."))))
@@ -190,7 +189,7 @@
       (#;Some [input' output])
       (if (p output)
         (#R;Success [input' (char;as-text output)])
-        (#R;Error (format "Character does not satisfy predicate: " (:: text;Codec encode input))))
+        (#R;Error (format "Character does not satisfy predicate: " (text;encode input))))
 
       _
       (#R;Error "Cannot parse character from empty text."))))
diff --git a/stdlib/source/lux/macro/code.lux b/stdlib/source/lux/macro/code.lux
index 6d2dd4604..caa846e61 100644
--- a/stdlib/source/lux/macro/code.lux
+++ b/stdlib/source/lux/macro/code.lux
@@ -108,9 +108,11 @@
      [#;Deg    Codec]
      [#;Real   Codec]
      [#;Char   char;Codec]
-     [#;Text   text;Codec]
      [#;Symbol Codec])
 
+    [_ (#;Text value)]
+    (text;encode value)
+
     [_ (#;Tag ident)]
     (Text/append  "#" (:: Codec encode ident))
 
diff --git a/stdlib/source/lux/macro/poly/text-encoder.lux b/stdlib/source/lux/macro/poly/text-encoder.lux
index e1250c9e7..af0cff4f8 100644
--- a/stdlib/source/lux/macro/poly/text-encoder.lux
+++ b/stdlib/source/lux/macro/poly/text-encoder.lux
@@ -49,7 +49,7 @@
                  [Deg poly;deg (:: number;Codec encode)]
                  [Real poly;real (:: number;Codec encode)]
                  [Char poly;char (:: char;Codec encode)]
-                 [Text poly;text (:: text;Codec encode)])]
+                 [Text poly;text text;encode])]
       ($_ macro;either
           ## Primitives
           
diff --git a/stdlib/test/test/lux/data/text.lux b/stdlib/test/test/lux/data/text.lux
index fafba01e2..bf509ff53 100644
--- a/stdlib/test/test/lux/data/text.lux
+++ b/stdlib/test/test/lux/data/text.lux
@@ -141,16 +141,3 @@
         (test "" (&/>= "abc" "abc"))
         (test "" (not (&/>= "bcd" "abc")))
         )))
-
-(context: "Codec"
-  [size bounded-size
-   sample (R;text size)
-   #let [(^open) &;Eq]]
-  (test "" (|> sample
-               (:: &;Codec encode)
-               (:: &;Codec decode)
-               (case> (#;Right decoded)
-                      (= sample decoded)
-
-                      _
-                      false))))
-- 
cgit v1.2.3