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 | |
laurentlb | 566ef5a | 2018-05-22 10:35:06 -0700 | [diff] [blame] | 16 | import javax.annotation.Nullable; |
| 17 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | /** |
| 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 | */ |
laurentlb | 566ef5a | 2018-05-22 10:35:06 -0700 | [diff] [blame] | 22 | class Token { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | |
laurentlb | 566ef5a | 2018-05-22 10:35:06 -0700 | [diff] [blame] | 24 | TokenKind kind; |
laurentlb | fc23edc | 2018-06-05 09:42:59 -0700 | [diff] [blame] | 25 | int left; |
| 26 | int right; |
laurentlb | 566ef5a | 2018-05-22 10:35:06 -0700 | [diff] [blame] | 27 | /** |
| 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 | */ |
laurentlb | fc23edc | 2018-06-05 09:42:59 -0700 | [diff] [blame] | 32 | @Nullable Object value; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | |
laurentlb | 566ef5a | 2018-05-22 10:35:06 -0700 | [diff] [blame] | 34 | Token(TokenKind kind, int left, int right) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | this(kind, left, right, null); |
| 36 | } |
| 37 | |
laurentlb | 566ef5a | 2018-05-22 10:35:06 -0700 | [diff] [blame] | 38 | Token(TokenKind kind, int left, int right, Object value) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | this.kind = kind; |
| 40 | this.left = left; |
| 41 | this.right = right; |
| 42 | this.value = value; |
| 43 | } |
| 44 | |
laurentlb | fc23edc | 2018-06-05 09:42:59 -0700 | [diff] [blame] | 45 | Token copy() { |
| 46 | return new Token(kind, left, right, value); |
| 47 | } |
| 48 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | /** |
| 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 | } |