Class: DParse::Parsers::JSON
- Inherits:
-
DParse::Parser
- Object
- DParse::Parser
- DParse::Parsers::JSON
- Defined in:
- lib/d-parse/parsers/highlevel/json.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ JSON
constructor
A new instance of JSON.
Methods inherited from DParse::Parser
#apply, #bind, #capture, #compact, #expectation_message, #first, #flatten, #ignore, #inspect, #map, #match?, #read, #second, #select_even, #select_odd, #to_s
Constructor Details
#initialize ⇒ JSON
Returns a new instance of JSON.
232 233 234 |
# File 'lib/d-parse/parsers/highlevel/json.rb', line 232 def initialize(*) raise ArgumentError, "#{self.class} is not supposed to be initialized" end |
Class Method Details
.new ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/d-parse/parsers/highlevel/json.rb', line 4 def self.new extend DParse::DSL json_value = nil # Undefined for now whitespace = repeat( alt( char(' '), char("\t"), char("\r"), char("\n"), ), ) # String json_digit_hex = alt( char_in('0'..'9'), char_in('a'..'f'), char_in('A'..'F'), ) json_string = seq( char('"').ignore, repeat( alt( char_not_in(%w(" \\ )).capture, seq( char('\\').ignore, alt( char_in(%w(" \\ / b f n r t)).capture, seq( char('u').capture, seq( json_digit_hex, json_digit_hex, json_digit_hex, json_digit_hex, ).capture, ), ), ).compact, ), ), char('"').ignore, ).compact.first.map do |d, _, _| new_chars = d.map do |char| case char when ::String char when ::Array case char[0] when '"' '"' when '\\' '\\' when '/' '/' when 'b' "\b" when 'f' "\f" when 'n' "\n" when 'r' "\r" when 't' "\t" else if char[0].is_a?(Array) && char[0][0] == 'u' char[0][1].to_i(16).chr(Encoding::UTF_8) else raise "Unexpected escape sequence #{char[0].inspect}" end end else raise "??? #{char.inspect} (#{char.class})" end end new_chars.join('') end # Array json_elements = intersperse( lazy { json_value }, seq( whitespace, char(','), whitespace, ).ignore, ).compact json_array = seq( char('[').ignore, whitespace.ignore, json_elements, whitespace.ignore, char(']').ignore, ).compact.first # Misc json_true = string('true').map { true } json_false = string('false').map { false } json_null = string('null').map { nil } # Number json_digit = char_in('0'..'9') json_number = seq( opt(char('-')).capture, alt( char('0'), seq( char_in('1'..'9'), repeat(json_digit), ), ).capture, opt( seq( char('.').ignore, repeat(json_digit).capture, ).compact, ), opt( seq( alt(char('e'), char('E')), alt(char('+'), char('-'), succeed).capture, repeat(json_digit).capture, ).compact, ), ).map do |d, _, _| sign_char = d[0] digits_before_dot = d[1] digits_after_dot = d[2] sci_data = d[3] base = if digits_after_dot [sign_char, digits_before_dot, '.', digits_after_dot].join('').to_f else [sign_char, digits_before_dot].join('').to_i(10) end factor = if sci_data sign_char = sci_data[0] exponent = sci_data[1] unsigned_factor = exponent.to_i(10) case sign_char when '+', '' 10**unsigned_factor when '-' - 10**unsigned_factor end else 1 end base * factor end # Object json_pair = seq( json_string, whitespace.ignore, char(':').ignore, whitespace.ignore, lazy { json_value }, ).compact json_pairs = intersperse( json_pair, seq( whitespace, char(','), whitespace, ).ignore, ).compact.map { |d| Hash[d] } json_object = seq( char('{').ignore, whitespace.ignore, json_pairs, whitespace.ignore, char('}').ignore, ).compact.first # Value json_value = alt( json_string, json_number, json_object, json_array, json_true, json_false, json_null, ) # All json_object end |