def _parse_single_line(instructions: Sequence[ActPhaseInstruction]) -> str: non_empty_lines = all_source_code_lines__std_syntax(instructions) if not non_empty_lines: raise ParseException.of_str(_NO_SOURCE_LINES) if len(non_empty_lines) > 1: raise ParseException.of_str(_TOO_MANY_SOURCE_LINES) return non_empty_lines[0]
def parse(self) -> _SourceInfoForInterpreterWithArgumentList: try: return self._parse_program_with_arguments() except TokenSyntaxError as ex: raise ParseException.of_str( std_error_message_text_for_token_syntax_error_from_exception( ex)) except SingleInstructionInvalidArgumentException as ex: raise ParseException.of_str(ex.error_message)
def _syntax_error_if_not_at_eof(source: parse_source.ParseSource): if source.is_at_eof: return if source.is_at_eol__except_for_space: source.consume_current_line() _syntax_error_if_not_at_eof(source) else: raise ParseException.of_str( 'Superfluous arguments of {PROGRAM}: {src}'.format( PROGRAM=syntax_elements.PROGRAM_SYNTAX_ELEMENT.singular_name, src=source.remaining_part_of_current_line))
def test_WHEN_parse_raises_parse_exception_THEN_execution_SHOULD_stop_with_result_of_syntax_error( self): # ARRANGE # expected_cause = 'failure message' step_recorder = ListRecorder() recording_atc = ActionToCheckWrapperThatRecordsSteps( step_recorder, _atc_that_does_nothing()) actor = ActorForConstantAtc(recording_atc, parse_atc=do_raise( ParseException.of_str(expected_cause))) arrangement = Arrangement(test_case=_empty_test_case(), actor=actor) # ASSERT # expectation = Expectation(phase_result=asrt_result.status_is( ExecutionFailureStatus.SYNTAX_ERROR)) # APPLY # execute_and_check(self, arrangement, expectation) self.assertEqual([], step_recorder.recorded_elements, 'executed steps')
def parse(self, instructions: Sequence[ActPhaseInstruction]) -> ActionToCheck: try: source_lines = list( itertools.chain.from_iterable( map(self._get_source_code_lines, instructions))) reference_instructions_arguments = self._get_reference_instruction_arguments( source_lines) references = list( itertools.chain.from_iterable( map(_parse_reference_arguments, reference_instructions_arguments))) return ActionToCheckThatRunsConstantActions( symbol_usages_action=do_return(references)) except SingleInstructionInvalidArgumentException as ex: raise ParseException.of_str(ex.error_message)
def _get_reference_instruction_arguments( self, lines: Sequence[str]) -> List[str]: ret_val = [] for line in lines: if not line or line.isspace(): continue parts = line.split(maxsplit=1) if len(parts ) == 2 and parts[0] == self._reference_instruction_name: ret_val.append(parts[1]) else: err_msg = new_pre_formatted_str_for_test( str_constructor.FormatPositional( 'Invalid act phase instruction: {}\nExpecting: {}', line, self._reference_instruction_name)) raise ParseException.of_str(err_msg) return ret_val
def apply(self, instructions: Sequence[ActPhaseInstruction]): raise ParseException(self.cause)
def _parse_program(self, source: parse_source.ParseSource) -> ProgramSdv: try: return self._program_parser.parse(source) except SingleInstructionInvalidArgumentException as ex: raise ParseException.of_str(ex.error_message)
def parse(self, instructions: Sequence[ActPhaseInstruction]): raise ParseException.of_str('unconditional parse failure')