Skip to content

Commit

Permalink
Stricter prefix matching (fixes #28) (#29)
Browse files Browse the repository at this point in the history
This PR adds the requirement that the `CHECK` be preceded by either a
new-line, or a character that is not `[A-Za-z]`. This fixes #28

It also adds diagnostic printing when checking fails inside a DAG region
by printing all currently considered input and greying out regions that
are already consumed.
  • Loading branch information
AntonLydike committed Jul 30, 2024
1 parent ba7820b commit ee8526c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
9 changes: 9 additions & 0 deletions filecheck/finput.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dataclasses import dataclass, field
from typing import Iterable

from filecheck.colors import FMT
from filecheck.options import Options

ANY_NEWLINES = re.compile(r"\n*")
Expand Down Expand Up @@ -297,6 +298,14 @@ def match_and_add_hole(self, pattern: re.Pattern[str]) -> re.Match[str] | None:
self.range.add_hole(InputRange(match.start(0), match.end(0)))
return match

def print_current_range(self):
end = self.range.start
for a, b in self.range.ranges():
yield f"{FMT.GRAY}{self.content[end:a]}{FMT.RESET}"
yield f"{self.content[a:b]}"
end = b
yield f"{FMT.GRAY}{self.content[end:self.range.end]}{FMT.RESET}"

def advance_to_last_hole(self):
"""
Advance to end of the last hole in the discontigous region
Expand Down
13 changes: 13 additions & 0 deletions filecheck/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ def run(self) -> int:
)
print("Current position at " + self.file.print_line(), file=sys.stderr)

if self.file.is_discontigous():
print(
"\nCurrently matching in range (grey is already matched):",
file=sys.stderr,
)
print("".join(self.file.print_current_range()), file=sys.stderr)

# try to look for a shorter match, and print that if possible
prefix_match = self.find_prefix_match_for(ex.op)
if prefix_match is not None:
Expand All @@ -143,6 +150,12 @@ def run(self) -> int:
self.file.print_line(ex.match.start(0), ex.match.end(0)),
file=sys.stderr,
)
if self.file.is_discontigous():
print(
"\nCurrently matching in range (grey is already matched):",
file=sys.stderr,
)
print("".join(self.file.print_current_range()), file=sys.stderr)
return 1

return 0
Expand Down
11 changes: 6 additions & 5 deletions filecheck/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
def pattern_for_opts(opts: Options) -> tuple[re.Pattern[str], re.Pattern[str]]:
prefixes = f"({'|'.join(map(re.escape, opts.check_prefixes))})"
return re.compile(
prefixes
r"(^|[^a-zA-Z])"
+ prefixes
+ r"(-(DAG|COUNT-\d+|NOT|EMPTY|NEXT|SAME|LABEL))?(\{LITERAL})?:\s?([^\n]*)\n?"
), re.compile(f"({'|'.join(map(re.escape, opts.comment_prefixes))}).*{prefixes}")

Expand Down Expand Up @@ -86,10 +87,10 @@ def __next__(self) -> CheckOp:
# no check line = skip
if match is None:
continue
prefix = match.group(1)
kind = match.group(3)
literal = match.group(4)
arg = match.group(5)
prefix = match.group(2)
kind = match.group(4)
literal = match.group(5)
arg = match.group(6)
if kind is None:
kind = "CHECK"
if arg is None:
Expand Down
1 change: 1 addition & 0 deletions tests/filecheck/flags/check-prefix.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

hallo welt
// TEST: hallo welt
// NOTEST: will not be matched

0 comments on commit ee8526c

Please sign in to comment.