Class: PGN::Parser

Inherits:
Whittle::Parser
  • Object
show all
Defined in:
lib/pgn/parser.rb

Overview

Parser uses the whittle gem to parse pgn files based on their context free grammar.

Instance Method Summary collapse

Instance Method Details

#lex(input) {|({ name: :$end, line: line, value: nil, offset: offset })| ... } ⇒ Object

Yields:

  • (({ name: :$end, line: line, value: nil, offset: offset }))


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pgn/parser.rb', line 8

def lex(input)
  line   = 1
  offset = 0
  ending = input.length
  @@pgn ||= ''
  @@game_comment ||= nil
  until offset == ending
    next_token(input, offset, line).tap do |token|
      if !token.nil?
        token[:offset] = offset
        line, token[:line] = token[:line], line
        yield token unless token[:discarded]
        @@pgn += token[:value]
        offset += token[:value].length
      else
        raise Whittle::UnconsumedInputError,
              "Unmatched input #{input[offset..-1].inspect} on line #{line}"
        # offset += 1
      end
    end
  end

  yield ({ name: :$end, line: line, value: nil, offset: offset })
end