Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.syntax; |
| 15 | |
| 16 | import com.google.devtools.build.lib.events.Location; |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 17 | import java.io.IOException; |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 18 | |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 19 | /** |
| 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 Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 24 | public final class IndexExpression extends Expression { |
| 25 | |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 26 | private final Expression object; |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 27 | |
| 28 | private final Expression key; |
| 29 | |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 30 | public IndexExpression(Expression object, Expression key) { |
| 31 | this.object = object; |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 32 | this.key = key; |
| 33 | } |
| 34 | |
| 35 | public Expression getObject() { |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 36 | return object; |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | public Expression getKey() { |
| 40 | return key; |
| 41 | } |
| 42 | |
| 43 | @Override |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 44 | public void prettyPrint(Appendable buffer) throws IOException { |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 45 | object.prettyPrint(buffer); |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 46 | buffer.append('['); |
| 47 | key.prettyPrint(buffer); |
| 48 | buffer.append(']'); |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | Object doEval(Environment env) throws EvalException, InterruptedException { |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 53 | return evaluate(object.eval(env), key.eval(env), env, getLocation()); |
Vladimir Moskva | 23ba4a8 | 2017-02-21 15:30:54 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 57 | * 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 Moskva | 23ba4a8 | 2017-02-21 15:30:54 +0000 | [diff] [blame] | 60 | */ |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 61 | 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 Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 68 | return SkylarkType.convertToSkylark(result, env); |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 69 | } else if (object instanceof String) { |
| 70 | String string = (String) object; |
| 71 | int index = EvalUtils.getSequenceIndex(key, string.length(), loc); |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 72 | return string.substring(index, index + 1); |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 73 | } 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 Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 79 | } |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void accept(SyntaxTreeVisitor visitor) { |
| 84 | visitor.visit(this); |
| 85 | } |
laurentlb | af682d1 | 2017-08-24 20:32:02 +0200 | [diff] [blame] | 86 | |
| 87 | @Override |
| 88 | public Kind kind() { |
| 89 | return Kind.INDEX; |
| 90 | } |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 91 | } |