aboutsummaryrefslogtreecommitdiff
path: root/src/lex.c
blob: 3b1026672ed5d363794e364220afd841c0923969 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <regex.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "utils.h"
#include "string-builder.h"
#include "lexer.h"

const char *file;

static void
print_lexing_error (const struct lexer *lexer,
                    const struct lexeme *lexeme,
                    const char *fmt, ...)
{
  const char *beginning_of_line = lexer->sv.beg;
  size_t line = 1;

  for (const char *p = lexer->sv.beg; p < lexeme->sv.beg; ++p) {
    if (*p == '\n') {
      beginning_of_line = p + 1;
      line += 1;
    }
  }

  fprintf (stderr, "%s:%d:%d: error: ", file, (int) line,
           (int) (lexeme->sv.beg - beginning_of_line));
  va_list ap;
  va_start (ap, fmt);
  vfprintf (stderr, fmt, ap);
  va_end (ap);
  putc ('\n', stderr);

  const char *p = beginning_of_line;
  fprintf (stderr, "%5lu | ", line);
  while (*p != '\n')
    putc (*p++, stderr);
  putc ('\n', stderr);

  p = beginning_of_line;
  fprintf (stderr, "      | ");
  while (p++ < lexeme->sv.beg)
    putc (' ', stderr);
  putc ('^', stderr);
  while (p++ < lexeme->sv.end)
    putc ('~', stderr);
  putc ('\n', stderr);
}

static int
display_to_byte_len (const char *str, int len)
{
  while (*str) {
    if (*str++ & 0xD0) {
      len += 1;
      str += 1;
    }
  }

  return len;
}

int
main (int argc, char *argv[])
{
  const char *progname = *argv++;
  if (argc < 2) DIE ("Usage: %s file", progname);

  file = *argv++;
  int fd = open (file, O_RDONLY);
  if (fd == -1) DIE ("error: %s: %s", strerror (errno), file);

  struct stat st;
  fstat (fd, &st);
  char *string = mmap (NULL, st.st_size + 1, PROT_READ, MAP_PRIVATE, fd, 0);
  close (fd);

  puts ("Исходный текст:");
  fwrite (string, 1, st.st_size, stdout);

  FILE *dump = fopen ("./build/dump.txt", "w");

  struct {
    struct lexeme *items;
    uint32_t count;
    uint32_t capacity;
  } invalid_lexemes = {0};

  struct string_builder sb = {0};
  struct lexer lexer = lexer_create (string, st.st_size);
  putchar ('\n');
  printf ("| *Лексема*        | *Тип лексемы*               | *Значение* |\n");
  printf ("|------------------+-----------------------------+------------|\n");
  for (struct lexeme lexeme = lexer_next_lexeme (&lexer);
       lexeme.kind != LEXEME_KIND_END_OF_FILE;
       lexeme = lexer_next_lexeme (&lexer)) {
    switch (lexeme.kind) {
      case LEXEME_KIND_INVALID:
      case LEXEME_KIND_IDENTIFIER_TOO_LONG:
      case LEXEME_KIND_CONSTANT_STRING_TOO_LONG:
      case LEXEME_KIND_CONSTANT_NUMBER_INVALID:
      case LEXEME_KIND_COMMENT_UNTERMINATED:
        DYNAMIC_ARRAY_APPEND (invalid_lexemes, lexeme);
        continue;
      default: break;
    }

    if (lexeme.kind == LEXEME_KIND_COMMENT)
      continue;

    fwrite (lexeme.sv.beg, STRING_VIEW_LEN (lexeme.sv), 1, dump);
    putc (' ', dump);

    switch (lexeme.kind) {
      case LEXEME_KIND_KEYWORD:
        string_builder_printf (&sb, "K[%d]", lexeme.value.table_index);
        break;
      case LEXEME_KIND_SYMBOL_COMPARISON:
        string_builder_printf (&sb, "C[%d]", lexeme.value.table_index);
        break;
      case LEXEME_KIND_CONSTANT_INTEGER:
        string_builder_printf (&sb, "%d", lexeme.value.integer);
        break;
      case LEXEME_KIND_CONSTANT_REAL:
        string_builder_printf (&sb, "%g", lexeme.value.real);
        break;
      case LEXEME_KIND_CONSTANT_STRING:
        string_builder_printf (&sb, "S[%d]", lexeme.value.table_index);
        break;
      case LEXEME_KIND_IDENTIFIER:
        string_builder_printf (&sb, "I[%d]", lexeme.value.table_index);
        break;
      default:
        STRING_BUILDER_APPEND_CSTR (sb, "");
    }
    string_builder_append_ntimes (&sb, ' ', 10 - sb.count);
    STRING_BUILDER_APPEND_NULL (sb);
    printf ("| %-16.*s | %-*s | %s |\n",
            (int) STRING_VIEW_LEN (lexeme.sv), lexeme.sv.beg,
            display_to_byte_len (lexeme_kind_names[lexeme.kind - 1], 27),
            lexeme_kind_names[lexeme.kind - 1],
            sb.items);
    STRING_BUILDER_RESET (sb);
  }

  fclose (dump);

  putchar ('\n');
  printf ("| *№* | *K* |\n");
  printf ("|-----+-----|\n");
  for (int32_t i = 0; i < KEYWORD_COUNT; ++i)
    printf ("| %3d | %-3s |\n", i, keyword_names[i]);

  putchar ('\n');
  printf ("| *№* | *C* |\n");
  printf ("|-----+-----|\n");
  for (int32_t i = 0; i < COMPARISON_COUNT; ++i)
    printf ("| %3d | %-3s |\n", i, comparison_names[i]);

  putchar ('\n');
  printf ("| *№* | *S*            |\n");
  printf ("|-----+----------------|\n");
  DYNAMIC_ARRAY_FOREACH (e, lexer.constant_string_table) {
    printf ("| %3ld | %-14.*s |\n", e - lexer.constant_string_table.items,
            (int) STRING_VIEW_LEN (*e), e->beg);
  }

  putchar ('\n');
  printf ("| *№* | *I*              |\n");
  printf ("|-----+------------------|\n");
  DYNAMIC_ARRAY_FOREACH (e, lexer.identifier_table) {
    printf ("| %3ld | %-16.*s |\n", e - lexer.identifier_table.items,
            (int) STRING_VIEW_LEN (*e), e->beg);
  }

  DYNAMIC_ARRAY_FOREACH (invalid_lexeme, invalid_lexemes) {
    switch (invalid_lexeme->kind) {
      case LEXEME_KIND_INVALID:
        print_lexing_error (&lexer, invalid_lexeme, "invalid lexeme `%.*s'",
                            (int) STRING_VIEW_LEN (invalid_lexeme->sv),
                            invalid_lexeme->sv.beg);
        break;
      case LEXEME_KIND_CONSTANT_STRING_TOO_LONG:
        print_lexing_error (&lexer, invalid_lexeme,
                            "string constant longer than 16 symbols (%d)",
                            (int) STRING_VIEW_LEN (invalid_lexeme->sv));
        break;
      case LEXEME_KIND_IDENTIFIER_TOO_LONG:
        print_lexing_error (&lexer, invalid_lexeme,
                            "identifier longer than 16 symbols (%d)",
                            (int) STRING_VIEW_LEN (invalid_lexeme->sv));
        break;
      case LEXEME_KIND_CONSTANT_NUMBER_INVALID:
        print_lexing_error (&lexer, invalid_lexeme, "invalid number constant `%.*s'",
                            (int) STRING_VIEW_LEN (invalid_lexeme->sv),
                            invalid_lexeme->sv.beg);
        break;
      case LEXEME_KIND_COMMENT_UNTERMINATED:
        invalid_lexeme->sv.end = invalid_lexeme->sv.beg + 2;
        print_lexing_error (&lexer, invalid_lexeme, "unterminated comment",
                            (int) STRING_VIEW_LEN (invalid_lexeme->sv),
                            invalid_lexeme->sv.beg);
        break;
      default: break;
    }
  }

  munmap (string, st.st_size);

  return EXIT_SUCCESS;
}