1 # Copyright 2012 DWARF Debugging Information Format Committee
3 # All the little classes used in storing latex source data.
4 # Reads in the tex source and builds internal lists of the
5 # tokenized source. The tokenization is adequate
6 # for our purposes, but just barely adequate.
16 if isIndivid(c) == "y":
18 if ord(c) >= ord('a') and ord(c) <= ord('z'):
20 if ord(c) >= ord('A') and ord(c) <= ord('Z'):
22 # It is tex/latex, so backslash starts a word.
30 if isIndivid(c) == "y":
32 if ord(c) >= ord('a') and ord(c) <= ord('z'):
34 if ord(c) >= ord('A') and ord(c) <= ord('Z'):
36 if ord(c) >= ord('0') and ord(c) <= ord('9'):
38 # This is so we allow the colon in our tags
39 # Unfortunately, this gives trouble if we have a
40 # : at the end of a DW* name on input.
71 # Class is "id", "ind","other","none"
73 def insertid(self,string):
75 self._tex = list(string)
76 self._underbar = self._tex
78 self._label = self._tex
79 def setIndivid(self,c):
85 def setInitialIdChar(self,c):
88 def setNextIdChar(self,c):
91 def setInitialOther(self,c):
97 def setNextOther(self,c):
102 self._class = "other"
103 def finishUpId(self):
104 """ This transforms the strings from the input form into
105 the internal forms we want.
112 while int(n) < len(self._tex):
114 if n < (len (self._tex) - 1) and c == "\\" and self._tex[n+1] == "-":
117 self._underbar += [c]
121 while int(n) < len(self._underbar):
122 c = self._underbar[n]
123 if n < (len (self._underbar) - 1) and c == "\\" and self._underbar[n+1] == "_":
130 while int(n) < len(self._std):
138 def dwprintquotedshortform(self,d):
139 print "'",self.shortform(d),"'",
140 def shortform(self,d):
143 if self._class == "ind":
145 self.dwprintquotedshortform(self._tex)
148 # This prints the token with end-line oddly.
150 self.dwprintquotedshortform(self._tex)
151 self.dwprintquotedshortform(self._underbar)
152 self.dwprintquotedshortform(self._std)
153 self.dwprintquotedshortform(self._label)
155 def dwwrite(self,outfile):
160 """using an input line, create a list of tokens for the line.
161 Legal class transitions in tokenize() are:
175 def tokenize(self,rec):
176 """using an input line, create a list of tokens for the line.
177 Legal class transitions in tokenize() are:
192 if keepcomments == "d" and c == "%" and ( charnum == 0 or rec[charnum - 1] != "\\" ):
193 # Not keeping comments. We drop % and following to end of line
194 # unless preceeded by \
197 if c == "\n" or c == "\r":
198 # Just drop these for now. Allowing them
199 # would not be harmful.
201 elif dwclass == "none" or dwclass == "ind":
202 if isIndivid(c) == "y":
207 if isIdStart(c) == "y":
208 combotok.setInitialIdChar(c)
212 combotok.setInitialOther(c)
215 elif dwclass == "id":
216 if isIdNext(c) == "y":
217 combotok.setNextIdChar(c)
219 if isIndivid(c) == "y":
220 combotok.finishUpId()
221 self._toks += [combotok]
228 # Other class input, other starts here.
229 combotok.finishUpId()
230 self._toks += [combotok]
232 combotok.setInitialOther(c);
235 elif dwclass == "other":
236 if isIndivid(c) == "y":
237 self._toks += [combotok]
244 if isIdStart(c) == "y":
245 self._toks += [combotok]
247 combotok.setInitialIdChar(c);
250 combotok.setNextOther(c);
252 # Else case impossible.
254 #Finish up final non-empty other or id token
256 combotok.finishUpId()
257 self._toks += [combotok]
259 if dwclass == "other":
260 self._toks += [combotok]
262 def dwprint(self,linenum):
263 print "Number of tokens in line ",linenum," : ",len(self._toks)
264 if len(self._toks) == 0:
265 #Just print an empty line.
270 def dwwrite(self, outfile, linenum):
274 def dwtransformline(self,callfunc,myfile,lnum):
275 toks = callfunc(self._toks,myfile,lnum)
280 def __init__(self,name):
286 file = open(name,"r");
287 except IOError, message:
288 print >> sys.stderr , "File could not be opened: ", name
292 rec = file.readline()
301 self._lines += [aline]
304 print "Number of lines in ", self._name, ": ",len(self._lines)
306 for l in self._lines:
310 # The lnum is just for debugging messages.
312 outname = self._name + ".out"
315 outfile = open(outname,"w");
316 except IOError, message:
317 print >> sys.stderr , "Output File could not be opened: ", name
320 for l in self._lines:
321 l.dwwrite(outfile,lnum)
323 def dwtransformline(self,callfunc,myfile):
325 for l in self._lines:
326 l.dwtransformline(callfunc,myfile,lnum)
336 def addFile(self,name):
341 print "Number of files: ",len(self._files);
342 for f in self._files:
345 for f in self._files:
347 def dwtransformline(self,callfunc):
348 for f in self._files:
349 f.dwtransformline(callfunc,f)
352 def setkeepordeletecomments(val):
353 """ Pass in "k" or "d" to keep or delete comments, respectively """
357 def readFilelist(filelist):