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.support;
018
019import org.parboiled.Node;
020import org.parboiled.buffers.InputBuffer;
021import org.parboiled.errors.ParseError;
022
023import java.util.List;
024
025import static org.parboiled.common.Preconditions.checkArgNotNull;
026
027/**
028 * A simple container encapsulating the result of a parsing run.
029 */
030public class ParsingResult<V> {
031
032    /**
033     * Indicates whether the input was successfully parsed.
034     */
035    public final boolean matched;
036
037    /**
038     * The root node of the parse tree created by the parsing run. This field will only be non-null when
039     * parse-tree-building has been enabled.
040     */
041    public final Node<V> parseTreeRoot;
042
043    /**
044     * The top value of the value stack at the end of the parsing run or null, if the value stack is empty.
045     */
046    public final V resultValue;
047
048    /**
049     * The ValueStack used during the parsing run containing all values not popped of the stack by the parser.
050     */
051    public final ValueStack<V> valueStack;
052
053    /**
054     * The list of parse errors created during the parsing run.
055     */
056    public final List<ParseError> parseErrors;
057
058    /**
059     * The underlying input buffer.
060     */
061    public final InputBuffer inputBuffer;
062
063    /**
064     * Creates a new ParsingResult.
065     *
066     * @param matched       true if the rule matched the input
067     * @param parseTreeRoot the parse tree root node
068     * @param valueStack    the value stack of the parsing run
069     * @param parseErrors   the list of parse errors
070     * @param inputBuffer   the input buffer
071     */
072    public ParsingResult(boolean matched, Node<V> parseTreeRoot, ValueStack<V> valueStack, List<ParseError> parseErrors,
073                         InputBuffer inputBuffer) {
074        this.matched = matched;
075        this.parseTreeRoot = parseTreeRoot;
076        this.valueStack = checkArgNotNull(valueStack, "valueStack");
077        this.resultValue = valueStack.isEmpty() ? null : valueStack.peek();
078        this.parseErrors = checkArgNotNull(parseErrors, "parseErrors");
079        this.inputBuffer = checkArgNotNull(inputBuffer, "inputBuffer");
080    }
081
082    /**
083     * @return true if this parsing result contains parsing errors.
084     */
085    public boolean hasErrors() {
086        return !parseErrors.isEmpty();
087    }
088}