import sys
from os import listdir
from os.path import isfile, join

from Scanning import scan
from Parsing import parse


def allFiles(testDir):
     return [testDir + f for f in listdir(testDir) if isfile(join(testDir, f)) and f.startswith('J1')]

def main():

    # All files in the test directory
    testDirectory = "./Tests/"
    testFiles = allFiles(testDirectory)
    print("**********************************************************")

    for f in testFiles:
        print(f)

        # Scanning 
        content = open(f, "r").read()
        (tokens, errorString) = scan(content)
        # 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)
    
        # Parsing
        print("Parsing starts")

        try:
            (steps, errorString) = parse(tokens)
        except:
            print("Exception in Parsing")

        # Error in Parsing
        if steps is None:
            print("ERROR in Parsing: ", errorString)
            print("**********************************************************")
            continue
        
        print("All Steps:")
        print(steps)

        print("**********************************************************")

main()