Class: Fdlint::Parser::CSS::CssParser

Inherits:
BaseParser show all
Includes:
ParserVisitable
Defined in:
lib/fdlint/parser/css/css_parser.rb

Constant Summary collapse

TERM =
%q([^;{}'"])
TERM2 =
%q([^;{}'",])
QUOT_EXPR =
"'[^']*'"
DQUOT_EXPR =
'"[^"]*"'
R_IDENT =
/-?[_a-z][_a-z0-9-]*/
R_ANY =
%r"((?:#{TERM})|(?:#{QUOT_EXPR})|(?:#{DQUOT_EXPR}))+"
R_SELECTOR =
%r"((?:#{TERM2})|(?:#{QUOT_EXPR})|(?:#{DQUOT_EXPR}))+"
R_PROPERTY =
/[*_+\\]?-?[_a-z\\][\\_a-z0-9-]*/

Constants included from Helper::Logger

Helper::Logger::LEVELS

Instance Attribute Summary collapse

Attributes inherited from BaseParser

#source

Instance Method Summary collapse

Methods included from ParserVisitable

#add_visitor, #add_visitors, included, #parse_no_throw, #results, #visitors, wrap

Methods inherited from BaseParser

#batch, #check, #eos?, #raw_scan, #reset, #rest_source, #scan, #scanned_source, #scanner_pos, #skip, #skip_empty, #to_s

Methods included from Helper::Logger

#log, #logger

Constructor Details

#initialize(css) ⇒ CssParser

Returns a new instance of CssParser.



22
23
24
25
# File 'lib/fdlint/parser/css/css_parser.rb', line 22

def initialize(css)
  super
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments.



20
21
22
# File 'lib/fdlint/parser/css/css_parser.rb', line 20

def comments
  @comments
end

Instance Method Details

#parse_commentObject



142
143
144
145
146
147
# File 'lib/fdlint/parser/css/css_parser.rb', line 142

def parse_comment
  debug { 'pare comment' }
  comment =  raw_scan /\/\*[^*]*\*+([^\/*][^*]*\*+)*\//
  debug { "  #{comment}" }
  comment
end

#parse_declarationObject



117
118
119
120
121
122
123
124
125
# File 'lib/fdlint/parser/css/css_parser.rb', line 117

def parse_declaration
  debug { '   parse declaration' }
  
  property = parse_property
  skip /:/
  value = parse_value
  
  Declaration.new(property, value)
end

#parse_declarationsObject



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fdlint/parser/css/css_parser.rb', line 104

def parse_declarations
  first = true
  batch(:parse_declaration) do
    if check /\}/
      false
    else 
      skip(first ? /[;\s]*/ : /[;\s]+/)
      first = false
      !check /\}/
    end
  end
end

#parse_directiveObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fdlint/parser/css/css_parser.rb', line 53

def parse_directive
  debug { 'parse directive' }

  skip /@/
  keyword = scan R_IDENT 
  skip_empty
  
  expr = check(/\{/) || check(/;/) ? nil : 
      scan(R_ANY)

  block = nil
  if check /\{/
    skip /\{/
    block = parse_stylesheet true
    skip /\}/
  end

  unless block
    skip /;/ 
  end

  debug { "  keyword: #{keyword} #{keyword.position}" }
  debug { "  expression: #{expr} #{expr.position}" } if expr
  debug { "  block:\n#{block}\n#{block.position}"  } if block
  Directive.new keyword, expr, block
end

#parse_propertyObject



127
128
129
130
131
132
# File 'lib/fdlint/parser/css/css_parser.rb', line 127

def parse_property
  debug { '    parse property' }
  property = scan R_PROPERTY 
  debug { "     #{property} #{property.position}" }
  property
end

#parse_rulesetObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/fdlint/parser/css/css_parser.rb', line 80

def parse_ruleset
  debug { 'parse ruleset' }

  selector = check(/\{/) ? nil : parse_selector
  skip /\{/
  declarations = do_parse_declarations
  skip /\}/
  
  RuleSet.new selector, declarations
end

#parse_selectorObject



91
92
93
94
95
# File 'lib/fdlint/parser/css/css_parser.rb', line 91

def parse_selector
  debug { ' parse selector' }
  simple_selectors = batch(:parse_simple_selector, /\{/, /,/)
  Selector.new simple_selectors
end

#parse_simple_selectorObject



97
98
99
100
101
102
# File 'lib/fdlint/parser/css/css_parser.rb', line 97

def parse_simple_selector
  selector = scan R_SELECTOR 
  debug { '  parse simple selector' }
  debug { "   #{selector} #{selector.position}" }
  selector
end

#parse_statementObject

ruleset or directive



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

def parse_statement
  debug { 'parse statement' }

  if check /@/
    parse_directive
  else
    parse_ruleset
  end
end

#parse_stylesheet(inner = false) ⇒ Object Also known as: parse



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fdlint/parser/css/css_parser.rb', line 27

def parse_stylesheet(inner = false)
  debug { 'parse stylesheet' }
  
  do_parse_comment

  stats = batch(:parse_statement) do 
    skip_empty
    !(inner ? check(/\}/) : eos?)
  end
  
  StyleSheet.new stats
end

#parse_valueObject



134
135
136
137
138
139
140
# File 'lib/fdlint/parser/css/css_parser.rb', line 134

def parse_value
  debug { '    parse value' }

  value = scan R_ANY 
  debug { "     #{value} #{value.position}" }
  value 
end