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

Contents of /factorcss/Parser.y

Parent Directory Parent Directory | Revision Log Revision Log


Revision 65 - (show annotations) (download)
Tue Dec 28 14:23:12 2004 UTC (20 years ago) by james
File size: 5398 byte(s)
Output line and column number with parse errors.

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

  ViewVC Help
Powered by ViewVC 1.1.26