Skip to content

Commit 666b364

Browse files
authored
Merge pull request yhirose#72 from romeoxbm/master
Replace peg::any with std::any
2 parents 523137c + 1afa498 commit 666b364

File tree

7 files changed

+189
-183
lines changed

7 files changed

+189
-183
lines changed

CMakeLists.txt

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
1-
cmake_minimum_required(VERSION 2.8)
2-
3-
# Check if a supported compiler is used and add c++11 flag:
4-
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
5-
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
6-
message(FATAL_ERROR "Need at least gcc 4.9 to compile.")
7-
endif()
8-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
9-
elseif(MSVC)
10-
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19)
11-
message(FATAL_ERROR "Visual Studio 2015 or newer is required.")
12-
endif()
13-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
14-
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
15-
message(FATAL_ERROR "Need at least AppleClang 7.0 to compile.")
1+
cmake_minimum_required(VERSION 3.1.0)
2+
project("cpp-peglib")
3+
4+
# Check if a supported compiler is used to setup the C++ standard to use:
5+
get_property(known_features GLOBAL PROPERTY CMAKE_CXX_KNOWN_FEATURES)
6+
list(FIND known_features "cxx_std_17" found)
7+
if(NOT ${found} EQUAL -1)
8+
# C++17 standard is supported
9+
set(CMAKE_CXX_STANDARD 17)
10+
else()
11+
# Check for C++11 standard support
12+
list(FIND known_features "cxx_std_11" found)
13+
if(NOT ${found} EQUAL -1)
14+
# C++11 standard is supported
15+
set(CMAKE_CXX_STANDARD 11)
1616
endif()
17-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
18-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
19-
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.4)
20-
message(FATAL_ERROR "Clang below version 3.4 will most likely not work. Please upgrade your compiler.")
21-
endif()
22-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
23-
else() # no GNU, no MSVC, no Clang
24-
message(WARNING "You are using an unsupported compiler. Compilation has only been tested with MSVC, GCC and Clang.")
17+
endif()
2518

26-
include(CheckCXXCompilerFlag)
27-
check_cxx_compiler_flag(-std=c++11 HAS_CXX11_FLAG)
28-
if(HAS_CXX11_FLAG)
29-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
30-
else()
31-
message(FATAL_ERROR "Your compiler doesn't support the '-std=c++11' flag.")
32-
endif()
19+
if(${found} EQUAL -1)
20+
message(FATAL_ERROR "Your compiler is not supported.")
3321
endif()
3422

23+
set(CMAKE_CXX_EXTENSIONS OFF)
3524

3625
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
3726
OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@ There are four semantic actions available:
110110

111111
`any& dt` is a 'read-write' context data which can be used for whatever purposes. The initial context data is set in `peg::parser::parse` method.
112112

