aboutsummaryrefslogtreecommitdiff
path: root/src/z/parser/js/grammar/root.zig
blob: e1a1e969ee93d25912cf00c7a87a4cf81a7d35dd (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
const std = @import("std");

const parser = @import("../../root.zig");
pub const Lexer = parser.Lexer(Grammar);
pub const Token = parser.Token(Grammar);

pub const Whitespace = @import("whitespace.zig").Whitespace;
pub const Literal = @import("literal.zig").Literal;
pub const Punctuator = @import("punctuator.zig").Punctuator;
pub const Identifier = @import("identifier.zig").Identifier;

pub const Grammar = union(enum) {
    whitespace: Whitespace,
    literal: Literal,
    punctuator: Punctuator,
    identifier: Identifier,
    end_of_file: void,

    pub fn tokenize(lexer: *Lexer) Lexer.Error!void {
        while (lexer.peakChar()) |_| {
            Whitespace.tokenize(lexer) catch {
                Literal.tokenize(lexer) catch {
                    Punctuator.tokenize(lexer) catch {
                        try Identifier.tokenize(lexer);
                    };
                };
            };
        }

        lexer.push(lexer.token(0, .end_of_file));
    }
};

test {
    _ = std.testing.refAllDecls(@This());
}