File: grammar_sequence_test.py

package info (click to toggle)
sqlfluff 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,000 kB
  • sloc: python: 106,131; sql: 34,188; makefile: 52; sh: 8
file content (554 lines) | stat: -rw-r--r-- 17,264 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
"""Tests for the Sequence grammar.

NOTE: All of these tests depend somewhat on the KeywordSegment working as planned.
"""

import logging

import pytest

from sqlfluff.core.errors import SQLParseError
from sqlfluff.core.parser import Dedent, Indent, KeywordSegment, StringParser
from sqlfluff.core.parser.context import ParseContext
from sqlfluff.core.parser.grammar import Bracketed, Conditional, Sequence
from sqlfluff.core.parser.match_result import MatchResult
from sqlfluff.core.parser.types import ParseMode


def test__parser__grammar_sequence_repr():
    """Test the Sequence grammar __repr__ method."""
    bar = StringParser("bar", KeywordSegment)
    assert repr(bar) == "<StringParser: 'BAR'>"
    foo = StringParser("foo", KeywordSegment)
    sequence = Sequence(bar, foo)
    assert (
        repr(sequence) == "<Sequence: [<StringParser: 'BAR'>, <StringParser: 'FOO'>]>"
    )


def test__parser__grammar_sequence_nested_match(test_segments, caplog):
    """Test the Sequence grammar when nested."""
    bar = StringParser("bar", KeywordSegment)
    foo = StringParser("foo", KeywordSegment)
    baar = StringParser("baar", KeywordSegment)
    g = Sequence(Sequence(bar, foo), baar)

    ctx = ParseContext(dialect=None)
    # Confirm the structure of the test segments:
    assert [s.raw for s in test_segments] == ["bar", " \t ", "foo", "baar", " \t ", ""]

    with caplog.at_level(logging.DEBUG, logger="sqlfluff.parser"):
        # Matching just the start of the list shouldn't work.
        result1 = g.match(test_segments[:3], 0, ctx)

    assert not result1  # Check it returns falsy

    with caplog.at_level(logging.DEBUG, logger="sqlfluff.parser"):
        # Matching the whole list should.
        result2 = g.match(test_segments, 0, ctx)

    assert result2  # Check it returns truthy
    assert result2 == MatchResult(
        matched_slice=slice(0, 4),  # NOTE: One of these is space.
        child_matches=(
            MatchResult(
                matched_slice=slice(0, 1),
                matched_class=KeywordSegment,
                segment_kwargs={"instance_types": ("keyword",)},
            ),
            MatchResult(
                matched_slice=slice(2, 3),
                matched_class=KeywordSegment,
                segment_kwargs={"instance_types": ("keyword",)},
            ),
            MatchResult(
                matched_slice=slice(3, 4),
                matched_class=KeywordSegment,
                segment_kwargs={"instance_types": ("keyword",)},
            ),
        ),
    )


