01 /*
02 * Java Genetic Algorithm Library (jenetics-7.1.1).
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.Character.isWhitespace;
23 import static java.lang.String.format;
24 import static java.util.Objects.requireNonNull;
25
26 /**
27 * Base class for all tokenizers.
28 *
29 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
30 * @since 7.1
31 * @version 7.1
32 */
33 public abstract class CharSequenceTokenizer implements Tokenizer<Token<String>> {
34
35 private static final char EOF = (char)-1;
36
37 private final CharSequence _input;
38
39 protected int pos = 0;
40 protected char c = EOF;
41
42 /**
43 * Create a new tokenizer from the given character sequence.
44 *
45 * @param input the input character sequence
46 * @throws NullPointerException if the given {@code input} is {@code null}
47 */
48 protected CharSequenceTokenizer(final CharSequence input) {
49 requireNonNull(input);
50
51 if (input.length() > 0) {
52 c = input.charAt(0);
53 }
54 _input = input;
55 }
56
57 protected void match(final char ch) {
58 if (ch == c) {
59 consume();
60 } else {
61 throw new ParsingException(format(
62 "Got invalid character '%s' at position '%d'; expected '%s'",
63 c, pos, ch
64 ));
65 }
66 }
67
68 protected void consume() {
69 if (pos + 1 >= _input.length()) {
70 c = EOF;
71 } else {
72 c = _input.charAt(++pos);
73 }
74 }
75
76 protected char LA(final int index) {
77 final int i = pos + index - 1;
78 return i < _input.length() && i >= 0 ? _input.charAt(i) : EOF;
79 }
80
81 public static boolean isNonEof(final char ch) {
82 return ch != EOF;
83 }
84
85 public static boolean isAlphabetic(final char c) {
86 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
87 }
88
89 protected void WS() {
90 do {
91 consume();
92 } while (isNonEof(c) && isWhitespace(c));
93 }
94
95 }
|