001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics.ext.internal.parser;
021
022import static java.util.Objects.requireNonNull;
023
024/**
025 * This interface represents a parsed token. A token is a <em>pair</em> of a
026 * token type and a token value.
027 *
028 * @param type the type of {@code this} token
029 * @param value the actual token value
030 * @param <V> the token value type
031 *
032 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
033 * @since 7.1
034 * @version 7.1
035 */
036public record Token<V>(Type type, V value) {
037
038        /**
039         * Represents the type of the token, with a given type code and type name.
040         */
041        public interface Type {
042
043                /**
044                 * The common <em>end of file</em> token type.
045                 */
046                Type EOF = Type.of(-1, "EOF");
047
048                /**
049                 * Return the type code, which uniquely identifies the token type.
050                 *
051                 * @return the code of the token type
052                 */
053                int code();
054
055                /**
056                 * Return the name of the token.
057                 *
058                 * @return the name of the token
059                 */
060                String name();
061
062                /**
063                 * Create a new token of the given {@code value} and {@code this} type.
064                 *
065                 * @param value the token value
066                 * @return a new toke
067                 * @throws NullPointerException if the given {@code value} is {@code null}
068                 */
069                default <V> Token<V> token(final V value) {
070                        return new Token<>(this, value);
071                }
072
073                /**
074                 * Create a new token of the given character and {@code this} type.
075                 *
076                 * @param c the token value
077                 * @return a new toke
078                 */
079                default Token<String> token(final char c) {
080                        return token(Character.toString(c));
081                }
082
083                /**
084                 * Create a new token type with the given {@code code} and {@code name}.
085                 *
086                 * @param code the code of the created token type
087                 * @param name the name of the created token type
088                 * @return a new token type
089                 * @throws NullPointerException if the given {@code name} is {@code null}
090                 */
091                static <V> Type of(final int code, final String name) {
092                        record SimpleType(int code, String name) implements Type {
093                                SimpleType {
094                                        requireNonNull(name);
095                                }
096                        }
097
098                        return new SimpleType(code, name);
099                }
100        }
101
102}