@pytest.mark.parametrize(
    "mode,sequence,terminators,input_slice,output_tuple",
    [
        # #####
        # Test matches where we should get something, and that's
        # the whole sequence.
        # NOTE: Include a little whitespace in the slice (i.e. the first _two_
        # segments) to check that it isn't included in the match.
        (ParseMode.STRICT, ["a"], [], slice(None, 2), (("keyword", "a"),)),
        (ParseMode.GREEDY, ["a"], [], slice(None, 2), (("keyword", "a"),)),
        (ParseMode.GREEDY_ONCE_STARTED, ["a"], [], slice(None, 2), (("keyword", "a"),)),
        # #####
        # Test matching on sequences where we run out of segments before matching
        # the whole sequence.
        # STRICT returns no match.
        (ParseMode.STRICT, ["a", "b"], [], slice(None, 2), ()),
        # GREEDY & GREEDY_ONCE_STARTED returns the content as unparsable, and
        # still don't include the trailing whitespace. The return value does
        # however have the matched "a" as a keyword and not a raw.
        (
            ParseMode.GREEDY,
            ["a", "b"],
            [],
            slice(None, 2),
            (("unparsable", (("keyword", "a"),)),),
        ),
        (
            ParseMode.GREEDY_ONCE_STARTED,
            ["a", "b"],
            [],
            slice(None, 2),
            (("unparsable", (("keyword", "a"),)),),
        ),
        # #####
        # Test matching on sequences where we fail to match the first element.
        # STRICT & GREEDY_ONCE_STARTED return no match.
        (ParseMode.STRICT, ["b"], [], slice(None, 2), ()),
        (ParseMode.GREEDY_ONCE_STARTED, ["b"], [], slice(None, 2), ()),
        # GREEDY claims the remaining elements (unmutated) as unparsable, but
        # does not claim any trailing whitespace.
        (
            ParseMode.GREEDY,
            ["b"],
            [],
            slice(None, 2),
            (("unparsable", (("raw", "a"),)),),
        ),
        # #####
        # Test matches where we should match the sequence fully, but there's more
        # to match.
        # First without terminators...
        # STRICT ignores the rest.
        (ParseMode.STRICT, ["a"], [], slice(None, 5), (("keyword", "a"),)),
        # The GREEDY modes claim the rest as unparsable.
        # NOTE: the whitespace in between is _not_ unparsable.
        (
            ParseMode.GREEDY,
            ["a"],
            [],
            slice(None, 5),
            (
                ("keyword", "a"),
                ("whitespace", " "),
                ("unparsable", (("raw", "b"), ("whitespace", " "), ("raw", "c"))),
            ),
        ),
        (
            ParseMode.GREEDY_ONCE_STARTED,
            ["a"],
            [],
            slice(None, 5),
            (
                ("keyword", "a"),
                ("whitespace", " "),
                ("unparsable", (("raw", "b"), ("whitespace", " "), ("raw", "c"))),
            ),
        ),
        # Second *with* terminators.
        # NOTE: The whitespace before the terminator is not included.
        (ParseMode.STRICT, ["a"], ["c"], slice(None, 5), (("keyword", "a"),)),
        (
            ParseMode.GREEDY,
            ["a"],
            ["c"],
            slice(None, 5),
            (("keyword", "a"), ("whitespace", " "), ("unparsable", (("raw", "b"),))),
        ),
        (
            ParseMode.GREEDY_ONCE_STARTED,
            ["a"],
            ["c"],
            slice(None, 5),
            (("keyword", "a"), ("whitespace", " "), ("unparsable", (("raw", "b"),))),
        ),
        # #####
        # Test matches where we match the first element of a sequence but not the
        # second (with terminators)
        (ParseMode.STRICT, ["a", "x"], ["c"], slice(None, 5), ()),
        # NOTE: For GREEDY modes, the matched portion is not included as an "unparsable"
        # only the portion which failed to match. The terminator is not included and
        # the matched portion is still mutated correctly.
        (
            ParseMode.GREEDY,
            ["a", "x"],
            ["c"],
            slice(None, 5),
            (("keyword", "a"), ("whitespace", " "), ("unparsable", (("raw", "b"),))),
        ),
        (
            ParseMode.GREEDY_ONCE_STARTED,
            ["a", "x"],
            ["c"],
            slice(None, 5),
            (("keyword", "a"), ("whitespace", " "), ("unparsable", (("raw", "b"),))),
        ),
        # #####
        # Test competition between sequence elements and terminators.
        # In GREEDY_ONCE_STARTED, the first element is matched before any terminators.
        (
            ParseMode.GREEDY_ONCE_STARTED,
            ["a"],
            ["a"],
            slice(None, 2),
            (("keyword", "a"),),
        ),
        # In GREEDY, the terminator is matched first and so takes precedence.
        (
            ParseMode.GREEDY,
            ["a"],
            ["a"],
            slice(None, 2),
            (),
        ),
        # NOTE: In these last two cases, the "b" isn't included because it acted as
        # a terminator before being considered in the sequence.
        (
            ParseMode.GREEDY_ONCE_STARTED,
            ["a", "b"],
            ["b"],
            slice(None, 3),
            (("unparsable", (("keyword", "a"),)),),
        ),
        (
            ParseMode.GREEDY,
            ["a", "b"],
            ["b"],
            slice(None, 3),
            (("unparsable", (("keyword", "a"),)),),
        ),
    ],
)
def test__parser__grammar_sequence_modes(
    mode,
    sequence,
    terminators,
    input_slice,
    output_tuple,
    structural_parse_mode_test,
):
    """Test the Sequence grammar with various parse modes.

    In particular here we're testing the treatment of unparsable
    sections.
    """
    structural_parse_mode_test(
        ["a", " ", "b", " ", "c", "d", " ", "d"],
        Sequence,
        sequence,
        terminators,
        {},
        mode,
        input_slice,
        output_tuple,
    )


