com.github.leeonky.dal.compiler.DALProcedure Maven / Gradle / Ivy
package com.github.leeonky.dal.compiler;
import com.github.leeonky.dal.ast.node.DALExpression;
import com.github.leeonky.dal.ast.node.DALNode;
import com.github.leeonky.dal.ast.node.GroupExpression;
import com.github.leeonky.dal.ast.opt.DALOperator;
import com.github.leeonky.dal.runtime.RuntimeContextBuilder.DALRuntimeContext;
import com.github.leeonky.interpreter.ExpressionFactory;
import com.github.leeonky.interpreter.NodeParser;
import com.github.leeonky.interpreter.Procedure;
import com.github.leeonky.interpreter.SourceCode;
import java.util.LinkedList;
import java.util.Optional;
import java.util.function.Supplier;
import static com.github.leeonky.dal.compiler.Notations.OPENING_GROUP;
import static com.github.leeonky.dal.compiler.Notations.Operators.NOT_EQUAL;
import static java.util.Collections.singleton;
public class DALProcedure extends Procedure {
private final LinkedList enableAndComma = new LinkedList<>(singleton(true));
private boolean enableSlashProperty = false, enableRelaxProperty = false, enableNumberProperty = false;
public DALProcedure(SourceCode sourceCode, DALRuntimeContext runtimeContext,
ExpressionFactory expressionFactory) {
super(sourceCode, runtimeContext, expressionFactory);
}
public static NodeParser disableCommaAnd(
NodeParser nodeParser) {
return procedure -> procedure.commaAnd(false, () -> nodeParser.parse(procedure));
}
public static NodeParser enableCommaAnd(
NodeParser nodeParser) {
return procedure -> procedure.commaAnd(true, () -> nodeParser.parse(procedure));
}
private Optional commaAnd(boolean b, Supplier> nodeFactory) {
enableAndComma.push(b);
try {
return nodeFactory.get();
} finally {
enableAndComma.pop();
}
}
public boolean isEnableCommaAnd() {
return enableAndComma.getFirst();
}
public boolean isCodeBeginning() {
return getSourceCode().isBeginning();
}
public boolean mayBeUnEqual() {
return getSourceCode().startsWith(NOT_EQUAL);
}
public boolean mayBeOpeningGroup() {
return getSourceCode().startsWith(OPENING_GROUP);
}
public boolean mayBeElementEllipsis() {
return getSourceCode().startsWith("..");
}
public boolean isEnableSlashProperty() {
return enableSlashProperty;
}
public boolean mayBeMetaProperty() {
return getSourceCode().startsWith("::");
}
public static NodeParser enableSlashProperty(
NodeParser nodeParser) {
return procedure -> procedure.enableSlashProperty(() -> nodeParser.parse(procedure));
}
public static NodeParser.Mandatory enableSlashProperty(
NodeParser.Mandatory mandatory) {
return procedure -> procedure.enableSlashProperty(() -> mandatory.parse(procedure));
}
private T enableSlashProperty(Supplier supplier) {
enableSlashProperty = true;
try {
return supplier.get();
} finally {
enableSlashProperty = false;
}
}
public boolean isEnableRelaxProperty() {
return enableRelaxProperty;
}
public static NodeParser.Mandatory enableNumberProperty(
NodeParser.Mandatory mandatory) {
return procedure -> procedure.enableNumberProperty(() -> mandatory.parse(procedure));
}
private T enableNumberProperty(Supplier supplier) {
enableNumberProperty = true;
try {
return supplier.get();
} finally {
enableNumberProperty = false;
}
}
public boolean isEnableNumberProperty() {
return enableNumberProperty;
}
public static NodeParser enableRelaxProperty(
NodeParser nodeParser) {
return procedure -> procedure.enableRelaxProperty(() -> nodeParser.parse(procedure));
}
public static NodeParser.Mandatory enableRelaxProperty(
NodeParser.Mandatory mandatory) {
return procedure -> procedure.enableRelaxProperty(() -> mandatory.parse(procedure));
}
private T enableRelaxProperty(Supplier supplier) {
enableRelaxProperty = true;
try {
return supplier.get();
} finally {
enableRelaxProperty = false;
}
}
@Override
public DALNode createExpression(DALNode left, DALOperator operator, DALNode right) {
if (left instanceof GroupExpression)
return ((GroupExpression) left).append(operator, right, this);
if (right instanceof GroupExpression)
return ((GroupExpression) right).insert(left, operator, this);
return super.createExpression(left, operator, right);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy