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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#[doc="Constructs a list of css 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,
Height, Width, StyleDeclaration, Selector};
import parser::css_lexer::{Token, StartDescription, EndDescription,
Descendant, Child, Sibling,
Comma, Element, Attr, Description,
Eof};
import comm::recv;
import option::{map, is_none};
import vec::push;
import parser::parser_util::{parse_display_type, parse_font_size, parse_size};
import util::color::parsing::parse_color;
import vec::push;
type TokenReader = {stream : pipes::Port<Token>, mut lookahead : Option<Token>};
trait TokenReaderMethods {
fn get() -> Token;
fn unget(-tok : Token);
}
impl TokenReader : TokenReaderMethods {
fn get() -> Token {
match copy self.lookahead {
Some(tok) => { self.lookahead = None; copy tok }
None => { self.stream.recv() }
}
}
fn unget(-tok : Token) {
assert is_none(self.lookahead);
self.lookahead = Some(tok);
}
}
trait ParserMethods {
fn parse_element() -> Option<~style::Selector>;
fn parse_selector() -> Option<~[~Selector]>;
fn parse_description() -> Option<~[StyleDeclaration]>;
fn parse_rule() -> Option<~style::Rule>;
}
impl TokenReader : ParserMethods {
fn parse_element() -> Option<~style::Selector> {
// Get the current element type
let elmt_name = match self.get() {
Element(tag) => { copy tag }
Eof => { return None; }
_ => { fail ~"Expected an element" }
};
let mut attr_list = ~[];
// Get the attributes associated with that element
loop {
let tok = self.get();
match tok {
Attr(attr) => { push(attr_list, copy attr); }
StartDescription | Descendant | Child | Sibling | Comma => {
self.unget(tok);
break;
}
Eof => { return None; }
Element(_) => fail ~"Unexpected second element without relation to first element",
EndDescription => fail ~"Unexpected '}'",
Description(_, _) => fail ~"Unexpected description"
}
}
return Some(~style::Element(elmt_name, attr_list));
}
fn parse_selector() -> Option<~[~Selector]> {
let mut sel_list = ~[];
// Collect all the selectors that this rule applies to
loop {
let mut cur_sel;
match self.parse_element() {
Some(elmt) => { cur_sel = copy elmt; }
None => { return None; } // we hit an eof in the middle of a rule
}
loop {
let tok = self.get();
let built_sel <- cur_sel;
match tok {
Descendant => {
match self.parse_element() {
Some(elmt) => {
let new_sel = copy elmt;
cur_sel <- ~style::Descendant(built_sel, new_sel)
}
None => { return None; }
}
}
Child => {
match self.parse_element() {
Some(elmt) => {
let new_sel = copy elmt;
cur_sel <- ~style::Child(built_sel, new_sel)
}
None => { return None; }
}
}
Sibling => {
match self.parse_element() {
Some(elmt) => {
let new_sel = copy elmt;
cur_sel <- ~style::Sibling(built_sel, new_sel)
}
None => { return None; }
}
}
StartDescription => {
push(sel_list, built_sel);
self.unget(StartDescription);
break;
}
Comma => {
push(sel_list, built_sel);
self.unget(Comma);
break;
}
Attr(_) | EndDescription | Element(_) | Description(_, _) => {
fail #fmt["Unexpected token %? in elements", tok];
}
Eof => { return None; }
}
}
// check if we should break out of the nesting loop as well
// TODO: fix this when rust gets labelled loops
let tok = self.get();
match tok {
StartDescription => { break; }
Comma => { }
_ => { self.unget(tok); }
}
}
return Some(sel_list);
}
fn parse_description() -> Option<~[StyleDeclaration]> {
let mut desc_list : ~[StyleDeclaration]= ~[];
// Get the description to be applied to the selector
loop {
let tok = self.get();
match tok {
EndDescription => { break; }
Description(prop, val) => {
let desc = match prop {
// TODO: have color parsing return an option instead of a real value
~"background-color" => parse_color(val).map(|res| BackgroundColor(res)),
~"color" => parse_color(val).map(|res| TextColor(res)),
~"display" => parse_display_type(val).map(|res| Display(res)),
~"font-size" => parse_font_size(val).map(|res| FontSize(res)),
~"height" => parse_size(val).map(|res| Height(res)),
~"width" => parse_size(val).map(|res| Width(res)),
_ => { #debug["Recieved unknown style property '%s'", val]; None }
};
desc.map(|res| push(desc_list, res));
}
Eof => { return None; }
StartDescription | Descendant | Child | Sibling | Comma | Element(_) | Attr(_) => {
fail #fmt["Unexpected token %? in description", tok];
}
}
}
return Some(desc_list);
}
fn parse_rule() -> Option<~style::Rule> {
// TODO: get rid of copies once match move works
let sel_list = match self.parse_selector() {
Some(list) => { copy list }
None => { return None; }
};
#debug("sel_list: %?", sel_list);
// Get the description to be applied to the selector
let desc_list = match self.parse_description() {
Some(list) => { copy list }
None => { return None; }
};
#debug("desc_list: %?", desc_list);
return Some(~(sel_list, desc_list));
}
}
fn build_stylesheet(+stream : pipes::Port<Token>) -> ~[~style::Rule] {
let mut rule_list = ~[];
let reader = {stream : stream, mut lookahead : None};
loop {
match reader.parse_rule() {
Some(rule) => { push(rule_list, copy rule); }
None => { break; }
}
}
return rule_list;
}
|