~subsetpark/the-brzozowski-variations

ref: 58727c21f0a59e3aa6caeab28b87724e66a2de87 the-brzozowski-variations/impls/ts/brzo.ts -rw-r--r-- 2.8 KiB
58727c21 — Zach Smith add typescript 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
interface Empty {
        kind: "empty";
}

function empty(): Empty { return { kind: "empty" } }

interface Epsilon {
        kind: "epsilon";
}

interface Char {
        kind: "char"
        value: string;
}

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

interface Cat {
        kind: "cat"
        left: Language
        right: Language
}

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

interface Alt {
        kind: "alt"
        left: Language
        right: Language
}

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

interface Rep {
        kind: "rep";
        value: Language;
}

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

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

function is_nullable(l: Language): boolean {
        switch (l.kind) {
                case "empty":
                        return false;
                case "epsilon":
                        return true;
                case "char":
                        return false;
                case "alt":
                        return is_nullable(l.left) || is_nullable(l.right);
                case "cat":
                        return is_nullable(l.left) && is_nullable(l.right);
                case "rep":
                        return true;
        }
}

function derive(pat: Language, char: string): Language {
        switch (pat.kind) {
                case "epsilon":
                case "empty":
                        return empty();
                case "char":
                        if (pat.value == char) {
                                return { kind: "epsilon" };
                        } else {
                                return { kind: "empty" };
                        }
                case "rep":
                        return cat(derive(pat.value, char), pat);
                case "alt":
                        return {
                                kind: "alt",
                                left: derive(pat.left, char),
                                right: derive(pat.right, char)
                        };
                case "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 }