|
| 1 | +// Copyright (c) 2021 Daniel Deptford |
| 2 | +// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/ |
| 3 | + |
| 4 | +#ifndef TAO_PEGTL_CONTRIB_PEG_HPP |
| 5 | +#define TAO_PEGTL_CONTRIB_PEG_HPP |
| 6 | + |
| 7 | +#include <tao/pegtl.hpp> |
| 8 | + |
| 9 | +namespace TAO_PEGTL_NAMESPACE::peg |
| 10 | +{ |
| 11 | + // PEG grammar from https://pdos.csail.mit.edu/~baford/packrat/popl04/peg-popl04.pdf |
| 12 | + |
| 13 | + // clang-format off |
| 14 | + struct AND; |
| 15 | + struct Char; |
| 16 | + struct Class; |
| 17 | + struct CLOSE; |
| 18 | + struct Comment; |
| 19 | + struct Definition; |
| 20 | + struct DOT; |
| 21 | + struct EndOfFile; |
| 22 | + struct EndOfLine; |
| 23 | + struct Expression; |
| 24 | + struct QUESTION; |
| 25 | + struct IdentCont; |
| 26 | + struct Identifier; |
| 27 | + struct IdentStart; |
| 28 | + struct LEFTARROW; |
| 29 | + struct Literal; |
| 30 | + struct NOT; |
| 31 | + struct OPEN; |
| 32 | + struct PLUS; |
| 33 | + struct Prefix; |
| 34 | + struct Primary; |
| 35 | + struct Range; |
| 36 | + struct Sequence; |
| 37 | + struct SLASH; |
| 38 | + struct Space; |
| 39 | + struct Spacing; |
| 40 | + struct STAR; |
| 41 | + struct Suffix; |
| 42 | + |
| 43 | + struct Grammar : tao::pegtl::seq<Spacing, tao::pegtl::plus<Definition>, EndOfFile> {}; |
| 44 | + struct Definition : tao::pegtl::seq<Identifier, LEFTARROW, Expression> {}; |
| 45 | + struct Expression : tao::pegtl::seq< |
| 46 | + Sequence, |
| 47 | + tao::pegtl::star< |
| 48 | + tao::pegtl::seq< |
| 49 | + SLASH, |
| 50 | + Sequence |
| 51 | + > |
| 52 | + > |
| 53 | + > {}; |
| 54 | + struct Sequence : tao::pegtl::star<Prefix> {}; |
| 55 | + struct Prefix : tao::pegtl::seq< |
| 56 | + tao::pegtl::opt< |
| 57 | + tao::pegtl::sor< |
| 58 | + AND, |
| 59 | + NOT |
| 60 | + > |
| 61 | + >, |
| 62 | + Suffix |
| 63 | + > {}; |
| 64 | + |
| 65 | + struct Suffix : tao::pegtl::seq< |
| 66 | + Primary, |
| 67 | + tao::pegtl::opt< |
| 68 | + tao::pegtl::sor< |
| 69 | + QUESTION, |
| 70 | + STAR, |
| 71 | + PLUS |
| 72 | + > |
| 73 | + > |
| 74 | + > {}; |
| 75 | + |
| 76 | + struct Primary : tao::pegtl::sor< |
| 77 | + tao::pegtl::seq<Identifier, tao::pegtl::not_at<LEFTARROW> >, |
| 78 | + tao::pegtl::seq<OPEN, Expression, CLOSE>, |
| 79 | + Literal, |
| 80 | + Class, |
| 81 | + DOT> {}; |
| 82 | + |
| 83 | + struct Identifier : tao::pegtl::seq<IdentStart, tao::pegtl::star<IdentCont>, Spacing> {}; |
| 84 | + |
| 85 | + struct IdentStart : tao::pegtl::ranges< 'a', 'z', 'A', 'Z', '_' > {}; |
| 86 | + |
| 87 | + struct IdentCont : tao::pegtl::sor< |
| 88 | + IdentStart, |
| 89 | + tao::pegtl::range<'0','9'> |
| 90 | + > {}; |
| 91 | + |
| 92 | + struct Literal : tao::pegtl::sor< |
| 93 | + tao::pegtl::seq< |
| 94 | + tao::pegtl::one<'\''>, |
| 95 | + tao::pegtl::star< |
| 96 | + tao::pegtl::seq< |
| 97 | + tao::pegtl::not_at< |
| 98 | + tao::pegtl::one<'\''> |
| 99 | + >, |
| 100 | + Char |
| 101 | + > |
| 102 | + >, |
| 103 | + tao::pegtl::one<'\''>, |
| 104 | + Spacing |
| 105 | + >, |
| 106 | + tao::pegtl::seq< |
| 107 | + tao::pegtl::one<'\"'>, |
| 108 | + tao::pegtl::star< |
| 109 | + tao::pegtl::seq< |
| 110 | + tao::pegtl::not_at<tao::pegtl::one<'\"'> >, |
| 111 | + Char |
| 112 | + > |
| 113 | + >, |
| 114 | + tao::pegtl::one<'\"'>, |
| 115 | + Spacing |
| 116 | + > |
| 117 | + > {}; |
| 118 | + |
| 119 | + struct Class : tao::pegtl::seq< |
| 120 | + tao::pegtl::one<'['>, |
| 121 | + tao::pegtl::star< |
| 122 | + tao::pegtl::seq< |
| 123 | + tao::pegtl::not_at<tao::pegtl::one<']'> >, |
| 124 | + Range |
| 125 | + > |
| 126 | + >, |
| 127 | + tao::pegtl::one<']'>, |
| 128 | + Spacing |
| 129 | + > {}; |
| 130 | + |
| 131 | + struct Range : tao::pegtl::sor< |
| 132 | + tao::pegtl::seq< |
| 133 | + Char, |
| 134 | + tao::pegtl::one<'-'>, |
| 135 | + Char>, |
| 136 | + Char |
| 137 | + > {}; |
| 138 | + |
| 139 | + struct Char : tao::pegtl::sor< |
| 140 | + tao::pegtl::seq< |
| 141 | + tao::pegtl::one<'\\'>, |
| 142 | + tao::pegtl::one<'n','r','t','\'','\"','[',']','\\'> >, |
| 143 | + tao::pegtl::seq< |
| 144 | + tao::pegtl::one<'\\'>, |
| 145 | + tao::pegtl::range<'0','2'>, |
| 146 | + tao::pegtl::range<'0','7'>, |
| 147 | + tao::pegtl::range<'0','7'> >, |
| 148 | + tao::pegtl::seq< |
| 149 | + tao::pegtl::one<'\\'>, |
| 150 | + tao::pegtl::range<'0','7'>, |
| 151 | + tao::pegtl::opt<tao::pegtl::range<'0','7'> > >, |
| 152 | + tao::pegtl::seq< |
| 153 | + tao::pegtl::not_at<tao::pegtl::one<'\\'> >, |
| 154 | + tao::pegtl::any> |
| 155 | + > {}; |
| 156 | + |
| 157 | + struct LEFTARROW : tao::pegtl::seq<tao::pegtl::string<'<','-'>, Spacing> {}; |
| 158 | + struct SLASH : tao::pegtl::seq<tao::pegtl::one<'/'>, Spacing> {}; |
| 159 | + struct AND : tao::pegtl::seq<tao::pegtl::one<'&'>, Spacing> {}; |
| 160 | + struct NOT : tao::pegtl::seq<tao::pegtl::one<'!'>, Spacing> {}; |
| 161 | + struct QUESTION : tao::pegtl::seq<tao::pegtl::one<'?'>, Spacing> {}; |
| 162 | + struct STAR : tao::pegtl::seq<tao::pegtl::one<'*'>, Spacing> {}; |
| 163 | + struct PLUS : tao::pegtl::seq<tao::pegtl::one<'+'>, Spacing> {}; |
| 164 | + struct OPEN : tao::pegtl::seq<tao::pegtl::one<'('>, Spacing> {}; |
| 165 | + struct CLOSE : tao::pegtl::seq<tao::pegtl::one<')'>, Spacing> {}; |
| 166 | + struct DOT : tao::pegtl::seq<tao::pegtl::one<'.'>, Spacing> {}; |
| 167 | + |
| 168 | + struct Spacing : tao::pegtl::star<tao::pegtl::sor<Space, Comment> > {}; |
| 169 | + struct Comment : |
| 170 | + tao::pegtl::seq< |
| 171 | + tao::pegtl::one<'#'>, |
| 172 | + tao::pegtl::star< |
| 173 | + tao::pegtl::seq< |
| 174 | + tao::pegtl::not_at<EndOfLine>, |
| 175 | + tao::pegtl::any |
| 176 | + > |
| 177 | + >, |
| 178 | + EndOfLine |
| 179 | + > {}; |
| 180 | + |
| 181 | + struct Space : tao::pegtl::sor<tao::pegtl::one<' '>, tao::pegtl::one<'\t'>, EndOfLine> {}; |
| 182 | + struct EndOfLine : tao::pegtl::sor<tao::pegtl::string<'\r','\n'>, tao::pegtl::one<'\n'>, tao::pegtl::one<'\r'> > {}; |
| 183 | + struct EndOfFile : tao::pegtl::eof {}; |
| 184 | + // clang-format on |
| 185 | + |
| 186 | +} // namespace TAO_PEGTL_NAMESPACE::peg |
| 187 | + |
| 188 | +#endif |
0 commit comments