mirror of
https://github.com/openappsec/openappsec.git
synced 2025-09-30 03:34:26 +03:00
Feb 15th 2023 update
This commit is contained in:
1
external/graphqlparser/ast/.gitignore
vendored
Normal file
1
external/graphqlparser/ast/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.pyc
|
203
external/graphqlparser/ast/ast.ast
vendored
Normal file
203
external/graphqlparser/ast/ast.ast
vendored
Normal file
@@ -0,0 +1,203 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
|
||||
# Mini-language for AST definition.
|
||||
# All AST nodes extend AstNode.
|
||||
# All AST nodes are visitible.
|
||||
# All AST fields have a getter and a setter and are a constructor argument.
|
||||
# We have concrete types (code T) and unions (code U).
|
||||
# S for singular field, P for plural, ? for nullable.
|
||||
# O for option in a union.
|
||||
# Scalar type ontology: string, boolean
|
||||
|
||||
# Definitions other than OperationDefinition and FragmentDefinition
|
||||
# are experimental additions for schema parsing. (We don't support
|
||||
# nested unions in the AST mini-language, so I've flattened and elided
|
||||
# TypeSystemDefinition and TypeDefinition.)
|
||||
U Definition
|
||||
O OperationDefinition
|
||||
O FragmentDefinition
|
||||
O SchemaDefinition
|
||||
O ScalarTypeDefinition
|
||||
O ObjectTypeDefinition
|
||||
O InterfaceTypeDefinition
|
||||
O UnionTypeDefinition
|
||||
O EnumTypeDefinition
|
||||
O InputObjectTypeDefinition
|
||||
O TypeExtensionDefinition
|
||||
O DirectiveDefinition
|
||||
|
||||
|
||||
T Document
|
||||
P Definition definitions
|
||||
|
||||
T OperationDefinition
|
||||
S OperationKind operation
|
||||
S? Name name
|
||||
P? VariableDefinition variableDefinitions
|
||||
P? Directive directives
|
||||
S SelectionSet selectionSet
|
||||
|
||||
T VariableDefinition
|
||||
S Variable variable
|
||||
S Type type
|
||||
S? Value defaultValue
|
||||
|
||||
T SelectionSet
|
||||
P Selection selections
|
||||
|
||||
U Selection
|
||||
O Field
|
||||
O FragmentSpread
|
||||
O InlineFragment
|
||||
|
||||
T Field
|
||||
S? Name alias
|
||||
S Name name
|
||||
P? Argument arguments
|
||||
P? Directive directives
|
||||
S? SelectionSet selectionSet
|
||||
|
||||
T Argument
|
||||
S Name name
|
||||
S Value value
|
||||
|
||||
T FragmentSpread
|
||||
S Name name
|
||||
P? Directive directives
|
||||
|
||||
T InlineFragment
|
||||
S? NamedType typeCondition
|
||||
P? Directive directives
|
||||
S SelectionSet selectionSet
|
||||
|
||||
T FragmentDefinition
|
||||
S Name name
|
||||
S NamedType typeCondition
|
||||
P? Directive directives
|
||||
S SelectionSet selectionSet
|
||||
|
||||
U Value
|
||||
O Variable
|
||||
O IntValue
|
||||
O FloatValue
|
||||
O StringValue
|
||||
O BooleanValue
|
||||
O NullValue
|
||||
O EnumValue
|
||||
O ListValue
|
||||
O ObjectValue
|
||||
|
||||
T Variable
|
||||
S Name name
|
||||
|
||||
T IntValue
|
||||
S string value
|
||||
|
||||
T FloatValue
|
||||
S string value
|
||||
|
||||
T StringValue
|
||||
S string value
|
||||
|
||||
T BooleanValue
|
||||
S boolean value
|
||||
|
||||
T NullValue
|
||||
|
||||
T EnumValue
|
||||
S string value
|
||||
|
||||
T ListValue
|
||||
P Value values
|
||||
|
||||
T ObjectValue
|
||||
P ObjectField fields
|
||||
|
||||
T ObjectField
|
||||
S Name name
|
||||
S Value value
|
||||
|
||||
T Directive
|
||||
S Name name
|
||||
P? Argument arguments
|
||||
|
||||
U Type
|
||||
O NamedType
|
||||
O ListType
|
||||
O NonNullType
|
||||
|
||||
T NamedType
|
||||
S Name name
|
||||
|
||||
T ListType
|
||||
S Type type
|
||||
|
||||
T NonNullType
|
||||
# JS version prohibits nesting nonnull in nonnull, we can't because we
|
||||
# can't support multiple unions. Fix?
|
||||
S Type type
|
||||
|
||||
T Name
|
||||
S string value
|
||||
|
||||
T SchemaDefinition
|
||||
P? Directive directives
|
||||
P OperationTypeDefinition operationTypes
|
||||
|
||||
T OperationTypeDefinition
|
||||
S OperationKind operation
|
||||
S NamedType type
|
||||
|
||||
T ScalarTypeDefinition
|
||||
S Name name
|
||||
P? Directive directives
|
||||
|
||||
T ObjectTypeDefinition
|
||||
S Name name
|
||||
P? NamedType interfaces
|
||||
P? Directive directives
|
||||
P FieldDefinition fields
|
||||
|
||||
T FieldDefinition
|
||||
S Name name
|
||||
P? InputValueDefinition arguments
|
||||
S Type type
|
||||
P? Directive directives
|
||||
|
||||
T InputValueDefinition
|
||||
S Name name
|
||||
S Type type
|
||||
S? Value defaultValue
|
||||
P? Directive directives
|
||||
|
||||
T InterfaceTypeDefinition
|
||||
S Name name
|
||||
P? Directive directives
|
||||
P FieldDefinition fields
|
||||
|
||||
T UnionTypeDefinition
|
||||
S Name name
|
||||
P? Directive directives
|
||||
P NamedType types
|
||||
|
||||
T EnumTypeDefinition
|
||||
S Name name
|
||||
P? Directive directives
|
||||
P EnumValueDefinition values
|
||||
|
||||
T EnumValueDefinition
|
||||
S Name name
|
||||
P? Directive directives
|
||||
|
||||
T InputObjectTypeDefinition
|
||||
S Name name
|
||||
P? Directive directives
|
||||
P InputValueDefinition fields
|
||||
|
||||
T TypeExtensionDefinition
|
||||
S ObjectTypeDefinition definition
|
||||
|
||||
T DirectiveDefinition
|
||||
S Name name
|
||||
P? InputValueDefinition arguments
|
||||
P Name locations
|
61
external/graphqlparser/ast/ast.py
vendored
Normal file
61
external/graphqlparser/ast/ast.py
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env python
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
from importlib import import_module
|
||||
|
||||
def load_lang(lang):
|
||||
return import_module(lang).Printer()
|
||||
|
||||
|
||||
def print_ast(lang_module, input_file):
|
||||
lang_module.start_file()
|
||||
line = input_file.readline()
|
||||
while line:
|
||||
line = line.strip()
|
||||
if line.startswith('#') or not line:
|
||||
line = input_file.readline()
|
||||
continue
|
||||
|
||||
code, rest = line.split(None, 1)
|
||||
if code[0] == 'T':
|
||||
lang_module.start_type(rest)
|
||||
field_line = input_file.readline().strip()
|
||||
while field_line:
|
||||
if field_line.startswith('#'):
|
||||
field_line = input_file.readline().strip()
|
||||
continue
|
||||
field_kind, field_type, field_name = field_line.split()
|
||||
nullable = len(field_kind) > 1 and field_kind[1] == '?'
|
||||
if field_kind[0] == 'S':
|
||||
plural = False
|
||||
elif field_kind[0] == 'P':
|
||||
plural = True
|
||||
else:
|
||||
raise Error('Unknown field kind: ' + field_kind)
|
||||
lang_module.field(field_type, field_name, nullable, plural)
|
||||
field_line = input_file.readline().strip()
|
||||
lang_module.end_type(rest)
|
||||
elif code[0] == 'U':
|
||||
lang_module.start_union(rest)
|
||||
field_line = input_file.readline().strip()
|
||||
while field_line:
|
||||
option_code, option_type = field_line.split()
|
||||
if option_code != 'O':
|
||||
raise Error('Unknown code in union: ' + option_code)
|
||||
lang_module.union_option(option_type)
|
||||
field_line = input_file.readline().strip()
|
||||
lang_module.end_union(rest)
|
||||
line = input_file.readline()
|
||||
|
||||
lang_module.end_file()
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
lang = sys.argv[1]
|
||||
filename = sys.argv[2]
|
||||
|
||||
lang_module = load_lang(lang)
|
||||
|
||||
print_ast(lang_module, open(filename, 'r'))
|
100
external/graphqlparser/ast/c.py
vendored
Normal file
100
external/graphqlparser/ast/c.py
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
from casing import snake
|
||||
|
||||
from license import C_LICENSE_COMMENT
|
||||
|
||||
def struct_name(type):
|
||||
return 'GraphQLAst' + type
|
||||
|
||||
|
||||
def return_type(type):
|
||||
if type == 'OperationKind' or type == 'string':
|
||||
return 'const char *'
|
||||
|
||||
if type == 'boolean':
|
||||
return 'int'
|
||||
|
||||
return 'const struct %s *' % struct_name(type)
|
||||
|
||||
|
||||
def field_prototype(owning_type, type, name, nullable, plural):
|
||||
st_name = struct_name(owning_type)
|
||||
if plural:
|
||||
return 'int %s_get_%s_size(const struct %s *node)' % (
|
||||
st_name, snake(name), st_name)
|
||||
else:
|
||||
ret_type = return_type(type)
|
||||
return '%s %s_get_%s(const struct %s *node)' % (
|
||||
ret_type, st_name, snake(name), st_name)
|
||||
|
||||
|
||||
class Printer(object):
|
||||
'''Printer for the pure C interface to the AST.
|
||||
|
||||
Merely a set of wrappers around the C++ interface; makes it possible
|
||||
to use the AST from C code and simplifies the task of writing
|
||||
bindings for other langugages.
|
||||
|
||||
The mapping is as follows:
|
||||
|
||||
- For each concrete type, you get an opaque C struct type,
|
||||
accessible only by pointer.
|
||||
|
||||
- For each singular field of a concrete type, you get an accessor
|
||||
function, returning said field in the obvious way.
|
||||
|
||||
- For each plural field of a concrete type, you get an accessor
|
||||
function telling you its size. For access to elements of a plural
|
||||
field, you can use the visitor API.
|
||||
|
||||
- For each union type, you get nothing specific (REVIEW), but you
|
||||
can use the visitor API to work around this entirely.
|
||||
|
||||
'''
|
||||
|
||||
def __init__(self):
|
||||
self._current_type = None
|
||||
|
||||
def start_file(self):
|
||||
print C_LICENSE_COMMENT + '''/** @generated */
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
'''
|
||||
|
||||
def end_file(self):
|
||||
print '''
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
'''
|
||||
|
||||
def start_type(self, name):
|
||||
# Forward declarations for AST nodes.
|
||||
st_name = struct_name(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) + ';'
|
||||
|
||||
def end_type(self, name):
|
||||
print
|
||||
|
||||
def start_union(self, name):
|
||||
print 'struct ' + struct_name(name) + ';'
|
||||
|
||||
def union_option(self, option):
|
||||
pass
|
||||
|
||||
def end_union(self, name):
|
||||
print
|
61
external/graphqlparser/ast/c_impl.py
vendored
Normal file
61
external/graphqlparser/ast/c_impl.py
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
from c import field_prototype, return_type, struct_name
|
||||
from casing import title
|
||||
from license import C_LICENSE_COMMENT
|
||||
|
||||
class Printer(object):
|
||||
'''Printer for the implementation of the pure C interface to the AST.
|
||||
'''
|
||||
|
||||
def __init__(self):
|
||||
self._current_type = None
|
||||
|
||||
def start_file(self):
|
||||
print C_LICENSE_COMMENT + '''/** @generated */
|
||||
|
||||
#include "GraphQLAst.h"
|
||||
#include "../Ast.h"
|
||||
|
||||
using namespace facebook::graphql::ast; // NOLINT
|
||||
'''
|
||||
|
||||
def end_file(self):
|
||||
pass
|
||||
|
||||
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
|
||||
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)
|
||||
else:
|
||||
print ' return %s.size();' % call_get
|
||||
else:
|
||||
if type in ['string', 'OperationKind', 'boolean']:
|
||||
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 '}'
|
||||
|
||||
def end_type(self, name):
|
||||
pass
|
||||
|
||||
def start_union(self, name):
|
||||
pass
|
||||
|
||||
def union_option(self, option):
|
||||
pass
|
||||
|
||||
def end_union(self, name):
|
||||
pass
|
39
external/graphqlparser/ast/c_visitor_impl.py
vendored
Normal file
39
external/graphqlparser/ast/c_visitor_impl.py
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
from casing import snake
|
||||
from license import C_LICENSE_COMMENT
|
||||
|
||||
class Printer(object):
|
||||
'''Printer for a simple list of types to be visited by the C visitor.
|
||||
'''
|
||||
|
||||
def __init__(self):
|
||||
self._types = []
|
||||
|
||||
def start_file(self):
|
||||
print C_LICENSE_COMMENT + '/** @generated */'
|
||||
print '#define FOR_EACH_CONCRETE_TYPE(MACRO) \\'
|
||||
|
||||
def start_type(self, name):
|
||||
self._types.append(name)
|
||||
|
||||
def field(self, type, name, nullable, plural):
|
||||
pass
|
||||
|
||||
def end_type(self, name):
|
||||
pass
|
||||
|
||||
def end_file(self):
|
||||
print ' \\\n'.join('MACRO(%s, %s)' % (name, snake(name)) for name in self._types)
|
||||
|
||||
def start_union(self, name):
|
||||
pass
|
||||
|
||||
def union_option(self, option):
|
||||
pass
|
||||
|
||||
def end_union(self, name):
|
||||
pass
|
26
external/graphqlparser/ast/casing.py
vendored
Normal file
26
external/graphqlparser/ast/casing.py
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
def title(s):
|
||||
'''Capitalize the first character of s.'''
|
||||
return s[0].capitalize() + s[1:]
|
||||
|
||||
|
||||
def camel(s):
|
||||
'''Lowercase the first character of s.'''
|
||||
return s[0].lower() + s[1:]
|
||||
|
||||
|
||||
def snake(s):
|
||||
'''Convert from title or camelCase to snake_case.'''
|
||||
if len(s) < 2:
|
||||
return s.lower()
|
||||
out = s[0].lower()
|
||||
for c in s[1:]:
|
||||
if c.isupper():
|
||||
out += '_'
|
||||
c = c.lower()
|
||||
out += c
|
||||
return out
|
197
external/graphqlparser/ast/cxx.py
vendored
Normal file
197
external/graphqlparser/ast/cxx.py
vendored
Normal file
@@ -0,0 +1,197 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
#
|
||||
# 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 casing import title
|
||||
from license import C_LICENSE_COMMENT
|
||||
|
||||
class Printer(object):
|
||||
def __init__(self):
|
||||
self._type_name = None
|
||||
# Map concrete type to base class
|
||||
self._bases = {}
|
||||
# HACK: Defer everything we print so that forward declarations for
|
||||
# all classes come first. Avoids having to do 2 passes over the
|
||||
# input file.
|
||||
self._deferredOutput = StringIO.StringIO()
|
||||
|
||||
self._fields = []
|
||||
|
||||
def start_file(self):
|
||||
print C_LICENSE_COMMENT + '''/** @generated */
|
||||
#pragma once
|
||||
|
||||
#include "AstNode.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace facebook {
|
||||
namespace graphql {
|
||||
namespace ast {
|
||||
|
||||
// The parser uses strdup to move from yytext to the heap, so we need
|
||||
// to use free instead of delete.
|
||||
struct CDeleter {
|
||||
void operator()(const char *p) const { free((void *)p); }
|
||||
};
|
||||
'''
|
||||
|
||||
def end_file(self):
|
||||
print
|
||||
print self._deferredOutput.getvalue()
|
||||
print '}'
|
||||
print '}'
|
||||
print '}'
|
||||
|
||||
def _base_class(self, type):
|
||||
return self._bases.get(type, 'Node')
|
||||
|
||||
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)
|
||||
self._fields = []
|
||||
|
||||
def field(self, type, name, nullable, plural):
|
||||
if type == 'OperationKind':
|
||||
type = 'string'
|
||||
self._fields.append((type, name, nullable, plural))
|
||||
|
||||
def end_type(self, name):
|
||||
self._print_fields()
|
||||
print >> self._deferredOutput, ' public:'
|
||||
self._print_constructor()
|
||||
print >> self._deferredOutput
|
||||
self._print_destructor_prototype()
|
||||
print >> self._deferredOutput
|
||||
self._print_noncopyable()
|
||||
print >> self._deferredOutput
|
||||
self._print_getters()
|
||||
print >> self._deferredOutput, ' void accept(visitor::AstVisitor *visitor) const override;'
|
||||
print >> self._deferredOutput, '};'
|
||||
print >> self._deferredOutput
|
||||
print >> self._deferredOutput
|
||||
self._type_name = None
|
||||
self._fields = []
|
||||
|
||||
def _storage_type(self, type):
|
||||
if type == 'string':
|
||||
return 'std::unique_ptr<const char, CDeleter>'
|
||||
elif type == 'boolean':
|
||||
return 'bool'
|
||||
else:
|
||||
return 'std::unique_ptr<%s>' % type
|
||||
|
||||
def _print_fields(self):
|
||||
for (type, name, nullable, plural) in self._fields:
|
||||
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)
|
||||
|
||||
def _ctor_singular_type(self, type):
|
||||
if type == 'string':
|
||||
return 'const char *'
|
||||
elif type == 'boolean':
|
||||
return 'bool'
|
||||
else:
|
||||
return '%s *' % type
|
||||
|
||||
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 '')
|
||||
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, ' )'
|
||||
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)
|
||||
for (type, name, nullable, plural)
|
||||
in self._fields)
|
||||
print >> self._deferredOutput, ' {}'
|
||||
|
||||
def _getter_type(self, type, nullable, plural):
|
||||
if plural and nullable:
|
||||
return 'const std::vector<%s>*' % self._storage_type(type)
|
||||
elif plural:
|
||||
return 'const std::vector<%s>&' % self._storage_type(type)
|
||||
|
||||
if type == 'string':
|
||||
assert not nullable
|
||||
return 'const char *'
|
||||
elif type == 'boolean':
|
||||
assert not nullable
|
||||
return 'bool'
|
||||
elif nullable:
|
||||
return 'const %s*' % type
|
||||
else:
|
||||
return 'const %s&' % type
|
||||
|
||||
def _getter_value_to_return(self, raw_value, type, nullable, plural):
|
||||
if plural and nullable:
|
||||
return raw_value + '.get()'
|
||||
elif plural:
|
||||
return '*%s' % raw_value
|
||||
elif type == 'boolean':
|
||||
return raw_value
|
||||
elif nullable or type == 'string':
|
||||
return '%s.get()' % raw_value
|
||||
else:
|
||||
return '*%s' % raw_value
|
||||
|
||||
def _print_getters(self):
|
||||
for (type, name, nullable, plural) in self._fields:
|
||||
print >> self._deferredOutput, ' %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
|
||||
|
||||
def _print_destructor_prototype(self):
|
||||
print >> self._deferredOutput, ' ~%s() {}' % self._type_name
|
||||
|
||||
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)
|
||||
|
||||
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:'
|
||||
self._print_constructor()
|
||||
print >> self._deferredOutput, '};'
|
||||
print >> self._deferredOutput
|
||||
|
||||
def union_option(self, type):
|
||||
assert type not in self._bases, '%s cannot appear in more than one union!' % type
|
||||
self._bases[type] = self._type_name
|
||||
|
||||
def end_union(self, name):
|
||||
pass
|
61
external/graphqlparser/ast/cxx_impl.py
vendored
Normal file
61
external/graphqlparser/ast/cxx_impl.py
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
from license import C_LICENSE_COMMENT
|
||||
|
||||
class Printer(object):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def start_file(self):
|
||||
print C_LICENSE_COMMENT + '''/** @generated */
|
||||
|
||||
#include "Ast.h"
|
||||
#include "AstVisitor.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace graphql {
|
||||
namespace ast {
|
||||
'''
|
||||
|
||||
def end_file(self):
|
||||
print '} // namespace ast'
|
||||
print '} // namespace graphql'
|
||||
print '} // namespace facebook'
|
||||
|
||||
def start_type(self, name):
|
||||
print '''void %s::accept(visitor::AstVisitor *visitor) const {
|
||||
if (visitor->visit%s(*this)) {
|
||||
''' % (name, name)
|
||||
|
||||
def field(self, type, name, nullable, plural):
|
||||
if type in ['OperationKind', 'string', 'boolean']:
|
||||
return
|
||||
|
||||
if plural:
|
||||
accept = '{ for (const auto &x : *%s_) { x->accept(visitor); } }' % name
|
||||
if nullable:
|
||||
accept = 'if (%s_) %s' % (name, accept)
|
||||
print ' ' + accept
|
||||
else:
|
||||
accept = '%s_->accept(visitor);' % name
|
||||
if nullable:
|
||||
accept = 'if (%s_) { %s }' % (name, accept)
|
||||
print ' ' + accept
|
||||
|
||||
def end_type(self, name):
|
||||
print ''' }
|
||||
visitor->endVisit%s(*this);
|
||||
}
|
||||
''' % name
|
||||
|
||||
def start_union(self, name):
|
||||
pass
|
||||
|
||||
def union_option(self, option):
|
||||
pass
|
||||
|
||||
def end_union(self, name):
|
||||
pass
|
42
external/graphqlparser/ast/cxx_json_visitor_header.py
vendored
Normal file
42
external/graphqlparser/ast/cxx_json_visitor_header.py
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
# All rights reserved.
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
from casing import title
|
||||
from license import C_LICENSE_COMMENT
|
||||
|
||||
class Printer(object):
|
||||
def __init__(self):
|
||||
self._anyFieldIsANode = False
|
||||
|
||||
def start_file(self):
|
||||
print C_LICENSE_COMMENT + '/** @generated */'
|
||||
|
||||
def end_file(self):
|
||||
pass
|
||||
|
||||
def start_type(self, name):
|
||||
self._anyFieldIsANode = False
|
||||
|
||||
def end_type(self, name):
|
||||
titleName = title(name)
|
||||
if self._anyFieldIsANode:
|
||||
print 'bool visit%s(const %s &node) override;' % (titleName, titleName)
|
||||
print 'void endVisit%s(const %s &node) override;' % (titleName, titleName)
|
||||
print
|
||||
|
||||
def field(self, type, name, nullable, plural):
|
||||
if (not self._anyFieldIsANode and
|
||||
type not in ('OperationKind', 'string', 'boolean')):
|
||||
self._anyFieldIsANode = True
|
||||
|
||||
def start_union(self, name):
|
||||
pass
|
||||
|
||||
def union_option(self, option):
|
||||
pass
|
||||
|
||||
def end_union(self, name):
|
||||
pass
|
80
external/graphqlparser/ast/cxx_json_visitor_impl.py
vendored
Normal file
80
external/graphqlparser/ast/cxx_json_visitor_impl.py
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
# All rights reserved.
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
from casing import title
|
||||
from license import C_LICENSE_COMMENT
|
||||
|
||||
class Printer(object):
|
||||
def __init__(self):
|
||||
self._fields = []
|
||||
|
||||
def start_file(self):
|
||||
print C_LICENSE_COMMENT + '/** @generated */'
|
||||
|
||||
def end_file(self):
|
||||
pass
|
||||
|
||||
def start_type(self, name):
|
||||
self._fields = []
|
||||
|
||||
def field(self, type, name, nullable, plural):
|
||||
if type == 'OperationKind':
|
||||
type = 'string'
|
||||
self._fields.append((type, name, nullable, plural))
|
||||
|
||||
def end_type(self, name):
|
||||
titleName = title(name)
|
||||
anyFieldIsANode = any(type not in ('string, boolean')
|
||||
for (type, _, _ ,_) in self._fields)
|
||||
if anyFieldIsANode:
|
||||
print '''bool JsonVisitor::visit%s(const %s &node) {
|
||||
visitNode();
|
||||
return true;
|
||||
}
|
||||
''' % (titleName, titleName)
|
||||
print '''void JsonVisitor::endVisit%(tn)s(const %(tn)s &node) {
|
||||
NodeFieldPrinter fields(*this, "%(tn)s", node);''' % {'tn': titleName}
|
||||
|
||||
for (type, fieldName, nullable, plural) in self._fields:
|
||||
funcName = None
|
||||
if type == 'string':
|
||||
assert not plural, 'plural string fields not supported yet'
|
||||
funcName = 'printSingularPrimitiveField'
|
||||
elif type == 'boolean':
|
||||
assert not plural, 'plural boolean fields not supported yet'
|
||||
funcName = 'printSingularBooleanField'
|
||||
elif not nullable and not plural:
|
||||
# Special case: singular object fields don't need the value passed.
|
||||
print ' fields.printSingularObjectField("%s");' % fieldName
|
||||
continue
|
||||
else:
|
||||
nullable_str = 'Nullable' if nullable else ''
|
||||
plural_str = 'Plural' if plural else 'SingularObject'
|
||||
funcName = 'print%s%sField' % (nullable_str, plural_str)
|
||||
|
||||
assert funcName is not None
|
||||
print ' fields.%s("%s", node.get%s());' % (
|
||||
funcName, fieldName, title(fieldName))
|
||||
|
||||
if anyFieldIsANode:
|
||||
print '''
|
||||
endVisitNode(fields.finishPrinting());
|
||||
}
|
||||
'''
|
||||
else:
|
||||
print '''
|
||||
printed_.back().emplace_back(fields.finishPrinting());
|
||||
}
|
||||
'''
|
||||
|
||||
def start_union(self, name):
|
||||
pass
|
||||
|
||||
def union_option(self, option):
|
||||
pass
|
||||
|
||||
def end_union(self, name):
|
||||
pass
|
64
external/graphqlparser/ast/cxx_visitor.py
vendored
Normal file
64
external/graphqlparser/ast/cxx_visitor.py
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
from casing import camel, title
|
||||
from license import C_LICENSE_COMMENT
|
||||
|
||||
class Printer(object):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def start_file(self):
|
||||
print C_LICENSE_COMMENT + '''/** @generated */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Ast.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace graphql {
|
||||
namespace ast {
|
||||
namespace visitor {
|
||||
|
||||
class AstVisitor {
|
||||
public:
|
||||
virtual ~AstVisitor() {}
|
||||
'''
|
||||
|
||||
def end_file(self):
|
||||
print '};' # end AstVisitor
|
||||
print
|
||||
print '}'
|
||||
print '}'
|
||||
print '}'
|
||||
print '}'
|
||||
|
||||
def start_type(self, name):
|
||||
titleName = title(name)
|
||||
camelName = camel(titleName)
|
||||
print ' virtual bool visit%s(const %s &%s) { return true; }' % (
|
||||
titleName,
|
||||
titleName,
|
||||
camelName)
|
||||
print ' virtual void endVisit%s(const %s &%s) { }' % (
|
||||
titleName,
|
||||
titleName,
|
||||
camelName)
|
||||
print
|
||||
|
||||
def end_type(self, name):
|
||||
pass
|
||||
|
||||
def field(self, type, name, nullable, plural):
|
||||
pass
|
||||
|
||||
def start_union(self, name):
|
||||
pass
|
||||
|
||||
def union_option(self, option):
|
||||
pass
|
||||
|
||||
def end_union(self, name):
|
||||
pass
|
65
external/graphqlparser/ast/js.py
vendored
Normal file
65
external/graphqlparser/ast/js.py
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
# Copyright 2019-present, GraphQL Foundation
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
|
||||
class Printer(object):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def start_file(self):
|
||||
print '''/* @flow */
|
||||
/* @generated */
|
||||
/* jshint ignore:start */
|
||||
|
||||
/**
|
||||
* Copyright 2019-present, GraphQL Foundation
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
type Node = {
|
||||
kind: string;
|
||||
start?: ?number;
|
||||
end?: ?number;
|
||||
};
|
||||
|
||||
type OperationKind = 'query' | 'mutation' | 'subscription';'''
|
||||
|
||||
def end_file(self):
|
||||
pass
|
||||
|
||||
def start_type(self, name):
|
||||
print
|
||||
print 'type %s = Node & {' % name
|
||||
kind = name
|
||||
if kind == 'GenericType':
|
||||
kind = 'Type'
|
||||
print ' kind: \'%s\';' % kind
|
||||
|
||||
def end_type(self, name):
|
||||
print '}'
|
||||
|
||||
def _js_type(self, type, plural):
|
||||
if plural:
|
||||
type = 'Array<%s>' % type
|
||||
return type
|
||||
|
||||
def field(self, type, name, nullable, plural):
|
||||
nullable_char = '?' if nullable else ''
|
||||
js_type = self._js_type(type, plural)
|
||||
print ' %(name)s%(nullable_char)s: %(nullable_char)s%(js_type)s;' % locals()
|
||||
|
||||
def start_union(self, name):
|
||||
print ('type %s = ' % name),
|
||||
self._current_options = []
|
||||
|
||||
def union_option(self, type):
|
||||
self._current_options.append(type)
|
||||
|
||||
def end_union(self, name):
|
||||
print '\n | '.join(self._current_options)
|
||||
print
|
||||
self._current_options = None
|
10
external/graphqlparser/ast/license.py
vendored
Normal file
10
external/graphqlparser/ast/license.py
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# Copyright 2019-present GraphQL Foundation
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
C_LICENSE_COMMENT = '''/**
|
||||
* Copyright 2019-present GraphQL Foundation
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
'''
|
Reference in New Issue
Block a user