Module: BisonParser::Base

Included in:
BisonParser
Defined in:
lib/bison_parser/base.rb

Instance Method Summary collapse

Instance Method Details

#begin_tokenObject



54
55
56
57
# File 'lib/bison_parser/base.rb', line 54

def begin_token
  self.token_row = row
  self.token_col = col
end

#error(msg, row, col) ⇒ Object

Raises:



59
60
61
# File 'lib/bison_parser/base.rb', line 59

def error(msg, row, col)
  raise Error.new(msg, source, row, col)
end

#initialize(io) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/bison_parser/base.rb', line 9

def initialize(io)
  if String === io
    io = StringIO.new(io)
  end
  @source = io.respond_to?(:path) ? io.path : nil
  @io, @row, @col = io, 1, 0
end

#peakObject



50
51
52
# File 'lib/bison_parser/base.rb', line 50

def peak
  io.read(1).tap{ |c| io.ungetc(c) if c }
end

#readObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/bison_parser/base.rb', line 39

def read
  io.read(1).tap do |c|
    if c == "\n"
      self.row += 1
      self.col = 0
    elsif c
      self.col += 1
    end
  end
end

#read_integer(number = '') ⇒ Object



32
33
34
35
36
37
# File 'lib/bison_parser/base.rb', line 32

def read_integer(number='')
  while (c = self.peak) && ('0'..'9').include?(c)
    number << self.read
  end
  number
end

#read_over_whitespace(line_comment_prefix: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bison_parser/base.rb', line 17

def read_over_whitespace(line_comment_prefix: nil)
  while true
    while (c = self.read) && c =~ /\s/
    end

    if line_comment_prefix && c == line_comment_prefix
      while (char = self.read) && char != "\n"
      end
    else
      break
    end
  end
  c
end