Class: Fiveruns::JSON::Pure::Parser

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/fiveruns/json/parser.rb

Overview

This class implements the FiverunsJSON parser that is used to parse a FiverunsJSON string into a Ruby data structure.

Constant Summary collapse

STRING =
/" ((?:[^\x0-\x1f"\\] |
    \\["\\\/bfnrt] |
    \\u[0-9a-fA-F]{4} |
    \\[\x20-\xff])*)
"/nx
INTEGER =
/(-?0|-?[1-9]\d*)/
FLOAT =
/(-?
(?:0|[1-9]\d*)
(?:
  \.\d+(?i:e[+-]?\d+) |
  \.\d+ |
  (?i:e[+-]?\d+)
)
)/x
NAN =
/NaN/
INFINITY =
/Infinity/
MINUS_INFINITY =
/-Infinity/
OBJECT_OPEN =
/\{/
OBJECT_CLOSE =
/\}/
ARRAY_OPEN =
/\[/
ARRAY_CLOSE =
/\]/
PAIR_DELIMITER =
/:/
COLLECTION_DELIMITER =
/,/
TRUE =
/true/
FALSE =
/false/
NULL =
/null/
IGNORE =
%r(
  (?:
   //[^\n\r]*[\n\r]| # line comments
   /\*               # c-style comments
   (?:
    [^*/]|        # normal chars
    /[^*]|        # slashes that do not start a nested comment
    \*[^/]|       # asterisks that do not end this comment
    /(?=\*/)      # single slash before this comment's end 
   )*
     \*/               # the End of this comment
     |[ \t\r\n]+       # whitespaces: space, horicontal tab, lf, cr
  )+
)mx
UNPARSED =
Object.new

Instance Method Summary collapse

Constructor Details

#initialize(source, opts = {}) ⇒ Parser

Creates a new FiverunsJSON::Pure::Parser instance for the string source.

It will be configured by the opts hash. opts can have the following keys:

  • max_nesting: The maximum depth of nesting allowed in the parsed data structures. Disable depth checking with :max_nesting => false|nil|0, it defaults to 19.

  • allow_nan: If set to true, allow NaN, Infinity and -Infinity in defiance of RFC 4627 to be parsed by the Parser. This option defaults to false.

  • create_additions: If set to false, the Parser doesn’t create additions even if a matchin class and create_id was found. This option defaults to true.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fiveruns/json/parser.rb', line 64

def initialize(source, opts = {})
  super
  if !opts.key?(:max_nesting) # defaults to 19
    @max_nesting = 19
  elsif opts[:max_nesting]
    @max_nesting = opts[:max_nesting]
  else
    @max_nesting = 0
  end
  @allow_nan = !!opts[:allow_nan]
  ca = true
  ca = opts[:create_additions] if opts.key?(:create_additions)
  @create_id = ca ? ::Fiveruns::JSON.create_id : nil
end

Instance Method Details

#parseObject

Parses the current FiverunsJSON string source and returns the complete data structure as a result.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fiveruns/json/parser.rb', line 83

def parse
  reset
  obj = nil
  until eos?
    case
    when scan(OBJECT_OPEN)
      obj and raise ParserError, "source '#{peek(20)}' not in FiverunsJSON!"
      @current_nesting = 1
      obj = parse_object
    when scan(ARRAY_OPEN)
      obj and raise ParserError, "source '#{peek(20)}' not in FiverunsJSON!"
      @current_nesting = 1
      obj = parse_array
    when skip(IGNORE)
      ;
    else
      raise ParserError, "source '#{peek(20)}' not in FiverunsJSON!"
    end
  end
  obj or raise ParserError, "source did not contain any FiverunsJSON!"
  obj
end