acc: print something for identifiers and numbers
[acc.git] / main.c
blobcf1bbb6032305c13501c3736b70abb22201a5dea
1 /* Alexey's C compiler. */
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <unistd.h>
9 #include <stdarg.h>
10 #include <stdint.h>
11 #include <inttypes.h>
12 #include <string.h>
13 #include <limits.h>
15 struct pos {
16 unsigned int line, column;
19 #ifdef __GNUC__
20 #define __noreturn __attribute((noreturn))
21 #define __printf(a, b) __attribute__((format(printf, a, b)))
22 #else
23 #define __noreturn
24 #define __printf(a, b)
25 #endif
27 static void warning(struct pos *pos, const char *fmt, ...) __printf(2, 3);
28 static void warning(struct pos *pos, const char *fmt, ...)
30 va_list args;
32 fprintf(stderr, "%u:%u: warning: ", pos->line, pos->column);
33 va_start(args, fmt);
34 vfprintf(stderr, fmt, args);
35 va_end(args);
36 fputc('\n', stderr);
39 static void error_exit(struct pos *pos, const char *fmt, ...) __printf(2, 3) __noreturn;
40 static void error_exit(struct pos *pos, const char *fmt, ...)
42 va_list args;
44 fprintf(stderr, "%u:%u: error: ", pos->line, pos->column);
45 va_start(args, fmt);
46 vfprintf(stderr, fmt, args);
47 va_end(args);
48 fputc('\n', stderr);
49 exit(EXIT_FAILURE);
52 static void perror_exit(const char *fmt, ...) __printf(1, 2) __noreturn;
53 static void perror_exit(const char *fmt, ...)
55 int old_errno = errno;
56 va_list args;
58 fputs("acc: ", stderr);
59 va_start(args, fmt);
60 vfprintf(stderr, fmt, args);
61 va_end(args);
62 fputs(": ", stderr);
63 errno = old_errno;
64 perror(NULL);
65 exit(EXIT_FAILURE);
68 static void _error_exit(const char *fmt, ...) __printf(1, 2) __noreturn;
69 static void _error_exit(const char *fmt, ...)
71 va_list args;
73 fputs("acc: error: ", stderr);
74 va_start(args, fmt);
75 vfprintf(stderr, fmt, args);
76 va_end(args);
77 fputc('\n', stderr);
78 exit(EXIT_FAILURE);
81 static void *xmalloc(size_t size)
83 void *p;
85 p = malloc(size);
86 if (!p)
87 perror_exit("%s: size %zu", __func__, size);
88 return p;
91 static void *xmemdup(const void *src, size_t n)
93 void *dst;
95 dst = xmalloc(n);
96 memcpy(dst, src, n);
97 return dst;
100 static ssize_t _xread(int fd, void *buf, size_t count)
102 ssize_t rv;
104 do {
105 rv = read(fd, buf, count);
106 } while (rv == -1 && (errno == EAGAIN || errno == EINTR));
107 return rv;
110 static void xread(int fd, void *buf, size_t count)
112 while (count > 0) {
113 ssize_t rv;
115 rv = _xread(fd, buf, count);
116 if (rv == -1)
117 perror_exit("read fd %d, buf %p, count %zu", fd, buf, count);
118 if (rv == 0)
119 _error_exit("fd %d truncated, buf %p, count %zu", fd, buf, count);
121 buf = (char *)buf + rv;
122 count -= rv;
126 static void convert_from_utf8(uint8_t *_c, unsigned int _nr_c, uint32_t **c, unsigned int *nr_c)
128 unsigned int i;
130 if (_nr_c >= 0xffffffff / sizeof(uint32_t))
131 _error_exit("integer overflow _nr_c %"PRIu32, _nr_c);
133 /* At worse all data is ASCII. */
134 *c = xmalloc(_nr_c * sizeof(uint32_t));
135 *nr_c = 0;
137 i = 0;
138 while (i < _nr_c) {
139 static const struct {
140 uint8_t mask1, res1;
141 uint32_t min;
142 } _mask[] = {
143 { 0x80, 0x00, 0 }, /* 0xxxxxxx */
144 { 0xe0, 0xc0, 0x80 }, /* 110xxxxx 10xxxxxx */
145 { 0xf0, 0xe0, 0x800 }, /* 1110xxxx 10xxxxxx 10xxxxxx */
146 { 0xf8, 0xf0, 0x10000 }, /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
148 unsigned int level, j;
149 uint32_t ch;
151 level = 0;
152 while (level < sizeof(_mask) / sizeof(_mask[0])) {
153 if ((_c[i] & _mask[level].mask1) == _mask[level].res1)
154 break;
155 level++;
157 if (level == sizeof(_mask) / sizeof(_mask[0]))
158 _error_exit("invalid UTF-8 octet sequence at %u: %02"PRIx8, i, _c[i]);
159 if (i + level >= _nr_c)
160 _error_exit("truncated UTF-8 octet sequence at %u: %02"PRIx8, i, _c[i]);
161 for (j = 0; j < level; j++) {
162 if ((_c[i + j + 1] & 0xc0) != 0x80)
163 _error_exit("invalid UTF-8 octet sequence at %u: %02"PRIx8" ... %02"PRIx8, i + j + 1, _c[i], _c[i + j + 1]);
166 ch = _c[i] & ~_mask[level].mask1;
167 for (j = 0; j < level; j++)
168 ch = (ch << 6) | (_c[i + j + 1] & ~0xc0);
170 if (ch < _mask[level].min)
171 _error_exit("invalid UTF-8 octet sequence at %u: %02"PRIx8, i, _c[i]);
173 i += level + 1;
175 (*c)[*nr_c] = ch;
176 (*nr_c)++;
180 /* LINE SEPARATOR to catch \n. */
181 #define LS ((uint32_t)0x2028)
183 static void fix_newline(uint32_t *c, unsigned int *nr_c)
185 unsigned int i;
187 i = 0;
188 while (i < *nr_c) {
189 if (c[i] == 0x0d && i + 1 < *nr_c && c[i + 1] == 0x0a) {
190 memmove(&c[i], &c[i + 1], *nr_c - i - 1);
191 (*nr_c)--;
193 switch (c[i]) {
194 case 0x0d:
195 case 0x0a:
196 case 0x85:
197 case 0x0b:
198 case 0x0c:
199 case 0x2028:
200 case 0x2029:
201 c[i] = LS;
203 i++;
207 static struct pos *line_column(const uint32_t *c, unsigned int nr_c)
209 struct pos *pos;
210 unsigned int line, column;
211 unsigned int i;
213 if (nr_c >= 0xffffffff / sizeof(struct pos))
214 _error_exit("integer overflow nr_c %u", nr_c);
215 pos = xmalloc(nr_c * sizeof(struct pos));
217 line = 1;
218 column = 1;
219 for (i = 0; i < nr_c; i++) {
220 pos[i].line = line;
221 pos[i].column = column;
223 if (c[i] == LS) {
224 line++;
225 column = 1;
226 } else
227 column++;
229 return pos;
232 static void warn_trigraph(const uint32_t *c, unsigned int nr_c, struct pos *pos)
234 unsigned int i;
236 i = 0;
237 while (i + 2 < nr_c) {
238 if (c[i] == '?' && c[i + 1] == '?') {
239 switch (c[i + 2]) {
240 case '=':case ')':case '!':
241 case '(':case '\'':case '>':
242 case '/':case '<':case '-':
243 warning(&pos[i], "trigraph sequence ??%c, ignoring", c[i + 2]);
244 i += 3;
245 break;
246 default:
247 i++;
249 } else
250 i++;
254 static void delete_backslash_newline(uint32_t *c, unsigned int *nr_c, struct pos *pos)
256 unsigned int i;
258 i = 0;
259 while (i + 1 < *nr_c) {
260 if (c[i] == '\\' && c[i + 1] == LS) {
261 unsigned int nr_to_move = *nr_c - i - 2;
263 memmove(&c[i], &c[i + 2], nr_to_move * sizeof(uint32_t));
264 memmove(&pos[i], &pos[i + 2], nr_to_move * sizeof(struct pos));
265 (*nr_c) -= 2;
266 } else
267 i++;
271 struct pp_token {
272 struct pp_token *next;
273 enum pp_token_type {
274 PP_TOKEN_IDENTIFIER = UCHAR_MAX + 1,
275 PP_TOKEN_NUMBER,
277 #define _2(c1, c2) ((((uint32_t)c1) << 8) | ((uint32_t)c2))
278 #define _3(c1, c2, c3) ((((uint32_t)c1) << 16)| (((uint32_t)c2) << 8) | ((uint32_t)c3))
279 PP_TOKEN_DOTDOTDOT = _3('.', '.', '.'),
280 PP_TOKEN_DEREFERENCE = _2('-', '>'),
281 PP_TOKEN_SUB_EQ = _2('-', '='),
282 PP_TOKEN_DEC = _2('-', '-'),
283 PP_TOKEN_ADD_EQ = _2('+', '='),
284 PP_TOKEN_INC = _2('+', '+'),
285 PP_TOKEN_AND_EQ = _2('&', '='),
286 PP_TOKEN_AND = _2('&', '&'),
287 PP_TOKEN_MUL_EQ = _2('*', '='),
288 PP_TOKEN_NOT_EQ = _2('!', '='),
289 PP_TOKEN_DIV_EQ = _2('/', '='),
290 PP_TOKEN_REM_EQ = _2('%', '='),
291 PP_TOKEN_LSHIFT_EQ = _3('<', '<', '='),
292 PP_TOKEN_LSHIFT = _2('<', '<'),
293 PP_TOKEN_LEQ = _2('<', '='),
294 PP_TOKEN_RSHIFT_EQ = _3('>', '>', '='),
295 PP_TOKEN_RSHIFT = _2('>', '>'),
296 PP_TOKEN_GEQ = _2('>', '='),
297 PP_TOKEN_EQ = _2('=', '='),
298 PP_TOKEN_XOR_EQ = _2('^', '='),
299 PP_TOKEN_OR_EQ = _2('|', '='),
300 PP_TOKEN_OR = _2('|', '|'),
301 PP_TOKEN_SHARPSHARP = _2('#', '#'),
302 #undef _2
303 #undef _3
304 } type;
305 uint32_t *id; /* string representation, if type is not enough */
306 struct pos pos;
309 static struct pp_token *pp_token_create(enum pp_token_type type, struct pos *pos)
311 struct pp_token *ppt;
313 ppt = xmalloc(sizeof(struct pp_token));
314 ppt->next = NULL;
315 ppt->type = type;
316 ppt->id = NULL;
317 ppt->pos = *pos;
318 return ppt;
321 /* [start, end) */
322 static void pp_token_add(struct pp_token *ppt, const uint32_t *c, unsigned int start, unsigned int end)
324 ppt->id = xmemdup(&c[start], (end - start) * sizeof(uint32_t));
327 static void pp_token_free(struct pp_token *ppt_head)
329 struct pp_token *ppt;
331 ppt = ppt_head;
332 while (ppt) {
333 struct pp_token *next;
335 next = ppt->next;
336 if (ppt->id)
337 free(ppt->id);
338 free(ppt);
339 ppt = next;
343 static void pp_token_print(struct pp_token *ppt)
345 printf("%u:%u:\t", ppt->pos.line, ppt->pos.column);
346 switch (ppt->type) {
347 case LS:
348 printf("\\n");
349 break;
350 case ' ':
351 printf("' '");
352 break;
353 case PP_TOKEN_IDENTIFIER:
354 printf("pp-identifier");
355 break;
356 case PP_TOKEN_NUMBER:
357 printf("pp-number");
358 break;
359 case PP_TOKEN_DOTDOTDOT:
360 case PP_TOKEN_LSHIFT_EQ:
361 case PP_TOKEN_RSHIFT_EQ:
362 printf("%c%c%c", (ppt->type >> 16) & 0xff, (ppt->type >> 8) & 0xff, ppt->type & 0xff);
363 break;
364 case PP_TOKEN_DEREFERENCE:
365 case PP_TOKEN_SUB_EQ:
366 case PP_TOKEN_DEC:
367 case PP_TOKEN_ADD_EQ:
368 case PP_TOKEN_INC:
369 case PP_TOKEN_AND_EQ:
370 case PP_TOKEN_AND:
371 case PP_TOKEN_MUL_EQ:
372 case PP_TOKEN_NOT_EQ:
373 case PP_TOKEN_DIV_EQ:
374 case PP_TOKEN_REM_EQ:
375 case PP_TOKEN_LSHIFT:
376 case PP_TOKEN_LEQ:
377 case PP_TOKEN_RSHIFT:
378 case PP_TOKEN_GEQ:
379 case PP_TOKEN_EQ:
380 case PP_TOKEN_XOR_EQ:
381 case PP_TOKEN_OR_EQ:
382 case PP_TOKEN_OR:
383 case PP_TOKEN_SHARPSHARP:
384 printf("%c%c", (ppt->type >> 8) & 0xff, ppt->type & 0xff);
385 break;
386 default:
387 printf("%c", ppt->type);
389 putc('\n', stdout);
392 static int pp_nondigit(const uint32_t c)
394 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_';
397 static int pp_digit(const uint32_t c)
399 return '0' <= c && c <= '9';
402 static int pp_hexdigit(const uint32_t c)
404 return pp_digit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
407 /* pp-identifier: ([a-zA-Z_]|\u[0-9a-fA-F]{4}|\U[0-9a-fA-F]{8})([a-zA-Z_0-9]|\u[0-9a-fA-F]{4}|\U[0-9a-fA-F]{8})* */
408 static unsigned int _pp_identifier_end(const uint32_t *c, unsigned int nr_c, unsigned int start)
410 unsigned int i;
412 /* First identifier-nondigit is already "parsed". */
413 i = start;
414 while (i < nr_c) {
415 if (pp_nondigit(c[i]) || pp_digit(c[i])) {
416 i++;
417 } else if (i + 5 < nr_c && c[i] == '\\' && c[i + 1] == 'u' &&
418 pp_hexdigit(c[i + 2]) && pp_hexdigit(c[i + 3]) && pp_hexdigit(c[i + 4]) && pp_hexdigit(c[i + 5])) {
419 i += 2 + 4;
420 } else if (i + 9 < nr_c && c[i] == '\\' && c[i + 1] == 'U' &&
421 pp_hexdigit(c[i + 2]) && pp_hexdigit(c[i + 3]) && pp_hexdigit(c[i + 4]) && pp_hexdigit(c[i + 5]) &&
422 pp_hexdigit(c[i + 6]) && pp_hexdigit(c[i + 7]) && pp_hexdigit(c[i + 8]) && pp_hexdigit(c[i + 9])) {
423 i += 2 + 4 + 4;
424 } else
425 return i;
427 return i;
430 /* pp-number: \.?[0-9]([eEpP][+-]|[a-zA-Z_.]|\u[0-9a-fA-F]{4}|\U[0-9a-fA-F]{8})* */
431 static unsigned int _pp_number_end(const uint32_t *c, unsigned int nr_c, unsigned int start)
433 unsigned int i;
435 /* First digit is already "parsed". */
436 i = start;
437 while (i < nr_c) {
438 if ((c[i] == 'e' || c[i] == 'E' || c[i] == 'p' || c[i] == 'P') &&
439 i + 1 < nr_c && (c[i + 1] == '+' || c[i + 1] == '-')) {
440 i += 2;
441 } else if (pp_digit(c[i]) || pp_nondigit(c[i]) || c[i] == '.') {
442 i++;
443 } else if (c[i] == '\\' && i + 5 < nr_c && c[i + 1] == 'u' &&
444 pp_hexdigit(c[i + 2]) && pp_hexdigit(c[i + 3]) && pp_hexdigit(c[i + 4]) && pp_hexdigit(c[i + 5])) {
445 i += 2 + 4;
446 } else if (c[i] == '\\' && i + 9 < nr_c && c[i + 1] == 'U' &&
447 pp_hexdigit(c[i + 2]) && pp_hexdigit(c[i + 3]) && pp_hexdigit(c[i + 4]) && pp_hexdigit(c[i + 5]) &&
448 pp_hexdigit(c[i + 6]) && pp_hexdigit(c[i + 7]) && pp_hexdigit(c[i + 8]) && pp_hexdigit(c[i + 9])) {
449 i += 2 + 4 + 4;
450 } else
451 return i;
453 return i;
456 static unsigned int c_comment_end(const uint32_t *c, unsigned int nr_c, unsigned int start)
458 unsigned int i;
460 i = start + 2;
461 while (i + 1 < nr_c) {
462 if (c[i] == '*' && c[i + 1] == '/')
463 return i + 2;
464 i++;
466 return nr_c;
469 static unsigned int cpp_comment_end(const uint32_t *c, unsigned int nr_c, unsigned int start)
471 unsigned int i;
473 i = start + 2;
474 while (i < nr_c && c[i] != LS)
475 i++;
476 return i;
479 static struct pp_token *pp_tokenize(const uint32_t *c, unsigned int nr_c, struct pos *_pos)
481 struct pp_token *ppt_head, *ppt_tail;
482 unsigned int i;
484 ppt_head = NULL;
485 i = 0;
486 while (i < nr_c) {
487 struct pos *pos = &_pos[i];
488 struct pp_token *ppt;
490 switch (c[i]) {
491 unsigned int j;
493 case '\t':
494 case ' ':
495 ppt = pp_token_create(' ', pos);
496 i++;
497 break;
498 case LS:
499 ppt = pp_token_create(LS, pos);
500 i++;
501 break;
502 case '[':case ']':
503 case '(':case ')':
504 case '{':case '}':
505 case '~':
506 case '?':
507 case ':':
508 case ';':
509 case ',':
510 pp_token_simple:
511 ppt = pp_token_create(c[i], pos);
512 i++;
513 break;
514 case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':case 'g':
515 case 'h':case 'i':case 'j':case 'k':case 'l':case 'm':case 'n':
516 case 'o':case 'p':case 'q':case 'r':case 's':case 't':case 'u':
517 case 'v':case 'w':case 'x':case 'y':case 'z':
518 case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':case 'G':
519 case 'H':case 'I':case 'J':case 'K':case 'L':case 'M':case 'N':
520 case 'O':case 'P':case 'Q':case 'R':case 'S':case 'T':case 'U':
521 case 'V':case 'W':case 'X':case 'Y':case 'Z':
522 case '_':
523 ppt = pp_token_create(PP_TOKEN_IDENTIFIER, pos);
524 j = _pp_identifier_end(c, nr_c, i + 1);
525 pp_token_add(ppt, c, i, j);
526 i = j;
527 break;
528 case '\\':
529 if (i + 5 < nr_c && c[i + 1] == 'u' &&
530 pp_hexdigit(c[i + 2]) && pp_hexdigit(c[i + 3]) && pp_hexdigit(c[i + 4]) && pp_hexdigit(c[i + 5])) {
531 ppt = pp_token_create(PP_TOKEN_IDENTIFIER, pos);
532 j = _pp_identifier_end(c, nr_c, i + 2 + 4);
533 pp_token_add(ppt, c, i, j);
534 i = j;
535 } else if (i + 9 < nr_c && c[i + 1] == 'U' &&
536 pp_hexdigit(c[i + 2]) && pp_hexdigit(c[i + 3]) && pp_hexdigit(c[i + 4]) && pp_hexdigit(c[i + 5]) &&
537 pp_hexdigit(c[i + 6]) && pp_hexdigit(c[i + 7]) && pp_hexdigit(c[i + 8]) && pp_hexdigit(c[i + 9])) {
538 ppt = pp_token_create(PP_TOKEN_IDENTIFIER, pos);
539 j = _pp_identifier_end(c, nr_c, i + 2 + 4 + 4);
540 pp_token_add(ppt, c, i, j);
541 i = j;
542 } else
543 error_exit(pos, "unknown character %08"PRIx32, c[i]);
544 break;
545 case '0':case '1':case '2':case '3':case '4':
546 case '5':case '6':case '7':case '8':case '9':
547 ppt = pp_token_create(PP_TOKEN_NUMBER, pos);
548 j = _pp_number_end(c, nr_c, i + 1);
549 pp_token_add(ppt, c, i, j);
550 i = j;
551 break;
552 case '.':
553 if (i + 2 < nr_c && c[i + 1] == '.' && c[i + 2] == '.') {
554 ppt = pp_token_create(PP_TOKEN_DOTDOTDOT, pos);
555 i += 3;
556 } else if (i + 1 < nr_c && pp_digit(c[i + 1])) {
557 ppt = pp_token_create(PP_TOKEN_NUMBER, pos);
558 j = _pp_number_end(c, nr_c, i + 2);
559 pp_token_add(ppt, c, i, j);
560 i = j;
561 } else
562 goto pp_token_simple;
563 break;
564 case '/':
565 if (i + 1 < nr_c && c[i + 1] == '*') {
566 ppt = pp_token_create(' ', pos);
567 i = c_comment_end(c, nr_c, i);
568 } else if (i + 1 < nr_c && c[i + 1] == '/') {
569 warning(pos, "C++ comment");
570 ppt = pp_token_create(' ', pos);
571 i = cpp_comment_end(c, nr_c, i);
572 } else if (i + 1 < nr_c && c[i + 1] == '=') {
573 ppt = pp_token_create((c[i] << 8) | c[i + 1], pos);
574 i += 2;
575 } else
576 goto pp_token_simple;
577 break;
578 case '-':
579 if (i + 1 < nr_c && (c[i + 1] == '>' || c[i + 1] == '=' || c[i + 1] == '-')) {
580 ppt = pp_token_create((c[i] << 8) | c[i + 1], pos);
581 i += 2;
582 } else
583 goto pp_token_simple;
584 break;
585 case '+':
586 case '&':
587 case '|':
588 if (i + 1 < nr_c && (c[i + 1] == '=' || c[i + 1] == c[i])) {
589 ppt = pp_token_create((c[i] << 8) | c[i + 1], pos);
590 i += 2;
591 } else
592 goto pp_token_simple;
593 break;
594 case '*':
595 case '!':
596 case '%':
597 case '=':
598 case '^':
599 if (i + 1 < nr_c && c[i + 1] == '=') {
600 ppt = pp_token_create((c[i] << 8) | c[i + 1], pos);
601 i += 2;
602 } else
603 goto pp_token_simple;
604 break;
605 case '<':
606 case '>':
607 if (i + 2 < nr_c && c[i + 1] == c[i] && c[i + 2] == '=') {
608 ppt = pp_token_create((c[i] << 16) | (c[i + 1] << 8) | c[i + 2], pos);
609 i += 3;
610 } else if (i + 1 < nr_c && (c[i + 1] == c[i] || c[i + 1] == '=')) {
611 ppt = pp_token_create((c[i] << 8) | c[i + 1], pos);
612 i += 2;
613 } else
614 goto pp_token_simple;
615 break;
616 case '#':
617 if (i + 1 < nr_c && c[i + 1] == '#') {
618 ppt = pp_token_create((c[i] << 8) | c[i + 1], pos);
619 i += 2;
620 } else
621 goto pp_token_simple;
622 break;
623 default:
624 error_exit(pos, "unknown character %08"PRIx32, c[i]);
627 if (!ppt_head)
628 ppt_head = ppt;
629 else
630 ppt_tail->next = ppt;
631 ppt_tail = ppt;
633 return ppt_head;
636 int main(int argc, char *argv[])
638 int fd;
639 struct stat st;
640 unsigned int st_size;
641 void *buf;
642 uint8_t *_c;
643 unsigned int _nr_c;
644 uint32_t *c;
645 unsigned int nr_c;
646 struct pos *pos;
647 struct pp_token *ppt_head;
649 if (argc < 2)
650 return EXIT_FAILURE;
652 fd = open(argv[1], O_RDONLY);
653 if (fd == -1)
654 perror_exit("open %s", argv[1]);
655 if (fstat(fd, &st) == -1)
656 perror_exit("fstat %s", argv[1]);
657 if (st.st_size < 0)
658 _error_exit("%s: negative st_size %"PRIdMAX, argv[1], (intmax_t)st.st_size);
659 st_size = (unsigned int)(uintmax_t)(intmax_t)st.st_size;
660 if ((uintmax_t)(intmax_t)st.st_size != (uintmax_t)st_size)
661 _error_exit("%s: too big st_size %"PRIdMAX, argv[1], (intmax_t)st.st_size);
663 buf = xmalloc(st_size);
664 xread(fd, buf, st_size);
665 close(fd);
667 _c = buf;
668 _nr_c = st_size;
669 /* Skip UTF-8 "BOM" if any. */
670 if (st_size >= 3 && _c[0] == 0xef && _c[1] == 0xbb && _c[2] == 0xbf) {
671 _c += 3;
672 _nr_c -= 3;
674 convert_from_utf8(_c, _nr_c, &c, &nr_c);
675 free(buf);
677 fix_newline(c, &nr_c);
678 pos = line_column(c, nr_c);
679 warn_trigraph(c, nr_c, pos);
680 delete_backslash_newline(c, &nr_c, pos);
682 ppt_head = pp_tokenize(c, nr_c, pos);
683 free(c);
684 free(pos);
687 struct pp_token *ppt;
689 for (ppt = ppt_head; ppt; ppt = ppt->next)
690 pp_token_print(ppt);
693 pp_token_free(ppt_head);
695 return EXIT_SUCCESS;