Skip to content

Commit

Permalink
Merge pull request #27 from plasma-umass/fix-25
Browse files Browse the repository at this point in the history
Fix #25
  • Loading branch information
emeryberger authored Sep 26, 2023
2 parents 8e3a469 + fdbbad0 commit 869a489
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions src/cwhy/cwhy.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import collections
import os
import re
import subprocess
import sys
import textwrap
import os
import subprocess
import collections
from typing import Dict, List, Tuple

import openai
Expand Down Expand Up @@ -195,39 +195,39 @@ def evaluate_text_prompt(args, prompt, wrap=True, **kwargs):
# Define error patterns with associated information. The numbers
# correspond to the groups matching file name and line number.
error_patterns = [
("C#", re.compile(
r"([a-zA-Z0-9./][^:\r\n]+)\((\d+),(\d+)\): error ([A-Za-z0-9]+): (.*)"
), 1, 2),
("C/C++/Rust", re.compile(
r"([a-zA-Z0-9./][^:->]+):([0-9]+):([0-9]+)"
), 1, 2),
("Visual Studio C/C++", re.compile(
r"([a-zA-Z]?:?[\\\/a-zA-Z0-9._-]+)\(([0-9]+)\)"
), 1, 2),
(
"C#",
re.compile(
r"([a-zA-Z0-9./][^:\r\n]+)\((\d+),(\d+)\): error ([A-Za-z0-9]+): (.*)"
),
1,
2,
),
("C/C++/Rust", re.compile(r"([a-zA-Z0-9./][^:->]+):([0-9]+):([0-9]+)"), 1, 2),
(
"Visual Studio C/C++",
re.compile(r"([a-zA-Z]?:?[\\\/a-zA-Z0-9._-]+)\(([0-9]+)\)"),
1,
2,
),
# Note: LaTeX must precede Java
("LaTeX", re.compile(
r"(.*\.tex):(\d+): error: (.*)"
), 1, 2),
("Java", re.compile(
r"([a-zA-Z0-9./][^:->]+):([0-9]+):"
), 1, 2),
("Python", re.compile(
r'\s*File "(.*?)", line (\d+), in ([^\<].*)'
), 1, 2),
("Go", re.compile(
r"([a-zA-Z0-9./][^:\r\n]+):([0-9]+):([0-9]+): (.*): (.*)"
), 1, 2),
("TypeScript", re.compile(
r"([a-zA-Z0-9./][^:\r\n]+)\((\d+),(\d+)\): error ([A-Za-z0-9]+): (.*)"
), 1, 2),
("Ruby", re.compile(
r'"(.*\.rb)", line (\d+)(?:, in `.*\')?: (.*)'
), 1, 2),
("PHP", re.compile(
r"PHP (?:Parse|Fatal) error: (.*) in (.*) on line (\d+)"
), 2, 3),
("LaTeX", re.compile(r"(.*\.tex):(\d+): error: (.*)"), 1, 2),
("Java", re.compile(r"([a-zA-Z0-9./][^:->]+):([0-9]+): error:"), 1, 2),
("Python", re.compile(r'\s*File "(.*?)", line (\d+), in ([^\<].*)'), 1, 2),
("Go", re.compile(r"([a-zA-Z0-9./][^:\r\n]+):([0-9]+):([0-9]+): (.*): (.*)"), 1, 2),
(
"TypeScript",
re.compile(
r"([a-zA-Z0-9./][^:\r\n]+)\((\d+),(\d+)\): error ([A-Za-z0-9]+): (.*)"
),
1,
2,
),
("Ruby", re.compile(r'"(.*\.rb)", line (\d+)(?:, in `.*\')?: (.*)'), 1, 2),
("PHP", re.compile(r"PHP (?:Parse|Fatal) error: (.*) in (.*) on line (\d+)"), 2, 3),
]


class explain_context:
def __init__(self, args, diagnostic):
self.args = args
Expand All @@ -238,7 +238,7 @@ def __init__(self, args, diagnostic):
self.code_locations = collections.defaultdict(dict)

# Go through the diagnostic and build up a list of code locations.
for (linenum, line) in enumerate(self.diagnostic_lines):
for linenum, line in enumerate(self.diagnostic_lines):
file_name = None
line_number = None
for lang, pattern, file_group, line_group in error_patterns:
Expand All @@ -252,13 +252,16 @@ def __init__(self, args, diagnostic):

if not file_name:
continue

try:
(abridged_code, line_start) = read_lines(
file_name, line_number - 7, line_number + 3
)
except FileNotFoundError:
print(f"Cwhy warning: file not found: {file_name.lstrip()}")
print(
f"Cwhy warning: file not found: {file_name.lstrip()}",
file=sys.stderr,
)
continue

for i, line_content in enumerate(abridged_code):
Expand Down

0 comments on commit 869a489

Please sign in to comment.