Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. 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.common.base.Joiner; |
| 17 | import com.google.common.collect.ImmutableList; |
| 18 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 19 | |
| 20 | import java.util.List; |
| 21 | |
| 22 | /** |
| 23 | * Syntax node for an import statement. |
| 24 | */ |
| 25 | public final class LoadStatement extends Statement { |
| 26 | |
Googler | ecb643d | 2015-02-26 10:22:49 +0000 | [diff] [blame] | 27 | public static final String PATH_ERROR_MSG = "Path '%s' is not valid. " |
| 28 | + "It should either start with a slash or refer to a file in the current directory."; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | private final ImmutableList<Ident> symbols; |
| 30 | private final PathFragment importPath; |
| 31 | |
| 32 | /** |
| 33 | * Constructs an import statement. |
| 34 | */ |
| 35 | LoadStatement(String path, List<Ident> symbols) { |
| 36 | this.symbols = ImmutableList.copyOf(symbols); |
| 37 | this.importPath = new PathFragment(path + ".bzl"); |
| 38 | } |
| 39 | |
| 40 | public ImmutableList<Ident> getSymbols() { |
| 41 | return symbols; |
| 42 | } |
| 43 | |
| 44 | public PathFragment getImportPath() { |
| 45 | return importPath; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public String toString() { |
| 50 | return String.format("load(\"%s\", %s)", importPath, Joiner.on(", ").join(symbols)); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | void exec(Environment env) throws EvalException, InterruptedException { |
| 55 | for (Ident i : symbols) { |
| 56 | try { |
| 57 | if (i.getName().startsWith("_")) { |
| 58 | throw new EvalException(getLocation(), "symbol '" + i + "' is private and cannot " |
| 59 | + "be imported"); |
| 60 | } |
| 61 | env.importSymbol(getImportPath(), i.getName()); |
| 62 | } catch (Environment.NoSuchVariableException | Environment.LoadFailedException e) { |
| 63 | throw new EvalException(getLocation(), e.getMessage()); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void accept(SyntaxTreeVisitor visitor) { |
| 70 | visitor.visit(this); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | void validate(ValidationEnvironment env) throws EvalException { |
Googler | ecb643d | 2015-02-26 10:22:49 +0000 | [diff] [blame] | 75 | if (!importPath.isAbsolute() && importPath.segmentCount() > 1) { |
| 76 | throw new EvalException(getLocation(), String.format(PATH_ERROR_MSG, importPath)); |
| 77 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | for (Ident symbol : symbols) { |
Laurent Le Brun | 964d8d5 | 2015-04-13 12:15:04 +0000 | [diff] [blame^] | 79 | env.declare(symbol.getName(), getLocation()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |