Skip to content

Commit 418118f

Browse files
committed
Codegen: use more robust regex to patch qldocs
1 parent 30161b0 commit 418118f

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

misc/codegen/generators/qlgen.py

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -316,39 +316,27 @@ def _get_stub(cls: schema.Class, base_import: str, generated_import_prefix: str)
316316
ql_internal="ql_internal" in cls.pragmas)
317317

318318

319-
def _patch_class_qldocs(cls: str, qldoc: str, stub_file: pathlib.Path):
319+
_stub_qldoc_header = "// the following QLdoc is generated: if you need to edit it, do it in the schema file\n"
320+
321+
_class_qldoc_re = re.compile(rf"(?P<qldoc>(?:{_stub_qldoc_header})?/\*\*.*?\*/\s*|^\s*)class\s+(?P<class>\w+)",
322+
re.MULTILINE | re.DOTALL)
323+
324+
325+
def _patch_class_qldoc(cls: str, qldoc: str, stub_file: pathlib.Path):
320326
if not qldoc or not stub_file.exists():
321327
return
322328
qldoc = "\n".join(l.rstrip() for l in qldoc.splitlines())
323-
tmp = stub_file.with_suffix(f'{stub_file.suffix}.bkp')
324-
header = "// the following QLdoc is generated: if you need to edit it, do it in the schema file\n"
325329
with open(stub_file) as input:
326-
qldoc_start = None
327-
qldoc_end = None
328-
class_start = None
329-
for lineno, line in enumerate(input, 1):
330-
if line == header:
331-
qldoc_start = lineno
332-
if line.startswith("/**") and lineno - 1 != qldoc_start:
333-
qldoc_start = lineno
334-
if line.endswith(" */\n"):
335-
qldoc_end = lineno + 1
336-
elif line.startswith(f"class {cls}"):
337-
class_start = lineno
338-
break
339-
assert class_start, stub_file
340-
assert bool(qldoc_start) == bool(qldoc_end), stub_file
341-
if not qldoc_start or qldoc_end != class_start:
342-
qldoc_start = class_start
343-
input.seek(0)
344-
with open(tmp, 'w') as output:
345-
for lineno, line in enumerate(input, 1):
346-
if lineno == qldoc_start:
347-
print(header, end='', file=output)
348-
print(qldoc, file=output)
349-
if lineno < qldoc_start or lineno >= class_start:
350-
print(line, end='', file=output)
351-
tmp.rename(stub_file)
330+
contents = input.read()
331+
for match in _class_qldoc_re.finditer(contents):
332+
if match["class"] == cls:
333+
qldoc_start, qldoc_end = match.span("qldoc")
334+
contents = f"{contents[:qldoc_start]}{_stub_qldoc_header}{qldoc}\n{contents[qldoc_end:]}"
335+
tmp = stub_file.with_suffix(f"{stub_file.suffix}.bkp")
336+
with open(tmp, "w") as out:
337+
out.write(contents)
338+
tmp.rename(stub_file)
339+
return
352340

353341

354342
def generate(opts, renderer):
@@ -404,7 +392,7 @@ def generate(opts, renderer):
404392
renderer.render(stub, stub_file)
405393
else:
406394
qldoc = renderer.render_str(stub, template='ql_stub_class_qldoc')
407-
_patch_class_qldocs(c.name, qldoc, stub_file)
395+
_patch_class_qldoc(c.name, qldoc, stub_file)
408396

409397
# for example path/to/elements -> path/to/elements.qll
410398
renderer.render(ql.ImportList([i for name, i in imports.items() if not classes[name].ql_internal]),

0 commit comments

Comments
 (0)