Skip to content

Commit 7f5d35e

Browse files
committed
Don't include PEGTL from public headers
1 parent 0b118ce commit 7f5d35e

File tree

7 files changed

+51
-72
lines changed

7 files changed

+51
-72
lines changed

include/GraphQLGrammar.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,29 @@
88

99
#include <graphqlservice/GraphQLTree.h>
1010

11+
#define TAO_PEGTL_NAMESPACE tao::graphqlpeg
12+
13+
#include <tao/pegtl.hpp>
14+
#include <tao/pegtl/contrib/parse_tree.hpp>
15+
16+
#include <variant>
17+
#include <vector>
18+
#include <functional>
19+
1120
namespace facebook::graphql::peg {
1221

1322
using namespace tao::graphqlpeg;
1423

24+
struct ast_node : parse_tree::basic_node<ast_node>
25+
{
26+
std::string unescaped;
27+
};
28+
29+
struct ast_input
30+
{
31+
std::variant<std::vector<char>, std::unique_ptr<file_input<>>, std::string_view> data;
32+
};
33+
1534
template <typename Rule>
1635
void for_each_child(const ast_node& n, std::function<void(const ast_node&)>&& func)
1736
{

include/SchemaGenerator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include <graphqlservice/GraphQLService.h>
77

8+
#include "GraphQLGrammar.h"
9+
810
#include <array>
911
#include <cstdio>
1012

include/graphqlservice/GraphQLService.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ using TypeMap = std::unordered_map<std::string, std::shared_ptr<Object>>;
630630
struct SubscriptionParams
631631
{
632632
std::shared_ptr<RequestState> state;
633-
peg::ast<std::vector<char>> query;
633+
peg::ast query;
634634
std::string operationName;
635635
response::Value variables;
636636
};
@@ -666,12 +666,12 @@ using SubscriptionName = std::string;
666666
struct SubscriptionData : std::enable_shared_from_this<SubscriptionData>
667667
{
668668
explicit SubscriptionData(std::shared_ptr<OperationData>&& data, std::unordered_map<SubscriptionName, std::vector<response::Value>>&& fieldNamesAndArgs,
669-
peg::ast<std::vector<char>>&& query, std::string&& operationName, SubscriptionCallback&& callback,
669+
peg::ast&& query, std::string&& operationName, SubscriptionCallback&& callback,
670670
const peg::ast_node& selection);
671671

672672
std::shared_ptr<OperationData> data;
673673
std::unordered_map<SubscriptionName, std::vector<response::Value>> fieldNamesAndArgs;
674-
peg::ast<std::vector<char>> query;
674+
peg::ast query;
675675
std::string operationName;
676676
SubscriptionCallback callback;
677677
const peg::ast_node& selection;

include/graphqlservice/GraphQLTree.h

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,26 @@
33

44
#pragma once
55

6-
#define TAO_PEGTL_NAMESPACE tao::graphqlpeg
7-
8-
#include <tao/pegtl.hpp>
9-
#include <tao/pegtl/contrib/parse_tree.hpp>
10-
11-
#include <vector>
6+
#include <memory>
127
#include <string_view>
13-
#include <functional>
148

159
namespace facebook::graphql {
1610
namespace peg {
1711

18-
using namespace tao::graphqlpeg;
12+
struct ast_node;
13+
struct ast_input;
1914

20-
struct ast_node
21-
: parse_tree::basic_node<ast_node>
22-
{
23-
std::string unescaped;
24-
};
25-
26-
template <typename Input>
2715
struct ast
2816
{
29-
ast() = default;
30-
ast(ast&& other) = default;
31-
~ast();
32-
33-
ast& operator=(ast&& other) = default;
34-
35-
Input input;
36-
std::unique_ptr<ast_node> root;
17+
std::shared_ptr<ast_input> input;
18+
std::shared_ptr<ast_node> root;
3719
};
3820

39-
ast<std::vector<char>> parseString(std::string_view input);
40-
ast<std::unique_ptr<file_input<>>> parseFile(std::string_view filename);
21+
ast parseString(std::string_view input);
22+
ast parseFile(std::string_view filename);
4123

4224
} /* namespace peg */
4325

44-
peg::ast<const char*> operator "" _graphql(const char* text, size_t size);
26+
peg::ast operator "" _graphql(const char* text, size_t size);
4527

4628
} /* namespace facebook::graphql */

samples/today/sample.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,11 @@ int main(int argc, char** argv)
5858

5959
try
6060
{
61-
const peg::ast_node* ast = nullptr;
62-
peg::ast<std::vector<char>> ast_input;
63-
peg::ast<std::unique_ptr<peg::file_input<>>> ast_file;
61+
peg::ast query;
6462

6563
if (argc > 1)
6664
{
67-
ast_file = peg::parseFile(argv[1]);
68-
ast = ast_file.root.get();
65+
query = peg::parseFile(argv[1]);
6966
}
7067
else
7168
{
@@ -77,11 +74,10 @@ int main(int argc, char** argv)
7774
input.append(line);
7875
}
7976

80-
ast_input = peg::parseString(std::move(input));
81-
ast = ast_input.root.get();
77+
query = peg::parseString(std::move(input));
8278
}
8379

84-
if (!ast)
80+
if (!query.root)
8581
{
8682
std::cerr << "Unknown error!" << std::endl;
8783
std::cerr << std::endl;
@@ -90,7 +86,7 @@ int main(int argc, char** argv)
9086

9187
std::cout << "Executing query..." << std::endl;
9288

93-
std::cout << response::toJSON(service->resolve(nullptr, *ast, ((argc > 2) ? argv[2] : ""), response::Value(response::Type::Map)).get()) << std::endl;
89+
std::cout << response::toJSON(service->resolve(nullptr, *query.root, ((argc > 2) ? argv[2] : ""), response::Value(response::Type::Map)).get()) << std::endl;
9490
}
9591
catch (const std::runtime_error& ex)
9692
{

src/GraphQLService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ void OperationDefinitionVisitor::visit(std::launch launch, const std::string & o
12751275
}
12761276

12771277
SubscriptionData::SubscriptionData(std::shared_ptr<OperationData> && data, std::unordered_map<SubscriptionName, std::vector<response::Value>> && fieldNamesAndArgs,
1278-
peg::ast<std::vector<char>> && query, std::string && operationName, SubscriptionCallback && callback,
1278+
peg::ast && query, std::string && operationName, SubscriptionCallback && callback,
12791279
const peg::ast_node & selection)
12801280
: data(std::move(data))
12811281
, fieldNamesAndArgs(std::move(fieldNamesAndArgs))

src/GraphQLTree.cpp

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -637,57 +637,37 @@ template <> const std::string ast_control<enum_type_extension_content>::error_me
637637
template <> const std::string ast_control<input_object_type_extension_content>::error_message = "Expected https://facebook.github.io/graphql/June2018/#InputObjectTypeExtension";
638638
template <> const std::string ast_control<document_content>::error_message = "Expected https://facebook.github.io/graphql/June2018/#Document";
639639

640-
template <>
641-
ast<std::vector<char>>::~ast()
640+
ast parseString(std::string_view input)
642641
{
643-
// The default destructor gets inlined and may use a different allocator to free ast<>'s member
644-
// variables than the graphqlservice module used to allocate them. So even though this could be
645-
// omitted, declare it explicitly and define it in graphqlservice.
646-
}
647-
648-
template <>
649-
ast<std::unique_ptr<file_input<>>>::~ast()
650-
{
651-
// The default destructor gets inlined and may use a different allocator to free ast<>'s member
652-
// variables than the graphqlservice module used to allocate them. So even though this could be
653-
// omitted, declare it explicitly and define it in graphqlservice.
654-
}
655-
656-
template <>
657-
ast<const char*>::~ast()
658-
{
659-
// The default destructor gets inlined and may use a different allocator to free ast<>'s member
660-
// variables than the graphqlservice module used to allocate them. So even though this could be
661-
// omitted, declare it explicitly and define it in graphqlservice.
662-
}
663-
664-
ast<std::vector<char>> parseString(std::string_view input)
665-
{
666-
ast<std::vector<char>> result{ { input.cbegin(), input.cend() }, nullptr };
667-
memory_input<> in(result.input.data(), result.input.size(), "GraphQL");
642+
ast result{ std::make_shared<ast_input>(ast_input{ std::vector<char>{ input.cbegin(), input.cend() } }), {}};
643+
const auto& data = std::get<std::vector<char>>(result.input->data);
644+
memory_input<> in(data.data(), data.size(), "GraphQL");
668645

669646
result.root = parse_tree::parse<document, ast_node, ast_selector, nothing, ast_control>(std::move(in));
670647

671648
return result;
672649
}
673650

674-
ast<std::unique_ptr<file_input<>>> parseFile(std::string_view filename)
651+
ast parseFile(std::string_view filename)
675652
{
676-
auto in = std::make_unique<file_input<>>(std::string(filename));
677-
ast<std::unique_ptr<file_input<>>> result { std::move(in), nullptr };
653+
ast result{ std::make_shared<ast_input>(ast_input{ std::make_unique<file_input<>>(filename) }), {} };
654+
auto& in = *std::get<std::unique_ptr<file_input<>>>(result.input->data);
678655

679-
result.root = parse_tree::parse<document, ast_node, ast_selector, nothing, ast_control>(std::move(*result.input));
656+
result.root = parse_tree::parse<document, ast_node, ast_selector, nothing, ast_control>(std::move(in));
680657

681658
return result;
682659
}
683660

684661
} /* namespace peg */
685662

686-
peg::ast<const char*> operator "" _graphql(const char* text, size_t size)
663+
peg::ast operator "" _graphql(const char* text, size_t size)
687664
{
688665
peg::memory_input<> in(text, size, "GraphQL");
689666

690-
return { text, peg::parse_tree::parse<peg::document, peg::ast_node, peg::ast_selector, peg::nothing, peg::ast_control>(std::move(in)) };
667+
return {
668+
std::make_shared<peg::ast_input>(peg::ast_input{ { std::string_view{ text, size } } }),
669+
peg::parse_tree::parse<peg::document, peg::ast_node, peg::ast_selector, peg::nothing, peg::ast_control>(std::move(in))
670+
};
691671
}
692672

693673
} /* namespace facebook::graphql */

0 commit comments

Comments
 (0)