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

com.shapesecurity.shift.es2017.reducer.MonoidalReducer Maven / Gradle / Ivy

// Generated by reducer-monoidal.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.es2017.reducer;

import com.shapesecurity.functional.data.*;
import com.shapesecurity.shift.es2017.ast.*;

import javax.annotation.Nonnull;

public class MonoidalReducer implements Reducer {
    @Nonnull
    protected final Monoid monoidClass;

    public MonoidalReducer(@Nonnull Monoid monoidClass) {
        this.monoidClass = monoidClass;
    }

    protected State identity() {
        return this.monoidClass.identity();
    }

    protected State append(State a, State b) {
        return this.monoidClass.append(a, b);
    }

    protected State append(State a, State b, State c) {
        return append(append(a, b), c);
    }

    protected State append(State a, State b, State c, State d) {
        return append(append(a, b, c), d);
    }

    protected State fold(ImmutableList as) {
        return as.foldLeft(this::append, this.identity());
    }

    protected State fold1(ImmutableList as, State a) {
        return as.foldLeft(this::append, a);
    }

    @Nonnull
    protected State o(@Nonnull Maybe s) {
        return s.orJust(this.identity());
    }

    @Nonnull
    @Override
    public State reduceArrayAssignmentTarget(
            @Nonnull ArrayAssignmentTarget node,
            @Nonnull ImmutableList> elements,
            @Nonnull Maybe rest) {
        return append(fold(Maybe.catMaybes(elements)), o(rest));
    }

    @Nonnull
    @Override
    public State reduceArrayBinding(
            @Nonnull ArrayBinding node,
            @Nonnull ImmutableList> elements,
            @Nonnull Maybe rest) {
        return append(fold(Maybe.catMaybes(elements)), o(rest));
    }

    @Nonnull
    @Override
    public State reduceArrayExpression(
            @Nonnull ArrayExpression node,
            @Nonnull ImmutableList> elements) {
        return fold(Maybe.catMaybes(elements));
    }

    @Nonnull
    @Override
    public State reduceArrowExpression(
            @Nonnull ArrowExpression node,
            @Nonnull State params,
            @Nonnull State body) {
        return append(params, body);
    }

    @Nonnull
    @Override
    public State reduceAssignmentExpression(
            @Nonnull AssignmentExpression node,
            @Nonnull State binding,
            @Nonnull State expression) {
        return append(binding, expression);
    }

    @Nonnull
    @Override
    public State reduceAssignmentTargetIdentifier(@Nonnull AssignmentTargetIdentifier node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceAssignmentTargetPropertyIdentifier(
            @Nonnull AssignmentTargetPropertyIdentifier node,
            @Nonnull State binding,
            @Nonnull Maybe init) {
        return append(binding, o(init));
    }

    @Nonnull
    @Override
    public State reduceAssignmentTargetPropertyProperty(
            @Nonnull AssignmentTargetPropertyProperty node,
            @Nonnull State name,
            @Nonnull State binding) {
        return append(name, binding);
    }

    @Nonnull
    @Override
    public State reduceAssignmentTargetWithDefault(
            @Nonnull AssignmentTargetWithDefault node,
            @Nonnull State binding,
            @Nonnull State init) {
        return append(binding, init);
    }

    @Nonnull
    @Override
    public State reduceAwaitExpression(
            @Nonnull AwaitExpression node,
            @Nonnull State expression) {
        return expression;
    }

    @Nonnull
    @Override
    public State reduceBinaryExpression(
            @Nonnull BinaryExpression node,
            @Nonnull State left,
            @Nonnull State right) {
        return append(left, right);
    }

    @Nonnull
    @Override
    public State reduceBindingIdentifier(@Nonnull BindingIdentifier node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceBindingPropertyIdentifier(
            @Nonnull BindingPropertyIdentifier node,
            @Nonnull State binding,
            @Nonnull Maybe init) {
        return append(binding, o(init));
    }

    @Nonnull
    @Override
    public State reduceBindingPropertyProperty(
            @Nonnull BindingPropertyProperty node,
            @Nonnull State name,
            @Nonnull State binding) {
        return append(name, binding);
    }

    @Nonnull
    @Override
    public State reduceBindingWithDefault(
            @Nonnull BindingWithDefault node,
            @Nonnull State binding,
            @Nonnull State init) {
        return append(binding, init);
    }

    @Nonnull
    @Override
    public State reduceBlock(
            @Nonnull Block node,
            @Nonnull ImmutableList statements) {
        return fold(statements);
    }

    @Nonnull
    @Override
    public State reduceBlockStatement(
            @Nonnull BlockStatement node,
            @Nonnull State block) {
        return block;
    }

    @Nonnull
    @Override
    public State reduceBreakStatement(@Nonnull BreakStatement node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceCallExpression(
            @Nonnull CallExpression node,
            @Nonnull State callee,
            @Nonnull ImmutableList arguments) {
        return fold1(arguments, callee);
    }

    @Nonnull
    @Override
    public State reduceCatchClause(
            @Nonnull CatchClause node,
            @Nonnull State binding,
            @Nonnull State body) {
        return append(binding, body);
    }

    @Nonnull
    @Override
    public State reduceClassDeclaration(
            @Nonnull ClassDeclaration node,
            @Nonnull State name,
            @Nonnull Maybe _super,
            @Nonnull ImmutableList elements) {
        return append(name, o(_super), fold(elements));
    }

    @Nonnull
    @Override
    public State reduceClassElement(
            @Nonnull ClassElement node,
            @Nonnull State method) {
        return method;
    }

    @Nonnull
    @Override
    public State reduceClassExpression(
            @Nonnull ClassExpression node,
            @Nonnull Maybe name,
            @Nonnull Maybe _super,
            @Nonnull ImmutableList elements) {
        return append(o(name), o(_super), fold(elements));
    }

    @Nonnull
    @Override
    public State reduceCompoundAssignmentExpression(
            @Nonnull CompoundAssignmentExpression node,
            @Nonnull State binding,
            @Nonnull State expression) {
        return append(binding, expression);
    }

    @Nonnull
    @Override
    public State reduceComputedMemberAssignmentTarget(
            @Nonnull ComputedMemberAssignmentTarget node,
            @Nonnull State object,
            @Nonnull State expression) {
        return append(object, expression);
    }

    @Nonnull
    @Override
    public State reduceComputedMemberExpression(
            @Nonnull ComputedMemberExpression node,
            @Nonnull State object,
            @Nonnull State expression) {
        return append(object, expression);
    }

    @Nonnull
    @Override
    public State reduceComputedPropertyName(
            @Nonnull ComputedPropertyName node,
            @Nonnull State expression) {
        return expression;
    }

    @Nonnull
    @Override
    public State reduceConditionalExpression(
            @Nonnull ConditionalExpression node,
            @Nonnull State test,
            @Nonnull State consequent,
            @Nonnull State alternate) {
        return append(test, consequent, alternate);
    }

    @Nonnull
    @Override
    public State reduceContinueStatement(@Nonnull ContinueStatement node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceDataProperty(
            @Nonnull DataProperty node,
            @Nonnull State name,
            @Nonnull State expression) {
        return append(name, expression);
    }

    @Nonnull
    @Override
    public State reduceDebuggerStatement(@Nonnull DebuggerStatement node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceDirective(@Nonnull Directive node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceDoWhileStatement(
            @Nonnull DoWhileStatement node,
            @Nonnull State body,
            @Nonnull State test) {
        return append(body, test);
    }

    @Nonnull
    @Override
    public State reduceEmptyStatement(@Nonnull EmptyStatement node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceExport(
            @Nonnull Export node,
            @Nonnull State declaration) {
        return declaration;
    }

    @Nonnull
    @Override
    public State reduceExportAllFrom(@Nonnull ExportAllFrom node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceExportDefault(
            @Nonnull ExportDefault node,
            @Nonnull State body) {
        return body;
    }

    @Nonnull
    @Override
    public State reduceExportFrom(
            @Nonnull ExportFrom node,
            @Nonnull ImmutableList namedExports) {
        return fold(namedExports);
    }

    @Nonnull
    @Override
    public State reduceExportFromSpecifier(@Nonnull ExportFromSpecifier node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceExportLocalSpecifier(
            @Nonnull ExportLocalSpecifier node,
            @Nonnull State name) {
        return name;
    }

    @Nonnull
    @Override
    public State reduceExportLocals(
            @Nonnull ExportLocals node,
            @Nonnull ImmutableList namedExports) {
        return fold(namedExports);
    }

    @Nonnull
    @Override
    public State reduceExpressionStatement(
            @Nonnull ExpressionStatement node,
            @Nonnull State expression) {
        return expression;
    }

    @Nonnull
    @Override
    public State reduceForInStatement(
            @Nonnull ForInStatement node,
            @Nonnull State left,
            @Nonnull State right,
            @Nonnull State body) {
        return append(left, right, body);
    }

    @Nonnull
    @Override
    public State reduceForOfStatement(
            @Nonnull ForOfStatement node,
            @Nonnull State left,
            @Nonnull State right,
            @Nonnull State body) {
        return append(left, right, body);
    }

    @Nonnull
    @Override
    public State reduceForStatement(
            @Nonnull ForStatement node,
            @Nonnull Maybe init,
            @Nonnull Maybe test,
            @Nonnull Maybe update,
            @Nonnull State body) {
        return append(o(init), o(test), o(update), body);
    }

    @Nonnull
    @Override
    public State reduceFormalParameters(
            @Nonnull FormalParameters node,
            @Nonnull ImmutableList items,
            @Nonnull Maybe rest) {
        return append(fold(items), o(rest));
    }

    @Nonnull
    @Override
    public State reduceFunctionBody(
            @Nonnull FunctionBody node,
            @Nonnull ImmutableList directives,
            @Nonnull ImmutableList statements) {
        return append(fold(directives), fold(statements));
    }

    @Nonnull
    @Override
    public State reduceFunctionDeclaration(
            @Nonnull FunctionDeclaration node,
            @Nonnull State name,
            @Nonnull State params,
            @Nonnull State body) {
        return append(name, params, body);
    }

    @Nonnull
    @Override
    public State reduceFunctionExpression(
            @Nonnull FunctionExpression node,
            @Nonnull Maybe name,
            @Nonnull State params,
            @Nonnull State body) {
        return append(o(name), params, body);
    }

    @Nonnull
    @Override
    public State reduceGetter(
            @Nonnull Getter node,
            @Nonnull State name,
            @Nonnull State body) {
        return append(name, body);
    }

    @Nonnull
    @Override
    public State reduceIdentifierExpression(@Nonnull IdentifierExpression node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceIfStatement(
            @Nonnull IfStatement node,
            @Nonnull State test,
            @Nonnull State consequent,
            @Nonnull Maybe alternate) {
        return append(test, consequent, o(alternate));
    }

    @Nonnull
    @Override
    public State reduceImport(
            @Nonnull Import node,
            @Nonnull Maybe defaultBinding,
            @Nonnull ImmutableList namedImports) {
        return fold1(namedImports, o(defaultBinding));
    }

    @Nonnull
    @Override
    public State reduceImportNamespace(
            @Nonnull ImportNamespace node,
            @Nonnull Maybe defaultBinding,
            @Nonnull State namespaceBinding) {
        return append(o(defaultBinding), namespaceBinding);
    }

    @Nonnull
    @Override
    public State reduceImportSpecifier(
            @Nonnull ImportSpecifier node,
            @Nonnull State binding) {
        return binding;
    }

    @Nonnull
    @Override
    public State reduceLabeledStatement(
            @Nonnull LabeledStatement node,
            @Nonnull State body) {
        return body;
    }

    @Nonnull
    @Override
    public State reduceLiteralBooleanExpression(@Nonnull LiteralBooleanExpression node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceLiteralInfinityExpression(@Nonnull LiteralInfinityExpression node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceLiteralNullExpression(@Nonnull LiteralNullExpression node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceLiteralNumericExpression(@Nonnull LiteralNumericExpression node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceLiteralRegExpExpression(@Nonnull LiteralRegExpExpression node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceLiteralStringExpression(@Nonnull LiteralStringExpression node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceMethod(
            @Nonnull Method node,
            @Nonnull State name,
            @Nonnull State params,
            @Nonnull State body) {
        return append(name, params, body);
    }

    @Nonnull
    @Override
    public State reduceModule(
            @Nonnull Module node,
            @Nonnull ImmutableList directives,
            @Nonnull ImmutableList items) {
        return append(fold(directives), fold(items));
    }

    @Nonnull
    @Override
    public State reduceNewExpression(
            @Nonnull NewExpression node,
            @Nonnull State callee,
            @Nonnull ImmutableList arguments) {
        return fold1(arguments, callee);
    }

    @Nonnull
    @Override
    public State reduceNewTargetExpression(@Nonnull NewTargetExpression node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceObjectAssignmentTarget(
            @Nonnull ObjectAssignmentTarget node,
            @Nonnull ImmutableList properties) {
        return fold(properties);
    }

    @Nonnull
    @Override
    public State reduceObjectBinding(
            @Nonnull ObjectBinding node,
            @Nonnull ImmutableList properties) {
        return fold(properties);
    }

    @Nonnull
    @Override
    public State reduceObjectExpression(
            @Nonnull ObjectExpression node,
            @Nonnull ImmutableList properties) {
        return fold(properties);
    }

    @Nonnull
    @Override
    public State reduceReturnStatement(
            @Nonnull ReturnStatement node,
            @Nonnull Maybe expression) {
        return o(expression);
    }

    @Nonnull
    @Override
    public State reduceScript(
            @Nonnull Script node,
            @Nonnull ImmutableList directives,
            @Nonnull ImmutableList statements) {
        return append(fold(directives), fold(statements));
    }

    @Nonnull
    @Override
    public State reduceSetter(
            @Nonnull Setter node,
            @Nonnull State name,
            @Nonnull State param,
            @Nonnull State body) {
        return append(name, param, body);
    }

    @Nonnull
    @Override
    public State reduceShorthandProperty(
            @Nonnull ShorthandProperty node,
            @Nonnull State name) {
        return name;
    }

    @Nonnull
    @Override
    public State reduceSpreadElement(
            @Nonnull SpreadElement node,
            @Nonnull State expression) {
        return expression;
    }

    @Nonnull
    @Override
    public State reduceStaticMemberAssignmentTarget(
            @Nonnull StaticMemberAssignmentTarget node,
            @Nonnull State object) {
        return object;
    }

    @Nonnull
    @Override
    public State reduceStaticMemberExpression(
            @Nonnull StaticMemberExpression node,
            @Nonnull State object) {
        return object;
    }

    @Nonnull
    @Override
    public State reduceStaticPropertyName(@Nonnull StaticPropertyName node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceSuper(@Nonnull Super node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceSwitchCase(
            @Nonnull SwitchCase node,
            @Nonnull State test,
            @Nonnull ImmutableList consequent) {
        return fold1(consequent, test);
    }

    @Nonnull
    @Override
    public State reduceSwitchDefault(
            @Nonnull SwitchDefault node,
            @Nonnull ImmutableList consequent) {
        return fold(consequent);
    }

    @Nonnull
    @Override
    public State reduceSwitchStatement(
            @Nonnull SwitchStatement node,
            @Nonnull State discriminant,
            @Nonnull ImmutableList cases) {
        return fold1(cases, discriminant);
    }

    @Nonnull
    @Override
    public State reduceSwitchStatementWithDefault(
            @Nonnull SwitchStatementWithDefault node,
            @Nonnull State discriminant,
            @Nonnull ImmutableList preDefaultCases,
            @Nonnull State defaultCase,
            @Nonnull ImmutableList postDefaultCases) {
        return append(discriminant, fold(preDefaultCases), defaultCase, fold(postDefaultCases));
    }

    @Nonnull
    @Override
    public State reduceTemplateElement(@Nonnull TemplateElement node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceTemplateExpression(
            @Nonnull TemplateExpression node,
            @Nonnull Maybe tag,
            @Nonnull ImmutableList elements) {
        return fold1(elements, o(tag));
    }

    @Nonnull
    @Override
    public State reduceThisExpression(@Nonnull ThisExpression node) {
        return this.identity();
    }

    @Nonnull
    @Override
    public State reduceThrowStatement(
            @Nonnull ThrowStatement node,
            @Nonnull State expression) {
        return expression;
    }

    @Nonnull
    @Override
    public State reduceTryCatchStatement(
            @Nonnull TryCatchStatement node,
            @Nonnull State body,
            @Nonnull State catchClause) {
        return append(body, catchClause);
    }

    @Nonnull
    @Override
    public State reduceTryFinallyStatement(
            @Nonnull TryFinallyStatement node,
            @Nonnull State body,
            @Nonnull Maybe catchClause,
            @Nonnull State finalizer) {
        return append(body, o(catchClause), finalizer);
    }

    @Nonnull
    @Override
    public State reduceUnaryExpression(
            @Nonnull UnaryExpression node,
            @Nonnull State operand) {
        return operand;
    }

    @Nonnull
    @Override
    public State reduceUpdateExpression(
            @Nonnull UpdateExpression node,
            @Nonnull State operand) {
        return operand;
    }

    @Nonnull
    @Override
    public State reduceVariableDeclaration(
            @Nonnull VariableDeclaration node,
            @Nonnull ImmutableList declarators) {
        return fold(declarators);
    }

    @Nonnull
    @Override
    public State reduceVariableDeclarationStatement(
            @Nonnull VariableDeclarationStatement node,
            @Nonnull State declaration) {
        return declaration;
    }

    @Nonnull
    @Override
    public State reduceVariableDeclarator(
            @Nonnull VariableDeclarator node,
            @Nonnull State binding,
            @Nonnull Maybe init) {
        return append(binding, o(init));
    }

    @Nonnull
    @Override
    public State reduceWhileStatement(
            @Nonnull WhileStatement node,
            @Nonnull State test,
            @Nonnull State body) {
        return append(test, body);
    }

    @Nonnull
    @Override
    public State reduceWithStatement(
            @Nonnull WithStatement node,
            @Nonnull State object,
            @Nonnull State body) {
        return append(object, body);
    }

    @Nonnull
    @Override
    public State reduceYieldExpression(
            @Nonnull YieldExpression node,
            @Nonnull Maybe expression) {
        return o(expression);
    }

    @Nonnull
    @Override
    public State reduceYieldGeneratorExpression(
            @Nonnull YieldGeneratorExpression node,
            @Nonnull State expression) {
        return expression;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy