File: file.py

package info (click to toggle)
mma 21.09-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 51,828 kB
  • sloc: python: 16,751; sh: 26; makefile: 18; perl: 12
file content (348 lines) | stat: -rw-r--r-- 9,925 bytes parent folder | download | duplicates (2)
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
# file.py

"""
This module is an integeral part of the program
MMA - Musical Midi Accompaniment.

This program 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 2 of the License, or
(at your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Bob van der Poel <bob@mellowood.ca>

"""

import sys
import os
from . import gbl
from   MMA.common import *
import MMA.debug

PY3 = sys.version_info[0] == 3


def fixfname(f):
    """ Convert embedded space characters in filename to real spaces and
        expand ~ for user home directory.

        Originally this was done with .decode("string-escape") but that
        doesn't work with windows path names. So, now we just replace
        any \x20 sequences with single spaces.

        
    """

    f = f.replace('\\x20', ' ')
    return os.path.expanduser(f)


def locFile(name, lib):
    """ Locate a filename.

        This checks, in order:
          lib/name + .mma
          lib/name
          name + .mma
          name
    """

    ext = gbl.EXT
    exists = os.path.exists

    name = fixfname(name)  # for ~ expansion only
    
    if lib:
        if not name.endswith(ext):
            t = os.path.join(lib, name + ext)
            if exists(t):
                return t
        t = os.path.join(lib, name)
        if exists(t):
            return t

    if not name.endswith(ext):
        t = name + ext
        if exists(t):
            return t

    if exists(name):
        return name

    return None


###########################
# File read class
###########################


class ReadFile:

    class FileData:
        """ After reading the file in bulk it is parsed and stored in this
            data structure. Blanks lines and comments are removed.
        """

        def __init__(self, lnum, data, label):
            self.lnum = lnum
            self.data = data
            self.label = label

    def __init__(self, fname):

        self.fdata = fdata = []
        self.lastline = None
        self.lineptr = None
        self.fname = None

        self.que = []     # que for pushed lines (mainly for REPEAT)
        self.qnums = []
        #self.atEOFlines = []

        dataStore = self.FileData  # shortcut to avoid '.'s

        if fname == 1:
            inpath = sys.stdin
        else:
            try:
                if PY3:
                    inpath = open(fname, 'r', encoding=gbl.encoding)
                else:
                    inpath = open(fname, 'r')
            except IOError:
                error("Unable to open '%s' for input" % fname)
 
        if MMA.debug.debug or MMA.debug.showFilenames:
            dPrint("Opening file '%s'." % fname)

        self.fname = fname

        """ Read entire file, line by line:
             - strip off blanks, comments
             - join continuation lines
             - parse out LABELS
             - create line numbers
        """

        lcount = 0
        label = ''
        labs = []     # track label defs, error if duplicate in same file
        nlabs = []    # track linenumber label defs
        inComment = False  # multiline comment flag

        while 1:
            l = inpath.readline()

            if not l:        # EOF
                if inComment:
                    error("Multiline comment (/* */) not terminated.")
                break

            l = l.strip()
            lcount += 1

            if not l:
                continue

            # join lines ending in '\' - the strip() makes this the last char

            while l[-1] == '\\':
                l = l[0:-1] + ' ' + inpath.readline().strip()
                lcount += 1

            """ input cleanup ... for now the only cleanup is to convert
                0xa0 (non-breakable space) to 0x20 (regular space).
            """

            l = l.replace('\xa0', '\x20')

            # multiline comments
            if inComment:
                if "/*" in l:
                    warning ("Block comment start '/*' found inside comment.")
                if "*/" in l:
                    _, _, l = l.partition("*/")
                    inComment = False
                else:
                    continue

            l = l.split('//', 1)[0]  # Strip off line comment

            while "/*" in l:
                if "*/" in l:
                    t1, _, t2 = l.partition("/*")
                    t3, _, t4 = t2.partition("*/")
                    l = t1 + t4
                else:
                    l = l.split("/*")[0]
                    inComment = True

            """ Splits the remaining line into tokens using whitespace delimiters.
                Note that split() will strip off trailing and leading
                spaces, so a strip() is not needed here.
            """

            l = l.split()

            if not l:
                continue

            """ Parse out label lines. There are 2 different kinds of labels:
                - LABEL XXX
                   and
                - NNN

                The first kind is treated as an exclusive. If a NNN label or previous
                XXX duplicates, an error is generated.

                The LINE NUMBER type is not exclusive. If a duplicate NNN is found, the
                last one is used.

                XXX NNN types can not duplicate each other.

                Also note that XXX lines are stripped from input as well as NNN lines
                with only a NNN.
            """

            if l[0].upper() == 'LABEL':
                if len(l) != 2:
                    gbl.lineno = lcount
                    error("Usage: LABEL <string>")
                label = l[1].upper()
                if label[0] == '$':
                    gbl.lineno = lcount
                    error("Variables are not permitted as labels")
                if label in labs:
                    gbl.lineno = lcount
                    error("Duplicate label specified in line %s" % lcount)
                elif label in nlabs:
                    gbl.lineno = lcount
                    error("Label '%s' duplicates line number label" % label)
                labs.append(label)

            elif l[0].isdigit():
                label = l[0]

                if label in labs:
                    gbl.lineno = lcount
                    error("Line number '%s' duplicates LABEL" % label)

                if not label in nlabs:
                    nlabs.append(label)
                else:
                    for i, a in enumerate(fdata):
                        if a.label == label:
                            fdata[i].label = ''

            else:
                label = None

            # Save the line, linenumber and (maybe) the label.

            fdata.append(dataStore(lcount, l, label))

        inpath.close()

        self.lineptr = 0
        self.lastline = len(fdata)

    def toEof(self):
        """ Move pointer to End of File. """

        self.lineptr = self.lastline+1
        self.que = []
        self.qnums = []

    def goto(self, l):
        """ Do a goto jump.

            This isn't perfect, but is probably the way most GOTOs work. If
            inside a repeat/if then nothing more is processed. The jump is
            immediate. Of course, you'll run into problems with missing
            repeat/repeatend if you try it. Since all repeats are stacked
            back into the que, we just delete the que. Then we look for a
            matching label in the file line array.

            Label search is linear. Not too efficient, but the lists
            will probably never be that long either.

        """

        if not l:
            error("No label specified")

        if self.que:
            self.que = []

        for i, a in enumerate(self.fdata):
            if a.label == l:
                self.lineptr = i
                return

        error("Label '%s' has not been set" % l)

    def pushEOFline(self, ln):
        self.fdata.append(self.FileData(gbl.lineno, ln, ''))
        self.lastline+=1

    def push(self, q, nums):
        """ Push a list of lines back into the input stream.

            Note: This is a list of semi-processed lines, no comments, etc.

            It's quicker to extend a list than to insert, so add to the end.
            Note: we reverse the original, extend() then reverse again, just
            in case the caller cares.

            nums is a list of linenumbers. Needed to report error lines.
        """

        if not self.que:
            self.que = ['']
            self.qnums = [gbl.lineno]

        q.reverse()
        self.que.extend(q)
        q.reverse()

        nums.reverse()
        self.qnums.extend(nums)
        nums.reverse()

    def read(self):
        """ Return a line.

            This will return either a queued line or a line from the
            file (which was stored/processed earlier).
        """

        while 1:
            if self.que:            # Return a queued line if possible.
                ln = self.que.pop(-1)

                gbl.lineno = self.qnums.pop()

                if not ln:
                    continue

                return ln

            # Return the next line in the file.
            if self.lineptr >= self.lastline:
                return None  # EOF

            ln = self.fdata[self.lineptr].data
            gbl.lineno = self.fdata[self.lineptr].lnum
            self.lineptr += 1

            return ln