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

com.shapesecurity.shift.es2018.template.ReduceStructured Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
// Generated by template-structured-reducer.js
/**
 * Copyright 2018 Shape Security, Inc.
 *
 * 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 com.shapesecurity.shift.es2018.template;

import com.shapesecurity.functional.F;
import com.shapesecurity.functional.Pair;
import com.shapesecurity.functional.data.ConcatList;
import com.shapesecurity.functional.data.ImmutableList;
import com.shapesecurity.functional.data.Maybe;
import com.shapesecurity.functional.data.NonEmptyImmutableList;
import com.shapesecurity.shift.es2018.ast.*;
import com.shapesecurity.shift.es2018.reducer.ThunkedReducer;

import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.shapesecurity.shift.es2018.reducer.LazyReconstructingReducer.listMaybeRefEquals;
import static com.shapesecurity.shift.es2018.reducer.LazyReconstructingReducer.listRefEquals;
import static com.shapesecurity.shift.es2018.reducer.LazyReconstructingReducer.maybeRefEquals;


public class ReduceStructured implements ThunkedReducer {
    public static abstract class Label {}
    public static class Bare extends Label {
        final String name;

        public Bare(String name) {
            this.name = name;
        }
    }
    public static class If extends Label {
        final boolean inverted;
        final String condition;

        public If(boolean inverted, String condition) {
            this.inverted = inverted;
            this.condition = condition;
        }
    }
    public static class Loop extends Label {
        final String variable;
        final String values;

        public Loop(String variable, String values) {
            this.variable = variable;
            this.values = values;
        }
    }

    public static class TemplateValues { // TODO relocate to Template
        public final Map conditions;
        public final Map> lists;
        public final Map> replacers;

        public TemplateValues(Map conditions, Map> lists, Map> replacers) {
            this.conditions = conditions;
            this.lists = lists;
            this.replacers = replacers;
        }

        public TemplateValues merge(TemplateValues other, String namespace) {
             // non-destructive
            TemplateValues merged = new TemplateValues(new HashMap<>(this.conditions), new HashMap<>(this.lists), new HashMap<>(this.replacers));
            for (Map.Entry entry : other.conditions.entrySet()) {
                String namespaced = namespace + "::" + entry.getKey();
                if (merged.conditions.containsKey(namespaced)) {
                    throw new RuntimeException("Name " + namespaced + " already exists");
                }
                merged.conditions.put(namespaced, entry.getValue());
            }
            for (Map.Entry> entry : other.lists.entrySet()) {
                String namespaced = namespace + "::" + entry.getKey();
                if (merged.lists.containsKey(namespaced)) {
                    throw new RuntimeException("Name " + namespaced + " already exists");
                }
                merged.lists.put(namespaced, entry.getValue());
            }
            for (Map.Entry> entry : other.replacers.entrySet()) {
                String namespaced = namespace + "::" + entry.getKey();
                if (merged.replacers.containsKey(namespaced)) {
                    throw new RuntimeException("Name " + namespaced + " already exists");
                }
                merged.replacers.put(namespaced, entry.getValue());
            }
            return merged;
        }
    }

    IdentityHashMap> nodeToLabels;
    TemplateValues values;
    boolean currentNodeMayHaveStructuredLabel;

    public ReduceStructured(IdentityHashMap> nodeToLabels, TemplateValues values) {
        this.nodeToLabels = nodeToLabels;
        this.values = values;
        this.currentNodeMayHaveStructuredLabel = false;
    }

    void enforceNoStrayStructuralLabels(Node node) {
        List




© 2015 - 2025 Weber Informatics LLC | Privacy Policy