Class: SGF::Parser

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

Overview

The parser returns a SGF::Collection representation of the SGF file parser = SGF::Parser.new collection = parser.parse sgf_in_string_form

Constant Summary collapse

NEW_NODE =
";"
BRANCHING =
%w{( )}
END_OF_FILE =
false
NODE_DELIMITERS =
[NEW_NODE].concat(BRANCHING).concat([END_OF_FILE])
PROPERTY =
%w([ ])
LIST_IDENTITIES =
%w(AW AB AE AR CR DD LB LN MA SL SQ TR VW TB TW)

Instance Method Summary collapse

Instance Method Details

#parse(sgf, strict_parsing = true) ⇒ Object

This takes as argument an SGF and returns an SGF::Collection object It accepts a local path (String), a stringified SGF (String), or a file handler (File). The second argument is optional, in case you don’t want this to raise errors. You probably shouldn’t use it, but who’s gonna stop you?



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sgf/parser.rb', line 22

def parse sgf, strict_parsing = true
  error_checker = strict_parsing ? SGF::StrictErrorChecker.new : SGF::LaxErrorChecker.new
  @sgf_stream = SGF::Stream.new(sgf, error_checker)
  @assembler = SGF::CollectionAssembler.new
  until @sgf_stream.eof?
    case @sgf_stream.next_character
    when "(" then @assembler.open_branch
    when ";" then
      parse_node_data
      @assembler.create_node_with_properties @node_properties
    when ")" then @assembler.close_branch
    else next
    end
  end
  @assembler.collection
end