001/* 002 * Copyright 2020 nils.hoffmann. 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 */ 016package de.isas.lipidomics.palinom; 017 018import de.isas.lipidomics.palinom.exceptions.ParsingException; 019import org.antlr.v4.runtime.Lexer; 020import org.antlr.v4.runtime.Parser; 021 022/** 023 * Base interface for grammar-specific parser implementations based on the 024 * ANTLRv4 generated parsers. 025 * 026 * @author nils.hoffmann 027 * @param <T> the type of the visitor parser 028 */ 029public interface VisitorParser<T> { 030 031 T parse(String lipidString, SyntaxErrorListener listener) throws ParsingException; 032 033 /** 034 * Calls parse with {@link SyntaxErrorListener}. 035 * 036 * @param lipidString 037 * @return the target object of the visitor parser. 038 * @throws ParsingException when syntax are encountered. 039 */ 040 default T parse(String lipidString) throws ParsingException { 041 return parse(lipidString, new SyntaxErrorListener()); 042 } 043 044 /** 045 * Sets up parser and lexer with custom error listener and 046 * {@link GoslinErrorHandler}. 047 * 048 * @param parser the parser to configure 049 * @param lexer the lexer to configure 050 * @param listener the syntax error listener 051 */ 052 default void prepare(Parser parser, Lexer lexer, SyntaxErrorListener listener) { 053 lexer.removeErrorListeners(); 054 lexer.addErrorListener(listener); 055 parser.removeErrorListeners(); 056 parser.addErrorListener(listener); 057 parser.setBuildParseTree(true); 058 parser.setErrorHandler(new GoslinErrorHandler()); 059 } 060 061}