Skip to content

Commit

Permalink
feat(#3742): use EoParserErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Dec 24, 2024
1 parent 9b44b6b commit 179d1ef
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 71 deletions.
10 changes: 6 additions & 4 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.EoParserErrors;
import org.eolang.parser.errors.ParsingErrors;
import org.xembly.Directives;
import org.xembly.Xembler;
Expand Down Expand Up @@ -114,17 +115,18 @@ public XML parsed() throws IOException {
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(spy).append(eospy)
).domQuietly()
)
);
if (spy.size() == 0) {
if (spy.size() + eospy.size() == 0) {
Logger.debug(
this,
"The %s program of %d EO lines compiled, no errors",
Expand All @@ -133,7 +135,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(), spy.size() + eospy.size()
);
}
return dom;
Expand Down
150 changes: 149 additions & 1 deletion eo-parser/src/main/java/org/eolang/parser/errors/EoParserErrors.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,160 @@
/*
* 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.errors;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.InputMismatchException;
import org.antlr.v4.runtime.NoViableAltException;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.cactoos.Text;
import org.eolang.parser.EoParser;
import org.eolang.parser.ParsingException;
import org.xembly.Directive;

/**
* Accumulates all parsing errors related to EO parser.
* @since 0.50
*/
public final class EoParserErrors extends BaseErrorListener implements Iterable<Directive> {

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

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

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

/**
* Ctor.
* @param errors Errors accumulated
* @param lines The source in lines
*/
private EoParserErrors(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
) {
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;
final String rule = parser.getRuleInvocationStack().get(0);
final String[] names = parser.getRuleNames();
final String detailed;
if (names[EoParser.RULE_objects].equals(rule)) {
detailed = "Invalid object declaration";
} else if (names[EoParser.RULE_metas].equals(rule)) {
detailed = "Invalid meta declaration";
} else if (names[EoParser.RULE_program].equals(rule)) {
detailed = "Invalid program declaration";
} else {
detailed = "no viable alternative at input";
}
this.errors.add(
new ParsingException(
String.format(
"[%d:%d] %s: %s:%n%s",
line, position,
"error",
detailed,
new UnderlinedMessage(
this.lines.line(line),
position,
Math.max(token.getStopIndex() - token.getStartIndex(), 1)
).formatted()
),
error,
line
)
);
} else if (Objects.isNull(error)) {
this.errors.add(
new ParsingException(
String.format(
"[%d:%d] %s: %s", line, position, "error", msg
),
line
)
);
} else {
this.errors.add(
new ParsingException(
String.format(
"[%d:%d] %s: \"%s\"",
line, position, msg, this.lines.line(line)
),
error,
line
)
);
}
}

@Override
public Iterator<Directive> iterator() {
return null;
return new ErrorDirectives(this.errors).iterator();
}

/**
* The number of errors.
* @return The number of errors.
*/
public int size() {
return this.errors.size();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* 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.errors;

import java.util.Iterator;
Expand All @@ -7,7 +30,11 @@
import org.xembly.Directive;
import org.xembly.Directives;

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

/**
* Errors accumulated.
Expand All @@ -18,7 +45,7 @@ public final class ErrorDirectives implements Iterable<Directive> {
* Ctor.
* @param errors The errors.
*/
public ErrorDirectives(final List<ParsingException> errors) {
ErrorDirectives(final List<ParsingException> errors) {
this.errors = errors;
}

Expand Down
35 changes: 33 additions & 2 deletions eo-parser/src/main/java/org/eolang/parser/errors/Lines.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
/*
* 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.errors;

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

/**
* The source in lines.
* @since 0.50
*/
final class Lines {

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

/**
* Ctor.
* @param lines The source in lines
*/
Lines(final List<Text> lines) {
this.lines = lines;
}
Expand All @@ -21,7 +52,7 @@ final class Lines {
* @param number The line number.
* @return The line.
*/
Optional<String> line(final int number) {
String line(final int number) {
final Optional<String> result;
if (number < 1 || number > this.lines.size()) {
result = Optional.empty();
Expand All @@ -30,6 +61,6 @@ Optional<String> line(final int number) {
.map(UncheckedText::new)
.map(UncheckedText::asString);
}
return result;
return result.orElse("EOF");
}
}
Loading

0 comments on commit 179d1ef

Please sign in to comment.