Skip to content

Commit

Permalink
feat(#316): fix all the code offences
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jun 20, 2024
1 parent 782ac63 commit 4b3e8e8
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 54 deletions.
50 changes: 49 additions & 1 deletion src/main/java/org/eolang/opeo/ast/Arguments.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* 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.eolang.opeo.ast;

import java.util.Collections;
Expand All @@ -6,18 +29,44 @@
import org.eolang.jeo.representation.xmir.XmlNode;
import org.eolang.opeo.compilation.Parser;

/**
* Arguments of an invocation.
* @since 0.2
*/
public final class Arguments {

/**
* Root node.
*/
private final XmlNode root;

/**
* Parser that understands how to parse subnodes.
*/
private final Parser parser;

/**
* Begin index.
* We start to parse arguments from this index.
*/
private final int begin;

/**
* Constructor.
* @param root Root node.
* @param parser Parser that understands how to parse subnodes.
* @param begin Begin index.
*/
public Arguments(final XmlNode root, final Parser parser, final int begin) {
this.root = root;
this.parser = parser;
this.begin = begin;
}

/**
* Convert to list.
* @return List of arguments.
*/
public List<AstNode> toList() {
final List<XmlNode> all = this.root.children().collect(Collectors.toList());
final List<AstNode> arguments;
Expand All @@ -31,5 +80,4 @@ public List<AstNode> toList() {
}
return arguments;
}

}
1 change: 1 addition & 0 deletions src/main/java/org/eolang/opeo/ast/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ private static Map<String, String> fromXmir(final XmlNode node) {
/**
* Parse attributes from Xmir.
* @param node Xmir node with attributes.
* @param fallback Use this attributes if there are no attributes in the node.
* @return Map of attributes.
*/
private static Map<String, String> fromXmir(final XmlNode node, final Attributes fallback) {
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/org/eolang/opeo/ast/Constructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public Constructor(
/**
* Constructor.
* @param node Xmir representation of constructor.
* @param parser Parser that understands how to parse subnodes.
*/
public Constructor(final XmlNode node, final Parser parser) {
this(
Expand All @@ -120,16 +121,6 @@ public Constructor(final XmlNode node, final Parser parser) {
);
}

/**
* Get target node.
* @param node Constructor node.
* @param parser Parser, which can extract AstNode from XmlNode.
* @return Target node.
*/
private static AstNode xtarget(final XmlNode node, final Parser parser) {
return parser.parse(node.children().collect(Collectors.toList()).get(0));
}

/**
* Constructor.
* @param ctype Constructor type
Expand Down Expand Up @@ -226,4 +217,14 @@ private static Attributes xattrs(final XmlNode node, final Parser parser) {
);
return attrs;
}

/**
* Get target node.
* @param node Constructor node.
* @param parser Parser, which can extract AstNode from XmlNode.
* @return Target node.
*/
private static AstNode xtarget(final XmlNode node, final Parser parser) {
return parser.parse(node.children().collect(Collectors.toList()).get(0));
}
}
2 changes: 0 additions & 2 deletions src/main/java/org/eolang/opeo/ast/InterfaceInvocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
package org.eolang.opeo.ast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.ToString;
import org.eolang.jeo.representation.xmir.XmlNode;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/eolang/opeo/compilation/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ public interface Parser {
* @param node XML node
* @return AST node
*/
AstNode parse(final XmlNode node);
AstNode parse(XmlNode node);
}
59 changes: 29 additions & 30 deletions src/main/java/org/eolang/opeo/compilation/XmirParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.eolang.opeo.compilation;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.eolang.jeo.representation.xmir.HexString;
Expand All @@ -41,7 +40,6 @@
import org.eolang.opeo.ast.ClassName;
import org.eolang.opeo.ast.Constant;
import org.eolang.opeo.ast.Constructor;
import org.eolang.opeo.ast.ConstructorDescriptor;
import org.eolang.opeo.ast.Duplicate;
import org.eolang.opeo.ast.Field;
import org.eolang.opeo.ast.FieldAssignment;
Expand Down Expand Up @@ -105,34 +103,6 @@ final class XmirParser implements Parser {
this.nodes = nodes;
}

/**
* Convert to XML nodes.
*
* @return XML nodes.
*/
List<XmlNode> toJeoNodes() {
return this.nodes.stream()
.map(this::opcodes)
.flatMap(List::stream)
.collect(Collectors.toList());
}

/**
* Convert XmlNode into a list of opcodes.
*
* @param node XmlNode
* @return List of opcodes
*/
private List<XmlNode> opcodes(final XmlNode node) {
return this.parse(node).opcodes()
.stream()
.map(AstNode::toXmir)
.map(Xembler::new)
.map(Xembler::xmlQuietly)
.map(XmlNode::new)
.collect(Collectors.toList());
}

/**
* Convert XmlNode to AstNode.
*
Expand All @@ -153,6 +123,7 @@ private List<XmlNode> opcodes(final XmlNode node) {
* @checkstyle JavaNCSSCheck (200 lines)
* @checkstyle NestedIfDepthCheck (200 lines)
* @checkstyle MethodLengthCheck (200 lines) *
* @checkstyle NoJavadocForOverriddenMethodsCheck (200 lines)
*/
@SuppressWarnings({"PMD.NcssCount", "PMD.ExcessiveMethodLength", "PMD.CognitiveComplexity"})
@Override
Expand Down Expand Up @@ -306,4 +277,32 @@ node, new Arguments(node, this, 1).toList()
}
return result;
}

/**
* Convert to XML nodes.
*
* @return XML nodes.
*/
List<XmlNode> toJeoNodes() {
return this.nodes.stream()
.map(this::opcodes)
.flatMap(List::stream)
.collect(Collectors.toList());
}

/**
* Convert XmlNode into a list of opcodes.
*
* @param node XmlNode
* @return List of opcodes
*/
private List<XmlNode> opcodes(final XmlNode node) {
return this.parse(node).opcodes()
.stream()
.map(AstNode::toXmir)
.map(Xembler::new)
.map(Xembler::xmlQuietly)
.map(XmlNode::new)
.collect(Collectors.toList());
}
}
27 changes: 17 additions & 10 deletions src/test/java/org/eolang/opeo/ast/ConstructorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.eolang.jeo.representation.xmir.XmlNode;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.xembly.ImpossibleModificationException;
import org.xembly.Transformers;
Expand Down Expand Up @@ -101,23 +100,31 @@ void createsConstructorFromXmir() {
new Constructor(
new XmlNode(ConstructorTest.CONSTRUCTOR),
node -> {
final AstNode result;
if (node.equals(
new XmlNode("<o base='string' data='bytes'>66 69 72 73 74</o>"))) {
return new Literal("first");
new XmlNode("<o base='string' data='bytes'>66 69 72 73 74</o>")
)) {
result = new Literal("first");
} else if (node.equals(
new XmlNode("<o base='string' data='bytes'>73 65 63 6F 6E 64</o>"))) {
return new Literal("second");
new XmlNode("<o base='string' data='bytes'>73 65 63 6F 6E 64</o>")
)) {
result = new Literal("second");
} else if (node.equals(
new XmlNode("<o base='int' data='bytes'>00 00 00 00 00 00 00 03</o>"))) {
return new Literal(3);
} else if (node.equals(new XmlNode(
"<o base='.new-type'><o base='string' data='bytes'>41</o></o>"))) {
return new NewAddress("A");
new XmlNode("<o base='int' data='bytes'>00 00 00 00 00 00 00 03</o>")
)) {
result = new Literal(3);
} else if (node.equals(
new XmlNode(
"<o base='.new-type'><o base='string' data='bytes'>41</o></o>"
)
)) {
result = new NewAddress("A");
} else {
throw new IllegalArgumentException(
String.format("Can't parse constructor from node %s", node)
);
}
return result;
}
),
Matchers.equalTo(
Expand Down

1 comment on commit 4b3e8e8

@0pdd
Copy link

@0pdd 0pdd commented on 4b3e8e8 Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 316-a21f103d discovered in src/main/java/org/eolang/opeo/ast/Constructor.java) and submitted as #323. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.