-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinlineparser.y
More file actions
160 lines (151 loc) · 3.93 KB
/
inlineparser.y
File metadata and controls
160 lines (151 loc) · 3.93 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
94
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class CSSPool::CSS::InlineParserBase
token CHARSET_SYM IMPORT_SYM STRING SEMI IDENT S COMMA LBRACE RBRACE STAR HASH
token LSQUARE RSQUARE EQUAL INCLUDES DASHMATCH RPAREN FUNCTION GREATER PLUS
token SLASH NUMBER MINUS LENGTH PERCENTAGE EMS EXS ANGLE TIME FREQ URI
token IMPORTANT_SYM MEDIA_SYM
rule
inlineruleset
: {}
| '' | S
| declarations
;
declarations
: declaration SEMI declarations
| declaration SEMI
| declaration
;
declaration
: property ':' expr prio
{ @handler.property val.first, val[2], val[3] }
| property ':' S expr prio
{ @handler.property val.first, val[3], val[4] }
| property S ':' expr prio
{ @handler.property val.first, val[3], val[4] }
| property S ':' S expr prio
{ @handler.property val.first, val[4], val[5] }
;
prio
: IMPORTANT_SYM { result = true }
| { result = false }
;
property
: IDENT { result = interpret_identifier val[0] }
| STAR IDENT { result = interpret_identifier val.join }
;
operator
: COMMA
| SLASH
| EQUAL
;
expr
: term operator expr {
result = [val.first, val.last].flatten
val.last.first.operator = val[1]
}
| term expr { result = val.flatten }
| term { result = val }
;
term
: ident
| numeric
| string
| uri
| hexcolor
| function
;
function
: function S { result = val.first }
| FUNCTION expr RPAREN {
name = interpret_identifier val.first.sub(/\($/, '')
if name == 'rgb'
result = Terms::Rgb.new(*val[1])
else
result = Terms::Function.new name, val[1]
end
}
;
hexcolor
: hexcolor S { result = val.first }
| HASH { result = Terms::Hash.new val.first.sub(/^#/, '') }
;
uri
: uri S { result = val.first }
| URI { result = Terms::URI.new interpret_uri val.first }
string
: string S { result = val.first }
| STRING { result = Terms::String.new interpret_string val.first }
;
numeric
: unary_operator numeric {
result = val[1]
val[1].unary_operator = val.first
}
| NUMBER {
result = Terms::Number.new numeric val.first
}
| PERCENTAGE {
result = Terms::Number.new numeric(val.first), nil, '%'
}
| LENGTH {
unit = val.first.gsub(/[\s\d.]/, '')
result = Terms::Number.new numeric(val.first), nil, unit
}
| EMS {
result = Terms::Number.new numeric(val.first), nil, 'em'
}
| EXS {
result = Terms::Number.new numeric(val.first), nil, 'ex'
}
| ANGLE {
unit = val.first.gsub(/[\s\d.]/, '')
result = Terms::Number.new numeric(val.first), nil, unit
}
| TIME {
unit = val.first.gsub(/[\s\d.]/, '')
result = Terms::Number.new numeric(val.first), nil, unit
}
| FREQ {
unit = val.first.gsub(/[\s\d.]/, '')
result = Terms::Number.new numeric(val.first), nil, unit
}
;
unary_operator
: MINUS { result = :minus }
| PLUS { result = :plus }
;
ident
: ident S { result = val.first }
| IDENT { result = Terms::Ident.new interpret_identifier val.first }
;
---- inner
def numeric thing
thing = thing.gsub(/[^\d.]/, '')
Integer(thing) rescue Float(thing)
end
def interpret_identifier s
interpret_escapes s
end
def interpret_uri s
interpret_escapes s.match(/^url\((.*)\)$/mu)[1].strip.match(/^(['"]?)((?:\\.|.)*)\1$/mu)[2]
end
def interpret_string s
interpret_escapes s.match(/^(['"])((?:\\.|.)*)\1$/mu)[2]
end
def interpret_escapes s
token_exp = /\\([0-9a-fA-F]{1,6}(?:\r\n|\s)?)|\\(.)|(.)/mu
characters = s.scan(token_exp).map do |u_escape, i_escape, ident|
if u_escape
code = u_escape.chomp.to_i 16
code = 0xFFFD if code > 0x10FFFF
[code].pack('U')
elsif i_escape
if i_escape == "\n"
''
else
i_escape
end
else
ident
end
end.join ''
end