Пример #1
0
 def _advance_source(source: ParseSource):
     if source.has_current_line:
         if not source.is_at_eol__except_for_space:
             source.consume(len(source.remaining_part_of_current_line))
             misc_utils.raise_superfluous_arguments(
                 source.remaining_part_of_current_line.strip())
         source.consume_current_line()
Пример #2
0
def token_stream_from_parse_source(parse_source: ParseSource):
    """
    Gives a :class:`TokenStream` backed by the given :class:`ParseSource`.

    The source of the :class:`TokenStream` is the remaining sources of the :class:`ParseSource`
    """
    ts = new_token_stream(parse_source.remaining_source)
    yield ts
    parse_source.consume(ts.position)
Пример #3
0
def from_remaining_part_of_current_line_of_parse_source(parse_source: ParseSource):
    """
    Gives a :class:`TokenParserPrime` backed by the given :class:`ParseSource`.

    The source of the :class:`TokenParserPrime` is the remaining part of the current line of the :class:`ParseSource`
    """
    tp = new_token_parser(parse_source.remaining_part_of_current_line,
                          first_line_number=parse_source.current_line_number)
    yield tp
    parse_source.consume(tp.token_stream.position)
Пример #4
0
def parse_string_sdv_from_parse_source(source: ParseSource,
                                       conf: Configuration = DEFAULT_CONFIGURATION) -> StringSdv:
    """
    :param source: Has a current line
    :raises SingleInstructionInvalidArgumentException: If cannot parse a PathDdv
    """

    ts = new_token_stream(source.remaining_part_of_current_line)
    ret_val = parse_string_sdv(ts, conf)
    source.consume(ts.position)
    return ret_val
Пример #5
0
 def test_consume_with_invalid_arguments(self):
     test_cases = [
         ('a', 0, 2),
         ('a', 1, 1),
         ('a\nb', 0, 4),
         ('a\nb', 1, 3),
     ]
     for source, num_chars_on_current_line_to_consume_before_check, num_chars in test_cases:
         with self.subTest(source=source, num_chars=num_chars):
             source = ParseSource(source)
             source.consume_part_of_current_line(num_chars_on_current_line_to_consume_before_check)
             with self.assertRaises(ValueError):
                 source.consume(num_chars)
Пример #6
0
def from_remaining_part_of_current_line_of_parse_source(
        parse_source: ParseSource) -> ContextManager[TokenParser]:
    """
    Gives a :class:`TokenParser` backed by the given :class:`ParseSource`.

    The source of the :class:`TokenParser` is the remaining part of the current line of the :class:`ParseSource`
    """
    tp = new_token_parser(parse_source.remaining_part_of_current_line,
                          first_line_number=parse_source.current_line_number)
    try:
        yield tp
    finally:
        parse_source.consume(tp.token_stream.position)
Пример #7
0
 def test_consume__consume_all_characters_of_current_line_plus_one(self):
     test_cases = [
         ('first line\nsecond line',
          assert_source(is_at_eof=asrt.is_false,
                        has_current_line=asrt.is_true,
                        is_at_eol=asrt.is_false,
                        is_at_eol__except_for_space=asrt.is_false,
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('second line'),
                        column_index=asrt.equals(0),
                        remaining_source=asrt.equals('second line'))
          ),
         ('single line\n',
          assert_source(is_at_eof=asrt.is_true,
                        has_current_line=asrt.is_true,
                        is_at_eol=asrt.is_true,
                        is_at_eol__except_for_space=asrt.is_true,
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals(''),
                        column_index=asrt.equals(0),
                        remaining_source=asrt.equals(''))
          ),
         ('\n',
          assert_source(is_at_eof=asrt.is_true,
                        has_current_line=asrt.is_true,
                        is_at_eol=asrt.is_true,
                        is_at_eol__except_for_space=asrt.is_true,
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals(''),
                        column_index=asrt.equals(0),
                        remaining_source=asrt.equals(''))
          ),
         ('\nsecond line',
          assert_source(is_at_eof=asrt.is_false,
                        has_current_line=asrt.is_true,
                        is_at_eol=asrt.is_false,
                        is_at_eol__except_for_space=asrt.is_false,
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('second line'),
                        column_index=asrt.equals(0),
                        remaining_source=asrt.equals('second line'))
          ),
     ]
     for source_string, expectation in test_cases:
         with self.subTest(msg='consume len(current line)+1: ' + repr(source_string)):
             source = ParseSource(source_string)
             source.consume(1 + len(source.current_line_text))
             expectation.apply_with_message(self, source,
                                            'consume len(current line)+1:{}'.format(repr(source_string)))
Пример #8
0
def from_parse_source(source: ParseSource,
                      consume_last_line_if_is_at_eol_after_parse: bool = False,
                      consume_last_line_if_is_at_eof_after_parse: bool = False):
    """
    Gives a :class:`TokenParserPrime` backed by the given :class:`ParseSource`.

    The source of the :class:`TokenParserPrime` is the remaining sources of the :class:`ParseSource`
    """
    tp = new_token_parser(source.remaining_source,
                          first_line_number=source.current_line_number)
    yield tp
    source.consume(tp.token_stream.position)
    if consume_last_line_if_is_at_eol_after_parse and source.is_at_eol:
        source.consume_current_line()
    elif consume_last_line_if_is_at_eof_after_parse and source.is_at_eof:
        source.consume_current_line()
Пример #9
0
def from_parse_source(
    source: ParseSource,
    consume_last_line_if_is_at_eol_after_parse: bool = False,
    consume_last_line_if_is_at_eof_after_parse: bool = False
) -> ContextManager[TokenParser]:
    """
    Gives a :class:`TokenParser` backed by the given :class:`ParseSource`.

    The source of the :class:`TokenParser` is the remaining sources of the :class:`ParseSource`
    """
    tp = new_token_parser(source.remaining_source,
                          first_line_number=source.current_line_number)
    try:
        yield tp
    finally:
        source.consume(tp.token_stream.position)

    if consume_last_line_if_is_at_eol_after_parse and source.is_at_eol__except_for_space:
        source.consume_current_line()
    elif consume_last_line_if_is_at_eof_after_parse and source.is_at_eof:
        source.consume_current_line()
Пример #10
0
 def test_consume_with_valid_arguments(self):
     test_cases = [
         (TestSetupForConsume('one whole line and part of next, with one line remaining',
                              source_lines=['123',
                                            '45',
                                            '6'],
                              number_of_characters_to_consume_from_current_line_before_test=0,
                              number_of_characters_to_consume=4 + 1),
          assert_source(is_at_eof=asrt.equals(False),
                        is_at_eol=asrt.equals(False),
                        is_at_eol__except_for_space=asrt.equals(False),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        column_index=asrt.equals(1),
                        remaining_part_of_current_line=asrt.equals('5'),
                        remaining_source=asrt.equals('5\n6'))),
         (TestSetupForConsume('one whole line and part of next, with one line remaining, on column index 1',
                              source_lines=['123',
                                            '45',
                                            '6'],
                              number_of_characters_to_consume_from_current_line_before_test=1,
                              number_of_characters_to_consume=4 + 1 - 1),
          assert_source(is_at_eof=asrt.equals(False),
                        is_at_eol=asrt.equals(False),
                        is_at_eol__except_for_space=asrt.equals(False),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        column_index=asrt.equals(1),
                        remaining_part_of_current_line=asrt.equals('5'),
                        remaining_source=asrt.equals('5\n6'))),
         (TestSetupForConsume('two whole lines, with one line remaining',
                              source_lines=['123',
                                            '45',
                                            '6'],
                              number_of_characters_to_consume_from_current_line_before_test=0,
                              number_of_characters_to_consume=5 + 1),
          assert_source(is_at_eof=asrt.equals(False),
                        is_at_eol=asrt.equals(True),
                        is_at_eol__except_for_space=asrt.equals(True),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        column_index=asrt.equals(2),
                        remaining_part_of_current_line=asrt.equals(''),
                        remaining_source=asrt.equals('\n6'))),
         (TestSetupForConsume('two whole lines, with one line remaining, on column index 2',
                              source_lines=['123',
                                            '45',
                                            '6'],
                              number_of_characters_to_consume_from_current_line_before_test=2,
                              number_of_characters_to_consume=5 + 1 - 2),
          assert_source(is_at_eof=asrt.equals(False),
                        is_at_eol=asrt.equals(True),
                        is_at_eol__except_for_space=asrt.equals(True),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        column_index=asrt.equals(2),
                        remaining_part_of_current_line=asrt.equals(''),
                        remaining_source=asrt.equals('\n6'))),
         (TestSetupForConsume('two whole lines, with no line after',
                              source_lines=['123',
                                            '45'],
                              number_of_characters_to_consume_from_current_line_before_test=0,
                              number_of_characters_to_consume=5 + 1),
          assert_source(is_at_eof=asrt.equals(True),
                        is_at_eol=asrt.equals(True),
                        is_at_eol__except_for_space=asrt.equals(True),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        remaining_part_of_current_line=asrt.equals(''),
                        remaining_source=asrt.equals(''))),
         (TestSetupForConsume('two whole lines, with no line after, on column index 3',
                              source_lines=['123',
                                            '45'],
                              number_of_characters_to_consume_from_current_line_before_test=3,
                              number_of_characters_to_consume=5 + 1 - 3),
          assert_source(is_at_eof=asrt.equals(True),
                        is_at_eol=asrt.equals(True),
                        is_at_eol__except_for_space=asrt.equals(True),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        column_index=asrt.equals(2),
                        remaining_part_of_current_line=asrt.equals(''),
                        remaining_source=asrt.equals(''))),
     ]
     for setup, assertion in test_cases:
         with self.subTest(case_name=setup.name):
             source_lines = '\n'.join(setup.source_lines)
             source = ParseSource(source_lines)
             source.consume_part_of_current_line(setup.number_of_characters_to_consume_from_current_line_before_test)
             source.consume(setup.number_of_characters_to_consume)
             assertion.apply(self, source, asrt.MessageBuilder(setup.name))
Пример #11
0
 def test_consume__consume_all_characters_of_current_line(self):
     for source_string, expectation in self.test_cases_for_consume_all_characters_of_current_line:
         with self.subTest(msg='consume: ' + repr(source_string)):
             source = ParseSource(source_string)
             source.consume(len(source.current_line_text))
             expectation.apply_with_message(self, source, 'consume:{}'.format(repr(source_string)))