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

sparksoniq.semantics.StaticContext Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 * Authors: Stefan Irimescu, Can Berker Cikis
 *
 */

package sparksoniq.semantics;

import sparksoniq.exceptions.SemanticException;
import sparksoniq.jsoniq.compiler.translator.metadata.ExpressionMetadata;
import sparksoniq.semantics.types.SequenceType;
import sparksoniq.utils.Tuple;

import java.util.HashMap;
import java.util.Map;

public class StaticContext {

    private Map> _inScopeVariables;
    private StaticContext _parent;

    public StaticContext() {
        this._parent = null;
        this._inScopeVariables = new HashMap<>();
    }

    public StaticContext(StaticContext parent) {
        this._parent = parent;
        this._inScopeVariables = new HashMap<>();
    }

    public StaticContext getParent() {
        return _parent;
    }

    public boolean isInScope(String varName) {
        boolean found = false;
        if (_inScopeVariables.containsKey(varName))
            return true;
        else {
            StaticContext ancestor = _parent;
            while (ancestor != null) {
                found = found || ancestor.getInScopeVariables().containsKey(varName);
                ancestor = ancestor.getParent();
            }
        }
        return found;
    }

    public SequenceType getVariableSequenceType(String varName) {
        if (_inScopeVariables.containsKey(varName))
            return _inScopeVariables.get(varName).getFirst();
        else if (_parent != null)
            return _parent.getVariableSequenceType(varName);
        else
            throw new SemanticException("Variable " + varName + " not in scope", null);
    }

    public void addVariable(String varName, SequenceType type, ExpressionMetadata metadata) {
        this._inScopeVariables.put(varName, new Tuple<>(type, metadata));
    }

    protected Map> getInScopeVariables() {
        return _inScopeVariables;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy