Skip to content

Commit

Permalink
Merge pull request #3751 from volodya-lombrozo/3742_errors
Browse files Browse the repository at this point in the history
feat(#3742): Split `ParsingErrors` on Two Separate Classes
  • Loading branch information
yegor256 authored Dec 25, 2024
2 parents bf2b212 + d4764a6 commit 71e3409
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 76 deletions.
68 changes: 68 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/DrErrors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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.parser;

import java.util.Iterator;
import org.cactoos.iterable.Mapped;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Error directives.
* @since 0.50
*/
final class DrErrors implements Iterable<Directive> {

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

/**
* Ctor.
* @param errors The errors.
*/
DrErrors(final Iterable<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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
*/
package org.eolang.parser;

import java.util.ArrayList;
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,12 @@
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.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}.
* Accumulates all parsing errors related to EO parser.
* @since 0.50
*/
final class ParsingErrors extends BaseErrorListener implements Iterable<Directive> {
final class EoParserErrors extends BaseErrorListener implements Iterable<ParsingException> {

/**
* Errors accumulated.
Expand All @@ -61,23 +50,24 @@ 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
* @param src The source in lines
*/
ParsingErrors(final Text... lines) {
this(new ListOf<>(lines));
EoParserErrors(final List<Text> src) {
this(new ArrayList<>(0), new Lines(src));
}

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

// @checkstyle ParameterNumberCheck (10 lines)
Expand All @@ -90,6 +80,13 @@ public void syntaxError(
final String msg,
final RecognitionException error
) {
if (!Parser.class.isInstance(recognizer)) {
throw new IllegalArgumentException(
String.format(
"Illegal usage of %s, please use it to recognize only EO parser errors", this
)
);
}
if (error instanceof NoViableAltException || error instanceof InputMismatchException) {
final Token token = (Token) symbol;
final Parser parser = (Parser) recognizer;
Expand All @@ -113,7 +110,7 @@ public void syntaxError(
"error",
detailed,
new UnderlinedMessage(
this.line(line).orElse("EOF"),
this.lines.line(line),
position,
Math.max(token.getStopIndex() - token.getStartIndex(), 1)
).formatted()
Expand All @@ -136,7 +133,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)
),
error,
line
Expand All @@ -146,46 +143,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();
}

/**
* How many errors?
* @return Count of errors accumulated
*/
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;
public Iterator<ParsingException> iterator() {
return this.errors.iterator();
}
}
15 changes: 10 additions & 5 deletions eo-parser/src/main/java/org/eolang/parser/EoSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.cactoos.Text;
import org.cactoos.io.InputOf;
import org.cactoos.list.ListOf;
import org.cactoos.scalar.LengthOf;
import org.cactoos.scalar.Unchecked;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.cactoos.text.Split;
Expand Down Expand Up @@ -105,25 +107,28 @@ public EoSyntax(final String nme, final Input ipt) {
*/
public XML parsed() throws IOException {
final List<Text> lines = this.lines();
final ParsingErrors spy = new ParsingErrors(lines);
final GeneralErrors spy = new GeneralErrors(lines);
final EoLexer lexer = new EoIndentLexer(this.normalize());
lexer.removeErrorListeners();
lexer.addErrorListener(spy);
final EoParser parser = new EoParser(
new CommonTokenStream(lexer)
);
parser.removeErrorListeners();
parser.addErrorListener(spy);
final EoParserErrors eospy = new EoParserErrors(lines);
parser.addErrorListener(eospy);
final XeEoListener xel = new XeEoListener(this.name);
new ParseTreeWalker().walk(xel, parser.program());
final XML dom = Syntax.CANONICAL.pass(
new XMLDocument(
new Xembler(
new Directives(xel).append(spy)
new Directives(xel).append(new DrErrors(spy)).append(new DrErrors(eospy))
).domQuietly()
)
);
if (spy.size() == 0) {
final long errors = new Unchecked<>(new LengthOf(spy)).value()
+ new Unchecked<>(new LengthOf(eospy)).value();
if (errors == 0) {
Logger.debug(
this,
"The %s program of %d EO lines compiled, no errors",
Expand All @@ -132,7 +137,7 @@ public XML parsed() throws IOException {
} else {
Logger.debug(
this, "The %s program of %d EO lines compiled with %d error(s)",
this.name, lines.size(), spy.size()
this.name, lines.size(), errors
);
}
return dom;
Expand Down
104 changes: 104 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/GeneralErrors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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.parser;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.cactoos.Text;
import org.cactoos.list.ListOf;

/**
* Accumulates all parsing errors.
*
* @since 0.30.0
*/
final class GeneralErrors extends BaseErrorListener implements Iterable<ParsingException> {

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

/**
* The source.
*/
private final Lines lines;

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

/**
* Ctor.
* @param src The source in lines
*/
GeneralErrors(final List<Text> src) {
this(new ArrayList<>(0), new Lines(src));
}

/**
* Ctor.
* @param errors Errors accumulated
* @param lines The source in lines
*/
private GeneralErrors(final List<ParsingException> errors, final Lines lines) {
this.errors = errors;
this.lines = lines;
}

// @checkstyle ParameterNumberCheck (10 lines)
@Override
public void syntaxError(
final Recognizer<?, ?> recognizer,
final Object symbol,
final int line,
final int position,
final String msg,
final RecognitionException error
) {
this.errors.add(
new ParsingException(
String.format(
"[%d:%d] %s: \"%s\"",
line, position, msg, this.lines.line(line)
),
error,
line
)
);
}

@Override
public Iterator<ParsingException> iterator() {
return this.errors.iterator();
}
}
Loading

3 comments on commit 71e3409

@0pdd
Copy link

@0pdd 0pdd commented on 71e3409 Dec 25, 2024

Choose a reason for hiding this comment

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

Puzzle 3706-8f2012b6 disappeared from eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java), that's why I closed #3742. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 71e3409 Dec 25, 2024

Choose a reason for hiding this comment

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

Puzzle 3332-fa5220a9 disappeared from eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java), that's why I closed #3707. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 71e3409 Dec 25, 2024

Choose a reason for hiding this comment

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

Puzzle 3332-6fa7c070 discovered in eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java) and submitted as #3756. 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.