Cannot figure out how to filter out tab whitespace

I’m stuck on Stage #ER2.

I’ve tried using the \t symbol to filter tabs and I’ve also manually tried to filter out tab by checking if there are four consecutive spaces but I always get the Unexpected character error. I’ve tried copying the same lines of code from people in example code but it still wont work.

Here are my logs:

remote: [tester::#ER2] [test-1] Running test case: 1
remote: [tester::#ER2] [test-1] Writing contents to ./test.lox:
remote: [tester::#ER2] [test-1] [test.lox] <|SPACE|>
remote: [tester::#ER2] [test-1] $ ./your_program.sh tokenize test.lox
remote: [your_program] |SPACE|
remote: [your_program] EOF  null
remote: [tester::#ER2] [test-1] ✓ 1 line(s) match on stdout
remote: [tester::#ER2] [test-1] ✓ Received exit code 0.
remote: [tester::#ER2] [test-2] Running test case: 2
remote: [tester::#ER2] [test-2] Writing contents to ./test.lox:
remote: [tester::#ER2] [test-2] [test.lox]  <|TAB|>
remote: [tester::#ER2] [test-2] [test.lox] <|SPACE|>
remote: [tester::#ER2] [test-2] $ ./your_program.sh tokenize test.lox
remote: [your_program] |SPACE|
remote: [your_program] |TAB|
remote: [your_program] [line 1] Error: Unexpected character:
remote: [your_program]
remote: [your_program] |SPACE|
remote: [your_program] EOF  null
remote: [tester::#ER2] [test-2] expected exit code 0, got 65

And here’s a snippet of my code:

import sys


def main():
    if len(sys.argv) < 3:
        print("Usage: ./your_program.sh tokenize <filename>", file=sys.stderr)
        exit(1)

    command = sys.argv[1]
    filename = sys.argv[2]

    if command != "tokenize":
        print(f"Unknown command: {command}", file=sys.stderr)
        exit(1)

    with open(filename) as file:
        file_contents = file.read()
    
    global errorCode
    global skip
    global comment
    errorCode = 0
    skip = 0
    comment = False
    

    for index, c in enumerate(file_contents):
        if skip > 0 or comment:
            skip -= 1
            if c == "n" and file_contents[index - 1] == "\\":
                comment = False
            continue

        if c == " ":
            print("|SPACE|", file=sys.stderr)
            continue
        elif c == "\t": # This part is not working
            print("|TAB|", file=sys.stderr)
            continue

        match c:
            case "(":
                print("LEFT_PAREN ( null")
            case ")":
                print("RIGHT_PAREN ) null")
            case "{":
                print("LEFT_BRACE { null")
            case "}":
                print("RIGHT_BRACE } null")
            case "*":
                print("STAR * null")
            case ",":
                print("COMMA , null")
            case ".":
                print("DOT . null")
            case "+":
                print("PLUS + null")
            case "-":
                print("MINUS - null")
            case ";":
                print("SEMICOLON ; null")
            case "=":
                print(checkDouble(index, file_contents, "="))
            case "!":
                print(checkDouble(index, file_contents, "!"))
            case "<":
                print(checkDouble(index, file_contents, "<"))
            case ">":
                print(checkDouble(index, file_contents, ">"))
            case "/":
                checkDouble(index, file_contents, "/", "/")
            case _:
                line = file_contents.count("\n", 0, index) + 1
                errorCode = 65
                print(f"[line {line}] Error: Unexpected character: {c}", file=sys.stderr)

    print("EOF  null")
    exit(errorCode)


def checkDouble(index, file_contents, symbol, symbol2="="):
        global skip
        global comment
        if index == len(file_contents) - 1 or file_contents[index + 1] != symbol2:
            match symbol:
                case "=":
                    return "EQUAL = null"
                case "!":
                    return "BANG ! null"
                case ">":
                    return "GREATER > null"
                case "<":
                    return "LESS < null"
                case "/":
                    print("SLASH / null")
        else:
            skip += 1
            match symbol:
                case "=":
                    return "EQUAL_EQUAL == null"
                case "!":
                    return "BANG_EQUAL != null"
                case ">":
                    return "GREATER_EQUAL >= null"
                case "<":
                    return "LESS_EQUAL <= null"
                case "/":
                    comment = True
                    pass
        

if __name__ == "__main__":
    main()

I figured out that the issue wasn’t the tab, it was an invisible \n after it so I just needed to add a scenario to handle \n. Why doesn’t the description say that we needed to handle \n?

We’ll update instructions to mention \n! Will keep this open until that’s done

1 Like

Thanks @EthanWeygang for highlighting the issue! We’ve updated the instructions via this PR.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.