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 | /** |
| 17 | * A Token represents an actual lexeme; that is, a lexical unit, its location in |
| 18 | * the input text, its lexical kind (TokenKind) and any associated value. |
| 19 | */ |
| 20 | public class Token { |
| 21 | |
| 22 | public final TokenKind kind; |
| 23 | public final int left; |
| 24 | public final int right; |
| 25 | public final Object value; |
| 26 | |
| 27 | public Token(TokenKind kind, int left, int right) { |
| 28 | this(kind, left, right, null); |
| 29 | } |
| 30 | |
| 31 | public Token(TokenKind kind, int left, int right, Object value) { |
| 32 | this.kind = kind; |
| 33 | this.left = left; |
| 34 | this.right = right; |
| 35 | this.value = value; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Constructs an easy-to-read string representation of token, suitable for use |
| 40 | * in user error messages. |
| 41 | */ |
| 42 | @Override |
| 43 | public String toString() { |
| 44 | // TODO(bazel-team): do proper escaping of string literals |
| 45 | return kind == TokenKind.STRING ? ("\"" + value + "\"") |
| 46 | : value == null ? kind.getPrettyName() |
| 47 | : value.toString(); |
| 48 | } |
| 49 | |
| 50 | } |