forked from zmoazeni/csscss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.rb
More file actions
93 lines (78 loc) · 2.37 KB
/
common.rb
File metadata and controls
93 lines (78 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module Csscss
module Parser
module Common
include Parslet
UNITS = %w(px em ex in cm mm pt pc)
rule(:space) { match['\s'].repeat(1) }
rule(:space?) { space.maybe }
rule(:number) { match["0-9"] }
rule(:numbers) { number.repeat(1) }
rule(:decimal) { numbers >> str(".").maybe >> numbers.maybe }
rule(:percent) { decimal >> stri("%") >> space? }
rule(:non_zero_length) { decimal >> stri_list(UNITS) >> space? }
rule(:zero_length) { match["0"] }
rule(:length) { zero_length | non_zero_length }
rule(:identifier) { match["a-zA-Z"].repeat(1) }
rule(:inherit) { stri("inherit") }
rule(:eof) { any.absent? }
rule(:nada) { any.repeat.as(:nada) }
rule(:http) {
(match['a-zA-Z0-9.:/\-'] | str('\(') | str('\)')).repeat >> space?
}
rule(:data) {
stri("data:") >> match['a-zA-Z0-9.:/+;,=\-'].repeat >> space?
}
rule(:url) {
stri("url") >> parens do
(any_quoted { http } >> space?) |
(any_quoted { data } >> space?) |
data | http
end
}
def stri(str)
key_chars = str.split(//)
key_chars.
collect! { |char|
if char.upcase == char.downcase
str(char)
else
match["#{char.upcase}#{char.downcase}"]
end
}.reduce(:>>)
end
def symbol(s, label = nil)
if label
stri(s).as(label) >> space?
else
stri(s) >> space?
end
end
def between(left, right)
raise "block not given" unless block_given?
symbol(left) >> yield >> symbol(right)
end
def parens(&block)
between("(", ")", &block)
end
def double_quoted(&block)
between('"', '"', &block)
end
def single_quoted(&block)
between("'", "'", &block)
end
def any_quoted(&block)
double_quoted(&block) | single_quoted(&block)
end
def stri_list(list)
list.map {|u| stri(u) }.reduce(:|)
end
def symbol_list(list)
list.map {|u| symbol(u) }.reduce(:|)
end
def try_parse(input)
parsed = (root | nada).parse(input)
parsed[:nada] ? false : parsed
end
end
end
end