Class: HParser::Util::LineScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/hparser/util/line_scanner.rb

Overview

StringScanner like class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ LineScanner

Returns a new instance of LineScanner.



9
10
11
# File 'lib/hparser/util/line_scanner.rb', line 9

def initialize(lines)
  @lines = lines
end

Instance Attribute Details

#matchedObject (readonly)

Returns the value of attribute matched.



8
9
10
# File 'lib/hparser/util/line_scanner.rb', line 8

def matched
  @matched
end

#matched_patternObject (readonly)

Returns the value of attribute matched_pattern.



8
9
10
# File 'lib/hparser/util/line_scanner.rb', line 8

def matched_pattern
  @matched_pattern
end

Instance Method Details

#match?(exp) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hparser/util/line_scanner.rb', line 30

def match?(exp)
  if @lines == [] then
    false
  elsif exp.class == Regexp and (@matched_pattern = @lines[0].match(exp)) then
    true
  elsif @lines[0] == exp
    true
  else
    false
  end        
end

#scan(exp) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/hparser/util/line_scanner.rb', line 13

def scan(exp)
  @matched_pattern = nil
  if match?(exp) then
    @matched = @lines.shift
  else
    nil
  end
end

#skip(exp) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/hparser/util/line_scanner.rb', line 22

def skip(exp)
  if match?(exp) then
    @lines.shift
  else
    nil
  end
end