diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2016-11-25 11:18:34 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2016-11-25 11:18:34 +0200 |
commit | 28f8338ded34f160e0083da9be4679bc778be7ca (patch) | |
tree | 7bd01311683d835f946c73d7d8220f552bae718f /unit-tests | |
parent | f32bb0aceb00cfa4bd04eea72f8fa2fe02b738b3 (diff) |
Distinguish token quoting type and completeness
Diffstat (limited to 'unit-tests')
-rw-r--r-- | unit-tests/buildfile | 2 | ||||
-rw-r--r-- | unit-tests/lexer/buildfile | 13 | ||||
-rw-r--r-- | unit-tests/lexer/comment.test (renamed from unit-tests/test/script/lexer/comment.test) | 7 | ||||
-rw-r--r-- | unit-tests/lexer/driver.cxx | 94 | ||||
-rw-r--r-- | unit-tests/lexer/quoting.test | 95 |
5 files changed, 206 insertions, 5 deletions
diff --git a/unit-tests/buildfile b/unit-tests/buildfile index 5d06ec7..f8cfb9d 100644 --- a/unit-tests/buildfile +++ b/unit-tests/buildfile @@ -2,6 +2,6 @@ # copyright : Copyright (c) 2014-2016 Code Synthesis Ltd # license : MIT; see accompanying LICENSE file -d = function/ test/script/ +d = function/ lexer/ test/script/ ./: $d include $d diff --git a/unit-tests/lexer/buildfile b/unit-tests/lexer/buildfile new file mode 100644 index 0000000..d9bd2df --- /dev/null +++ b/unit-tests/lexer/buildfile @@ -0,0 +1,13 @@ +# file : unit-tests/lexer/buildfile +# copyright : Copyright (c) 2014-2016 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +#@@ Temporary until we get utility library support. +# +import libs = libbutl%lib{butl} +src = token lexer diagnostics utility variable name b-options types-parsers + +exe{driver}: cxx{driver} ../../build2/cxx{$src} $libs \ +test{comment quoting} + +include ../../build2/ diff --git a/unit-tests/test/script/lexer/comment.test b/unit-tests/lexer/comment.test index 0092ed9..07d7ac5 100644 --- a/unit-tests/test/script/lexer/comment.test +++ b/unit-tests/lexer/comment.test @@ -1,7 +1,6 @@ -# @@ This one should be moved to build2/lexer since we use base lexer -# functionality as is. -# -test.arguments += script-line +# file : unit-tests/lexer/comment.test +# copyright : Copyright (c) 2014-2016 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file # Single-line comments. diff --git a/unit-tests/lexer/driver.cxx b/unit-tests/lexer/driver.cxx new file mode 100644 index 0000000..326ac8a --- /dev/null +++ b/unit-tests/lexer/driver.cxx @@ -0,0 +1,94 @@ +// file : unit-tests/lexer/driver.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include <cassert> +#include <iostream> + +#include <build2/types> +#include <build2/utility> + +#include <build2/token> +#include <build2/lexer> + +using namespace std; + +namespace build2 +{ + // Usage: argv[0] [-q] [<lexer-mode>] + // + int + main (int argc, char* argv[]) + { + bool quote (false); + lexer_mode m (lexer_mode::normal); + + for (int i (1); i != argc; ++i) + { + string a (argv[i]); + + if (a == "-q") + quote = true; + else + { + if (a == "normal") m = lexer_mode::normal; + else if (a == "variable") m = lexer_mode::variable; + else if (a == "value") m = lexer_mode::value; + else if (a == "attribute") m = lexer_mode::attribute; + else if (a == "eval") m = lexer_mode::eval; + else assert (false); + break; + } + } + + try + { + cin.exceptions (istream::failbit | istream::badbit); + + // Most alternative modes auto-expire so we need something underneath. + // + lexer l (cin, path ("stdin")); + + if (m != lexer_mode::normal) + l.mode (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); + + if (quote) + { + char q ('\0'); + switch (t.qtype) + { + case quote_type::single: q = 'S'; break; + case quote_type::double_: q = 'D'; break; + case quote_type::mixed: q = 'M'; break; + case quote_type::unquoted: break; + } + + if (q != '\0') + cout << " [" << q << (t.qcomp ? "/C" : "/P") << ']'; + } + + cout << endl; + } + } + catch (const failed&) + { + return 1; + } + + return 0; + } +} + +int +main (int argc, char* argv[]) +{ + return build2::main (argc, argv); +} diff --git a/unit-tests/lexer/quoting.test b/unit-tests/lexer/quoting.test new file mode 100644 index 0000000..76fd904 --- /dev/null +++ b/unit-tests/lexer/quoting.test @@ -0,0 +1,95 @@ +# file : unit-tests/lexer/quoting.test +# copyright : Copyright (c) 2014-2016 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +test.options += -q + +: unquoted +: +$* <'foo' >>EOO +'foo' +<newline> +EOO + +: single-comp +: +$* <":'foo':" >>EOO +: +'foo' [S/C] +: +<newline> +EOO + +: double-comp +: +$* <':"foo":' >>EOO +: +'foo' [D/C] +: +<newline> +EOO + +: single-empty-comp +: +$* <"''" >>EOO +'' [S/C] +<newline> +EOO + +: double-empty-comp +: +$* <'""' >>EOO +'' [D/C] +<newline> +EOO + +: part-start-quoted +: Token start already quoted +: +$* <'"$foo"' >>EOO +'' [D/P] +\$ +'foo' [D/P] +<newline> +EOO + +: part-end-quoted +: Token end still quoted +: +$* <'"foo$"' >>EOO +'foo' [D/P] +\$ +'' [D/P] +<newline> +EOO + +: part-start-unquoted +: Token starts with unquoted character +: +$* <'f"oo"' >>EOO +'foo' [D/P] +<newline> +EOO + +: part-unquoted +: Token continous with unquoted character +: +$* <'"fo"o' >>EOO +'foo' [D/P] +<newline> +EOO + +: part-unquoted-escape +: Token continous with unquoted escaped character +: +$* <'"fo"\"' >>EOO +'fo"' [D/P] +<newline> +EOO + +: mixed +: +$* <"\"fo\"'o'" >>EOO +'foo' [M/P] +<newline> +EOO |