Class: Citrus::Input

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/citrus.rb

Overview

An Input is a scanner that is responsible for executing rules at different positions in the input string and persisting event streams.

Direct Known Subclasses

MemoizedInput

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Input



173
174
175
176
177
# File 'lib/citrus.rb', line 173

def initialize(source)
  super(source_text(source))
  @source = source
  @max_offset = 0
end

Instance Attribute Details

#max_offsetObject (readonly)

The maximum offset in the input that was successfully parsed.



180
181
182
# File 'lib/citrus.rb', line 180

def max_offset
  @max_offset
end

#sourceObject (readonly)

The initial source passed at construction. Typically a String or a Pathname.



184
185
186
# File 'lib/citrus.rb', line 184

def source
  @source
end

Instance Method Details

#exec(rule, events = []) ⇒ Object

Returns an array of events for the given rule at the current pointer position. Objects in this array may be one of three types: a Rule, Citrus::CLOSE, or a length (integer).



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/citrus.rb', line 246

def exec(rule, events=[])
  position = pos
  index = events.size

  if apply_rule(rule, position, events).size > index
    @max_offset = pos if pos > @max_offset
  else
    self.pos = position
  end

  events
end

#line(pos = pos()) ⇒ Object

Returns the text of the line that contains the character at the given pos. pos defaults to the current pointer position.



234
235
236
# File 'lib/citrus.rb', line 234

def line(pos=pos())
  lines[line_index(pos)]
end

#line_index(pos = pos()) ⇒ Object

Returns the 0-based number of the line that contains the character at the given pos. pos defaults to the current pointer position.



214
215
216
217
218
219
220
221
222
# File 'lib/citrus.rb', line 214

def line_index(pos=pos())
  p = n = 0
  string.each_line do |line|
    p += line.length
    return n if p >= pos
    n += 1
  end
  0
end

#line_number(pos = pos()) ⇒ Object Also known as: lineno

Returns the 1-based number of the line that contains the character at the given pos. pos defaults to the current pointer position.



226
227
228
# File 'lib/citrus.rb', line 226

def line_number(pos=pos())
  line_index(pos) + 1
end

#line_offset(pos = pos()) ⇒ Object

Returns the 0-based offset of the given pos in the input on the line on which it is found. pos defaults to the current pointer position.



202
203
204
205
206
207
208
209
210
# File 'lib/citrus.rb', line 202

def line_offset(pos=pos())
  p = 0
  string.each_line do |line|
    len = line.length
    return (pos - p) if p + len >= pos
    p += len
  end
  0
end

#linesObject

Returns an array containing the lines of text in the input.



192
193
194
195
196
197
198
# File 'lib/citrus.rb', line 192

def lines
  if string.respond_to?(:lines)
    string.lines.to_a
  else
    string.to_a
  end
end

#memoized?Boolean

Returns true when using memoization to cache match results.



239
240
241
# File 'lib/citrus.rb', line 239

def memoized?
  false
end

#resetObject

:nodoc:



186
187
188
189
# File 'lib/citrus.rb', line 186

def reset # :nodoc:
  @max_offset = 0
  super
end

#test(rule) ⇒ Object

Returns the length of a match for the given rule at the current pointer position, nil if none can be made.



261
262
263
264
265
266
# File 'lib/citrus.rb', line 261

def test(rule)
  position = pos
  events = apply_rule(rule, position, [])
  self.pos = position
  events[-1]
end