-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsaw.py
More file actions
executable file
·2043 lines (1552 loc) · 57.1 KB
/
csaw.py
File metadata and controls
executable file
·2043 lines (1552 loc) · 57.1 KB
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
csaw by Robin Allen <r@foon.uk>
Licence
=======
You may redistribute csaw as long as the original author remains
acknowledged as above, and this licence text is included
unmodified.
If you use csaw to build your software, an acknowledgement in
the product documentation would be appreciated but is not
required.
Areas where csaw needs some help
================================
Nested classes
Use #pragma depends Parent if you use Parent::x
Return nested type
Always fully-qualify return types, e.g.
class C { struct T {}; C::T f() {} };
Templates
No way of knowing whether template params need to be fully
defined or not. We just assume they do.
Idea: Maybe introduce something like
#pragma depends<T> vector<T> T
Preprocessor
============
Not supported. Preprocess your files before csaw sees them. One
custom directive is supported, #pragma depends, which marks all items
in a file as depending on a specific name.
We don't read function bodies, so preprocessor directives inside a
function will make it through csaw unchanged.
Qt
==
Pass -qt and Q_OBJECT will be recognized. This makes the emitted
source file suitable for processing with moc.
"""
from collections import defaultdict
import concurrent.futures
import contextlib
import threading
import functools
import argparse
import string
import array
import sys
import re
import os
line_directives_enabled = True
qt_enabled = False
TComment = sys.intern('Comment')
TString = sys.intern('String')
TWord = sys.intern('Word')
TPunctuation = sys.intern('Punctuation')
TNumber = sys.intern('Number')
TDirective = sys.intern('Directive')
TEnd = sys.intern('End')
local = threading.local()
local.RecordScope = []
def fatal(msg):
print('\n--- FATAL ERROR ---', file=sys.stderr)
for item in local.RecordScope:
print('While parsing ', item, file=sys.stderr)
print(msg, file=sys.stderr)
raise Exception(msg)
sys.exit(1)
@contextlib.contextmanager
def to_file(path_or_file, mode):
if isinstance(path_or_file, str):
with open(path_or_file, mode, encoding='utf-8') as f:
yield f
else:
yield path_or_file
@contextlib.contextmanager
def append(xs, x):
xs.append(x)
yield
xs.pop()
class Token:
def __init__(self, lexer, type_, index, length):
self.lexer = lexer
self.type = type_
self.index = index
self.length = length
self.mapped_line_offset = 0
self.mapped_path = None
@property
def text(self):
if self.type == TEnd:
return 'end of file'
return self.lexer.source_text[self.index:self.index+self.length]
@property
def text_with_whitespace(self):
if self.type == TEnd:
return 'end of file'
i = self.index
j = self.index + self.length
while j < len(self.lexer.source_text) and self.lexer.source_text[j].isspace():
j += 1
return self.lexer.source_text[i:j]
@property
def real_line(self):
if self.type == TEnd:
return self.lexer.line_map[-1] + 1
return self.lexer.line_map[self.index]
@property
def line(self):
return self.real_line + self.mapped_line_offset
@property
def path(self):
return self.mapped_path or self.lexer.input_path
@property
def line_directive(self):
return '#line %i "%s"\n' % (self.line, self.path.replace('\\', '\\\\'))
def __repr__(self):
return '%s "%s"' % (self.type, self.text)
class Lexer:
regexes = [
(None, re.compile('|'.join([
r'\s+',
r'//.*?(\n|$)',
r'/\*(.|\n)*?\*/'
]))),
(TWord, re.compile(r'@?[A-Za-z_][A-Za-z_0-9]*')),
(TString, re.compile('|'.join([
r'"([^"\\]|\\.)*"',
r"'([^'\\]|\\.)*'"
]))),
(TDirective, re.compile(r"^#[A-Za-z0-9_]+", re.MULTILINE)),
(TPunctuation, re.compile('|'.join([
r'<<',
r'\[\[',
r'\]\]',
r'::',
r'->',
'[' + re.escape(string.punctuation) + ']'
]))),
(TNumber, re.compile(r'[0-9][0-9A-Fa-f.]*')),
]
line_directive_regex = re.compile('(\d+) "(.*)"')
@functools.cached_property
def line_map(self):
source_length = len(self.source_text)
line_map = array.array('L', [0]) * source_length
line = 1
for i, char in enumerate(self.source_text):
if char == '\n':
line += 1
line_map[i] = line
self._line_map = line_map
return line_map
def __init__(self, input_path):
if isinstance(input_path, str):
self.input_path = input_path
with open(input_path, 'rt', encoding='utf-8') as f:
self.source_text = f.read()
else:
self.input_path = '<memory>'
self.source_text = input_path.read()
source_length = len(self.source_text)
self.tokens = []
# Tokenize
mapped_path = None
mapped_line_offset = 0
pos = 0
while pos < source_length:
for type_, regex in Lexer.regexes:
match = regex.match(self.source_text, pos)
if match:
span = match.span(0)
length = span[1] - span[0]
assert length > 0
token = None
if type_ is not None:
token = Token(self, type_, pos, length)
if mapped_path is not None:
token.mapped_path = mapped_path
token.mapped_line_offset = mapped_line_offset
# Handle line directives
if token and (token.text == '#' or token.text == '#line'):
line_end = self.source_text.find('\n', pos)
if self.source_text[pos + len(token.text)] == ' ':
m = Lexer.line_directive_regex.search(
self.source_text,
pos,
line_end
)
if m:
mapped_line = int(m.group(1))
mapped_path = m.group(2)
mapped_line_offset = mapped_line - token.real_line - 1
else:
fatal("Wonky line directive at index %i of '%s'" % (pos, input_path))
pos = line_end
else:
pos += length
# Normal token
else:
if token:
self.tokens.append(token)
pos += length
break
else:
fatal('Unexpected character at index %i of "%s"' % (pos, input_path))
self.tokens.append(Token(self, TEnd, source_length, 0))
def __iter__(self):
return iter(self.tokens)
def __getitem__(self, i):
return self.tokens[i]
class ParseError(Exception):
pass
class Cursor:
def __init__(self, tokens, index):
self.tokens = tokens
self.index = index
def __bool__(self):
return self.token.type != TEnd
def next(self):
if self.token.type == TEnd:
raise StopIteration
self.index += 1
def error(self, msg):
e = ParseError(
'%s:%i:%s' % (self.token.path, self.token.line, msg)
)
e.record_scope = local.RecordScope[:]
raise e
@property
def token(self):
return self.tokens[self.index]
@property
def text(self):
return self.token.text
@property
def type(self):
return self.token.type
def copy(self):
return Cursor(self.tokens, self.index)
def set(self, other):
self.tokens = other.tokens
self.index = other.index
class TokenRange:
def __init__(self, start_cursor, end_cursor=None):
self.start_cursor = start_cursor.copy()
if end_cursor:
self.set_end(end_cursor)
else:
self.end_cursor = None
def set_end(self, end_cursor):
self.end_cursor = end_cursor.copy()
self.end_cursor.index -= 1
@property
def text(self):
tokens = self.start_cursor.tokens
if not self.end_cursor:
return tokens.source_text[self.start_cursor.token.index:]
i = self.start_cursor.token.index
j = self.end_cursor.token.index + len(self.end_cursor.text)
return tokens.source_text[i:j]
@property
def tokens(self):
return self.start_cursor.tokens[
self.start_cursor.index :
self.end_cursor.index + 1
]
@property
def line_directive(self):
return self.start_cursor.token.line_directive
def emit_line_directive(self, f):
if line_directives_enabled:
f.write(self.line_directive)
class Node:
dump_text = False
@property
def text(self):
return self.range.text
@property
def tokens(self):
return self.range.tokens
@property
def line_directive(self):
return self.range.line_directive
def emit_line_directive(self, f):
if line_directives_enabled:
f.write(self.line_directive)
def __repr__(self):
r = '%s(%s)' % (
self.__class__.__name__,
', '.join(
'%s=%s' % (k, v)
for k, v in self.__dict__.items()
if type(v) is str
or v is True
)
)
if self.dump_text:
r += ' <"' + self.text + '">'
return r
def dump(self, indent=0):
tab = ' ' * indent
if indent == 0:
print(repr(self))
for k, v in self.__dict__.items():
if type(v) is list:
for i, item in enumerate(v):
if isinstance(item, Node):
print('%s %s[%s]: %s' % (tab, k, i, repr(item)))
item.dump(indent+1)
elif isinstance(v, Node):
print('%s %s: %s' % (tab, k, repr(v)))
v.dump(indent+1)
@classmethod
def parse(cls, cursor):
start_cursor = cursor.copy()
node = cls._parse(cursor)
if node:
node.range = TokenRange(start_cursor, cursor)
return node
def emit_forward_declaration(self, f):
pass
def emit_interface(self, f):
pass
def emit_implementation(self, f):
pass
def emit_inline_function_definitions(self, f):
pass
def get_dependencies(self, names, typedef_names):
return set()
class BaseRecord(Node):
@classmethod
def _parse(cls, cursor):
self = BaseRecord()
self.access = None
self.name = None
self.template_params = None
self.is_virtual = False
while cursor:
if cursor.text in ['public', 'protected', 'private']:
if self.access:
cursor.error('Too many access specifiers')
self.access = cursor.text
cursor.next()
elif cursor.text == 'virtual':
self.is_virtual = True
cursor.next()
elif cursor.type == TWord:
if self.name:
cursor.error('Unexpected "%s"' % cursor.text)
self.name = Name.parse(cursor)
self.template_params = self.name.template_params
else:
if self.name:
return self
cursor.error("Base type declaration has no name")
class AccessLabel(Node):
def emit_interface(self, f):
self.emit_line_directive(f)
f.write(self.access + ':\n\n')
@classmethod
def _parse(cls, cursor):
assert cursor.text in ['public', 'private', 'protected']
self = AccessLabel()
self.access = cursor.text
cursor.next()
if cursor.text != ':':
cursor.error('Expected colon after "%s"' % cursor.text)
cursor.next()
return self
class TemplateParams(Node):
dump_text = True
@classmethod
def _parse(cls, cursor):
self = TemplateParams()
assert cursor.text == '<'
level = 0
while cursor:
if cursor.text == '<':
level += 1
elif cursor.text == '>':
level -= 1
if level == 0:
cursor.next()
break
assert level >= 0
cursor.next()
return self
class Name(Node):
@classmethod
def _parse(cls, cursor):
self = Name()
self.scope = []
self.identifier = None
self.template_params = None
another = True
first = True
while cursor:
if another and cursor.type == TWord:
self.scope.append(cursor.text)
self.identifier = cursor.text
another = False
cursor.next()
elif cursor.text == '::':
if another and not first:
cursor.error("Repeated '::'")
another = True
cursor.next()
elif self.identifier and (not another) and cursor.text == '<':
if self.template_params is not None:
cursor.error("Extra template paramter list")
self.template_params = TemplateParams.parse(cursor)
else:
if not self.identifier:
cursor.error("Expected a name")
return self
first = False
class RecordDefinition(Node):
@classmethod
def _parse(cls, cursor):
self = RecordDefinition()
self.record_kind = None
self.name = None
self.bases = None
self.children = []
self.attributes = []
self.is_q_object = False
self.manual_deps = set()
self.manual_non_deps = set()
self.head = TokenRange(cursor)
while cursor:
if cursor.text in ['class', 'struct', 'union', 'enum']:
if self.record_kind is None:
self.record_kind = cursor.text
elif self.record_kind == 'enum' and cursor.text == 'class':
self.record_kind = 'enum class'
else:
cursor.error('Unexpected "%s" after "%s"' % (cursor.text, self.record_kind))
cursor.next()
elif cursor.type == TWord:
if self.name is None:
name = Name.parse(cursor)
self.name = name.identifier
else:
cursor.error('Unexpected "%s"; name is already "%s"' % (cursor.text, self.name))
elif cursor.text == ':':
if self.bases is not None:
cursor.error('Unexpected colon')
cursor.next()
base = BaseRecord.parse(cursor)
if not base:
cursor.error('Expected base type after colon')
self.bases = [base]
while cursor.text == ',':
cursor.next()
base = BaseRecord.parse(cursor)
if not base:
cursor.error('Expected base type after comma')
self.bases.append(base)
if cursor.text != '{':
cursor.error('Expected "{" after base type')
break
elif cursor.text == '{':
break
elif cursor.text == '[[':
cursor.next()
attr = cursor.text
self.attributes.append(attr)
cursor.next()
if cursor.text != ']]':
cursor.error('Expected "]]" after "%s" to close attribute specifier' % attr)
cursor.next()
else:
cursor.error(f'Unexpected "{cursor.text}"')
# Parse body
assert cursor.text == '{'
head_end = cursor.copy()
head_end.next()
self.head.set_end(head_end)
with append(local.RecordScope, self):
if self.record_kind in ['enum', 'enum class']:
self.enum_values = FunctionBody.parse(cursor)
else:
cursor.next()
while cursor:
if cursor.text == '}':
cursor.next()
break
elif cursor.text in ['public', 'private', 'protected']:
label = AccessLabel.parse(cursor)
self.children.append(label)
elif qt_enabled and cursor.text == 'Q_OBJECT':
self.is_q_object = True
cursor.next()
else:
decl = Declaration.parse(cursor)
assert decl
self.children.append(decl)
# Decl could be function def (no ';') or normal def (';')
if cursor.text == ';':
cursor.next()
return self
def get_dependencies(self, names, typedef_names):
deps = set()
if self.bases:
for base in self.bases:
if len(base.name.scope) == 1 and base.name.identifier in names:
deps.add(base.name.identifier)
for child in self.children:
deps = deps | child.get_dependencies(names, typedef_names)
deps |= self.manual_deps
deps -= self.manual_non_deps
return deps
def emit_forward_declaration(self, f):
if self.record_kind in ['enum', 'enum class']:
self.emit_line_directive(f)
for attr in self.attributes:
f.write(f'[[f:{attr}]] ')
f.write(self.text)
f.write(';\n')
else:
f.write('%s %s;\n' % (self.record_kind, self.name))
def emit_interface(self, f):
if self.record_kind in ['enum', 'enum class'] and local.RecordScope:
self.head.emit_line_directive(f)
f.write(self.text)
f.write('\n')
elif self.record_kind in ['enum', 'enum class'] and not local.RecordScope:
pass
else:
self.head.emit_line_directive(f)
f.write(self.head.text.replace(self.record_kind, self.record_kind))
f.write('\n')
if self.is_q_object:
f.write('Q_OBJECT\n\n')
with append(local.RecordScope, self.name):
for child in self.children:
child.emit_interface(f)
f.write('}')
def emit_inline_function_definitions(self, f):
with append(local.RecordScope, self.name):
for child in self.children:
child.emit_inline_function_definitions(f)
class Specifier(Node):
@classmethod
def _parse(cls, cursor):
start_cursor = cursor.copy()
self = Specifier()
self.record_kind = None
self.record_definition = None
self.record_start = None
self.name = None
self.template_params = None
self.is_const = False
self.is_static = False
self.is_typedef = False
self.is_virtual = False
self.is_inline = False
self.is_constexpr = False
self.is_explicit = False
self.is_extern_c = False
self.attributes = []
record_start = None
while cursor:
if cursor.text in ['class', 'struct', 'union', 'enum']:
if self.record_kind is None:
self.record_kind = cursor.text
record_start = cursor.copy()
elif self.record_kind == 'enum' and cursor.text == 'class':
self.record_kind = 'enum class'
else:
cursor.error('Unexpected "%s" after "%s"' % (cursor.text, record_kind))
cursor.next()
elif cursor.text == 'const':
if not self.is_const:
self.is_const = True
cursor.next()
else:
cursor.error('Repeated "const"')
elif cursor.text == 'explicit':
if not self.is_explicit:
self.is_explicit = True
cursor.next()
else:
cursor.error('Repeated "explicit"')
elif cursor.text == 'static':
if not self.is_static:
self.is_static = True
cursor.next()
else:
cursor.error('Repeated "static"')
elif cursor.text == 'typedef':
if not self.is_typedef:
self.is_typedef = True
cursor.next()
else:
cursor.error('Repeated "typedef"')
elif cursor.text == 'virtual':
if not self.is_virtual:
self.is_virtual = True
cursor.next()
else:
cursor.error('Repeated "virtual"')
elif cursor.text == 'inline':
if not self.is_inline:
self.is_inline = True
cursor.next()
else:
cursor.error('Repeated "inline"')
elif cursor.text == 'constexpr':
if not self.is_constexpr:
self.is_constexpr = True
cursor.next()
else:
cursor.error('Repeated "constexpr"')
elif cursor.text == '__stdcall':
cursor.next()
elif cursor.text == 'extern':
cursor.next()
if cursor.text != '"C"':
cursor.error('extern "C" is the only supported use of extern')
cursor.next()
self.is_extern_c = True
elif cursor.text == 'template':
cursor.next()
if cursor.text != '<':
cursor.error('Expected "<" after "template"')
self.template_params = TemplateParams.parse(cursor)
elif self.record_kind and cursor.text in [':', '{']:
cursor.set(record_start)
self.record_definition = RecordDefinition.parse(cursor)
break
elif cursor.text == 'operator':
# This must be a conversion operator; stop parsing the specifier
break
elif cursor.type == TWord or cursor.text == '::':
# If this is a constructor, stop parsing the specifier
if local.RecordScope and cursor.text == local.RecordScope[-1].name:
next_cursor = cursor.copy()
next_cursor.next()
if next_cursor.text == '(':
break
if self.name is None:
self.name = Name.parse(cursor)
else:
# We already have a name; this name must be the first declarator
break
elif cursor.text == '~':
# This is a destructor; stop parsing the specifier
break
elif cursor.text == '[[':
cursor.next()
attr = cursor.text
self.attributes.append(attr)
cursor.next()
if cursor.text != ']]':
cursor.error('Expected "]]" after "%s" to close attribute specifier' % attr)
cursor.next()
else:
if self.record_kind or self.name:
break
cursor.error("Unexpected specifier component '%s'" % cursor.text)
return self
@property
def text_without_attributes(self):
return re.sub(r'\[\[.*?\]\]', '', self.text)
class FunctionBody(Node):
@classmethod
def _parse(cls, cursor):
self = FunctionBody()
assert cursor.text == '{'
level = 0
while cursor:
if cursor.text == '{':
level += 1
elif cursor.text == '}':
level -= 1
if level == 0:
cursor.next()
return self
if level < 0:
cursor.error("Unexpected '}'")
cursor.next()
cursor.error("Expected '}'")
class InitializerList(Node):
@classmethod
def _parse(cls, cursor):
self = InitializerList()
level = 0
while cursor:
if cursor.text in ['(', '[', '{', '<']:
level += 1
elif cursor.text in [')', ']', '}', '>']:
level -= 1
cursor.next()
if level == 0 and cursor.text in ['{', ';']:
return self
if level < 0:
cursor.error("Unexpected '}'")
class Declarator(Node):
dump_text = True
@classmethod
def parse(cls, cursor):
node = super().parse(cursor)
if node.text == '()':
cursor.error("'()' is not a valid declarator")
return node
@classmethod
def _parse(cls, cursor):
self = Declarator()
self.bitfield_width = None
can_be_bitfield = True
level = 0
while cursor:
if level == 0 and cursor.text == '=':
cursor.next()
if cursor.text == '{':
level += 1
cursor.next()
continue
if level == 0 and can_be_bitfield and cursor.text == ':':
pass
elif level == 0 and cursor.text in [',', ';', '{', ':']:
break
if cursor.text in ['(', '[', '{',]:
# TODO: Not really true. "int (a): 4" is a valid, if silly,
# bitfield declaration. Probably the part after the colon
# should just be parsed as part of the declarator in all
# cases, whether it's an initializer list or a bitfield width,
# and Declaration can sort out which one it is.
can_be_bitfield = False
level += 1
elif cursor.text in [')', ']', '}',]:
level -= 1
if level < 0:
cursor.error("Unexpected '}'")
cursor.next()
return self
@property
def name(self):
for token in self.range.tokens:
if token.type == TWord:
return token.text
@property
def reading(self):
name_i = None
tokens = self.range.tokens
for i, token in enumerate(tokens):
if token.type == TWord:
name_i = i
break
if name_i is None:
self.range.start_cursor.error("Cannot find declarator's name: " + self.text)
l = r = name_i
end = len(tokens)
fwd = True
while True:
if fwd:
r += 1
if r >= end:
fwd = False
elif tokens[r].text == ')':
fwd = False
elif tokens[r].text == '=':
fwd = False
elif tokens[r].text == '[':
while r < end and tokens[r].text != ']':
r += 1
yield 'array'
elif tokens[r].text == '(':