Skip to content

Commit

Permalink
feat(objectionary#757): add more informative messages during the XMIR…
Browse files Browse the repository at this point in the history
… parsing phase
  • Loading branch information
volodya-lombrozo committed Oct 14, 2024
1 parent 91ab0cb commit 3806d81
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public Bytecode toBytecode() {
String.format("Can't transform '%s' to bytecode", xmir),
exception
);
} catch (final IllegalStateException exception) {
throw new IllegalStateException(
String.format("Can't transform XMIR to bytecode from the '%s' source", this.source),
exception
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.jeo.representation.xmir;

/**
* XMIR parsing exception.
* This exception is thrown when XMIR parsing fails.
* @since 0.6
*/
final class XmirParsingException extends IllegalStateException {

/**
* Serial version UID.
*/
private static final long serialVersionUID = 4711234567890L;

/**
* Constructor.
* @param message Message.
* @param cause Cause.
*/
XmirParsingException(final String message, final Throwable cause) {
super(message, cause);
}
}
40 changes: 25 additions & 15 deletions src/main/java/org/eolang/jeo/representation/xmir/XmlClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,31 @@ public XmlClass(final XmlNode node) {
* @return Bytecode class.
*/
public BytecodeClass bytecode() {
return new BytecodeClass(
new PrefixedName(this.name()).decode(),
this.methods().stream().map(XmlMethod::bytecode)
.collect(Collectors.toList()),
this.fields().stream()
.map(XmlField::bytecode)
.collect(Collectors.toList()),
this.annotations()
.map(XmlAnnotations::bytecode)
.orElse(new BytecodeAnnotations()),
this.attributes()
.map(XmlAttributes::attributes)
.orElse(new ArrayList<>(0)),
this.properties().bytecode()
);
try {
return new BytecodeClass(
new PrefixedName(this.name()).decode(),
this.methods().stream().map(XmlMethod::bytecode)
.collect(Collectors.toList()),
this.fields().stream()
.map(XmlField::bytecode)
.collect(Collectors.toList()),
this.annotations()
.map(XmlAnnotations::bytecode)
.orElse(new BytecodeAnnotations()),
this.attributes()
.map(XmlAttributes::attributes)
.orElse(new ArrayList<>(0)),
this.properties().bytecode()
);
} catch (final IllegalStateException exception) {
throw new XmirParsingException(
String.format(
"Unexpected exception during parsing the class '%s'",
this.name()
),
exception
);
}
}

/**
Expand Down
50 changes: 30 additions & 20 deletions src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,36 @@ public XmlMethod(final XmlNode xmlnode) {
* @return Bytecode method.
*/
public BytecodeMethod bytecode() {
return new BytecodeMethod(
this.trycatchEntries()
.stream()
.map(XmlTryCatchEntry::bytecode)
.collect(Collectors.toList()),
this.instructions()
.stream()
.map(XmlBytecodeEntry::bytecode)
.collect(Collectors.toList()),
this.annotations(),
this.properties(),
this.defvalue()
.map(XmlDefaultValue::bytecode)
.filter(Optional::isPresent)
.map(Optional::get)
.map(Collections::singletonList)
.orElse(Collections.emptyList()),
this.maxs().map(XmlMaxs::bytecode)
.orElse(new BytecodeMaxs(0, 0))
);
try {
return new BytecodeMethod(
this.trycatchEntries()
.stream()
.map(XmlTryCatchEntry::bytecode)
.collect(Collectors.toList()),
this.instructions()
.stream()
.map(XmlBytecodeEntry::bytecode)
.collect(Collectors.toList()),
this.annotations(),
this.properties(),
this.defvalue()
.map(XmlDefaultValue::bytecode)
.filter(Optional::isPresent)
.map(Optional::get)
.map(Collections::singletonList)
.orElse(Collections.emptyList()),
this.maxs().map(XmlMaxs::bytecode)
.orElse(new BytecodeMaxs(0, 0))
);
} catch (final IllegalStateException exception) {
throw new XmirParsingException(
String.format(
"Unexpected exception during parsing the method '%s'",
this.name()
),
exception
);
}
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/eolang/jeo/representation/xmir/XmlProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,17 @@ public String toString() {
* @return Bytecode program.
*/
public BytecodeProgram bytecode() {
return new BytecodeProgram(this.pckg(), this.top().bytecode());
try {
return new BytecodeProgram(this.pckg(), this.top().bytecode());
} catch (final IllegalStateException exception) {
throw new XmirParsingException(
String.format(
"Unexpected exception during parsing the program in package '%s'",
this.pckg()
),
exception
);
}
}

/**
Expand Down

0 comments on commit 3806d81

Please sign in to comment.