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

ai.vespa.schemals.schemadocument.resolvers.RankExpression.argument.IntegerArgument Maven / Gradle / Ivy

There is a newer version: 8.441.21
Show newest version
package ai.vespa.schemals.schemadocument.resolvers.RankExpression.argument;

import java.util.Optional;

import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;

import ai.vespa.schemals.common.SchemaDiagnostic;
import ai.vespa.schemals.context.ParseContext;
import ai.vespa.schemals.index.Symbol;
import ai.vespa.schemals.index.Symbol.SymbolStatus;
import ai.vespa.schemals.parser.rankingexpression.ast.INTEGER;
import ai.vespa.schemals.tree.Node;
import ai.vespa.schemals.tree.SchemaNode;
import ai.vespa.schemals.tree.rankingexpression.RankNode;

public class IntegerArgument implements Argument {

    private String displayStr;

    public IntegerArgument(String displayStr) {
        this.displayStr = displayStr;
    }

    public IntegerArgument() {
        this("n");
    }

    @Override
    public int getStrictness() {
        return 6;
    }

    @Override
    public boolean validateArgument(RankNode node) {
        Node leaf = node.getSchemaNode().findFirstLeaf();
        return leaf.isASTInstance(INTEGER.class);
    }

    @Override
    public Optional parseArgument(ParseContext context, RankNode argument) {

        Node leaf = argument.getSchemaNode();

        while (leaf.size() > 0) {
            if (leaf.hasSymbol()) {
                Symbol symbol = leaf.getSymbol();
                if (symbol.getStatus() == SymbolStatus.REFERENCE) {
                    context.schemaIndex().deleteSymbolReference(symbol);
                }
                leaf.removeSymbol();
            }
            leaf = leaf.get(0);
        }

        if (!leaf.isASTInstance(INTEGER.class)) {
            return Optional.of(new SchemaDiagnostic.Builder()
                .setRange(leaf.getRange())
                .setMessage("Argument of function must be an INTEGER.")
                .setSeverity(DiagnosticSeverity.Error)
                .build());
        }


        return Optional.empty();
    }

    @Override
    public String displayString() {
        return displayStr;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy