graphql-parser 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,181 @@
1
+ /**
2
+ * Copyright (c) 2015, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ */
9
+ /** @generated */
10
+
11
+ #pragma once
12
+
13
+ #ifdef __cplusplus
14
+ extern "C" {
15
+ #endif
16
+
17
+ #include <ruby.h>
18
+
19
+ void init_graphql(void);
20
+
21
+
22
+ struct GraphQLAstDefinition;
23
+ extern const rb_data_type_t definition_type;
24
+ extern VALUE definition_class;
25
+
26
+ struct GraphQLAstDocument;
27
+ extern const rb_data_type_t document_type;
28
+ extern VALUE document_class;
29
+ extern ID visit_document_id;
30
+ extern ID end_visit_document_id;
31
+
32
+ struct GraphQLAstOperationDefinition;
33
+ extern const rb_data_type_t operation_definition_type;
34
+ extern VALUE operation_definition_class;
35
+ extern ID visit_operation_definition_id;
36
+ extern ID end_visit_operation_definition_id;
37
+
38
+ struct GraphQLAstVariableDefinition;
39
+ extern const rb_data_type_t variable_definition_type;
40
+ extern VALUE variable_definition_class;
41
+ extern ID visit_variable_definition_id;
42
+ extern ID end_visit_variable_definition_id;
43
+
44
+ struct GraphQLAstSelectionSet;
45
+ extern const rb_data_type_t selection_set_type;
46
+ extern VALUE selection_set_class;
47
+ extern ID visit_selection_set_id;
48
+ extern ID end_visit_selection_set_id;
49
+
50
+ struct GraphQLAstSelection;
51
+ extern const rb_data_type_t selection_type;
52
+ extern VALUE selection_class;
53
+
54
+ struct GraphQLAstField;
55
+ extern const rb_data_type_t field_type;
56
+ extern VALUE field_class;
57
+ extern ID visit_field_id;
58
+ extern ID end_visit_field_id;
59
+
60
+ struct GraphQLAstArgument;
61
+ extern const rb_data_type_t argument_type;
62
+ extern VALUE argument_class;
63
+ extern ID visit_argument_id;
64
+ extern ID end_visit_argument_id;
65
+
66
+ struct GraphQLAstFragmentSpread;
67
+ extern const rb_data_type_t fragment_spread_type;
68
+ extern VALUE fragment_spread_class;
69
+ extern ID visit_fragment_spread_id;
70
+ extern ID end_visit_fragment_spread_id;
71
+
72
+ struct GraphQLAstInlineFragment;
73
+ extern const rb_data_type_t inline_fragment_type;
74
+ extern VALUE inline_fragment_class;
75
+ extern ID visit_inline_fragment_id;
76
+ extern ID end_visit_inline_fragment_id;
77
+
78
+ struct GraphQLAstFragmentDefinition;
79
+ extern const rb_data_type_t fragment_definition_type;
80
+ extern VALUE fragment_definition_class;
81
+ extern ID visit_fragment_definition_id;
82
+ extern ID end_visit_fragment_definition_id;
83
+
84
+ struct GraphQLAstValue;
85
+ extern const rb_data_type_t value_type;
86
+ extern VALUE value_class;
87
+
88
+ struct GraphQLAstVariable;
89
+ extern const rb_data_type_t variable_type;
90
+ extern VALUE variable_class;
91
+ extern ID visit_variable_id;
92
+ extern ID end_visit_variable_id;
93
+
94
+ struct GraphQLAstIntValue;
95
+ extern const rb_data_type_t int_value_type;
96
+ extern VALUE int_value_class;
97
+ extern ID visit_int_value_id;
98
+ extern ID end_visit_int_value_id;
99
+
100
+ struct GraphQLAstFloatValue;
101
+ extern const rb_data_type_t float_value_type;
102
+ extern VALUE float_value_class;
103
+ extern ID visit_float_value_id;
104
+ extern ID end_visit_float_value_id;
105
+
106
+ struct GraphQLAstStringValue;
107
+ extern const rb_data_type_t string_value_type;
108
+ extern VALUE string_value_class;
109
+ extern ID visit_string_value_id;
110
+ extern ID end_visit_string_value_id;
111
+
112
+ struct GraphQLAstBooleanValue;
113
+ extern const rb_data_type_t boolean_value_type;
114
+ extern VALUE boolean_value_class;
115
+ extern ID visit_boolean_value_id;
116
+ extern ID end_visit_boolean_value_id;
117
+
118
+ struct GraphQLAstEnumValue;
119
+ extern const rb_data_type_t enum_value_type;
120
+ extern VALUE enum_value_class;
121
+ extern ID visit_enum_value_id;
122
+ extern ID end_visit_enum_value_id;
123
+
124
+ struct GraphQLAstArrayValue;
125
+ extern const rb_data_type_t array_value_type;
126
+ extern VALUE array_value_class;
127
+ extern ID visit_array_value_id;
128
+ extern ID end_visit_array_value_id;
129
+
130
+ struct GraphQLAstObjectValue;
131
+ extern const rb_data_type_t object_value_type;
132
+ extern VALUE object_value_class;
133
+ extern ID visit_object_value_id;
134
+ extern ID end_visit_object_value_id;
135
+
136
+ struct GraphQLAstObjectField;
137
+ extern const rb_data_type_t object_field_type;
138
+ extern VALUE object_field_class;
139
+ extern ID visit_object_field_id;
140
+ extern ID end_visit_object_field_id;
141
+
142
+ struct GraphQLAstDirective;
143
+ extern const rb_data_type_t directive_type;
144
+ extern VALUE directive_class;
145
+ extern ID visit_directive_id;
146
+ extern ID end_visit_directive_id;
147
+
148
+ struct GraphQLAstType;
149
+ extern const rb_data_type_t type_type;
150
+ extern VALUE type_class;
151
+
152
+ struct GraphQLAstNamedType;
153
+ extern const rb_data_type_t named_type_type;
154
+ extern VALUE named_type_class;
155
+ extern ID visit_named_type_id;
156
+ extern ID end_visit_named_type_id;
157
+
158
+ struct GraphQLAstListType;
159
+ extern const rb_data_type_t list_type_type;
160
+ extern VALUE list_type_class;
161
+ extern ID visit_list_type_id;
162
+ extern ID end_visit_list_type_id;
163
+
164
+ struct GraphQLAstNonNullType;
165
+ extern const rb_data_type_t non_null_type_type;
166
+ extern VALUE non_null_type_class;
167
+ extern ID visit_non_null_type_id;
168
+ extern ID end_visit_non_null_type_id;
169
+
170
+ struct GraphQLAstName;
171
+ extern const rb_data_type_t name_type;
172
+ extern VALUE name_class;
173
+ extern ID visit_name_id;
174
+ extern ID end_visit_name_id;
175
+
176
+
177
+
178
+ #ifdef __cplusplus
179
+ }
180
+ #endif
181
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'graphql/parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'graphql-parser'
8
+ spec.version = Graphql::Parser::VERSION
9
+ spec.authors = ['Evan Huus']
10
+ spec.homepage = 'https://github.com/Shopify/graphql-parser'
11
+ spec.email = ['evan.huus@shopify.com']
12
+ spec.summary = "Ruby bindings for Facebook's libgraphqlparser."
13
+ spec.license = 'BSD-3-Clause'
14
+
15
+ spec.extensions = 'ext/graphql_parser/extconf.rb'
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rake-compiler', '~> 0.9'
24
+ spec.add_development_dependency 'minitest', '~> 5.8'
25
+ end
@@ -0,0 +1,9 @@
1
+ require 'graphql/parser/version'
2
+ require 'graphql/visitor'
3
+ require 'graphql_parser'
4
+
5
+ module GraphQL
6
+ module Parser
7
+ # Your code goes here...
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Graphql
2
+ module Parser
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'graphql_parser'
2
+
3
+ module GraphQL
4
+ class Visitor
5
+ end
6
+ end
data/script/generate ADDED
@@ -0,0 +1,12 @@
1
+ #!/bin/sh
2
+
3
+ set -x
4
+ set -e
5
+
6
+ git submodule update --init
7
+
8
+ export PYTHONPATH=$PYTHONPATH:script
9
+ GEN=libgraphqlparser/ast
10
+ OUT=ext/graphql_parser
11
+ python ${GEN}/ast.py ruby_header_gen ${GEN}/ast.ast > ${OUT}/graphql_ruby.h
12
+ python ${GEN}/ast.py ruby_impl_gen ${GEN}/ast.ast > ${OUT}/graphql_ruby.c
@@ -0,0 +1,66 @@
1
+ # Copyright (c) 2015, Facebook, Inc.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the BSD-style license found in the
5
+ # LICENSE file in the root directory of this source tree. An additional grant
6
+ # of patent rights can be found in the PATENTS file in the same directory.
7
+
8
+ from casing import snake
9
+
10
+ from license import C_LICENSE_COMMENT
11
+
12
+ from c import struct_name
13
+
14
+ class Printer(object):
15
+ '''Printer for Ruby wrapper's C header.
16
+ '''
17
+
18
+ def __init__(self):
19
+ pass
20
+
21
+ def start_file(self):
22
+ print C_LICENSE_COMMENT + '''/** @generated */
23
+
24
+ #pragma once
25
+
26
+ #ifdef __cplusplus
27
+ extern "C" {
28
+ #endif
29
+
30
+ #include <ruby.h>
31
+
32
+ void init_graphql(void);
33
+
34
+ '''
35
+
36
+ def end_file(self):
37
+ print '''
38
+
39
+ #ifdef __cplusplus
40
+ }
41
+ #endif
42
+ '''
43
+
44
+ def start_type(self, name):
45
+ print 'struct ' + struct_name(name) + ';'
46
+ print 'extern const rb_data_type_t %s_type;' % snake(name)
47
+ print 'extern VALUE %s_class;' % snake(name)
48
+ print 'extern ID visit_%s_id;' % snake(name)
49
+ print 'extern ID end_visit_%s_id;' % snake(name)
50
+
51
+ def field(self, type, name, nullable, plural):
52
+ pass
53
+
54
+ def end_type(self, name):
55
+ print
56
+
57
+ def start_union(self, name):
58
+ print 'struct ' + struct_name(name) + ';'
59
+ print 'extern const rb_data_type_t %s_type;' % snake(name)
60
+ print 'extern VALUE %s_class;' % snake(name)
61
+
62
+ def union_option(self, option):
63
+ pass
64
+
65
+ def end_union(self, name):
66
+ print
@@ -0,0 +1,211 @@
1
+ # Copyright (c) 2015, Facebook, Inc.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the BSD-style license found in the
5
+ # LICENSE file in the root directory of this source tree. An additional grant
6
+ # of patent rights can be found in the PATENTS file in the same directory.
7
+
8
+ from casing import snake, title
9
+
10
+ from license import C_LICENSE_COMMENT
11
+
12
+ from c import struct_name
13
+
14
+ def field_accessor(owning_type, type, name, nullable, plural):
15
+ preface = ' struct %s *node;\n' % struct_name(owning_type)
16
+ preface += ' TypedData_Get_Struct(self, struct %s, &%s_type, node);\n' % (
17
+ struct_name(owning_type), snake(owning_type))
18
+
19
+ call_get = "%s_get_%s" % (struct_name(owning_type), snake(name))
20
+ func = '%s_get_%s' % (snake(owning_type), snake(name))
21
+ ret = ' return '
22
+
23
+ if plural:
24
+ func += '_size'
25
+ call_get += '_size(node)'
26
+ ret += 'INT2FIX(%s)' % call_get
27
+ else:
28
+ call_get += '(node)'
29
+ if nullable:
30
+ preface += ' if (%s == NULL) return Qnil;\n' % call_get
31
+ if type in ['string', 'OperationKind']:
32
+ ret += 'rb_str_new_cstr(%s)' % call_get
33
+ elif type in ['boolean']:
34
+ ret += 'INT2BOOL(%s)' % call_get
35
+ else:
36
+ ret += 'TypedData_Wrap_Struct(%s_class, &%s_type, (void*)%s)' % (
37
+ snake(type), snake(type), call_get)
38
+
39
+ return 'static VALUE ' + func + '(VALUE self) {\n' + preface + ret + ';\n}\n'
40
+
41
+ def type_visitor_body(name):
42
+ ret = ' VALUE parent = (VALUE)user_data;\n'
43
+ ret += ' VALUE param = TypedData_Wrap_Struct(%s_class, &%s_type, (void*)%s);\n' % (
44
+ snake(name), snake(name), snake(name))
45
+ return ret
46
+
47
+ def type_visitor(name):
48
+ prototype = 'static int visit_%s(const struct GraphQLAst%s *%s, void *user_data) {\n' % (
49
+ snake(name), name, snake(name))
50
+ ret = ' return rb_funcall(parent, visit_%s_id, 1, param) != skip_children;\n' % (
51
+ snake(name))
52
+ return prototype + type_visitor_body(name) + ret + '}\n'
53
+
54
+ def type_end_visitor(name):
55
+ prototype = 'static void end_visit_%s(const struct GraphQLAst%s *%s, void *user_data) {\n' % (
56
+ snake(name), name, snake(name))
57
+ ret = ' rb_funcall(parent, end_visit_%s_id, 1, param);\n' % (
58
+ snake(name))
59
+ return prototype + type_visitor_body(name) + ret + '}\n'
60
+
61
+
62
+ class Printer(object):
63
+ '''Printer for Ruby wrapper's C implementation.
64
+ '''
65
+
66
+ def __init__(self):
67
+ self._current_type = None
68
+ self._class_init_buffer = ''
69
+ self._func_init_buffer = ''
70
+ self._sym_init_buffer = ''
71
+ self._cbs_init_buffer = ''
72
+
73
+ def start_file(self):
74
+ print C_LICENSE_COMMENT + '''/** @generated */
75
+
76
+ #include "c/GraphQLParser.h"
77
+ #include "c/GraphQLAstNode.h"
78
+ #include "c/GraphQLAstVisitor.h"
79
+ #include "c/GraphQLAst.h"
80
+
81
+ #include <ruby.h>
82
+
83
+ #include "graphql_ruby.h"
84
+
85
+ VALUE ast_class, parse_error, skip_children;
86
+
87
+ static struct GraphQLAstVisitorCallbacks cbs;
88
+
89
+ static void free_ast(void *x) {
90
+ graphql_node_free((struct GraphQLAstNode*)x);
91
+ }
92
+
93
+ static const rb_data_type_t ast_type = { "AST", {0, free_ast,}, };
94
+
95
+ static VALUE INT2BOOL(int x) {
96
+ return x ? Qtrue : Qfalse;
97
+ }
98
+
99
+ '''
100
+
101
+ def end_file(self):
102
+ print '''
103
+ static VALUE parse(VALUE self, VALUE text) {
104
+ char *input;
105
+ struct GraphQLAstNode *n;
106
+ const char *error;
107
+
108
+ input = StringValueCStr(text);
109
+
110
+ n = graphql_parse_string(input, &error);
111
+
112
+ if (n == NULL) {
113
+ VALUE exc = rb_exc_new_cstr(parse_error, error);
114
+ graphql_error_free(error);
115
+ rb_exc_raise(exc);
116
+ return Qnil;
117
+ }
118
+
119
+ return TypedData_Wrap_Struct(ast_class, &ast_type, n);
120
+ }
121
+
122
+ static VALUE accept(VALUE self, VALUE ast) {
123
+ struct GraphQLAstNode *n;
124
+
125
+ TypedData_Get_Struct(ast, struct GraphQLAstNode, &ast_type, n);
126
+
127
+ graphql_node_visit(n, &cbs, (void*)self);
128
+
129
+ return Qnil;
130
+ }
131
+
132
+ void init_graphql(void) {
133
+ VALUE module, parser, visitor, node_class;
134
+
135
+ module = rb_define_module("GraphQL");
136
+
137
+ parser = rb_define_module_under(module, "Parser");
138
+ rb_define_module_function(parser, "parse", parse, 1);
139
+
140
+ visitor = rb_define_class_under(module, "Visitor", rb_cObject);
141
+ rb_define_method(visitor, "accept", accept, 1);
142
+
143
+ parse_error = rb_define_class_under(module, "ParseError", rb_eArgError);
144
+
145
+ ast_class = rb_define_class_under(module, "AST", rb_cObject);
146
+
147
+ node_class = rb_define_class_under(module, "Node", rb_cObject);
148
+
149
+ skip_children = rb_class_new_instance(0, NULL, rb_cObject);
150
+ rb_define_const(module, "SKIP_CHILDREN", skip_children);
151
+
152
+ '''
153
+ print self._class_init_buffer
154
+ print self._func_init_buffer
155
+ print self._sym_init_buffer
156
+ print self._cbs_init_buffer
157
+ print '}'
158
+
159
+ def start_type(self, name):
160
+ # define the values declared 'extern' in the header
161
+ print 'const rb_data_type_t %s_type = { "%s", {}, };' % (
162
+ snake(name), struct_name(name))
163
+ print 'VALUE %s_class;' % snake(name)
164
+ print 'ID visit_%s_id;' % snake(name)
165
+ print 'ID end_visit_%s_id;' % snake(name)
166
+
167
+ # build the two visitor methods
168
+ print type_visitor(name)
169
+ print type_end_visitor(name)
170
+
171
+ # init the class
172
+ self._class_init_buffer += ' %s_class = rb_define_class_under(module, "%s", node_class);\n' % (
173
+ snake(name), name)
174
+
175
+ # init the symbols
176
+ self._sym_init_buffer += ' visit_%s_id = rb_intern("visit_%s");\n' % (
177
+ snake(name), snake(name))
178
+ self._sym_init_buffer += ' end_visit_%s_id = rb_intern("end_visit_%s");\n' % (
179
+ snake(name), snake(name))
180
+
181
+ # init the visitor struct
182
+ self._cbs_init_buffer += ' cbs.visit_%s = visit_%s;\n' % (
183
+ snake(name), snake(name))
184
+ self._cbs_init_buffer += ' cbs.end_visit_%s = end_visit_%s;\n' % (
185
+ snake(name), snake(name))
186
+ self._current_type = name
187
+
188
+ def field(self, type, name, nullable, plural):
189
+ owning_type = self._current_type
190
+
191
+ # build the field accessor method
192
+ print field_accessor(owning_type, type, name, nullable, plural)
193
+
194
+ # associate the method with the class
195
+ trailer = '_size' if plural else ''
196
+ self._func_init_buffer += ' rb_define_method(%s_class, "%s%s", %s_get_%s%s, 0);\n' % (
197
+ snake(owning_type), snake(name), trailer,
198
+ snake(owning_type), snake(name), trailer)
199
+
200
+ def end_type(self, name):
201
+ print
202
+
203
+ def start_union(self, name):
204
+ print 'const rb_data_type_t %s_type = { "%s", {}, };' % (snake(name), struct_name(name))
205
+ print 'VALUE %s_class;' % snake(name)
206
+
207
+ def union_option(self, option):
208
+ pass
209
+
210
+ def end_union(self, name):
211
+ print