1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
#include <inttypes.h>
#include <stdio.h>
#include <parserutils/charset/utf8.h>
#include <parserutils/input/inputstream.h>
#include <libcss/libcss.h>
#include "charset/detect.h"
#include "utils/utils.h"
#include "lex/lex.h"
#include "testutils.h"
#define ITERATIONS (1)
#define DUMP_TOKENS (0)
static void *myrealloc(void *data, size_t len, void *pw)
{
UNUSED(pw);
return realloc(data, len);
}
static void printToken(const css_token *token)
{
#if !DUMP_TOKENS
UNUSED(token);
#else
printf("[%d, %d] : ", token->line, token->col);
switch (token->type) {
case CSS_TOKEN_IDENT:
printf("IDENT(%.*s)",
(int) token->data.len, token->data.data);
break;
case CSS_TOKEN_ATKEYWORD:
printf("ATKEYWORD(%.*s)",
(int) token->data.len, token->data.data);
break;
case CSS_TOKEN_STRING:
printf("STRING(%.*s)",
(int) token->data.len, token->data.data);
break;
case CSS_TOKEN_INVALID_STRING:
printf("INVALID(%.*s)",
(int) token->data.len, token->data.data);
break;
case CSS_TOKEN_HASH:
printf("HASH(%.*s)",
(int) token->data.len, token->data.data);
break;
case CSS_TOKEN_NUMBER:
printf("NUMBER(%.*s)",
(int) token->data.len, token->data.data);
break;
case CSS_TOKEN_PERCENTAGE:
printf("PERCENTAGE(%.*s)",
(int) token->data.len, token->data.data);
break;
case CSS_TOKEN_DIMENSION:
printf("DIMENSION(%.*s)",
(int) token->data.len, token->data.data);
break;
case CSS_TOKEN_URI:
printf("URI(%.*s)", (int) token->data.len, token->data.data);
break;
case CSS_TOKEN_UNICODE_RANGE:
printf("UNICODE-RANGE(%.*s)",
(int) token->data.len, token->data.data);
break;
case CSS_TOKEN_CDO:
printf("CDO");
break;
case CSS_TOKEN_CDC:
printf("CDC");
break;
case CSS_TOKEN_S:
printf("S");
break;
case CSS_TOKEN_COMMENT:
printf("COMMENT(%.*s)", (int) token->data.len, token->data.data);
break;
case CSS_TOKEN_FUNCTION:
printf("FUNCTION(%.*s)",
(int) token->data.len, token->data.data);
break;
case CSS_TOKEN_INCLUDES:
printf("INCLUDES");
break;
case CSS_TOKEN_DASHMATCH:
printf("DASHMATCH");
break;
case CSS_TOKEN_PREFIXMATCH:
printf("PREFIXMATCH");
break;
case CSS_TOKEN_SUFFIXMATCH:
printf("SUFFIXMATCH");
break;
case CSS_TOKEN_SUBSTRINGMATCH:
printf("SUBSTRINGMATCH");
break;
case CSS_TOKEN_CHAR:
printf("CHAR(%.*s)", (int) token->data.len, token->data.data);
break;
case CSS_TOKEN_EOF:
printf("EOF");
break;
case CSS_TOKEN_LAST_INTERN_LOWER:
case CSS_TOKEN_LAST_INTERN:
break;
}
printf("\n");
#endif
}
int main(int argc, char **argv)
{
parserutils_inputstream *stream;
css_lexer *lexer;
FILE *fp;
size_t len, origlen;
#define CHUNK_SIZE (4096)
uint8_t buf[CHUNK_SIZE];
css_token *tok;
css_error error;
int i;
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
for (i = 0; i < ITERATIONS; i++) {
assert(parserutils_inputstream_create("UTF-8",
CSS_CHARSET_DICTATED,css__charset_extract,
(parserutils_alloc) myrealloc, NULL, &stream) ==
PARSERUTILS_OK);
assert(css__lexer_create(stream, myrealloc, NULL, &lexer) ==
CSS_OK);
fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("Failed opening %s\n", argv[1]);
return 1;
}
fseek(fp, 0, SEEK_END);
origlen = len = ftell(fp);
fseek(fp, 0, SEEK_SET);
while (len >= CHUNK_SIZE) {
size_t read = fread(buf, 1, CHUNK_SIZE, fp);
assert(read == CHUNK_SIZE);
assert(parserutils_inputstream_append(stream,
buf, CHUNK_SIZE) == PARSERUTILS_OK);
len -= CHUNK_SIZE;
while ((error = css__lexer_get_token(lexer, &tok)) ==
CSS_OK) {
printToken(tok);
if (tok->type == CSS_TOKEN_EOF)
break;
}
}
if (len > 0) {
size_t read = fread(buf, 1, len, fp);
assert(read == len);
assert(parserutils_inputstream_append(stream,
buf, len) == PARSERUTILS_OK);
len = 0;
}
fclose(fp);
assert(parserutils_inputstream_append(stream, NULL, 0) ==
PARSERUTILS_OK);
while ((error = css__lexer_get_token(lexer, &tok)) == CSS_OK) {
printToken(tok);
if (tok->type == CSS_TOKEN_EOF)
break;
}
css__lexer_destroy(lexer);
parserutils_inputstream_destroy(stream);
}
printf("PASS\n");
return 0;
}
|