Skip to content

Commit 4e305b4

Browse files
committed
Added example/choice.cc
1 parent 195746d commit 4e305b4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

example/choice.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// choice.cc
3+
//
4+
// Copyright (c) 2023 Yuji Hirose. All rights reserved.
5+
// MIT License
6+
//
7+
8+
#include <cstdlib>
9+
#include <iostream>
10+
#include <peglib.h>
11+
12+
using namespace peg;
13+
14+
int main(void) {
15+
parser parser(R"(
16+
type <- 'string' / 'int' / 'double'
17+
%whitespace <- [ \t\r\n]*
18+
)");
19+
20+
parser["type"] = [](const SemanticValues &vs) {
21+
std::cout << vs.choice() << std::endl;
22+
};
23+
24+
if (parser.parse("int")) { return 0; }
25+
26+
std::cout << "syntax error..." << std::endl;
27+
return -1;
28+
}

0 commit comments

Comments
 (0)