File: instrparser.py

package info (click to toggle)
mccode 3.5.19%2Bds5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,113,256 kB
  • sloc: ansic: 40,697; python: 25,137; yacc: 8,438; sh: 5,405; javascript: 4,596; lex: 1,632; cpp: 742; perl: 296; lisp: 273; makefile: 226; fortran: 132
file content (382 lines) | stat: -rw-r--r-- 14,084 bytes parent folder | download
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
'''
Implementation of classes involved in the PLY-based translation of the mcdisplay "--trace output" 
mini language.

Read the PLY documentation here: http://www.dabeaz.com/ply/ply.html#ply_nn23.
'''
from ply import lex, yacc
from .nodetree import Node
from .instrgeom import InstrumentSpecific, Component, Vector3d, Matrix3, drawclass_factory, DrawCommand

class InstrTraceParser:
    '''
    Parser for the instrument definition section of mcdisplay --trace output.
    '''
    parsetree = None
    def __init__(self, data=None, debug=False):
        self.mantid = None

        
        self.debug = debug
        self.build_lexer()
        self.build_parser()
        if data:
            self.parse(data)
    
    # these tokens match ID, but are of these types (handled in t_ID)
    reserved = {
        'INSTRUMENT'  : 'INSTRUMENT',
        'END'         : 'END',
        'Instrument'  : 'INSTRKW',
        'COMPONENT'   : 'COMPONENT',
        'POS'         : 'POS',
        'MCDISPLAY'   : 'MCDISPLAY',
        'start'       : 'STARTKWLC',
        'end'         : 'ENDKWLC',
        'component'   : 'COMPKWLC',
        
        'magnify'     : 'DRAWCALL',
        'line'        : 'DRAWCALL',
        'dashed_line' : 'DRAWCALL',
        'multiline'   : 'DRAWCALL',
        'rectangle'   : 'DRAWCALL',
        'box'         : 'DRAWCALL',
        'circle'      : 'DRAWCALL',

        'sphere'      : 'DRAWCALL',
        'cone'        : 'DRAWCALL',
        'cylinder'    : 'DRAWCALL',
        'disc'        : 'DRAWCALL',
        'annulus'     : 'DRAWCALL',
        'new_circle'  : 'DRAWCALL',
        'polygon'     : 'DRAWCALL',
        'polyhedron'  : 'DRAWCALL',


        'MANTID_PIXEL': 'MANTID_PIXEL',
        'MANTID_BANANA_DET': 'MANTID_BANANA_DET',
        'MANTID_RECTANGULAR_DET': 'MANTID_RECTANGULAR_DET',
        
    }
    
    # tokens 
    tokens = [
              'LB',
              'RB',
              'COLON',
              'QUOTE',
              'SQUOTE',
              'COMMA',
              'NL',
              
              'ABSPATH',
              'DEC',
              'ID',
              'INSTRNAME',
              'JSON',
             ] + list(set(reserved.values()))
    
    t_LB = r'\('
    t_RB = r'\)'
    t_COLON = r':'
    t_QUOTE = r'"'
    t_SQUOTE = r'\''
    t_COMMA = r','

    def t_ANY_NL(self, t):
        r'\n'
        self.lexer.lineno += 1
        return t
    
    def t_ABSPATH(self, t):
        r'[/\w\\\:\-]+\.instr'
        return t
    
    def t_DEC(self, t):
        r'-?[\d.]+(e[+-][0-9]+)?'
        return t
    
    def t_ID(self, t):
        r'[a-zA-Z_]\w*'
        t.type = self.reserved.get(t.value, 'ID')
        return t
    
    def t_INSTRNAME(self, t):
        r'\w[\w\-0-9]*'
        return t

    def t_JSON(self, t):
        r'\{.*\}'
        return t
    
    # ignore whitespaces and tabs means that we do not tokenize them, but they are still applied in the regex checks defined above for our tokens
    t_ignore = ' \t'
    
    def t_error(self, t):
        print('error: %s' % t.value)
    
    ##################################
    # parsing rules and action code
    ##################################
    
    def p_document(self, p):
        'document : instr_open comp_defs draw_lines instr_end'
        print('instrument definition parsed')
        # quirky: reverse ordering of components
        self.comps.children = self.comps.children[::-1]
        # assemble parse tree
        if not self.mantid: 
            self.parsetree = Node(type='instrdeftree', children=[self.instr, self.comps])
        else:
            self.parsetree = Node(type='instrdeftree', children=[self.instr, self.comps, self.mantid])
    
    instr = None
    def p_instr_open(self, p):
        'instr_open : INSTRUMENT COLON NL INSTRKW SQUOTE instr_name SQUOTE LB ABSPATH RB NL'
        self.instr = Node(type='instrument', children=[p[6], Node(type='abspath', leaf=p[9])])
    
    def p_instr_end(self, p):
        'instr_end : INSTRUMENT END COLON NL'
        
    def p_instr_name(self, p):
        '''instr_name : INSTRNAME
                      | ID '''
        p[0] = Node(type='instr_name', leaf=p[1])
    
    comps = Node(type='comps')
    def p_comp_defs(self, p):
        '''comp_defs : comp_def comp_defs
                     | comp_def '''
        self.comps.children.append(p[1])
        p[0] = self.comps
    
    def p_comp_def(self, p):
        'comp_def : COMPONENT COLON QUOTE comp_name QUOTE NL POS COLON m4 NL'
        v3 = Node(type='v3', leaf=[p[9].leaf[0], p[9].leaf[1], p[9].leaf[2]])
        p[0] = Node(type='comp', children=[p[4], p[9], v3])
    
    def p_comp_name(self, p):
        'comp_name : ID'
        p[0] = Node(type="comp_name", leaf=p[1])
    
    def p_m4(self, p):
        'm4 : DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC'
        p[0] = Node(type='m4', leaf=[p[1], p[3], p[5], p[7], p[9], p[11], p[13], p[15], p[17], p[19], p[21], p[23]])
    
    def p_draw_lines(self, p):
        '''draw_lines : draw_line draw_lines
                      | draw_line '''
    
    def p_draw_line(self, p):
        '''draw_line : draw_header
                     | draw_command
                     | draw_open
                     | draw_close'''
    
    def p_draw_header(self, p):
        '''draw_header : MCDISPLAY COLON COMPKWLC comp_name NL'''
        self.commands = Node(type='draw_commands')
        for comp in self.comps.children:
            for c in comp.children:
                if c.leaf==p[4].leaf:
                    comp.children.append(self.commands)
                    break
    
    commands = None
    def p_draw_command(self, p):
        '''draw_command : MCDISPLAY COLON DRAWCALL LB args RB NL
                        | MCDISPLAY COLON DRAWCALL LB SQUOTE arg SQUOTE RB NL
                        | MCDISPLAY COLON DRAWCALL LB SQUOTE arg SQUOTE COMMA args RB NL
                        | MCDISPLAY COLON DRAWCALL LB SQUOTE SQUOTE RB NL
                        | MCDISPLAY COLON DRAWCALL LB RB NL
                        | MCDISPLAY COLON DRAWCALL json NL
                        | MANTID_PIXEL COLON nineteen_dec NL
                        | MANTID_BANANA_DET COLON eight_dec NL
                        | MANTID_RECTANGULAR_DET COLON seven_dec NL
        '''
        if p[1] in ['MANTID_PIXEL', 'MANTID_BANANA_DET', 'MANTID_RECTANGULAR_DET']:
            if not self.mantid:
                self.mantid = Node(type='mantid', leaf={})
                self.mantid.leaf['MANTID_PIXEL'] = []
                self.mantid.leaf['MANTID_BANANA_DET'] = []
                self.mantid.leaf['MANTID_RECTANGULAR_DET'] = []
            if p[1] == 'MANTID_PIXEL':
                self.mantid.leaf['MANTID_PIXEL'].append(p[3])
                self.commands.children.append(Node(type='mantid', children=[], leaf=MantidPixelLine(p[3])))
            elif p[1] == 'MANTID_RECTANGULAR_DET':
                self.mantid.leaf['MANTID_RECTANGULAR_DET'].append(p[3])
                self.commands.children.append(Node(type='mantid', children=[], leaf=MantidRectangularDetectorLine(p[3])))
            elif p[1] == 'MANTID_BANANA_DET':
                self.mantid.leaf['MANTID_BANANA_DET'].append(p[3])
                self.commands.children.append(Node(type='mantid', children=[], leaf=MantidBananaDetectorLine(p[3])))
            return
        
        # special case: remove first argument of args
        if p[3] == 'multiline':
            self.args.leaf = self.args.leaf[1:]
        
        self.commands.children.append(Node(type='draw', children=[self.args], leaf=p[3]))
        # reset args after having parsed them all, which is now
        self.args = Node(type='args', leaf=[])
    
    def p_nineteen_dec(self, p):
        ''' nineteen_dec : DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC
        '''
        p[0] = [p[2*i+1] for i in range(19)]
    
    def p_eight_dec(self, p):
        ''' eight_dec : DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC
        '''
        p[0] = [p[2*i+1] for i in range(8)]
    
    def p_seven_dec(self, p):
        ''' seven_dec : DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC COMMA DEC
        '''
        p[0] = [p[2*i+1] for i in range(7)]
    
    args = Node(type='args', leaf=[])
    def p_args(self, p):
        '''args : arg COMMA args
                | arg'''
    
    def p_arg(self, p):
        '''arg : DEC
               | ID'''
        self.args.leaf.append(p[1])

    def p_json(self, p):
        'json : JSON'
        self.args.leaf.append(p[1])

    def p_draw_open(self, p):
        '''draw_open : MCDISPLAY COLON STARTKWLC NL'''
    
    def p_draw_close(self, p):
        'draw_close : MCDISPLAY COLON ENDKWLC NL'
    
    # error rule for syntax errors
    def p_error(self, p):
        print("Syntax error in input!")
        print(p)

    ##################################
    # build and test 
    ##################################
    
    def build_lexer(self, **kwargs):
        ''' builds the lexer '''
        self.lexer = lex.lex(module=self, **kwargs)
    
    def test_lexer(self, data):
        ''' test built lexer on data '''
        self.lexer.input(data)
        for token in self.lexer:
            print(token)
    
    def build_parser(self, **kwargs):
        ''' builds the yaccer '''
        self.parser = yacc.yacc(module=self, debug=self.debug, write_tables=False)

    def parse(self, data):
        ''' attempts to parse data '''
        self.parser.parse(data, lexer=self.lexer)


class InstrObjectConstructor:
    '''
    Instrument reconstruction from the syntax tree produced by InstrTraceParser
    '''
    def __init__(self, parsetreeroot):
        self.root = parsetreeroot
        if (type(self.root) is not Node) or (self.root.type != 'instrdeftree'):
            raise Exception('InstrObjectConstructor: parsetreeroot must be a Node of type "instrdeftree"')
    
    def build_instr(self):
        ''' builds and returns the instrument representation object '''
        
        # create instrument object
        instrument_tree = InstrumentSpecific(name='', params=[], params_defaults=[])
        
        # for internal use
        self.compindices = {}
        comp_idx = 0
        
        # iterate through parse tree, parsing instr information and components
        for dc in self.root.children[0:2]:
            # handle instrument branch
            if dc.type == 'instrument':
                for ic in dc.children:    
                    if ic.type == 'instr_name':
                        instrument_tree.name = ic.leaf
                    if ic.type == 'abspath':
                        instrument_tree.abspath = ic.leaf
            
            # handle component branch
            if dc.type == 'comps':
                for csc in dc.children:
                    
                    if csc.type == 'comp':
                        name = ''
                        pos = None
                        rot = None
                        
                        # get name, pos and rot
                        for cc in csc.children:
                            if cc.type == 'comp_name':
                                name = cc.leaf
                            if cc.type == 'm4':
                                pos = Vector3d(x=float(cc.leaf[0]), y=float(cc.leaf[1]), z=float(cc.leaf[2]))
                                # transpose rotation to conform from mcstas trans rot convention to 4x4 transform notation
                                rot = Matrix3(
                                        a11=float(cc.leaf[3]),
                                        a21=float(cc.leaf[4]),
                                        a31=float(cc.leaf[5]),
                                        a12=float(cc.leaf[6]),
                                        a22=float(cc.leaf[7]),
                                        a32=float(cc.leaf[8]),
                                        a13=float(cc.leaf[9]),
                                        a23=float(cc.leaf[10]),
                                        a33=float(cc.leaf[11])
                                        )

                        comp = Component(name=name, pos=pos, rot=rot)
                        
                        # get draw commands (please print a parse tree with NodeTreePrint to understand this)
                        for cc in csc.children:
                            if cc.type == 'draw_commands':
                                for dc in cc.children:
                                    if dc.type == 'mantid':
                                        comp.drawcalls.append(dc.leaf)
                                        continue

                                    # get args
                                    argsnode = dc.children[0]
                                    args = argsnode.leaf
                                    
                                    commandname = dc.leaf
                                    draw = drawclass_factory(commandname, args, reduced=True)
                                    if draw:
                                        comp.drawcalls.append(draw)
                        
                        instrument_tree.components.append(comp)
                        
                        # save component in dictionary-by-name for internal use
                        comp_idx += 1
                        self.compindices[name] = comp_idx

        return instrument_tree

class MantidPixelLine(DrawCommand):
    def __init__(self, line_lst):
        self.line = line_lst
        super().__init__()

class MantidRectangularDetectorLine(DrawCommand):
    def __init__(self, line_lst):
        self.line = line_lst
        super().__init__()

class MantidBananaDetectorLine(DrawCommand):
    def __init__(self, line_lst):
        self.line = line_lst
        super().__init__()