Skip to content

Qualify all instances with Local or Global

Tej Chajed requested to merge tchajed/iris-coq:qualify-instances into master

Added Local within a section and Global outside. This was done automatically with the following Python script, which tracks whether a section is open. The script handles nested sections but makes no attempt to fully handle Coq sentences or comments, so multiple section commands on one line or section commands in comments will confuse it. I also manually fixed some Instances in example code in documentation.

EDIT: See stdpp!207 (merged) for a newer version of this script.

#!/usr/bin/env python3

# Add Local or Global to any unqualified Instance declarations. Tracks whether
# a section is open and adds Global outside any section and Local inside one.

import sys
import re

SECTION_RE = re.compile("""\s*Section\s+(?P<name>\w*).*\..*""")
END_RE = re.compile("""\s*End\s+(?P<name>\w*).*\..*""")
INSTANCE_RE = re.compile("""(?P<indent>\s*)(?P<instance>Instance.*)""")

def qualify_lines(lines):
    """Qualify instances in text given as a list of lines."""
    # tracks the list of sections open
    section_stack = []
    out = []
    for line in lines:
        m = SECTION_RE.match(line)
        if m:
            name ="name")
        m = END_RE.match(line)
        if m:
            name ="name")
            # close a section if it's open (name may not match because End is
            # for a module instead)
            if len(section_stack) > 0 and section_stack[-1] == name:
        m = INSTANCE_RE.match(line)
        if m:
            indent ="indent")
            modifier = "Local" if section_stack else "Global"
            instance ="instance")
            line = f"{indent}{modifier} {instance}\n"
    if section_stack:
        print(f"oops {section_stack} remaining", file=sys.stderr)
    return out

for fname in sys.argv[1:]:
    with open(fname) as f:
        lines = f.readlines()
    with open(fname, "w") as f:
        lines = qualify_lines(lines)
Edited by Ralf Jung

Merge request reports