Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jun 18, 2024
2 parents 59e5f63 + 1224321 commit ef8cd9d
Show file tree
Hide file tree
Showing 29 changed files with 1,634 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/cqfn/astranaut/core/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
*
* @since 1.1.0
*/
public interface Action extends DifferenceTreeItem {
public interface Action extends DifferenceTreeItem, PatternItem {
}
7 changes: 6 additions & 1 deletion src/main/java/org/cqfn/astranaut/core/DifferenceNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,13 @@ public boolean deleteNode(final Node node) {
return result;
}

@Override
public String toString() {
return this.prototype.toString();
}

/**
* Transforms children nodes to convertible ones.
* Transforms children nodes to difference ones.
* @return List of difference nodes
*/
private List<DifferenceTreeItem> initChildrenList() {
Expand Down
43 changes: 37 additions & 6 deletions src/main/java/org/cqfn/astranaut/core/DraftNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* Draft node for wrapping the results of third-party parsers
Expand Down Expand Up @@ -121,16 +124,35 @@ public String toString() {
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public static Node createByDescription(final String description) {
return DraftNode.createByDescription(description, null);
}

/**
* Creates a tree from draft nodes based on description.
* Description format: A(B,C(...),...) where 'A' is the type name
* (it consists only of letters) followed by child nodes (in the same format) in parentheses
* separated by commas.
* @param description Description
* @param nodes Collection in which to place the nodes to be created, sorted by type name
* @return Root node of the tree created by description
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public static Node createByDescription(
final String description,
final Map<String, Set<Node>> nodes) {
final CharacterIterator iterator = new StringCharacterIterator(description);
return DraftNode.createByDescription(iterator);
return DraftNode.createByDescription(iterator, nodes);
}

/**
* Creates a tree based on its description (recursive method).
* @param iterator Iterator by description characters
* @param nodes Collection in which to place the nodes to be created, sorted by type name
* @return Node of the tree with its children, created by description
*/
private static Node createByDescription(final CharacterIterator iterator) {
private static Node createByDescription(
final CharacterIterator iterator,
final Map<String, Set<Node>> nodes) {
char symbol = iterator.current();
final Node result;
final StringBuilder name = new StringBuilder();
Expand All @@ -146,9 +168,14 @@ private static Node createByDescription(final CharacterIterator iterator) {
symbol = iterator.current();
}
if (symbol == '(') {
builder.setChildrenList(DraftNode.parseChildrenList(iterator));
builder.setChildrenList(DraftNode.parseChildrenList(iterator, nodes));
}
result = builder.createNode();
if (nodes != null) {
final Set<Node> set =
nodes.computeIfAbsent(result.getTypeName(), k -> new HashSet<>());
set.add(result);
}
} else {
result = null;
}
Expand Down Expand Up @@ -181,21 +208,25 @@ private static String parseData(final CharacterIterator iterator) {
/**
* Parses children list from description.
* @param iterator Iterator by description characters
* @param nodes Collection in which to place the nodes to be created, sorted by type name
* @return Children list, created by description
*/
private static List<Node> parseChildrenList(final CharacterIterator iterator) {
private static List<Node> parseChildrenList(
final CharacterIterator iterator,
final Map<String, Set<Node>> nodes) {
assert iterator.current() == '(';
final List<Node> children = new LinkedList<>();
char next;
do {
iterator.next();
final Node child = DraftNode.createByDescription(iterator);
final Node child = DraftNode.createByDescription(iterator, nodes);
if (child != null) {
children.add(child);
}
next = iterator.current();
assert next == ')' || next == ',' || next == ' ';
} while (next != ')');
iterator.next();
return children;
}

Expand All @@ -212,7 +243,7 @@ private static class TypeImpl implements Type {

/**
* Constructor.
* @param name The type name
* @param name The type name`
*/
TypeImpl(final String name) {
this.name = name;
Expand Down
142 changes: 142 additions & 0 deletions src/main/java/org/cqfn/astranaut/core/Hole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* 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.astranaut.core;

import java.util.List;

/**
* A special pattern node that can substitute for any node of a suitable type.
*
* @since 1.1.5
*/
public final class Hole implements PatternItem {
/**
* The type of the hole.
*/
private final Type type;

/**
* The number of the hole.
*/
private final int number;

/**
* Constructor.
* @param type The type of the hole
* @param number The number of the hole
*/
public Hole(final Type type, final int number) {
this.type = new HoleType(type);
this.number = number;
}

@Override
public Fragment getFragment() {
return EmptyFragment.INSTANCE;
}

@Override
public Type getType() {
return this.type;
}

@Override
public String getData() {
return String.format("#%d", this.number);
}

@Override
public int getChildCount() {
return 0;
}

@Override
public Node getChild(final int index) {
return null;
}

/**
* Return number of the hole.
* @return The number
*/
public int getNumber() {
return this.number;
}

@Override
public String toString() {
return this.getData();
}

/**
* Type implementation for a hole based on a prototype.
*
* @since 1.1.5
*/
private static final class HoleType implements Type {
/**
* Prototype.
*/
private final Type prototype;

/**
* Constructor.
* @param prototype Prototype
*/
private HoleType(final Type prototype) {
this.prototype = prototype;
}

@Override
public String getName() {
return this.prototype.getName();
}

@Override
public List<ChildDescriptor> getChildTypes() {
return this.prototype.getChildTypes();
}

@Override
public List<String> getHierarchy() {
return this.prototype.getHierarchy();
}

@Override
public String getProperty(final String name) {
final String property;
if ("color".equals(name)) {
property = "purple";
} else {
property = this.prototype.getProperty(name);
}
return property;
}

@Override
public Builder createBuilder() {
return this.prototype.createBuilder();
}
}
}
50 changes: 49 additions & 1 deletion src/main/java/org/cqfn/astranaut/core/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
package org.cqfn.astranaut.core;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Consumer;

/**
* An abstract syntax tree node.
*
* @since 1.0
*/
public interface Node {
public interface Node extends Iterable<Node> {
/**
* Returns the fragment associated with the node.
* @return The fragment
Expand Down Expand Up @@ -126,4 +128,50 @@ default boolean deepCompare(Node other) {
}
return equals;
}

@Override
default Iterator<Node> iterator() {
return new NodeIterator(this);
}

/**
* Iterator that enumerates the children of a node.
*
* @since 1.1.5
*/
class NodeIterator implements Iterator<Node> {
/**
* Node.
*/
private final Node node;

/**
* Current index.
*/
private int index;

/**
* Constructor.
* @param node Node whose children should be enumerated.
*/
private NodeIterator(final Node node) {
this.node = node;
this.index = 0;
}

@Override
public boolean hasNext() {
return this.index < this.node.getChildCount();
}

@Override
public Node next() {
if (!this.hasNext()) {
throw new NoSuchElementException();
}
final Node child = this.node.getChild(this.index);
this.index = this.index + 1;
return child;
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/org/cqfn/astranaut/core/PatternItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.astranaut.core;

/**
* An element of a pattern.
*
* @since 1.1.5
*/
public interface PatternItem extends Node {
}
Loading

0 comments on commit ef8cd9d

Please sign in to comment.