File tree Expand file tree Collapse file tree 3 files changed +132
-0
lines changed
Expand file tree Collapse file tree 3 files changed +132
-0
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1+ //
2+ // enter_leave.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+ S <- A+
17+ A <- 'A'
18+ )" );
19+
20+ parser[" A" ].enter = [](const Context & /* c*/ , const char * /* s*/ ,
21+ size_t /* n*/ , std::any & /* dt*/ ) {
22+ std::cout << " enter" << std::endl;
23+ };
24+
25+ parser[" A" ] = [](const SemanticValues & /* vs*/ , std::any & /* dt*/ ) {
26+ std::cout << " action!" << std::endl;
27+ };
28+
29+ parser[" A" ].leave = [](const Context & /* c*/ , const char * /* s*/ ,
30+ size_t /* n*/ , size_t /* matchlen*/ ,
31+ std::any & /* value*/ , std::any & /* dt*/ ) {
32+ std::cout << " leave" << std::endl;
33+ };
34+
35+ if (parser.parse (" A" )) { return 0 ; }
36+
37+ std::cout << " syntax error..." << std::endl;
38+ return -1 ;
39+ }
Original file line number Diff line number Diff line change 1+ //
2+ // sequence.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+ START <- SEQUENCE_A
17+ SEQUENCE_A <- SEQUENCE('A')
18+ SEQUENCE(X) <- X (',' X)*
19+ )" );
20+
21+ parser[" SEQUENCE_A" ] = [](const SemanticValues & /* vs*/ ) {
22+ std::cout << " SEQUENCE_A" << std::endl;
23+ };
24+
25+ if (parser.parse (" A,A" )) { return 0 ; }
26+
27+ std::cout << " syntax error..." << std::endl;
28+ return -1 ;
29+ }
You can’t perform that action at this time.
0 commit comments