diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2020-05-07 22:30:17 +0300 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2020-05-27 08:35:29 +0200 |
commit | d85461c4280e292994412056216daf5f82fdd6e9 (patch) | |
tree | 16115d669f55b0bcce00425d7f81c970b774b6c5 /libbuild2/script/lexer.test.cxx | |
parent | 98db2c71fa8175c1680ab2c6cca07c4c00853047 (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); +} |