com.github.leeonky.interpreter.Rules Maven / Gradle / Ivy
package com.github.leeonky.interpreter;
import com.github.leeonky.interpreter.Syntax.CompositeSyntax;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import static com.github.leeonky.util.function.When.when;
import static java.lang.String.format;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
public class Rules {
public static , P extends Procedure, N, ?, ?>, PA extends Parser,
MA extends Parser.Mandatory
, T, R, A> Function,
Syntax> endWith(Predicate isClose, Supplier message) {
return syntax -> new CompositeSyntax(syntax) {
private boolean closed;
@Override
protected void close(P procedure) {
if (!closed)
throw procedure.getSourceCode().syntaxError(message.get(), 0);
}
@Override
protected boolean isClose(P procedure) {
return closed = isClose.test(procedure);
}
};
}
public static , P extends Procedure, N, ?, ?>, PA extends Parser,
MA extends Parser.Mandatory
, T, R, A> Function,
Syntax> endWith(Notation, N, ?, P, ?> notation) {
return syntax -> new EndWith<>(syntax, notation);
}
@SafeVarargs
public static , P extends Procedure, N, ?, ?>, PA extends Parser,
MA extends Parser.Mandatory
, T, R, A> Function,
Syntax> endBefore(Notation, N, ?, P, ?>... notations) {
return syntax -> new EndBefore<>(syntax, notations);
}
public static , O extends Operator, P extends Procedure,
E extends Expression,
PA extends Parser, MA extends Parser.Mandatory
, T, R, A>
Function, Syntax> endWith(String closing) {
return syntax -> new CompositeSyntax(
syntax.and(Rules.endWith(Notation.notation(closing)))) {
@Override
public boolean isClose(P procedure) {
return !procedure.getSourceCode().hasCode() || procedure.getSourceCode().startsWith(closing);
}
};
}
public static , O extends Operator,
P extends Procedure, E extends Expression,
PA extends Parser, MA extends Parser.Mandatory