Parser.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-7.1.0).
03  * Copyright (c) 2007-2022 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19  */
20 package io.jenetics.ext.internal.parser;
21 
22 import static java.lang.String.format;
23 import static java.util.Objects.requireNonNull;
24 
25 import java.util.function.Predicate;
26 
27 /**
28  * Parser implementation for parsing a given token sequences.
29  *
30  @param <T> the token type
31  *
32  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
33  @since 7.1
34  @version 7.1
35  */
36 public class Parser<T> {
37 
38     private final Tokenizer<T> _tokenizer;
39     private final TokenRing<T> _lookahead;
40 
41     /**
42      * Create a new parser object with the given {@code tokenizer} and lookahead
43      * count {@code k}.
44      *
45      @param tokenizer the token source {@code this} parser uses
46      @param k the lookahead count
47      */
48     public Parser(final Tokenizer<T> tokenizer, final int k) {
49         _tokenizer = requireNonNull(tokenizer);
50         _lookahead = new TokenRing<>(k);
51         for (int i = 0; i < k; ++i) {
52             consume();
53         }
54     }
55 
56     /**
57      * Return the lookahead token with the given index. The index starts at
58      * {@code 1}. The returned token might be {@code null}.
59      *
60      @param index lookahead index
61      @return the token at the given index
62      */
63     public T LT(final int index) {
64         return _lookahead.LT(index);
65     }
66 
67     /**
68      * Try to <em>match</em> and consume the next token of the given
69      * {@code type}. If the current token is not from the given type, a
70      {@link ParsingException} is thrown.
71      *
72      @param token the token type to match
73      @return the matched token
74      @throws ParsingException if the current token doesn't match the desired
75      *         {@code token}
76      */
77     public T match(final Predicate<? super T> token) {
78         final var next = LT(1);
79         if (token.test(next)) {
80             consume();
81             return next;
82         else {
83             throw new ParsingException(format(
84                 "Found unexpected token: %s.", LT(1)
85             ));
86         }
87     }
88 
89     /**
90      * Consumes the next token.
91      */
92     public void consume() {
93         _lookahead.add(_tokenizer.next());
94     }
95 
96 }