|
| 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() |
0 commit comments