This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gqlast.py
executable file
·862 lines (698 loc) · 30.3 KB
/
gqlast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#!/bin/python3
'''Graphql format manipulation
Usage:
gqlast.py [--debug] [--dgraph] [--nv] [FILE ...]
Parse the FILE input and apply transformations:
* Add interface attributes on implemented types.
* remove duplicate type and inherits input arguments.
* move/copy directives based on their name (see graphql/directives.graphql).
* Remove comments.
Options:
-d --debug Show debug informations.
--dgraph Filter schema for dgraph.
--nv Silent output.
'''
import sys
import re
import itertools
from loguru import logger
from collections import OrderedDict, defaultdict
from copy import deepcopy
from pprint import pprint
from docopt import docopt
from tatsu import compile
from tatsu.util import asjsons
from tatsu.ast import AST
from gram.graphql import GRAPHQLParser
sys.setrecursionlimit(10**4)
_dgraph_directives = ['id', 'search', 'hasInverse', 'remote', 'custom', 'auth', 'lambda', 'generate', 'secret', 'dgraph', 'default', 'cacheControl', 'withSubscription']
_hook_prefix = "hook_"
_input_names = ["input", "filter"]
# IMPROVMENT:
# * Show the line when an assert error occurs...
class AST2(AST):
# see https://github.com/neogeny/TatSu/issues/164#issuecomment-609044281
# Created because of the need of _ast_set to keep order...
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def clear(self):
for k in list(self):
del self[k]
class SemanticFilter:
''' Semantic based on the EBNF Grammar defined at gram/graphql.ebnf '''
def __init__(self):
# Keep track of mutables object in the grammar.
self.interfaces = OrderedDict()
self.types = OrderedDict()
self.inputs = OrderedDict()
self.enums = []
self.unions = []
# Also :
# {type}__directive : [query level directives] | Seek @_hook_prefix directive
# {type}__implements : [implemented interfaces]
# Directives definition to append to the schema
self.extra_directives = []
@staticmethod
def get_name(ast):
return '' if not ast else ast._name.name
@staticmethod
def get_fields(ast):
''' Returns the fields of a object.
* remove comments
'''
if not ast._fields or len(ast._fields) != 3:
raise ValueError("""Parsing error: field not found. Please check your grammar and be cautious with multiline comments.
AST: %s""" % ast)
fields = ast._fields[1]
# Filter Comments
to_remove = [i for i, f in enumerate(fields) if not f.field.get('_name')]
for i in to_remove[::-1]:
fields.pop(i)
return fields
@staticmethod
def get_args(field):
''' field is an ast.
Returns an ast representing the args
'''
if field.get('args'):
assert(field.args[0] == '(')
assert(field.args[-1] == ')')
return field.args
else:
return None
@classmethod
def get_directives(cls, field):
''' field is an ast.
Returns an ast representing the directives
'''
if field.get('_directives'):
return field._directives
else:
cls._ast_set(field, '_directives', [])
return field._directives
@staticmethod
def _ast_set(ast, rule_name, value, pos=None):
assert(isinstance(ast, AST))
items = list(ast.items())
if pos:
# DEBUG AST, assume empty rule are push at the end
i = list(ast).index(rule_name)
items.pop(i)
assert(list(ast)[pos] != rule_name)
assert(i > pos)
items = items[:pos] + [(rule_name, value)] + items[pos:]
ast.clear()
for k, _v in items:
v = _v
if k == rule_name:
v = value
ast[k] = v
def populate_data(self, data_type, name, ast, filter_directives=True):
# LOG DEBUG
#print('Populate: %s %s' % (data_type, name))
data = getattr(self, data_type)
data[name] = []
self._populate_data(data, name, ast, filter_directives=filter_directives)
return
def _populate_data(self, data, name, ast, filter_directives=True):
''' Populate data from ast parsing. '''
# Populate Types Directives
data[name+'__directives'] = []
if ast.get('_directives'):
to_remove = []
for i, d in enumerate(ast._directives):
if not d:
continue
data[name+'__directives'].append(d)
if d._name.name == _hook_prefix:
to_remove.append(i)
for i in to_remove[::-1]:
ast._directives.pop(i)
# add interfaces info
if ast.get('_implements'):
data[name+'__implements'] = ast._implements[1].name
# Populate fields
fields = self.get_fields(ast)
for f in fields:
self._push_field(name, f, data, filter_directives)
return
def _push_field(self, name, f, data, filter_directives=False, update=False):
field = f.field
# Add field
field_data = {'name': self.get_name(field),
'args': self.get_args(field), # list of AST
'directives': [], # list of AST
# -- keep pointer to propagate modifications
'ast': f, # AST
}
# Feed and filter directives
if field.get('_directives'):
to_remove = []
for i, d in enumerate(field._directives):
if not d:
continue
# build directive lookup
dn = self.get_name(d)
field_data['directives'].append({
'name': dn,
'ast': d,
})
if dn.startswith('x_') or dn.startswith('w_'):
to_remove.append(i)
# filter directives
if filter_directives:
for i in to_remove[::-1]:
field._directives.pop(i)
if update:
# There should be at list one field already in this object.
# extra will be added athe end of the stringify functions for this field
data[name][-1]['ast']['extra'] = f
else:
data[name].append(field_data)
return field_data
def inherit_interface(self, ast):
'''Inherits implemented interface '''
if not ast._implements:
return
if len(ast._implements) > 2:
# @debug: multiple inheritance will break.
raise NotImplementedError('Review this code for multiple inheritance.')
else:
interface_name = ast._implements[1].name
# LOG DEBUG
#print('%s Inheriting interface %s : ' % (ast._name.name, interface_name))
#pprint(self.interfaces[interface_name])
# Get ast fields...
fields = self.get_fields(ast)
field_names = [self.get_name(f.field) for f in fields]
for itf_fd in self.interfaces[interface_name]:
fd = itf_fd['ast']
name = self.get_name(fd.field)
if name in field_names:
continue
# LOG DEBUG
#print('%s inherited %s field from %s' % (ast._name.name, name, interface_name))
# Inherit a field
# deepcopy prevent the parent AST to be modified later, when
# working on the child AST.
fields.append(deepcopy(fd))
# Current field
curfd = [x.field for x in fields if name == self.get_name(x.field)][0]
# Inherit a directive
directives = itf_fd['directives']
if not curfd._directives and directives:
self._ast_set(curfd, '_directives', [x['ast'] for x in directives])
# LOG DEBUG
#print('%s inherited %s directive from %s' % (curfd._name.name, len(directives), interface_name))
return
def inherit_interface_dgraph(self, ast):
'''Inherits implemented interface.
* if field is already defined in interface, removed it. Dgraph will throw an error otherwie.
* If type if empty add a dummy field.
'''
if not ast._implements:
return
if len(ast._implements) > 2:
raise NotImplementedError('Review this code for multiple inheritance.')
else:
interface_name = ast._implements[1].name
# Get ast fields
fields = self.get_fields(ast)
fd_names = [self.get_name(f['ast'].field) for f in self.interfaces[interface_name]]
to_remove = []
for i, f in enumerate(fields):
if self.get_name(f.field) in fd_names:
to_remove.append(i)
for i in to_remove[::-1]:
fields.pop(i)
if len(fields) == 0:
# Dgraph need at least one field.
fields.append(AST(field='_VOID: String'))
return
def copy_directives(self, name_in, data_types_in,
name_out, data_type_out,
directive_name, set_default=False, with_args=False):
_fields = None
for data_type in data_types_in:
data_in = getattr(self, data_type)
if name_in in data_in:
_fields = data_in[name_in]
# LOG DEBUG
#print('Entering input copy %s -> %s ' % (name_in, name_out))
break
if not _fields:
logger.warning('Type `%s` unknown' % name_in)
return
data_out = getattr(self, data_type_out)
for f in data_out[name_out]:
for _f in _fields:
if f['name'] != _f['name']:
continue
for d in _f['directives']:
dn = d['name']
if re.search(directive_name, dn) and (not with_args or with_args and d['ast']._args):
if not f['ast'].field._directives:
self._ast_set(f['ast'].field, '_directives', [])
f['ast'].field['_directives'].append(d['ast'])
# LOG DEBUG
#print('directives %s copied in %s' % (d['ast']._name.name, name_out+'.'+self.get_name(f['ast'].field)))
if set_default and not f['ast'].field._directives:
# Protect the object from Patch queries by default...
ro = AST2({'_cst__bb': '@', '_name': AST2({'name': 'x_patch_ro'}), '_args': None})
self._ast_set(f['ast'].field, '_directives', [ro])
return
def copy_hook_directives(self, data_types_in, name_out, data_type_out):
''' Copy Special directive as pre and post hook that start by {_hook_prefix}*
in mutation corresponding types. '''
for data_type in data_types_in:
data_in = getattr(self, data_type)
data_out = getattr(self, data_type_out)
for f in data_out[name_out]:
m = re.match(r'(query|get|add|update|delete)(\w*)', f['name'])
if not m:
# unnkow query
continue
groups = m.groups()
op = groups[0]
type_ = groups[1]
if type_ in data_in:
for directive_ in data_in[type_ + '__directives']:
if directive_._name.name != _hook_prefix:
continue
pre_directive = directive_.copy()
post_directive = directive_.copy()
# Add Pre Hook (Input) (Query + Mutations)
pre_directive_name = _hook_prefix + op + type_ + 'Input'
self._ast_set(pre_directive, '_name', pre_directive_name) # @warning: breaks the original Grammar syntax.
args = list(self.get_args(f['ast'].field)) # tuple are non mutable !
# Directive should apply on either input or filter argument (not on eventual upsert)
for i, a in enumerate(args):
if isinstance(a, dict) and hasattr(a, "field") and self.get_name(a.field) in _input_names:
args.insert(i+1, pre_directive)
break
self._ast_set(f['ast'].field, 'args', args)
# Push the directive definition
if pre_directive_name not in self.extra_directives:
directive_definition = "directive @%s on ARGUMENT_DEFINITION" % (pre_directive_name)
self.extra_directives.append(directive_definition)
# Only add Post Hook for Mutation queries
if op in ('add', 'update', 'delete'):
# Add Post Hook (Query or Mutation Field)
post_directive_name = _hook_prefix + op + type_
self._ast_set(post_directive, '_name', post_directive_name) # @warning: breaks the original Grammar syntax.
#post_directive['cst'] = post_directive_name
post_directives = self.get_directives(f['ast'].field)
post_directives.insert(len(post_directives)-1, post_directive)
# Push the directive definition
if post_directive_name not in self.extra_directives:
directive_definition = "directive @%s on FIELD_DEFINITION" % (post_directive_name)
self.extra_directives.append(directive_definition)
def update_fields(self, data_type, name, ast):
""" Add new fields if not present on object.
Update arguments eventually.
"""
data = getattr(self, data_type)
field_names = [x.get('name') for x in data[name]]
interface_name = data.get(name+'__implements')
if interface_name and data_type != "interfaces":
field_names += [x.get('name') for x in getattr(self, 'interfaces')[interface_name]]
# LOG DEBUG
#print('Updating Doublon: %s interface: %s, fields: %s' % (name, interface_name, field_names))
for f in data[name]:
# Iterates over the fields of the 'duplicated' object <f>
for _ff in self.get_fields(ast):
_field = _ff.field
_name = self.get_name(_field)
if _name not in field_names and _name not in ['_VOID']:
# Add a new field.
self._push_field(name, _ff, data, update=True)
field_names.append(_name)
elif f['name'] == _name:
# Update a field
args = f['args']
new_args = self.get_args(_field)
if not args and new_args:
# Update args(input/filter); if the arguments don't already exists
# and if the new field has non empty arguments.
self._ast_set(f['ast'].field, 'args', new_args)
# We don't need that anymore since the ASR is ordered now ?
#pos = list(_field).index('args')
#self._ast_set(f['ast'].field, 'args', new_args, pos)
return
class GraphqlSemantics:
''' Base GQL semantic'''
def __init__(self):
self.sf = SemanticFilter()
def _default(self, ast):
if isinstance(ast, AST):
ast = AST2(ast)
return ast
def CHARACTER(self, ast):
ast = AST(_join=''.join(ast._join))
return ast
def int_value(self, ast):
flatten = itertools.chain.from_iterable
ast = AST(_join=''.join(flatten(ast._join)))
return ast
def float_value(self, ast):
flatten = itertools.chain.from_iterable
ast = AST(_join=''.join(flatten(ast._join)))
return ast
class GqlgenSemantics(GraphqlSemantics):
'''Gqlgen Semantic
* filter-out dgraph directive
* change interface to normal type to avoid trouble...
* copy fields of implemented type to comply with the Ggqlgen schema semantic.
* copy new argument/filter eventually present in doublon.
* filter doublon
'''
def interface_type_definition(self, ast):
''' Interface handle
* filter out doublon
'''
assert(isinstance(ast, AST))
ast = AST2(ast)
name = self.sf.get_name(ast)
# Watch out duplicate !
if name in self.sf.interfaces:
self.sf.update_fields('interfaces', name, ast)
return None
else:
self.sf.populate_data('interfaces', name, ast)
# rename interface to type for gqlgen compatibility !
self.sf._ast_set(ast, '_cst', 'type')
return ast
def object_type_definition(self, ast):
''' Type handle
* add or updated (doublon) types: Doublon occurs because Type are present twice, once from the file
where the type is defined, and twice from the generated schema from dgraph.
We need both because, the original bring the magixc query and input directive
while Dgraph can bring new properties.
* inherit from interfaces fields and directives if not already presents
'''
assert(isinstance(ast, AST))
ast = AST2(ast)
name = self.sf.get_name(ast)
# Watch out duplicate !
if name in self.sf.types:
self.sf.update_fields('types', name, ast)
return None
else:
self.sf.inherit_interface(ast)
self.sf.populate_data('types', name, ast)
# remove interface gqlgen compatibility !
self.sf._ast_set(ast, '_implements', None)
if name in ('Mutation', 'Query'):
self.sf.copy_hook_directives(['types', 'interfaces'], name, 'types')
return ast
def input_object_type_definition(self, ast):
''' Input handle
* filter out doublon
* add filtered directive
- @x_* directive work with *Patch input (we assumed that AddInput are managed by the BLA).
- @w_* directive work with Add*Input, *Patch and *Filter inputs (used to alter a input field).
'''
assert(isinstance(ast, AST))
ast = AST2(ast)
name = self.sf.get_name(ast)
# Watch out duplicate !
if name in self.sf.inputs:
return None
else:
self.sf.populate_data('inputs', name, ast, filter_directives=False)
type_name = None
if name.startswith('Add') and name.endswith('Input'):
# This match the fields for the 'Add' mutations
# - only copy directive x_* with arguments as add input are allowed by default.
type_name = re.match(r'Add(\w*)Input', name).groups()[0]
if type_name:
self.sf.copy_directives(type_name, ['types', 'interfaces'], name, 'inputs', r'^w_(add|alter)')
self.sf.copy_directives(type_name, ['types', 'interfaces'], name, 'inputs', r'^x_(add|alter)', with_args=True)
elif name.endswith('Patch'):
# This match the `input` field for the 'Update' and 'Remove' mutations
# - set_defaut protect field with no auth directives as read_only
type_name = re.match(r'(\w*)Patch', name).groups()[0]
if type_name:
self.sf.copy_directives(type_name, ['types', 'interfaces'], name, 'inputs', r'^w_(?!add)')
self.sf.copy_directives(type_name, ['types', 'interfaces'], name, 'inputs', r'^x_(?!add)', set_default=True)
elif name.endswith('Filter'):
# This match the `filter` field for the 'Update' and 'Remove' mutations
type_name = re.match(r'(\w*)Filter', name).groups()[0]
# Ignore Unions
if type_name and not type_name in self.sf.unions:
self.sf.copy_directives(type_name, ['types', 'interfaces'], name, 'inputs', r'^w_')
elif name.endswith('Ref'):
# This match the fields for the 'Update' and 'Remove' mutations
type_name = re.match(r'(\w*)Ref', name).groups()[0]
# Ignore Unions
if type_name and not type_name in self.sf.unions:
self.sf.copy_directives(type_name, ['types', 'interfaces'], name, 'inputs', r'^w_')
self.sf.copy_directives(type_name, ['types', 'interfaces'], name, 'inputs', r'^x_')
return ast
def enum_type_definition(self, ast):
''' Enum handle
* filter out doublon
'''
assert(not isinstance(ast, AST))
name = ast[1].name
# Watch out duplicate !
if name in self.sf.enums:
return None
else:
self.sf.enums.append(name)
return ast
def union_type_definition(self, ast):
''' Union handle
* filter out doublon
'''
assert(not isinstance(ast, AST))
name = ast[1].name
# Watch out duplicate !
if name in self.sf.unions:
return None
else:
self.sf.unions.append(name)
return ast
def directive(self, ast):
''' Filter non-dgraph directive. '''
if ast._name.name in _dgraph_directives:
return ''
else:
return ast
class DgraphSemantics(GraphqlSemantics):
'''Dgraph semantic.
* filter-out non-dgraph directives
* copy fields of implemented type to comply with the Ggqlgen schema semantic.
* filter doublon
'''
def interface_type_definition(self, ast):
''' Interface handle
* filter or doublon
'''
assert(isinstance(ast, AST))
ast = AST2(ast)
name = self.sf.get_name(ast)
# Watch out duplicate !
if name in self.sf.interfaces:
self.sf.update_fields('interfaces', name, ast)
return None
else:
self.sf.populate_data('interfaces', name, ast)
return ast
def object_type_definition(self, ast):
'''Type handle
* filter on doublon
* add implemented interfaces fields if not already presents
'''
assert(isinstance(ast, AST))
ast = AST2(ast)
name = self.sf.get_name(ast)
# Watch out duplicate !
if name in self.sf.types:
self.sf.update_fields('types', name, ast)
return None
else:
# Here, this method will remove attribute.
# Dgraph want the same field of the interface or nothing.
self.sf.populate_data('types', name, ast)
self.sf.inherit_interface_dgraph(ast)
return ast
def directive(self, ast):
''' Filter out non-dgraph directive. '''
if ast._name.name in _dgraph_directives:
return ast
else:
return ''
class SDL:
'''Parse graphql file with semantics.
The module interpret the rule name given by tatsu
(with the synxax `rule_name:rule`) with the following semantics:
* if rule_name starts with '_', it will be appended to
the output with no special treatment.
* rule_name can be defined as `name__code` where code
can be [bb, bs, bs] that stands respectively for:
* blank before
* blank after
* blank surrounded
* `name` has a special treatment to manage space syntax.
* `comment` are filtered out.
* `args` do not make new line.
* other rule are appended with a new line,
notably the `field` rule name.
Furthermore special rule are defined be Semantic class `*Semantics`.
Reports to the methods documentation for further informantion.
'''
def __init__(self, settings):
self.s = settings
if not self.s['FILE']:
raise ValueError('You must provide a GraphQL FILE argument.')
else:
infile = self.s['FILE'][0]
self._grammar = open('gram/graphql.ebnf').read()
self._target = open(infile).read()
if self.s['--dgraph']:
self.semantics = DgraphSemantics()
else:
self.semantics = GqlgenSemantics()
#self.parser = compile(self._grammar)
self.parser = GRAPHQLParser()
self.ast = self.parser.parse(self._target,
rule_name='start',
semantics=self.semantics,
parseinfo=False)
self.sf = self.semantics.sf
def stringify(self, ast=None, out=None, root=False,
_prev=None, _next=None, ignore_nl=False):
nl = '\n'
# Init
if ast is None:
root = True
ast = [
list(map(lambda x:x+'\n', self.sf.extra_directives)),
self.ast
]
out = [nl]
# filter empty things
out = [x for x in out if x != '']
for nth, o in enumerate(ast):
if isinstance(o, dict): # AST like
keys = list(o)
update = False
if len(keys) != len(set(keys)):
# @DEBUG: duplicate keys in AST (caused by AST updates!)
raise ValueError('Related to tatsu.AST issue #164..')
if len(keys) > 1:
update = True
for ith, k in enumerate(keys):
pack = k.split('__')
v = o[k]
if len(pack) == 2:
_type, code = pack
else:
_type = k
code = None
if update:
if ith > 0:
_prev = o[keys[ith-1]]
if ith < len(o) - 1:
_next = o[keys[ith+1]]
# Code filtering
if code == 'bb':
# Blank Before (space)
if out[-1] != ' ':
out.append(' ')
elif code == 'ba':
# Blank After (space)
v += ' '
elif code == 'bs':
# Blank Suround (space)
if isinstance(v, str):
if out[-1] == ' ':
v += ' '
else:
v = ' ' + v + ' '
if v is None:
# empty choice (only happen with generated parser)
continue
# type/rule_name filtering
if _type in ('comment', 'doc'):
try:
comment = ''.join(o.comment)
except:
# bug in non dgraph...@debug
continue
if o.comment and comment.startswith('# Dgraph.Authorization'):
# keep comments
out += '\n\n'
else: # ignore comments
continue
elif _type == 'args':
ignore_nl = True
elif _type in ('name'):
# Manage space between names
if out[-1] == '\n':
# field indentation
if _prev in ('{',) and out[-3][-1] != ' ':
out[-3] += ' '
out.append(' ')
elif out[-1] not in ('[', '(', '@'):
# Space separator between words.
out.append(' ')
elif _prev in ('[',):
out[-2] += ' '
# space after object definition
if _next and _next == '{':
# without AST
v += ' '
elif _next and isinstance(_next, (tuple, list)) and _next[0] == '{':
# with AST
v += ' '
elif _next and isinstance(_next, (tuple, list)) and _next[0] == 'implements':
v += ' '
#print('dict-- ', k, v, _prev, _next)
elif _type.startswith('_'):
# Don't append newline for rulename that starts with '_'.
pass
elif _type.endswith('_definition'):
# indention in field definition
out.extend([nl]*2)
else:
# newline rational
if not ignore_nl:
out.append(nl)
out = self.stringify([v], # removing list breaks the space logics
out,
_prev=_prev, _next=_next,
ignore_nl=ignore_nl,
)
elif isinstance(o, (list, tuple)):
# Assume Closure
for mth, oo in enumerate(o):
if mth < len(o)-1:
_next = o[mth+1]
if mth > 0:
_prev = o[mth-1]
out = self.stringify([oo], # removing list breaks the space logics
out,
_prev=_prev, _next=_next,
ignore_nl=ignore_nl)
elif isinstance(o, str):
if o == '}':
o = '\n'+o
out.append(o)
else:
raise NotImplementedError('Unknown type: %s' % type(o))
if root:
out = ''.join(out)
return out
if __name__ == '__main__':
args = docopt(__doc__, version='0.0')
parser = SDL(args)
sdl = parser.stringify()
if not args['--nv']:
print(sdl)
if args['--debug']:
print(args)
print()
#asj = asjsons(ast)
pprint(parser.ast, indent=2)