File: mpsclasses.py

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (286 lines) | stat: -rwxr-xr-x 7,210 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python

# mpsclasses.py: show the MPS class hierarchy
# $Id$
# Copyright (c) 2007-2020 Ravenbrook Limited.  See end of file for license.

# This file parses MPS C source for MPS class definitions
# (DEFINE_CLASS() et al), and prints out the class hierarchy.
#
# USAGE:
#
# Invoke it with the list of MPS source files, like this:
#    cd code
#    ../tool/mpsclasses.py *.c
#
# Example output line (to stdout):
#  : : : : : AWLPoolClass  (POOL)  <<< AbstractCollectPoolClass +++ Pool:Format
#
# This means:
#   the MPS class "AWLPoolClass" is defined
#   using the DEFINE_POOL_CLASS macro (not DEFINE_CLASS)
#   its parent is AbstractCollectPoolClass
#   it uses the PoolClassMixInFormat mixin.


# VERSIONS
# -- first checkin --
# Version 06, Version 07
#  tidied and commented
#
# Version 05
#  sorted: shallowest first, then alphabetically
#
# Version 04
#  shows forest of classes, one class per line

import re
import fileinput

# examples:
"""
    DEFINE_CLASS(RankBufClass, class)
    {
      INHERIT_CLASS(class, SegBufClass);
      ...
    }

    DEFINE_POOL_CLASS(AWLPoolClass, this)
    {
      INHERIT_CLASS(this, AbstractCollectPoolClass);
      PoolClassMixInFormat(this);
      ...
    }
"""

#  Grammar:
#  C -> D I? M? E
#


class GramTerm(object):
  """a GramTerm holds information about a terminal in the grammar"""
  def __init__(self, name, patt, show):
    super(GramTerm, self).__init__()
    self.name = name
    self.patt = patt
    self.show = show


# ___D___ -- DEFINE_(<family>_)CLASS ( name
#
patt_D = re.compile( r"""
     DEFINE_
     (?P<family> [A-Z]*) [_]?   # family
     CLASS
 \s* [(]
 \s* (?P<name> [A-Za-z_][A-Za-z0-9_]* )  # name
""", re.VERBOSE)

def show_D(match):
   print ("%s  (%s)" % (match.group("name", "family")))

term_D = GramTerm("D", patt_D, show_D)


# ___I___ -- INHERIT_CLASS ( this , parentname
#
patt_I = re.compile( r"""
     INHERIT_CLASS
 \s* [(]
 \s* ( [A-Za-z_][A-Za-z0-9_]* )              # this
 \s* [,]
 \s* (?P<parentname> [A-Za-z_][A-Za-z0-9_]* )  # parentname
""", re.VERBOSE)

def show_I(match):
  print "  <<< %s" % (match.group("parentname"))

term_I = GramTerm("I", patt_I, show_I)


# ___M___ -- <family>ClassMixIn<mixin> ( this ) ;
#
patt_M = re.compile( r"""
     (?P<family> [A-Za-z_][A-Za-z0-9_]*)   # family
     ClassMixIn
     (?P<mixin> [A-Za-z0-9_]*)   # mixin
 \s* [(]
 \s* ( [A-Za-z_][A-Za-z0-9_]* )              # this
 \s* [)]
 \s* [;]
""", re.VERBOSE)

def show_M(match):
  print "  +++ %s [%s]" % (match.group("mixin", "family"))

term_M = GramTerm("M", patt_M, show_M)


# ___E___ -- }
#
patt_E = re.compile( r"""
     [}]
""", re.VERBOSE)

def show_E(match):
  print "..."

term_E = GramTerm("E", patt_E, show_E)


class NoMoreInput(Exception):
  """NoMoreInput"""

class MPSClass(object):
  """represents an MPS Class, as created by DEFINE_CLASS() et al"""
  def __init__(self, match_D):
    """init from match_D"""
    super(MPSClass, self).__init__()
    self.name, self.family = match_D.group("name", "family")
    self.parentname = None
    self.mixinnames = []
    self.descendants = 0

  def add_I(self, match_I):
    """add_I: parentname to inherit from"""
    assert(self.parentname == None)
    self.parentname = match_I.group("parentname")

  def add_M(self, match_M):
    """add_M: mixinname"""
    self.mixinnames.append("%s:%s" % match_M.group("family", "mixin"))

  def show(self, prefix):
    mix = ""
    sep = " +++ "
    for m in self.mixinnames:
      mix = mix + sep + m
      sep = ", "
    print ("%s%s  (%s)  <<< %s%s"
           % (prefix, self.name, self.family, self.parentname, mix))

def main():
  lines = fileinput.input()
  tops = []
  CChildListfromName = {}
  # (a cmp() function works even with very old Pythons)
  ChildrenSort = lambda C1, C2: cmp(
    [C1.descendants, C1.name.lower()],
    [C2.descendants, C2.name.lower()])

  def calc_descendants_tree(CT):
    for child in CChildListfromName[CT.name]:
      CT.descendants += calc_descendants_tree(child)
    return CT.descendants + 1

  def show_tree(CT, prefix):
    CT.show(prefix)
    CChildListfromName[CT.name].sort(ChildrenSort)
    for child in CChildListfromName[CT.name]:
      show_tree(child, prefix + ": ")

  try:
    while True:
      C = next_C(lines)

      # with C
      #C.show()

      if C.name not in CChildListfromName:
        CChildListfromName[C.name] = []
      if C.parentname == None:
        tops.append(C)
      else:
        if C.parentname not in CChildListfromName:
          CChildListfromName[C.parentname] = []
        CChildListfromName[C.parentname].append(C)

      C = None

  except NoMoreInput:
    assert(C == None)

  tot = 0
  for CT in tops:
    tot += calc_descendants_tree(CT)

  tops.sort(ChildrenSort)
  for CT in tops:
    show_tree(CT, "")
    print
  print "%d classes in total" % tot

def next_C(lines):
  """find the next class definition C"""

  # .DIME -- find next DEFINE_CLASS
  for l in lines:
    match_D = term_D.patt.search(l)
    if match_D != None:
      break
    for t in (term_I, term_M):
      match = t.patt.search(l)
      if match != None:
        print "ERROR parsing .DIME: got I or M: ", l
        raise
  else:
    raise NoMoreInput()
  # with match_D
  #term_D.show(match_D)
  C = MPSClass(match_D)

  # D.IME -- find next InheritFrom, Mixin, or End
  for l in lines:
    for t in (term_D, term_I, term_M, term_E):
      match = t.patt.search(l)
      if match != None:
        break
    else:
      continue
    # with t
    #t.show(match)
    if t == term_D:
      print "ERROR parsing D.IME: got (another) D; expected I, M, or E"
      raise
    elif t == term_I:
      C.add_I(match)
    elif t == term_M:
      C.add_M(match)
    elif t == term_E:
      return C
  else:
    raise NoMoreInput()

if __name__ == "__main__":
  main()


# C. COPYRIGHT AND LICENSE
#
# Copyright (C) 2007-2020 Ravenbrook Limited <https://www.ravenbrook.com/>.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the
#    distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#