Skip to content

Commit

Permalink
+ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Nov 14, 2024
1 parent 1fbe582 commit b292795
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/test/resources/dsl/node_with_one_child.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ StringLiteral <- 'String', '""';
Expression <- IntegerLiteral | StringLiteral;

StatementExpression <- Expression;
Return <- [Expression];
190 changes: 190 additions & 0 deletions src/test/resources/end_to_end/node_with_one_child.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public final class CommonFactory extends DefaultFactory {
.put(IntegerLiteral.NAME, IntegerLiteral.TYPE)
.put(StringLiteral.NAME, StringLiteral.TYPE)
.put(StatementExpression.NAME, StatementExpression.TYPE)
.put(Return.NAME, Return.TYPE)
.make();

/**
Expand Down Expand Up @@ -367,6 +368,195 @@ public final class IntegerLiteral implements Expression {
*/
package org.cqfn.uast.tree.common.nodes;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.cqfn.astranaut.core.algorithms.NodeAllocator;
import org.cqfn.astranaut.core.base.Builder;
import org.cqfn.astranaut.core.base.ChildDescriptor;
import org.cqfn.astranaut.core.base.Fragment;
import org.cqfn.astranaut.core.base.Node;
import org.cqfn.astranaut.core.base.Type;
import org.cqfn.astranaut.core.utils.ListUtils;

/**
* Node of the 'Return' type.
* @since 1.0.0
*/
public final class Return implements Node {
/**
* Name of the type.
*/
public static final String NAME = "Return";

/**
* Type of the node.
*/
public static final Type TYPE = new ReturnType();

/**
* Fragment of source code that is associated with the node.
*/
private Fragment fragment;

/**
* List of child nodes.
*/
private List<Node> children;

@Override
public Fragment getFragment() {
return this.fragment;
}

@Override
public Type getType() {
return Return.TYPE;
}

@Override
public String getData() {
return "";
}

@Override
public int getChildCount() {
return this.children.size();
}

@Override
public Node getChild(final int index) {
return this.children.get(index);
}

@Override
public List<Node> getChildrenList() {
return this.children;
}

/**
* Type implementation describing 'Return' nodes.
* @since 1.0.0
*/
private static final class ReturnType implements Type {
/**
* The 'Expression' type name.
*/
private static final String TYPE_EXPRESSION = "Expression";

/**
* List of child node descriptors.
*/
private static final List<ChildDescriptor> CHILD_TYPES =
ChildDescriptor.create().optional(ReturnType.TYPE_EXPRESSION).build();

/**
* Node hierarchy.
*/
private static final List<String> HIERARCHY = Collections.singletonList(Return.NAME);

@Override
public List<ChildDescriptor> getChildTypes() {
return ReturnType.CHILD_TYPES;
}

@Override
public String getName() {
return Return.NAME;
}

@Override
public List<String> getHierarchy() {
return ReturnType.HIERARCHY;
}

@Override
public Map<String, String> getProperties() {
return CommonFactory.PROPERTIES;
}

@Override
public Builder createBuilder() {
return new Return.Constructor();
}
}

/**
* Constructor (builder) that creates nodes of the 'Return' type.
* @since 1.0.0
*/
public static final class Constructor implements Builder {
/**
* Fragment of source code that is associated with the node.
*/
private Fragment fragment;

/**
* Child node.
*/
private Expression child;

@Override
public void setFragment(final Fragment object) {
this.fragment = object;
}

@Override
public boolean setData(final String value) {
return value.isEmpty();
}

@Override
public boolean setChildrenList(final List<Node> list) {
final NodeAllocator allocator = new NodeAllocator(ReturnType.CHILD_TYPES);
final Node[] nodes = new Node[1];
final boolean result = allocator.allocate(nodes, list);
if (result) {
this.child = (Expression) nodes[0];
}
return result;
}

@Override
public boolean isValid() {
return true;
}

@Override
public Node createNode() {
final Return node = new Return();
node.fragment = this.fragment;
node.children = new ListUtils<Node>().add(this.child).make();
return node;
}
}
}

/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Ivan Kniazkov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cqfn.uast.tree.common.nodes;

import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down

0 comments on commit b292795

Please sign in to comment.