(* * Copyright (c) 2000 Carnegie Mellon University. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The name "Carnegie Mellon University" must not be used to * endorse or promote products derived from this software without * prior written permission. For permission or any other legal * details, please contact * Office of Technology Transfer * Carnegie Mellon University * 5000 Forbes Avenue * Pittsburgh, PA 15213-3890 * (412) 268-4387, fax: (412) 268-7395 * tech-transfer@andrew.cmu.edu * * 4. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by Computing Services * at Carnegie Mellon University (http://www.cmu.edu/computing/)." * * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *) structure Value :> VALUE = struct datatype value = Single of string | List of string list | Nil | Default exception DefaultValue val debugLiteral = ref false val safeMap = CharMap.mkCharMap {bindings=[("\"\\", false), ("\r\n", false)], default=true} val safeChar = CharMap.mapChr safeMap fun stringAll f = let fun g NONE = true | g (SOME (c, ss)) = f c andalso g (Substring.getc ss) in g o Substring.getc o Substring.all end val safe = stringAll safeChar fun itos 0 = "0" | itos n = let fun d 0 = #"0" | d 1 = #"1" | d 2 = #"2" | d 3 = #"3" | d 4 = #"4" | d 5 = #"5" | d 6 = #"6" | d 7 = #"7" | d 8 = #"8" | d 9 = #"9" fun f(0, l) = l | f(n, l) = f(n div 10, (d (n mod 10))::l) in String.implode (f(n, [])) end fun wrap s = let val l = String.size s val isquote = l < 256 andalso safe s val s = if isquote then concat ["\"", s, "\""] else concat ["{", itos l, "}\r\n", s] val _ = if !debugLiteral andalso not isquote then TextIO.print (concat ["[", s, "]\n"]) else () in s end fun toString (Single s) = wrap s | toString (List nil) = toString (Nil) | toString (List l) = ListFormat.fmt {init="(", sep=" ", final=")", fmt=wrap} l | toString (Nil) = "NIL" | toString (Default) = raise DefaultValue fun size (Single s) = [String.size s] | size (List l) = List.map String.size l | size Nil = nil | size Default = nil fun eat "" getc = (fn strm => (true, strm)) | eat s getc = let val n = String.size s fun isPrefix (i, strm) = if (i = n) then SOME strm else (case getc strm of (SOME(c, strm)) => if (String.sub(s, i) = c) then isPrefix(i+1, strm) else NONE | NONE => NONE) fun eat' strm = (case isPrefix (0, strm) of (SOME strm) => (true, strm) | NONE => (false, strm)) in eat' end (* this is the "safe" implementation *) fun grab len getc strm = let val arr = CharArray.array (len, #"\000") fun grab' (n, strm) = if n = len then SOME (CharArray.extract (arr, 0, NONE), strm) else (case getc strm of (SOME (c, strm)) => (CharArray.update (arr, n, c); grab' (n+1, strm)) | NONE => NONE) in grab' (0, strm) end (* but this one is faster *) fun grab len getc strm = let val arr = Unsafe.CharVector.create len fun grab' (n, strm) = if n = len then SOME (arr, strm) else (case getc strm of (SOME (c, strm)) => (Unsafe.CharVector.update (arr, n, c); grab' (n+1, strm)) | NONE => NONE) in grab' (0, strm) end fun scanOne getc strm = case getc strm of (SOME (#"{", strm)) => let (* literal *) val scanInt = Int.scan StringCvt.DEC getc in case scanInt strm of NONE => NONE | (SOME (len, strm)) => (case eat "}\r\n" getc strm of (true, strm) => (* grab len bytes *) (case grab len getc strm of NONE => NONE | s => s) | (false, strm) => NONE) end | (SOME (#"\"", strm)) => let val (s, strm) = StringCvt.splitl (fn c => c <> #"\"") getc strm in case (getc strm) of NONE => NONE | (SOME (#"\"", strm)) => SOME (s, strm) | _ => NONE end | _ => NONE fun scanList getc = ListFormat.scan {init="(", sep=" ", final=")", scan=scanOne} getc (* val scan : (char, 'a) StringCvt.reader -> (value, 'a) StringCvt.reader *) fun scan getc strm = case getc strm of (SOME (#"(", _)) => (case scanList getc strm of NONE => NONE | (SOME (l, strm)) => SOME (List l, strm)) | (SOME (#"N", strm')) => (case eat "NIL" getc strm of (true, strm) => SOME (Nil, strm) | (false, _) => NONE) | (SOME (_, strm')) => (case scanOne getc strm of NONE => NONE | (SOME (s, strm)) => SOME (Single s, strm)) | NONE => NONE val fmt = toString end