]> git.armaanb.net Git - gen-shell.git/blob - src/libshared/src/PEG.h
added install instructions
[gen-shell.git] / src / libshared / src / PEG.h
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2015 - 2017, Paul Beckingham, Federico Hernandez.
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 #ifndef INCLUDED_PEG
28 #define INCLUDED_PEG
29
30 #include <FS.h>
31 #include <string>
32 #include <vector>
33 #include <map>
34 #include <set>
35
36 class PEG
37 {
38 public:
39   class Token
40   {
41   public:
42     Token (const std::string& value)           { _token = value; }
43     void tag (const std::string& tag)          { _tags.insert (tag); }
44     bool hasTag (const std::string& tag) const { return _tags.find (tag) != _tags.end (); };
45     std::string dump () const;
46
47     enum class Quantifier                      { one, zero_or_one, one_or_more, zero_or_more };
48     enum class Lookahead                       { none, positive, negative };
49
50     std::string _token                         {};
51     std::set <std::string> _tags               {};
52     Quantifier  _quantifier                    {Quantifier::one};
53     Lookahead   _lookahead                     {Lookahead::none};
54     // TODO Added Lexer::Type support, which allows the PEG to specify
55     //      "<Lexer::Type>" as a built-in type.
56   };
57
58   class Production : public std::vector <Token>
59   {
60   };
61
62   class Rule : public std::vector <Production>
63   {
64   };
65
66 public:
67   void loadFromFile (File&);
68   void loadFromString (const std::string&);
69   std::map <std::string, PEG::Rule> syntax () const;
70   std::string firstRule () const;
71   void debug ();
72   void strict (bool);
73   std::string dump () const;
74
75 private:
76   std::vector <std::string> loadImports (const std::vector <std::string>&);
77   void validate () const;
78
79 private:
80   //        rule name    rule
81   //        |            |
82   std::map <std::string, PEG::Rule> _rules     {};
83   std::string                       _start     {};
84   int                               _debug     {0};
85   bool                              _strict    {false};
86   std::vector <std::string>         _imports   {};
87 };
88
89 #endif