Class: SGF::Parser
- Inherits:
-
Object
- Object
- SGF::Parser
- Defined in:
- lib/sgf/parser.rb
Instance Attribute Summary collapse
-
#event_listener ⇒ Object
readonly
Returns the value of attribute event_listener.
Class Method Summary collapse
- .is_binary?(file) ⇒ Boolean
- .parse(input, debug = false) ⇒ Object
- .parse_file(filename, debug = false) ⇒ Object
Instance Method Summary collapse
-
#initialize(event_listener) ⇒ Parser
constructor
A new instance of Parser.
- #parse(input) ⇒ Object
- #parse_file(filename) ⇒ Object
Constructor Details
#initialize(event_listener) ⇒ Parser
Returns a new instance of Parser.
5 6 7 8 9 |
# File 'lib/sgf/parser.rb', line 5 def initialize event_listener @event_listener = event_listener @stm = SGFStateMachine.new @stm.context = event_listener end |
Instance Attribute Details
#event_listener ⇒ Object (readonly)
Returns the value of attribute event_listener.
3 4 5 |
# File 'lib/sgf/parser.rb', line 3 def event_listener @event_listener end |
Class Method Details
.is_binary?(file) ⇒ Boolean
48 49 50 51 52 |
# File 'lib/sgf/parser.rb', line 48 def is_binary? file # This does not work?! # parts = %x(/usr/bin/file -i #{file}).split(':', 2) # not parts[1].include?('text') end |
Instance Method Details
#parse(input) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/sgf/parser.rb', line 11 def parse input @stm.reset raise ArgumentError.new if input.nil? or input.strip.empty? input.strip! 0.upto(input.size - 1) do |i| @position = i @stm.event(input[i,1]) end @stm.end rescue StateMachineError => e raise ParseError.new(input, @position, e.) end |
#parse_file(filename) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/sgf/parser.rb', line 28 def parse_file filename raise BinaryFileError if Parser.is_binary?(filename) File.open(filename) do |f| parse f.readlines.join end end |