Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(843)

Side by Side Diff: experimental/svg/model/SkPEG.h

Issue 2271743002: Reland: Experimental parsing expression grammar (PEG) template library (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: SkTLazy initializer fix Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « BUILD.gn ('k') | gyp/svg.gyp » ('j') | include/core/SkTLazy.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkPEG_DEFINED
9 #define SkPEG_DEFINED
10
11 #include "SkTDArray.h"
12 #include "SkTLazy.h"
13
14 namespace skpeg {
15
16 /**
17 * The result of an expression match attempt.
18 *
19 * If the match was successful, |fNext| points to the next unconsumed character in the
20 * input string, and |fValue| holds an (arbitrarily nested) match result value.
21 *
22 * Otherwise, |fNext| is nullptr and |fValue| is uninitialized.
23 */
24 template <typename T>
mtklein 2016/08/24 01:19:28 Not clear here why this is parameterized on T and
f(malita) 2016/08/24 13:47:29 True, doesn't do much, does it? Changed to straig
25 struct MatchResult {
26 using V = typename T::V;
27
28 MatchResult(std::nullptr_t) : fNext(nullptr) {}
29 MatchResult(const char* next, const V& v) : fNext(next), fValue(&v) {}
30
31 operator bool() const {
32 SkASSERT(fValue.isValid() == SkToBool(fNext));
33 return SkToBool(fNext);
34 }
35
36 const V& operator* () const { return *fValue.get(); }
37 const V* operator->() const { return fValue.get(); }
38
39 const char* fNext;
40 SkTLazy<V> fValue;
41 };
42
43 /**
44 * Optional operator (e?). Always succeeds.
45 *
46 * If e also matches, then the result of e::Match() is stored in |fValue|.
47 * Otherwise, |fValue| is uninitialized.
48 *
49 */
50 template <typename E>
51 struct Opt {
52 struct V {
53 V(const typename E::V* v) : fValue(v) {}
54
55 SkTLazy<typename E::V> fValue;
56 };
57
58 static MatchResult<Opt> Match(const char* in) {
59 const auto m = E::Match(in);
60 return m ? MatchResult<Opt>(m.fNext, V(m.fValue.get()))
mtklein 2016/08/24 01:19:28 Depending on how that T/V parameterization questio
f(malita) 2016/08/24 13:47:29 I ended up adding a MatchT alias, as it's also app
61 : MatchResult<Opt>(in, nullptr);
62 }
63 };
64
65 /**
66 * Sequence operator (e1e2).
67 *
68 * Succeeds when both e1 and e2 match, in sequence. The subexpression match
69 * results are stored in |v1|, |v2|.
70 *
71 */
72 template <typename E1, typename E2>
73 struct Seq {
bungeman-skia 2016/08/23 20:49:18 While I'm not sure off hand how to do it, having S
mtklein 2016/08/24 01:19:28 Might make sense to have Seq stay binary, but buil
f(malita) 2016/08/24 13:47:29 That's an elegant approach. One thing I like abou
f(malita) 2016/08/24 13:47:29 Yeah, that would definitely be nice. At the time
74 struct V {
75 V(const typename E1::V& v1, const typename E2::V& v2) : v1(v1), v2(v2) { }
76
77 typename E1::V v1;
78 typename E2::V v2;
79 };
80
81 static MatchResult<Seq> Match(const char* in) {
82 const auto m1 = E1::Match(in);
83 if (!m1) {
84 return nullptr;
85 }
86 const auto m2 = E2::Match(m1.fNext);
87 if (!m2) {
88 return nullptr;
89 }
90 return MatchResult<Seq>(m2.fNext, V(*m1, *m2));
91 }
92 };
93
94 /**
95 * Ordered choice operator (e1|e2).
96 *
97 * Succeeds when either e1 or e2 match (e1 is tried first, then e2).
98 *
99 * The (optional) match results are stored in |v1|, |v2|.
100 *
101 */
102 template <typename E1, typename E2>
103 struct Choice {
104 struct V {
105 V (const typename E1::V* v1, const typename E2::V* v2) : v1(v1), v2(v2)
106 {
107 SkASSERT(!v1 || !v2);
108 }
109
110 SkTLazy<typename E1::V> v1;
111 SkTLazy<typename E2::V> v2;
112 };
113
114 static MatchResult<Choice> Match(const char* in) {
115 if (const auto m1 = E1::Match(in)) {
116 return MatchResult<Choice>(m1.fNext, V(m1.fValue.get(), nullptr));
117 }
118 if (const auto m2 = E2::Match(in)) {
119 return MatchResult<Choice>(m2.fNext, V(nullptr, m2.fValue.get()));
120 }
121 return nullptr;
122 }
123 };
124
125 /**
126 * Zero-or-more operator (e*). Always succeeds.
127 *
128 * Matches e greedily, and stores the match results in |fValues|.
129 *
130 */
131 template <typename E>
132 struct Any {
133 struct V {
134 V(SkTDArray<typename E::V>&& vs) : fValues(vs) {}
135
136 SkTDArray<typename E::V> fValues;
137 };
138
139 static MatchResult<Any> Match(const char* in) {
140 SkTDArray<typename E::V> values;
141 for (;;) {
142 if (const auto m = E::Match(in)) {
143 in = m.fNext;
144 *values.append() = *m;
145 } else {
146 break;
147 }
148 }
149
150 return MatchResult<Any>(in, std::move(values));
151 }
152 };
153
154 /**
155 * One-or-more operator (e+).
156 *
157 * Same as zero-or-more, except it fails if e doesn't match at least once.
158 *
159 */
160 template <typename E>
161 struct Some {
mtklein 2016/08/24 01:19:28 Perhaps template <typename E> using Some =
f(malita) 2016/08/24 13:47:29 The only downside is that the value accessor is no
162 struct V {
163 V(SkTDArray<typename E::V>&& vs) : fValues(vs) {}
164
165 SkTDArray<typename E::V> fValues;
166 };
167
168 static MatchResult<Some> Match(const char* in) {
169 auto m = Any<E>::Match(in);
170 SkASSERT(m);
171
172 return m->fValues.isEmpty()
173 ? nullptr
174 : MatchResult<Some>(m.fNext, V(std::move(m.fValue.get()->fValues)));
175 }
176 };
177
178 /**
179 * End-of-string atom. Matches \0.
180 */
181 struct EOS {
182 struct V {};
183
184 static MatchResult<EOS> Match(const char* in) {
185 return *in ? nullptr : MatchResult<EOS>(in, V());
mtklein 2016/08/24 01:19:28 probably nice to write out (*in != '\0')
f(malita) 2016/08/24 13:47:29 Done.
186 }
187 };
188
189
190 /**
191 * Literal atom. Matches a list of char literals.
mtklein 2016/08/24 01:19:28 You may find this more or less annoying, but it is
f(malita) 2016/08/24 13:47:29 Heh, I heard rumors of this unnatural ability :)
192 */
193 template <char... Cs> struct LIT;
194
195 template <>
196 struct LIT<> {
197 struct V {};
198
199 static MatchResult<LIT> Match(const char* in) {
200 return MatchResult<LIT>(in, V());
201 }
202 };
203
204 template <char C, char... Cs>
205 struct LIT<C, Cs...> {
206 struct V {};
207
208 static MatchResult<LIT> Match(const char* in) {
209 if (*in != C) {
210 return nullptr;
211 }
212 const auto m = LIT<Cs...>::Match(in + 1);
213 return m ? MatchResult<LIT>(m.fNext, V()) : nullptr;
214 }
215 };
216
217 /**
218 * Matches a letter (['a'-'z','A'-Z']) and returns the matched
219 * character as the MatchResult value.
220 *
221 */
222 struct Alpha {
223 using V = char;
224
225 static MatchResult<Alpha> Match(const char* in) {
226 static constexpr unsigned kAlphaRange = 'z' - 'a';
227 return static_cast<unsigned>(*in - 'a') <= kAlphaRange
228 || static_cast<unsigned>(*in - 'A') <= kAlphaRange
229 ? MatchResult<Alpha>(in + 1, *in)
230 : nullptr;
231 }
232 };
233
234 /**
235 * Matches a digit ([0-9]) and returns the matched char
236 * as the MatchResult value.
237 */
238 struct Digit {
239 using V = uint8_t;
240
241 static MatchResult<Digit> Match(const char* in) {
242 static constexpr unsigned kDigitRange = '9' - '0';
243 return static_cast<unsigned>(*in - '0') <= kDigitRange
244 ? MatchResult<Digit>(in + 1, SkTo<uint8_t>(*in - '0'))
245 : nullptr;
246 }
247 };
248
249 } // skpeg ns
250
251 #endif // SkPEG_DEFINED
OLDNEW
« no previous file with comments | « BUILD.gn ('k') | gyp/svg.gyp » ('j') | include/core/SkTLazy.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698