Class: Pray::Literal::Parser
- Inherits:
-
Object
- Object
- Pray::Literal::Parser
- Defined in:
- lib/pray/literal.rb
Instance Method Summary collapse
- #finished? ⇒ Boolean
- #identifier_continue?(character) ⇒ Boolean
- #identifier_start?(character) ⇒ Boolean
-
#initialize(input) ⇒ Parser
constructor
A new instance of Parser.
- #next_character ⇒ Object
- #parse_array ⇒ Object
- #parse_identifier ⇒ Object
- #parse_identifier_from(start) ⇒ Object
- #parse_identifier_name ⇒ Object
- #parse_integer_or_identifier ⇒ Object
- #parse_map ⇒ Object
- #parse_map_key ⇒ Object
- #parse_string ⇒ Object
- #parse_symbol ⇒ Object
- #parse_value ⇒ Object
- #peek ⇒ Object
- #remaining ⇒ Object
- #skip_whitespace ⇒ Object
Constructor Details
#initialize(input) ⇒ Parser
Returns a new instance of Parser.
179 180 181 182 |
# File 'lib/pray/literal.rb', line 179 def initialize(input) @input = input @cursor = 0 end |
Instance Method Details
#finished? ⇒ Boolean
184 |
# File 'lib/pray/literal.rb', line 184 def finished? = @cursor >= @input.length |
#identifier_continue?(character) ⇒ Boolean
377 378 379 |
# File 'lib/pray/literal.rb', line 377 def identifier_continue?(character) character.match?(%r{[[:alnum:]_\-.\\/]}) end |
#identifier_start?(character) ⇒ Boolean
373 374 375 |
# File 'lib/pray/literal.rb', line 373 def identifier_start?(character) character && (character.match?(/[[:alpha:]]/) || character == "_") end |
#next_character ⇒ Object
195 196 197 198 199 |
# File 'lib/pray/literal.rb', line 195 def next_character character = peek @cursor += 1 if character character end |
#parse_array ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/pray/literal.rb', line 262 def parse_array next_character values = [] loop do skip_whitespace break if peek == "]" && next_character values << parse_value skip_whitespace case peek when "," next_character when "]" next_character break else raise Error.parse("literal", "expected ',' or ']'") end end LiteralValue.new(kind: :array, value: values) end |
#parse_identifier ⇒ Object
345 346 347 348 349 350 351 352 353 |
# File 'lib/pray/literal.rb', line 345 def parse_identifier identifier = parse_identifier_name case identifier when "true" then LiteralValue.new(kind: :bool, value: true) when "false" then LiteralValue.new(kind: :bool, value: false) when "nil" then LiteralValue.new(kind: :null, value: nil) else LiteralValue.new(kind: :string, value: identifier) end end |
#parse_identifier_from(start) ⇒ Object
366 367 368 369 370 371 |
# File 'lib/pray/literal.rb', line 366 def parse_identifier_from(start) while peek && (identifier_continue?(peek) || peek == ".") next_character end LiteralValue.new(kind: :string, value: @input[start...@cursor]) end |
#parse_identifier_name ⇒ Object
355 356 357 358 359 360 361 362 363 364 |
# File 'lib/pray/literal.rb', line 355 def parse_identifier_name raise Error.parse("literal", "expected identifier") unless identifier_start?(peek) start = @cursor next_character while peek && identifier_continue?(peek) next_character end @input[start...@cursor] end |
#parse_integer_or_identifier ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/pray/literal.rb', line 330 def parse_integer_or_identifier start = @cursor next_character if peek == "-" while peek&.match?(/[0-9_]/) next_character end if peek == "." return parse_identifier_from(start) end text = @input[start...@cursor].delete("_") LiteralValue.new(kind: :integer, value: Integer(text)) rescue ArgumentError => error raise Error.parse("literal", error.) end |
#parse_map ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/pray/literal.rb', line 284 def parse_map next_character entries = {} loop do skip_whitespace break if peek == "}" && next_character key = parse_map_key skip_whitespace if remaining.start_with?("=>") @cursor += 2 elsif peek == ":" next_character else raise Error.parse("literal", "expected ':' or '=>' after map key") end entries[key] = parse_value skip_whitespace case peek when "," next_character when "}" next_character break else raise Error.parse("literal", "expected ',' or '}'") end end LiteralValue.new(kind: :map, value: entries) end |
#parse_map_key ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/pray/literal.rb', line 315 def parse_map_key skip_whitespace case peek when '"', "'" value = parse_string value.value when ":" parse_symbol.value when "a".."z", "A".."Z", "_" parse_identifier_name else raise Error.parse("literal", "invalid map key") end end |
#parse_string ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/pray/literal.rb', line 221 def parse_string quote = next_character output = +"" escaped = false while (character = next_character) if escaped output << case character when "n" then "\n" when "r" then "\r" when "t" then "\t" when "\\" then "\\" when "\"", "'" then character else character end escaped = false next end if character == "\\" escaped = true next end if character == quote return LiteralValue.new(kind: :string, value: output) end output << character end raise Error.parse("literal", "unterminated string literal") end |
#parse_symbol ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/pray/literal.rb', line 250 def parse_symbol next_character output = +"" while (character = peek) && character.match?(%r{[[:alnum:]_\-.\\/]}) output << character next_character end raise Error.parse("literal", "empty symbol") if output.empty? LiteralValue.new(kind: :symbol, value: output) end |
#parse_value ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/pray/literal.rb', line 201 def parse_value skip_whitespace case peek when '"', "'" parse_string when ":" parse_symbol when "[" parse_array when "{" parse_map when "-", "0".."9" parse_integer_or_identifier when nil raise Error.parse("literal", "unexpected end of input") else parse_identifier end end |
#peek ⇒ Object
193 |
# File 'lib/pray/literal.rb', line 193 def peek = remaining[0] |
#remaining ⇒ Object
185 |
# File 'lib/pray/literal.rb', line 185 def remaining = @input[@cursor..] |
#skip_whitespace ⇒ Object
187 188 189 190 191 |
# File 'lib/pray/literal.rb', line 187 def skip_whitespace while (character = peek) && character.match?(/\s/) @cursor += 1 end end |