Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pcl_parser.py « parser « pclc « src - github.com/ianj-als/pcl.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 397a323bbfe1b914fbe5e32f8a2f81ffc14f6e73 (plain)
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
#
# Copyright Capita Translation and Interpreting 2013
#
# This file is part of Pipeline Creation Language (PCL).
# 
# Pipeline Creation Language (PCL) is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Pipeline Creation Language (PCL) is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Pipeline Creation Language (PCL).  If not, see <http://www.gnu.org/licenses/>.
#
#
# Pipeline Creation Language Parser
#
import ply.yacc as yacc
import sys

from pcl_lexer import tokens, PCLLexer
from import_spec import Import
from component import Component
from declaration import Declaration
from command import Command, Function, Return, IfCommand, LetCommand
from conditional_expressions import AndConditionalExpression, \
     OrConditionalExpression, \
     XorConditionalExpression, \
     EqualsConditionalExpression, \
     NotEqualsConditionalExpression, \
     GreaterThanConditionalExpression, \
     LessThanConditionalExpression, \
     GreaterThanEqualToConditionalExpression, \
     LessThanEqualToConditionalExpression, \
     UnaryConditionalExpression, \
     TerminalConditionalExpression
from expressions import Literal, \
     Identifier, \
     StateIdentifier, \
     UnaryExpression, \
     CompositionExpression, \
     ParallelWithTupleExpression, \
     ParallelWithScalarExpression, \
     FirstExpression, \
     SecondExpression, \
     SplitExpression, \
     MergeExpression, \
     WireExpression, \
     WireTupleExpression, \
     IfExpression, \
     IdentifierExpression
from mappings import Mapping, \
     TopMapping, \
     BottomMapping, \
     LiteralMapping, \
     ReturnMapping
from module import Module


precedence = (('nonassoc', 'NONASSOC'),
              ('left', 'COMPOSITION', 'PARALLEL_WITH_TUPLE', 'PARALLEL_WITH_SCALAR'),
              ('right', 'UNARY'))

def p_module(p):
    '''module : opt_imports_list component_definition'''
    p[0] = Module(p.parser.filename, p.lineno(1), p[1], p[2])
    # Store the parent module in the imports and definition objects
    for import_ in p[1]:
        import_.module = p[0]
    p[2].module = p[0]

def p_opt_imports_list(p):
    '''opt_imports_list : imports_list
                        | '''
    if len(p) > 1:
        p[0] = p[1]
    else:
        p[0] = list()

def p_imports_list(p):
    '''imports_list : import_spec imports_list
                    | import_spec'''
    if len(p) > 2:
        p[0] = [p[1]] + p[2]
    else:
        p[0] = [p[1]]

def p_import_spec(p):
    '''import_spec : IMPORT identifier_or_qual_identifier AS IDENTIFIER'''
    p[0] = Import(p.parser.filename,
                  p.lineno(1),
                  p[2],
                  Identifier(p.parser.filename, p.lineno(4), p[4]))

def p_component_definition(p):
    '''component_definition : COMPONENT IDENTIFIER INPUTS scalar_or_tuple_identifier_comma_list OUTPUTS scalar_or_tuple_identifier_comma_list opt_configuration opt_declarations AS arrow_expression
                            | COMPONENT IDENTIFIER INPUTS scalar_or_tuple_identifier_comma_list OUTPUTS scalar_or_tuple_identifier_comma_list opt_configuration DO do_commands'''
    lineno = p.lineno(1)
    identifier = Identifier(p.parser.filename, p.lineno(2), p[2])
    if len(p) > 10:
        p[0] = Component.getNodeComponent(p.parser.filename,
                                          lineno,
                                          identifier,
                                          p[4],
                                          p[6],
                                          p[7],
                                          p[8],
                                          p[10])
    else:
        p[0] = Component.getLeafComponent(p.parser.filename,
                                          lineno,
                                          identifier,
                                          p[4],
                                          p[6],
                                          p[7],
                                          p[9])

def p_opt_configuration(p):
    '''opt_configuration : CONFIGURATION identifier_comma_list
                         | '''
    if len(p) > 1:
        p[0] = p[2]
    else:
        p[0] = list()

def p_opt_declarations(p):
    '''opt_declarations : DECLARE declarations
                        | '''
    if len(p) > 1:
        p[0] = p[2]
    else:
        p[0] = list()

def p_declarations(p):
    '''declarations : declaration declarations
                    | declaration'''
    if len(p) > 2:
        p[0] = [p[1]] + p[2]
    else:
        p[0] = [p[1]]

def p_declaration(p):
    '''declaration : IDENTIFIER ASSIGN NEW IDENTIFIER opt_with_clause'''
    p[0] = Declaration(p.parser.filename,
                       p.lineno(1),
                       Identifier(p.parser.filename, p.lineno(1), p[1]),
                       Identifier(p.parser.filename, p.lineno(4), p[4]),
                       p[5] if p[5] else list())

def p_opt_with_clause(p):
    '''opt_with_clause : WITH configuration_mappings
                       | '''
    if len(p) > 1:
        p[0] = p[2]

def p_configuration_mappings(p):
    '''configuration_mappings : mapping ',' configuration_mappings
                              | mapping'''
    if len(p) > 2:
        p[0] = [p[1]] + p[3]
    else:
        p[0] = [p[1]]

def p_mapping(p):
    '''mapping : identifier_or_literal MAPS_TO identifier_or_qual_identifier'''
    p[0] = Mapping(p.parser.filename, p[1].lineno, p[1], p[3])

def p_arrow_expression(p):
    '''arrow_expression : composition_expression'''
    p[0] = p[1]

def p_composition_expression(p):
    '''composition_expression : parallel_with_tuple_expression
                              | composition_expression COMPOSITION parallel_with_tuple_expression'''
    if len(p) == 2:
        p[0] = p[1]
    else:
        p[0] = CompositionExpression(p.parser.filename, p.lineno(2), p[1], p[3])

def p_parallel_with_tuple_expression(p):
    '''parallel_with_tuple_expression : parallel_with_scalar_expression
                                      | parallel_with_tuple_expression PARALLEL_WITH_TUPLE parallel_with_scalar_expression'''
    if len(p) == 2:
        p[0] = p[1]
    else:
        p[0] = ParallelWithTupleExpression(p.parser.filename, p.lineno(2), p[1], p[3])

def p_parallel_with_scalar_expression(p):
    '''parallel_with_scalar_expression : unary_expression
                                       | parallel_with_scalar_expression PARALLEL_WITH_SCALAR unary_expression'''
    if len(p) == 2:
        p[0] = p[1]
    else:
        p[0] = ParallelWithScalarExpression(p.parser.filename, p.lineno(2), p[1], p[3])

def p_unary_expression(p):
    '''unary_expression : first_expression
                        | second_expression
                        | split_expression
                        | merge_expression
                        | wire_expression
                        | if_expression
                        | identifier_expression
                        | '(' arrow_expression ')' '''
    if len(p) > 2:
        p[0] = UnaryExpression(p.parser.filename, p[2].lineno, p[2])
    else:
        p[0] = p[1]

def p_first_expression(p):
    '''first_expression : FIRST arrow_expression %prec UNARY'''
    p[0] = FirstExpression(p.parser.filename, p.lineno(1), p[2])

def p_second_expression(p):
     '''second_expression : SECOND arrow_expression %prec UNARY'''
     p[0] = SecondExpression(p.parser.filename, p.lineno(1), p[2])

def p_split_expression(p):
    '''split_expression : SPLIT %prec NONASSOC'''
    p[0] = SplitExpression(p.parser.filename, p.lineno(1))

def p_merge_expression(p):
    '''merge_expression : MERGE merge_mappings %prec NONASSOC'''
    p[0] = MergeExpression(p.parser.filename, p.lineno(1), p[2])

def p_wire_expression(p):
    '''wire_expression : WIRE wire_mappings %prec NONASSOC
                       | WIRE '(' wire_mappings ')' ',' '(' wire_mappings ')' %prec UNARY'''
    if len(p) < 4:
        p[0] = WireExpression(p.parser.filename, p.lineno(1), tuple(p[2]))
    else:
        p[0] = WireTupleExpression(p.parser.filename,
                                   p.lineno(1),
                                   tuple(p[3]),
                                   tuple(p[7]))

def p_if_expression(p):
    '''if_expression : IF conditional_expression arrow_expression arrow_expression'''
    p[0] = IfExpression(p.parser.filename, p.lineno(1), p[2], p[3], p[4])

def p_identifier_expression(p):
    '''identifier_expression : identifier_or_qual_identifier'''
    p[0] = IdentifierExpression(p.parser.filename, p[1].lineno, p[1])

def p_merge_mappings(p):
    '''merge_mappings : merge_mapping ',' merge_mappings
                      | merge_mapping'''
    if len(p) > 2:
        p[0] = [p[1]] + p[3]
    else:
        p[0] = [p[1]]

def p_merge_mapping(p):
    '''merge_mapping : TOP '[' identifier_or_qual_identifier ']' MAPS_TO identifier_or_qual_identifier
                     | BOTTOM '[' identifier_or_qual_identifier ']' MAPS_TO identifier_or_qual_identifier
                     | literal MAPS_TO identifier_or_qual_identifier'''
    if str(p[1]).upper() == 'TOP':
        p[0] = TopMapping(p.parser.filename, p.lineno(1), p[3], p[6])
    elif str(p[1]).upper() == 'BOTTOM':
        p[0] = BottomMapping(p.parser.filename, p.lineno(1), p[3], p[6])
    else:
        p[0] = LiteralMapping(p.parser.filename, p.lineno(1), p[1], p[3])

def p_wire_mappings(p):
    '''wire_mappings : wire_mapping ',' wire_mappings
                     | wire_mapping'''
    if len(p) > 2:
        p[0] = [p[1]] + p[3]
    else:
        p[0] = [p[1]]

def p_wire_mapping(p):
    '''wire_mapping : identifier_or_qual_identifier MAPS_TO identifier_or_qual_identifier
                    | literal MAPS_TO identifier_or_qual_identifier'''
    if isinstance(p[1], Literal):
        p[0] = LiteralMapping(p.parser.filename, p[1].lineno, p[1], p[3])
    else:
        p[0] = Mapping(p.parser.filename, p[1].lineno, p[1], p[3])

def p_conditional_expression(p):
    '''conditional_expression : or_conditional_expression'''
    p[0] = p[1]

def p_or_conditional_expression(p):
    '''or_conditional_expression : and_conditional_expression
                                 | or_conditional_expression OR and_conditional_expression'''
    if len(p) > 2:
        p[0] = OrConditionalExpression(p.parser.filename, p.lineno(2), p[1], p[3])
    else:
        p[0] = p[1]

def p_and_conditional_expression(p):
    '''and_conditional_expression : xor_conditional_expression
                                  | and_conditional_expression AND xor_conditional_expression'''
    if len(p) > 2:
        p[0] = AndConditionalExpression(p.parser.filename, p.lineno(2), p[1], p[3])
    else:
        p[0] = p[1]

def p_xor_conditional_expression(p):
    '''xor_conditional_expression : equals_conditional_expression
                                  | xor_conditional_expression XOR equals_conditional_expression'''
    if len(p) > 2:
        p[0] = XorConditionalExpression(p.parser.filename, p.lineno(2), p[1], p[3])
    else:
        p[0] = p[1]

def p_equals_conditional_expression(p):
    '''equals_conditional_expression : not_equals_conditional_expression
                                     | equals_conditional_expression EQUALS not_equals_conditional_expression'''
    if len(p) > 2:
        p[0] = EqualsConditionalExpression(p.parser.filename, p.lineno(2), p[1], p[3])
    else:
        p[0] = p[1]

def p_not_equals_conditional_expression(p):
    '''not_equals_conditional_expression : greater_than_conditional_expression
                                         | not_equals_conditional_expression NOT_EQUALS greater_than_conditional_expression'''
    if len(p) > 2:
        p[0] = NotEqualsConditionalExpression(p.parser.filename, p.lineno(2), p[1], p[3])
    else:
        p[0] = p[1]

def p_greater_than_conditional_expression(p):
    '''greater_than_conditional_expression : less_than_conditional_expression
                                           | greater_than_conditional_expression GT less_than_conditional_expression'''
    if len(p) > 2:
        p[0] = GreaterThanConditionalExpression(p.parser.filename, p.lineno(2), p[1], p[3])
    else:
        p[0] = p[1]

