summaryrefslogtreecommitdiff
path: root/src/parser.zig
blob: 49fcdb6f4f96bd5b6da997876b6299d7eea3369e (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
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
const std = @import("std");
const Token = @import("regex.zig").Token;

const TokenList = std.SinglyLinkedList(Token);

pub const Rule = enum { Terminal, Re, Rer, Cat, Catr, E, L };

pub const ParseTree = struct {
    rule: Rule,
    token: ?Token,
    sibling: ?*ParseTree,
    child: ?*ParseTree,

    pub fn init(rule: Rule, allocator: *std.mem.Allocator) !*ParseTree {
        const t = try allocator.create(ParseTree);

        t.* = .{
            .rule = rule,
            .token = null,
            .sibling = null,
            .child = null,
        };

        return t;
    }

    pub fn appendChild(self: *ParseTree, child: *ParseTree) void {
        if (self.child) |c| {
            var p = c;
            while (p.sibling) |sibling| {
                p = sibling;
            }
            p.sibling = child;
        } else {
            self.child = child;
        }
    }
};

pub const Parser = struct {
    tokens: *const TokenList,
    itr: ?*TokenList.Node,
    allocator: *std.mem.Allocator,

    pub const Error = error{ UnexpectedToken, ExpectedToken };

    pub fn init(tl: *const TokenList, allocator: *std.mem.Allocator) Parser {
        return .{ .tokens = tl, .allocator = allocator, .itr = tl.first.? };
    }

    pub fn expectToken(self: *Parser, kind: Token.Kind) !*ParseTree {
        if (self.itr) |node| {
            if (node.data.kind == kind) {
                const t = try ParseTree.init(Rule.Terminal, self.allocator);

                t.token = node.data;

                self.itr = node.next;

                return t;
            } else {
                std.debug.print("{d} | error: expected token of kind {s} but found token of kind {s}", .{ node.data.pos, @tagName(kind), @tagName(node.data.kind) });
                return Error.UnexpectedToken;
            }
        } else {
            std.debug.print("error: expected token of kind {s} but reached end of stream!\n", .{@tagName(kind)}); // should use actual logging eventually
            return Error.ExpectedToken;
        }
    }

    pub fn parseRe(self: *Parser) error{ ExpectedToken, UnexpectedToken, OutOfMemory }!*ParseTree {
        const t = try ParseTree.init(Rule.Re, self.allocator);

        t.appendChild(try self.parseCat());
        t.appendChild(try self.parseRer());

        return t;
    }

    pub fn parseRer(self: *Parser) !*ParseTree {
        const t = try ParseTree.init(Rule.Rer, self.allocator);

        if (self.itr) |_| {
            t.appendChild(try self.expectToken(Token.Kind.Or));
            t.appendChild(try self.parseCat());
        } // else -> epsilon

        return t;
    }

    pub fn parseCat(self: *Parser) !*ParseTree {
        const t = try ParseTree.init(Rule.Cat, self.allocator);

        t.appendChild(try self.parseE());
        t.appendChild(try self.parseCatr());

        return t;
    }

    pub fn parseCatr(self: *Parser) !*ParseTree {
        const t = try ParseTree.init(Rule.Catr, self.allocator);

        if (self.itr) |_| {
            t.appendChild(try self.parseE());
            t.appendChild(try self.parseCatr());
        }

        return t;
    }

    pub fn parseE(self: *Parser) !*ParseTree {
        const t = try ParseTree.init(Rule.E, self.allocator);

        t.appendChild(try self.parseL());

        if (self.itr) |node| {
            switch (node.data.kind) {
                .Plus, .Question, .Star => {
                    t.appendChild(try self.expectToken(node.data.kind)); // bruh
                },
                else => {},
            }
        } else {
            return Error.ExpectedToken;
        }

        return t;
    }

    pub fn parseL(self: *Parser) !*ParseTree {
        const t = try ParseTree.init(Rule.L, self.allocator);

        if (self.itr) |node| {
            switch (node.data.kind) {
                .Literal, .Class, .Dot => {
                    t.appendChild(try self.expectToken(node.data.kind)); // bruh
                },
                .LParen => {
                    t.appendChild(try self.expectToken(Token.Kind.LParen));
                    t.appendChild(try self.parseRe());
                    t.appendChild(try self.expectToken(Token.Kind.RParen));
                },
                else => {
                    return Error.UnexpectedToken;
                },
            }
        } else {
            return Error.ExpectedToken;
        }

        return t;
    }
};