From 402e2980115c5b26ffc0e145726b7e265286d8a6 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Mon, 15 Aug 2016 22:02:26 -0400 Subject: [PATCH] Factor bytes and errors into Common submodule --- src/Common.jl | 11 +++++++++++ src/JSON.jl | 22 ++++------------------ src/Parser.jl | 6 +++--- src/bytes.jl | 23 +++++++++++++++++++++++ src/errors.jl | 3 +++ 5 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 src/Common.jl diff --git a/src/Common.jl b/src/Common.jl new file mode 100644 index 0000000..05da8b3 --- /dev/null +++ b/src/Common.jl @@ -0,0 +1,11 @@ +""" +Internal implementation detail. +""" +module Common + +using Compat + +include("bytes.jl") +include("errors.jl") + +end diff --git a/src/JSON.jl b/src/JSON.jl index eab751f..7446601 100644 --- a/src/JSON.jl +++ b/src/JSON.jl @@ -6,9 +6,10 @@ using Compat export json # returns a compact (or indented) JSON representation as a string +include("Common.jl") include("Parser.jl") -include("bytes.jl") +using .Common import .Parser.parse # These are temporary ways to bypass excess memory allocation @@ -63,21 +64,6 @@ lower(x::Real) = Float64(x) const INDENT=true const NOINDENT=false -const REVERSE_ESCAPES = Dict(map(reverse, ESCAPES)) -const escaped = Array(Vector{UInt8}, 256) -for c in 0x00:0xFF - escaped[c + 1] = if c == SOLIDUS - [SOLIDUS] # don't escape this one - elseif c ≥ 0x80 - [c] # UTF-8 character copied verbatim - elseif haskey(REVERSE_ESCAPES, c) - [BACKSLASH, REVERSE_ESCAPES[c]] - elseif iscntrl(Char(c)) || !isprint(Char(c)) - UInt8[BACKSLASH, LATIN_U, hex(c, 4)...] - else - [c] - end -end type State{I} indentstep::Int @@ -134,14 +120,14 @@ end function print_escaped(io::IO, s::AbstractString) @inbounds for c in s - c <= '\x7f' ? Base.write(io, escaped[UInt8(c) + 0x01]) : + c <= '\x7f' ? Base.write(io, ESCAPED_ARRAY[@compat UInt8(c) + 0x01]) : Base.print(io, c) #JSON is UTF8 encoded end end function print_escaped(io::IO, s::Compat.UTF8String) @inbounds for c in s.data - Base.write(io, escaped[c + 0x01]) + Base.write(io, ESCAPED_ARRAY[c + 0x01]) end end diff --git a/src/Parser.jl b/src/Parser.jl index 96f15e2..41bef5b 100644 --- a/src/Parser.jl +++ b/src/Parser.jl @@ -1,12 +1,12 @@ -module Parser #JSON +module Parser # JSON + +using ..Common using Compat import Compat: String export parse -include("bytes.jl") -include("errors.jl") # A string constructor function (not necessarily a type) const _String = VERSION < v"0.4" ? utf8 : Compat.UTF8String diff --git a/src/bytes.jl b/src/bytes.jl index 6417a5a..d33226e 100644 --- a/src/bytes.jl +++ b/src/bytes.jl @@ -42,3 +42,26 @@ const ESCAPES = Dict( LATIN_N => NEWLINE, LATIN_R => RETURN, LATIN_T => TAB) + +const REVERSE_ESCAPES = Dict(map(reverse, ESCAPES)) +const ESCAPED_ARRAY = Array(Vector{UInt8}, 256) +for c in 0x00:0xFF + ESCAPED_ARRAY[c + 1] = if c == SOLIDUS + [SOLIDUS] # don't escape this one + elseif c ≥ 0x80 + [c] # UTF-8 character copied verbatim + elseif haskey(REVERSE_ESCAPES, c) + [BACKSLASH, REVERSE_ESCAPES[c]] + elseif iscntrl(@compat Char(c)) || !isprint(@compat Char(c)) + UInt8[BACKSLASH, LATIN_U, hex(c, 4)...] + else + [c] + end +end + +export BACKSPACE, TAB, NEWLINE, FORM_FEED, RETURN, SPACE, STRING_DELIM, + PLUS_SIGN, DELIMITER, MINUS_SIGN, DECIMAL_POINT, SOLIDUS, DIGIT_ZERO, + DIGIT_NINE, SEPARATOR, LATIN_UPPER_A, LATIN_UPPER_E, LATIN_UPPER_F, + ARRAY_BEGIN, BACKSLASH, ARRAY_END, LATIN_A, LATIN_B, LATIN_E, LATIN_F, + LATIN_L, LATIN_N, LATIN_R, LATIN_S, LATIN_T, LATIN_U, OBJECT_BEGIN, + OBJECT_END, ESCAPES, REVERSE_ESCAPES, ESCAPED_ARRAY diff --git a/src/errors.jl b/src/errors.jl index 32506f3..c9c1c87 100644 --- a/src/errors.jl +++ b/src/errors.jl @@ -7,3 +7,6 @@ const E_BAD_ESCAPE = "Invalid escape sequence" const E_BAD_CONTROL = "ASCII control character in string" const E_LEADING_ZERO = "Invalid leading zero in number" const E_BAD_NUMBER = "Invalid number" + +export E_EXPECTED_EOF, E_UNEXPECTED_EOF, E_UNEXPECTED_CHAR, E_BAD_KEY, + E_BAD_ESCAPE, E_BAD_CONTROL, E_LEADING_ZERO, E_BAD_NUMBER