Class: Crass::Parser
- Inherits:
-
Object
- Object
- Crass::Parser
- Defined in:
- lib/crass/parser.rb
Overview
Parses a CSS string or list of tokens.
Constant Summary collapse
- BLOCK_END_TOKENS =
{ :'{' => :'}', :'[' => :']', :'(' => :')' }
Instance Attribute Summary collapse
-
#tokens ⇒ Object
readonly
Array of tokens generated from this parser’s input.
Class Method Summary collapse
-
.parse_stylesheet(input, options = {}) ⇒ Object
Parses a CSS stylesheet and returns a parse tree.
-
.stringify(nodes, options = {}) ⇒ Object
Converts a node or array of nodes into a CSS string based on their original tokenized input.
Instance Method Summary collapse
-
#consume_at_rule(tokens = @tokens) ⇒ Object
Consumes an at-rule and returns it.
-
#consume_component_value(tokens = @tokens) ⇒ Object
Consumes a component value and returns it.
-
#consume_declaration(tokens = @tokens) ⇒ Object
Consumes a declaration and returns it, or ‘nil` on parse error.
-
#consume_declarations(tokens = @tokens) ⇒ Object
Consumes a list of declarations and returns them.
-
#consume_function(tokens = @tokens) ⇒ Object
Consumes a function and returns it.
-
#consume_qualified_rule(tokens = @tokens) ⇒ Object
Consumes a qualified rule and returns it, or ‘nil` if a parse error occurs.
-
#consume_rules(flags = {}) ⇒ Object
Consumes a list of rules and returns them.
-
#consume_simple_block(tokens = @tokens) ⇒ Object
Consumes and returns a simple block associated with the current input token.
-
#create_node(type, properties = {}) ⇒ Object
Creates and returns a new parse node with the given properties.
-
#initialize(input, options = {}) ⇒ Parser
constructor
Initializes a parser based on the given input, which may be a CSS string or an array of tokens.
-
#parse_selector(tokens) ⇒ Object
Parses the given tokens into a selector node and returns it.
-
#parse_style_rule(rule) ⇒ Object
Parses a style rule and returns the result.
-
#parse_value(nodes) ⇒ Object
Returns the unescaped value of a selector name or property declaration.
Constructor Details
#initialize(input, options = {}) ⇒ Parser
Initializes a parser based on the given input, which may be a CSS string or an array of tokens.
See Tokenizer#initialize for options.
84 85 86 87 88 89 90 |
# File 'lib/crass/parser.rb', line 84 def initialize(input, = {}) unless input.kind_of?(Enumerable) input = Tokenizer.tokenize(input, ) end @tokens = TokenScanner.new(input) end |
Instance Attribute Details
#tokens ⇒ Object (readonly)
Array of tokens generated from this parser’s input.
78 79 80 |
# File 'lib/crass/parser.rb', line 78 def tokens @tokens end |
Class Method Details
.parse_stylesheet(input, options = {}) ⇒ Object
Parses a CSS stylesheet and returns a parse tree.
See Tokenizer#initialize for options.
www.w3.org/TR/2013/WD-css-syntax-3-20130919/#parse-a-stylesheet
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/crass/parser.rb', line 24 def self.parse_stylesheet(input, = {}) parser = Parser.new(input, ) rules = parser.consume_rules(:top_level => true) rules.map do |rule| case rule[:node] # TODO: handle at-rules when :qualified_rule then parser.parse_style_rule(rule) else rule end end end |
.stringify(nodes, options = {}) ⇒ Object
Converts a node or array of nodes into a CSS string based on their original tokenized input.
Options:
* **:exclude_comments** - When `true`, comments will be excluded.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/crass/parser.rb', line 44 def self.stringify(nodes, = {}) nodes = [nodes] unless nodes.is_a?(Array) string = '' nodes.each do |node| case node[:node] when :comment string << node[:raw] unless [:exclude_comments] when :style_rule string << self.stringify(node[:selector][:tokens], ) string << "{" string << self.stringify(node[:children], ) string << "}" when :property string << [:indent] if [:indent] string << self.stringify(node[:tokens], ) else if node.key?(:raw) string << node[:raw] elsif node.key?(:tokens) string << self.stringify(node[:tokens], ) end end end string end |
Instance Method Details
#consume_at_rule(tokens = @tokens) ⇒ Object
Consumes an at-rule and returns it.
www.w3.org/TR/2013/WD-css-syntax-3-20130919/#consume-an-at-rule0
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/crass/parser.rb', line 95 def consume_at_rule(tokens = @tokens) rule = {:prelude => []} rule[:tokens] = tokens.collect do while token = tokens.consume case token[:node] when :comment then next when :semicolon, :eof then break when :'{' then rule[:block] = consume_simple_block(tokens) break # TODO: At this point, the spec says we should check for a "simple block # with an associated token of <<{-token>>", but isn't that exactly what # we just did above? And the tokenizer only ever produces standalone # <<{-token>>s, so how could the token stream ever contain one that's # already associated with a simple block? What am I missing? else tokens.reconsume rule[:prelude] << consume_component_value(tokens) end end end create_node(:at_rule, rule) end |
#consume_component_value(tokens = @tokens) ⇒ Object
Consumes a component value and returns it.
www.w3.org/TR/2013/WD-css-syntax-3-20130919/#consume-a-component-value0
127 128 129 130 131 132 133 134 135 |
# File 'lib/crass/parser.rb', line 127 def consume_component_value(tokens = @tokens) return nil unless token = tokens.consume case token[:node] when :'{', :'[', :'(' then consume_simple_block(tokens) when :function then consume_function(tokens) else token end end |
#consume_declaration(tokens = @tokens) ⇒ Object
Consumes a declaration and returns it, or ‘nil` on parse error.
www.w3.org/TR/2013/WD-css-syntax-3-20130919/#consume-a-declaration0
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/crass/parser.rb', line 140 def consume_declaration(tokens = @tokens) declaration = {} declaration[:tokens] = tokens.collect do declaration[:name] = tokens.consume[:value] value = [] token = tokens.consume token = tokens.consume while token[:node] == :whitespace return nil if token[:node] != :colon # TODO: parse error value << token while token = tokens.consume declaration[:value] = value maybe_important = value.reject {|v| v[:node] == :whitespace }[-2, 2] if maybe_important && maybe_important[0][:node] == :delim && maybe_important[0][:value] == '!' && maybe_important[1][:node] == :ident && maybe_important[1][:value].downcase == 'important' declaration[:important] = true end end create_node(:declaration, declaration) end |
#consume_declarations(tokens = @tokens) ⇒ Object
Consumes a list of declarations and returns them.
NOTE: The returned list may include ‘:comment`, `:semicolon`, and `:whitespace` nodes, which is non-standard.
www.w3.org/TR/2013/WD-css-syntax-3-20130919/#consume-a-list-of-declarations0
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/crass/parser.rb', line 176 def consume_declarations(tokens = @tokens) declarations = [] while token = tokens.consume case token[:node] when :comment, :semicolon, :whitespace declarations << token when :at_keyword # TODO: this is technically a parse error when parsing a style rule, # but not necessarily at other times. declarations << consume_at_rule(tokens) when :ident decl_tokens = [token] tokens.consume while tokens.current decl_tokens << tokens.current break if tokens.current[:node] == :semicolon tokens.consume end if decl = consume_declaration(TokenScanner.new(decl_tokens)) declarations << decl end else # TODO: parse error (invalid property name, etc.) while token && token[:node] != :semicolon token = consume_component_value(tokens) end end end declarations end |
#consume_function(tokens = @tokens) ⇒ Object
Consumes a function and returns it.
www.w3.org/TR/2013/WD-css-syntax-3-20130919/#consume-a-function
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/crass/parser.rb', line 217 def consume_function(tokens = @tokens) function = { :name => tokens.current[:value], :value => [], :tokens => [tokens.current] } function[:tokens].concat(tokens.collect do while token = tokens.consume case token[:node] when :')', :eof then break when :comment then next else tokens.reconsume function[:value] << consume_component_value(tokens) end end end) create_node(:function, function) end |
#consume_qualified_rule(tokens = @tokens) ⇒ Object
Consumes a qualified rule and returns it, or ‘nil` if a parse error occurs.
www.w3.org/TR/2013/WD-css-syntax-3-20130919/#consume-a-qualified-rule0
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/crass/parser.rb', line 244 def consume_qualified_rule(tokens = @tokens) rule = {:prelude => []} rule[:tokens] = tokens.collect do while true return nil unless token = tokens.consume if token[:node] == :'{' rule[:block] = consume_simple_block(tokens) break # elsif [simple block with an associated <<{-token>>??] # TODO: At this point, the spec says we should check for a "simple block # with an associated token of <<{-token>>", but isn't that exactly what # we just did above? And the tokenizer only ever produces standalone # <<{-token>>s, so how could the token stream ever contain one that's # already associated with a simple block? What am I missing? else tokens.reconsume rule[:prelude] << consume_component_value(tokens) end end end create_node(:qualified_rule, rule) end |
#consume_rules(flags = {}) ⇒ Object
Consumes a list of rules and returns them.
www.w3.org/TR/2013/WD-css-syntax-3-20130919/#consume-a-list-of-rules0
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/crass/parser.rb', line 276 def consume_rules(flags = {}) rules = [] while true return rules unless token = @tokens.consume case token[:node] when :comment, :whitespace then rules << token when :eof then return rules when :cdc, :cdo unless flags[:top_level] @tokens.reconsume rule = consume_qualified_rule rules << rule if rule end when :at_keyword @tokens.reconsume rule = consume_at_rule rules << rule if rule else @tokens.reconsume rule = consume_qualified_rule rules << rule if rule end end end |
#consume_simple_block(tokens = @tokens) ⇒ Object
Consumes and returns a simple block associated with the current input token.
www.w3.org/TR/2013/WD-css-syntax-3-20130919/#consume-a-simple-block0
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/crass/parser.rb', line 310 def consume_simple_block(tokens = @tokens) start_token = tokens.current[:node] end_token = BLOCK_END_TOKENS[start_token] block = { :start => start_token.to_s, :end => end_token.to_s, :value => [], :tokens => [tokens.current] } block[:tokens].concat(tokens.collect do while token = tokens.consume break if token[:node] == end_token || token[:node] == :eof tokens.reconsume block[:value] << consume_component_value(tokens) end end) create_node(:simple_block, block) end |
#create_node(type, properties = {}) ⇒ Object
Creates and returns a new parse node with the given properties.
334 335 336 |
# File 'lib/crass/parser.rb', line 334 def create_node(type, properties = {}) {:node => type}.merge!(properties) end |
#parse_selector(tokens) ⇒ Object
Parses the given tokens into a selector node and returns it.
Doesn’t bother splitting the selector list into individual selectors or validating them. Feel free to do that yourself! It’ll be fun!
342 343 344 345 346 |
# File 'lib/crass/parser.rb', line 342 def parse_selector(tokens) create_node(:selector, :value => parse_value(tokens), :tokens => tokens) end |
#parse_style_rule(rule) ⇒ Object
Parses a style rule and returns the result.
www.w3.org/TR/2013/WD-css-syntax-3-20130919/#style-rules www.w3.org/TR/2013/WD-css-syntax-3-20130919/#consume-a-list-of-declarations0
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/crass/parser.rb', line 352 def parse_style_rule(rule) children = [] tokens = TokenScanner.new(rule[:block][:value]) consume_declarations(tokens).each do |decl| unless decl[:node] == :declaration children << decl next end children << create_node(:property, :name => decl[:name], :value => parse_value(decl[:value]), :tokens => decl[:tokens]) end create_node(:style_rule, :selector => parse_selector(rule[:prelude]), :children => children ) end |
#parse_value(nodes) ⇒ Object
Returns the unescaped value of a selector name or property declaration.
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/crass/parser.rb', line 375 def parse_value(nodes) string = '' nodes.each do |node| case node[:node] when :comment, :semicolon then next when :ident then string << node[:value] when :function if node[:value].is_a?(String) string << node[:value] else string << parse_value(node[:value]) end else if node.key?(:raw) string << node[:raw] elsif node.key?(:tokens) string << parse_value(node[:tokens]) end end end string.strip end |