From 45be2f18740de4c5340d29ad561dba28bd7ebfcb Mon Sep 17 00:00:00 2001 From: Xun Yang <x299yang@uwaterloo.ca> Date: Mon, 10 Feb 2020 17:41:36 -0500 Subject: [PATCH] implement/extend --- Weeding.py | 29 ++++++++++++++++++++++++----- joosc.py | 12 +++++++++--- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Weeding.py b/Weeding.py index 93abb8d..0b86e7a 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 2fe4e6f..a6909e0 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() -- GitLab