2 """reformat noweb and compiler errors for LyX.
4 Expects to read from stdin and output to stdout.
7 __author__ = "Kayvan A. Sylvan <kayvan@sylvan.com>"
8 __date__ = "$Date: 2002/03/19 21:42:48 $"
9 __version__ = "$Revision: 1.1 $"
10 __credits__ = """Edmar Wienskoski Jr. <edmar-w-jr@technologist.com>
11 original Literate support for LyX.
12 Bernard Michael Hurley <berhardh@westherts.ac.uk>
13 modifications to original listerrors."""
14 __copyright__ = "Copyright 2002 - The LyX team."
18 def write_error(msg, tool = "noweb", line_number = 1):
19 """Write out the given message in TeX error style.
21 called like: write_error(msg, tool, line_number)."""
22 print "! Build Error: ==> %s ==>\n" % (tool),
23 print " ...\n\nl.%d ...\n" % (line_number),
24 if type(msg) == type("str"): # simple string
26 else: # some kind of list (sequence or tuple)
31 __lines = [] # lines pushed back
33 def getline(file = sys.stdin):
34 """read a line from internal stack or from file.
36 optional file argument defaults to sys.stdin."""
43 line = file.readline()
47 "push a line onto the pushback stack."
50 lines += (line,) # push a list onto the stack, not individual letters
53 """Entry point for listerrors. Takes no options.
55 Reads stdin and writes to stdout. Filter errors"""
60 try_patterns_dispatch = [ noweb_try, gcc_try, xlc_try ]
61 for predicate in try_patterns_dispatch:
62 if predicate(line): break
64 """see if line is a noweb error.
66 Returns 1 on success, 0 otherwise. Outputs on stdout."""
68 if line.find(": unescaped << in documentation chunk") != -1:
69 line_parts = line.split(':')
70 num_str = line_parts[1]
71 num_len = len(num_str)
73 while i < num_len and num_str[i].isdigit(): i += 1
75 write_error(":" + line_parts[2], "noweb", int(num_str))
78 left = line.find("<<")
79 if (left != -1) and ((left + 2) < len(line)) and \
80 (line[left+2:].find(">>") != -1):
81 write_error(line, "noweb");
84 msgs_to_try = ("couldn't open file",
85 "couldn't open temporary file",
86 "error writing temporary file",
89 "Bad format sequence",
90 "Can't open output file",
91 "Can't open temporary file",
93 "Ignoring unknown option -",
95 "non-numeric line number in")
96 for msg in msgs_to_try:
97 if line.find(msg) != -1:
98 write_error(line, "noweb")
104 """See if line is a gcc error. Read ahead to handle all the lines.
106 Returns 1 on success, 0 otherwise. Outputs on stdout."""
108 first_space = line.find(' ')
109 if first_space > 1: # The smallest would be "X: "
110 if line[first_space - 1] == ':':
111 header_to_see = line[:first_space - 1]
112 next_line = getline()
113 if next_line and next_line[:first_space - 1] == header_to_see:
114 num_end = first_space
115 while next_line[num_end].isdigit(): num_end += 1
116 if num_end > first_space: # good!
117 num_str = next_line[first_space:num_end]
119 msgs += (line[first_space:],)
120 msgs += (next_line[num_end + 1:],)
121 header_to_see = next_line[:num_end]
122 next_line = getline()
123 while next_line and next_line[:num_end] == header_to_see:
124 msgs += (next_line[num_end + 1:],)
125 next_line = getline()
126 if next_line: pushline(next_line)
127 write_error(msgs, "gcc", int(num_str))
129 else: # oops! Not a gcc error.
132 pushline(next_line) # return this line to input stream
136 """see if line is an xlc error.
138 Returns 1 on success, 0 otherwise. Outputs on stdout."""
140 if line[0] == '"': # This is the first character of all xlc errors
141 next_quote = line.find('"', 1)
142 first_space = line.find(' ')
143 if (next_quote != -1) and (first_space > next_quote): # no space inisde quotes
144 if line[first_space - 1:first_space + 6] == ", line ":
145 num_start = num_end = first_space + 6
146 while line[num_end].isdigit(): num_end += 1
147 if num_end > num_start:
148 write_error(line, "xlc", int(line[num_start : num_end]))
153 if __name__ == "__main__":