blob: b80a2fe0696de2bdc1cc84916ab138e181192e9d [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
tomlua155b532017-11-08 20:12:47 +010016import com.google.common.base.Preconditions;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000017import javax.annotation.Nullable;
18
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019/**
Googler73d942f2019-10-03 12:31:02 -070020 * Syntax node for an argument to a function.
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000021 *
Googler73d942f2019-10-03 12:31:02 -070022 * <p>Arguments may be of four forms, as in {@code f(expr, id=expr, *expr, **expr)}. These are
23 * represented by the subclasses Positional, Keyword, Star, and StarStar.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024 */
Googler4ace4652019-09-16 07:47:08 -070025public abstract class Argument extends Node {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010026
adonovan07b15e62020-04-09 18:32:33 -070027 protected final Expression value;
Googler73d942f2019-10-03 12:31:02 -070028
adonovand51ac9b2020-04-10 10:15:08 -070029 Argument(FileLocations locs, Expression value) {
30 super(locs);
Googler73d942f2019-10-03 12:31:02 -070031 this.value = Preconditions.checkNotNull(value);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010032 }
33
Googler73d942f2019-10-03 12:31:02 -070034 public final Expression getValue() {
35 return value;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010036 }
37
adonovan07b15e62020-04-09 18:32:33 -070038 @Override
39 public int getEndOffset() {
40 return value.getEndOffset();
41 }
42
Googler73d942f2019-10-03 12:31:02 -070043 /** Return the name of this argument's parameter, or null if it is not a Keyword argument. */
44 @Nullable
45 public String getName() {
46 return null;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000047 }
48
Googler73d942f2019-10-03 12:31:02 -070049 /** Syntax node for a positional argument, {@code f(expr)}. */
50 public static final class Positional extends Argument {
adonovand51ac9b2020-04-10 10:15:08 -070051 Positional(FileLocations locs, Expression value) {
52 super(locs, value);
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000053 }
adonovan07b15e62020-04-09 18:32:33 -070054
55 @Override
56 public int getStartOffset() {
57 return value.getStartOffset();
58 }
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000059 }
60
Googler73d942f2019-10-03 12:31:02 -070061 /** Syntax node for a keyword argument, {@code f(id=expr)}. */
62 public static final class Keyword extends Argument {
63
64 // Unlike in Python, keyword arguments in Bazel BUILD files
65 // are about 10x more numerous than positional arguments.
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000066
adonovan07b15e62020-04-09 18:32:33 -070067 final Identifier id;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000068
adonovand51ac9b2020-04-10 10:15:08 -070069 Keyword(FileLocations locs, Identifier id, Expression value) {
70 super(locs, value);
adonovan07b15e62020-04-09 18:32:33 -070071 this.id = id;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000072 }
73
Googler73d942f2019-10-03 12:31:02 -070074 public Identifier getIdentifier() {
adonovan07b15e62020-04-09 18:32:33 -070075 return id;
Googler73d942f2019-10-03 12:31:02 -070076 }
77
brandjone2ffd5d2017-06-27 18:14:54 +020078 @Override
79 public String getName() {
adonovan07b15e62020-04-09 18:32:33 -070080 return id.getName();
81 }
82
83 @Override
84 public int getStartOffset() {
85 return id.getStartOffset();
Taras Tsugrii36941362018-06-08 16:31:53 -070086 }
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000087 }
88
Googler73d942f2019-10-03 12:31:02 -070089 /** Syntax node for an argument of the form {@code f(*expr)}. */
90 public static final class Star extends Argument {
adonovan07b15e62020-04-09 18:32:33 -070091 private final int starOffset;
92
adonovand51ac9b2020-04-10 10:15:08 -070093 Star(FileLocations locs, int starOffset, Expression value) {
94 super(locs, value);
adonovan07b15e62020-04-09 18:32:33 -070095 this.starOffset = starOffset;
96 }
97
98 @Override
99 public int getStartOffset() {
100 return starOffset;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +0000101 }
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +0000102 }
103
Googler73d942f2019-10-03 12:31:02 -0700104 /** Syntax node for an argument of the form {@code f(**expr)}. */
105 public static final class StarStar extends Argument {
adonovan07b15e62020-04-09 18:32:33 -0700106 private final int starStarOffset;
107
adonovand51ac9b2020-04-10 10:15:08 -0700108 StarStar(FileLocations locs, int starStarOffset, Expression value) {
109 super(locs, value);
adonovan07b15e62020-04-09 18:32:33 -0700110 this.starStarOffset = starStarOffset;
111 }
112
113 @Override
114 public int getStartOffset() {
115 return starStarOffset;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +0000116 }
laurentlb1e5352d2018-11-06 12:25:55 -0800117 }
118
brandjone2ffd5d2017-06-27 18:14:54 +0200119 @Override
Googler73d942f2019-10-03 12:31:02 -0700120 public void accept(NodeVisitor visitor) {
121 visitor.visit(this);
122 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100123}