-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathparse_errors.py
More file actions
204 lines (163 loc) · 5.92 KB
/
parse_errors.py
File metadata and controls
204 lines (163 loc) · 5.92 KB
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
#!/usr/bin/env python3
# pyre-strict
import re
from dataclasses import dataclass
from enum import Enum
from typing import IO, List, Tuple
@dataclass
class ErrorCode:
type: str
code: int
@dataclass
class Position:
fileName: str
line: int
startColumn: int
endColumn: int
@dataclass
class PositionedMessage:
position: Position
message: str
class Severity(Enum):
ERROR = "ERROR"
WARNING = "WARN"
def __format__(self, spec) -> str:
return str.__format__(str(self.name), spec)
@dataclass
class Error:
severity: Severity
code: ErrorCode
message: PositionedMessage
reason: List[PositionedMessage]
class ParseException(Exception):
pass
def make_error(
severity: Severity,
errorCode: ErrorCode,
position: Position,
message: str,
reasons: List[PositionedMessage],
) -> Error:
return Error(severity, errorCode, PositionedMessage(position, message), reasons)
def end_of_file(line: str) -> bool:
return line == "" or line == "\n"
def parse_errors(output_file_name: str) -> List[Error]:
with open(output_file_name) as output_file:
try:
return parse_error(output_file, True)
except ParseException:
try:
return parse_error(output_file, False)
except ParseException as ex:
raise ParseException(f"at file {output_file_name}: {ex}")
def same_error(line: str, multiple_error_file: bool) -> bool:
if multiple_error_file:
return starts_with_space(line)
else:
return not end_of_file(line)
# parse a "plain" formatted error. The expected format is emittted by
# Errors.to_string (OCaml) or Error::display_plain() (Rust).
def parse_error(output_file: IO[str], multiple_error_file: bool) -> List[Error]:
errors = []
line = output_file.readline()
while not end_of_file(line):
if line == "No errors\n":
return []
severity_string, position_string = line.split(": ", 1)
severity = Severity(severity_string)
position = parse_position(line)
line = output_file.readline()
if starts_with_space(line):
# this is part of an hh_show, so ignore and continue
while starts_with_space(line):
line = output_file.readline()
continue
(message, errorCode) = parse_message_and_code(output_file, line)
line = output_file.readline()
reasons = []
while same_error(line, multiple_error_file):
reasonPos = parse_position(line)
line = output_file.readline()
(line, reasonMessage) = parse_message(output_file, line)
reason = PositionedMessage(reasonPos, reasonMessage)
reasons.append(reason)
errors.append(make_error(severity, errorCode, position, message, reasons))
return errors
position_regex = r'^\s*File "(.*)", line (\d+), characters (\d+)-(\d+):(\[\d+\])?\n'
def parse_position(line: str) -> Position:
match = re.match(position_regex, line)
if match is None:
raise ParseException(f"Could not parse position line: {line}")
file = match.group(1)
lineNum = int(match.group(2))
startCol = int(match.group(3))
endCol = int(match.group(4))
return Position(file, lineNum, startCol, endCol)
def parse_message_and_code(file: IO[str], line: str) -> Tuple[str, ErrorCode]:
message_chunks = []
message_and_code_regex = r"^\s*(.*) \((.*)\[(\d+)\]\)\n"
match = re.match(message_and_code_regex, line)
while match is None:
match = re.match(r"^\s*(.*)\n", line)
if match is None:
raise ParseException(f"Could not parse message line: {line}")
message_line = match.group(1)
message_chunks.append(message_line)
line = file.readline()
match = re.match(message_and_code_regex, line)
assert match is not None, "should have raised exception above"
message_line = match.group(1)
type = match.group(2)
code = int(match.group(3))
message_chunks.append(message_line)
message = "".join(message_chunks)
return (message, ErrorCode(type, code))
def parse_message(file: IO[str], line: str) -> Tuple[str, str]:
message_chunks = []
message_regex = r"^\s*(.*)\n"
match = re.match(message_regex, line)
if match is None:
raise ParseException(f"Could not parse message line: {line}")
message_line = match.group(1)
message_chunks.append(message_line)
line = file.readline()
match = re.match(position_regex, line)
while match is None and not end_of_file(line):
match = re.match(message_regex, line)
if match is None:
raise ParseException(f"Could not parse message line: {line}")
message_line = match.group(1)
message_chunks.append(message_line)
line = file.readline()
match = re.match(position_regex, line)
message = "".join(message_chunks)
return (line, message)
def starts_with_space(s: str) -> bool:
return re.match(r"^\s", s) is not None
def sprint_error(error: Error) -> str:
file = error.message.position.fileName
line = error.message.position.line
startCol = error.message.position.startColumn
endCol = error.message.position.endColumn
message = error.message.message
type = error.code.type
code = error.code.code
out = [
f"\033[91m{file}:{line}:{startCol},{endCol}:\033[0m "
f"{message} ({type}[{code}])\n"
]
for reason in error.reason:
file = reason.position.fileName
line = reason.position.line
startCol = reason.position.startColumn
endCol = reason.position.endColumn
message = reason.message
out.append(f" \033[91m{file}:{line}:{startCol},{endCol}:\033[0m {message}\n")
return "".join(out)
def sprint_errors(errors: List[Error]) -> str:
if not errors:
return "No errors\n"
out = []
for error in errors:
out.append(sprint_error(error))
return "".join(out)