def __init__(self, file_name, input_str, position, message_factory): from parglare.parser import pos_to_line_col self.file_name = file_name self.position = position self.line, self.column = pos_to_line_col(input_str, position) super(ParseError, self).__init__( message_factory(file_name, input_str, position))
def check_name(context, name): """ Used in actions to check for reserved names usage. """ if name in RESERVED_SYMBOL_NAMES: from parglare.parser import pos_to_line_col raise GrammarError('Rule name "{}" is reserved at {}.'.format( name, pos_to_line_col(context.input_str, context.start_position)))
def _debug_context(self, position, layout_content=None, lookahead_tokens=None, expected_symbols=None): input_str = self.input_str h_print("Position:", pos_to_line_col(input_str, position)) h_print("Context:", _(position_context(input_str, position))) if layout_content: h_print("Layout: ", "'{}'".format(_(layout_content)), level=1) if expected_symbols: h_print("Symbols expected: ", [s.name for s in expected_symbols]) if lookahead_tokens: h_print("Token(s) ahead:", _(str(lookahead_tokens)))
def _do_error_recovery(self): """ If recovery is enabled, does error recovery for the heads in _last_shifted_heads. """ if self.debug: a_print("*** STARTING ERROR RECOVERY.", new_line=True) error = self.errors[-1] debug = self.debug self._active_heads = {} for head in self._last_shifted_heads: if debug: input_str = head.input_str symbols = head.state.actions.keys() h_print("Recovery initiated for head {}.".format(head), level=1, new_line=True) h_print("Symbols expected: ", [s.name for s in symbols], level=1) if type(self.error_recovery) is bool: # Default recovery if debug: prints("\tDoing default error recovery.") successful = self.default_error_recovery(head) else: # Custom recovery provided during parser construction if debug: prints("\tDoing custom error recovery.") successful = self.error_recovery(head, error) if successful: error.location.context.end_position = head.position if debug: a_print("New position is ", pos_to_line_col(input_str, head.position), level=1) a_print("New lookahead token is ", head.token_ahead, level=1) self._active_heads[head.state.state_id] = head if self.debug: a_print("*** ERROR RECOVERY SUCCEEDED. CONTINUING.", new_line=True) else: if debug: a_print("Killing head: ", head, level=1) if self.debug_trace: self._trace_step_kill(head)
def prod_callable(new_nt): if sep_ref: from parglare import pos_to_line_col raise GrammarError('Repetition modifier not allowed for ' 'optional (?) for symbol "{}" at {}.'.format( gsymbol.name, pos_to_line_col(context.input_str, context.start_position))) # Optional new_productions = [ Production(new_nt, ProductionRHS([gsymbol])), Production(new_nt, ProductionRHS([EMPTY])) ] return new_productions
def _full_context(input_str, position): from parglare.parser import pos_to_line_col, position_context line, column = pos_to_line_col(input_str, position) context = position_context(input_str, position) return context, line, column
def evaluate_line_col(self): from parglare.parser import pos_to_line_col self._line, self._column = \ pos_to_line_col(self.input_str, self.start_position)
def evaluate_line_col(self): if self.input_str and self.start_position is not None: from parglare.parser import pos_to_line_col self._line, self._column = pos_to_line_col(self.input_str, self.start_position)