1 |
-- Tokeniser for CSS 2.1 |
2 |
-- See CSS 2.1 G.2 |
3 |
|
4 |
{ |
5 |
module Tokeniser where |
6 |
} |
7 |
|
8 |
%wrapper "basic" |
9 |
|
10 |
$h = [0-9a-f] |
11 |
$nonascii = [\x80-\xff] |
12 |
@unicode = \\$h{1,6}(\r\n|[\ \t\r\n\f])? |
13 |
@escape = @unicode|\\[\x20-\x7e\x80-\xff] |
14 |
@nmstart = [_a-z]|$nonascii|@escape |
15 |
@nmchar = [\-_a-zA-Z0-9]|$nonascii|@escape |
16 |
@nl = \n|\r\n|\r|\f |
17 |
@string1 = \"([\t\ !#\x24\x25&\x28-\x7e]|\\@nl|\'|$nonascii|@escape|\.)*\" |
18 |
@string2 = \'([\t\ !#\x24\x25&\x28-\x7e]|\\@nl|\"|$nonascii|@escape|\.)*\' |
19 |
@ident = @nmstart@nmchar* |
20 |
@name = @nmchar+ |
21 |
@num = [\-\053]?[0-9]+|[0-9]*"."[0-9]+ |
22 |
@string = @string1|@string2 |
23 |
@url = ([!#\x24\x25&\x2a-\x7e]|$nonascii|@escape)* |
24 |
$s = [\ \t\r\n\f] |
25 |
@w = $s* |
26 |
@range = \?{1,6}|$h(\?{0,5}|$h(\?{0,4}|$h(\?{0,3}|$h(\?{0,2}|$h(\??|$h))))) |
27 |
@comment = \/\*[^\x2a]*\*+([^\/\x2a][^\x2a]*\*+)*\/ |
28 |
|
29 |
tokens :- |
30 |
|
31 |
$s+ ; |
32 |
@comment ; |
33 |
|
34 |
"<!--" ; |
35 |
"-->" ; |
36 |
"~=" { \s -> INCLUDES } |
37 |
"|=" { \s -> DASHMATCH } |
38 |
|
39 |
@w"{" { \s -> LBRACE } |
40 |
@w"+" { \s -> PLUS } |
41 |
@w">" { \s -> GREATER } |
42 |
@w"," { \s -> COMMA } |
43 |
|
44 |
@string { \s -> STRING s } |
45 |
|
46 |
@ident { \s -> IDENT s } |
47 |
|
48 |
"#"@name { \s -> HASH s } |
49 |
|
50 |
"@import" { \s -> IMPORT_SYM } |
51 |
"@page" { \s -> PAGE_SYM } |
52 |
"@media" { \s -> MEDIA_SYM } |
53 |
"@charset" { \s -> CHARSET_SYM } |
54 |
|
55 |
"!"@w"important" { \s -> IMPORTANT_SYM } |
56 |
|
57 |
@num em { \s -> EMS (read (take (length s - 2) s)) } |
58 |
@num ex { \s -> EXS (read (take (length s - 2) s)) } |
59 |
@num px { \s -> LENGTH s } |
60 |
@num cm { \s -> LENGTH s } |
61 |
@num mm { \s -> LENGTH s } |
62 |
@num in { \s -> LENGTH s } |
63 |
@num pt { \s -> LENGTH s } |
64 |
@num pc { \s -> LENGTH s } |
65 |
@num deg { \s -> ANGLE s } |
66 |
@num rad { \s -> ANGLE s } |
67 |
@num grad { \s -> ANGLE s } |
68 |
@num ms { \s -> TIME s } |
69 |
@num s { \s -> TIME s } |
70 |
@num Hz { \s -> FREQ s } |
71 |
@num kHz { \s -> FREQ s } |
72 |
@num @ident { \s -> DIMEN s } |
73 |
@num "%" { \s -> PERCENTAGE (read (take (length s - 1) s)) } |
74 |
@num { \s -> NUMBER (read s) } |
75 |
|
76 |
"url("@w@string@w")" { \s -> URI s } |
77 |
"url("@w@url@w")" { \s -> URI s } |
78 |
@ident"\050" { \s -> FUNCTION s } |
79 |
|
80 |
";" { \s -> SEMI } |
81 |
"}" { \s -> RBRACE } |
82 |
":" { \s -> COLON } |
83 |
"/" { \s -> SLASH } |
84 |
"-" { \s -> MINUS } |
85 |
"." { \s -> DOT } |
86 |
"*" { \s -> ASTERISK } |
87 |
"[" { \s -> LBRAC } |
88 |
"]" { \s -> RBRAC } |
89 |
"=" { \s -> EQUALS } |
90 |
")" { \s -> RPAREN } |
91 |
. { \s -> DELIM (head s) } |
92 |
|
93 |
|
94 |
{ |
95 |
data Token = |
96 |
INCLUDES | |
97 |
DASHMATCH | |
98 |
LBRACE | |
99 |
PLUS | |
100 |
GREATER | |
101 |
COMMA | |
102 |
STRING String | |
103 |
IDENT String | |
104 |
HASH String | |
105 |
IMPORT_SYM | |
106 |
PAGE_SYM | |
107 |
MEDIA_SYM | |
108 |
CHARSET_SYM | |
109 |
IMPORTANT_SYM | |
110 |
EMS Float | |
111 |
EXS Float | |
112 |
LENGTH String | |
113 |
ANGLE String | |
114 |
TIME String | |
115 |
FREQ String | |
116 |
DIMEN String | |
117 |
PERCENTAGE Float | |
118 |
NUMBER Float | |
119 |
URI String | |
120 |
FUNCTION String | |
121 |
SEMI | |
122 |
RBRACE | |
123 |
COLON | |
124 |
SLASH | |
125 |
MINUS | |
126 |
DOT | |
127 |
ASTERISK | |
128 |
LBRAC | |
129 |
RBRAC | |
130 |
EQUALS | |
131 |
RPAREN | |
132 |
DELIM Char |
133 |
deriving (Eq, Show) |
134 |
} |