Class: IDL::Scanner::In

Inherits:
Object
  • Object
show all
Defined in:
lib/ridl/scanner.rb

Overview

Position

Instance Method Summary collapse

Constructor Details

#initialize(src, name = '', line = 0, column = 1) ⇒ In

Returns a new instance of In.



47
48
49
50
51
52
53
# File 'lib/ridl/scanner.rb', line 47

def initialize(src, name = '', line = 0, column = 1)
  @src = src
  @fwd = src.getc     # look ahead character
  @bwd = nil          # look back character
  @pos = Position.new(name, line, column)
  @mark = nil
end

Instance Method Details

#closeObject



144
145
146
# File 'lib/ridl/scanner.rb', line 144

def close
  @src.close # close input source
end

#columnObject



59
60
61
# File 'lib/ridl/scanner.rb', line 59

def column
  @pos.column
end

#getcObject Also known as: skipc



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ridl/scanner.rb', line 73

def getc
  cur = @fwd
  @fwd = @src.getc unless @src.nil?
  @mark << cur unless @mark.nil?
  if [nil, "\n", "\r"].include? @bwd
    if @bwd == "\r" and cur == "\n"
    else
      @pos.line += 1
      @pos.column = 1
    end
  else
    @pos.column += 1
  end
  @bwd = cur
end

#getregionObject



138
139
140
141
142
# File 'lib/ridl/scanner.rb', line 138

def getregion
  ret = @mark
  @mark = nil
  ret
end

#getsObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/ridl/scanner.rb', line 89

def gets
  return nil if @fwd.nil?

  s = ''
  s << getc until [nil, "\n", "\r"].include? lookc
  s << getc while ["\n", "\r"].include? lookc

  @mark << s unless @mark.nil?
  s
end

#lookcObject



69
70
71
# File 'lib/ridl/scanner.rb', line 69

def lookc
  @fwd
end

#mark(*ini) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ridl/scanner.rb', line 123

def mark(*ini)
  @mark = ''
  ini.each { |i|
    case i
    when nil
    when String
      @mark << i.dup
    when Fixnum
      @mark << i
    when Array
      i.each { |j| @mark << j } # array of array is not incoming.
    end
  }
end

#positionObject



55
56
57
# File 'lib/ridl/scanner.rb', line 55

def position
  @pos
end

#skipuntil(*_chars, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/ridl/scanner.rb', line 112

def skipuntil(*_chars, &block)
  if block
    until (ch = lookc).nil?
      return ch if block.call(ch)

      skipc
    end
  end
  nil
end

#skipwhile(*_chars, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/ridl/scanner.rb', line 101

def skipwhile(*_chars, &block)
  if block
    until (ch = lookc).nil?
      return ch unless block.call(ch)

      skipc
    end
  end
  nil
end

#to_sObject

cursor set at last gotten character. ex: after initialization, position is (0,0).



65
66
67
# File 'lib/ridl/scanner.rb', line 65

def to_s
  @src.to_s
end