summaryrefslogtreecommitdiffstats
path: root/llparse-builder/test/loop-checker-test.ts
blob: 0df60641d2e7ffe2ecb06889686b4216cf0fc647 (plain)
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
import * as assert from 'assert';

import { Builder, LoopChecker } from '../src/builder';

describe('LLParse/LoopChecker', () => {
  let b: Builder;
  let lc: LoopChecker;
  beforeEach(() => {
    b = new Builder();
    lc = new LoopChecker();
  });

  it('should detect shallow loops', () => {
    const start = b.node('start');

    start
      .otherwise(start);

    assert.throws(() => {
      lc.check(start);
    }, /Detected loop in "start".*"start"/);
  });

  it('should detect loops', () => {
    const start = b.node('start');
    const a = b.node('a');
    const invoke = b.invoke(b.code.match('nop'), {
      0: start,
    }, b.error(1, 'error'));

    start
      .peek('a', a)
      .otherwise(b.error(1, 'error'));

    a.otherwise(invoke);

    assert.throws(() => {
      lc.check(start);
    }, /Detected loop in "a".*"a" -> "invoke_nop"/);
  });

  it('should detect seemingly unreachable keys', () => {
    const start = b.node('start');
    const loop = b.node('loop');

    start
      .peek('a', loop)
      .otherwise(b.error(1, 'error'));

    loop
      .match('a', loop)
      .otherwise(loop);

    assert.throws(() => {
      lc.check(start);
    }, /Detected loop in "loop" through.*"loop"/);
  });

  it('should ignore loops through `peek` to `match`', () => {
    const start = b.node('start');
    const a = b.node('a');
    const invoke = b.invoke(b.code.match('nop'), {
      0: start,
    }, b.error(1, 'error'));

    start
      .peek('a', a)
      .otherwise(b.error(1, 'error'));

    a
      .match('abc', invoke)
      .otherwise(start);

    assert.doesNotThrow(() => lc.check(start));
  });

  it('should ignore irrelevant `peek`s', () => {
    const start = b.node('start');
    const a = b.node('a');

    start
      .peek('a', a)
      .otherwise(b.error(1, 'error'));

    a
      .peek('b', start)
      .otherwise(b.error(1, 'error'));

    assert.doesNotThrow(() => lc.check(start));
  });

  it('should ignore loops with multi `peek`/`match`', () => {
    const start = b.node('start');
    const another = b.node('another');

    const NUM: ReadonlyArray<string> = [
      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    ];

    const ALPHA: ReadonlyArray<string> = [
      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
      'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
      'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    ];

    start
      .match(ALPHA, start)
      .peek(NUM, another)
      .skipTo(start);

    another
      .match(NUM, another)
      .otherwise(start);

    assert.doesNotThrow(() => lc.check(start));
  });
});