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