def p_less_than_conditional_expression(p):
    '''less_than_conditional_expression : greater_than_equal_conditional_expression
                                        | less_than_conditional_expression LT greater_than_conditional_expression'''
    if len(p) > 2:
        p[0] = LessThanConditionalExpression(p.parser.filename, p.lineno(2), p[1], p[3])
    else:
        p[0] = p[1]

def p_greater_than_equal_conditional_expression(p):
    '''greater_than_equal_conditional_expression : less_than_equal_condition_expression
                                                 | greater_than_equal_conditional_expression G_EQUAL less_than_conditional_expression'''
    if len(p) > 2:
        p[0] = GreaterThanEqualToConditionalExpression(p.parser.filename, p.lineno(2), p[1], p[3])
    else:
        p[0] = p[1]

def p_less_than_equal_condition_expression(p):
    '''less_than_equal_condition_expression : unary_conditional_expression
                                            | less_than_conditional_expression L_EQUAL unary_conditional_expression'''
    if len(p) > 2:
        p[0] = LessThanEqualToConditionalExpression(p.parser.filename, p.lineno(2), p[1], p[3])
    else:
        p[0] = p[1]

def p_unary_conditional_expression(p):
    '''unary_conditional_expression : identifier_or_literal
                                    | state_identifier
                                    | '(' conditional_expression ')' '''
    if len(p) > 2:
        p[0] = UnaryConditionalExpression(p.parser.filename, p[2].lineno, p[2])
    else:
        p[0] = TerminalConditionalExpression(p[1])

def p_do_commands(p):
    '''do_commands : opt_do_command_list RETURN return_mapping_list'''
    p[1].append(Return(p.parser.filename, p.lineno(2), None, p[3]))
    p[0] = p[1]

def p_opt_do_commmand_list(p):
    '''opt_do_command_list : do_command_list
                           | '''
    if len(p) > 1:
        p[0] = p[1]
    else:
        p[0] = list()

def p_do_command_list(p):
    '''do_command_list : do_command do_command_list
                       | do_command'''
    if len(p) > 2:
        p[0] = p[1] + p[2]
    else:
        p[0] = p[1]

def p_do_command(p):
    '''do_command : identifier_or_qual_identifier LEFT_ARROW function
                  | function
                  | LET let_bindings IN function
                  | identifier_or_qual_identifier LEFT_ARROW LET let_bindings IN function
                  | IF conditional_expression THEN opt_do_command_list return_command ELSE opt_do_command_list return_command ENDIF
                  | identifier_or_qual_identifier LEFT_ARROW IF conditional_expression THEN opt_do_command_list return_command ELSE opt_do_command_list return_command ENDIF'''
    p[0] = list()
    if len(p) > 10:
        # len(p) == 12
        p[6].append(p[7])
        p[9].append(p[10])
        p[0].append(IfCommand(p.parser.filename, p[1].lineno, p[1], p[4], p[6], p[9]))
    elif len(p) > 9:
        # len(p) == 10
        p[4].append(p[5])
        p[7].append(p[8])
        p[0].append(IfCommand(p.parser.filename, p.lineno(1), None, p[2], p[4], p[7]))
    elif len(p) > 6:
        # len(p) == 7
        p[0].append(LetCommand(p.parser.filename, p.lineno(1), p[1], p[4], p[6]))
    elif len(p) > 4:
        # len(p) == 5
        p[0].append(LetCommand(p.parser.filename, p.lineno(1), None, p[2], p[4]))
    elif len(p) > 3:
        # len(p) == 4
        p[0].append(Command(p.parser.filename, p[1].lineno, p[1], p[3]))
    else:
        # len(p) == 2
        p[0].append(Command(p.parser.filename, p[1].lineno, None, p[1]))

def p_let_bindings(p):
    '''let_bindings : let_binding let_bindings
                    | let_binding'''
    if len(p) > 2:
        p[0] = [p[1]] + p[2]
    else:
        p[0] = [p[1]]

def p_let_binding(p):
    '''let_binding : identifier_or_qual_identifier LEFT_ARROW function'''
    p[0] = Command(p.parser.filename, p[1].lineno, p[1], p[3])

def p_function(p):
    '''function : QUALIFIED_IDENTIFIER '(' opt_function_args ')' '''
    p[0] = Function(p.parser.filename, p.lineno(1), p[1], p[3])

def p_opt_function_args(p):
    '''opt_function_args : function_arg_list
                         | '''
    if len(p) > 1:
        p[0] = p[1]
    else:
        p[0] = list()

def p_function_arg_list(p):
    '''function_arg_list : function_arg ',' function_arg_list
                         | function_arg'''
    if len(p) > 2:
        p[0] = [p[1]] + p[3]
    else:
        p[0] = [p[1]]

def p_function_arg(p):
    '''function_arg : identifier_or_literal
                    | state_identifier'''
    p[0] = p[1]

def p_return_command(p):
    '''return_command : RETURN '(' ')'
                      | RETURN function_arg'''
    if len(p) > 3:
        p[0] = Return(p.parser.filename, -1)
    else:
        p[0] = Return(p.parser.filename, p.lineno(1), p[2])

def p_return_mapping_list(p):
    '''return_mapping_list : return_mapping ',' return_mapping_list
                           | return_mapping'''
    if len(p) > 2:
        p[0] = [p[1]] + p[3]
    else:
        p[0] = [p[1]]

def p_return_mapping(p):
    '''return_mapping : identifier_or_qual_identifier LEFT_ARROW identifier_or_literal
                      | identifier_or_qual_identifier LEFT_ARROW state_identifier'''
    p[0] = ReturnMapping(p.parser.filename,
                         p[1].lineno,
                         p[3],
                         p[1])

def p_scalar_or_tuple_identifier_comma_list(p):
    '''scalar_or_tuple_identifier_comma_list : '(' identifier_comma_list ')' ',' '(' identifier_comma_list ')'
                                             | identifier_comma_list'''
    if len(p) > 2:
        p[0] = (p[2], p[6])
    else:
        p[0] = p[1]

def p_identifier_comma_list(p):
    '''identifier_comma_list : identifier_or_qual_identifier ',' identifier_comma_list
                             | identifier_or_qual_identifier'''
    if len(p) > 2:
        p[0] = [p[1]] + p[3]
    else:
        p[0] = [p[1]]

def p_identifier_or_literal(p):
    '''identifier_or_literal : identifier_or_qual_identifier
                             | literal'''
    p[0] = p[1]

def p_state_identifier(p):
    '''state_identifier : '@' identifier_or_qual_identifier'''
    p[0] = StateIdentifier(p.parser.filename, p[2].lineno, p[2].identifier)

def p_identifier_or_qual_identifier(p):
    '''identifier_or_qual_identifier : IDENTIFIER
                                     | QUALIFIED_IDENTIFIER'''
    p[0] = Identifier(p.parser.filename, p.lineno(1), p[1])

def p_literal(p):
    '''literal : INTEGER
               | FLOAT
               | STRING
               | BOOLEAN'''
    p[0] = Literal(p.parser.filename, p.lineno(1), p[1])

recovery_tokens = (')',
                   'COMPOSITION',
                   'PARALLEL_WITH_TUPLE',
                   'PARALLEL_WITH_SCALAR',
                   'AS',
                   'DECLARE',
                   'DO',
                   'IF',
                   'RETURN')

def p_error(token):
    if not token:
        print >> sys.stderr, "ERROR: Unexpected EOF"
    else:
        print >> sys.stderr, "ERROR: line %d parser failure at or near %s" % \
                             (token.lineno, token.type)

        while True:
            tok = yacc.token()
            if not tok or tok.type in recovery_tokens:
                break
        yacc.restart()

class PCLParser(object):
    def __init__(self, lexer, logger, **kwargs):
        self.__lexer = lexer
        self.__logger = logger
        if 'debuglog' not in kwargs:
            kwargs['debuglog'] = logger
        if 'errorlog' not in kwargs:
            kwargs['errorlog'] = logger
        self.__parser = yacc.yacc(**kwargs)

    def parseFile(self, filename, **kwargs):
        self.__parser.filename = filename
        f = open(filename, "r")
        return self.__parser.parse(input = f.read(),
                                   lexer = self.__lexer,
                                   debug = self.__logger)