16void usage(std::ostream &out,
const char *name)
18 out <<
"Performs lexicographic analysis of the input.\n"
19 <<
"USAGE:\n\t" << name <<
" <FILE>"
20 <<
"\n\t" << name <<
" -"
24int main(
int argc,
const char **argv)
27#define ADD(TYPE, VAR, INIT, SHORT, LONG, DESCR, CALLBACK)\
30 AP.add<TYPE>(SHORT, LONG, DESCR, CALLBACK);\
34 "prints this help message",
36 ADD(
bool, color,
false,
39 [&](
bool) { color =
true; });
44 usage(std::cout, argv[0]);
45 std::cout <<
"WHERE\n" << AP;
46 std::exit(EXIT_SUCCESS);
49 if (AP.
args().size() != 1) {
50 usage(std::cerr, argv[0]);
51 std::cerr <<
"WHERE\n" << AP;
52 std::exit(EXIT_FAILURE);
55 const char *filename = AP.
args()[0];
57 if (
streq(filename,
"-")) {
62 in =
new std::ifstream(filename, std::ios_base::in);
67 std::cerr <<
"Failed to open stdin: ";
69 std::cerr <<
"Failed to open the file '" << filename <<
"': ";
70 std::cerr << strerror(errno) << std::endl;
77 while (
auto tok = lexer.
next())
78 diag(tok.pos) << tok.text <<
' ' << tok.type << std::endl;
83 std::exit(diag.
num_errors() ? EXIT_FAILURE : EXIT_SUCCESS);
bool show_help
whether to show a help message
A parser for command line arguments.
void parse_args(int argc, const char **argv)
Parses the arguments from argv.
const std::vector< const char * > & args() const
Returns all positional arguments.
void usage(std::ostream &out, const char *name)
#define ADD(TYPE, VAR, INIT, SHORT, LONG, DESCR, CALLBACK)
bool streq(const char *first, const char *second)
unsigned num_errors() const
Returns the number of errors emitted since the last call to clear().
Token next()
Obtains the next token from the input stream.