Skip to content

Commit 49e2b6b

Browse files
support for c++17 style namespace declaration
1 parent c8b2e1b commit 49e2b6b

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ set(TEST_SNIPPET_EMBEDDED_TESTS
125125
${CMAKE_CURRENT_LIST_DIR}/test/unit/expr-tests.cpp
126126
${CMAKE_CURRENT_LIST_DIR}/test/unit/attribute-specifier-sequence.cpp
127127
${CMAKE_CURRENT_LIST_DIR}/test/unit/error-handler-test.cpp
128+
${CMAKE_CURRENT_LIST_DIR}/test/unit/namespace-test.cpp
128129
)
129130

130131
add_executable(cppparserunittest

src/parser.y

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ extern int yylex();
282282

283283
%type <str> strlit
284284
%type <str> optapidecor apidecor apidecortokensq
285-
%type <str> identifier numbertype typeidentifier varidentifier optname id name operfuncname funcname
285+
%type <str> identifier optidentifier numbertype typeidentifier varidentifier optname id name operfuncname funcname
286286
%type <str> templidentifier templqualifiedid
287287
%type <str> doccommentstr
288288
%type <str> rshift
@@ -727,6 +727,9 @@ id : tknID [ZZLOG; $$ = $1; ] {}
727727

728728
optname : [ZZLOG;] { $$ = makeCppToken(nullptr, nullptr); }
729729
| name [ZZLOG;] { $$ = $1; }
730+
731+
optidentifier : [ZZLOG;] { $$ = makeCppToken(nullptr, nullptr); }
732+
| identifier [ZZLOG;] { $$ = $1; }
730733
;
731734

732735
enumitem : name [ZZLOG;] { $$ = new CppEnumItem($1); }
@@ -1652,7 +1655,7 @@ classdefn : classspecifier optapidecor optattribspecifiers identifier op
16521655
}
16531656
;
16541657

1655-
namespacedefn : tknNamespace optname '{'
1658+
namespacedefn : tknNamespace optidentifier '{'
16561659
[
16571660
ZZVALID;
16581661
gCompoundStack.push(classNameFromIdentifier($2));

test/unit/namespace-test.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <catch/catch.hpp>
2+
3+
#include "cppparser.h"
4+
5+
#include "embedded-snippet-test-base.h"
6+
7+
class NamespaceTest : public EmbeddedSnippetTestBase
8+
{
9+
protected:
10+
NamespaceTest()
11+
: EmbeddedSnippetTestBase(__FILE__)
12+
{
13+
}
14+
};
15+
16+
#if TEST_CASE_SNIPPET_STARTS_FROM_NEXT_LINE
17+
# if __cplusplus > 201703L
18+
namespace my::ns1 {
19+
auto p = new char*[5];
20+
}
21+
# endif
22+
#endif
23+
24+
TEST_CASE_METHOD(NamespaceTest, " C++17 style nested namespace")
25+
{
26+
auto testSnippet = getTestSnippetParseStream(__LINE__ - 5);
27+
28+
CppParser parser;
29+
const auto ast = parser.parseStream(testSnippet.data(), testSnippet.size());
30+
REQUIRE(ast != nullptr);
31+
32+
const auto& members = ast->members();
33+
REQUIRE(members.size() == 3);
34+
35+
CppCompoundEPtr ns = members[1];
36+
REQUIRE(ns);
37+
CHECK(ns->name() == "my::ns1");
38+
const auto& nsMembers = ns->members();
39+
REQUIRE(nsMembers.size() == 1);
40+
41+
CppVarEPtr var = nsMembers[0];
42+
REQUIRE(var);
43+
44+
CHECK(var->assignValue() != nullptr);
45+
}

0 commit comments

Comments
 (0)