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

jscl.text.OperatorParser Maven / Gradle / Ivy

There is a newer version: 1.0.11
Show newest version
package jscl.text;

import jscl.math.Generic;
import jscl.math.operator.Operator;
import jscl.math.operator.matrix.OperatorsRegistry;
import jscl.text.msg.Messages;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class OperatorParser implements Parser {

	public static final Parser parser = new OperatorParser();

	private OperatorParser() {
	}

	@Nonnull
	public Operator parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException {
		int pos0 = p.getPosition().intValue();

		final String operatorName = Identifier.parser.parse(p, previousSumElement);
		if (!valid(operatorName)) {
			ParserUtils.throwParseException(p, pos0, Messages.msg_3, operatorName);
		}

		final Operator operator = OperatorsRegistry.getInstance().get(operatorName);

		Operator result = null;
		if (operator != null) {
			final Generic parameters[] = ParserUtils.parseWithRollback(new ParameterListParser(operator.getMinParameters()), pos0, previousSumElement, p);

			result = OperatorsRegistry.getInstance().get(operatorName, parameters);
			if (result == null) {
				ParserUtils.throwParseException(p, pos0, Messages.msg_2, operatorName);
			}
		} else {
			ParserUtils.throwParseException(p, pos0, Messages.msg_3, operatorName);
		}

		assert result != null;
		return result;
	}

	static boolean valid(@Nullable String name) {
		return name != null && OperatorsRegistry.getInstance().getNames().contains(name);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy