blob: efb1a64edf3c8e02ba71839fc0e52941d68847d5 [file] [log] [blame]
Vladimir Moskva8d610c62016-09-15 14:36:41 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.syntax;
15
16import com.google.devtools.build.lib.events.Location;
brandjone2ffd5d2017-06-27 18:14:54 +020017import java.io.IOException;
Vladimir Moskva8d610c62016-09-15 14:36:41 +000018
brandjon2b51f782017-07-25 21:05:04 +020019/**
20 * An index expression ({@code obj[field]}). Not to be confused with a slice expression ({@code
21 * obj[from:to]}). The object may be either a sequence or an associative mapping (most commonly
22 * lists and dictionaries).
23 */
Vladimir Moskva8d610c62016-09-15 14:36:41 +000024public final class IndexExpression extends Expression {
25
brandjon990622b2017-07-11 19:56:45 +020026 private final Expression object;
Vladimir Moskva8d610c62016-09-15 14:36:41 +000027
28 private final Expression key;
29
brandjon990622b2017-07-11 19:56:45 +020030 public IndexExpression(Expression object, Expression key) {
31 this.object = object;
Vladimir Moskva8d610c62016-09-15 14:36:41 +000032 this.key = key;
33 }
34
35 public Expression getObject() {
brandjon990622b2017-07-11 19:56:45 +020036 return object;
Vladimir Moskva8d610c62016-09-15 14:36:41 +000037 }
38
39 public Expression getKey() {
40 return key;
41 }
42
43 @Override
brandjone2ffd5d2017-06-27 18:14:54 +020044 public void prettyPrint(Appendable buffer) throws IOException {
brandjon990622b2017-07-11 19:56:45 +020045 object.prettyPrint(buffer);
brandjone2ffd5d2017-06-27 18:14:54 +020046 buffer.append('[');
47 key.prettyPrint(buffer);
48 buffer.append(']');
Vladimir Moskva8d610c62016-09-15 14:36:41 +000049 }
50
51 @Override
52 Object doEval(Environment env) throws EvalException, InterruptedException {
brandjon2b51f782017-07-25 21:05:04 +020053 return evaluate(object.eval(env), key.eval(env), env, getLocation());
Vladimir Moskva23ba4a82017-02-21 15:30:54 +000054 }
55
56 /**
brandjon2b51f782017-07-25 21:05:04 +020057 * Retrieves the value associated with a key in the given object.
58 *
59 * @throws EvalException if {@code object} is not a list or dictionary
Vladimir Moskva23ba4a82017-02-21 15:30:54 +000060 */
brandjon2b51f782017-07-25 21:05:04 +020061 public static Object evaluate(Object object, Object key, Environment env, Location loc)
62 throws EvalException, InterruptedException {
63 if (object instanceof SkylarkIndexable) {
64 Object result = ((SkylarkIndexable) object).getIndex(key, loc);
65 // TODO(bazel-team): We shouldn't have this convertToSkylark call here. If it's needed at all,
66 // it should go in the implementations of SkylarkIndexable#getIndex that produce non-Skylark
67 // values.
Vladimir Moskva8d610c62016-09-15 14:36:41 +000068 return SkylarkType.convertToSkylark(result, env);
brandjon2b51f782017-07-25 21:05:04 +020069 } else if (object instanceof String) {
70 String string = (String) object;
71 int index = EvalUtils.getSequenceIndex(key, string.length(), loc);
Vladimir Moskva8d610c62016-09-15 14:36:41 +000072 return string.substring(index, index + 1);
brandjon2b51f782017-07-25 21:05:04 +020073 } else {
74 throw new EvalException(
75 loc,
76 String.format(
77 "type '%s' has no operator [](%s)",
78 EvalUtils.getDataTypeName(object), EvalUtils.getDataTypeName(key)));
Vladimir Moskva8d610c62016-09-15 14:36:41 +000079 }
Vladimir Moskva8d610c62016-09-15 14:36:41 +000080 }
81
82 @Override
83 public void accept(SyntaxTreeVisitor visitor) {
84 visitor.visit(this);
85 }
laurentlbaf682d12017-08-24 20:32:02 +020086
87 @Override
88 public Kind kind() {
89 return Kind.INDEX;
90 }
Vladimir Moskva8d610c62016-09-15 14:36:41 +000091}