Skip to content

Commit

Permalink
feat(#3742): create a separate package for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Dec 24, 2024
1 parent 78f13fd commit 9b44b6b
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 51 deletions.
1 change: 1 addition & 0 deletions eo-parser/src/main/java/org/eolang/parser/EoSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.cactoos.text.Joined;
import org.cactoos.text.Split;
import org.cactoos.text.TextOf;
import org.eolang.parser.errors.ParsingErrors;
import org.xembly.Directives;
import org.xembly.Xembler;

Expand Down
1 change: 1 addition & 0 deletions eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.cactoos.Text;
import org.cactoos.io.InputStreamOf;
import org.eolang.parser.errors.ParsingErrors;
import org.xembly.Directive;
import org.xembly.Directives;
import org.xembly.Xembler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.eolang.parser.errors;

import java.util.Iterator;
import org.antlr.v4.runtime.BaseErrorListener;
import org.xembly.Directive;

public final class EoParserErrors extends BaseErrorListener implements Iterable<Directive> {
@Override
public Iterator<Directive> iterator() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.eolang.parser.errors;

import java.util.Iterator;
import java.util.List;
import org.cactoos.iterable.Mapped;
import org.eolang.parser.ParsingException;
import org.xembly.Directive;
import org.xembly.Directives;

public final class ErrorDirectives implements Iterable<Directive> {

/**
* Errors accumulated.
*/
private final List<ParsingException> errors;

/**
* Ctor.
* @param errors The errors.
*/
public ErrorDirectives(final List<ParsingException> errors) {
this.errors = errors;
}

@Override
public Iterator<Directive> iterator() {
return new org.cactoos.iterable.Joined<>(
new Mapped<Iterable<Directive>>(
error -> new Directives()
.xpath("/program")
.strict(1)
.addIf("errors")
.strict(1)
.add("error")
.attr("check", "eo-parser")
.attr("line", error.line())
.attr("severity", "critical")
.set(error.getMessage()),
this.errors
)
).iterator();
}
}
35 changes: 35 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/errors/Lines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.eolang.parser.errors;

import java.util.List;
import java.util.Optional;
import org.cactoos.Text;
import org.cactoos.text.UncheckedText;

final class Lines {

/**
* The source.
*/
private final List<Text> lines;

Lines(final List<Text> lines) {
this.lines = lines;
}

/**
* Get the line by number.
* @param number The line number.
* @return The line.
*/
Optional<String> line(final int number) {
final Optional<String> result;
if (number < 1 || number > this.lines.size()) {
result = Optional.empty();
} else {
result = Optional.ofNullable(this.lines.get(number - 1))
.map(UncheckedText::new)
.map(UncheckedText::asString);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.parser;
package org.eolang.parser.errors;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.InputMismatchException;
import org.antlr.v4.runtime.NoViableAltException;
Expand All @@ -36,22 +35,17 @@
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.cactoos.Text;
import org.cactoos.iterable.Mapped;
import org.cactoos.list.ListOf;
import org.cactoos.text.UncheckedText;
import org.eolang.parser.EoParser;
import org.eolang.parser.ParsingException;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Accumulates all parsing errors.
*
* @since 0.30.0
* @todo #3706:30min Split {@link ParsingErrors} into two classes.
* Currently we use the same {@link ParsingErrors} class to accumulate all the parsing errors
* despite their origin. This class should be split into two classes: one for parsing errors
* {@link ParserErrors} and another for lexer errors {@link LexerErrors}.
*/
final class ParsingErrors extends BaseErrorListener implements Iterable<Directive> {
public final class ParsingErrors extends BaseErrorListener implements Iterable<Directive> {

/**
* Errors accumulated.
Expand All @@ -61,23 +55,23 @@ final class ParsingErrors extends BaseErrorListener implements Iterable<Directiv
/**
* The source.
*/
private final List<Text> lines;
private final Lines lines;

/**
* Ctor.
* @param lines The source in lines
*/
ParsingErrors(final Text... lines) {
public ParsingErrors(final Text... lines) {
this(new ListOf<>(lines));
}

/**
* Ctor.
* @param src The source in lines
*/
ParsingErrors(final List<Text> src) {
public ParsingErrors(final List<Text> src) {
this.errors = new LinkedList<>();
this.lines = src;
this.lines = new Lines(src);
}

// @checkstyle ParameterNumberCheck (10 lines)
Expand Down Expand Up @@ -113,7 +107,7 @@ public void syntaxError(
"error",
detailed,
new UnderlinedMessage(
this.line(line).orElse("EOF"),
this.lines.line(line).orElse("EOF"),
position,
Math.max(token.getStopIndex() - token.getStartIndex(), 1)
).formatted()
Expand All @@ -136,7 +130,7 @@ public void syntaxError(
new ParsingException(
String.format(
"[%d:%d] %s: \"%s\"",
line, position, msg, this.line(line).orElse("EOF")
line, position, msg, this.lines.line(line).orElse("EOF")
),
error,
line
Expand All @@ -147,21 +141,7 @@ public void syntaxError(

@Override
public Iterator<Directive> iterator() {
return new org.cactoos.iterable.Joined<>(
new Mapped<Iterable<Directive>>(
error -> new Directives()
.xpath("/program")
.strict(1)
.addIf("errors")
.strict(1)
.add("error")
.attr("check", "eo-parser")
.attr("line", error.line())
.attr("severity", "critical")
.set(error.getMessage()),
this.errors
)
).iterator();
return new ErrorDirectives(this.errors).iterator();
}

/**
Expand All @@ -171,21 +151,4 @@ public Iterator<Directive> iterator() {
public int size() {
return this.errors.size();
}

/**
* Get the line by number.
* @param number The line number.
* @return The line.
*/
private Optional<String> line(final int number) {
final Optional<String> result;
if (number < 1 || number > this.lines.size()) {
result = Optional.empty();
} else {
result = Optional.ofNullable(this.lines.get(number - 1))
.map(UncheckedText::new)
.map(UncheckedText::asString);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.parser;
package org.eolang.parser.errors;

import java.util.Collections;

Expand All @@ -41,7 +41,7 @@
* </p>
* @since 0.50
* @todo #3332:30min Add more decorators for the error message.
* For example, {@link ParsingErrors} currently contains logic related to the message formatting.
* For example, {@link org.eolang.parser.ParsingErrors} currently contains logic related to the message formatting.
* It's better to create a separate class for this purpose.
*/
final class UnderlinedMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.parser;
package org.eolang.parser.errors;

import java.util.stream.Stream;
import org.hamcrest.MatcherAssert;
Expand Down

0 comments on commit 9b44b6b

Please sign in to comment.