Skip to content

Commit

Permalink
#107 better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 24, 2022
1 parent 1e13214 commit ca91d81
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/xembly/Directives.java
Expand Up @@ -102,9 +102,8 @@ public Directives() {
/**
* Public ctor.
* @param text Xembly script
* @throws SyntaxException If syntax is broken
*/
public Directives(final String text) throws SyntaxException {
public Directives(final String text) {
this(new Verbs(text).directives());
}

Expand Down
24 changes: 19 additions & 5 deletions src/main/java/org/xembly/Verbs.java
Expand Up @@ -70,29 +70,43 @@ public Collection<Directive> directives() {
new CommonTokenStream(lexer)
);
lexer.removeErrorListeners();
lexer.addErrorListener(this.errors());
lexer.addErrorListener(Verbs.errors());
parser.removeErrorListeners();
parser.addErrorListener(this.errors());
parser.addErrorListener(Verbs.errors());
try {
return parser.directives().ret;
} catch (final ParsingException ex) {
throw new SyntaxException(this.text, ex);
throw new SyntaxException(
String.format(
"%s \"%s\" in \"%s\"",
ex.getClass().getCanonicalName(),
ex.getLocalizedMessage(),
this.text
),
ex
);
}
}

/**
* Errors listener.
* @return Listener
*/
private ANTLRErrorListener errors() {
private static ANTLRErrorListener errors() {
return new BaseErrorListener() {
// @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) {
throw new SyntaxException(Verbs.this.text, error);
throw new SyntaxException(
String.format(
"\"%s\" at line #%d, position #%d, symbol %s",
msg, line, position, symbol
),
error
);
}
};
}
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/org/xembly/DirectivesTest.java
Expand Up @@ -90,9 +90,12 @@ void throwsOnBrokenGrammar() {

@Test
void throwsOnBrokenXmlContent() {
Assertions.assertThrows(
SyntaxException.class,
() -> new Directives("ADD '\u001b';")
MatcherAssert.assertThat(
Assertions.assertThrows(
SyntaxException.class,
() -> new Directives("ADD 't';\nADD '\u001b';")
).getMessage(),
Matchers.containsString("ADD")
);
}

Expand Down
54 changes: 54 additions & 0 deletions src/test/java/org/xembly/VerbsTest.java
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2013-2022, xembly.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the xembly.org nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.xembly;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link Verbs}.
*
* @since 0.29
*/
final class VerbsTest {

@Test
void throwsOnBrokenSyntax() {
MatcherAssert.assertThat(
Assertions.assertThrows(
SyntaxException.class,
() -> new Verbs("ADD 't';\nADD 'x';broken").directives()
).getMessage(),
Matchers.containsString("token recognition error at: 'b'")
);
}
}

0 comments on commit ca91d81

Please sign in to comment.