후입 선출
class CodeLinter(object):
def __init__(self, code):
self.__code = code
self.__opening_brace = ["(", "[", "{"]
self.__closing_brace = [")", "]", "}"]
self.__brace_set = {
"(": ")",
"[": "]",
"{": "}"
}
self.__stack = []
def __is_opening_brace(self, char):
return char in self.__opening_brace
def __is_closing_brace(self, char):
return char in self.__closing_brace
def __does_not_match(self, char, pop_from_stack):
return char != self.__brace_set[pop_from_stack]
def __lint(self):
for c in self.__code:
if self.__is_opening_brace(c):
self.__stack.append(c)
elif self.__is_closing_brace(c):
if not self.__stack:
return f"여는 괄호가 없습니다."
pop_from_stack = self.__stack.pop()
if self.__does_not_match(c, pop_from_stack):
return f"여는 괄호와 닫는 괄호가 맞지 않습니다."
if self.__stack:
return f"닫는 괄호가 부족합니다."
return f"문법 오류가 없습니다."
def lint(self):
return self.__lint()
js_code = "const a = y : [ 1, 2, 3 ]}"
code_linter = CodeLinter(js_code)
result = code_linter.lint()
print(result)