From 46ced3f20df5e8b778b59bcdce029c6d2a99b158 Mon Sep 17 00:00:00 2001 From: Xun Yang <x299yang@uwaterloo.ca> Date: Mon, 10 Feb 2020 17:13:20 -0500 Subject: [PATCH] filename check --- Test.py | 41 ++++++++++++++++++++++++++++------------- Weeding.py | 18 +++++++++++++++++- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/Test.py b/Test.py index 72d5abe..e92c827 100644 --- a/Test.py +++ b/Test.py @@ -4,6 +4,7 @@ from os.path import isfile, join from Scanning import scan from Parsing import parse +import Weeding def allFiles(testDir): @@ -19,28 +20,42 @@ def main(): for f in testFiles: # print(f) - # Scanning + # Scanning content = open(f, "r").read() (tokens, errorString) = scan(content) - # Error in Scanning + # Error in Scanning if tokens is None: # print("ERROR in Scanning: " + errorString) print("**********************************************************") continue - # s = "All Tokens: " - # for token in tokens: - # if (token.name and token.lex): - # s += '(' + token.name + ',' + token.lex + '), ' - # print(s) - + s = "All Tokens: " + for token in tokens: + if (token.name and token.lex): + s += '(' + token.name + ',' + token.lex + '), ' + print(s) + + # No weeds if everything is good (weeds = None) + weeds = Weeding.fileNameCheck(tokens, f) + if weeds: + print(weeds) + continue + + + # Parsing # print("Parsing starts") - try: - (steps, errorString) = parse(tokens) - except: - print("Exception in Parsing") + # try: + # (steps, errorString) = parse(tokens) + # except: + # print("Exception in Parsing") + # + # # Error in Parsing + # if steps is None: + # print("ERROR in Parsing: ", errorString) + # print("**********************************************************") + # continue # Error in Parsing if steps is None: @@ -54,4 +69,4 @@ def main(): print("**********************************************************") -main() \ No newline at end of file +main() diff --git a/Weeding.py b/Weeding.py index b918cac..93abb8d 100644 --- a/Weeding.py +++ b/Weeding.py @@ -1,6 +1,22 @@ import string +import os ##################### Weeding ########################################## +def fileNameCheck(tokens, f): + fileName = os.path.basename(f).split('.java')[0] + + check = False + 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 + if t.name == 'INTERFACE' or t.name == 'CLASS': + check = True + return None + + # node: Node[] def oneConstructor(node, insideClass): @@ -14,7 +30,7 @@ def oneConstructor(node, insideClass): elif insideClass: if node.name == 'constructorDcl': return True - + # tree: Node[] def weed(tree): -- GitLab