Skip to content

Commit

Permalink
fix comments and invalid capture group handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLydike committed Jul 2, 2024
1 parent 7545e15 commit 46b8dee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
6 changes: 3 additions & 3 deletions filecheck/finput.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def match(self, pattern: re.Pattern[str]) -> re.Match[str] | None:
"""
Match (exactly from the current position)
"""
print(f"matching on r{repr(pattern.pattern)}")
print(f"matching on r'{pattern.pattern}'")
return pattern.match(self.content, pos=self.pos)

def find(
Expand All @@ -72,7 +72,7 @@ def find(
If this_line is given, match only until the next newline.
"""
print(f"searching for r{repr(pattern.pattern)}")
print(f"searching for r'{pattern.pattern}'")
endpos = self.content.find("\n", self.pos) if this_line else -1
if endpos == -1:
endpos = sys.maxsize
Expand All @@ -84,7 +84,7 @@ def find_between(
"""
Find the first occurance of a pattern, might be far away.
"""
print(f"searching for r{repr(pattern.pattern)} in input[{start}:{end}]")
print(f"searching for r'{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
33 changes: 20 additions & 13 deletions filecheck/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
from filecheck.regex import posix_to_python_regex, pattern_from_num_subst_spec


def pattern_for_opts(opts: Options) -> re.Pattern[str]:
def pattern_for_opts(opts: Options) -> tuple[re.Pattern[str], re.Pattern[str]]:
return re.compile(
"(("
re.escape(opts.check_prefix)
+ r"(-(DAG|COUNT|NOT|EMPTY|NEXT|SAME|LABEL))?:\s?([^\n]*)\n?"
), re.compile(
"("
+ "|".join(map(re.escape, opts.comment_prefixes))
+ r"):)?[^\n]*"
+ ").*"
+ re.escape(opts.check_prefix)
+ r"(-(DAG|COUNT|NOT|EMPTY|NEXT|SAME|LABEL))?:\s?([^\n]*)\n?"
)


Expand Down Expand Up @@ -54,12 +56,13 @@ class Parser(Iterator[CheckOp]):
input: TextIO

check_line_regexp: re.Pattern[str]
comment_line_regexp: re.Pattern[str]

_line: int = field(default=0)

@classmethod
def from_opts(cls, opts: Options):
return Parser(opts, open(opts.match_filename), pattern_for_opts(opts))
return Parser(opts, open(opts.match_filename), *pattern_for_opts(opts))

def __next__(self):
kind, arg, line = self.next_matching_line()
Expand All @@ -79,16 +82,16 @@ def next_matching_line(self) -> tuple[str, str, int]:
line = self.input.readline()
if line == "":
raise StopIteration()
# skip any lines containing comment markers before checks
if self.comment_line_regexp.match(line):
continue

match = self.check_line_regexp.search(line)
# no check line = skip
if match is None:
continue
# skip lines containing comment markers, even through they also contain check lines
if match.group(1) is not None:
continue
kind = match.group(4)
arg = match.group(5)
kind = match.group(2)
arg = match.group(3)
if kind is None:
kind = "CHECK"
if arg is None:
Expand All @@ -110,7 +113,7 @@ def parse_args(self, arg: str) -> list[UOp]:
if part == "[[":
# grab parts greedily until we hit a ]]
while not part.endswith("]]"):
assert len(parts) > 0, "Malformed substitution pattern"
assert len(parts) > 0, "Invalid substitution block, no ]]"
part += parts.pop(0)
# check if we are a simple capture pattern [[<name>:<regex>]]
if match := VAR_CAPTURE_PATTERN.fullmatch(part):
Expand All @@ -136,11 +139,15 @@ def parse_args(self, arg: str) -> list[UOp]:
match.group(2), match.group(3)
)
uops.append(Capture(match.group(5), pattern, mapper))
else:
raise RuntimeError(
f"Invalid substitution block, unknown format: {part}"
)
elif part == "{{":
assert len(parts) > 0, "Malformed regex pattern"
assert len(parts) > 0, "Invalid regex block, no }}"
next_part = parts.pop(0)
while not next_part.endswith("}}"):
assert len(parts) > 0, "Malformed regex pattern"
assert len(parts) > 0, "Malformed regex pattern, no }}"
next_part += parts.pop(0)

pattern = next_part[:-2]
Expand Down

0 comments on commit 46b8dee

Please sign in to comment.