com.github.leeonky.interpreter.NodeParser Maven / Gradle / Ivy
package com.github.leeonky.interpreter;
import java.util.function.BiFunction;
import java.util.function.Function;
import static java.util.Optional.ofNullable;
public interface NodeParser, P extends Procedure, N, ?, ?>>
extends Parser, NodeParser.Mandatory, N> {
static , P extends Procedure, N, ?, ?>> NodeParser.Mandatory positionNode(
NodeParser.Mandatory mandatory) {
return procedure -> procedure.positionOf((position, indent) ->
mandatory.parse(procedure).setPositionBegin(position).setIndent(indent));
}
static , P extends Procedure, N, ?, ?>> NodeParser positionNode(
NodeParser mandatory) {
return procedure -> procedure.positionOf((position, indent) -> mandatory.parse(procedure)
.map(node -> node.setPositionBegin(position).setIndent(indent)));
}
static , P extends Procedure, N, ?, ?>> NodeParser.Mandatory columnMandatory(
Function> mandatoryFactory) {
return procedure -> mandatoryFactory.apply(procedure.getColumn()).parse(procedure);
}
static , P extends Procedure, N, ?, ?>> NodeParser columnParser(
Function> parserFactory) {
return procedure -> parserFactory.apply(procedure.getColumn()).parse(procedure);
}
@Override
default NodeParser castParser(Parser,
Mandatory, N> parser) {
return parser::parse;
}
@Override
default Mandatory castMandatory(Parser.Mandatory,
Mandatory, N> mandatory) {
return mandatory::parse;
}
default NodeParser concat(ClauseParser.Mandatory mandatory) {
return procedure -> parse(procedure).map(node -> mandatory.parse(procedure).expression(node));
}
default NodeParser concat(ClauseParser clauseParser) {
return procedure -> parse(procedure).map(node -> clauseParser.parseAndMakeExpressionOrInput(procedure, node));
}
default NodeParser concatAll(ClauseParser clauseParser) {
return procedure -> parse(procedure).map(node ->
clauseParser.parseAndMakeExpressionOrInputContinuously(procedure, node));
}
default NodeParser with(ClauseParser clauseParser) {
return procedure -> procedure.getSourceCode().tryFetch(() -> parse(procedure)
.flatMap(node -> clauseParser.parseAndMakeExpression(procedure, node)));
}
default NodeParser with(ClauseParser.Mandatory mandatory) {
return procedure -> procedure.getSourceCode().tryFetch(() -> parse(procedure)
.flatMap(node -> ofNullable(mandatory.parse(procedure).expression(node))));
}
default ClauseParser clause(BiFunction biFunction) {
return procedure -> parse(procedure).map(n -> input -> biFunction.apply(input, n));
}
interface Mandatory, P extends Procedure, N, ?, ?>> extends
Parser.Mandatory, Mandatory, N> {
@Override
default NodeParser castParser(Parser,
Mandatory, N> parser) {
return parser::parse;
}
@Override
default Mandatory castMandatory(Parser.Mandatory, Mandatory, N> mandatory) {
return mandatory::parse;
}
default Mandatory concat(ClauseParser.Mandatory clauseMandatory) {
return procedure -> {
N node = parse(procedure);
return clauseMandatory.parse(procedure).expression(node);
};
}
default Mandatory concat(ClauseParser clauseParser) {
return procedure -> clauseParser.parseAndMakeExpressionOrInput(procedure, parse(procedure));
}
default Mandatory concatAll(ClauseParser clauseParser) {
return procedure -> clauseParser.parseAndMakeExpressionOrInputContinuously(procedure, parse(procedure));
}
default NodeParser with(ClauseParser clauseParser) {
return procedure -> procedure.getSourceCode().tryFetch(() ->
clauseParser.parseAndMakeExpression(procedure, parse(procedure)));
}
default NodeParser.Mandatory with(ClauseParser.Mandatory mandatory) {
return procedure -> {
N input = parse(procedure);
return mandatory.parse(procedure).expression(input);
};
}
}
}