Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python3 support for building the lib. #66

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ INCLUDE(version)

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

FIND_PACKAGE(PythonInterp 2 REQUIRED)
IF (NOT PYTHON_VERSION_MAJOR EQUAL 2)
MESSAGE(FATAL_ERROR "Python 2 is required.")
ENDIF()
FIND_PACKAGE(PythonInterp 2)

FIND_PROGRAM(CTYPESGEN_FOUND ctypesgen.py)

Expand Down
1 change: 1 addition & 0 deletions ast/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def print_ast(lang_module, input_file):
lang_module.end_file()
if __name__ == '__main__':
import sys

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove extra blank line

lang = sys.argv[1]
filename = sys.argv[2]

Expand Down
20 changes: 10 additions & 10 deletions ast/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import print_function
from casing import snake

from license import C_LICENSE_COMMENT
Expand Down Expand Up @@ -60,41 +60,41 @@ def __init__(self):
self._current_type = None

def start_file(self):
print C_LICENSE_COMMENT + '''/** @generated */
print(C_LICENSE_COMMENT + '''/** @generated */

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

'''
''')

def end_file(self):
print '''
print('''

#ifdef __cplusplus
}
#endif
'''
''')

def start_type(self, name):
# Forward declarations for AST nodes.
st_name = struct_name(name)
print 'struct ' + st_name + ';'
print('struct ' + st_name + ';')
self._current_type = name

def field(self, type, name, nullable, plural):
print field_prototype(self._current_type, type, name, nullable, plural) + ';'
print(field_prototype(self._current_type, type, name, nullable, plural) + ';')

def end_type(self, name):
print
print()

def start_union(self, name):
print 'struct ' + struct_name(name) + ';'
print('struct ' + struct_name(name) + ';')

def union_option(self, option):
pass

def end_union(self, name):
print
print()
19 changes: 10 additions & 9 deletions ast/c_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import print_function

from c import field_prototype, return_type, struct_name
from casing import title
Expand All @@ -15,13 +16,13 @@ def __init__(self):
self._current_type = None

def start_file(self):
print C_LICENSE_COMMENT + '''/** @generated */
print(C_LICENSE_COMMENT + '''/** @generated */

#include "GraphQLAst.h"
#include "../Ast.h"

using namespace facebook::graphql::ast; // NOLINT
'''
''')

def end_file(self):
pass
Expand All @@ -30,23 +31,23 @@ def start_type(self, name):
self._current_type = name

def field(self, type, name, nullable, plural):
print field_prototype(self._current_type, type, name, nullable, plural) + ' {'
print ' const auto *realNode = reinterpret_cast<const %s *>(node);' % self._current_type
print(field_prototype(self._current_type, type, name, nullable, plural) + ' {')
print(' const auto *realNode = reinterpret_cast<const %s *>(node);' % self._current_type)
title_name = title(name)
call_get = 'realNode->get%s()' % title_name
if plural:
if nullable:
print ' return %s ? %s->size() : 0;' % (call_get, call_get)
print(' return %s ? %s->size() : 0;' % (call_get, call_get))
else:
print ' return %s.size();' % call_get
print(' return %s.size();' % call_get)
else:
if type in ['string', 'OperationKind', 'boolean']:
print ' return %s;' % call_get
print(' return %s;' % call_get)
else:
fmt = ' return reinterpret_cast<const struct %s *>(%s%s);'
print fmt % (struct_name(type), '' if nullable else '&', call_get)
print(fmt % (struct_name(type), '' if nullable else '&', call_get))

print '}'
print('}')

def end_type(self, name):
pass
Expand Down
8 changes: 4 additions & 4 deletions ast/c_visitor_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import print_function
from casing import snake
from license import C_LICENSE_COMMENT

Expand All @@ -14,8 +14,8 @@ def __init__(self):
self._types = []

def start_file(self):
print C_LICENSE_COMMENT + '/** @generated */'
print '#define FOR_EACH_CONCRETE_TYPE(MACRO) \\'
print(C_LICENSE_COMMENT + '/** @generated */')
print('#define FOR_EACH_CONCRETE_TYPE(MACRO) \\')

def start_type(self, name):
self._types.append(name)
Expand All @@ -27,7 +27,7 @@ def end_type(self, name):
pass

def end_file(self):
print ' \\\n'.join('MACRO(%s, %s)' % (name, snake(name)) for name in self._types)
print(' \\\n'.join('MACRO(%s, %s)' % (name, snake(name)) for name in self._types))

def start_union(self, name):
pass
Expand Down
93 changes: 48 additions & 45 deletions ast/cxx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import cStringIO as StringIO
from __future__ import print_function
try:
import cStringIO as StringIO
except ImportError:
import io as StringIO

from casing import title
from license import C_LICENSE_COMMENT
Expand All @@ -21,7 +24,7 @@ def __init__(self):
self._fields = []

def start_file(self):
print C_LICENSE_COMMENT + '''/** @generated */
print(C_LICENSE_COMMENT + '''/** @generated */
#pragma once

#include "AstNode.h"
Expand All @@ -40,14 +43,14 @@ def start_file(self):
struct CDeleter {
void operator()(const char *p) const { free((void *)p); }
};
'''
''')

def end_file(self):
print
print self._deferredOutput.getvalue()
print '}'
print '}'
print '}'
print()
print(self._deferredOutput.getvalue())
print('}')
print('}')
print('}')

def _base_class(self, type):
return self._bases.get(type, 'Node')
Expand All @@ -56,8 +59,8 @@ def start_type(self, name):
self._type_name = name
base = self._base_class(name)
# non-deferred!
print 'class %s;' % name
print >> self._deferredOutput, 'class %s : public %s {' % (name, base)
print('class %s;' % name)
print('class %s : public %s {' % (name, base), file=self._deferredOutput)
self._fields = []

