2
* Copyright (C) 2009-2011 Mathias Doenitz
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package org.parboiled.matchervisitors;
19
import org.parboiled.matchers.*;
20
import org.parboiled.support.Characters;
22
import java.util.Random;
25
* Returns the first character a given matcher can start a match with.
26
* For all complex matchers, i.e. the ones not always matching just one character, the visitor returns null.
28
public class GetStarterCharVisitor extends DefaultMatcherVisitor<Character> {
31
public Character visit(AnyMatcher matcher) {
36
public Character visit(AnyOfMatcher matcher) {
37
Characters characters = matcher.characters;
38
if (!characters.isSubtractive()) {
39
return characters.getChars()[0];
42
// for substractive sets we try to randomly choose a fitting character
43
Random random = new Random();
46
c = (char) random.nextInt(Character.MAX_VALUE);
47
} while (!Character.isDefined(c) || !characters.contains(c));
52
public Character visit(CharIgnoreCaseMatcher matcher) {
53
return matcher.charLow;
57
public Character visit(CharMatcher matcher) {
58
return matcher.character;
62
public Character visit(CharRangeMatcher matcher) {
67
public Character visit(CustomMatcher matcher) {
68
return matcher.getStarterChar();