from typing import List


class Converter:
    template = """/*
 * Please do not edit this file, it is autogenerated.
 */
 
#pragma once 
#include "String.hpp"
#include "TokenType.hpp"


String stringify_token(TokenType token) {{
{conditions}
    return "TOKEN_NOT_REGISTERED";
}}
    """

    def __init__(self, file_name: str):
        self.file_name = file_name
        self.tokens = self.parse_tokens()
        self.output_data = self.gen_conditions()
        self.write_file()
        
    def parse_tokens(self) -> List[str]:
        conditions = []
        with open(f"{self.file_name}.hpp") as f:
            for line in f:
                value = line.strip()
                if any((
                    not value,
                    value.startswith("//"),
                    value.startswith("#pragma"),
                    "enum class" in value,
                    "}" in value,
                    "String stringify_token(TokenType token);" in value,
                )):
                    continue
                if "," in value:
                    conditions.append(value[:value.find(",")])
                else:
                    conditions.append(value)
        return conditions

    def gen_conditions(self):
        output = ""
        for token in self.tokens:
            output += f"    if (token == TokenType::{token}) return \"{token}\";\n"

        return self.template.format(conditions=output)

    def write_file(self):
        with open(f"{self.file_name}.cpp", "w") as f:
            f.write(self.output_data)


if __name__ == '__main__':
    Converter("src/TokenType")
