1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2010 - 2021, Göteborg Bit Factory.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26 
27 #include <cmake.h>
28 #include <PEG.h>
29 #include <Packrat.h>
30 #include <test.h>
31 
32 ////////////////////////////////////////////////////////////////////////////////
main(int,char **)33 int main (int, char**)
34 {
35   UnitTest t (13);
36 
37   // Grammar that is valid.
38   PEG peg;
39   peg.loadFromString ("thing: item*\nitem: 'a'");
40   t.is (peg.firstRule (), "thing",                                                "question: firstRule found");
41 
42   auto rules = peg.syntax ();
43   t.is (rules["thing"][0][0]._token,  "item",                                     "question: thing: item");
44   t.ok (rules["thing"][0][0]._quantifier == PEG::Token::Quantifier::zero_or_more, "question: thing: item quantifier zero_or_more");
45   t.ok (rules["thing"][0][0]._lookahead == PEG::Token::Lookahead::none,           "question: thing: item lookahead none");
46   t.ok (rules["thing"][0][0]._tags == std::set <std::string> {},                  "question: thing: item tags {}");
47 
48   t.is (rules["item"][0][0]._token,  "'a'",                                       "question: item: 'a'");
49   t.ok (rules["item"][0][0]._quantifier == PEG::Token::Quantifier::one,           "question: item: 'a' quantifier one");
50   t.ok (rules["item"][0][0]._lookahead == PEG::Token::Lookahead::none,            "question: item: 'a' lookahead none");
51   t.ok (rules["item"][0][0]._tags == std::set <std::string> {"character", "literal"},
52                                                                                   "question: item: 'a' tags {'character', 'literal'}");
53 
54   // '' is valid.
55   try
56   {
57     Packrat rat;
58     rat.parse (peg, "");
59     t.pass ("question: '' valid");
60   }
61   catch (const std::string& e) { t.fail ("question: '' " + e); }
62 
63   // 'a' is valid.
64   try
65   {
66     Packrat rat;
67     rat.parse (peg, "a");
68     t.pass ("question: 'a' valid");
69   }
70   catch (const std::string& e) { t.fail ("question: 'a' " + e); }
71 
72   // 'aa' is valid.
73   try
74   {
75     Packrat rat;
76     rat.parse (peg, "aa");
77     t.pass ("question: 'aa' valid");
78   }
79   catch (const std::string& e) { t.fail ("question: 'aa' " + e); }
80 
81   // 'ab' is not valid.
82   try
83   {
84     Packrat rat;
85     rat.parse (peg, "ab");  // Expected to fail.
86     t.fail ("question: 'ab' not valid");
87   }
88   catch (const std::string& e) { t.pass ("question: 'ab' " + e); }
89 
90   return 0;
91 }
92 
93 ////////////////////////////////////////////////////////////////////////////////
94