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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#[doc="Constructs a list of style rules from a token stream"]
// TODO: fail according to the css spec instead of failing when things
// are not as expected
import dom::style;
import style::{DisInline, DisBlock, DisNone, Display, TextColor, BackgroundColor, FontSize};
import parser::css_lexer::{Token, StartDescription, EndDescription,
Descendant, Child, Sibling,
Comma, Element, Attr, Description,
Eof};
import comm::recv;
import option::is_none;
import util::color::parsing::parse_color;
import vec::push;
type TokenReader = {stream : port<Token>, mut lookahead : option<Token>};
impl methods for TokenReader {
fn get() -> Token {
alt copy self.lookahead {
some(tok) { self.lookahead = none; copy tok }
none { recv(self.stream) }
}
}
fn unget(-tok : Token) {
assert is_none(self.lookahead);
self.lookahead = some(tok);
}
}
fn parse_element(reader : TokenReader) -> option<~style::Selector> {
// Get the current element type
let elmt_name = alt reader.get() {
Element(tag) { copy tag }
Eof { ret none; }
_ { fail "Expected an element" }
};
let mut attr_list = ~[];
// Get the attributes associated with that element
loop {
let tok = reader.get();
alt tok {
Attr(attr) { push(attr_list, copy attr); }
StartDescription | Descendant | Child | Sibling | Comma {
reader.unget(tok);
break;
}
Eof { ret none; }
Element(_) { fail "Unexpected second element without "
+ "relation to first element"; }
EndDescription { fail "Unexpected '}'"; }
Description(_, _) { fail "Unexpected description"; }
}
}
ret some(~style::Element(elmt_name, attr_list));
}
fn parse_rule(reader : TokenReader) -> option<~style::Rule> {
let mut sel_list = ~[];
let mut desc_list = ~[];
// Collect all the selectors that this rule applies to
loop {
let mut cur_sel;
alt parse_element(reader) {
some(elmt) { cur_sel = copy elmt; }
none { ret none; } // we hit an eof in the middle of a rule
}
loop {
let tok = reader.get();
alt tok {
Descendant {
alt parse_element(reader) {
some(elmt) {
let built_sel <- cur_sel;
let new_sel = copy elmt;
cur_sel <- ~style::Descendant(built_sel, new_sel)
}
none { ret none; }
}
}
Child {
alt parse_element(reader) {
some(elmt) {
let built_sel <- cur_sel;
let new_sel = copy elmt;
cur_sel <- ~style::Child(built_sel, new_sel)
}
none { ret none; }
}
}
Sibling {
alt parse_element(reader) {
some(elmt) {
let built_sel <- cur_sel;
let new_sel = copy elmt;
cur_sel <- ~style::Sibling(built_sel, new_sel)
}
none { ret none; }
}
}
StartDescription {
let built_sel <- cur_sel;
push(sel_list, built_sel);
reader.unget(StartDescription);
break;
}
Comma {
let built_sel <- cur_sel;
push(sel_list, built_sel);
reader.unget(Comma);
break;
}
Attr(_) | EndDescription | Element(_) | Description(_, _) {
fail #fmt["Unexpected token %? in elements", tok];
}
Eof { ret none; }
}
}
// check if we should break out of the nesting loop as well
let tok = reader.get();
alt tok {
StartDescription { break; }
Comma { }
_ { reader.unget(tok); }
}
}
// Get the description to be applied to the selector
loop {
let tok = reader.get();
alt tok {
EndDescription { break; }
Description(prop, val) {
alt prop {
"font-size" {
// TODO, support more ways to declare a font size than # pt
assert val.ends_with("pt");
let num = val.substr(0u, val.len() - 2u);
alt uint::from_str(num) {
some(n) { push(desc_list, FontSize(n)); }
none { fail "Nonnumber provided as font size"; }
}
}
"display" {
alt val {
"inline" { push(desc_list, Display(DisInline)); }
"block" { push(desc_list, Display(DisBlock)); }
"none" { push(desc_list, Display(DisNone)); }
_ { #debug["Recieved unknown display value '%s'", val]; }
}
}
"color" {
push(desc_list, TextColor(parse_color(val)));
}
"background-color" {
push(desc_list, BackgroundColor(parse_color(val)));
}
_ { #debug["Recieved unknown style property '%s'", val]; }
}
}
Eof { ret none; }
StartDescription | Descendant | Child | Sibling
| Comma | Element(_) | Attr(_) {
fail #fmt["Unexpected token %? in description", tok];
}
}
}
ret some(~(sel_list, desc_list));
}
fn build_stylesheet(stream : port<Token>) -> ~[~style::Rule] {
let mut rule_list = ~[];
let reader = {stream : stream, mut lookahead : none};
loop {
alt parse_rule(reader) {
some(rule) { push(rule_list, copy rule); }
none { break; }
}
}
ret rule_list;
}
|