summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ContextParser.java
blob: a5afce5cdceb5e093b3211edd5bec7ece70a8fe5 (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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package io.dico.dicore.command.parameter;

import io.dico.dicore.command.CommandException;
import io.dico.dicore.command.ExecutionContext;

import java.lang.reflect.Array;
import java.util.*;

public class ContextParser {
    private final ExecutionContext m_context;
    private final ArgumentBuffer m_buffer;
    private final ParameterList m_paramList;
    private final Parameter<?, ?> m_repeatedParam;
    private final List<Parameter<?, ?>> m_indexedParams;
    private final int m_maxIndex;
    private final int m_maxRequiredIndex;

    private Map<String, Object> m_valueMap;
    private Set<String> m_parsedKeys;
    private int m_completionCursor = -1;
    private Parameter<?, ?> m_completionTarget = null;

    public ContextParser(ExecutionContext context,
                         ParameterList parameterList,
                         Map<String, Object> valueMap,
                         Set<String> keySet) {
        m_context = context;
        m_paramList = parameterList;
        m_valueMap = valueMap;
        m_parsedKeys = keySet;

        m_buffer = context.getBuffer();
        m_repeatedParam = m_paramList.getRepeatedParameter();
        m_indexedParams = m_paramList.getIndexedParameters();
        m_maxIndex = m_indexedParams.size() - 1;
        m_maxRequiredIndex = m_paramList.getRequiredCount() - 1;
    }

    public ExecutionContext getContext() {
        return m_context;
    }

    public Map<String, Object> getValueMap() {
        return m_valueMap;
    }

    public Set<String> getParsedKeys() {
        return m_parsedKeys;
    }

    public void parse() throws CommandException {
        parseAllParameters();
    }

    public int getCompletionCursor() {
        if (!m_done) {
            throw new IllegalStateException();
        }
        return m_completionCursor;
    }

    public Parameter<?, ?> getCompletionTarget() {
        if (!m_done) {
            throw new IllegalStateException();
        }
        return m_completionTarget;
    }

    // ################################
    // # PARSING METHODS              #
    // ################################

    private boolean m_repeating = false;
    private boolean m_done = false;
    private int m_curParamIndex = -1;
    private Parameter<?, ?> m_curParam = null;
    private List<Object> m_curRepeatingList = null;

    private void parseAllParameters() throws CommandException {
        try {
            do {
                prepareStateToParseParam();
                if (m_done) break;
                parseCurParam();
            } while (!m_done);

        } finally {
            m_curParam = null;
            m_curRepeatingList = null;
            assignDefaultValuesToUncomputedParams();
            arrayifyRepeatedParamValue();
        }
    }

    private void prepareStateToParseParam() throws CommandException {

        boolean requireInput;
        if (identifyFlag()) {
            m_buffer.advance();
            prepareRepeatedParameterIfSet();
            requireInput = false;

        } else if (m_repeating) {
            m_curParam = m_repeatedParam;
            requireInput = false;

        } else if (m_curParamIndex < m_maxIndex) {
            m_curParamIndex++;
            m_curParam = m_indexedParams.get(m_curParamIndex);
            prepareRepeatedParameterIfSet();
            requireInput = m_curParamIndex <= m_maxRequiredIndex;

        } else if (m_buffer.hasNext()) {
            throw new CommandException("Too many arguments for /" + m_context.getAddress().getAddress());

        } else {
            m_done = true;
            return;
        }

        if (!m_buffer.hasNext()) {
            if (requireInput) {
                reportParameterRequired(m_curParam);
            }

            if (m_repeating) {
                m_done = true;
            }
        }

    }

    private boolean identifyFlag() {
        String potentialFlag = m_buffer.peekNext();
        Parameter<?, ?> target;
        if (potentialFlag != null
                && potentialFlag.startsWith("-")
                && (target = m_paramList.getParameterByName(potentialFlag)) != null
                && target.isFlag()
                && !m_valueMap.containsKey(potentialFlag)

//              Disabled because it's checked by {@link Parameter#parse(ExecutionContext, ArgumentBuffer)}
//              && (target.getFlagPermission() == null || m_context.getSender().hasPermission(target.getFlagPermission()))
                ) {
            m_curParam = target;
            return true;
        }

        return false;
    }

    private void prepareRepeatedParameterIfSet() throws CommandException {
        if (m_curParam != null && m_curParam == m_repeatedParam) {

            if (m_curParam.isFlag() && m_curParamIndex < m_maxRequiredIndex) {
                Parameter<?, ?> requiredParam = m_indexedParams.get(m_curParamIndex + 1);
                reportParameterRequired(requiredParam);
            }

            m_curRepeatingList = new ArrayList<>();
            assignValue(m_curRepeatingList);
            m_repeating = true;
        }
    }

    private void reportParameterRequired(Parameter<?, ?> param) throws CommandException {
        throw new CommandException("The argument '" + param.getName() + "' is required");
    }

    private void parseCurParam() throws CommandException {
        if (!m_buffer.hasNext() && !m_curParam.isFlag()) {
            assignDefaultValue();
            return;
        }

        int cursorStart = m_buffer.getCursor();

        if (m_context.isTabComplete() && "".equals(m_buffer.peekNext())) {
            assignAsCompletionTarget(cursorStart);
            return;
        }

        Object parseResult;
        try {
            parseResult = m_curParam.parse(m_context, m_buffer);
        } catch (CommandException e) {
            assignAsCompletionTarget(cursorStart);
            throw e;
        }

        assignValue(parseResult);
        m_parsedKeys.add(m_curParam.getName());
    }

    private void assignDefaultValue() throws CommandException {
        assignValue(m_curParam.getDefaultValue(m_context, m_buffer));
    }

    private void assignAsCompletionTarget(int cursor) {
        m_completionCursor = cursor;
        m_completionTarget = m_curParam;
        m_done = true;
    }

    private void assignValue(Object value) {
        if (m_repeating) {
            m_curRepeatingList.add(value);
        } else {
            m_valueMap.put(m_curParam.getName(), value);
        }
    }

    private void assignDefaultValuesToUncomputedParams() throws CommandException {
        // add default values for unset parameters
        for (Map.Entry<String, Parameter<?, ?>> entry : m_paramList.getParametersByName().entrySet()) {
            String name = entry.getKey();
            if (!m_valueMap.containsKey(name)) {
                if (m_repeatedParam == entry.getValue()) {
                    // below value will be turned into an array later
                    m_valueMap.put(name, Collections.emptyList());
                } else {
                    m_valueMap.put(name, entry.getValue().getDefaultValue(m_context, m_buffer));
                }
            }
        }
    }

    private void arrayifyRepeatedParamValue() {
        if (m_repeatedParam != null) {
            m_valueMap.computeIfPresent(m_repeatedParam.getName(), (k, v) -> {
                List list = (List) v;
                Class<?> returnType = m_repeatedParam.getType().getReturnType();
                Object array = Array.newInstance(returnType, list.size());
                ArraySetter setter = ArraySetter.getSetter(returnType);
                for (int i = 0, n = list.size(); i < n; i++) {
                    setter.set(array, i, list.get(i));
                }

                return array;
            });
        }
    }

    private interface ArraySetter {
        void set(Object array, int index, Object value);

        static ArraySetter getSetter(Class<?> clazz) {
            if (!clazz.isPrimitive()) {
                return (array, index, value) -> ((Object[]) array)[index] = value;
            }

            switch (clazz.getSimpleName()) {
                case "boolean":
                    return (array, index, value) -> ((boolean[]) array)[index] = (boolean) value;
                case "int":
                    return (array, index, value) -> ((int[]) array)[index] = (int) value;
                case "double":
                    return (array, index, value) -> ((double[]) array)[index] = (double) value;
                case "long":
                    return (array, index, value) -> ((long[]) array)[index] = (long) value;
                case "short":
                    return (array, index, value) -> ((short[]) array)[index] = (short) value;
                case "byte":
                    return (array, index, value) -> ((byte[]) array)[index] = (byte) value;
                case "float":
                    return (array, index, value) -> ((float[]) array)[index] = (float) value;
                case "char":
                    return (array, index, value) -> ((char[]) array)[index] = (char) value;
                case "void":
                default:
                    throw new InternalError("This should not happen");
            }
        }
    }

}