diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2020-05-07 22:30:17 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2020-05-17 17:48:32 +0300 |
commit | e5827facbfbfe90eae1b71c355a08bf61e2f6e1a (patch) | |
tree | bae45ffd2dedcf8dcf2f67fda91878d17d9233a4 /libbuild2/script/lexer.test.cxx | |
parent | db47f3b176421d4325c3439ae54a3cabf953ae7b (diff) |
Factor out generic script parsing/executing functionality from build2::test::script namespace
Diffstat (limited to 'libbuild2/script/lexer.test.cxx')
-rw-r--r-- | libbuild2/script/lexer.test.cxx | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/libbuild2/script/lexer.test.cxx b/libbuild2/script/lexer.test.cxx new file mode 100644 index 0000000..85304ea --- /dev/null +++ b/libbuild2/script/lexer.test.cxx @@ -0,0 +1,66 @@ +// file : libbuild2/script/lexer.test.cxx -*- C++ -*- +// license : MIT; see accompanying LICENSE file + +#include <cassert> +#include <iostream> + +#include <libbuild2/types.hxx> +#include <libbuild2/utility.hxx> + +#include <libbuild2/script/token.hxx> +#include <libbuild2/script/lexer.hxx> + +using namespace std; + +namespace build2 +{ + namespace script + { + // Usage: argv[0] <lexer-mode> + // + int + main (int argc, char* argv[]) + { + lexer_mode m; + { + assert (argc == 2); + string s (argv[1]); + + if (s == "command-expansion") m = lexer_mode::command_expansion; + else if (s == "here-line-single") m = lexer_mode::here_line_single; + else if (s == "here-line-double") m = lexer_mode::here_line_double; + else assert (false); + } + + try + { + cin.exceptions (istream::failbit | istream::badbit); + + path_name in ("<stdin>"); + lexer l (cin, in, m); + + // No use printing eos since we will either get it or loop forever. + // + for (token t (l.next ()); t.type != token_type::eos; t = l.next ()) + { + // Print each token on a separate line without quoting operators. + // + t.printer (cout, t, false); + cout << endl; + } + } + catch (const failed&) + { + return 1; + } + + return 0; + } + } +} + +int +main (int argc, char* argv[]) +{ + return build2::script::main (argc, argv); +} |