Skip to content

Commit e9122b8

Browse files
committed
Implementing peg2pegtl, based on abnf2pegtl. Added peg grammar header and peg.peg grammar.
1 parent d7b821b commit e9122b8

File tree

4 files changed

+735
-0
lines changed

4 files changed

+735
-0
lines changed

include/tao/pegtl/contrib/peg.hpp

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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

src/example/pegtl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set(example_sources
2626
modulus_match.cpp
2727
parse_tree.cpp
2828
parse_tree_user_state.cpp
29+
peg2pegtl.cpp
2930
proto3.cpp
3031
random_order.cpp
3132
recover.cpp

src/example/pegtl/peg.peg

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Parsing Expression Grammar (PEG) taken from
2+
# https://pdos.csail.mit.edu/~baford/packrat/popl04/peg-popl04.pdf
3+
4+
# Hierarchical syntax
5+
Grammar <- Spacing Definition+ EndOfFile
6+
Definition <- Identifier LEFTARROW Expression
7+
Expression <- Sequence (SLASH Sequence)*
8+
Sequence <- Prefix*
9+
Prefix <- (AND / NOT)? Suffix
10+
Suffix <- Primary (QUESTION / STAR / PLUS)?
11+
Primary <- Identifier !LEFTARROW / OPEN Expression CLOSE / Literal / Class / DOT
12+
13+
# Lexical syntax
14+
Identifier <- IdentStart IdentCont* Spacing
15+
IdentStart <- [a-zA-Z_]
16+
IdentCont <- IdentStart / [0-9]
17+
Literal <- ['] (!['] Char)* ['] Spacing / ["] (!["] Char)* ["] Spacing
18+
Class <- '[' (!']' Range)* ']' Spacing
19+
Range <- Char '-' Char / Char
20+
Char <- '\\' [nrt'"\[\]\\] / '\\' [0-2][0-7][0-7] / '\\' [0-7][0-7]? / !'\\' .
21+
22+
LEFTARROW <- '<-' Spacing
23+
SLASH <- '/' Spacing
24+
AND <- '&' Spacing
25+
NOT <- '!' Spacing
26+
QUESTION <- '?' Spacing
27+
STAR <- '*' Spacing
28+
PLUS <- '+' Spacing
29+
OPEN <- '(' Spacing
30+
CLOSE <- ')' Spacing
31+
DOT <- '.' Spacing
32+
33+
Spacing <- (Space / Comment)*
34+
Comment <- '#' (!EndOfLine .)* EndOfLine
35+
Space <- ' ' / '\t' / EndOfLine
36+
EndOfLine <- '\r\n' / '\n' / '\r'
37+
EndOfFile <- !.

0 commit comments

Comments
 (0)