Class: XRay::BaseParser

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

Direct Known Subclasses

CSS::Parser, HTML::Parser, JS::Parser

Instance Method Summary collapse

Constructor Details

#initialize(text, log = nil) ⇒ BaseParser

Returns a new instance of BaseParser.



13
14
15
16
17
18
19
# File 'lib/base_parser.rb', line 13

def initialize(text, log = nil)
  @log = log
  text = filter_text(prepare_text(text))
  @pos_info = PositionInfo.new text
  @scanner = StringScanner.new text
  @text_size = text.size
end

Instance Method Details

#batch(name, stop = nil, skip_pattern = nil, not_skip_empty = false, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/base_parser.rb', line 59

def batch(name, stop = nil, skip_pattern = nil, not_skip_empty = false, &block)
  first = true
  result = []
  while !@scanner.eos? && 
      (stop ? batch_check(stop, skip_pattern, not_skip_empty, first) : 
          block ? block.call : true) && 
      item = send(name)
    result << item
    first = false
  end
  result
end

#check(pattern, not_skip_empty = false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/base_parser.rb', line 35

def check(pattern, not_skip_empty = false)
  skip_empty = !not_skip_empty
  if skip_empty && @scanner.check(/\s+/)
    last_pos = @scanner.pos
    @scanner.skip /\s+/
  end
  ret = @scanner.check pattern
  @scanner.pos = last_pos if last_pos
  ret
end

#eos?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/base_parser.rb', line 80

def eos?
  @scanner.eos?
end

#raw_scan(pattern, not_skip_empty = false) ⇒ Object



52
53
54
55
56
57
# File 'lib/base_parser.rb', line 52

def raw_scan(pattern, not_skip_empty = false)
  not_skip_empty || skip_empty
  pos = scanner_pos
  text = @scanner.scan pattern
  text ? Node.new(text, pos) : parse_error(pattern)
end

#resetObject



76
77
78
# File 'lib/base_parser.rb', line 76

def reset
  @scanner.reset
end

#scan(pattern, not_skip_empty = false) ⇒ Object



46
47
48
49
50
# File 'lib/base_parser.rb', line 46

def scan(pattern, not_skip_empty = false)
  node = raw_scan pattern, not_skip_empty
  after_scan pattern
  node
end

#skip(pattern, not_skip_empty = false) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/base_parser.rb', line 25

def skip(pattern, not_skip_empty = false)
  not_skip_empty || skip_empty
  pos = scanner_pos
  unless @scanner.skip pattern
    parse_error pattern
  end
  after_skip pattern
  pos
end

#skip_emptyObject



21
22
23
# File 'lib/base_parser.rb', line 21

def skip_empty
  @scanner.skip(/\s*/)
end

#to_sObject



72
73
74
# File 'lib/base_parser.rb', line 72

def to_s
  self.class.to_s
end