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

eu.cqse.check.matlab.MatlabActionParser 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.matlab;

import java.util.List;

import org.conqat.lib.commons.collections.Pair;

import eu.cqse.check.framework.scanner.ETokenType;
import eu.cqse.check.framework.scanner.IToken;

/**
 * Wrapper class for the generated {@link MatlabActionParserGenerated} to provide a generic
 * interface that is available in packages without cup dependencies (e.g. in services).
 */
public class MatlabActionParser {

	private final MatlabActionParserGenerated matlabActionParser;

	/**
	 * Parses the tokens and returns a pair of the resulting {@link MatlabActionAstNode}.
	 */
	public static MatlabActionAstNode parse(List tokens) throws MatlabActionParserException {
		// prevent parsing since this is not supported by the parser
		if (tokens.isEmpty() || tokens.stream().allMatch(token -> token.getType() == ETokenType.EOL)) {
			return null;
		}
		MatlabActionParser parser = new MatlabActionParser(tokens);
		return parser.parse();
	}

	/**
	 * Parses the tokens and returns a pair of the resulting {@link MatlabActionAstNode} and the debug
	 * messages.
	 */
	public static Pair debugParse(List tokens) throws MatlabActionParserException {
		MatlabActionParser parser = new MatlabActionParser(tokens);
		return parser.debugParse();
	}

	/**
	 * Constructor. Note that the called constructor is deprecated and should be replaced by the
	 * constructor accepting a {@link java_cup.runtime.SymbolFactory} (see TS-27362).
	 */
	public MatlabActionParser(List tokens) {
		MatlabActionScanner scanner = new MatlabActionScanner(tokens);
		matlabActionParser = new MatlabActionParserGenerated(scanner);
	}

	/** Parses the tokens and returns the resulting {@link MatlabActionAstNode}. */
	public MatlabActionAstNode parse() throws MatlabActionParserException {
		try {
			return (MatlabActionAstNode) matlabActionParser.parse().value;
		} catch (MatlabActionParserException e) {
			// If e is already a MatlabActionParserException, don't wrap it into another
			// one.
			throw e;
		} catch (Exception e) {
			// Intercept any Exception since CUP parsers can throw quite diverse exceptions
			// and we don't want the calling code (checks) to break on these.
			throw new MatlabActionParserException(e);
		}
	}

	/**
	 * Parses the tokens and returns a pair of the resulting {@link MatlabActionAstNode} and the debug
	 * messages.
	 */
	private Pair debugParse() throws MatlabActionParserException {
		try {
			MatlabActionAstNode node = (MatlabActionAstNode) matlabActionParser.debug_parse().value;
			return Pair.createPair(node, matlabActionParser.getDebugMessages());
		} catch (Exception e) {
			// generic catch needed because matlabActionParser.debug_parse() throws an exception and we want to
			// convert this into something more meaningful.
			throw new MatlabActionParserException(e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy