Now uses.py properly reads in tex (for its purposes)
[dwarf-doc.git] / dwarf5 / tools / refclassfixup.py
1 # Copyright 2012 DWARF Debugging Information Format Committee
2 # This reads the input tokens and then
3 # writes them out in new files.  
4 # Used to verify that the output matches the input byte-for-byte
5
6 import sys
7 import fileio
8
9 refclass = { 
10 #"address":"chap:address", 
11 #"block":"chap:block", 
12 #"constant":"chap:constant", 
13 "exprloc":"chap:exprloc", 
14 #"flag":"chap:flag",
15 "lineptr":"chap:lineptr",
16 "loclistptr":"chap:loclistptr",
17 "macptr":"chap:macptr",
18 "rangelistptr":"chap:rangelistptr" }
19 #"reference":"chap:reference",
20 #"string":"chap:string" }
21
22 def ischar(tok,c):
23    if tok._class != "ind":
24       return "n"
25    if len(tok._tex) != 1:
26        return "n"
27    if tok._tex[0] != c:
28        return "n"
29    return "y"
30
31 def transformone(tok, string):
32   label = refclass[string]
33   # output is  \livelink{label}{string}
34   t1=fileio.dwtoken()
35   t1.insertid("\livelink")
36   t2=fileio.dwtoken()
37   t2.setIndivid("{")
38   t3=fileio.dwtoken()
39   t3.insertid(label)
40   t4=fileio.dwtoken()
41   t4.setIndivid("}")
42   t5 = t2
43   t6=fileio.dwtoken()
44   t6.insertid(string) 
45   t7 = t4
46   return [t1,t2,t3,t4,t5,t6,t7]
47
48 def append_to_out(out,addthese):
49   for a in addthese:
50     out += [a]
51
52 def transfunc(linetoks,myfile,linenum):
53   if len(linetoks) < 1:
54     return linetoks
55   tnumin = 0
56   changes = 0
57   lasttoknum = len(linetoks) -1
58   outtoks = []
59   for t in linetoks:
60     rawtok = ''.join(t._tex)
61     if  refclass.has_key(rawtok) == 1:
62       if tnumin == 0 or tnumin == lasttoknum:
63          tmp = transformone(t, rawtok);
64          changes = changes +  1
65          append_to_out(outtoks,tmp)
66       else:
67         if  ischar(linetoks[tnumin - 1],"{") == "n" and ischar(linetoks[tnumin+1],"}") == "n":
68           tmp = transformone(t, rawtok);
69           append_to_out(outtoks,tmp)
70           changes = changes +  1
71         else:
72           outtoks += [t]
73     else:
74       outtoks += [t]
75     tnumin = tnumin+ 1
76     # End of for loop.
77   return outtoks
78
79 def read_args():
80   cur = 1
81   filelist = []
82   while  len(sys.argv) > cur:
83     v = sys.argv[cur]
84     filelist += [v]
85     cur = int(cur) + 1
86
87   dwf = fileio.readFilelist(filelist)
88   dwf.dwtransformline(transfunc)
89   dwf.dwwrite()
90
91 if __name__ == '__main__':
92   read_args()
93   
94
95