tools/README: Documented what each .py file does.
[dwarf-doc.git] / dwarf5 / tools / dellivelink.py
1 # Copyright 2013 DWARF Debugging Information Format Committee
2 # This reads the input tokens and then
3 # writes them out in new files.  
4
5 # October 5, 2013. 
6 # This replaces \livelink and \livetarg with 
7 # a simpler \DWXXXyyy, per Ron Brender email of Oct 4, 2013
8 # It strives to be idempotent, so rerunning makes no 
9 # further changes.
10
11 import sys
12 import fileio
13
14 insidealltt = "n"
15
16 def ischar(tok,c):
17    if tok._class != "ind":
18       return "n"
19    if len(tok._tex) != 1:
20        return "n"
21    if tok._tex[0] != c:
22        return "n"
23    return "y"
24
25 def tokhasdigits(tok):
26   for c in tok._label:
27     if c >= '0' and c <= '9' and len(c) == 1:
28         return "y"
29   return "n"
30
31 def append_to_out(out,addthese):
32   for a in addthese:
33     out += [a]
34
35 def myjoinlabel(tok):
36   s = ''.join(tok._label)
37   return s
38
39 def doreplace(toks,curtoknum,lasttoknum):
40   if int(curtoknum) + 6 > int(lasttoknum):
41     return "n"
42   if toks[curtoknum+1]._class != "ind" or myjoinlabel(toks[curtoknum+1]) != "{":
43     return "n"
44   if toks[curtoknum+2]._class != "id":
45     return "n"
46   if toks[curtoknum+3]._class != "ind" or myjoinlabel(toks[curtoknum+3]) != "}":
47     return "n"
48   if toks[curtoknum+4]._class != "ind" or myjoinlabel(toks[curtoknum+4]) != "{":
49     return "n"
50   if toks[curtoknum+5]._class != "id":
51     return "n"
52   if myjoinlabel(toks[curtoknum+5]).startswith("DW") == 0:
53     return "n"
54   if toks[curtoknum+6]._class != "ind" or myjoinlabel(toks[curtoknum+6]) != "}":
55     return "n"
56   if tokhasdigits(toks[curtoknum+5]) == "y":
57     print "There are digits in ", myjoinlabel(toks[curtoknum+5]),": it is not converted"
58     return "n"
59   return "y"
60
61
62 def newt(toks,curtoknum,finalchars,):
63   s = myjoinlabel(toks[curtoknum + 5])
64   s2 = "\\" + s + finalchars
65   t = fileio.dwtoken()
66   t.insertid(s2)
67   return t
68
69 def checkalltt(linetoks,tnumin,lasttoknum,myfile,linenum):
70     global insidealltt
71     if ( int(tnumin) + 3 ) > int(lasttoknum):  
72       return "n"
73     t1 = linetoks[tnumin]
74     s1 = myjoinlabel(t1)
75     if  s1 != "\\begin" and s1 != "\\end":
76       return "n"
77     t2 = linetoks[tnumin+1]
78     s2 = myjoinlabel(t2)
79     if  s2 != "{":
80       return "n"
81
82     t3 = linetoks[tnumin+2]
83     s3 = myjoinlabel(t3)
84     if  s3 != "alltt":
85       return "n"
86
87     t4 = linetoks[tnumin+3]
88     s2 = myjoinlabel(t4)
89     if  s2 != "}":
90       return "n"
91
92     if  s1 == "\\begin":
93
94       if insidealltt =="y":
95         print "nested \\begin{alltt} is an error in",myfile._name, " at ",linenum
96       insidealltt = "y" 
97     if  s1 == "\\end":
98       if insidealltt =="n":
99         print "nested \\end{alltt} is an error in",myfile._name, " at ",linenum
100       insidealltt = "n" 
101     return "y"
102
103 def transfunc(linetoks,myfile,linenum):
104   global insidealltt
105   if len(linetoks) < 1:
106     return linetoks
107   tnumin = 0
108   changes = 0
109   lasttoknum = len(linetoks) -1
110   outtoks = []
111   for x in linetoks:
112     if int(tnumin) > int(lasttoknum):
113       break
114     t = linetoks[tnumin]
115     rawtok = ''.join(t._tex)
116     if  rawtok == "\\newcommand" and tnumin == 0:
117         # Do not touch newcommand stuff
118         return linetoks
119     checkalltt(linetoks,tnumin,lasttoknum,myfile,linenum)
120     if  rawtok == "\\livelink" and "y" == doreplace(linetoks,tnumin,lasttoknum):
121         newtok = newt(linetoks,tnumin,"")
122         outtoks += [newtok]
123         tnumin += 7
124         if int(tnumin)  <= int(lasttoknum):
125           # not at end of line, Check to see if we want a {} or not.
126           if  insidealltt == "n" and linetoks[tnumin]._class == "ind" and  myjoinlabel(linetoks[tnumin]) == " ":
127             newtlb = fileio.dwtoken();
128             newtlb.setIndivid("{")
129             outtoks += [newtlb]
130             newtrb = fileio.dwtoken();
131             newtrb.setIndivid("}")
132             outtoks +=  [newtrb]
133     elif  rawtok == "\\livetarg" and "y" == doreplace(linetoks,tnumin,lasttoknum):
134         newtok = newt(linetoks,tnumin,"TARG")
135         outtoks += [newtok]
136         tnumin += 7
137         if int(tnumin) <= int(lasttoknum):
138           # not at end of line. Check to see if we want a {} or not.
139           if insidealltt == "n" and linetoks[tnumin]._class == "ind" and  myjoinlabel(linetoks[tnumin]) == " ":
140             newtlb = fileio.dwtoken();
141             newtlb.setIndivid("{")
142             outtoks += [newtlb]
143             newtrb = fileio.dwtoken();
144             newtrb.setIndivid("}")
145             outtoks +=  [newtrb]
146     else:
147       tnumin = tnumin+ 1
148       outtoks += [t]
149     # End of for loop.
150   return outtoks
151
152 def read_args():
153   cur = 1
154   filelist = []
155   while  len(sys.argv) > cur:
156     v = sys.argv[cur]
157     filelist += [v]
158     cur = int(cur) + 1
159
160   filelist = {"..\\latexdoc\\attributesbytag.tex",
161               "..\\latexdoc\\changesummary.tex",
162               "..\\latexdoc\\compression.tex",
163               "..\\latexdoc\\copyright.tex",
164               "..\\latexdoc\\dataobject.tex",
165               "..\\latexdoc\\datarepresentation.tex",
166               "..\\latexdoc\\debugsectionrelationships.tex",
167               "..\\latexdoc\\dwarfnamecmds.tex",
168               "..\\latexdoc\\encodingdecoding.tex",
169               "..\\latexdoc\\examples.tex",
170               "..\\latexdoc\\generaldescription.tex",
171               "..\\latexdoc\\gnulicense.tex",
172               "..\\latexdoc\\introduction.tex",
173               "..\\latexdoc\\otherdebugginginformation.tex",
174               "..\\latexdoc\\programscope.tex",
175               "..\\latexdoc\\sectionversionnumbers.tex",
176               "..\\latexdoc\\typeentries.tex"
177                                 }
178   dwf = fileio.readFilelist(filelist)
179   dwf.dwtransformline(transfunc)
180   dwf.dwwrite()
181
182 if __name__ == '__main__':
183   read_args()
184   
185
186