aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java
blob: 3586c649820ccba1f5fe43c7fba5e1616db713ca (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// 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 java.util.EnumSet;
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 TemplateBuilder templateBuilder;

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

    private static TemplateParser parse(TemplateDescriptor descriptor, Cursor start, EnumSet<Sentinel> sentinel) {
        var parser = new TemplateParser(descriptor, start);
        parser.parse(parser.templateBuilder.topLevelSectionList(), sentinel);
        return parser;
    }

    private enum Sentinel { ELSE, END, EOT }

    private TemplateParser(TemplateDescriptor descriptor, Cursor start) {
        this.descriptor = descriptor;
        this.start = new Cursor(start);
        this.current = new Cursor(start);
        this.templateBuilder = new TemplateBuilder(start);
    }

    Template template() { return templateBuilder.build(); }

    private Sentinel parse(SectionList sectionList, EnumSet<Sentinel> sentinels) {
        do {
            current.advanceTo(descriptor.startDelimiter());
            if (!current.equals(start)) {
                sectionList.appendLiteralSection(current);
            }

            if (current.eot()) {
                if (!sentinels.contains(Sentinel.EOT)) {
                    throw new BadTemplateException(start, "Missing end directive for section started");
                }
                return Sentinel.EOT;
            }

            Optional<Sentinel> sentinel = parseSection(sectionList, sentinels);
            if (sentinel.isPresent()) return sentinel.get();
        } while (true);
    }

    private Optional<Sentinel> parseSection(SectionList sectionList, EnumSet<Sentinel> sentinels) {
        current.skip(descriptor.startDelimiter());

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

            switch (type) {
                case "else" -> {
                    if (!sentinels.contains(Sentinel.ELSE))
                        throw new BadTemplateException(startOfType, "Stray 'else'");
                    parseEndDirective();
                    return Optional.of(Sentinel.ELSE);
                }
                case "end" -> {
                    if (!sentinels.contains(Sentinel.END))
                        throw new BadTemplateException(startOfType, "Stray 'end'");
                    parseEndDirective();
                    return Optional.of(Sentinel.END);
                }
                case "if" -> parseIfSection(sectionList);
                case "list" -> parseListSection(sectionList);
                default -> throw new BadTemplateException(startOfType, "Unknown section '" + type + "'");
            }
        }

        return Optional.empty();
    }

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

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

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

        TemplateParser bodyParser = parse(descriptor, current, EnumSet.of(Sentinel.END));
        current.set(bodyParser.current);

        sectionList.appendListSection(name, startOfName, current, bodyParser.templateBuilder.build());
    }

    private void parseIfSection(SectionList sectionList) {
        skipRequiredWhitespaces();
        boolean negated = current.skip(Token.NEGATE_CHAR);
        current.skipWhitespaces();
        var startOfName = new Cursor(current);
        String name = parseId();
        parseEndDelimiter(true);

        SectionList ifSectionList = new SectionList(current, templateBuilder);
        Sentinel ifSentinel = parse(ifSectionList, EnumSet.of(Sentinel.ELSE, Sentinel.END));

        Optional<SectionList> elseSectionList = Optional.empty();
        if (ifSentinel == Sentinel.ELSE) {
            elseSectionList = Optional.of(new SectionList(current, templateBuilder));
            parse(elseSectionList.get(), EnumSet.of(Sentinel.END));
        }

        sectionList.appendIfSection(negated, name, startOfName, current, ifSectionList, elseSectionList);
    }

    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 void parseEndDelimiter(boolean allowSkipNewline) {
        boolean removeNewlineCharPresent = current.skip(Token.REMOVE_NEWLINE_CHAR);

        if (!current.skip(descriptor.endDelimiter()))
            throw new BadTemplateException(current, "Expected section end (" + descriptor.endDelimiter() + ")");

        // The presence of the remove-newline-char means the opposite behavior is wanted.
        if (allowSkipNewline && (removeNewlineCharPresent != descriptor.removeNewline()))
            current.skip('\n');
    }
}