summaryrefslogtreecommitdiff
path: root/src/gen_syntax.cc
blob: f455eb781b847e3d3dbcfacbaf7c015b2b8e8cef (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
#include "args.hh"
#include "errors.hh"
#include "grammar.hh"
#include "io.hh"
#include "prefix_tree.hh"

#include <algorithm>
#include <cassert>
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include <iostream>

namespace {

enum class CharacterClass : uint8_t {
  kIdentifier = 0,
  kLiteral = 1,
};

std::vector<std::string> const kCharacterClassNames(
    {"Identifier", "Literal"});

std::string make_define(std::string_view filename) {
  std::string ret;
  ret.reserve(filename.size());
  for (char c : filename) {
    if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') {
      ret.push_back(c);
    } else if (c >= 'a' && c <= 'z') {
      ret.push_back(static_cast<char>(c & ~0x20));
    } else {
      ret.push_back('_');
    }
  }
  return ret;
}

class Generator {
 public:
  bool generate(std::string_view header_name, std::string_view source_name,
                std::string const& ns, grammar::Grammar& grammar);
};

bool Generator::generate(std::string_view header_name,
                         std::string_view source_name, std::string const& ns,
                         grammar::Grammar& grammar) {
  std::fstream header{std::string(header_name),
                      std::fstream::trunc | std::fstream::out};
  std::fstream source{std::string(source_name),
                      std::fstream::trunc | std::fstream::out};

  auto header_guard = make_define(header_name);

  header << "#ifndef " << header_guard << "\n"
         << "#define " << header_guard << "\n"
         << "\n"
         << "namespace " << ns << " {\n"
         << "\n";


  header << "\n"
         << "}  // namespace " << ns << "\n"
         << "\n"
         << "#endif  // " << header_guard << "\n";

  source << "#include \"" << header_name << "\"\n"
         << "\n"
         << "namespace " << ns << " {\n"
         << "\n";


  source << "\n"
         << "}  // namespace " << ns << "\n";

  return true;
}

}  // namespace

int main(int argc, char** argv) {
  auto args = Args::create();
  auto opt_help = args->option('h', "help", "display this text and exit");
  auto opt_ns = args->option_argument('\0', "namespace", "ARG",
                                      "Namespace for syntax reader");
  std::vector<std::string_view> arguments;
  if (!args->run(argc, argv, &arguments)) {
    args->print_error(std::cerr);
    std::cerr << "Try `gen_syntax --help` for usage\n";
    return 1;
  }
  if (opt_help->is_set()) {
    std::cout << "Usage: `gen_syntax [OPTIONS...] syntax.grammar"
              << " OUTPUT.hh OUTPUT.cc`\n"
              << "Generates a syntax reader for grammar.\n"
              << "\n";
    args->print_help(std::cout);
    return 0;
  }
  if (!opt_ns->is_set()) {
    std::cerr << "No namespace given.\n"
              << "Try `gen_syntax --help` for usage\n";
    return 1;
  }
  auto ns = opt_ns->argument();
  if (arguments.size() != 3) {
    std::cerr << "Expecting three arguments. No more, no less.\n"
              << "Try `gen_syntax --help` for usage\n";
    return 1;
  }

  auto filename = std::string(arguments[0]);
  auto reader = io::open(filename);
  if (!reader.has_value()) {
    std::cerr << "Unable to open " << filename << '\n';
    return 1;
  }
  auto errors = src::file_errors(std::move(filename));
  auto grammar =
      grammar::load(std::move(reader.value()), kCharacterClassNames, *errors);
  if (!grammar || errors->errors() > 0)
    return 1;

  Generator generator;
  if (!generator.generate(arguments[1], arguments[2], ns, *grammar))
    return 1;
  return 0;
}