1- from helper import solo , comma
1+ from helper import solo , comma , full
22
33def clean (u , fn , fc ):
44 # pre-populate arrays
@@ -25,20 +25,24 @@ def clean(u, fn, fc):
2525 # open new file to write
2626 with open (new , 'w' ) as newF :
2727 soloFlag , multiFlag , newline , totalFlag , end = False , False , False , False , False
28+ # for each line
2829 for num , line in enumerate (f , 1 ):
2930 # if haven't seen all problem lines
3031 if nums [i ]:
3132 # if line has an unused rule
3233 if num == nums [i ][0 ]:
3334 # remove line num about to be cleaned
3435 del nums [i ][0 ]
35- x = solo (line )
36- soloFlag = x
37- multiFlag = not (x )
36+ # how many rules are on this line?
37+ soloFlag = solo (line )
38+ multiFlag = not (soloFlag )
39+ # is the entire rule defined on this line?
40+ y = full (line )
3841 # if multiple rules
3942 if multiFlag :
4043 l = line .split ()
4144 ll = len (l )
45+ # loop thru words in rule header
4246 for x in range (ll ):
4347 # replace unused rules with ?
4448 if comma (l [x ]) in rules [i ]:
@@ -55,6 +59,7 @@ def clean(u, fn, fc):
5559 else :
5660 soloFlag = True
5761 totalFlag = True
62+ # re-assemble line from list
5863 line = ' ' .join (l ) + '\n '
5964 multiFlag = False
6065 # if one rule
@@ -71,12 +76,17 @@ def clean(u, fn, fc):
7176 if not newline or line != '\n ' :
7277 newF .write (line )
7378 newline = True if line == '\n ' else False
74- # hack: remove definition of last rule
75- else :
79+ # remove definition of last rule, unless it was all on one line
80+ elif not nums [ i ] and not end and not y :
7681 for c in line :
7782 if end :
7883 newF .write (c )
84+ # keep reading until definition ends
7985 if c == '}' :
8086 end = True
87+ # no need to examine char at a time
88+ else :
89+ newF .write (line )
90+
8191 print (f'Wrote { new } ' )
8292 newF .close ()
0 commit comments