def field(self, type, name, nullable, plural):
Expand All @@ -67,18 +70,18 @@ def field(self, type, name, nullable, plural):

def end_type(self, name):
self._print_fields()
print >> self._deferredOutput, ' public:'
print(' public:', file=self._deferredOutput)
self._print_constructor()
print >> self._deferredOutput
print(file=self._deferredOutput)
self._print_destructor_prototype()
print >> self._deferredOutput
print(file=self._deferredOutput)
self._print_noncopyable()
print >> self._deferredOutput
print(file=self._deferredOutput)
self._print_getters()
print >> self._deferredOutput, ' void accept(visitor::AstVisitor *visitor) const override;'
print >> self._deferredOutput, '};'
print >> self._deferredOutput
print >> self._deferredOutput
print(' void accept(visitor::AstVisitor *visitor) const override;', file=self._deferredOutput)
print('};', file=self._deferredOutput)
print(file=self._deferredOutput)
print(file=self._deferredOutput)
self._type_name = None
self._fields = []

Expand All @@ -95,7 +98,7 @@ def _print_fields(self):
storage_type = self._storage_type(type)
if plural:
storage_type = 'std::unique_ptr<std::vector<%s>>' % storage_type
print >> self._deferredOutput, ' %s %s_;' % (storage_type, name)
print(' %s %s_;' % (storage_type, name), file=self._deferredOutput)

def _ctor_singular_type(self, type):
if type == 'string':
Expand All @@ -109,28 +112,28 @@ def _ctor_plural_type(self, type):
return 'std::vector<%s> *' % self._storage_type(type)

def _print_constructor(self):
print >> self._deferredOutput, ' explicit %s(' % self._type_name
print >> self._deferredOutput, ' const yy::location &location%s' % (',' if self._fields else '')
print(' explicit %s(' % self._type_name, file=self._deferredOutput)
print(' const yy::location &location%s' % (',' if self._fields else ''), file=self._deferredOutput)
def ctor_arg(type, name, plural):
if plural:
ctor_type = self._ctor_plural_type(type)
else:
ctor_type = self._ctor_singular_type(type)
return ' %s %s' % (ctor_type, name)
print >> self._deferredOutput, ',\n'.join(ctor_arg(type, name, plural)
for (type, name, nullable, plural) in self._fields)
print >> self._deferredOutput, ' )'
print(',\n'.join(ctor_arg(type, name, plural)
for (type, name, nullable, plural) in self._fields), file=self._deferredOutput)
print(' )', file=self._deferredOutput)
def ctor_init(type, name, plural):
# Strings are const char *, just pass.
# Vectors are passed by pointer and we take ownership.
# Node types are passed in by pointer and we take ownership.
value = name
return ' %s_(%s)' % (name, value)
print >> self._deferredOutput, ' : %s(location)%s' % (self._base_class(self._type_name), ',' if self._fields else '')
print >> self._deferredOutput, ',\n'.join(ctor_init(type, name, plural)
print(' : %s(location)%s' % (self._base_class(self._type_name), ',' if self._fields else ''), file=self._deferredOutput)
print(',\n'.join(ctor_init(type, name, plural)
for (type, name, nullable, plural)
in self._fields)
print >> self._deferredOutput, ' {}'
in self._fields), file=self._deferredOutput)
print(' {}', file=self._deferredOutput)

def _getter_type(self, type, nullable, plural):
if plural and nullable:
Expand Down Expand Up @@ -163,31 +166,31 @@ def _getter_value_to_return(self, raw_value, type, nullable, plural):

def _print_getters(self):
for (type, name, nullable, plural) in self._fields:
print >> self._deferredOutput, ' %s get%s() const' % (
print(' %s get%s() const' % (
self._getter_type(type, nullable, plural),
title(name))
print >> self._deferredOutput, ' { return %s; }' % (
self._getter_value_to_return(name + '_', type, nullable, plural))
print >> self._deferredOutput
title(name)), file=self._deferredOutput)
print(' { return %s; }' % (
self._getter_value_to_return(name + '_', type, nullable, plural)), file=self._deferredOutput)
print(file=self._deferredOutput)

def _print_destructor_prototype(self):
print >> self._deferredOutput, ' ~%s() {}' % self._type_name
print(' ~%s() {}' % self._type_name, file=self._deferredOutput)

def _print_noncopyable(self):
print >> self._deferredOutput, ' %s(const %s&) = delete;' % (
self._type_name, self._type_name)
print >> self._deferredOutput, ' %s& operator=(const %s&) = delete;' % (
self._type_name, self._type_name)
print(' %s(const %s&) = delete;' % (
self._type_name, self._type_name), file=self._deferredOutput)
print(' %s& operator=(const %s&) = delete;' % (
self._type_name, self._type_name), file=self._deferredOutput)

def start_union(self, name):
self._type_name = name
# non-deferred!
print 'class %s;' % name
print >> self._deferredOutput, 'class %s : public Node {' % name
print >> self._deferredOutput, ' public:'
print('class %s;' % name)
print('class %s : public Node {' % name, file=self._deferredOutput)
print(' public:', file=self._deferredOutput)
self._print_constructor()
print >> self._deferredOutput, '};'
print >> self._deferredOutput
print('};', file=self._deferredOutput)
print(file=self._deferredOutput)

def union_option(self, type):
assert type not in self._bases, '%s cannot appear in more than one union!' % type
Expand Down
Loading