Commit 1bdce291 authored by Kenton Varda's avatar Kenton Varda

Refactor parser code more.

parent 7001fc9d
...@@ -30,29 +30,26 @@ namespace parse { ...@@ -30,29 +30,26 @@ namespace parse {
namespace { namespace {
typedef IteratorInput<char, const char*> Input; typedef IteratorInput<char, const char*> Input;
ExactElementParser<Input> exactChar(char c) {
return exactElement<Input>(mv(c));
}
typedef Span<const char*> TestLocation; typedef Span<const char*> TestLocation;
TEST(Parsers, ExactElementParser) { TEST(Parsers, ExactElementParser) {
StringPtr text = "foo"; StringPtr text = "foo";
Input input(text.begin(), text.end()); Input input(text.begin(), text.end());
Maybe<Tuple<>> result = exactChar('f')(input); Maybe<Tuple<>> result = exactly('f')(input);
EXPECT_TRUE(result != nullptr); EXPECT_TRUE(result != nullptr);
EXPECT_FALSE(input.atEnd()); EXPECT_FALSE(input.atEnd());
result = exactChar('o')(input); result = exactly('o')(input);
EXPECT_TRUE(result != nullptr); EXPECT_TRUE(result != nullptr);
EXPECT_FALSE(input.atEnd()); EXPECT_FALSE(input.atEnd());
result = exactChar('x')(input); result = exactly('x')(input);
EXPECT_TRUE(result == nullptr); EXPECT_TRUE(result == nullptr);
EXPECT_FALSE(input.atEnd()); EXPECT_FALSE(input.atEnd());
Parser<Input, Tuple<>> wrapped = exactChar('o'); auto parser = exactly('o');
ParserRef<Input, Tuple<>> wrapped = ref<Input>(parser);
result = wrapped(input); result = wrapped(input);
EXPECT_TRUE(result != nullptr); EXPECT_TRUE(result != nullptr);
EXPECT_TRUE(input.atEnd()); EXPECT_TRUE(input.atEnd());
...@@ -63,21 +60,21 @@ TEST(Parsers, SequenceParser) { ...@@ -63,21 +60,21 @@ TEST(Parsers, SequenceParser) {
{ {
Input input(text.begin(), text.end()); Input input(text.begin(), text.end());
Maybe<Tuple<>> result = sequence(exactChar('f'), exactChar('o'), exactChar('o'))(input); Maybe<Tuple<>> result = sequence(exactly('f'), exactly('o'), exactly('o'))(input);
EXPECT_TRUE(result != nullptr); EXPECT_TRUE(result != nullptr);
EXPECT_TRUE(input.atEnd()); EXPECT_TRUE(input.atEnd());
} }
{ {
Input input(text.begin(), text.end()); Input input(text.begin(), text.end());
Maybe<Tuple<>> result = sequence(exactChar('f'), exactChar('o'))(input); Maybe<Tuple<>> result = sequence(exactly('f'), exactly('o'))(input);
EXPECT_TRUE(result != nullptr); EXPECT_TRUE(result != nullptr);
EXPECT_FALSE(input.atEnd()); EXPECT_FALSE(input.atEnd());
} }
{ {
Input input(text.begin(), text.end()); Input input(text.begin(), text.end());
Maybe<Tuple<>> result = sequence(exactChar('x'), exactChar('o'), exactChar('o'))(input); Maybe<Tuple<>> result = sequence(exactly('x'), exactly('o'), exactly('o'))(input);
EXPECT_TRUE(result == nullptr); EXPECT_TRUE(result == nullptr);
EXPECT_FALSE(input.atEnd()); EXPECT_FALSE(input.atEnd());
} }
...@@ -85,7 +82,7 @@ TEST(Parsers, SequenceParser) { ...@@ -85,7 +82,7 @@ TEST(Parsers, SequenceParser) {
{ {
Input input(text.begin(), text.end()); Input input(text.begin(), text.end());
Maybe<Tuple<>> result = Maybe<Tuple<>> result =
sequence(sequence(exactChar('f'), exactChar('o')), exactChar('o'))(input); sequence(sequence(exactly('f'), exactly('o')), exactly('o'))(input);
EXPECT_TRUE(result != nullptr); EXPECT_TRUE(result != nullptr);
EXPECT_TRUE(input.atEnd()); EXPECT_TRUE(input.atEnd());
} }
...@@ -93,15 +90,15 @@ TEST(Parsers, SequenceParser) { ...@@ -93,15 +90,15 @@ TEST(Parsers, SequenceParser) {
{ {
Input input(text.begin(), text.end()); Input input(text.begin(), text.end());
Maybe<Tuple<>> result = Maybe<Tuple<>> result =
sequence(sequence(exactChar('f')), exactChar('o'), exactChar('o'))(input); sequence(sequence(exactly('f')), exactly('o'), exactly('o'))(input);
EXPECT_TRUE(result != nullptr); EXPECT_TRUE(result != nullptr);
EXPECT_TRUE(input.atEnd()); EXPECT_TRUE(input.atEnd());
} }
{ {
Input input(text.begin(), text.end()); Input input(text.begin(), text.end());
Maybe<int> result = sequence(transform(exactChar('f'), [](TestLocation){return 123;}), Maybe<int> result = sequence(transform(exactly('f'), [](TestLocation){return 123;}),
exactChar('o'), exactChar('o'))(input); exactly('o'), exactly('o'))(input);
KJ_IF_MAYBE(i, result) { KJ_IF_MAYBE(i, result) {
EXPECT_EQ(123, *i); EXPECT_EQ(123, *i);
} else { } else {
...@@ -111,74 +108,11 @@ TEST(Parsers, SequenceParser) { ...@@ -111,74 +108,11 @@ TEST(Parsers, SequenceParser) {
} }
} }
TEST(Parsers, TransformParser) { TEST(Parsers, ManyParser) {
StringPtr text = "foo";
auto parser = transform(
sequence(exactChar('f'), exactChar('o'), exactChar('o')),
[](TestLocation location) -> int {
EXPECT_EQ("foo", StringPtr(location.begin(), location.end()));
return 123;
});
{
Input input(text.begin(), text.end());
Maybe<int> result = parser(input);
KJ_IF_MAYBE(i, result) {
EXPECT_EQ(123, *i);
} else {
ADD_FAILURE() << "Expected 123, got null.";
}
EXPECT_TRUE(input.atEnd());
}
}
TEST(Parsers, TransformParser_MaybeRef) {
struct Transform {
int value;
Transform(int value): value(value) {}
int operator()(TestLocation) const { return value; }
};
// Don't use auto for the TransformParsers here because we're trying to make sure that MaybeRef
// is working correctly. When transform() is given an lvalue, it should wrap the type in
// ParserRef.
TransformParser<ExactElementParser<Input>, Transform> parser1 =
transform(exactChar('f'), Transform(12));
auto otherParser = exactChar('o');
TransformParser<ParserRef<ExactElementParser<Input>>, Transform> parser2 =
transform(otherParser, Transform(34));
auto otherParser2 = exactChar('b');
TransformParser<ExactElementParser<Input>, Transform> parser3 =
transform(mv(otherParser2), Transform(56));
StringPtr text = "foob";
auto parser = transform(
sequence(parser1, parser2, exactChar('o'), parser3),
[](TestLocation, int i, int j, int k) { return i + j + k; });
{
Input input(text.begin(), text.end());
Maybe<int> result = parser(input);
KJ_IF_MAYBE(i, result) {
EXPECT_EQ(12 + 34 + 56, *i);
} else {
ADD_FAILURE() << "Expected 12 + 34 + 56, got null.";
}
EXPECT_TRUE(input.atEnd());
}
}
TEST(Parsers, RepeatedParser) {
StringPtr text = "foooob"; StringPtr text = "foooob";
auto parser = transform( auto parser = transform(
sequence(exactChar('f'), repeated(exactChar('o'))), sequence(exactly('f'), many(exactly('o'))),
[](TestLocation, ArrayPtr<Tuple<>> values) -> int { return values.size(); }); [](TestLocation, ArrayPtr<Tuple<>> values) -> int { return values.size(); });
{ {
...@@ -215,11 +149,57 @@ TEST(Parsers, RepeatedParser) { ...@@ -215,11 +149,57 @@ TEST(Parsers, RepeatedParser) {
} }
} }
TEST(Parsers, OptionalParser) {
auto parser = sequence(
transform(exactly('b'), [](TestLocation) -> uint { return 123; }),
optional(transform(exactly('a'), [](TestLocation) -> uint { return 456; })),
transform(exactly('r'), [](TestLocation) -> uint { return 789; }));
{
StringPtr text = "bar";
Input input(text.begin(), text.end());
Maybe<Tuple<uint, Maybe<uint>, uint>> result = parser(input);
KJ_IF_MAYBE(value, result) {
EXPECT_EQ(123, get<0>(*value));
KJ_IF_MAYBE(value2, get<1>(*value)) {
EXPECT_EQ(456, *value2);
} else {
ADD_FAILURE() << "Expected 456, got null.";
}
EXPECT_EQ(789, get<2>(*value));
} else {
ADD_FAILURE() << "Expected result tuple, got null.";
}
EXPECT_TRUE(input.atEnd());
}
{
StringPtr text = "br";
Input input(text.begin(), text.end());
Maybe<Tuple<uint, Maybe<uint>, uint>> result = parser(input);
KJ_IF_MAYBE(value, result) {
EXPECT_EQ(123, get<0>(*value));
EXPECT_TRUE(get<1>(*value) == nullptr);
EXPECT_EQ(789, get<2>(*value));
} else {
ADD_FAILURE() << "Expected result tuple, got null.";
}
EXPECT_TRUE(input.atEnd());
}
{
StringPtr text = "bzr";
Input input(text.begin(), text.end());
Maybe<Tuple<uint, Maybe<uint>, uint>> result = parser(input);
EXPECT_TRUE(result == nullptr);
}
}
TEST(Parsers, OneOfParser) { TEST(Parsers, OneOfParser) {
auto parser = oneOf( auto parser = oneOf(
transform(sequence(exactChar('f'), exactChar('o'), exactChar('o')), transform(sequence(exactly('f'), exactly('o'), exactly('o')),
[](TestLocation) -> StringPtr { return "foo"; }), [](TestLocation) -> StringPtr { return "foo"; }),
transform(sequence(exactChar('b'), exactChar('a'), exactChar('r')), transform(sequence(exactly('b'), exactly('a'), exactly('r')),
[](TestLocation) -> StringPtr { return "bar"; })); [](TestLocation) -> StringPtr { return "bar"; }));
{ {
...@@ -247,6 +227,107 @@ TEST(Parsers, OneOfParser) { ...@@ -247,6 +227,107 @@ TEST(Parsers, OneOfParser) {
} }
} }
TEST(Parsers, TransformParser) {
StringPtr text = "foo";
auto parser = transform(
sequence(exactly('f'), exactly('o'), exactly('o')),
[](TestLocation location) -> int {
EXPECT_EQ("foo", StringPtr(location.begin(), location.end()));
return 123;
});
{
Input input(text.begin(), text.end());
Maybe<int> result = parser(input);
KJ_IF_MAYBE(i, result) {
EXPECT_EQ(123, *i);
} else {
ADD_FAILURE() << "Expected 123, got null.";
}
EXPECT_TRUE(input.atEnd());
}
}
TEST(Parsers, References) {
struct TransformFunc {
int value;
TransformFunc(int value): value(value) {}
int operator()(TestLocation) const { return value; }
};
// Don't use auto for the parsers here in order to verify that the templates are properly choosing
// whether to use references or copies.
Transform_<Exactly_<char>, TransformFunc> parser1 =
transform(exactly('f'), TransformFunc(12));
auto otherParser = exactly('o');
Transform_<Exactly_<char>&, TransformFunc> parser2 =
transform(otherParser, TransformFunc(34));
auto otherParser2 = exactly('b');
Transform_<Exactly_<char>, TransformFunc> parser3 =
transform(mv(otherParser2), TransformFunc(56));
StringPtr text = "foob";
auto parser = transform(
sequence(parser1, parser2, exactly('o'), parser3),
[](TestLocation, int i, int j, int k) { return i + j + k; });
{
Input input(text.begin(), text.end());
Maybe<int> result = parser(input);
KJ_IF_MAYBE(i, result) {
EXPECT_EQ(12 + 34 + 56, *i);
} else {
ADD_FAILURE() << "Expected 12 + 34 + 56, got null.";
}
EXPECT_TRUE(input.atEnd());
}
}
TEST(Parsers, AcceptIfParser) {
auto parser = acceptIf(
oneOf(transform(exactly('a'), [](TestLocation) -> uint { return 123; }),
transform(exactly('b'), [](TestLocation) -> uint { return 456; }),
transform(exactly('c'), [](TestLocation) -> uint { return 789; })),
[](uint i) {return i > 200;});
{
StringPtr text = "a";
Input input(text.begin(), text.end());
Maybe<uint> result = parser(input);
EXPECT_TRUE(result == nullptr);
}
{
StringPtr text = "b";
Input input(text.begin(), text.end());
Maybe<uint> result = parser(input);
KJ_IF_MAYBE(value, result) {
EXPECT_EQ(456, *value);
} else {
ADD_FAILURE() << "Expected parse result, got null.";
}
EXPECT_TRUE(input.atEnd());
}
{
StringPtr text = "c";
Input input(text.begin(), text.end());
Maybe<uint> result = parser(input);
KJ_IF_MAYBE(value, result) {
EXPECT_EQ(789, *value);
} else {
ADD_FAILURE() << "Expected parse result, got null.";
}
EXPECT_TRUE(input.atEnd());
}
}
} // namespace } // namespace
} // namespace parse } // namespace parse
} // namespace kj } // namespace kj
...@@ -21,6 +21,20 @@ ...@@ -21,6 +21,20 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Parser combinator framework!
//
// This file declares several functions which construct parsers, usually taking other parsers as
// input, thus making them parser combinators.
//
// A valid parser is any functor which takes a reference to an input cursor (defined below) as its
// input and returns a Maybe. The parser returns null on parse failure, or returns the parsed
// result on success.
//
// An "input cursor" is any type which implements the same interface as IteratorInput, below. Such
// a type acts as a pointer to the current input location. When a parser returns successfully, it
// will have updated the input cursor to point to the position just past the end of what was parsed.
// On failure, the cursor position is unspecified.
#ifndef KJ_PARSER_H_ #ifndef KJ_PARSER_H_
#define KJ_PARSER_H_ #define KJ_PARSER_H_
...@@ -35,6 +49,8 @@ namespace parse { ...@@ -35,6 +49,8 @@ namespace parse {
template <typename Element, typename Iterator> template <typename Element, typename Iterator>
class IteratorInput { class IteratorInput {
// A parser input implementation based on an iterator range.
public: public:
typedef Element ElementType; typedef Element ElementType;
...@@ -81,162 +97,71 @@ private: ...@@ -81,162 +97,71 @@ private:
IteratorInput& operator=(IteratorInput&&) = delete; IteratorInput& operator=(IteratorInput&&) = delete;
}; };
template <typename T> template <typename T> struct OutputType_;
struct ExtractParseFuncType; template <typename T> struct OutputType_<Maybe<T>> { typedef T Type; };
template <typename Parser, typename Input>
template <typename I, typename O, typename Object> using OutputType = typename OutputType_<decltype(instance<Parser&>()(instance<Input&>()))>::Type;
struct ExtractParseFuncType<Maybe<O> (Object::*)(I&) const> { // Synonym for the output type of a parser, given the parser type and the input type.
typedef I InputType;
typedef typename I::ElementType ElementType;
typedef O OutputType;
};
template <typename I, typename O, typename Object>
struct ExtractParseFuncType<Maybe<O> (Object::*)(I&)> {
typedef I InputType;
typedef typename I::ElementType ElementType;
typedef O OutputType;
};
template <typename T>
struct ExtractParserType: public ExtractParseFuncType<decltype(&T::operator())> {};
template <typename T>
struct ExtractParserType<T&>: public ExtractParserType<T> {};
template <typename T>
struct ExtractParserType<T&&>: public ExtractParserType<T> {};
template <typename T>
struct ExtractParserType<const T>: public ExtractParserType<T> {};
template <typename T>
struct ExtractParserType<const T&>: public ExtractParserType<T> {};
template <typename T>
struct ExtractParserType<const T&&>: public ExtractParserType<T> {};
// ======================================================================================= // =======================================================================================
template <typename Input, typename Output> template <typename Input, typename Output>
class ParserWrapper { class ParserRef {
public: // Acts as a reference to some other parser, with simplified type. The referenced parser
virtual ~ParserWrapper() {} // is polymorphic by virtual call rather than templates. For grammars of non-trivial size,
// it is important to inject refs into the grammar here and there to prevent the parser types
typedef Input InputType; // from becoming ridiculous. Using too many of them can hurt performance, though.
typedef typename Input::ElementType ElementType;
typedef Output OutputType;
virtual Maybe<Output> operator()(Input& input) const = 0;
virtual Own<ParserWrapper> clone() = 0;
};
template <typename Input, typename Output>
class Parser {
public: public:
Parser(const Parser& other): wrapper(other.wrapper->clone()) {}
Parser(Parser& other): wrapper(other.wrapper->clone()) {}
Parser(const Parser&& other): wrapper(other.wrapper->clone()) {}
Parser(Parser&& other): wrapper(kj::mv(other.wrapper)) {}
Parser(Own<ParserWrapper<Input, Output>> wrapper): wrapper(kj::mv(wrapper)) {}
template <typename Other> template <typename Other>
Parser(Other&& other): wrapper(heap<WrapperImpl<Other>>(kj::mv(other))) {} ParserRef(Other& other): parser(&other), wrapper(WrapperImpl<Other>::instance()) {}
Parser& operator=(const Parser& other) { wrapper = other.wrapper->clone(); }
Parser& operator=(Parser&& other) { wrapper = kj::mv(other.wrapper); }
// Always inline in the hopes that this allows branch prediction to kick in so the virtual call KJ_ALWAYS_INLINE(Maybe<Output> operator()(Input& input) const) {
// doesn't hurt so much. // Always inline in the hopes that this allows branch prediction to kick in so the virtual call
inline Maybe<Output> operator()(Input& input) const __attribute__((always_inline)) { // doesn't hurt so much.
return (*wrapper)(input); return wrapper.parse(parser, input);
} }
private: private:
Own<ParserWrapper<Input, Output>> wrapper; struct Wrapper {
virtual Maybe<Output> parse(const void* parser, Input& input) const = 0;
template <typename Other> };
struct WrapperImpl: public ParserWrapper<Input, Output> { template <typename ParserImpl>
WrapperImpl(Other&& impl): impl(kj::mv(impl)) {}; struct WrapperImpl: public Wrapper {
~WrapperImpl() {} Maybe<Output> parse(const void* parser, Input& input) const override {
return (*reinterpret_cast<const ParserImpl*>(parser))(input);
Maybe<Output> operator()(Input& input) const {
return impl(input);
} }
Own<ParserWrapper<Input, Output>> clone() { static WrapperImpl& instance() {
return heap<WrapperImpl>(*this); static WrapperImpl obj;
return obj;
} }
Other impl;
}; };
};
template <typename ParserImpl>
Parser<typename ExtractParserType<ParserImpl>::InputType,
typename ExtractParserType<ParserImpl>::OutputType>
wrap(ParserImpl&& impl) {
typedef typename ExtractParserType<ParserImpl>::InputType Input;
typedef typename ExtractParserType<ParserImpl>::OutputType Output;
return Parser<Input, Output>(kj::mv(impl));
}
template <typename SubParser>
class ParserRef {
public:
explicit ParserRef(const SubParser& parser): parser(&parser) {}
Maybe<typename ExtractParserType<SubParser>::OutputType> operator()(
typename ExtractParserType<SubParser>::InputType& input) const {
return (*parser)(input);
}
private: const void* parser;
const SubParser* parser; Wrapper& wrapper;
};
template <typename SubParser>
ParserRef<Decay<SubParser>> ref(const SubParser& impl) {
return ParserRef<Decay<SubParser>>(impl);
}
template <typename T>
struct MaybeRef {
typedef Decay<T> Type;
template <typename U>
static Type from(U&& parser) {
return static_cast<Type&&>(parser);
}
}; };
template <typename T> template <typename Input, typename ParserImpl>
struct MaybeRef<T&> { ParserRef<Input, OutputType<ParserImpl, Input>>
typedef ParserRef<Decay<T>> Type; ref(ParserImpl& impl) {
// Constructs a ParserRef. You must specify the input type explicitly, e.g.
template <typename U> // `ref<MyInput>(myParser)`.
static Type from(U& parser) {
return parse::ref(parser);
}
};
template <template <typename SubParser> class WrapperParser> return ParserRef<Input, OutputType<ParserImpl, Input>>(impl);
struct WrapperParserConstructor { }
template <typename SubParser, typename... Args>
WrapperParser<typename MaybeRef<SubParser>::Type> operator()(
SubParser&& subParser, Args&&... args) {
return WrapperParser<typename MaybeRef<SubParser>::Type>(
MaybeRef<SubParser>::from(subParser),
kj::fwd(args)...);
}
};
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// ExactElementParser // exactly()
// Output = Tuple<> // Output = Tuple<>
template <typename Input> template <typename T>
class ExactElementParser { class Exactly_ {
public: public:
explicit ExactElementParser(typename Input::ElementType&& expected): expected(expected) {} explicit Exactly_(T&& expected): expected(expected) {}
virtual Maybe<Tuple<>> operator()(Input& input) const { template <typename Input>
Maybe<Tuple<>> operator()(Input& input) const {
if (input.atEnd() || input.current() != expected) { if (input.atEnd() || input.current() != expected) {
return nullptr; return nullptr;
} else { } else {
...@@ -246,40 +171,44 @@ public: ...@@ -246,40 +171,44 @@ public:
} }
private: private:
typename Input::ElementType expected; T expected;
}; };
template <typename Input> template <typename T>
ExactElementParser<Input> exactElement(typename Input::ElementType&& expected) { Exactly_<T> exactly(T&& expected) {
return ExactElementParser<Decay<Input>>(kj::mv(expected)); // Constructs a parser which succeeds when the input is exactly the token specified. The
// result is always the empty tuple.
return Exactly_<T>(kj::fwd<T>(expected));
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// SequenceParser // sequence()
// Output = Flattened Tuple of outputs of sub-parsers. // Output = Flattened Tuple of outputs of sub-parsers.
template <typename Input, typename... SubParsers> class SequenceParser; template <typename... SubParsers> class Sequence_;
template <typename Input, typename FirstSubParser, typename... SubParsers> template <typename FirstSubParser, typename... SubParsers>
class SequenceParser<Input, FirstSubParser, SubParsers...> { class Sequence_<FirstSubParser, SubParsers...> {
public: public:
template <typename T, typename... U> template <typename T, typename... U>
explicit SequenceParser(T&& firstSubParser, U&&... rest) explicit Sequence_(T&& firstSubParser, U&&... rest)
: first(kj::fwd<T>(firstSubParser)), rest(kj::fwd<U>(rest)...) {} : first(kj::fwd<T>(firstSubParser)), rest(kj::fwd<U>(rest)...) {}
template <typename Input>
auto operator()(Input& input) const -> auto operator()(Input& input) const ->
Maybe<decltype(tuple( Maybe<decltype(tuple(
instance<typename ExtractParserType<FirstSubParser>::OutputType>(), instance<OutputType<FirstSubParser, Input>>(),
instance<typename ExtractParserType<SubParsers>::OutputType>()...))> { instance<OutputType<SubParsers, Input>>()...))> {
return parseNext(input); return parseNext(input);
} }
template <typename... InitialParams> template <typename Input, typename... InitialParams>
auto parseNext(Input& input, InitialParams&&... initialParams) const -> auto parseNext(Input& input, InitialParams&&... initialParams) const ->
Maybe<decltype(tuple( Maybe<decltype(tuple(
kj::fwd<InitialParams>(initialParams)..., kj::fwd<InitialParams>(initialParams)...,
instance<typename ExtractParserType<FirstSubParser>::OutputType>(), instance<OutputType<FirstSubParser, Input>>(),
instance<typename ExtractParserType<SubParsers>::OutputType>()...))> { instance<OutputType<SubParsers, Input>>()...))> {
KJ_IF_MAYBE(firstResult, first(input)) { KJ_IF_MAYBE(firstResult, first(input)) {
return rest.parseNext(input, kj::fwd<InitialParams>(initialParams)..., return rest.parseNext(input, kj::fwd<InitialParams>(initialParams)...,
kj::mv(*firstResult)); kj::mv(*firstResult));
...@@ -290,52 +219,49 @@ public: ...@@ -290,52 +219,49 @@ public:
private: private:
FirstSubParser first; FirstSubParser first;
SequenceParser<Input, SubParsers...> rest; Sequence_<SubParsers...> rest;
}; };
template <typename Input> template <>
class SequenceParser<Input> { class Sequence_<> {
public: public:
template <typename Input>
Maybe<Tuple<>> operator()(Input& input) const { Maybe<Tuple<>> operator()(Input& input) const {
return parseNext(input); return parseNext(input);
} }
template <typename... Params> template <typename Input, typename... Params>
auto parseNext(Input& input, Params&&... params) const -> auto parseNext(Input& input, Params&&... params) const ->
Maybe<decltype(tuple(kj::fwd<Params>(params)...))> { Maybe<decltype(tuple(kj::fwd<Params>(params)...))> {
return tuple(kj::fwd<Params>(params)...); return tuple(kj::fwd<Params>(params)...);
} }
}; };
template <typename FirstSubParser, typename... MoreSubParsers> template <typename... SubParsers>
SequenceParser<typename ExtractParserType<FirstSubParser>::InputType, Sequence_<SubParsers...> sequence(SubParsers&&... subParsers) {
typename MaybeRef<FirstSubParser>::Type, // Constructs a parser that executes each of the parameter parsers in sequence and returns a
typename MaybeRef<MoreSubParsers>::Type...> // tuple of their results.
sequence(FirstSubParser&& first, MoreSubParsers&&... rest) {
return SequenceParser<typename ExtractParserType<FirstSubParser>::InputType,
typename MaybeRef<FirstSubParser>::Type,
typename MaybeRef<MoreSubParsers>::Type...>(
MaybeRef<FirstSubParser>::from(first), MaybeRef<MoreSubParsers>::from(rest)...);
}
return Sequence_<SubParsers...>(kj::fwd<SubParsers>(subParsers)...);
}
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// RepeatedParser // many()
// Output = Array of output of sub-parser. // Output = Array of output of sub-parser.
template <typename SubParser, bool atLeastOne> template <typename SubParser, bool atLeastOne>
class RepeatedParser { class Many_ {
public: public:
explicit RepeatedParser(SubParser&& subParser) explicit Many_(SubParser&& subParser)
: subParser(kj::mv(subParser)) {} : subParser(kj::mv(subParser)) {}
Maybe<Vector<typename ExtractParserType<SubParser>::OutputType>> operator()( template <typename Input>
typename ExtractParserType<SubParser>::InputType& input) const { Maybe<Array<OutputType<SubParser, Input>>> operator()(Input& input) const {
typedef Vector<typename ExtractParserType<SubParser>::OutputType> Results; typedef Vector<OutputType<SubParser, Input>> Results;
Results results; Results results;
while (!input.atEnd()) { while (!input.atEnd()) {
typename ExtractParserType<SubParser>::InputType subInput(input); Input subInput(input);
KJ_IF_MAYBE(subResult, subParser(subInput)) { KJ_IF_MAYBE(subResult, subParser(subInput)) {
subInput.advanceParent(); subInput.advanceParent();
...@@ -349,7 +275,7 @@ public: ...@@ -349,7 +275,7 @@ public:
return nullptr; return nullptr;
} }
return kj::mv(results); return results.releaseAsArray();
} }
private: private:
...@@ -357,41 +283,38 @@ private: ...@@ -357,41 +283,38 @@ private:
}; };
template <typename SubParser> template <typename SubParser>
RepeatedParser<typename MaybeRef<SubParser>::Type, false> Many_<SubParser, false> many(SubParser&& subParser) {
repeated(SubParser&& subParser) { // Constructs a parser that repeatedly executes the given parser until it fails, returning an
return RepeatedParser<typename MaybeRef<SubParser>::Type, false>( // Array of the results.
MaybeRef<SubParser>::from(subParser)); return Many_<SubParser, false>(kj::fwd<SubParser>(subParser));
} }
template <typename SubParser> template <typename SubParser>
RepeatedParser<typename MaybeRef<SubParser>::Type, true> Many_<SubParser, true> oneOrMore(SubParser&& subParser) {
oneOrMore(SubParser&& subParser) { // Like `many()` but the parser must parse at least one item to be successful.
return RepeatedParser<typename MaybeRef<SubParser>::Type, true>( return Many_<SubParser, true>(kj::fwd<SubParser>(subParser));
MaybeRef<SubParser>::from(subParser));
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// OptionalParser // optional()
// Output = Maybe<output of sub-parser> // Output = Maybe<output of sub-parser>
template <typename SubParser> template <typename SubParser>
class OptionalParser { class Optional_ {
public: public:
explicit OptionalParser(SubParser&& subParser) explicit Optional_(SubParser&& subParser)
: subParser(kj::mv(subParser)) {} : subParser(kj::mv(subParser)) {}
Maybe<Maybe<typename ExtractParserType<SubParser>::OutputType>> operator()( template <typename Input>
typename ExtractParserType<SubParser>::InputType& input) const { Maybe<Maybe<OutputType<SubParser, Input>>> operator()(Input& input) const {
typedef Maybe<typename ExtractParserType<SubParser>::OutputType> Result; typedef Maybe<OutputType<SubParser, Input>> Result;
typename ExtractParserType<SubParser>::InputType subInput(input); Input subInput(input);
auto subResult = subParser(subInput); KJ_IF_MAYBE(subResult, subParser(subInput)) {
if (subResult == nullptr) {
return Result(nullptr);
} else {
subInput.advanceParent(); subInput.advanceParent();
return Result(kj::mv(*subResult)); return Result(kj::mv(*subResult));
} else {
return Result(nullptr);
} }
} }
...@@ -400,34 +323,34 @@ private: ...@@ -400,34 +323,34 @@ private:
}; };
template <typename SubParser> template <typename SubParser>
OptionalParser<typename MaybeRef<SubParser>::Type> Optional_<SubParser> optional(SubParser&& subParser) {
optional(SubParser&& subParser) { // Constructs a parser that accepts zero or one of the given sub-parser, returning a Maybe
return OptionalParser<typename MaybeRef<SubParser>::Type>( // of the sub-parser's result.
MaybeRef<SubParser>::from(subParser)); return Optional_<SubParser>(kj::fwd<SubParser>(subParser));
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// OneOfParser // oneOf()
// All SubParsers must have same output type, which becomes the output type of the // All SubParsers must have same output type, which becomes the output type of the
// OneOfParser. // OneOfParser.
template <typename Input, typename Output, typename... SubParsers> template <typename... SubParsers>
class OneOfParser; class OneOf_;
template <typename Input, typename Output, typename FirstSubParser, typename... SubParsers> template <typename FirstSubParser, typename... SubParsers>
class OneOfParser<Input, Output, FirstSubParser, SubParsers...> { class OneOf_<FirstSubParser, SubParsers...> {
public: public:
template <typename T, typename... U> template <typename T, typename... U>
explicit OneOfParser(T&& firstSubParser, U&&... rest) explicit OneOf_(T&& firstSubParser, U&&... rest)
: first(kj::fwd<T>(firstSubParser)), rest(kj::fwd<U>(rest)...) {} : first(kj::fwd<T>(firstSubParser)), rest(kj::fwd<U>(rest)...) {}
Maybe<Output> operator()(Input& input) const { template <typename Input>
Maybe<OutputType<FirstSubParser, Input>> operator()(Input& input) const {
{ {
Input subInput(input); Input subInput(input);
Maybe<Output> firstResult = first(subInput); Maybe<OutputType<FirstSubParser, Input>> firstResult = first(subInput);
if (firstResult != nullptr) { if (firstResult != nullptr) {
// MAYBE: Should we try parsing with "rest" in order to check for ambiguities?
subInput.advanceParent(); subInput.advanceParent();
return kj::mv(firstResult); return kj::mv(firstResult);
} }
...@@ -439,32 +362,28 @@ public: ...@@ -439,32 +362,28 @@ public:
private: private:
FirstSubParser first; FirstSubParser first;
OneOfParser<Input, Output, SubParsers...> rest; OneOf_<SubParsers...> rest;
}; };
template <typename Input, typename Output> template <>
class OneOfParser<Input, Output> { class OneOf_<> {
public: public:
Maybe<Output> operator()(Input& input) const { template <typename Input>
decltype(nullptr) operator()(Input& input) const {
return nullptr; return nullptr;
} }
}; };
template <typename FirstSubParser, typename... MoreSubParsers> template <typename... SubParsers>
OneOfParser<typename ExtractParserType<FirstSubParser>::InputType, OneOf_<SubParsers...> oneOf(SubParsers&&... parsers) {
typename ExtractParserType<FirstSubParser>::OutputType, // Constructs a parser that accepts one of a set of options. The parser behaves as the first
typename MaybeRef<FirstSubParser>::Type, // sub-parser in the list which returns successfully. All of the sub-parsers must return the
typename MaybeRef<MoreSubParsers>::Type...> // same type.
oneOf(FirstSubParser&& first, MoreSubParsers&&... rest) { return OneOf_<SubParsers...>(kj::fwd<SubParsers>(parsers)...);
return OneOfParser<typename ExtractParserType<FirstSubParser>::InputType,
typename ExtractParserType<FirstSubParser>::OutputType,
typename MaybeRef<FirstSubParser>::Type,
typename MaybeRef<MoreSubParsers>::Type...>(
MaybeRef<FirstSubParser>::from(first), MaybeRef<MoreSubParsers>::from(rest)...);
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// TransformParser // transform()
// Output = Result of applying transform functor to input value. If input is a tuple, it is // Output = Result of applying transform functor to input value. If input is a tuple, it is
// unpacked to form the transformation parameters. // unpacked to form the transformation parameters.
...@@ -482,22 +401,25 @@ private: ...@@ -482,22 +401,25 @@ private:
Position end_; Position end_;
}; };
template <typename SubParser, typename Transform> template <typename Position>
class TransformParser { Span<Decay<Position>> span(Position&& start, Position&& end) {
public: return Span<Decay<Position>>(kj::fwd<Position>(start), kj::fwd<Position>(end));
explicit TransformParser(SubParser&& subParser, Transform&& transform) }
: subParser(kj::mv(subParser)), transform(kj::mv(transform)) {}
typedef typename ExtractParserType<SubParser>::InputType InputType;
typedef Decay<decltype(instance<InputType>().getPosition())> Position;
typedef typename ExtractParserType<SubParser>::OutputType SubOutput;
typedef decltype(kj::apply(instance<Transform&>(), instance<Span<Position>>(),
instance<SubOutput&&>())) Output;
Maybe<Output> operator()(InputType& input) const { template <typename SubParser, typename TransformFunc>
class Transform_ {
public:
explicit Transform_(SubParser&& subParser, TransformFunc&& transform)
: subParser(kj::fwd<SubParser>(subParser)), transform(kj::fwd<TransformFunc>(transform)) {}
template <typename Input>
Maybe<decltype(kj::apply(instance<TransformFunc&>(),
instance<Span<Decay<decltype(instance<Input&>().getPosition())>>>(),
instance<OutputType<SubParser, Input>&&>()))>
operator()(Input& input) const {
auto start = input.getPosition(); auto start = input.getPosition();
KJ_IF_MAYBE(subResult, subParser(input)) { KJ_IF_MAYBE(subResult, subParser(input)) {
return kj::apply(transform, Span<Position>(kj::mv(start), input.getPosition()), return kj::apply(transform, Span<decltype(start)>(kj::mv(start), input.getPosition()),
kj::mv(*subResult)); kj::mv(*subResult));
} else { } else {
return nullptr; return nullptr;
...@@ -506,33 +428,39 @@ public: ...@@ -506,33 +428,39 @@ public:
private: private:
SubParser subParser; SubParser subParser;
Transform transform; TransformFunc transform;
}; };
template <typename SubParser, typename Transform> template <typename SubParser, typename TransformFunc>
TransformParser<typename MaybeRef<SubParser>::Type, Decay<Transform>> Transform_<SubParser, TransformFunc> transform(SubParser&& subParser, TransformFunc&& functor) {
transform(SubParser&& subParser, Transform&& transform) { // Constructs a parser which executes some other parser and then transforms the result by invoking
return TransformParser<typename MaybeRef<SubParser>::Type, Decay<Transform>>( // `functor` on it. Typically `functor` is a lambda. It is invoked using `kj::apply`,
MaybeRef<SubParser>::from(subParser), kj::fwd<Transform>(transform)); // meaning tuples will be unpacked as arguments.
return Transform_<SubParser, TransformFunc>(
kj::fwd<SubParser>(subParser), kj::fwd<TransformFunc>(functor));
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// AcceptIfParser // acceptIf()
// Output = Same as SubParser // Output = Same as SubParser
template <typename SubParser, typename Condition> template <typename SubParser, typename Condition>
class AcceptIfParser { class AcceptIf_ {
public: public:
explicit AcceptIfParser(SubParser&& subParser, Condition&& condition) explicit AcceptIf_(SubParser&& subParser, Condition&& condition)
: subParser(kj::mv(subParser)), condition(kj::mv(condition)) {} : subParser(kj::mv(subParser)), condition(kj::mv(condition)) {}
Maybe<typename ExtractParserType<SubParser>::OutputType> template <typename Input>
operator()(typename ExtractParserType<SubParser>::InputType& input) const { Maybe<OutputType<SubParser, Input>> operator()(Input& input) const {
Maybe<typename ExtractParserType<SubParser>::OutputType> subResult = subParser(input); KJ_IF_MAYBE(subResult, subParser(input)) {
if (subResult && !condition(*subResult)) { if (condition(*subResult)) {
subResult = nullptr; return kj::mv(*subResult);
} else {
return nullptr;
}
} else {
return nullptr;
} }
return subResult;
} }
private: private:
...@@ -541,19 +469,22 @@ private: ...@@ -541,19 +469,22 @@ private:
}; };
template <typename SubParser, typename Condition> template <typename SubParser, typename Condition>
AcceptIfParser<typename MaybeRef<SubParser>::Type, Decay<Condition>> AcceptIf_<SubParser, Condition> acceptIf(SubParser&& subParser, Condition&& condition) {
acceptIf(SubParser&& subParser, Condition&& condition) { // Constructs a parser which executes some other parser and then invokes the functor
return AcceptIfParser<typename MaybeRef<SubParser>::Type, Decay<Condition>>( // `condition` on the result to check if it is valid. Typically, `condition` is a lambda
MaybeRef<SubParser>::from(subParser), kj::fwd<Condition>(condition)); // returning true or false. Like with `transform()`, `condition` is invoked using `kj::apply`
// to unpack tuples.
return AcceptIf_<SubParser, Condition>(
kj::fwd<SubParser>(subParser), kj::fwd<Condition>(condition));
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// EndOfInputParser // endOfInput()
// Output = Tuple<>, only succeeds if at end-of-input // Output = Tuple<>, only succeeds if at end-of-input
template <typename Input> class EndOfInput_ {
class EndOfInputParser {
public: public:
template <typename Input>
Maybe<Tuple<>> operator()(Input& input) const { Maybe<Tuple<>> operator()(Input& input) const {
if (input.atEnd()) { if (input.atEnd()) {
return Tuple<>(); return Tuple<>();
...@@ -563,9 +494,9 @@ public: ...@@ -563,9 +494,9 @@ public:
} }
}; };
template <typename T> EndOfInput_ endOfInput() {
EndOfInputParser<T> endOfInput() { // Constructs a parser that succeeds only if it is called with no input.
return EndOfInputParser<T>(); return EndOfInput_();
} }
} // namespace parse } // namespace parse
......
...@@ -36,6 +36,8 @@ class Vector { ...@@ -36,6 +36,8 @@ class Vector {
// move constructor throws, the Vector is left in an inconsistent state. This is acceptable // move constructor throws, the Vector is left in an inconsistent state. This is acceptable
// under KJ exception theory which assumes that exceptions leave things in inconsistent states. // under KJ exception theory which assumes that exceptions leave things in inconsistent states.
// TODO(someday): Allow specifying a custom allocator.
public: public:
inline Vector() = default; inline Vector() = default;
inline explicit Vector(size_t capacity): builder(heapArrayBuilder<T>(capacity)) {} inline explicit Vector(size_t capacity): builder(heapArrayBuilder<T>(capacity)) {}
...@@ -59,6 +61,14 @@ public: ...@@ -59,6 +61,14 @@ public:
inline T& front() { return builder.front(); } inline T& front() { return builder.front(); }
inline T& back() { return builder.back(); } inline T& back() { return builder.back(); }
inline Array<T> releaseAsArray() {
// TODO(perf): Avoid a copy/move by allowing Array<T> to point to incomplete space?
if (!builder.isFull()) {
setCapacity(size());
}
return builder.finish();
}
template <typename... Params> template <typename... Params>
inline void add(Params&&... params) { inline void add(Params&&... params) {
if (builder.isFull()) grow(); if (builder.isFull()) grow();
...@@ -69,10 +79,13 @@ private: ...@@ -69,10 +79,13 @@ private:
ArrayBuilder<T> builder; ArrayBuilder<T> builder;
void grow() { void grow() {
size_t newSize = capacity() == 0 ? 4 : capacity() * 2; setCapacity(capacity() == 0 ? 4 : capacity() * 2);
}
void setCapacity(size_t newSize) {
ArrayBuilder<T> newBuilder = heapArrayBuilder<T>(newSize); ArrayBuilder<T> newBuilder = heapArrayBuilder<T>(newSize);
for (T& element: builder) { size_t moveCount = kj::min(newSize, builder.size());
newBuilder.add(kj::mv(element)); for (size_t i = 0; i < moveCount; i++) {
newBuilder.add(kj::mv(builder[i]));
} }
builder = kj::mv(newBuilder); builder = kj::mv(newBuilder);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment