mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
parse.cpp
Go to the documentation of this file.
1#include "lex/Lexer.hpp"
3#include "parse/Parser.hpp"
4#include <cerrno>
5#include <cstdlib>
6#include <cstring>
7#include <fstream>
8#include <iostream>
11#include <mutable/util/fn.hpp>
12
13
14using namespace m;
15using namespace m::ast;
16
17
18void usage(std::ostream &out, const char *name)
19{
20 out << "Performs grammatical analysis of the input.\n"
21 << "USAGE:\n\t" << name << " <FILE>"
22 << "\n\t" << name << " -"
23 << std::endl;
24}
25
26int main(int argc, const char **argv)
27{
28 ArgParser AP;
29#define ADD(TYPE, VAR, INIT, SHORT, LONG, DESCR, CALLBACK)\
30 TYPE VAR = INIT;\
31 {\
32 AP.add<TYPE>(SHORT, LONG, DESCR, CALLBACK);\
33 }
34 ADD(bool, show_help, false, /* Type, Var, Init */
35 "-h", "--help", /* Short, Long */
36 "prints this help message", /* Description */
37 [&](bool) { show_help = true; }); /* Callback */
38 ADD(bool, color, false, /* Type, Var, Init */
39 nullptr, "--color", /* Short, Long */
40 "use colors", /* Description */
41 [&](bool) { color = true; }); /* Callback */
42 ADD(bool, ast, false, /* Type, Var, Init */
43 nullptr, "--ast", /* Short, Long */
44 "dump the abstract syntax tree", /* Description */
45 [&](bool) { ast = true; }); /* Callback */
46#undef ADD
47 AP.parse_args(argc, argv);
48
49 if (show_help) {
50 usage(std::cout, argv[0]);
51 std::cout << "WHERE\n" << AP;
52 std::exit(EXIT_SUCCESS);
53 }
54
55 if (AP.args().size() != 1) {
56 usage(std::cerr, argv[0]);
57 std::cerr << "WHERE\n" << AP;
58 std::exit(EXIT_FAILURE);
59 }
60
61 const char *filename = AP.args()[0];
62 std::istream *in;
63 if (streq(filename, "-")) {
64 /* read from stdin */
65 in = &std::cin;
66 } else {
67 /* read from file */
68 in = new std::ifstream(filename, std::ios_base::in);
69 }
70
71 if (in->fail()) {
72 if (in == &std::cin)
73 std::cerr << "Failed to open stdin: ";
74 else
75 std::cerr << "Failed to open the file '" << filename << "': ";
76 std::cerr << strerror(errno) << std::endl;
77 }
78
79 Diagnostic diag(color, std::cout, std::cerr);
81 Lexer lexer(diag, pool, filename, *in);
82 Parser parser(lexer);
83 ASTPrinter printer(std::cout);
84
85 while (parser.token()) {
86 auto cmd = parser.parse();
87 if (ast) {
88 cmd->dump(std::cout);
89 } else {
90 printer(*cmd);
91 std::cout << std::endl;
92 }
93 }
94
95 if (in != &std::cin)
96 delete in;
97
98 std::exit(diag.num_errors() ? EXIT_FAILURE : EXIT_SUCCESS);
99}
int main(void)
bool show_help
‍whether to show a help message
A parser for command line arguments.
Definition: ArgParser.hpp:20
void parse_args(int argc, const char **argv)
Parses the arguments from argv.
Definition: ArgParser.cpp:186
const std::vector< const char * > & args() const
Returns all positional arguments.
Definition: ArgParser.hpp:143
‍mutable namespace
Definition: Backend.hpp:10
bool streq(const char *first, const char *second)
Definition: fn.hpp:29
void usage(std::ostream &out, const char *name)
Definition: parse.cpp:18
#define ADD(TYPE, VAR, INIT, SHORT, LONG, DESCR, CALLBACK)
unsigned num_errors() const
Returns the number of errors emitted since the last call to clear().
Definition: Diagnostic.hpp:48
Pretty-prints the AST in SQL.
Definition: ASTPrinter.hpp:10
const Token & token()
Definition: Parser.hpp:38
std::unique_ptr< Command > parse()
Definition: Parser.cpp:98