aboutsummaryrefslogtreecommitdiff
path: root/src/bnf-lexer.c
blob: f859af90e38d23fd619ad10bd56c711b3e96729c (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
#include "bnf-lexer.h"
#include <regex.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "dynamic-array.h"

#define X(SYM, REG, ...) [BNF_LEXEME_KIND_##SYM] = "^(" REG ")",
static const char *lexeme_regex_strings[BNF_LEXEME_REGEX_COUNT] = { BNF_LEXEME_KINDS () };
#undef X

static regex_t lexeme_regexes[BNF_LEXEME_REGEX_COUNT];

static void
print_regex_error (const regex_t *comp, const char *regex, int errcode)
{
  size_t length = regerror (errcode, comp, NULL, 0);
  char *buffer = malloc (length);
  regerror (errcode, comp, buffer, length);
  fprintf (stderr, "error: regcomp (\"%s\"): %s\n",
           regex, buffer);
  free (buffer);
}

static void __attribute__((constructor))
compile_regexes (void)
{
  for (enum bnf_lexeme_kind kind = 0; kind < BNF_LEXEME_REGEX_COUNT; ++kind) {
    regex_t *compiled = &lexeme_regexes[kind];
    const char *pattern = lexeme_regex_strings[kind];
    int errcode = regcomp (compiled, pattern, REG_EXTENDED);
    if (errcode) {
      print_regex_error (compiled, pattern, errcode);
      exit (1);
    }
  }
}

struct bnf_lexer
bnf_lexer_from_string_view (struct string_view sv, const char *filename)
{
  return (struct bnf_lexer) {
    .sv = sv,
    .cursor = sv.beg,
    .filename = filename,
  };
}

static void
lexer_skip_whitespace (struct bnf_lexer *lexer)
{
  while (lexer->cursor < lexer->sv.end && (*lexer->cursor == ' ' ||
                                           *lexer->cursor == '\t'))
    lexer->cursor += 1;
}

struct bnf_lexeme
bnf_lexer_next_lexeme (struct bnf_lexer *lexer)
{
  lexer_skip_whitespace (lexer);
  struct bnf_lexeme lexeme = {
    .sv.beg = lexer->cursor,
    .kind = BNF_LEXEME_KIND_INVALID,
  };

  if (lexer->cursor >= lexer->sv.end) {
      lexeme.kind = BNF_LEXEME_KIND_END_OF_FILE;
      lexeme.sv.end = lexer->sv.end;
      return lexeme;
  }

  if (*lexer->cursor == '\n') {
      lexeme.kind = BNF_LEXEME_KIND_END_OF_LINE;
      lexeme.sv.end = ++lexer->cursor;
      return lexeme;
  }

  regmatch_t regmatch;
  enum bnf_lexeme_kind longest_matching_lexeme_kind = BNF_LEXEME_KIND_INVALID;
  int longest_matching_lexeme_kind_length = 0;
  for (enum bnf_lexeme_kind kind = 0; kind < BNF_LEXEME_REGEX_COUNT; ++kind) {
    if (regexec (&lexeme_regexes[kind], lexer->cursor, 1, &regmatch, 0)) continue;
    if (regmatch.rm_eo <= longest_matching_lexeme_kind_length) continue;
    longest_matching_lexeme_kind_length = regmatch.rm_eo;
    longest_matching_lexeme_kind = kind;
  }

  if (longest_matching_lexeme_kind != BNF_LEXEME_KIND_INVALID) {
    lexeme.kind = longest_matching_lexeme_kind;
    lexeme.sv.end = lexer->cursor += longest_matching_lexeme_kind_length;

    switch (lexeme.kind) {
      case BNF_LEXEME_KIND_COMMENT: return bnf_lexer_next_lexeme (lexer);
      default: return lexeme;
    }
  }

  lexeme.sv.end = ++lexer->cursor;
  return lexeme;
}