All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.cqse.check.framework.shallowparser.IShallowParser Maven / Gradle / Ivy

Go to download

The Teamscale Custom Check API allows users to extend Teamscale by writing custom analyses that create findings.

There is a newer version: 2024.7.2
Show newest version
/*
 * Copyright (c) CQSE GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.cqse.check.framework.shallowparser;

import java.util.Collections;
import java.util.List;

import org.apache.logging.log4j.LogManager;

import eu.cqse.check.framework.scanner.IToken;
import eu.cqse.check.framework.shallowparser.framework.ShallowEntity;

/**
 * Interface for a shallow parser. All shallow parsers are sufficiently robust to support parsing
 * even of non-compiling code and arbitrary code snippets.
 */
public interface IShallowParser {

	/**
	 * Shallow parses the given list of tokens and returns all entities found. The tokens provided are
	 * expected to represent an entire top-level element of the language (typically a file).
	 * 
	 * @throws ShallowParserException
	 *             if the parser encountered a severe parser error that prevents creating a partial
	 *             parse result.
	 */
	List parseTopLevelWithErrors(List tokens) throws ShallowParserException;

	/**
	 * Shallow parses the given list of tokens and returns all entities found. The tokens provided may
	 * be from an arbitrary contiguous chunk of source code.
	 *
	 * @throws ShallowParserException
	 *             if the parser encountered a severe parser error that prevents creating a partial
	 *             parse result.
	 */
	List parseFragmentWithErrors(List tokens) throws ShallowParserException;

	/**
	 * Same as {@link #parseTopLevelWithErrors(List)} but handles {@link ShallowParserException}s and
	 * guarantees a non-null result.
	 *
	 * In tests use {@link #parseTopLevelWithErrors(List)} to make sure parser errors result in test
	 * failures.
	 */
	default List parseTopLevel(List tokens) {
		try {
			return parseTopLevelWithErrors(tokens);
		} catch (ShallowParserException e) {
			// Parser bugs can lead to stack-overflows. In this case we simply return the
			// parse result at the time of the error.
			LogManager.getLogger().atError().withThrowable(e).log("Error while parsing tokens for origin: {}",
					TokenStreamUtils.determineMostSpecificOrigin(tokens));
			return Collections.emptyList();

		}
	}

	/**
	 * Same as {@link #parseFragmentWithErrors(List)} but handles {@link ShallowParserException}s and
	 * guarantees a non-null result.
	 * 
	 * In tests use {@link #parseFragmentWithErrors(List)} to make sure parser errors result in test
	 * failures.
	 */
	default List parseFragment(List tokens) {
		try {
			return parseFragmentWithErrors(tokens);
		} catch (ShallowParserException e) {
			// Parser bugs can lead to stack-overflows. In this case we simply return the
			// parse result at the time of the error.
			LogManager.getLogger().atError().withThrowable(e).log("Error while parsing tokens for origin: {}",
					TokenStreamUtils.determineMostSpecificOrigin(tokens));
			return Collections.emptyList();

		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy