Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | import com.google.common.collect.ImmutableList; |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableMap; |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 18 | import java.io.IOException; |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 19 | import java.util.Map; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Syntax node for an import statement. |
| 23 | */ |
| 24 | public final class LoadStatement extends Statement { |
| 25 | |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 26 | private final ImmutableMap<Identifier, String> symbolMap; |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 27 | private final StringLiteral imp; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Constructs an import statement. |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 31 | * |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 32 | * <p>{@code symbols} maps a symbol to the original name under which it was defined in |
| 33 | * the bzl file that should be loaded. If aliasing is used, the value differs from its key's |
| 34 | * {@code symbol.getName()}. Otherwise, both values are identical. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | */ |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 36 | public LoadStatement(StringLiteral imp, Map<Identifier, String> symbolMap) { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 37 | this.imp = imp; |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 38 | this.symbolMap = ImmutableMap.copyOf(symbolMap); |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | public ImmutableMap<Identifier, String> getSymbolMap() { |
| 42 | return symbolMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 45 | public ImmutableList<Identifier> getSymbols() { |
laurentlb | 3ca2a10 | 2017-08-17 13:08:49 +0200 | [diff] [blame] | 46 | return ImmutableList.copyOf(symbolMap.keySet()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 49 | public StringLiteral getImport() { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 50 | return imp; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 54 | public void prettyPrint(Appendable buffer, int indentLevel) throws IOException { |
| 55 | printIndent(buffer, indentLevel); |
| 56 | buffer.append("load("); |
| 57 | imp.prettyPrint(buffer); |
laurentlb | 3ca2a10 | 2017-08-17 13:08:49 +0200 | [diff] [blame] | 58 | for (Identifier symbol : symbolMap.keySet()) { |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 59 | buffer.append(", "); |
| 60 | String origName = symbolMap.get(symbol); |
| 61 | if (origName.equals(symbol.getName())) { |
| 62 | buffer.append('"'); |
| 63 | symbol.prettyPrint(buffer); |
| 64 | buffer.append('"'); |
| 65 | } else { |
| 66 | symbol.prettyPrint(buffer); |
| 67 | buffer.append("=\""); |
| 68 | buffer.append(origName); |
| 69 | buffer.append('"'); |
| 70 | } |
| 71 | } |
| 72 | buffer.append(")\n"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | public void accept(SyntaxTreeVisitor visitor) { |
| 77 | visitor.visit(this); |
| 78 | } |
laurentlb | af682d1 | 2017-08-24 20:32:02 +0200 | [diff] [blame] | 79 | |
| 80 | @Override |
| 81 | public Kind kind() { |
| 82 | return Kind.LOAD; |
| 83 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | } |