aboutsummaryrefslogtreecommitdiff
path: root/src/bnf-parser.c
blob: 7aa26f0656bb0f76de3d15d13463ab4e8a1646f1 (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
#include <stdarg.h>
#include "bnf-parser.h"
#include "dynamic-array.h"

void
print_error (struct bnf_lexer *lexer, const char *point,
             const char *template, ...)
{
  int row = 1;
  int col = 0;

  for (const char *head = lexer->sv.beg; head < point; ++head) {
    col += 1;
    if (*head == '\n') {
      row += 1;
      col = 0;
    }
  };

  fprintf (stderr, "%s:%d:%d: error: ", lexer->filename, row, col);

  va_list ap;
  va_start (ap, template);
  vfprintf (stderr, template, ap);
  va_end (ap);

  putc ('\n', stderr);
}

bool
is_bnf_lexeme_valid (struct bnf_lexeme lexeme)
{
  return lexeme.kind != BNF_LEXEME_KIND_INVALID;
}

bool
bnf_parse_lexeme (struct bnf_lexer *lexer, enum bnf_lexeme_kind kind)
{
  const char *checkpoint = lexer->cursor;
  struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);

  if (lexeme.kind != kind) goto revert;

  return true;

revert:
  lexer->cursor = checkpoint;
  return false;
}

struct bnf_lexeme
bnf_parse_nonterminal (struct bnf_lexer *lexer)
{
  const char *checkpoint = lexer->cursor;
  struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);
  if (lexeme.kind == BNF_LEXEME_KIND_NONTERMINAL)
    return lexeme;

revert:
  lexer->cursor = checkpoint;
  return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID };
}

struct bnf_lexeme
bnf_parse_terminal (struct bnf_lexer *lexer)
{
  const char *checkpoint = lexer->cursor;

  struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);
  if (lexeme.kind == BNF_LEXEME_KIND_TERMINAL)
    return lexeme;

revert:
  lexer->cursor = checkpoint;
  return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID };
}

bool
is_bnf_sequence_valid (struct bnf_sequence sequence)
{
  return sequence.term_count != 0;
}

struct bnf_sequence
bnf_parse_sequence (struct bnf_lexer *lexer)
{
  struct bnf_sequence sequence = {0};
  const char *checkpoint = lexer->cursor;

  for (;;) {
    struct bnf_lexeme lexeme = bnf_parse_nonterminal (lexer);
    if (!is_bnf_lexeme_valid (lexeme))
      lexeme = bnf_parse_terminal (lexer);
    if (!is_bnf_lexeme_valid (lexeme))
      break;

    DYNAMIC_ARRAY_APPEND (sequence, lexeme);
  } if (!is_bnf_sequence_valid (sequence)) goto revert;

  if (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_ALTERNATIVE) ||
      bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE))
    return sequence;

revert:
  DYNAMIC_ARRAY_FREE (sequence);
  lexer->cursor = checkpoint;
  return (struct bnf_sequence) {0};
}

bool
is_bnf_rule_valid (struct bnf_rule rule)
{
  return rule.is_replaced_by.sequence_count != 0;
}

struct bnf_rule
bnf_parse_rule (struct bnf_lexer *lexer)
{
  struct bnf_rule rule = {0};
  const char *checkpoint = lexer->cursor;

  rule.name = bnf_parse_nonterminal (lexer);
  if (!is_bnf_lexeme_valid (rule.name))
    goto revert;

  if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_IS_REPLACED_BY))
    goto revert;

  for (;;) {
    struct bnf_sequence sequence = bnf_parse_sequence (lexer);
    if (!is_bnf_sequence_valid (sequence)) break;
    DYNAMIC_ARRAY_APPEND (rule.is_replaced_by, sequence);
  } if (is_bnf_rule_valid (rule)) return rule;

revert:
  DYNAMIC_ARRAY_FREE (rule.is_replaced_by);
  lexer->cursor = checkpoint;
  return (struct bnf_rule) {0};
}

struct bnf_syntax
bnf_parse_syntax (struct bnf_lexer *lexer)
{
  struct bnf_syntax syntax = {0};

  for (;;) {
    while (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE));
    struct bnf_rule rule = bnf_parse_rule (lexer);
    if (!is_bnf_rule_valid (rule)) {
      if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_FILE)) {
        print_error (lexer, lexer->cursor, "unable to parse rule");
      }
      break;
    }

    DYNAMIC_ARRAY_APPEND (syntax, rule);
  }

  return syntax;
}

bool
bnf_check_syntax (struct bnf_lexer *lexer, struct bnf_syntax syntax)
{
  bool no_errors = true;
  DYNAMIC_ARRAY_FOREACH (rule, syntax) {
    DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by) {
      DYNAMIC_ARRAY_FOREACH (term, *sequence) {
        if (term->kind != BNF_LEXEME_KIND_NONTERMINAL) continue;
        if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR ("<EOL>"))) continue;
        if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR ("<EOF>"))) continue;

        bool rule_found = false;
        DYNAMIC_ARRAY_FOREACH (rule, syntax) {
          if (!STRING_VIEW_EQUALS (rule->name.sv, term->sv)) continue;
          rule_found = true;
          break;
        }

        if (!rule_found) {
          no_errors = false;
          print_error (lexer, term->sv.beg,
                       "rule not found: %.*s",
                       (int) STRING_VIEW_LEN (term->sv), term->sv.beg);
        }
      }
    }
  }

  return no_errors;
}