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 |
S { (S , _) } |
21 |
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 |
|
59 |
%% |
60 |
|
61 |
stylesheet :: { Stylesheet } |
62 |
: charset s import_list s statement_list s |
63 |
{ Stylesheet $1 $3 $5 } |
64 |
|
65 |
charset :: { Maybe String } |
66 |
: { Nothing } |
67 |
| CHARSET_SYM s STRING s SEMI { Just $3 } |
68 |
|
69 |
import_list :: { [(String, [String])] } |
70 |
: { [] } |
71 |
| import_list s import { $3 : $1 } |
72 |
|
73 |
import :: { (String, [String]) } |
74 |
: 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 |
|
78 |
statement_list :: { [Statement] } |
79 |
: { [] } |
80 |
| statement_list s statement { $3 : $1 } |
81 |
|
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 |
| medium_list_1 s COMMA s medium { $5 : $1 } |
94 |
|
95 |
media :: { Statement } |
96 |
: MEDIA_SYM s medium_list_1 LBRACE s ruleset_list RBRACE |
97 |
{ Media $3 $6 } |
98 |
|
99 |
medium :: { String } |
100 |
: IDENT { $1 } |
101 |
|
102 |
ruleset_list :: { [Statement] } |
103 |
: { [] } |
104 |
| ruleset_list s ruleset { $3 : $1 } |
105 |
|
106 |
page :: { Statement } |
107 |
: PAGE_SYM LBRACE s declaration_list_1 RBRACE |
108 |
{ Page Nothing $4 } |
109 |
| PAGE_SYM s pseudo_page LBRACE s declaration_list_1 RBRACE |
110 |
{ Page (Just $3) $6 } |
111 |
|
112 |
declaration_list_1 :: { [Declaration] } |
113 |
: { [] } |
114 |
| declaration { [$1] } |
115 |
| declaration_list_1 s SEMI s { $1 } |
116 |
| declaration_list_1 s SEMI s declaration |
117 |
{ $5 : $1 } |
118 |
|
119 |
pseudo_page :: { String } |
120 |
: COLON IDENT { $2 } |
121 |
|
122 |
ruleset :: { Statement } |
123 |
: selector_list_1 LBRACE s declaration_list_1 RBRACE |
124 |
{ Ruleset $1 $4 } |
125 |
|
126 |
selector_list_1 :: { [Selector] } |
127 |
: selector { [$1] } |
128 |
| selector_list_1 COMMA s selector { $4 : $1 } |
129 |
|
130 |
selector :: { Selector } |
131 |
: simple_selector { [($1, NoMore)] } |
132 |
| simple_selector combinator selector |
133 |
{ ($1, $2) : $3 } |
134 |
|
135 |
combinator :: { Combinator } |
136 |
: PLUS s { Preceded } |
137 |
| GREATER s { Parent } |
138 |
| S { Ancestor } |
139 |
|
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 |
: 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 |
|
177 |
pseudo :: { Detail } |
178 |
: COLON IDENT { Pseudo $2 } |
179 |
| COLON FUNCTION s RPAREN { PseudoFunc $2 "" } |
180 |
| COLON FUNCTION s IDENT s RPAREN { PseudoFunc $2 $4 } |
181 |
|
182 |
declaration :: { Declaration } |
183 |
: property COLON s value_list_1 { ($1, $4, False) } |
184 |
| property COLON s value_list_1 prio { ($1, $4, True) } |
185 |
|
186 |
property :: { String } |
187 |
: IDENT s { $1 } |
188 |
|
189 |
prio :: { Bool } |
190 |
: IMPORTANT_SYM { True } |
191 |
|
192 |
value_list :: { [Value] } |
193 |
: { [] } |
194 |
| value s value_list { $1 : $3 } |
195 |
|
196 |
value_list_1 :: { [Value] } |
197 |
: value s { [$1] } |
198 |
| value s value_list_1 { $1 : $3 } |
199 |
|
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 |
| FUNCTION s value_list s RPAREN { Function $1 $3 } |
216 |
|
217 |
s :: {} |
218 |
: { } |
219 |
| s S { } |
220 |
|
221 |
{ |
222 |
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 |
} |