summaryrefslogtreecommitdiff
path: root/common/lib/mmtcgen/core.py
blob: b6eae0cc90490168dc3784d59899484dcd0cd84b (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
#!/usr/bin/python

#############################################################################
#  Copyright (C) 2010 Spidcom
#
#############################################################################

import os
from const import *

class MMTypeConstGen:
    """MMType Generation."""
    def __init__ (self, infile, outfile = None):
        """Initialise class.
        infile: the input file to parse.
        outfile: the output file to write, default stdout.
        """
        self.infile = infile
        self.outfile = outfile

    def __check_entries (self, mmtypelist):
        """Check entries MMType duplicity and Base."""
        for i in range (len(mmtypelist)):
            mmtype = int (mmtypelist[i].value, 0)
            for c in MMTYPES_MIN_MAX:
                if mmtypelist[i].name.startswith(c[0]) \
                    and (mmtype < c[1] or mmtype > c[2]):
                    msg = "%s has an MME Type outside bounds" \
                            % mmtypelist[i].name
                    raise RuntimeError (msg)
            if mmtype & 0x3 != 0:
                msg = "%s base value must be 0x0, 0x4, 0x8, 0xC" \
                        % mmtypelist[i].name
                raise RuntimeError (msg)
            for j in range (i + 1, len (mmtypelist)):
                for c in MMTYPES_MIN_MAX:
                    if mmtypelist[i].name.startswith(c[0]):
                        len_i = len (c[0])
                    if mmtypelist[j].name.startswith(c[0]):
                        len_j = len (c[0])
                if mmtypelist[i].value == mmtypelist[j].value \
                    and mmtypelist[i].name[0:len_i] \
                        == mmtypelist[j].name[0:len_j]:
                    msg = "Duplicated MMType value for %s %s" % \
                            (mmtypelist[i].name, mmtypelist[j].name)
                    raise RuntimeError (msg)

    def writeOutputFile (self):
        """Store the parse data into the output file.
        Only header files for C code is supported."""
        from c import C
        cheader = C ()
        # defines part.
        defines = ""
        enums = ""
        for i in self.mmtypelist:
            defines += cheader.line  % (i.name, i.value)
            for j in i.types:
                data = MME_SUBTYPES[j]
                enums += \
                    cheader.mmtypes % (i.name + data[0], i.name, data[1])
        for i in MME_SUBTYPES.iterkeys ():
            i = MME_SUBTYPES[i]
            defines += cheader.line % (i[1], i[2])
        mmtypes_min_max = ""
        defines_is = ""
        for i in MMTYPES_MIN_MAX:
            mmtypes_min_max += \
                cheader.mmtypes_min_max % ("%s_MIN" % i[0], i[1])
            mmtypes_min_max += \
                cheader.mmtypes_min_max % ("%s_MAX" % i[0], i[2])
            defines_is += \
                cheader.define_is % (i[0], "%s_MIN" % i[0], "%s_MAX" % i[0])
        data = cheader.template.substitute (
                {'defines':defines, 'mmtypes':enums,
                    'mmtypes_min_max':mmtypes_min_max,
                    'defines_is': defines_is})
        if self.outfile:
            f = open (self.outfile, 'w')
            f.write (data)
            f.close ()
        else:
            print data

    def parse (self):
        """Parse file and export."""
        try:
            from parser import parse
        except:
            raise RuntimeError ("run yapps on parser.g (package yapps2)")
        f = open (self.infile, 'r')
        self.mmtypelist = parse ('parse', f.read())
        f.close ()
        self.__check_entries (self.mmtypelist)

    def process (self):
        """Parse and write output file."""
        self.parse ()
        self.writeOutputFile ()

    def mmtype_dict (self):
        """Convert the mmtype list got after parsing the file into a
        dictionary."""
        self.parse ()
        mmtypes_dict = {}
        for i in self.mmtypelist:
            for j in i.types:
                subtype = MME_SUBTYPES[j]
                mmtype_name = "".join ([i.name, subtype[0]])
                mmtypes_dict[mmtype_name] = \
                        int (i.value, 0) + int (subtype[2], 0)
        return mmtypes_dict