Last active
October 31, 2018 00:43
-
-
Save aczzdx/5b0aecd6c00ff673cacbf5de14a285c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" javalinelimit.py | |
This file is an extremly simple method-line-limit checker for java codes for USC CS 455. | |
Assumptions: we assmue that the codes has followed all the other code-style | |
requirements. | |
""" | |
import re | |
import sys | |
def debug(*args, **kargs): | |
# print(*args, **kargs) | |
pass | |
class JavaMethodLineLimitChecker(): | |
class_pattern = re.compile(r"\s*(public|private)\s+class\s+([^{}]+)\s*({)") | |
init_pattern = re.compile(r"\s*(public|private)\s+([^{=\s]+)\s*({)") | |
method_pattern = re.compile(r"\s*(public|private)\s+([^{=\s]+)\s+([^{=]+)\s*({)") | |
def __init__(self, linelimit): | |
self.lcnt = 0 | |
self.scopecnt = 0 | |
self.curclass = None | |
self.curmethod = None | |
self.linelimit = linelimit | |
def print_result(self): | |
assert self.curclass is not None | |
assert self.curmethod is not None | |
print("In class", self.curclass, ".", self.curmethod, self.lcnt, "lines...", "PASSED" if self.lcnt <= self.linelimit else "TOO LONG") | |
def parseline(self, i, l): | |
p = 0 | |
if self.curmethod is None: | |
m = self.class_pattern.match(l) | |
if m is not None: | |
debug("Line", i, "is a class-declartion.") | |
self.curclass = m.group(2) | |
elif self.init_pattern.match(l) is not None: | |
m = self.init_pattern.match(l) | |
debug("Line", i, "is an intializer-declartion.") | |
self.curmethod = m.group(2) | |
self.lcnt += 1 | |
elif self.method_pattern.match(l) is not None: | |
m = self.method_pattern.match(l) | |
debug("Line", i, "is a method-declartion.") | |
self.curmethod = m.group(2) + " " + m.group(3) | |
self.lcnt += 1 | |
if m is not None: | |
p = m.end() | |
self.scopecnt += 1 | |
else: | |
self.lcnt += 1 | |
for c in l[p:]: | |
if c == '{': | |
self.scopecnt += 1 | |
elif c == '}': | |
self.scopecnt -= 1 | |
if (self.curmethod is not None and self.curclass is not None and self.scopecnt == 1): | |
self.print_result() | |
self.curmethod = None | |
self.lcnt = 0 | |
elif (self.scopecnt == 0): | |
self.curclass = None | |
if __name__ == "__main__": | |
with open(sys.argv[1], "r") as f: | |
test_string = f.read() | |
lines = test_string.split("\n") | |
checker = JavaMethodLineLimitChecker(30) | |
for i, l in enumerate(lines): | |
checker.parseline(i, l) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment