aboutsummaryrefslogtreecommitdiff
path: root/src/math.zig
blob: 4c164bb19cd77ee2c2cf9ccfee65807ed7274541 (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
const std = @import("std");
const Allocator = std.mem.Allocator;
const Token = @import("lexer/Token.zig");
const Lexer = @import("lexer/Lexer.zig");
const Element = @import("eval/Element.zig");
const testing = @import("testing.zig");
const doTest = testing.doMath;
const doTestError = testing.doError;

pub const Error = error{InvalidMathBlock} || Allocator.Error;

pub fn parse(alloc: Allocator, l: *Lexer) Error!Element {
    _ = l.next();
    const beg = l.next() orelse return Error.InvalidMathBlock;
    if (!beg.kind.isDelimiter()) return Error.InvalidMathBlock;
    const math = try Element.Math.Block.init(alloc);
    var acc = try std.ArrayList(u8).initCapacity(alloc, 2);
    while (l.next()) |it| {
        if (it.kind == .math_block) return Error.InvalidMathBlock;
        try acc.appendSlice(alloc, it.content);
        // restore modifications done by the lexer
        if (it.kind.requiresSpace())
            try acc.append(alloc, ' ');
        if (it.kind.isDelimiter()) {
            const next = l.peek() orelse return Error.InvalidMathBlock;
            if (next.kind == .math_block) break;
        }
    }
    var end = l.next() orelse return Error.InvalidMathBlock;
    if (end.kind != .math_block) return Error.InvalidMathBlock;
    const el = try Element.Figure.init(alloc, math.element());
    math.content = try acc.toOwnedSlice(alloc);
    end = l.next() orelse return el.element();
    if (!end.kind.isDelimiter()) return Error.InvalidMathBlock;
    return el.element();
}

test {
    const alloc = std.testing.allocator;

    try doTest(parse, alloc,
        \\$$$
        \\x
        \\$$$
    , "<figure>" ++ @embedFile("data/block_1.svg") ++ "</figure>");
    try doTest(parse, alloc,
        \\$$$
        \\x^2
        \\$$$
    , "<figure>" ++ @embedFile("data/block_2.svg") ++ "</figure>");
    try doTest(parse, alloc,
        \\$$$
        \\forall x in RR, quad f(x) = x^2
        \\$$$
    , "<figure>" ++ @embedFile("data/block_3.svg") ++ "</figure>");
}