113-
`peg::any` is a simpler implementatin of [boost::any](http://www.boost.org/doc/libs/1_57_0/doc/html/any.html). It can wrap arbitrary data type.
113+
`peg::any` is a simpler implementatin of [boost::any](http://www.boost.org/doc/libs/1_57_0/doc/html/any.html). It can wrap arbitrary data type.
114+
If the compiler in use supports C++17, by default `peg::any` is defined as an alias to `std::any`.
115+
To force using the simpler `any` implementation that comes with `cpp-peglib`, define `PEGLIB_USE_STD_ANY` as 0 before including `peglib.h`:
116+
```cpp
117+
#define PEGLIB_USE_STD_ANY 0
118+
#include <peglib.h>
119+
[...]
120+
```
114121
115122
A semantic action can return a value of arbitrary data type, which will be wrapped by `peg::any`. If a user returns nothing in a semantic action, the first semantic value in the `const SemanticValues& sv` argument will be returned. (Yacc parser has the same behavior.)
116123

example/calc.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@
1010
#include <cstdlib>
1111

1212
using namespace peg;
13-
using namespace std;
1413

1514
int main(int argc, const char** argv)
1615
{
17-
if (argc < 2 || string("--help") == argv[1]) {
18-
cout << "usage: calc [formula]" << endl;
16+
if (argc < 2 || std::string("--help") == argv[1]) {
17+
std::cout << "usage: calc [formula]" << std::endl;
1918
return 1;
2019
}
2120

2221
auto reduce = [](const SemanticValues& sv) -> long {
23-
auto result = sv[0].get<long>();
22+
auto result = any_cast<long>(sv[0]);
2423
for (auto i = 1u; i < sv.size(); i += 2) {
25-
auto num = sv[i + 1].get<long>();
26-
auto ope = sv[i].get<char>();
24+
auto num = any_cast<long>(sv[i + 1]);
25+
auto ope = any_cast<char>(sv[i]);
2726
switch (ope) {
2827
case '+': result += num; break;
2928
case '-': result -= num; break;
@@ -53,11 +52,11 @@ int main(int argc, const char** argv)
5352
auto expr = argv[1];
5453
long val = 0;
5554
if (parser.parse(expr, val)) {
56-
cout << expr << " = " << val << endl;
55+
std::cout << expr << " = " << val << std::endl;
5756
return 0;
5857
}
5958

60-
cout << "syntax error..." << endl;
59+
std::cout << "syntax error..." << std::endl;
6160

6261
return -1;
6362
}

example/calc2.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <cstdlib>
1111

1212
using namespace peg;
13-
using namespace std;
1413

1514
//
1615
// PEG syntax:
@@ -24,16 +23,16 @@ using namespace std;
2423
//
2524
int main(int argc, const char** argv)
2625
{
27-
if (argc < 2 || string("--help") == argv[1]) {
28-
cout << "usage: calc [formula]" << endl;
26+
if (argc < 2 || std::string("--help") == argv[1]) {
27+
std::cout << "usage: calc [formula]" << std::endl;
2928
return 1;
3029
}
3130

3231
auto reduce = [](const SemanticValues& sv) -> long {
33-
auto result = sv[0].get<long>();
32+
auto result = any_cast<long>(sv[0]);
3433
for (auto i = 1u; i < sv.size(); i += 2) {
35-
auto num = sv[i + 1].get<long>();
36-
auto ope = sv[i].get<char>();
34+
auto num = any_cast<long>(sv[i + 1]);
35+
auto ope = any_cast<char>(sv[i]);
3736
switch (ope) {
3837
case '+': result += num; break;
3938
case '-': result -= num; break;
@@ -56,7 +55,7 @@ int main(int argc, const char** argv)
5655
auto expr = argv[1];
5756
long val = 0;
5857
if (EXPRESSION.parse_and_get_value(expr, val).ret) {
59-
cout << expr << " = " << val << endl;
58+
std::cout << expr << " = " << val << std::endl;
6059
return 0;
6160
}
6261

example/calc3.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
#include <cstdlib>
1111

1212
using namespace peg;
13-
using namespace std;
1413

1514
int main(int argc, const char** argv)
1615
{
17-
if (argc < 2 || string("--help") == argv[1]) {
18-
cout << "usage: calc3 [formula]" << endl;
16+
if (argc < 2 || std::string("--help") == argv[1]) {
17+
std::cout << "usage: calc3 [formula]" << std::endl;
1918
return 1;
2019
}
2120

22-
function<long (const Ast&)> eval = [&](const Ast& ast) {
21+
std::function<long (const Ast&)> eval = [&](const Ast& ast) {
2322
if (ast.name == "NUMBER") {
2423
return stol(ast.token);
2524
} else {
@@ -54,15 +53,15 @@ int main(int argc, const char** argv)
5453
parser.enable_ast();
5554

5655
auto expr = argv[1];
57-
shared_ptr<Ast> ast;
56+
std::shared_ptr<Ast> ast;
5857
if (parser.parse(expr, ast)) {
5958
ast = AstOptimizer(true).optimize(ast);
60-
cout << ast_to_s(ast);
61-
cout << expr << " = " << eval(*ast) << endl;
59+
std::cout << ast_to_s(ast);
60+
std::cout << expr << " = " << eval(*ast) << std::endl;
6261
return 0;
6362
}
6463

65-
cout << "syntax error..." << endl;
64+
std::cout << "syntax error..." << std::endl;
6665

6766
return -1;
6867
}

0 commit comments

Comments
 (0)