Skip to content

Commit 6a3693f

Browse files
author
Aurélien 'Bubu' Busi
committed
feat(build): Also support python3
1 parent 9d55a3c commit 6a3693f

11 files changed

+717
-4
lines changed

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ INCLUDE(version)
77

88
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
99

10-
FIND_PACKAGE(PythonInterp 2 REQUIRED)
11-
IF (NOT PYTHON_VERSION_MAJOR EQUAL 2)
12-
MESSAGE(FATAL_ERROR "Python 2 is required.")
13-
ENDIF()
10+
FIND_PACKAGE(PythonInterp 2)
1411

1512
FIND_PROGRAM(CTYPESGEN_FOUND ctypesgen.py)
1613

ast/ast.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ def print_ast(lang_module, input_file):
5454
if __name__ == '__main__':
5555
import sys
5656
lang = sys.argv[1]
57+
58+
try:
59+
if sys.version_info >= (3,0):
60+
lang = "%s_py3" % lang
61+
except:
62+
pass
63+
5764
filename = sys.argv[2]
5865

5966
lang_module = load_lang(lang)

ast/c_impl_py3.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) 2015-present, Facebook, Inc.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from c_py3 import field_prototype, return_type, struct_name
7+
from casing import title
8+
from license import C_LICENSE_COMMENT
9+
10+
class Printer(object):
11+
'''Printer for the implementation of the pure C interface to the AST.
12+
'''
13+
14+
def __init__(self):
15+
self._current_type = None
16+
17+
def start_file(self):
18+
print(C_LICENSE_COMMENT + '''/** @generated */
19+
20+
#include "GraphQLAst.h"
21+
#include "../Ast.h"
22+
23+
using namespace facebook::graphql::ast; // NOLINT
24+
''')
25+
26+
def end_file(self):
27+
pass
28+
29+
def start_type(self, name):
30+
self._current_type = name
31+
32+
def field(self, type, name, nullable, plural):
33+
print(field_prototype(self._current_type, type, name, nullable, plural) + ' {')
34+
print(' const auto *realNode = reinterpret_cast<const %s *>(node);' % self._current_type)
35+
title_name = title(name)
36+
call_get = 'realNode->get%s()' % title_name
37+
if plural:
38+
if nullable:
39+
print(' return %s ? %s->size() : 0;' % (call_get, call_get))
40+
else:
41+
print(' return %s.size();' % call_get)
42+
else:
43+
if type in ['string', 'OperationKind', 'boolean']:
44+
print(' return %s;' % call_get)
45+
else:
46+
fmt = ' return reinterpret_cast<const struct %s *>(%s%s);'
47+
print(fmt % (struct_name(type), '' if nullable else '&', call_get))
48+
49+
print('}')
50+
51+
def end_type(self, name):
52+
pass
53+
54+
def start_union(self, name):
55+
pass
56+
57+
def union_option(self, option):
58+
pass
59+
60+
def end_union(self, name):
61+
pass

ast/c_py3.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Copyright (c) 2015-present, Facebook, Inc.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from casing import snake
7+
8+
from license import C_LICENSE_COMMENT
9+
10+
def struct_name(type):
11+
return 'GraphQLAst' + type
12+
13+
14+
def return_type(type):
15+
if type == 'OperationKind' or type == 'string':
16+
return 'const char *'
17+
18+
if type == 'boolean':
19+
return 'int'
20+
21+
return 'const struct %s *' % struct_name(type)
22+
23+
24+
def field_prototype(owning_type, type, name, nullable, plural):
25+
st_name = struct_name(owning_type)
26+
if plural:
27+
return 'int %s_get_%s_size(const struct %s *node)' % (
28+
st_name, snake(name), st_name)
29+
else:
30+
ret_type = return_type(type)
31+
return '%s %s_get_%s(const struct %s *node)' % (
32+
ret_type, st_name, snake(name), st_name)
33+
34+
35+
class Printer(object):
36+
'''Printer for the pure C interface to the AST.
37+
38+
Merely a set of wrappers around the C++ interface; makes it possible
39+
to use the AST from C code and simplifies the task of writing
40+
bindings for other langugages.
41+
42+
The mapping is as follows:
43+
44+
- For each concrete type, you get an opaque C struct type,
45+
accessible only by pointer.
46+
47+
- For each singular field of a concrete type, you get an accessor
48+
function, returning said field in the obvious way.
49+
50+
- For each plural field of a concrete type, you get an accessor
51+
function telling you its size. For access to elements of a plural
52+
field, you can use the visitor API.
53+
54+
- For each union type, you get nothing specific (REVIEW), but you
55+
can use the visitor API to work around this entirely.
56+
57+
'''
58+
59+
def __init__(self):
60+
self._current_type = None
61+
62+
def start_file(self):
63+
print(C_LICENSE_COMMENT + '''/** @generated */
64+
65+
#pragma once
66+
67+
#ifdef __cplusplus
68+
extern "C" {
69+
#endif
70+
71+
''')
72+
73+
def end_file(self):
74+
print('''
75+
76+
#ifdef __cplusplus
77+
}
78+
#endif
79+
''')
80+
81+
def start_type(self, name):
82+
# Forward declarations for AST nodes.
83+
st_name = struct_name(name)
84+
print('struct ' + st_name + ';')
85+
self._current_type = name
86+
87+
def field(self, type, name, nullable, plural):
88+
print(field_prototype(self._current_type, type, name, nullable, plural) + ';')
89+
90+
def end_type(self, name):
91+
print()
92+
93+
def start_union(self, name):
94+
print('struct ' + struct_name(name) + ';')
95+
96+
def union_option(self, option):
97+
pass
98+
99+
def end_union(self, name):
100+
print()

ast/c_visitor_impl_py3.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (c) 2015-present, Facebook, Inc.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from casing import snake
7+
from license import C_LICENSE_COMMENT
8+
9+
class Printer(object):
10+
'''Printer for a simple list of types to be visited by the C visitor.
11+
'''
12+
13+
def __init__(self):
14+
self._types = []
15+
16+
def start_file(self):
17+
print(C_LICENSE_COMMENT + '/** @generated */')
18+
print('#define FOR_EACH_CONCRETE_TYPE(MACRO) \\')
19+
20+
def start_type(self, name):
21+
self._types.append(name)
22+
23+
def field(self, type, name, nullable, plural):
24+
pass
25+
26+
def end_type(self, name):
27+
pass
28+
29+
def end_file(self):
30+
print(' \\\n'.join('MACRO(%s, %s)' % (name, snake(name)) for name in self._types))
31+
32+
def start_union(self, name):
33+
pass
34+
35+
def union_option(self, option):
36+
pass
37+
38+
def end_union(self, name):
39+
pass

ast/cxx_impl_py3.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) 2015-present, Facebook, Inc.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from license import C_LICENSE_COMMENT
7+
8+
class Printer(object):
9+
def __init__(self):
10+
pass
11+
12+
def start_file(self):
13+
print(C_LICENSE_COMMENT + '''/** @generated */
14+
15+
#include "Ast.h"
16+
#include "AstVisitor.h"
17+
18+
namespace facebook {
19+
namespace graphql {
20+
namespace ast {
21+
''')
22+
23+
def end_file(self):
24+
print('} // namespace ast')
25+
print('} // namespace graphql')
26+
print('} // namespace facebook')
27+
28+
def start_type(self, name):
29+
print('''void %s::accept(visitor::AstVisitor *visitor) const {
30+
if (visitor->visit%s(*this)) {
31+
''' % (name, name))
32+
33+
def field(self, type, name, nullable, plural):
34+
if type in ['OperationKind', 'string', 'boolean']:
35+
return
36+
37+
if plural:
38+
accept = '{ for (const auto &x : *%s_) { x->accept(visitor); } }' % name
39+
if nullable:
40+
accept = 'if (%s_) %s' % (name, accept)
41+
print(' ' + accept)
42+
else:
43+
accept = '%s_->accept(visitor);' % name
44+
if nullable:
45+
accept = 'if (%s_) { %s }' % (name, accept)
46+
print(' ' + accept)
47+
48+
def end_type(self, name):
49+
print(''' }
50+
visitor->endVisit%s(*this);
51+
}
52+
''' % name)
53+
54+
def start_union(self, name):
55+
pass
56+
57+
def union_option(self, option):
58+
pass
59+
60+
def end_union(self, name):
61+
pass

ast/cxx_json_visitor_header_py3.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) 2016-present, Facebook, Inc.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the MIT license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
from casing import title
8+
from license import C_LICENSE_COMMENT
9+
10+
class Printer(object):
11+
def __init__(self):
12+
self._anyFieldIsANode = False
13+
14+
def start_file(self):
15+
print(C_LICENSE_COMMENT + '/** @generated */')
16+
17+
def end_file(self):
18+
pass
19+
20+
def start_type(self, name):
21+
self._anyFieldIsANode = False
22+
23+
def end_type(self, name):
24+
titleName = title(name)
25+
if self._anyFieldIsANode:
26+
print('bool visit%s(const %s &node) override;' % (titleName, titleName))
27+
print('void endVisit%s(const %s &node) override;' % (titleName, titleName))
28+
print()
29+
30+
def field(self, type, name, nullable, plural):
31+
if (not self._anyFieldIsANode and
32+
type not in ('OperationKind', 'string', 'boolean')):
33+
self._anyFieldIsANode = True
34+
35+
def start_union(self, name):
36+
pass
37+
38+
def union_option(self, option):
39+
pass
40+
41+
def end_union(self, name):
42+
pass

0 commit comments

Comments
 (0)