GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 50de1a...af1872 )
by Kaloyan
01:34
created

Parser.p_fqn_1()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
2 1
from collections import OrderedDict
3 1
from abc import ABCMeta
4 1
import ply.yacc as yacc
0 ignored issues
show
Configuration introduced by
The import ply.yacc could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5 1
from pyfranca import franca_lexer
6 1
from pyfranca import ast
7
8
9 1
class ArgumentGroup(object):
10
11 1
    __metaclass__ = ABCMeta
12
13 1
    def __init__(self, arguments=None):
14 1
        self.arguments = arguments if arguments else OrderedDict()
15
16
17 1
class InArgumentGroup(ArgumentGroup):
18 1
    pass
19
20
21 1
class OutArgumentGroup(ArgumentGroup):
22 1
    pass
23
24
25 1
class ErrorArgumentGroup(ArgumentGroup):
26 1
    pass
27
28
29 1
class ParserException(Exception):
30
31 1
    def __init__(self, message):
32 1
        super(ParserException, self).__init__()
33 1
        self.message = message
34
35 1
    def __str__(self):
36 1
        return self.message
37
38
39 1
class Parser(object):
40
    """
41
    Franca IDL PLY parser.
42
    """
43
44 1
    @staticmethod
45
    def _package_def(members):
46 1
        imports = []
47 1
        interfaces = OrderedDict()
48 1
        typecollections = OrderedDict()
49 1
        if members:
50 1
            for member in members:
51 1
                if isinstance(member, ast.Import):
52 1
                    imports.append(member)
53 1
                elif isinstance(member, ast.Interface):
54 1
                    interfaces[member.name] = member
55 1
                elif isinstance(member, ast.TypeCollection):
56 1
                    typecollections[member.name] = member
57
                else:
58
                    raise ParserException("Unexpected package member type.")
59 1
        return imports, interfaces, typecollections
60
61
    # noinspection PyIncorrectDocstring
62 1
    @staticmethod
63
    def p_package_def(p):
64
        """
65
        package_def : PACKAGE fqn defs
66
        """
67 1
        imports, interfaces, typecollections = Parser._package_def(p[3])
68 1
        p[0] = ast.Package(name=p[2], file_name=None, imports=imports,
69
                           interfaces=interfaces,
70
                           typecollections=typecollections)
71
72
    # noinspection PyIncorrectDocstring
73 1
    @staticmethod
74
    def p_defs_1(p):
75
        """
76
        defs : defs def
77
        """
78 1
        p[0] = p[1]
79 1
        p[0].append(p[2])
80
81
    # noinspection PyIncorrectDocstring
82 1
    @staticmethod
83
    def p_defs_2(p):
84
        """
85
        defs : def
86
        """
87 1
        p[0] = [p[1]]
88
89
    # noinspection PyIncorrectDocstring
90 1
    @staticmethod
91
    def p_defs_3(p):
92
        """
93
        defs : empty
94
        """
95
96
    # noinspection PyIncorrectDocstring
97 1
    @staticmethod
98
    def p_fqn_1(p):
99
        """
100
        fqn : ID '.' fqn
101
        """
102 1
        p[0] = "{}.{}".format(p[1], p[3])
103
104
    # noinspection PyIncorrectDocstring
105 1
    @staticmethod
106
    def p_fqn_2(p):
107
        """
108
        fqn : ID
109
        """
110 1
        p[0] = p[1]
111
112
    # noinspection PyIncorrectDocstring
113 1
    @staticmethod
114
    def p_fqn_3(p):
115
        """
116
        fqn : '*'
117
        """
118 1
        p[0] = p[1]
119
120
    # noinspection PyIncorrectDocstring
121 1
    @staticmethod
122
    def p_import_def_1(p):
123
        """
124
        def : IMPORT fqn FROM FILE_NAME
125
        """
126 1
        p[0] = ast.Import(file_name=p[4], namespace=p[2])
127
128
    # noinspection PyIncorrectDocstring
129 1
    @staticmethod
130
    def p_import_def_2(p):
131
        """
132
        def : IMPORT MODEL FILE_NAME
133
        """
134 1
        p[0] = ast.Import(file_name=p[3])
135
136
    # noinspection PyIncorrectDocstring
137 1
    @staticmethod
138
    def p_typecollection(p):
139
        """
140
        def : TYPECOLLECTION ID '{' typecollection_members '}'
141
        """
142 1
        try:
143 1
            p[0] = ast.TypeCollection(name=p[2], flags=None, members=p[4])
144 1
        except ast.ASTException as e:
145 1
            raise ParserException(e.message)
146
147
    # noinspection PyIncorrectDocstring
148 1
    @staticmethod
149
    def p_typecollection_members_1(p):
150
        """
151
        typecollection_members : typecollection_members typecollection_member
152
        """
153 1
        p[0] = p[1]
154 1
        p[0].append(p[2])
155
156
    # noinspection PyIncorrectDocstring
157 1
    @staticmethod
158
    def p_typecollection_members_2(p):
159
        """
160
        typecollection_members : typecollection_member
161
        """
162 1
        p[0] = [p[1]]
163
164
    # noinspection PyUnusedLocal, PyIncorrectDocstring
165 1
    @staticmethod
166
    def p_typecollection_members_3(p):
167
        """
168
        typecollection_members : empty
169
        """
170 1
        pass
171
172
    # noinspection PyIncorrectDocstring
173 1
    @staticmethod
174
    def p_typecollection_member(p):
175
        """
176
        typecollection_member : version_def
177
                              | type_def
178
                              | enumeration_def
179
                              | struct_def
180
                              | array_def
181
                              | map_def
182
        """
183 1
        p[0] = p[1]
184
185
    # noinspection PyIncorrectDocstring
186 1
    @staticmethod
187
    def p_version_def(p):
188
        """
189
        version_def : VERSION '{' MAJOR INTEGER MINOR INTEGER '}'
190
        """
191 1
        p[0] = ast.Version(major=p[4], minor=p[6])
192
193
    # noinspection PyIncorrectDocstring
194 1
    @staticmethod
195
    def p_type_def(p):
196
        """
197
        type_def : TYPEDEF ID IS type
198
        """
199 1
        p[0] = ast.Typedef(name=p[2], base_type=p[4])
200
201
    # noinspection PyIncorrectDocstring
202 1
    @staticmethod
203
    def p_interface_1(p):
204
        """
205
        def : INTERFACE ID '{' interface_members '}'
206
        """
207 1
        try:
208 1
            p[0] = ast.Interface(name=p[2], flags=None, members=p[4],
209
                                 extends=None)
210 1
        except ast.ASTException as e:
211 1
            raise ParserException(e.message)
212
213
    # noinspection PyIncorrectDocstring
214 1
    @staticmethod
215
    def p_interface_2(p):
216
        """
217
        def : INTERFACE ID EXTENDS fqn '{' interface_members '}'
218
        """
219 1
        try:
220 1
            p[0] = ast.Interface(name=p[2], flags=None, members=p[6],
221
                                 extends=p[4])
222
        except ast.ASTException as e:
223
            raise ParserException(e.message)
224
225
    # noinspection PyIncorrectDocstring
226 1
    @staticmethod
227
    def p_interface_members_1(p):
228
        """
229
        interface_members : interface_members interface_member
230
        """
231 1
        p[0] = p[1]
232 1
        p[0].append(p[2])
233
234
    # noinspection PyIncorrectDocstring
235 1
    @staticmethod
236
    def p_interface_members_2(p):
237
        """
238
        interface_members : interface_member
239
        """
240 1
        p[0] = [p[1]]
241
242
    # noinspection PyUnusedLocal, PyIncorrectDocstring
243 1
    @staticmethod
244
    def p_interface_members_3(p):
245
        """
246
        interface_members : empty
247
        """
248 1
        pass
249
250
    # noinspection PyIncorrectDocstring
251 1
    @staticmethod
252
    def p_interface_member(p):
253
        """
254
        interface_member : version_def
255
                         | attribute_def
256
                         | method_def
257
                         | broadcast_def
258
                         | type_def
259
                         | enumeration_def
260
                         | struct_def
261
                         | array_def
262
                         | map_def
263
        """
264 1
        p[0] = p[1]
265
266
    # noinspection PyIncorrectDocstring
267 1
    @staticmethod
268
    def p_attribute_def(p):
269
        """
270
        attribute_def : ATTRIBUTE type ID flag_defs
271
        """
272 1
        p[0] = ast.Attribute(name=p[3], attr_type=p[2], flags=p[4])
273
274 1
    @staticmethod
275
    def _method_def(arg_groups):
276 1
        in_args = None
277 1
        out_args = None
278 1
        errors = None
279 1
        if arg_groups:
280 1
            for arg_group in arg_groups:
281 1
                if isinstance(arg_group, InArgumentGroup):
282 1
                    if not in_args:
283 1
                        in_args = arg_group.arguments
284
                    else:
285
                        raise ParserException("Multiple in argument "
286
                                              "definitions for a method.")
287 1
                elif isinstance(arg_group, OutArgumentGroup):
288 1
                    if not out_args:
289 1
                        out_args = arg_group.arguments
290
                    else:
291
                        raise ParserException("Multiple out argument "
292
                                              "definitions for a method.")
293 1
                elif isinstance(arg_group, ErrorArgumentGroup):
294 1
                    if not errors:
295 1
                        errors = arg_group.arguments
296
                    else:
297
                        raise ParserException("Multiple error definitions "
298
                                              "for a method.")
299
                else:
300
                    raise ParserException("Unexpected method definition "
301
                                          "member.")
302 1
        return in_args, out_args, errors
303
304
    # noinspection PyIncorrectDocstring
305 1
    @staticmethod
306
    def p_method_def(p):
307
        """
308
        method_def : METHOD ID flag_defs '{' arg_group_defs '}'
309
        """
310 1
        in_args, out_args, errors = Parser._method_def(p[5])
311 1
        p[0] = ast.Method(name=p[2], flags=p[3],
312
                          in_args=in_args, out_args=out_args, errors=errors)
313
314
    # noinspection PyIncorrectDocstring
315 1
    @staticmethod
316
    def p_flag_defs_1(p):
317
        """
318
        flag_defs : flag_defs flag_def
319
        """
320 1
        p[0] = p[1]
321 1
        p[0].append(p[2])
322
323
    # noinspection PyIncorrectDocstring
324 1
    @staticmethod
325
    def p_flag_defs_2(p):
326
        """
327
        flag_defs : flag_def
328
        """
329 1
        p[0] = [p[1]]
330
331
    # noinspection PyIncorrectDocstring
332 1
    @staticmethod
333
    def p_flag_defs_3(p):
334
        """
335
        flag_defs : empty
336
        """
337 1
        pass
338
339
    # noinspection PyIncorrectDocstring
340 1
    @staticmethod
341
    def p_flag_def(p):
342
        """
343
        flag_def : SELECTIVE
344
                 | FIREANDFORGET
345
                 | POLYMORPHIC
346
                 | NOSUBSCRIPTIONS
347
                 | READONLY
348
        """
349 1
        p[0] = p[1]
350
351
    # noinspection PyIncorrectDocstring
352 1
    @staticmethod
353
    def p_arg_group_defs_1(p):
354
        """
355
        arg_group_defs : arg_group_defs arg_group_def
356
        """
357 1
        p[0] = p[1]
358 1
        p[0].append(p[2])
359
360
    # noinspection PyIncorrectDocstring
361 1
    @staticmethod
362
    def p_arg_group_defs_2(p):
363
        """
364
        arg_group_defs : arg_group_def
365
        """
366 1
        p[0] = [p[1]]
367
368
    # noinspection PyIncorrectDocstring
369 1
    @staticmethod
370
    def p_arg_group_defs_3(p):
371
        """
372
        arg_group_defs : empty
373
        """
374 1
        pass
375
376
    # noinspection PyIncorrectDocstring
377 1
    @staticmethod
378
    def p_arg_group_def_1(p):
379
        """
380
        arg_group_def : IN '{' arg_defs '}'
381
        """
382 1
        p[0] = InArgumentGroup(p[3])
383
384
    # noinspection PyIncorrectDocstring
385 1
    @staticmethod
386
    def p_arg_group_def_2(p):
387
        """
388
        arg_group_def : OUT '{' arg_defs '}'
389
        """
390 1
        p[0] = OutArgumentGroup(p[3])
391
392
    # noinspection PyIncorrectDocstring
393 1
    @staticmethod
394
    def p_arg_group_def_3(p):
395
        """
396
        arg_group_def : ERROR '{' enumerators '}'
397
        """
398 1
        p[0] = ErrorArgumentGroup(p[3])
399
400
    # noinspection PyIncorrectDocstring
401 1
    @staticmethod
402
    def p_arg_group_def_4(p):
403
        """
404
        arg_group_def : ERROR type
405
        """
406 1
        p[0] = ErrorArgumentGroup(p[2])
407
408
    # noinspection PyIncorrectDocstring
409 1
    @staticmethod
410
    def p_broadcast_def(p):
411
        """
412
        broadcast_def : BROADCAST ID flag_defs '{' arg_group_defs '}'
413
        """
414 1
        in_args, out_args, errors = Parser._method_def(p[5])
415 1
        if in_args or errors:
416
            raise ParserException("In arguments and errors cannot be part "
417
                                  "of a broadcast definition.")
418 1
        p[0] = ast.Broadcast(name=p[2], flags=p[3], out_args=out_args)
419
420
    # noinspection PyIncorrectDocstring
421 1 View Code Duplication
    @staticmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
422
    def p_arg_defs_1(p):
423
        """
424
        arg_defs : arg_defs arg_def
425
        """
426 1
        p[0] = p[1]
427 1
        if p[2].name not in p[0]:
428 1
            p[0][p[2].name] = p[2]
429
        else:
430 1
            raise ParserException("Duplicate argument '{}'.".format(p[2].name))
431
432
    # noinspection PyIncorrectDocstring
433 1
    @staticmethod
434
    def p_arg_defs_2(p):
435
        """
436
        arg_defs : arg_def
437
        """
438 1
        p[0] = OrderedDict()
439 1
        p[0][p[1].name] = p[1]
440
441
    # noinspection PyIncorrectDocstring
442 1
    @staticmethod
443
    def p_arg_def(p):
444
        """
445
        arg_def : type ID
446
        """
447 1
        p[0] = ast.Argument(name=p[2], arg_type=p[1])
448
449
    # noinspection PyIncorrectDocstring
450 1
    @staticmethod
451
    def p_enumeration_def_1(p):
452
        """
453
        enumeration_def : ENUMERATION ID '{' enumerators '}'
454
        """
455 1
        p[0] = ast.Enumeration(name=p[2], enumerators=p[4])
456
457
    # noinspection PyIncorrectDocstring
458 1
    @staticmethod
459
    def p_enumeration_def_2(p):
460
        """
461
        enumeration_def : ENUMERATION ID EXTENDS fqn '{' enumerators '}'
462
        """
463 1
        p[0] = ast.Enumeration(name=p[2], enumerators=p[6], extends=p[4])
464
465
    # noinspection PyIncorrectDocstring
466 1 View Code Duplication
    @staticmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
467
    def p_enumerators_1(p):
468
        """
469
        enumerators : enumerators enumerator
470
        """
471 1
        p[0] = p[1]
472 1
        if p[2].name not in p[0]:
473 1
            p[0][p[2].name] = p[2]
474
        else:
475 1
            raise ParserException(
476
                "Duplicate enumerator '{}'.".format(p[2].name))
477
478
    # noinspection PyIncorrectDocstring
479 1
    @staticmethod
480
    def p_enumerators_2(p):
481
        """
482
        enumerators : enumerator
483
        """
484 1
        p[0] = OrderedDict()
485 1
        p[0][p[1].name] = p[1]
486
487
    # noinspection PyUnusedLocal, PyIncorrectDocstring
488 1
    @staticmethod
489
    def p_enumerators_3(p):
490
        """
491
        enumerators : empty
492
        """
493 1
        pass
494
495
    # noinspection PyIncorrectDocstring
496 1
    @staticmethod
497
    def p_enumerator_1(p):
498
        """
499
        enumerator : ID
500
        """
501 1
        p[0] = ast.Enumerator(name=p[1])
502
503
    # noinspection PyIncorrectDocstring
504 1
    @staticmethod
505
    def p_enumerator_2(p):
506
        """
507
        enumerator : ID '=' INTEGER
508
        """
509 1
        p[0] = ast.Enumerator(name=p[1], value=p[3])
510
511
    # noinspection PyIncorrectDocstring
512 1
    @staticmethod
513
    def p_struct_def_1(p):
514
        """
515
        struct_def : STRUCT ID flag_defs '{' struct_fields '}'
516
        """
517 1
        p[0] = ast.Struct(name=p[2], fields=p[5], flags=p[3])
518
519
    # noinspection PyIncorrectDocstring
520 1
    @staticmethod
521
    def p_struct_def_2(p):
522
        """
523
        struct_def : STRUCT ID EXTENDS fqn '{' struct_fields '}'
524
        """
525 1
        p[0] = ast.Struct(name=p[2], fields=p[6], extends=p[4])
526
527
    # noinspection PyIncorrectDocstring
528 1 View Code Duplication
    @staticmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
529
    def p_struct_fields_1(p):
530
        """
531
        struct_fields : struct_fields struct_field
532
        """
533 1
        p[0] = p[1]
534 1
        if p[2].name not in p[0]:
535 1
            p[0][p[2].name] = p[2]
536
        else:
537 1
            raise ParserException(
538
                "Duplicate structure field '{}'.".format(p[2].name))
539
540
    # noinspection PyIncorrectDocstring
541 1
    @staticmethod
542
    def p_struct_fields_2(p):
543
        """
544
        struct_fields : struct_field
545
        """
546 1
        p[0] = OrderedDict()
547 1
        p[0][p[1].name] = p[1]
548
549
    # noinspection PyUnusedLocal, PyIncorrectDocstring
550 1
    @staticmethod
551
    def p_struct_fields_3(p):
552
        """
553
        struct_fields : empty
554
        """
555 1
        pass
556
557
    # noinspection PyIncorrectDocstring
558 1
    @staticmethod
559
    def p_struct_field_1(p):
560
        """
561
        struct_field : type ID
562
        """
563 1
        p[0] = ast.StructField(name=p[2], field_type=p[1])
564
565
    # noinspection PyIncorrectDocstring
566 1
    @staticmethod
567
    def p_array_def(p):
568
        """
569
        array_def : ARRAY ID OF type
570
        """
571 1
        p[0] = ast.Array(name=p[2], element_type=p[4])
572
573
    # noinspection PyIncorrectDocstring
574 1
    @staticmethod
575
    def p_map_def(p):
576
        """
577
        map_def : MAP ID '{' type TO type '}'
578
        """
579 1
        p[0] = ast.Map(name=p[2], key_type=p[4], value_type=p[6])
580
581
    # noinspection PyIncorrectDocstring
582 1
    @staticmethod
583
    def p_type_1(p):
584
        """
585
        type : INT8
586
             | INT16
587
             | INT32
588
             | INT64
589
             | UINT8
590
             | UINT16
591
             | UINT32
592
             | UINT64
593
             | BOOLEAN
594
             | FLOAT
595
             | DOUBLE
596
             | STRING
597
             | BYTEBUFFER
598
        """
599 1
        type_class = getattr(ast, p[1])
600 1
        p[0] = type_class()
601
602
    # noinspection PyIncorrectDocstring
603 1
    @staticmethod
604
    def p_type_2(p):
605
        """
606
        type : INT8 '[' ']'
607
             | INT16 '[' ']'
608
             | INT32 '[' ']'
609
             | INT64 '[' ']'
610
             | UINT8 '[' ']'
611
             | UINT16 '[' ']'
612
             | UINT32 '[' ']'
613
             | UINT64 '[' ']'
614
             | BOOLEAN '[' ']'
615
             | FLOAT '[' ']'
616
             | DOUBLE '[' ']'
617
             | STRING '[' ']'
618
             | BYTEBUFFER '[' ']'
619
        """
620 1
        type_class = getattr(ast, p[1])
621 1
        p[0] = ast.Array(name=None, element_type=type_class())
622
623
    # noinspection PyIncorrectDocstring
624 1
    @staticmethod
625
    def p_type_3(p):
626
        """
627
        type : fqn
628
        """
629 1
        p[0] = ast.Reference(name=p[1])
630
631
    # noinspection PyIncorrectDocstring
632 1
    @staticmethod
633
    def p_type_4(p):
634
        """
635
        type : fqn '[' ']'
636
        """
637
        element_type = ast.Reference(name=p[1])
638
        p[0] = ast.Array(name=None, element_type=element_type)
639
640
    # noinspection PyUnusedLocal, PyIncorrectDocstring
641 1
    @staticmethod
642
    def p_empty(p):
643
        """
644
        empty :
645
        """
646 1
        pass
647
648
    # noinspection PyIncorrectDocstring
649 1
    @staticmethod
650
    def p_error(p):
651 1
        if p:
652 1
            raise ParserException("Syntax error at line {} near '{}'.".format(
653
                                  p.lineno, p.value))
654
        else:
655 1
            raise ParserException("Reached unexpected end of file.")
656
657 1
    def __init__(self, the_lexer=None, **kwargs):
658
        """
659
        Constructor.
660
661
        :param lexer: a lexer object to use.
662
        """
663 1
        if not the_lexer:
664 1
            the_lexer = franca_lexer.Lexer()
665 1
        self._lexer = the_lexer
666 1
        self.tokens = self._lexer.tokens
667
        # Disable debugging, by default.
668 1
        if "debug" not in kwargs:
669 1
            kwargs["debug"] = False
670 1
        if "write_tables" not in kwargs:
671 1
            kwargs["write_tables"] = False
672 1
        self._parser = yacc.yacc(module=self, **kwargs)
673
674 1
    def parse(self, fidl):
675
        """
676
        Parse input text
677
678
        :param fidl: Input text to parse.
679
        :return: AST representation of the input.
680
        """
681 1
        package = self._parser.parse(fidl)
682 1
        return package
683
684 1
    def parse_file(self, fspec):
685
        """
686
        Parse input file
687
688
        :param fspec: Specification of a fidl to parse.
689
        :return: AST representation of the input.
690
        """
691
        with open(fspec, "r") as f:
692
            fidl = f.read()
693
        package = self.parse(fidl)
694
        if package:
695
            package.file = fspec
696
        return package
697