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; |
Florian Weikert | 45deb33 | 2015-06-12 13:01:16 +0000 | [diff] [blame] | 31 | private final String pathString; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Constructs an import statement. |
| 35 | */ |
| 36 | LoadStatement(String path, List<Ident> symbols) { |
| 37 | this.symbols = ImmutableList.copyOf(symbols); |
| 38 | this.importPath = new PathFragment(path + ".bzl"); |
Florian Weikert | 45deb33 | 2015-06-12 13:01:16 +0000 | [diff] [blame] | 39 | this.pathString = path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | public ImmutableList<Ident> getSymbols() { |
| 43 | return symbols; |
| 44 | } |
| 45 | |
| 46 | public PathFragment getImportPath() { |
| 47 | return importPath; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public String toString() { |
| 52 | return String.format("load(\"%s\", %s)", importPath, Joiner.on(", ").join(symbols)); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | void exec(Environment env) throws EvalException, InterruptedException { |
| 57 | for (Ident i : symbols) { |
| 58 | try { |
| 59 | if (i.getName().startsWith("_")) { |
| 60 | throw new EvalException(getLocation(), "symbol '" + i + "' is private and cannot " |
| 61 | + "be imported"); |
| 62 | } |
| 63 | env.importSymbol(getImportPath(), i.getName()); |
| 64 | } catch (Environment.NoSuchVariableException | Environment.LoadFailedException e) { |
| 65 | throw new EvalException(getLocation(), e.getMessage()); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void accept(SyntaxTreeVisitor visitor) { |
| 72 | visitor.visit(this); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | void validate(ValidationEnvironment env) throws EvalException { |
Florian Weikert | 45deb33 | 2015-06-12 13:01:16 +0000 | [diff] [blame] | 77 | validateLoadPath(); |
| 78 | |
Googler | ecb643d | 2015-02-26 10:22:49 +0000 | [diff] [blame] | 79 | if (!importPath.isAbsolute() && importPath.segmentCount() > 1) { |
| 80 | throw new EvalException(getLocation(), String.format(PATH_ERROR_MSG, importPath)); |
| 81 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | for (Ident symbol : symbols) { |
Laurent Le Brun | 964d8d5 | 2015-04-13 12:15:04 +0000 | [diff] [blame] | 83 | env.declare(symbol.getName(), getLocation()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | } |
| 85 | } |
Florian Weikert | 45deb33 | 2015-06-12 13:01:16 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Throws an exception if the path argument to load() does starts with more than one forward |
| 89 | * slash ('/') |
| 90 | * |
| 91 | * @throws EvalException if the path is empty or starts with two forward slashes |
| 92 | */ |
Florian Weikert | 5269b3d | 2015-07-10 09:51:43 +0000 | [diff] [blame] | 93 | public void validateLoadPath() throws EvalException { |
Florian Weikert | 45deb33 | 2015-06-12 13:01:16 +0000 | [diff] [blame] | 94 | String error = null; |
| 95 | |
| 96 | if (pathString.isEmpty()) { |
| 97 | error = "Path argument to load() must not be empty"; |
| 98 | } else if (pathString.startsWith("//")) { |
| 99 | error = |
| 100 | "First argument of load() is a path, not a label. " |
| 101 | + "It should start with a single slash if it is an absolute path."; |
| 102 | } |
| 103 | |
| 104 | if (error != null) { |
| 105 | throw new EvalException(getLocation(), error); |
| 106 | } |
| 107 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | } |