~subsetpark/the-brzozowski-variations

ref: 3bc81c857a3f4c1233da02051f1171de01c26f85 the-brzozowski-variations/impls/ts/brzo.ts -rw-r--r-- 3.1 KiB
3bc81c85 — Zach Smith Use enums for literal discriminants 2 months ago
                                                                                
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
const enum languageKind {
        Empty,
        Epsilon,
        Char,
        Cat,
        Alt,
        Rep
};

interface Empty {
        kind: languageKind.Empty;
}

function empty(): Empty { return { kind: languageKind.Empty } }

interface Epsilon {
        kind: languageKind.Epsilon;
}

function epsilon(): Epsilon { return { kind: languageKind.Epsilon } }

interface Char {
        kind: languageKind.Char
        value: string;
}

function char(value: string): Char { return { kind: languageKind.Char, value } }

interface Cat {
        kind: languageKind.Cat
        left: Language
        right: Language
}

function cat(left: Language, right: Language): Cat {
        return { kind: languageKind.Cat, left, right }
}

interface Alt {
        kind: languageKind.Alt
        left: Language
        right: Language
}

function alt(left: Language, right: Language): Alt {
        return { kind: languageKind.Alt, left, right }
}

interface Rep {
        kind: languageKind.Rep;
        value: Language;
}

function rep(value: Language): Rep { return { kind: languageKind.Rep, value } }

type Language = Empty | Epsilon | Rep | Cat | Alt | Char

function is_nullable(l: Language): boolean {
        switch (l.kind) {
                case languageKind.Empty:
                        return false;
                case languageKind.Epsilon:
                        return true;
                case languageKind.Char:
                        return false;
                case languageKind.Alt:
                        return is_nullable(l.left) || is_nullable(l.right);
                case languageKind.Cat:
                        return is_nullable(l.left) && is_nullable(l.right);
                case languageKind.Rep:
                        return true;
        }
}

function derive(pat: Language, char: string): Language {
        switch (pat.kind) {
                case languageKind.Empty:
                case languageKind.Epsilon:
                        return empty();
                case languageKind.Char:
                        if (pat.value == char) {
                                return epsilon();
                        } else {
                                return empty();
                        }
                case languageKind.Rep:
                        return cat(derive(pat.value, char), pat);
                case languageKind.Alt:
                        return alt(
                                derive(pat.left, char),
                                derive(pat.right, char)
                        );
                case languageKind.Cat:
                        const base = cat(derive(pat.left, char), pat.right);
                        if (is_nullable(pat.left)) {
                                return alt(derive(pat.right, char), base);
                        } else {
                                return base;
                        }
        }
}

function matches(pat: Language, text: string): boolean {
        if (text.length == 0) {
                return is_nullable(pat)
        } else {
                return matches(derive(pat, text[0]), text.slice(1))
        }
}

module.exports = { char, cat, alt, rep, matches }