Class: Fdlint::Parser::BaseParser

Inherits:
Object
  • Object
show all
Includes:
Helper::Logger
Defined in:
lib/fdlint/parser/base_parser.rb

Direct Known Subclasses

CSS::CssParser, HTML::HtmlParser, JS::JsParser

Constant Summary

Constants included from Helper::Logger

Helper::Logger::LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Logger

#log, #logger

Constructor Details

#initialize(text) ⇒ BaseParser

Returns a new instance of BaseParser.



16
17
18
19
20
21
22
# File 'lib/fdlint/parser/base_parser.rb', line 16

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

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



14
15
16
# File 'lib/fdlint/parser/base_parser.rb', line 14

def source
  @source
end

Instance Method Details

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



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fdlint/parser/base_parser.rb', line 67

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



43
44
45
46
47
48
49
50
51
52
# File 'lib/fdlint/parser/base_parser.rb', line 43

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)


88
89
90
# File 'lib/fdlint/parser/base_parser.rb', line 88

def eos?
  @scanner.eos?
end

#raw_scan(pattern, not_skip_empty = false) ⇒ Object



60
61
62
63
64
65
# File 'lib/fdlint/parser/base_parser.rb', line 60

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



84
85
86
# File 'lib/fdlint/parser/base_parser.rb', line 84

def reset
  @scanner.reset
end

#rest_sourceObject



97
98
99
# File 'lib/fdlint/parser/base_parser.rb', line 97

def rest_source
  @scanner.rest
end

#scan(pattern, not_skip_empty = false) ⇒ Object



54
55
56
57
58
# File 'lib/fdlint/parser/base_parser.rb', line 54

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

#scanned_sourceObject



92
93
94
95
# File 'lib/fdlint/parser/base_parser.rb', line 92

def scanned_source
  pos = @text_size - @scanner.rest.size
  source[0..(pos-1)]
end

#scanner_posObject



38
39
40
41
# File 'lib/fdlint/parser/base_parser.rb', line 38

def scanner_pos
  pos = @text_size - @scanner.rest.size
  @pos_info.locate pos    
end

#skip(pattern, not_skip_empty = false) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/fdlint/parser/base_parser.rb', line 28

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



24
25
26
# File 'lib/fdlint/parser/base_parser.rb', line 24

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

#to_sObject



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

def to_s
  self.class.to_s
end