blob: 733b5387d0dfd8a5510785a7daac61883a3ef638 [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
laurentlb566ef5a2018-05-22 10:35:06 -070016import javax.annotation.Nullable;
17
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018/**
19 * A Token represents an actual lexeme; that is, a lexical unit, its location in
20 * the input text, its lexical kind (TokenKind) and any associated value.
21 */
laurentlb566ef5a2018-05-22 10:35:06 -070022class Token {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023
laurentlb566ef5a2018-05-22 10:35:06 -070024 TokenKind kind;
laurentlbfc23edc2018-06-05 09:42:59 -070025 int left;
26 int right;
laurentlb566ef5a2018-05-22 10:35:06 -070027 /**
28 * value is an Integer if the kind is INT.
29 * It is a String if the kind is STRING, IDENTIFIER, or COMMENT.
30 * It is null otherwise.
31 */
laurentlbfc23edc2018-06-05 09:42:59 -070032 @Nullable Object value;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033
laurentlb566ef5a2018-05-22 10:35:06 -070034 Token(TokenKind kind, int left, int right) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010035 this(kind, left, right, null);
36 }
37
laurentlb566ef5a2018-05-22 10:35:06 -070038 Token(TokenKind kind, int left, int right, Object value) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039 this.kind = kind;
40 this.left = left;
41 this.right = right;
42 this.value = value;
43 }
44
laurentlbfc23edc2018-06-05 09:42:59 -070045 Token copy() {
46 return new Token(kind, left, right, value);
47 }
48
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010049 /**
50 * Constructs an easy-to-read string representation of token, suitable for use
51 * in user error messages.
52 */
53 @Override
54 public String toString() {
55 // TODO(bazel-team): do proper escaping of string literals
56 return kind == TokenKind.STRING ? ("\"" + value + "\"")
57 : value == null ? kind.getPrettyName()
58 : value.toString();
59 }
60
61}