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

jscl.text.ImplicitFunctionParser Maven / Gradle / Ivy

package jscl.text;

import jscl.math.Generic;
import jscl.math.function.Function;
import jscl.math.function.FunctionsRegistry;
import jscl.math.function.ImplicitFunction;
import jscl.math.operator.matrix.OperatorsRegistry;
import jscl.text.msg.Messages;
import jscl.util.ArrayUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ImplicitFunctionParser implements Parser {
    public static final Parser parser = new ImplicitFunctionParser();

    private ImplicitFunctionParser() {
    }

    public Function parse(@NotNull Parameters p, Generic previousSumElement) throws ParseException {
        int pos0 = p.getPosition().intValue();
        Generic a[];

        final String name = ParserUtils.parseWithRollback(CompoundIdentifier.parser, pos0, previousSumElement, p);
        if (FunctionsRegistry.getInstance().getNames().contains(name) || OperatorsRegistry.getInstance().getNames().contains(name)) {
            p.getPosition().setValue(pos0);
            throw new ParseException(Messages.msg_6, p.getPosition().intValue(), p.getExpression(), name);
        }

        final List subscripts = new ArrayList();
        while (true) {
            try {
                subscripts.add(Subscript.parser.parse(p, previousSumElement));
            } catch (ParseException e) {
                break;
            }
        }

        int b[];
        try {
            b = Derivation.parser.parse(p, previousSumElement);
        } catch (ParseException e) {
            b = new int[0];
        }
        try {
            a = ParameterListParser.parser1.parse(p, previousSumElement);
        } catch (ParseException e) {
            p.getPosition().setValue(pos0);
            throw e;
        }

        int derivations[] = new int[a.length];
        for (int i = 0; i < a.length && i < b.length; i++) {
            derivations[i] = b[i];
        }

        return new ImplicitFunction(name, a, derivations, ArrayUtils.toArray(subscripts, new Generic[subscripts.size()]));
    }
}

class Derivation implements Parser {

    public static final Parser parser = new Derivation();

    private Derivation() {
    }

    public int[] parse(@NotNull Parameters p, Generic previousSumElement) throws ParseException {

        int result[];
        try {
            result = new int[]{PrimeCharacters.parser.parse(p, previousSumElement)};
        } catch (ParseException e) {
            result = SuperscriptList.parser.parse(p, previousSumElement);
        }

        return result;
    }
}

class SuperscriptList implements Parser {

    public static final Parser parser = new SuperscriptList();

    private SuperscriptList() {
    }

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

        ParserUtils.tryToParse(p, pos0, '{');

        final List result = new ArrayList();
        try {
            result.add(IntegerParser.parser.parse(p, previousSumElement));
        } catch (ParseException e) {
            p.getPosition().setValue(pos0);
            throw e;
        }

        while (true) {
            try {
                result.add(CommaAndInteger.parser.parse(p, previousSumElement));
            } catch (ParseException e) {
                break;
            }
        }

        ParserUtils.tryToParse(p, pos0, '}');

        ParserUtils.skipWhitespaces(p);

        return ArrayUtils.toArray(result, new int[result.size()]);
    }
}

class CommaAndInteger implements Parser {

    public static final Parser parser = new CommaAndInteger();

    private CommaAndInteger() {
    }

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

        ParserUtils.skipWhitespaces(p);

        return ParserUtils.parseWithRollback(IntegerParser.parser, pos0, previousSumElement, p);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy