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