def test__parser__grammar_oneof_exclude(seg_list): """Test the OneOf grammar exclude option.""" fs = KeywordSegment.make('foo') bs = KeywordSegment.make('bar') g = OneOf(bs, exclude=Sequence(bs, fs)) with RootParseContext(dialect=None) as ctx: # Just against the first alone assert g.match(seg_list[:1], parse_context=ctx) # Now with the bit to exclude invluded assert not g.match(seg_list, parse_context=ctx)
def test__parser__grammar_sequence_nested(seg_list, caplog): """Test the Sequence grammar when nested.""" fs = KeywordSegment.make('foo') bs = KeywordSegment.make('bar') bas = KeywordSegment.make('baar') g = Sequence(Sequence(bs, fs), bas) with RootParseContext(dialect=None) as ctx: with caplog.at_level(logging.DEBUG, logger="sqluff.parser"): # Matching the start of the list shouldn't work logging.info("#### TEST 1") assert not g.match(seg_list[:2], parse_context=ctx) # Matching the whole list should, and the result should be flat logging.info("#### TEST 2") assert g.match(seg_list, parse_context=ctx).matched_segments == ( bs('bar', seg_list[0].pos_marker), seg_list[1], # This will be the whitespace segment fs('foo', seg_list[2].pos_marker), bas('baar', seg_list[3].pos_marker) # NB: No whitespace at the end, this shouldn't be consumed. )
def test__parser__grammar_sequence_nested(seg_list, caplog): """Test the Sequence grammar when nested.""" fs = KeywordSegment.make('foo') bs = KeywordSegment.make('bar') bas = KeywordSegment.make('baar') g = Sequence(Sequence(bs, fs), bas) c = ParseContext() with caplog.at_level(logging.DEBUG): # Matching the start of the list shouldn't work logging.info("#### TEST 1") assert not g.match(seg_list[:2], parse_context=c) # Matching the whole list should, and the result should be flat logging.info("#### TEST 2") assert g.match(seg_list, parse_context=c).matched_segments == ( bs('bar', seg_list[0].pos_marker), seg_list[1], # This will be the whitespace segment fs('foo', seg_list[2].pos_marker), bas('baar', seg_list[3].pos_marker), seg_list[4] # This will be the whitespace segment )
def test__parser__grammar_sequence(seg_list, caplog): """Test the Sequence grammar.""" fs = KeywordSegment.make('foo') bs = KeywordSegment.make('bar') g = Sequence(bs, fs) gc = Sequence(bs, fs, code_only=False) with RootParseContext(dialect=None) as ctx: with caplog.at_level(logging.DEBUG, logger="sqluff.parser"): # Should be able to match the list using the normal matcher logging.info("#### TEST 1") m = g.match(seg_list, parse_context=ctx) assert m assert len(m) == 3 assert m.matched_segments == ( bs('bar', seg_list[0].pos_marker), seg_list[1], # This will be the whitespace segment fs('foo', seg_list[2].pos_marker)) # Shouldn't with the code_only matcher logging.info("#### TEST 2") assert not gc.match(seg_list, parse_context=ctx) # Shouldn't match even on the normal one if we don't start at the beginning logging.info("#### TEST 2") assert not g.match(seg_list[1:], parse_context=ctx)