示例#1
0
def _dialect_specific_segment_parses(dialect, segmentref, raw, caplog):
    """Test that specific segments parse as expected.

    NB: We're testing the PARSE function not the MATCH function
    although this will be a recursive parse and so the match
    function of SUBSECTIONS will be tested if present. The match
    function of the parent will not be tested.
    """
    config = FluffConfig(overrides=dict(dialect=dialect))
    seg_list = lex(raw, config=config)
    Seg = validate_segment(segmentref, config=config)

    # Most segments won't handle the end of file marker. We should strip it.
    if seg_list[-1].is_type("end_of_file"):
        seg_list = seg_list[:-1]

    # This test is different if we're working with RawSegment
    # derivatives or not.
    if isinstance(Seg, Matchable) or issubclass(Seg, RawSegment):
        print("Raw/Parser route...")
        with RootParseContext.from_config(config) as ctx:
            with caplog.at_level(logging.DEBUG):
                parsed = Seg.match(segments=seg_list, parse_context=ctx)
        assert isinstance(parsed, MatchResult)
        assert len(parsed.matched_segments) == 1
        print(parsed)
        parsed = parsed.matched_segments[0]
        print(parsed)
    else:
        print("Base route...")
        # Construct an unparsed segment
        seg = Seg(seg_list, pos_marker=seg_list[0].pos_marker)
        # Perform the match (THIS IS THE MEAT OF THE TEST)
        with RootParseContext.from_config(config) as ctx:
            with caplog.at_level(logging.DEBUG):
                parsed = seg.parse(parse_context=ctx)
        print(parsed)
        assert isinstance(parsed, Seg)

    # Check we get a good response
    print(parsed)
    print(type(parsed))
    print(type(parsed.raw))
    # Check we're all there.
    assert parsed.raw == raw
    # Check that there's nothing un parsable
    typs = parsed.type_set()
    assert "unparsable" not in typs
示例#2
0
 def parse(
     self, segments: Sequence["BaseSegment"], recurse=True, fname: str = None
 ) -> Optional["BaseSegment"]:
     """Parse a series of lexed tokens using the current dialect."""
     if not segments:
         return None
     # Instantiate the root segment
     root_segment = self.RootSegment(segments=segments, fname=fname)
     # Call .parse() on that segment
     with RootParseContext.from_config(config=self.config, recurse=recurse) as ctx:
         parsed = root_segment.parse(parse_context=ctx)
     return parsed
示例#3
0
def _dialect_specific_segment_not_match(dialect, segmentref, raw, caplog):
    """Test that specific segments do not match.

    NB: We're testing the MATCH function not the PARSE function.
    This is the opposite to the above.
    """
    config = FluffConfig(overrides=dict(dialect=dialect))
    seg_list = lex(raw, config=config)
    Seg = validate_segment(segmentref, config=config)

    with RootParseContext.from_config(config) as ctx:
        with caplog.at_level(logging.DEBUG):
            match = Seg.match(segments=seg_list, parse_context=ctx)

    assert not match
示例#4
0
    def parse(
        self,
        segments: Sequence["BaseSegment"],
        recurse=True,
        fname: str = None,
    ) -> Optional["BaseSegment"]:
        """Parse a series of lexed tokens using the current dialect."""
        if not segments:  # pragma: no cover
            # This should normally never happen because there will usually
            # be an end_of_file segment. It would probably only happen in
            # api use cases.
            return None
        # Instantiate the root segment
        root_segment = self.RootSegment(segments=segments, fname=fname)
        # Call .parse() on that segment

        with RootParseContext.from_config(config=self.config,
                                          recurse=recurse) as ctx:
            parsed = root_segment.parse(parse_context=ctx)

        return parsed