Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#3742): Split ParsingErrors on Two Separate Classes #3751

Merged
merged 11 commits into from
Dec 25, 2024
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;
yegor256 marked this conversation as resolved.
Show resolved Hide resolved
}

@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
Loading