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.errors;
018
019import org.parboiled.buffers.InputBuffer;
020import org.parboiled.common.Preconditions;
021
022import static org.parboiled.common.Preconditions.checkArgNotNull;
023
024/**
025 * A basic {@link ParseError} implementation for a one-char parse error with an optional error message.
026 */
027public class BasicParseError implements ParseError {
028    private final InputBuffer inputBuffer;
029    private final int startIndex;
030    private final String errorMessage;
031    private int endIndex;
032    private int indexDelta;
033
034    public BasicParseError(InputBuffer inputBuffer, int errorIndex, String errorMessage) {
035        this.inputBuffer = Preconditions.checkArgNotNull(inputBuffer, "inputBuffer");
036        this.startIndex = errorIndex;
037        this.endIndex = errorIndex + 1;
038        this.errorMessage = errorMessage;
039    }
040
041    public InputBuffer getInputBuffer() {
042        return inputBuffer;
043    }
044
045    public int getStartIndex() {
046        return startIndex + indexDelta;
047    }
048
049    public int getEndIndex() {
050        return endIndex + indexDelta;
051    }
052
053    public void setEndIndex(int endIndex) {
054        this.endIndex = endIndex - indexDelta;
055    }
056
057    public String getErrorMessage() {
058        return errorMessage;
059    }
060
061    public int getIndexDelta() {
062        return indexDelta;
063    }
064
065    public void shiftIndexDeltaBy(int delta) {
066        this.indexDelta += delta;
067    }
068}