Skip to content

Commit

Permalink
#107 static instead
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 24, 2022
1 parent ca91d81 commit f1f8c65
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
52 changes: 24 additions & 28 deletions src/main/java/org/xembly/Verbs.java
Expand Up @@ -44,6 +44,26 @@
*/
final class Verbs {

/**
* Errors listener.
*/
private static final ANTLRErrorListener ERRORS = 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(
String.format(
"\"%s\" at line #%d, position #%d, symbol %s",
msg, line, position, symbol
),
error
);
}
};

/**
* The text.
*/
Expand All @@ -70,45 +90,21 @@ public Collection<Directive> directives() {
new CommonTokenStream(lexer)
);
lexer.removeErrorListeners();
lexer.addErrorListener(Verbs.errors());
lexer.addErrorListener(Verbs.ERRORS);
parser.removeErrorListeners();
parser.addErrorListener(Verbs.errors());
parser.addErrorListener(Verbs.ERRORS);
try {
return parser.directives().ret;
} catch (final ParsingException ex) {
throw new SyntaxException(
String.format(
"%s \"%s\" in \"%s\"",
"Parsing failed as %s: \"%s\"",
ex.getClass().getCanonicalName(),
ex.getLocalizedMessage(),
this.text
ex.getLocalizedMessage()
),
ex
);
}
}

/**
* Errors listener.
* @return Listener
*/
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(
String.format(
"\"%s\" at line #%d, position #%d, symbol %s",
msg, line, position, symbol
),
error
);
}
};
}

}
2 changes: 1 addition & 1 deletion src/test/java/org/xembly/DirectivesTest.java
Expand Up @@ -95,7 +95,7 @@ void throwsOnBrokenXmlContent() {
SyntaxException.class,
() -> new Directives("ADD 't';\nADD '\u001b';")
).getMessage(),
Matchers.containsString("ADD")
Matchers.containsString("Character #1B is in the restricted XML")
);
}

Expand Down

0 comments on commit f1f8c65

Please sign in to comment.