import sys

def check(path):
    s = open(path, encoding="utf-8").read()
    i, n = 0, len(s)
    depth = 0
    stack = []
    parens = brackets = braces = 0
    problems = []
    line = 1
    while i < n:
        c = s[i]
        if c == "\n":
            line += 1
            i += 1
            continue
        # comments
        if c == "-" and i + 1 < n and s[i+1] == "-":
            j = i + 2
            # long comment?
            if j < n and s[j] == "[":
                k = j + 1
                eq = 0
                while k < n and s[k] == "=":
                    eq += 1; k += 1
                if k < n and s[k] == "[":
                    close = "]" + "="*eq + "]"
                    end = s.find(close, k+1)
                    if end == -1:
                        problems.append("unterminated long comment at line %d" % line)
                        break
                    line += s.count("\n", i, end)
                    i = end + len(close)
                    continue
            end = s.find("\n", j)
            if end == -1: end = n
            i = end
            continue
        if c in "'\"":
            q = c
            i += 1
            while i < n:
                if s[i] == "\\":
                    i += 2
                    continue
                if s[i] == q:
                    i += 1
                    break
                if s[i] == "\n":
                    line += 1
                i += 1
            continue
        if c == "[":
            k = i + 1
            eq = 0
            while k < n and s[k] == "=":
                eq += 1; k += 1
            if k < n and s[k] == "[":
                close = "]" + "="*eq + "]"
                end = s.find(close, k+1)
                if end == -1:
                    problems.append("unterminated long string at line %d" % line)
                    break
                line += s.count("\n", i, end)
                i = end + len(close)
                continue
            brackets += 1
            i += 1
            continue
        if c == "(":
            parens += 1
        elif c == ")":
            parens -= 1
        elif c == "{":
            braces += 1
        elif c == "}":
            braces -= 1
        elif c == "]":
            brackets -= 1
        elif c.isalpha() or c == "_":
            j = i
            while j < n and (s[j].isalnum() or s[j] == "_"):
                j += 1
            w = s[i:j]
            if w == "function":
                depth += 1; stack.append(("function", line))
            elif w == "if":
                depth += 1; stack.append(("if", line))
            elif w == "do":
                depth += 1; stack.append(("do", line))
            elif w == "repeat":
                depth += 1; stack.append(("repeat", line))
            elif w == "end":
                depth -= 1
                if not stack:
                    problems.append("extra end at line %d" % line)
                else:
                    kind, ln = stack.pop()
                    if kind == "repeat":
                        problems.append("end closing repeat from line %d at line %d" % (ln, line))
            elif w == "until":
                depth -= 1
                if not stack:
                    problems.append("extra until at line %d" % line)
                else:
                    kind, ln = stack.pop()
                    if kind != "repeat":
                        problems.append("until closing %s from line %d at line %d" % (kind, ln, line))
            i = j
            continue
        i += 1
    print(path)
    print("  depth        :", depth)
    print("  unclosed     :", stack[-5:])
    print("  parens       :", parens, " braces:", braces, " brackets:", brackets)
    print("  problems     :", problems[:10], "count", len(problems))
    return depth == 0 and parens == 0 and braces == 0 and brackets == 0 and not problems

ok = True
for p in sys.argv[1:]:
    ok = check(p) and ok
sys.exit(0 if ok else 1)