@pytest.mark.parametrize(
    "input_seed,mode,sequence,kwargs,output_tuple",
    [
        # A strict asymmetric bracket shouldn't match
        (["(", "a"], ParseMode.STRICT, ["a"], {}, ()),
        # A sequence that isn't bracketed shouldn't match.
        # Regardless of mode.
        (["a"], ParseMode.STRICT, ["a"], {}, ()),
        (["a"], ParseMode.GREEDY, ["a"], {}, ()),
        # Test potential empty brackets (no whitespace)
        (
            ["(", ")"],
            ParseMode.STRICT,
            [],
            {},
            (
                (
                    "bracketed",
                    (
                        ("start_bracket", "("),
                        ("indent", ""),
                        ("dedent", ""),
                        ("end_bracket", ")"),
                    ),
                ),
            ),
        ),
        (
            ["(", ")"],
            ParseMode.GREEDY,
            [],
            {},
            (
                (
                    "bracketed",
                    (
                        ("start_bracket", "("),
                        ("indent", ""),
                        ("dedent", ""),
                        ("end_bracket", ")"),
                    ),
                ),
            ),
        ),
        # Test potential empty brackets (with whitespace)
        (
            ["(", " ", ")"],
            ParseMode.STRICT,
            [],
            {},
            (
                (
                    "bracketed",
                    (
                        ("start_bracket", "("),
                        ("indent", ""),
                        ("whitespace", " "),
                        ("dedent", ""),
                        ("end_bracket", ")"),
                    ),
                ),
            ),
        ),
        (
            ["(", " ", ")"],
            ParseMode.GREEDY,
            [],
            {},
            (
                (
                    "bracketed",
                    (
                        ("start_bracket", "("),
                        ("indent", ""),
                        ("whitespace", " "),
                        ("dedent", ""),
                        ("end_bracket", ")"),
                    ),
                ),
            ),
        ),
        (
            ["(", " ", ")"],
            ParseMode.STRICT,
            [],
            # Strict matching, without allowing gaps, shouldn't match.
            {"allow_gaps": False},
            (),
        ),
        # Happy path content match.
        (
            ["(", "a", ")"],
            ParseMode.STRICT,
            ["a"],
            {},
            (
                (
                    "bracketed",
                    (
                        ("start_bracket", "("),
                        ("indent", ""),
                        ("keyword", "a"),
                        ("dedent", ""),
                        ("end_bracket", ")"),
                    ),
                ),
            ),
        ),
        # Content match fails
        (
            ["(", "a", ")"],
            ParseMode.STRICT,
            ["b"],
            {},
            (),
        ),
        (
            ["(", "a", ")"],
            ParseMode.GREEDY,
            ["b"],
            {},
            (
                (
                    "bracketed",
                    (
                        ("start_bracket", "("),
                        ("indent", ""),
                        ("unparsable", (("raw", "a"),)),
                        ("dedent", ""),
                        ("end_bracket", ")"),
                    ),
                ),
            ),
        ),
        # Partial matches (not whole grammar matched)
        (
            ["(", "a", ")"],
            ParseMode.STRICT,
            ["a", "b"],
            {},
            (),
        ),
        (
            ["(", "a", ")"],
            ParseMode.GREEDY,
            ["a", "b"],
            {},
            (
                (
                    "bracketed",
                    (
                        ("start_bracket", "("),
                        ("indent", ""),
                        ("unparsable", (("keyword", "a"),)),
                        ("dedent", ""),
                        ("end_bracket", ")"),
                    ),
                ),
            ),
        ),
        # Partial matches (not whole sequence matched)
        (
            ["(", "a", " ", "b", ")"],
            ParseMode.STRICT,
            ["a"],
            {},
            (),
        ),
        (
            ["(", "a", " ", "b", ")"],
            ParseMode.GREEDY,
            ["a"],
            {},
            (
                (
                    "bracketed",
                    (
                        ("start_bracket", "("),
                        ("indent", ""),
                        ("keyword", "a"),
                        ("whitespace", " "),
                        ("unparsable", (("raw", "b"),)),
                        ("dedent", ""),
                        ("end_bracket", ")"),
                    ),
                ),
            ),
        ),
        # Test an unwrapped path (with square brackets)
        (
            ["[", "a", " ", "b", "]"],
            ParseMode.GREEDY,
            ["a"],
            {"bracket_type": "square"},
            (
                ("start_square_bracket", "["),
                ("indent", ""),
                ("keyword", "a"),
                ("whitespace", " "),
                ("unparsable", (("raw", "b"),)),
                ("dedent", ""),
                ("end_square_bracket", "]"),
            ),
        ),
    ],
)
def test__parser__grammar_bracketed_modes(
    input_seed,
    mode,
    sequence,
    kwargs,
    output_tuple,
    structural_parse_mode_test,
):
    """Test the Bracketed grammar with various parse modes."""
    structural_parse_mode_test(
        input_seed,
        Bracketed,
        sequence,
        [],
        kwargs,
        mode,
        slice(None, None),
        output_tuple,
    )


