summaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java
blob: 48bc4d737e29e8cdcfa229124426005262c980bd (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
127
128
129
130
131
132
133
134
135
136
137
// 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
 */
class TemplateParser {
    private final TemplateDescriptor descriptor;
    private final Cursor start;
    private final Cursor current;
    private final Template template;
    private final FormEndsIn formEndsIn;

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

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

    private 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()) {
                if (formEndsIn == FormEndsIn.END) {
                    throw new BadTemplateException(current,
                                                   "Missing end directive for section started at " +
                                                   start.calculateLocation().lineAndColumnText());
                }
                return;
            }

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

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

        if (current.skip(descriptor.variableDirectiveChar())) {
            parseVariableSection();
        } 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'");
                    parseEndDirective();
                    return false;
                case "form":
                    parseSubformSection();
                    break;
                default:
                    throw new BadTemplateException(startOfType, "Unknown section '" + type + "'");
            }
        }

        return !current.eot();
    }

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

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

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

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

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

    private void skipRequiredWhitespaces() {
        if (!current.skipWhitespaces()) {
            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 boolean parseEndDelimiter(boolean skipNewline) {
        boolean removeNewline = current.skip(descriptor.removeNewlineChar());
        if (!current.skip(descriptor.endDelimiter()))
            throw new BadTemplateException(current, "Expected section end (" + descriptor.endDelimiter() + ")");

        if (skipNewline && removeNewline)
            current.skip('\n');

        return removeNewline;
    }
}