def to_value(json): dbgprint("to_value", json) if json is pycket_json.json_false: return values.w_false elif json is pycket_json.json_true: return values.w_true if json.is_object: # The json-object should only contain one element obj = json.value_object() if "vector" in obj: return vector.W_Vector.fromelements( [to_value(v) for v in obj["vector"].value_array()], immutable=True) if "struct" in obj: key = to_value(obj["prefab-key"]) fields = [to_value(v) for v in obj["struct"].value_array()] return values_struct.W_Struct.make_prefab(key, fields) if "box" in obj: return values.W_IBox(to_value(obj["box"])) if "number" in obj: return _to_num(obj["number"]) if "path" in obj: return values.W_Path(obj["path"].value_string()) if "char" in obj: return values.W_Character.make( unichr(int(obj["char"].value_string()))) if "hash-keys" in obj and "hash-vals" in obj: return values_hash.W_EqualHashTable( [to_value(i) for i in obj["hash-keys"].value_array()], [to_value(i) for i in obj["hash-vals"].value_array()]) if "regexp" in obj: return values_regex.W_Regexp(obj["regexp"].value_string()) if "byte-regexp" in obj: arr = decode_byte_array(obj["byte-regexp"]) return values_regex.W_ByteRegexp("".join(arr)) if "pregexp" in obj: return values_regex.W_PRegexp(obj["pregexp"].value_string()) if "byte-pregexp" in obj: arr = decode_byte_array(obj["byte-pregexp"]) return values_regex.W_BytePRegexp("".join(arr)) if "bytes" in obj: arr = decode_byte_array(obj["bytes"]) return values.W_ImmutableBytes(arr) if "string" in obj: return values_string.W_String.make( str(obj["string"].value_string())) if "keyword" in obj: return values.W_Keyword.make(str(obj["keyword"].value_string())) if "improper" in obj: improper = obj["improper"].value_array() return values.to_improper( [to_value(v) for v in improper[0].value_array()], to_value(improper[1])) for i in ["toplevel", "lexical", "module", "source-name"]: if i in obj: return values.W_Symbol.make(obj[i].value_string()) if json.is_array: return values.to_list([to_value(j) for j in json.value_array()]) assert 0, "Unexpected json value: %s" % json.tostring()
def pregrexp(w_str): return values_regex.W_PRegexp(w_str.as_str_utf8())