diff --git a/Weeding.py b/Weeding.py index 93abb8de6df3eb5617a14fc678b2fe91ba30cbfc..0b86e7ab35c4aa65fa4403a4433a17042b4fc363 100644 --- a/Weeding.py +++ b/Weeding.py @@ -1,7 +1,13 @@ import string import os -##################### Weeding ########################################## +##################### Weeding after scanning ########################################## +def weedTokens(tokens, file): + result = fileNameCheck(tokens, file) + result += extendCheck(tokens) + return result + + def fileNameCheck(tokens, f): fileName = os.path.basename(f).split('.java')[0] @@ -9,13 +15,26 @@ def fileNameCheck(tokens, f): for t in tokens: if check: if (t.lex != fileName): - print(t.lex, fileName) - return ("ERROR: Class or Interface name should be the same as file name.") - return None + return ("ERROR: Class or Interface name should be the same as file name. " + t.lex + " " + fileName + "\n") + return "" if t.name == 'INTERFACE' or t.name == 'CLASS': check = True - return None + return "" + +def extendCheck(tokens): + check = False + for t in tokens: + if check: + if (not t.lex.startswith('java.' )): + return ("ERROR: Can not extend/implement class type " + t.lex + ". \n") + return "" + if t.name == 'EXTENDS' or t.name == 'IMPLEMENTS': + check = True + return "" + + +######################## Weeding after parsing ################################ # node: Node[] diff --git a/joosc.py b/joosc.py index 2fe4e6ff6c78818740183b80e992af3e5480ae0d..a6909e0f6e840b972a1458e59e8807c799ea988c 100644 --- a/joosc.py +++ b/joosc.py @@ -5,22 +5,28 @@ from os.path import isfile, join from Scanning import scan from Parsing import parse +from Weeding import weedTokens def main(): inputfile = sys.argv[1] content = open(inputfile, "r").read() - + # Scanning try: (tokens, errorString) = scan(content) except: return 42 - + # Error in Scanning if tokens is None: return 42 + # Weed the tokens + weeds = weedTokens(tokens, inputfile) + if weeds != "": + return 42 + # Parsing try: (steps, error) = parse(tokens) @@ -34,4 +40,4 @@ def main(): print("success in scanning and parsing") return 0 -main() \ No newline at end of file +main()