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

eu.cqse.check.matlab.MatlabActionAstNode 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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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

/**
 * Recursive data structure for building an abstract syntax tree
 */
public class MatlabActionAstNode {

	/** The type of the ASTNode */
	private EMatlabActionEntityType type;

	/**
	 * List of tokens of the ASTNode. It contains all tokens of the children and the token(s) of this
	 * AST level
	 */
	private final List tokens;

	/** A list of children that are also ASTNodes */
	private final List children;

	/**
	 * Constructor for an empty ast node without tokens and children. The tokens/children must be added
	 * later.
	 */
	protected MatlabActionAstNode(EMatlabActionEntityType type) {
		this(type, new ArrayList<>(), new ArrayList<>());
	}

	/** Constructor for an ast node without children. */
	protected MatlabActionAstNode(EMatlabActionEntityType type, List tokens) {
		this(type, tokens, new ArrayList<>());
	}

	/** Constructor for an ast node with children. */
	protected MatlabActionAstNode(EMatlabActionEntityType type, List tokens,
			List children) {
		this.type = type;
		this.tokens = tokens;
		this.children = children;
	}

	/** Creates a recursive string representation of an ASTNode. */
	@Override
	public String toString() {
		String representation = type.getReadableName();
		if (tokens.size() == 1 || type == EMatlabActionEntityType.FUNCTION) {
			representation += "[" + tokens.get(0).getText() + "]";
		}
		if (type == EMatlabActionEntityType.VARIABLE_WITH_TYPE) {
			representation += "[" + tokens.get(0).getText() + ", " + tokens.get(1).getText() + "]";
		}
		if (children.size() == 1) {
			representation += " > " + children.get(0).toString();
		} else if (children.size() > 1) {
			representation += " > ("
					+ children.stream().map(MatlabActionAstNode::toString).collect(Collectors.joining(", ")) + ")";
		}
		return representation;
	}

	/**
	 * Add a single child to the end of the children list of this node. Also adds the tokens of the
	 * child to the end of the token list of this node.
	 */
	protected void addChild(MatlabActionAstNode child) {
		children.add(child);
		tokens.addAll(child.getTokens());
	}

	/**
	 * Add multiple children. Also adds the tokens of the children to the end of the token list of this
	 * node.
	 */
	public void addChildren(List children) {
		for (MatlabActionAstNode child : children) {
			addChild(child);
		}
	}

	/**
	 * Appends all tokens and children of the given node to this node. This effectively clones the given
	 * node.
	 */
	protected void addContentsOf(MatlabActionAstNode otherNode) {
		tokens.addAll(otherNode.getTokens());
		children.addAll(otherNode.getChildren());
	}

	/** Add a single {@link IToken}. */
	public void addToken(IToken token) {
		tokens.add(token);
	}

	/** Add multiple {@link IToken}s. */
	public void addTokens(List tokens) {
		this.tokens.addAll(tokens);
	}

	public List getTokens() {
		return tokens;
	}

	public EMatlabActionEntityType getType() {
		return type;
	}

	public List getChildren() {
		return children;
	}

	public void setType(EMatlabActionEntityType type) {
		this.type = type;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy