Class: SyntaxTree::JSON::Parser

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

Overview

This class is responsible for converting from a plain string input into an AST.

Defined Under Namespace

Classes: ParseError, Token

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Parser

Returns a new instance of Parser.



13
14
15
# File 'lib/syntax_tree/json/parser.rb', line 13

def initialize(source)
  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



11
12
13
# File 'lib/syntax_tree/json/parser.rb', line 11

def source
  @source
end

Instance Method Details

#parseObject



17
18
19
20
21
22
23
# File 'lib/syntax_tree/json/parser.rb', line 17

def parse
  if parse_item(make_tokens) in [value, []]
    AST::Root.new(value: value)
  else
    raise ParseError, "unexpected tokens after value"
  end
end