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

io.liftwizard.model.reladomo.operation.compiler.literal.AbstractLiteralVisitor Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2024 Craig Motlin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.liftwizard.model.reladomo.operation.compiler.literal;

import java.util.Objects;

import com.gs.fw.common.mithra.finder.RelatedFinder;
import io.liftwizard.model.reladomo.operation.ReladomoOperationParser.BooleanLiteralContext;
import io.liftwizard.model.reladomo.operation.ReladomoOperationParser.CharacterLiteralContext;
import io.liftwizard.model.reladomo.operation.ReladomoOperationParser.FloatingPointLiteralContext;
import io.liftwizard.model.reladomo.operation.ReladomoOperationParser.IntegerLiteralContext;
import io.liftwizard.model.reladomo.operation.ReladomoOperationParser.ParameterContext;
import io.liftwizard.model.reladomo.operation.ReladomoOperationParser.StringLiteralContext;
import io.liftwizard.model.reladomo.operation.visitor.ReladomoOperationThrowingVisitor;
import org.antlr.v4.runtime.tree.ParseTree;

public abstract class AbstractLiteralVisitor
        extends ReladomoOperationThrowingVisitor
{
    protected final RelatedFinder finder;
    protected final String errorContext;

    protected AbstractLiteralVisitor(RelatedFinder finder, String errorContext)
    {
        this.finder = Objects.requireNonNull(finder);
        this.errorContext = Objects.requireNonNull(errorContext);
    }

    @Override
    public T visitParameter(ParameterContext ctx)
    {
        return this.visitChildren(ctx);
    }

    @Override
    public T visitStringLiteral(StringLiteralContext ctx)
    {
        return this.throwTypeError(ctx);
    }

    @Override
    public T visitBooleanLiteral(BooleanLiteralContext ctx)
    {
        return this.throwTypeError(ctx);
    }

    @Override
    public T visitCharacterLiteral(CharacterLiteralContext ctx)
    {
        return this.throwTypeError(ctx);
    }

    @Override
    public T visitIntegerLiteral(IntegerLiteralContext ctx)
    {
        return this.throwTypeError(ctx);
    }

    @Override
    public T visitFloatingPointLiteral(FloatingPointLiteralContext ctx)
    {
        return this.throwTypeError(ctx);
    }

    protected T throwTypeError(ParseTree ctx)
    {
        String error = "Expected <%s> but found: <%s> in %s".formatted(
                this.getExpectedType(),
                ctx.getText(),
                this.errorContext);
        throw new IllegalArgumentException(error);
    }

    protected abstract String getExpectedType();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy