An Input is a scanner that is responsible for executing rules at different positions in the input string and persisting event streams.
Methods
public class
public instance
External Aliases
string | -> | to_str |
Returns the scanned string. |
Attributes
max_offset | [R] | The maximum offset in the input that was successfully parsed. |
source | [R] | The initial source passed at construction. Typically a String or a Pathname. |
Public class methods
# File lib/citrus.rb, line 173 173: def initialize(source) 174: super(source_text(source)) 175: @source = source 176: @max_offset = 0 177: end
Public instance methods
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).
# File lib/citrus.rb, line 246 246: def exec(rule, events=[]) 247: position = pos 248: index = events.size 249: 250: if apply_rule(rule, position, events).size > index 251: @max_offset = pos if pos > @max_offset 252: else 253: self.pos = position 254: end 255: 256: events 257: end
Returns the text of the line that contains the character at the given pos. pos defaults to the current pointer position.
# File lib/citrus.rb, line 234 234: def line(pos=pos) 235: lines[line_index(pos)] 236: end
Returns the 0-based number of the line that contains the character at the given pos. pos defaults to the current pointer position.
# File lib/citrus.rb, line 214 214: def line_index(pos=pos) 215: p = n = 0 216: string.each_line do |line| 217: p += line.length 218: return n if p >= pos 219: n += 1 220: end 221: 0 222: end
Returns the 1-based number of the line that contains the character at the given pos. pos defaults to the current pointer position.
# File lib/citrus.rb, line 226 226: def line_number(pos=pos) 227: line_index(pos) + 1 228: end
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.
# File lib/citrus.rb, line 202 202: def line_offset(pos=pos) 203: p = 0 204: string.each_line do |line| 205: len = line.length 206: return (pos - p) if p + len >= pos 207: p += len 208: end 209: 0 210: end
Returns an array containing the lines of text in the input.
# File lib/citrus.rb, line 192 192: def lines 193: if string.respond_to?(:lines) 194: string.lines.to_a 195: else 196: string.to_a 197: end 198: end
Returns true when using memoization to cache match results.
# File lib/citrus.rb, line 239 239: def memoized? 240: false 241: end
Returns the length of a match for the given rule at the current pointer position, nil if none can be made.
# File lib/citrus.rb, line 261 261: def test(rule) 262: position = pos 263: events = apply_rule(rule, position, []) 264: self.pos = position 265: events[-1] 266: end