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.parserunners; 018 019import org.parboiled.MatchHandler; 020import org.parboiled.MatcherContext; 021import org.parboiled.Rule; 022import org.parboiled.buffers.InputBuffer; 023import org.parboiled.support.ParsingResult; 024import org.parboiled.support.ParsingResult; 025 026import static org.parboiled.common.Preconditions.checkArgNotNull; 027 028 029/** 030 * The most basic of all {@link ParseRunner} implementations. It runs a rule against a given input text and builds a 031 * corresponding {@link ParsingResult} instance. However, it does not report any parse errors nor recover from them. 032 * Instead it simply marks the ParsingResult as "unmatched" if the input is not valid with regard to the rule grammar. 033 * It never causes the parser to perform more than one parsing run and is the fastest way to determine 034 * whether a given input conforms to the rule grammar. 035 */ 036public class BasicParseRunner<V> extends AbstractParseRunner<V> implements MatchHandler { 037 038 /** 039 * Create a new BasicParseRunner instance with the given rule and input text and returns the result of 040 * its {@link #run(String)} method invocation. 041 * 042 * @param rule the parser rule to run 043 * @param input the input text to run on 044 * @return the ParsingResult for the parsing run 045 * @deprecated As of 0.11.0 you should use the "regular" constructor and one of the "run" methods rather than 046 * this static method. This method will be removed in one of the coming releases. 047 */ 048 @Deprecated 049 public static <V> ParsingResult<V> run(Rule rule, String input) { 050 checkArgNotNull(rule, "rule"); 051 checkArgNotNull(input, "input"); 052 return new BasicParseRunner<V>(rule).run(input); 053 } 054 055 /** 056 * Creates a new BasicParseRunner instance for the given rule. 057 * 058 * @param rule the parser rule 059 */ 060 public BasicParseRunner(Rule rule) { 061 super(rule); 062 } 063 064 public ParsingResult<V> run(InputBuffer inputBuffer) { 065 checkArgNotNull(inputBuffer, "inputBuffer"); 066 resetValueStack(); 067 068 MatcherContext<V> rootContext = createRootContext(inputBuffer, this, true); 069 boolean matched = rootContext.runMatcher(); 070 return createParsingResult(matched, rootContext); 071 } 072 073 public boolean match(MatcherContext<?> context) { 074 return context.getMatcher().match(context); 075 } 076}