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

com.github.leeonky.interpreter.ClauseParser Maven / Gradle / Ivy

The newest version!
package com.github.leeonky.interpreter;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public interface ClauseParser, P extends Procedure>
        extends Parser, ClauseParser.Mandatory, Clause> {

    static , P extends Procedure> ClauseParser positionClause(
            ClauseParser clauseParser) {
        return procedure -> procedure.positionOf((position, indent) -> clauseParser.parse(procedure)
                .map(clause -> node -> clause.expression(node).setPositionBegin(position).setIndent(indent)));
    }

    static , P extends Procedure> ClauseParser.Mandatory positionClause(
            ClauseParser.Mandatory clauseMandatory) {
        return procedure -> procedure.positionOf((position, indent) -> {
            Clause parse = clauseMandatory.parse(procedure);
            return node -> parse.expression(node).setPositionBegin(position).setIndent(indent);
        });
    }

    static , P extends Procedure> ClauseParser.Mandatory columnMandatory(
            Function> mandatoryFactory) {
        return procedure -> mandatoryFactory.apply(procedure.getColumn()).parse(procedure);
    }

    static , P extends Procedure> ClauseParser columnParser(
            Function> parserFactory) {
        return procedure -> parserFactory.apply(procedure.getColumn()).parse(procedure);
    }

    @Override
    default ClauseParser castParser(Parser,
            Mandatory, Clause> parser) {
        return parser::parse;
    }

    @Override
    default Mandatory castMandatory(Parser.Mandatory, Mandatory,
            Clause> mandatory) {
        return mandatory::parse;
    }

    default ClauseParser concat(ClauseParser clause) {
        return procedure -> parse(procedure).map(c1 -> clause.parse(procedure).>map(c2 -> previous ->
                c2.expression(c1.expression(previous))).orElse(c1));
    }

    default Optional parseAndMakeExpression(P procedure, N node) {
        return parse(procedure).map(clause -> clause.expression(node));
    }

    default N parseAndMakeExpressionOrInput(P procedure, N input) {
        return parseAndMakeExpression(procedure, input).orElse(input);
    }

    default N parseAndMakeExpressionOrInputContinuously(P procedure, N node) {
        N expression = parseAndMakeExpressionOrInput(procedure, node);
        if (expression == node)
            return expression;
        return parseAndMakeExpressionOrInputContinuously(procedure, expression);
    }

    default ClauseParser concatAll(ClauseParser clauseParser) {
        return procedure -> {
            List> clauses = new ArrayList<>();
            for (Optional> optionalClause = parse(procedure);
                 optionalClause.isPresent();
                 optionalClause = clauseParser.parse(procedure))
                clauses.add(optionalClause.get());
            return clauses.stream().reduce(Clause::merge);
        };
    }

    interface Mandatory, P extends Procedure> extends
            Parser.Mandatory, Mandatory, Clause> {

        static , P extends Procedure> Mandatory clause(
                Function> mandatoryFactory) {
            return procedure -> input -> mandatoryFactory.apply(input).parse(procedure);
        }

        @Override
        default ClauseParser castParser(Parser,
                Mandatory, Clause> parser) {
            return parser::parse;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy