001/*
002 * Copyright (C) 2009-2011 Mathias Doenitz
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.parboiled.matchers;
018
019import org.parboiled.Rule;
020import org.parboiled.matchervisitors.MatcherVisitor;
021
022import static org.parboiled.common.Preconditions.checkArgNotNull;
023
024/**
025 * Base class of custom matcher implementations. If you want to implement custom matchers they have to be derived
026 * from this class. Instances of derived classes can be directly used in rule defining expressions.
027 * Caution: For performance reasons the parsing engine delegates the task of taking and restoring value stack
028 * snapshots to the matchers. If your custom matcher can run parser actions underneath it your custom matcher
029 * implementation therefore has to take care of value stack managment itselves!
030 * (See the implementation of the SequenceMatcher for hints on how to do this!)
031 */
032public abstract class CustomMatcher extends AbstractMatcher {
033
034    protected CustomMatcher(String label) {
035        super(label);
036    }
037
038    protected CustomMatcher(Rule subRule, String label) {
039        super(checkArgNotNull(subRule, "subRule"), label);
040    }
041
042    protected CustomMatcher(Rule[] subRules, String label) {
043        super(checkArgNotNull(subRules, "subRules"), label);
044    }
045
046    /**
047     * Determines whether this matcher instance always matches exactly one character.
048     *
049     * @return true if this matcher always matches exactly one character
050     */
051    public abstract boolean isSingleCharMatcher();
052
053    /**
054     * Determines whether this matcher instance allows empty matches.
055     *
056     * @return true if this matcher instance allows empty matches
057     */
058    public abstract boolean canMatchEmpty();
059
060    /**
061     * Determines whether this matcher instance can start a match with the given char.
062     *
063     * @param c the char
064     * @return true if this matcher instance can start a match with the given char.
065     */
066    public abstract boolean isStarterChar(char c);
067
068    /**
069     * Returns one of possibly several chars that a match can start with.
070     *
071     * @return a starter char
072     */
073    public abstract char getStarterChar();
074
075    public <R> R accept(MatcherVisitor<R> visitor) {
076        checkArgNotNull(visitor, "visitor");
077        return visitor.visit(this);
078    }
079}