def check_program_validity(self): self.current_grammar = program_grammar self.set_number_of_priorities_level() try: self.iterate_over_statement() except custom_exception.ParsingException: raise custom_exception.ParsingException(err_const.PROGRAM_PARSING_FAILED)
def token_is_opening_stmt(self): if self.current_tokens[self.token_index] in OPENING_STMT: try: if self.current_tokens[self.token_index + 1] not in CLOSING_STMT: self.opening_stmt_indexes.append(self.token_index + 1) else: self.token_index += 1 except IndexError: raise custom_exception.ParsingException(err_const.STMT_PARSING_FAILED) elif self.current_tokens[self.token_index] in CLOSING_STMT: self.closing_stmt_index = self.token_index self.iterate_over_inner_statement()
def token_is_opening_stmt(self): opening_stmt = STATEMENT_PAIRS.keys() if self.current_tokens[self.token_index] in opening_stmt: current_token = self.current_tokens[self.token_index] try: self.stmt_longer_than_two_tokens(current_token) except IndexError: raise custom_exception.ParsingException( err_const.STMT_PARSING_FAILED) elif self.current_tokens[self.token_index] in STATEMENT_PAIRS.values(): self.closing_stmt_index = self.token_index self.iterate_over_inner_statement()
def check_program_validity(self): if len(self.current_statement ) == 1 and self.current_statement[0] is const.BLOCK_STMT: return self.current_grammar = program_grammar self.set_number_of_priorities_level() self.current_priority_level = 0 try: self.reduce_program() except custom_exception.ParsingException: raise custom_exception.ParsingException( err_const.PROGRAM_PARSING_FAILED)
def handle_failed_stmt_reduction(self): if self.statement_already_recalled: raise custom_exception.ParsingException(err_const.STMT_PARSING_FAILED) self.current_grammar = program_grammar self.set_number_of_priorities_level() try: self.iterate_over_statement() except custom_exception.ParsingException: self.statement_already_recalled = True self.current_grammar = statement_grammar self.set_number_of_priorities_level() self.current_tokens[self.current_index:self.closing_stmt_index] = self.current_statement self.token_index -= len(self.current_tokens[self.current_index + 1:self.closing_stmt_index])
def reduce_and_handle_error(self): is_valid_statement = False index = 0 while index < len(self.current_statement): possible_production_rules = self._get_possible_production_rules( self.current_statement[-(index + 1)]) matching_production = self._find_matching_production(index, possible_production_rules) if matching_production is not None: self._replace_current_tokens(index, matching_production) self.current_priority_level = 0 if len(self.current_statement) > MAX_FINAL_STATEMENT_LENGTH: self.reduce_and_handle_error() is_valid_statement = True index += 1 if not is_valid_statement and self.current_priority_level == (self.number_of_priorities - 1): raise custom_exception.ParsingException(err_const.STMT_PARSING_FAILED)
def iterate_over_stmt(self): self.program_grammar_applied = False self.stmt_grammar_applied = False self.is_valid_stmt = False try: while self.is_valid_stmt is False: self.current_priority_level = 0 while self.current_priority_level < self.number_of_priorities: self.reduce_and_handle_error() self.current_priority_level += 1 if len(self.current_statement ) == MAX_FINAL_STATEMENT_LENGTH: break if self.no_math_in_last_two_reductions(): self.switch_grammar() elif not self.is_valid_stmt: raise custom_exception.ParsingException( err_const.STMT_PARSING_FAILED) except custom_exception.ParsingException: raise