Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stricter prefix matching (fixes #28) #29

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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


Expand Down Expand Up @@ -289,6 +290,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
Loading