/[james]/factorcss/Parser.y
ViewVC logotype

Annotation of /factorcss/Parser.y

Parent Directory Parent Directory | Revision Log Revision Log


Revision 69 - (hide annotations) (download)
Fri Jan 7 21:54:20 2005 UTC (19 years, 9 months ago) by james
File size: 5661 byte(s)
Modify tokeniser and parser to parse ancestor selectors correctly.

1 james 63 --
2     -- This file is part of FactorCSS
3     -- Licensed under the MIT License,
4     -- http://www.opensource.org/licenses/mit-license
5     -- Copyright 2004 James Bursa <james@semichrome.net>
6     --
7    
8 james 61 -- Parser for CSS 2.1
9     -- See CSS 2.1 G.2
10    
11     {
12     module Parser where
13     import Tokeniser
14     import CSS
15     }
16    
17     %name parser
18 james 65 %tokentype { TokenPosn }
19 james 61 %token
20 james 69 S { (S , _) }
21 james 65 INCLUDES { (INCLUDES , _) }
22     DASHMATCH { (DASHMATCH , _) }
23     LBRACE { (LBRACE , _) }
24     PLUS { (PLUS , _) }
25     GREATER { (GREATER , _) }
26     COMMA { (COMMA , _) }
27     STRING { (STRING $$ , _) }
28     IDENT { (IDENT $$ , _) }
29     HASH { (HASH $$ , _) }
30     IMPORT_SYM { (IMPORT_SYM , _) }
31     PAGE_SYM { (PAGE_SYM , _) }
32     MEDIA_SYM { (MEDIA_SYM , _) }
33     CHARSET_SYM { (CHARSET_SYM , _) }
34     IMPORTANT_SYM { (IMPORTANT_SYM, _) }
35     EMS { (EMS $$ , _) }
36     EXS { (EXS $$ , _) }
37     LENGTH { (LENGTH $$ , _) }
38     ANGLE { (ANGLE $$ , _) }
39     TIME { (TIME $$ , _) }
40     FREQ { (FREQ $$ , _) }
41     DIMEN { (DIMEN $$ , _) }
42     PERCENTAGE { (PERCENTAGE $$, _) }
43     NUMBER { (NUMBER $$ , _) }
44     URI { (URI $$ , _) }
45     FUNCTION { (FUNCTION $$ , _) }
46     DELIM { (DELIM $$ , _) }
47     SEMI { (SEMI , _) }
48     RBRACE { (RBRACE , _) }
49     COLON { (COLON , _) }
50     SLASH { (SLASH , _) }
51     MINUS { (MINUS , _) }
52     DOT { (DOT , _) }
53     LBRAC { (LBRAC , _) }
54     RBRAC { (RBRAC , _) }
55     ASTERISK { (ASTERISK , _) }
56     EQUALS { (EQUALS , _) }
57     RPAREN { (RPAREN , _) }
58 james 61
59     %%
60    
61     stylesheet :: { Stylesheet }
62 james 69 : charset s import_list s statement_list s
63     { Stylesheet $1 $3 $5 }
64 james 61
65     charset :: { Maybe String }
66 james 65 : { Nothing }
67 james 69 | CHARSET_SYM s STRING s SEMI { Just $3 }
68 james 61
69     import_list :: { [(String, [String])] }
70     : { [] }
71 james 69 | import_list s import { $3 : $1 }
72 james 61
73     import :: { (String, [String]) }
74 james 69 : IMPORT_SYM s STRING s medium_list SEMI s
75     { ($3, $5) }
76     | IMPORT_SYM s URI s medium_list SEMI s { ($3, $5) }
77 james 61
78     statement_list :: { [Statement] }
79     : { [] }
80 james 69 | statement_list s statement { $3 : $1 }
81 james 61
82     statement :: { Statement }
83     : ruleset { $1 }
84     | media { $1 }
85     | page { $1 }
86    
87     medium_list :: { [String] }
88     : { [] }
89     | medium_list_1 { $1 }
90    
91     medium_list_1 :: { [String] }
92     : medium { [$1] }
93 james 69 | medium_list_1 s COMMA s medium { $5 : $1 }
94 james 61
95     media :: { Statement }
96 james 69 : MEDIA_SYM s medium_list_1 s LBRACE s ruleset_list s RBRACE
97     { Media $3 $7 }
98 james 61
99     medium :: { String }
100     : IDENT { $1 }
101    
102     ruleset_list :: { [Statement] }
103     : { [] }
104 james 69 | ruleset_list s ruleset { $3 : $1 }
105 james 61
106     page :: { Statement }
107 james 69 : PAGE_SYM s LBRACE s declaration_list_1 s RBRACE
108     { Page Nothing $5 }
109     | PAGE_SYM s pseudo_page s LBRACE s declaration_list_1 RBRACE
110     { Page (Just $3) $7 }
111 james 61
112     declaration_list_1 :: { [Declaration] }
113     : { [] }
114 james 69 | declaration s { [$1] }
115     | declaration_list_1 s SEMI { $1 }
116     | declaration_list_1 s SEMI s declaration
117     { $5 : $1 }
118 james 61
119     pseudo_page :: { String }
120     : COLON IDENT { $2 }
121    
122     ruleset :: { Statement }
123 james 69 : selector_list_1 s LBRACE s declaration_list_1 s RBRACE
124     { Ruleset $1 $5 }
125 james 61
126     selector_list_1 :: { [Selector] }
127     : selector { [$1] }
128 james 69 | selector_list_1 COMMA s selector { $4 : $1 }
129 james 61
130     selector :: { Selector }
131     : simple_selector { [($1, NoMore)] }
132 james 69 | simple_selector combinator selector
133     { ($1, $2) : $3 }
134 james 61
135     combinator :: { Combinator }
136 james 69 : PLUS s { Preceded }
137     | GREATER s { Parent }
138     | S { Ancestor }
139 james 61
140     simple_selector :: { SimpleSelector }
141     : element_name detail_list_1 { ($1, $2) }
142     | element_name { ($1, []) }
143     | detail_list_1 { (Nothing, $1) }
144    
145     element_name :: { Maybe String }
146     : IDENT { Just $1 }
147     | ASTERISK { Nothing }
148    
149     detail_list_1 :: { [Detail] }
150     : HASH { [Id $1] }
151     | class { [$1] }
152     | attrib { [$1] }
153     | pseudo { [$1] }
154     | detail_list_1 HASH { Id $2 : $1 }
155     | detail_list_1 class { $2 : $1 }
156     | detail_list_1 attrib { $2 : $1 }
157     | detail_list_1 pseudo { $2 : $1 }
158    
159     class :: { Detail }
160     : DOT IDENT { Class $2 }
161    
162     attrib :: { Detail }
163 james 69 : LBRAC s IDENT s RBRAC { Attrib $3 }
164     | LBRAC s IDENT s EQUALS s IDENT s RBRAC
165     { AttribEq $3 $7 }
166     | LBRAC s IDENT s EQUALS s STRING s RBRAC
167     { AttribEq $3 $7 }
168     | LBRAC s IDENT s INCLUDES s IDENT s RBRAC
169     { AttribInc $3 $7 }
170     | LBRAC s IDENT s INCLUDES s STRING s RBRAC
171     { AttribInc $3 $7 }
172     | LBRAC s IDENT s DASHMATCH s IDENT s RBRAC
173     { AttribDM $3 $7 }
174     | LBRAC s IDENT s DASHMATCH s STRING s RBRAC
175     { AttribDM $3 $7 }
176 james 61
177     pseudo :: { Detail }
178     : COLON IDENT { Pseudo $2 }
179 james 69 | COLON FUNCTION s RPAREN { PseudoFunc $2 "" }
180     | COLON FUNCTION s IDENT s RPAREN { PseudoFunc $2 $4 }
181 james 61
182     declaration :: { Declaration }
183 james 69 : property COLON s value_list_1 { ($1, $4, False) }
184     | property COLON s value_list_1 s prio { ($1, $4, True) }
185 james 61
186     property :: { String }
187     : IDENT { $1 }
188    
189     prio :: { Bool }
190     : IMPORTANT_SYM { True }
191    
192     value_list :: { [Value] }
193     : { [] }
194 james 69 | value s value_list { $1 : $3 }
195 james 61
196     value_list_1 :: { [Value] }
197     : value { [$1] }
198 james 69 | value s value_list_1 { $1 : $3 }
199 james 61
200     value :: { Value }
201     : NUMBER { Number $1 }
202     | PERCENTAGE { Percentage $1 }
203     | LENGTH { Length $1 }
204     | EMS { Ems $1 }
205     | EXS { Exs $1 }
206     | ANGLE { Angle $1 }
207     | TIME { Time $1 }
208     | FREQ { Freq $1 }
209     | STRING { StringV $1 }
210     | IDENT { Ident $1 }
211     | URI { Uri $1 }
212     | HASH { HexColour $1 }
213     | SLASH { Slash }
214     | COMMA { Comma }
215 james 69 | FUNCTION s value_list s RPAREN { Function $1 $3 }
216 james 61
217 james 69 s :: {}
218     : { }
219     | S { }
220    
221 james 61 {
222 james 65 happyError :: [TokenPosn] -> a
223     happyError tks = error ("Parse error at " ++ lcn)
224     where
225     lcn = case tks of
226     [] -> "end of file"
227     tk:_ -> "line " ++ show l ++ ", column " ++ show c
228     where
229     AlexPn _ l c = snd tk
230 james 61 }

  ViewVC Help
Powered by ViewVC 1.1.26