001/* 002 * Java Genetic Algorithm Library (jenetics-7.2.0). 003 * Copyright (c) 2007-2023 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.lang.String.format; 023import static java.util.Objects.requireNonNull; 024 025import java.util.function.Predicate; 026 027/** 028 * Parser implementation for parsing a given token sequences. 029 * 030 * @param <T> the token 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 class Parser<T> { 037 038 private final Tokenizer<T> _tokenizer; 039 private final TokenRing<T> _lookahead; 040 041 /** 042 * Create a new parser object with the given {@code tokenizer} and lookahead 043 * count {@code k}. 044 * 045 * @param tokenizer the token source {@code this} parser uses 046 * @param k the lookahead count 047 */ 048 public Parser(final Tokenizer<T> tokenizer, final int k) { 049 _tokenizer = requireNonNull(tokenizer); 050 _lookahead = new TokenRing<>(k); 051 for (int i = 0; i < k; ++i) { 052 consume(); 053 } 054 } 055 056 /** 057 * Return the lookahead token with the given index. The index starts at 058 * {@code 1}. The returned token might be {@code null}. 059 * 060 * @param index lookahead index 061 * @return the token at the given index 062 */ 063 public T LT(final int index) { 064 return _lookahead.LT(index); 065 } 066 067 /** 068 * Try to <em>match</em> and consume the next token of the given 069 * {@code type}. If the current token is not from the given type, a 070 * {@link ParsingException} is thrown. 071 * 072 * @param token the token type to match 073 * @return the matched token 074 * @throws ParsingException if the current token doesn't match the desired 075 * {@code token} 076 */ 077 public T match(final Predicate<? super T> token) { 078 final var next = LT(1); 079 if (token.test(next)) { 080 consume(); 081 return next; 082 } else { 083 throw new ParsingException(format( 084 "Found unexpected token: %s.", LT(1) 085 )); 086 } 087 } 088 089 /** 090 * Consumes the next token. 091 */ 092 public void consume() { 093 _lookahead.add(_tokenizer.next()); 094 } 095 096}