package jnpf.elnFormula;
|
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.List;
|
|
public final class FormulaAst {
|
|
private FormulaAst() {
|
}
|
|
public interface Node {
|
int getStartOffset();
|
|
int getEndOffset();
|
}
|
|
public enum LiteralKind {
|
DECIMAL,
|
TEXT,
|
BOOLEAN,
|
NULL
|
}
|
|
private abstract static class BaseNode implements Node {
|
private final int startOffset;
|
private final int endOffset;
|
|
private BaseNode(int startOffset, int endOffset) {
|
this.startOffset = startOffset;
|
this.endOffset = endOffset;
|
}
|
|
@Override
|
public final int getStartOffset() {
|
return startOffset;
|
}
|
|
@Override
|
public final int getEndOffset() {
|
return endOffset;
|
}
|
}
|
|
public static final class Literal extends BaseNode {
|
private final LiteralKind kind;
|
private final Object value;
|
|
public Literal(int startOffset, int endOffset, LiteralKind kind, Object value) {
|
super(startOffset, endOffset);
|
this.kind = kind;
|
this.value = value;
|
}
|
|
public LiteralKind getKind() {
|
return kind;
|
}
|
|
public Object getValue() {
|
return value;
|
}
|
|
public BigDecimal getDecimalValue() {
|
return (BigDecimal) value;
|
}
|
}
|
|
public static final class Parameter extends BaseNode {
|
private final String code;
|
|
public Parameter(int startOffset, int endOffset, String code) {
|
super(startOffset, endOffset);
|
this.code = code;
|
}
|
|
public String getCode() {
|
return code;
|
}
|
}
|
|
public static final class Unary extends BaseNode {
|
private final String operator;
|
private final Node operand;
|
|
public Unary(int startOffset, int endOffset, String operator, Node operand) {
|
super(startOffset, endOffset);
|
this.operator = operator;
|
this.operand = operand;
|
}
|
|
public String getOperator() {
|
return operator;
|
}
|
|
public Node getOperand() {
|
return operand;
|
}
|
}
|
|
public static final class Binary extends BaseNode {
|
private final String operator;
|
private final Node left;
|
private final Node right;
|
|
public Binary(int startOffset, int endOffset, String operator, Node left, Node right) {
|
super(startOffset, endOffset);
|
this.operator = operator;
|
this.left = left;
|
this.right = right;
|
}
|
|
public String getOperator() {
|
return operator;
|
}
|
|
public Node getLeft() {
|
return left;
|
}
|
|
public Node getRight() {
|
return right;
|
}
|
}
|
|
public static final class FunctionCall extends BaseNode {
|
private final String name;
|
private final List<Node> arguments;
|
|
public FunctionCall(int startOffset, int endOffset, String name, List<Node> arguments) {
|
super(startOffset, endOffset);
|
this.name = name;
|
this.arguments = Collections.unmodifiableList(new ArrayList<>(arguments));
|
}
|
|
public String getName() {
|
return name;
|
}
|
|
public List<Node> getArguments() {
|
return arguments;
|
}
|
}
|
}
|