aboutsummaryrefslogtreecommitdiffstats
path: root/src/servo/parser/css_builder.rs
blob: 2f551e5c6c5494264ad244fa271677516b923747 (plain) (blame)
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
#[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 : port<Token>, mut lookahead : option<Token>};

trait util_methods {
    fn get() -> Token;
    fn unget(-tok : Token);
}

impl util_methods of util_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);
    }
}

trait parser_methods {
    fn parse_element() -> option<~style::Selector>;
    fn parse_selector() -> option<~[~Selector]>;
    fn parse_description() -> option<~[StyleDeclaration]>;
    fn parse_rule() -> option<~style::Rule>;
}

impl parser_methods of parser_methods for TokenReader {
    fn parse_element() -> option<~style::Selector> {
        // Get the current element type
         let elmt_name = alt self.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 = self.get();
             alt tok {
               Attr(attr) { push(attr_list, copy attr); }
               StartDescription | Descendant | Child | Sibling | Comma {
                 self.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_selector() -> option<~[~Selector]> {
        let mut sel_list = ~[];

        // Collect all the selectors that this rule applies to
        loop {
            let mut cur_sel;

            alt self.parse_element() {
              some(elmt) { cur_sel = copy elmt; }
              none { ret none; } // we hit an eof in the middle of a rule
            }

            loop {
                let tok = self.get();
                let built_sel <- cur_sel;
                
                alt tok {
                  Descendant {
                    alt self.parse_element() {
                      some(elmt) { 
                        let new_sel = copy elmt;
                        cur_sel <- ~style::Descendant(built_sel, new_sel)
                      }
                      none { ret none; }
                    }
                  }
                  Child {
                    alt self.parse_element() {
                      some(elmt) { 
                        let new_sel = copy elmt;
                        cur_sel <- ~style::Child(built_sel, new_sel)
                      }
                      none { ret none; }
                    }
                  }
                  Sibling {
                    alt self.parse_element() {
                      some(elmt) { 
                        let new_sel = copy elmt;
                        cur_sel <- ~style::Sibling(built_sel, new_sel)
                      }
                      none { ret 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 { ret 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();
            alt tok {
              StartDescription { break; }
              Comma { }
              _ { self.unget(tok); }
            }
        }
        
        ret 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();
            alt tok {
              EndDescription { break; }
              Description(prop, val) {
                let desc = alt 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 { ret none; }
              StartDescription | Descendant | Child | Sibling | Comma | Element(_) | Attr(_)  {
                fail #fmt["Unexpected token %? in description", tok]; 
              }
            }
        }
        
        ret some(desc_list);
    }

    fn parse_rule() -> option<~style::Rule> {
        let sel_list = alt self.parse_selector() {
          some(list){ copy list }
          none { ret none; }
        };
        
        // Get the description to be applied to the selector
        let desc_list = alt self.parse_description() {
          some(list) { copy list }
          none { ret none; }
        };
        
        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 reader.parse_rule() {
          some(rule) { push(rule_list, copy rule); }
          none { break; }
        }
    }

    ret rule_list;
}