From a1f459f8446370704695919b3131653300866ee9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 25 May 2017 10:41:20 +0200 Subject: Implement parsing of C++ module declarations --- unit-tests/cc/parser/buildfile | 17 +++++ unit-tests/cc/parser/driver.cxx | 69 ++++++++++++++++++ unit-tests/cc/parser/module.test | 147 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 unit-tests/cc/parser/buildfile create mode 100644 unit-tests/cc/parser/driver.cxx create mode 100644 unit-tests/cc/parser/module.test (limited to 'unit-tests/cc') diff --git a/unit-tests/cc/parser/buildfile b/unit-tests/cc/parser/buildfile new file mode 100644 index 0000000..5d20367 --- /dev/null +++ b/unit-tests/cc/parser/buildfile @@ -0,0 +1,17 @@ +# file : unit-tests/cc/parser/buildfile +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +#@@ Temporary until we get utility library support. +# +import libs = libbutl%lib{butl} +src = cc/{lexer parser} token lexer diagnostics utility variable name b-options types-parsers \ +context scope parser target operation rule prerequisite file module function \ +functions-builtin functions-path functions-process-path functions-string \ +functions-target-triplet algorithm search dump filesystem scheduler \ +config/{utility init operation module} spec + +exe{driver}: cxx{driver} ../../../build2/cxx{$src} ../../../build2/liba{b} \ +$libs test{*} + +include ../../../build2/ diff --git a/unit-tests/cc/parser/driver.cxx b/unit-tests/cc/parser/driver.cxx new file mode 100644 index 0000000..cdddaca --- /dev/null +++ b/unit-tests/cc/parser/driver.cxx @@ -0,0 +1,69 @@ +// file : unit-tests/cc/parser/driver.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include +#include + +#include +#include + +#include + +using namespace std; + +namespace build2 +{ + namespace cc + { + // Usage: argv[0] [] + // + int + main (int argc, char* argv[]) + { + try + { + istream* is; + const char* in; + + // Reading from file is several times faster. + // + ifdstream ifs; + if (argc > 1) + { + in = argv[1]; + ifs.open (in); + is = &ifs; + } + else + { + in = "stdin"; + cin.exceptions (istream::failbit | istream::badbit); + is = &cin; + } + + parser p; + translation_unit u (p.parse (*is, path (in))); + + for (const string& n: u.module_imports) + cout << "import " << n << ';' << endl; + + if (!u.module_name.empty ()) + cout << (u.module_interface ? "export " : "") + << "module " << u.module_name << ';' << endl; + } + catch (const failed&) + { + return 1; + } + + return 0; + } + } +} + +int +main (int argc, char* argv[]) +{ + return build2::cc::main (argc, argv); +} diff --git a/unit-tests/cc/parser/module.test b/unit-tests/cc/parser/module.test new file mode 100644 index 0000000..f85c969 --- /dev/null +++ b/unit-tests/cc/parser/module.test @@ -0,0 +1,147 @@ +# file : unit-tests/cc/parser/module.test +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +# Test C++ module constructs. +# + +: import +: +$* <>EOI +import foo; +import foo.bar; +import foo.bar.baz; +EOI + +: module-implementation +: +$* <>EOI +module foo; +EOI + +: module-interface +: +$* <>EOI +export module foo; +EOI + +: export-imported +: +$* <>EOO +export import foo; +EOI +import foo; +EOO + +: export-imported-block +: +$* <>EOO +export {import foo;} + +export +{ + namespace foo + { + class c {}; + } + + template int f (); + + import bar; +} +EOI +import foo; +import bar; +EOO + +: non-module +: +$* <>EOO +import foo [[export({import})]]; +module bar [[module({module})]]; +EOI +import foo; +module bar; +EOO + +: import-duplicate +: +$* <>EOO +import foo; +import bar.baz; +import foo; +import bar . baz; +EOI +import foo; +import bar.baz; +EOO + +: brace-missing +: +$* <>EOE +export +{ + class foo + { + //}; + module foo; +} +EOI +stdin:8:1: warning: {}-imbalance detected +EOE + +: brace-stray +: +$* <>EOE +export +{ + class foo + { + };} +} +module foo; +EOI +stdin:6:1: warning: {}-imbalance detected +EOE + +: import-missing-name +: +$* <>EOE != 0 +import ; +EOI +stdin:1:8: error: module name expected instead of ';' +EOE + +: module-missing-name +: +$* <>EOE != 0 +module ; +EOI +stdin:1:8: error: module name expected instead of ';' +EOE + +: import-missing-semi +: +$* <>EOE != 0 +import foo +EOI +stdin:2:1: error: ';' expected instead of +EOE + +: module-missing-semi +: +$* <>EOE != 0 +export module foo +EOI +stdin:2:1: error: ';' expected instead of +EOE -- cgit v1.1