Skip to content

Commit

Permalink
fix CHECK-NEXT to consume as few matches as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLydike committed Jul 2, 2024
1 parent 1475b6a commit 7545e15
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ There are some features that are left out for now (e.g. [pseudo-numeric variable
- [ ] MLIR/xDSL integration tests
- **UX:**
- [ ] Good error messages: I have some error messages, but could be a lot better
- [ ] Error messages for malformed regexes
- **Infrastructure:**
- [X] Formatting: black
- [X] Pyright: Almost passes rn
Expand Down
2 changes: 0 additions & 2 deletions filecheck/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ def compile_uops(
expr: list[str] = []
if check.name == "NEXT":
expr.append(r"\n?[^\n]*")
elif check.name == "SAME":
expr.append("[^\n]*")
elif check.name == "EMPTY":
return CHECK_EMPTY_EXPR, dict()

Expand Down
17 changes: 12 additions & 5 deletions filecheck/finput.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,30 @@ def match(self, pattern: re.Pattern[str]) -> re.Match[str] | None:
"""
Match (exactly from the current position)
"""
print(f"matching on {pattern}")
print(f"matching on r{repr(pattern.pattern)}")
return pattern.match(self.content, pos=self.pos)

def find(self, pattern: re.Pattern[str]) -> re.Match[str] | None:
def find(
self, pattern: re.Pattern[str], this_line: bool = False
) -> re.Match[str] | None:
"""
Find the first occurance of a pattern, might be far away.
If this_line is given, match only until the next newline.
"""
print(f'searching for r"{pattern.pattern}"')
return pattern.search(self.content, pos=self.pos)
print(f"searching for r{repr(pattern.pattern)}")
endpos = self.content.find("\n", self.pos) if this_line else -1
if endpos == -1:
endpos = sys.maxsize
return pattern.search(self.content, pos=self.pos, endpos=endpos)

def find_between(
self, pattern: re.Pattern[str], start: int, end: int
) -> re.Match[str] | None:
"""
Find the first occurance of a pattern, might be far away.
"""
print(f"searching for {pattern} in input[{start}:{end}]")
print(f"searching for r{repr(pattern.pattern)} in input[{start}:{end}]")
return pattern.search(self.content, pos=start, endpos=end)

def print_line_with_current_pos(self, pos_override: int | None = None):
Expand Down
4 changes: 2 additions & 2 deletions filecheck/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def run(self) -> int:
"NOT": self.enqueue_not,
"EMPTY": self.check_empty,
"NEXT": self.match_immediately,
"SAME": self.match_immediately,
"SAME": self.match_eventually,
"LABEL": self.check_label,
"CHECK": self.match_eventually,
}
Expand Down Expand Up @@ -204,7 +204,7 @@ def match_eventually(self, op: CheckOp):
Uses file.find
"""
pattern, repl = compile_uops(op, self.ctx.live_variables, self.opts)
if match := self.file.find(pattern):
if match := self.file.find(pattern, op.name == "SAME"):
print(
f"matched: {op.check_line_repr()}, groups={match.groups()} mapping to {repl}"
)
Expand Down
6 changes: 6 additions & 0 deletions tests/filecheck/simple.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ SOME B C

// CHECK-NOT: 404
suffix

test A val1 val2 val1 val2 val1
// CHECK: test A
// CHECK-SAME: val2
// CHECK-SAME: val1
// CHECK-SAME: val2

0 comments on commit 7545e15

Please sign in to comment.