aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java
blob: c1dfc4a775b0c4b472977d1d02f13c0fb1c398c5 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.template;

import com.yahoo.vespa.hosted.node.admin.task.util.text.Cursor;
import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange;

import java.util.Optional;

/**
 * Parses a template String, see {@link Template} for details.
 *
 * @author hakonhall
 */
public class TemplateParser {
    private final TemplateDescriptor descriptor;
    private final Cursor start;
    private final Cursor current;
    private final Template template;
    private final FormEndsIn formEndsIn;

    public static Template parse(TemplateDescriptor descriptor, String text) {
        return parse(descriptor, new Cursor(text), FormEndsIn.EOT).template();
    }

    private static TemplateParser parse(TemplateDescriptor descriptor, Cursor start, FormEndsIn formEndsIn) {
        var parser = new TemplateParser(descriptor, start, formEndsIn);
        parser.parse();
        return parser;
    }

    enum FormEndsIn { EOT, END }

    TemplateParser(TemplateDescriptor descriptor, Cursor start, FormEndsIn formEndsIn) {
        this.descriptor = descriptor;
        this.start = new Cursor(start);
        this.current = new Cursor(start);
        this.template = new Template(start);
        this.formEndsIn = formEndsIn;
    }

    CursorRange range() { return new CursorRange(start, current); }
    Template template() { return template; }

    private void parse() {
        do {
            current.advanceTo(descriptor.startDelimiter());
            if (!current.equals(start)) {
                template.appendLiteralSection(current);
            }

            if (current.eot()) return;

            if (!parseSection()) return;
        } while (true);
    }

    /** Returns true if end was reached (according to formEndsIn). */
    private boolean parseSection() {
        current.skip(descriptor.startDelimiter());

        if (current.skip('=')) {
            parseExpression();
        } else {
            var startOfType = new Cursor(current);
            String type = skipId().orElseThrow(() -> new BadTemplateException(current, "Missing section name"));

            switch (type) {
                case "end":
                    if (formEndsIn == FormEndsIn.EOT)
                        throw new BadTemplateException(startOfType, "Extraneous 'end'");
                    parseEndAttribute();
                    return false;
                case "list":
                    parseListSection();
                    break;
                default:
                    throw new BadTemplateException(startOfType, "Unknown section '" + type + "'");
            }
        }

        return !current.eot();
    }

    private void parseExpression() {
        var nameStart = new Cursor(current);
        String name = parseId();
        parseEndDelimiter(false);
        template.appendVariableSection(name, nameStart, current);
    }

    private void parseEndAttribute() {
        parseEndDelimiter(true);
    }

    private void parseListSection() {
        skipWhitespace();
        var startOfName = new Cursor(current);
        String name = parseId();
        parseEndDelimiter(true);

        TemplateParser bodyParser = parse(descriptor, current, FormEndsIn.END);
        current.set(bodyParser.current);

        template.appendListSection(name, startOfName, current, bodyParser.template());
    }

    private void skipWhitespace() {
        if (!current.skipWhitespace()) {
            throw new BadTemplateException(current, "Expected whitespace");
        }
    }

    private String parseId() {
        return skipId().orElseThrow(() -> new BadTemplateException(current, "Expected identifier"));
    }

    private Optional<String> skipId() { return Token.skipId(current); }

    private void parseEndDelimiter(boolean removeFollowingNewline) {
        if (!current.skip(descriptor.endDelimiter())) {
            throw new BadTemplateException(current, "Expected section end (" + descriptor.endDelimiter() + ")");
        }

        if (removeFollowingNewline) current.skip('\n');
    }
}