@pytest.mark.parametrize(
    "input_seed,mode,sequence,kwargs",
    [
        # Unclosed greedy brackets always raise errors.
        (["(", "a"], ParseMode.GREEDY, ["a"], {}),
        (
            ["(", " ", ")"],
            ParseMode.GREEDY,
            [],
            # Greedy matching, without allowing gaps, should raise a parsing error.
            # NOTE: This functionality doesn't get used much.
            {"allow_gaps": False},
        ),
    ],
)
def test__parser__grammar_bracketed_error_modes(
    input_seed,
    mode,
    sequence,
    kwargs,
    structural_parse_mode_test,
):
    """Test the Bracketed grammar with various parse modes."""
    with pytest.raises(SQLParseError):
        structural_parse_mode_test(
            input_seed,
            Bracketed,
            sequence,
            [],
            kwargs,
            mode,
            slice(None, None),
            (),
        )


def test__parser__grammar_sequence_indent_conditional_match(test_segments, caplog):
    """Test the Sequence grammar with indents."""
    bar = StringParser("bar", KeywordSegment)
    foo = StringParser("foo", KeywordSegment)
    # We will assume the default config has indented_joins = False.
    # We're testing without explicitly setting the `config_type` because
    # that's the assumed way of using the grammar in practice.
    g = Sequence(
        Dedent,
        Conditional(Indent, indented_joins=False),
        bar,
        Conditional(Indent, indented_joins=True),
        foo,
        Dedent,
    )
    ctx = ParseContext(dialect=None)
    with caplog.at_level(logging.DEBUG, logger="sqlfluff.parser"):
        m = g.match(test_segments, 0, parse_context=ctx)

    assert m == MatchResult(
        matched_slice=slice(0, 3),  # NOTE: One of these is space.
        child_matches=(
            # The two child keywords
            MatchResult(
                matched_slice=slice(0, 1),
                matched_class=KeywordSegment,
                segment_kwargs={"instance_types": ("keyword",)},
            ),
            MatchResult(
                matched_slice=slice(2, 3),
                matched_class=KeywordSegment,
                segment_kwargs={"instance_types": ("keyword",)},
            ),
        ),
        insert_segments=(
            (0, Dedent),  # The starting, unconditional dedent.
            (0, Indent),  # The conditional (activated) Indent.
            # NOTE: There *isn't* the other Indent.
            (3, Dedent),  # The closing unconditional dedent.
            # NOTE: This last one is still included even though it's
            # after the last matched segment.
        ),
    )