def _parse_exe_file_ref(tokens: TokenStream) -> FileRefResolver: token = tokens.head if token.is_plain and option_parsing.matches(syntax_elements.PYTHON_EXECUTABLE_OPTION_NAME, token.string): tokens.consume() return file_ref_resolvers.constant(file_refs.absolute_file_name(sys.executable)) else: return parse_file_ref.parse_file_ref(tokens, conf=syntax_elements.REL_OPTION_ARG_CONF)
def _parse_rel_option_type(options: RelOptionsConfiguration, source: TokenStream) -> RelOptionType: option_str = source.head.string rel_option_type = _resolve_relativity_option_type(option_str) if rel_option_type not in options.accepted_options: return _raise_invalid_option(option_str, options) source.consume() return rel_option_type
def _parse_rel_option_type(options: RelOptionsConfiguration, source: TokenStream) -> RelOptionType: option_str = source.head.string rel_option_type = _resolve_relativity_option_type(option_str) if rel_option_type not in options.accepted_options: return _raise_invalid_option(option_str, options) source.consume() return rel_option_type
def _try_parse_rel_symbol_option(options: RelOptionsConfiguration, source: TokenStream) -> Optional[SymbolReference]: option_str = source.head.string if not option_parsing.matches(REL_SYMBOL_OPTION_NAME, option_str): return None source.consume() if source.is_null: msg = 'Missing symbol name argument for {} option'.format(option_str) raise SingleInstructionInvalidArgumentException(msg) _raise_invalid_argument_exception_if_symbol_does_not_have_valid_syntax(source.head, option_str) symbol_name = source.consume().string return SymbolReference(symbol_name, reference_restrictions_for_path_symbol(options.accepted_relativity_variants))
def _try_parse_rel_symbol_option( options: RelOptionsConfiguration, source: TokenStream) -> Optional[SymbolReference]: option_str = source.head.string if not option_parsing.matches(REL_SYMBOL_OPTION_NAME, option_str): return None source.consume() if source.is_null: msg = 'Missing symbol name argument for {} option'.format(option_str) raise SingleInstructionInvalidArgumentException(msg) _raise_invalid_argument_exception_if_symbol_does_not_have_valid_syntax( source.head, option_str) symbol_name = source.consume().string return SymbolReference( symbol_name, reference_restrictions_for_path_symbol( options.accepted_relativity_variants))
def remaining_source(remaining_contents_of_first_line: str, following_lines: Sequence[str] = ()) -> TokenParser: """ :param remaining_contents_of_first_line: Part of the first line that has not been consumed. :return: Source with some initial content of the first line that has been consumed. """ content = '\n'.join([remaining_contents_of_first_line] + list(following_lines)) token_stream = TokenStream(content) return TokenParser(token_stream)
def test_head_token_pass(self): # ARRANGE # token_stream = TokenStream('a_token') passing_token_assertion = asrt.sub_component('token_type', Token.is_quoted.fget, asrt.is_false) assertion = sut.assert_token_stream(head_token=passing_token_assertion) # ACT & ASSERT # assertion.apply_without_message(self, token_stream)
def _test_case__sym_ref_or_string_parser(self, parser: ParserFromTokens, tc: TC): # ARRANGE # token_stream = TokenStream(tc.source_string) token_parser = TokenParser(token_stream) expected_fragments = tc.expectation.fragments head_is_plain = TokenStream(tc.source_string).head.is_plain assertion_on_value = (_EqualsEitherOfSymbolName( expected_fragments[0].value) if head_is_plain and len(expected_fragments) == 1 and expected_fragments[0].is_symbol else _EqualsEitherOfStringSdv(expected_fragments)) # ACT # actual = parser.parse(token_parser) # ASSERT # tc.expectation.token_stream.apply_with_message(self, token_stream, 'token_stream') assertion_on_value.apply_with_message(self, actual, 'fragment')
def test_look_ahead_state_pass(self): # ARRANGE # assertion = sut.assert_token_stream(look_ahead_state=asrt.is_(LookAheadState.NULL)) # ACT & ASSERT # token_stream = TokenStream('') self.assertTrue(token_stream.look_ahead_state is LookAheadState.NULL, 'sanity check: this test assumes that the constructor of the token parser ' 'sets correct look-ahead state') assertion.apply_without_message(self, token_stream)
def test_fail(self): # ARRANGE # assertion = sut.assert_token_stream(look_ahead_state=asrt.is_(LookAheadState.NULL)) # ACT & ASSERT # token_stream = TokenStream('a_token') self.assertTrue(token_stream.look_ahead_state is LookAheadState.HAS_TOKEN, 'sanity check: this test assumes that the constructor of the token parser ' 'sets correct look-ahead state') assert_that_assertion_fails(assertion, token_stream)
def test_head_token_fail(self): # ARRANGE # token_stream = TokenStream('a_token') failing_token_assertion = asrt.sub_component('token_type', Token.is_quoted.fget, asrt.is_true) assertion = sut.assert_token_stream(head_token=failing_token_assertion) # ACT & ASSERT # assert_that_assertion_fails(assertion, token_stream)
def _parse_set(self, variable_name: str, tokens_for_value: TokenStream) -> TheInstructionEmbryo: if tokens_for_value.is_null: raise SingleInstructionInvalidArgumentException(_format('Missing {VALUE}.')) value_token = tokens_for_value.consume() if not tokens_for_value.is_null: raise SingleInstructionInvalidArgumentException(_format('Superfluous arguments.')) value_resolver = parse_string.parse_string_resolver_from_token(value_token, is_any_data_type()) executor = _SetExecutor(variable_name, value_resolver) return TheInstructionEmbryo(executor, value_resolver.references)
def _with_non_empty_token_stream( self, tokens: TokenStream) -> Either[SymbolName, PathSdv]: initial_argument_string = tokens.remaining_part_of_current_line relativity_info = parse_explicit_relativity_info( self.conf.rel_opt_conf.options, self.conf.source_file_location, tokens) if not self.conf.rel_opt_conf.path_suffix_is_required and tokens.remaining_part_of_current_line_is_empty: if relativity_info is None: return self._result_from_no_arguments() else: path_part_sdv2_path_sdv = self._path_constructor( relativity_info) return Either.of_right( path_part_sdv2_path_sdv(path_part_sdvs.empty())) if tokens.look_ahead_state is LookAheadState.SYNTAX_ERROR: raise SingleInstructionInvalidArgumentException( std_error_message_text_for_token_syntax_error( tokens.head_syntax_error_description)) elif tokens.look_ahead_state is LookAheadState.NULL: raise SingleInstructionInvalidArgumentException( 'Missing {}: {}'.format( self.conf.rel_opt_conf.argument_syntax_name, initial_argument_string)) head_token = tokens.head if reserved_tokens.IS_RESERVED_WORD.matches(head_token): raise SingleInstructionInvalidArgumentException( 'Illegal file name: {}'.format(head_token.string)) elif head_token.type is TokenType.PLAIN: ensure_is_not_option_argument(head_token.source_string) tokens.consume() if relativity_info is None: return self._without_explicit_relativity(head_token) else: path_part_2_path_sdv = self._path_constructor(relativity_info) return Either.of_right( self._with_explicit_relativity(head_token, path_part_2_path_sdv))
def _test_case__parse_string_sdv(self, tc: TC): # ARRANGE # token_stream = TokenStream(tc.source_string) # ACT # actual = sut.parse_string_sdv(token_stream, CONFIGURATION) # ASSERT # tc.expectation.token_stream.apply_with_message(self, token_stream, 'token_stream') assertion_on_result = assert_equals_string_sdv( tc.expectation.fragments) assertion_on_result.apply_with_message(self, actual, 'fragment')
def _test_case(self, tc: TC): with self.subTest(token_stream=tc.source_string): # ARRANGE # token_stream = TokenStream(tc.source_string) # ACT # actual = sut.parse_fragments_from_tokens(token_stream, CONFIGURATION) # ASSERT # self.assertEqual(tc.expectation.fragments, actual, 'fragment') tc.expectation.token_stream.apply_with_message( self, token_stream, 'token_stream')
def test_pass_when_no_explicit_component_assertion(self): test_cases = [ NameAndValue('Empty source', ''), NameAndValue('Single token source', 'a_token'), NameAndValue('Two token source', 'first_token second'), ] for test_case in test_cases: with self.subTest(test_case.name): # ARRANGE # token_stream = TokenStream(test_case.value) assertion = sut.assert_token_stream() # ACT & ASSERT # assertion.apply_without_message(self, token_stream)
def parse_fragments_from_tokens__w_is_plain( tokens: TokenStream, conf: Configuration = DEFAULT_CONFIGURATION) -> Tuple[bool, List[symbol_syntax.Fragment]]: """ Consumes a single token. :raises SingleInstructionInvalidArgumentException: Missing argument """ if tokens.is_null: raise SingleInstructionInvalidArgumentException('Expecting {} argument'.format(conf.argument_name)) string_token = tokens.consume() if string_token.is_plain and string_token.source_string in reserved_words.RESERVED_TOKENS: raise SingleInstructionInvalidArgumentException( 'Illegal {}: {}'.format(conf.argument_name, string_token.source_string), ) return string_token.is_plain, parse_fragments_from_token(string_token)
def _parse_rel_source_file(source: TokenStream) -> bool: option_str = source.head.string if option_parsing.matches(REL_SOURCE_FILE_DIR_OPTION_NAME, option_str): source.consume() return True return False
def _multi_line_source(lines: List[str], **kwargs) -> TokenStream: all_lines = '\n'.join([_src(line, **kwargs) for line in lines]) return TokenStream(all_lines)
def _single_line_source(s: str, **kwargs) -> TokenStream: return TokenStream(_src(s, **kwargs))
def test_missing_argument__parse_sym_ref_or_string_sdv(self): parser = sut.SymbolReferenceOrStringParser(CONFIGURATION) with self.assertRaises(SingleInstructionInvalidArgumentException): parser.parse(TokenParser(TokenStream('')))
def test_remaining_source_after_head_fail(self): # ARRANGE # token_stream = TokenStream('fst_token snd_token') assertion = sut.assert_token_stream(remaining_source_after_head=asrt.equals('fst_token')) # ACT & ASSERT # assert_that_assertion_fails(assertion, token_stream)
def _parse_rel_source_file(source: TokenStream) -> bool: option_str = source.head.string if option_parsing.matches(REL_SOURCE_FILE_DIR_OPTION_NAME, option_str): source.consume() return True return False
def test_remaining_source_after_head_pass(self): # ARRANGE # token_stream = TokenStream('fst_token snd_token') assertion = sut.assert_token_stream(remaining_source_after_head=asrt.equals('snd_token')) # ACT & ASSERT # assertion.apply_without_message(self, token_stream)
def test_is_null_fail(self): # ARRANGE # token_stream = TokenStream('a_token') assertion = sut.assert_token_stream(is_null=asrt.is_true) # ACT & ASSERT # assert_that_assertion_fails(assertion, token_stream)
def _token_parser_of(source: str) -> TokenParser: return TokenParser(TokenStream(source))
def test_is_null_pass(self): # ARRANGE # token_stream = TokenStream('') assertion = sut.assert_token_stream(is_null=asrt.is_true) # ACT & ASSERT # assertion.apply_without_message(self, token_stream)