Skip to content

Commit

Permalink
#99 antlr4, build is clean
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 15, 2020
1 parent e279293 commit 767fd4d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 38 deletions.
6 changes: 1 addition & 5 deletions pom.xml
Expand Up @@ -33,7 +33,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<parent>
<groupId>com.jcabi</groupId>
<artifactId>parent</artifactId>
<version>0.50.5</version>
<version>0.51.0</version>
</parent>
<groupId>com.jcabi.incubator</groupId>
<artifactId>xembly</artifactId>
Expand Down Expand Up @@ -92,10 +92,6 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Expand Up @@ -29,28 +29,14 @@
*/
grammar Xembly;

@header {
package org.xembly;
@lexer::header {
import java.util.Collection;
import java.util.LinkedList;
}

@lexer::header {
package org.xembly;
}

@lexer::members {
@Override
public void emitErrorMessage(String msg) {
throw new ParsingException(msg);
}
}

@parser::members {
@Override
public void emitErrorMessage(String msg) {
throw new ParsingException(msg);
}
@parser::header {
import java.util.Collection;
import java.util.LinkedList;
}

directives returns [Collection<Directive> ret]
Expand Down
1 change: 0 additions & 1 deletion src/main/aspect/README.txt

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/org/xembly/SyntaxException.java
Expand Up @@ -34,7 +34,7 @@
*
* @since 0.3
*/
public final class SyntaxException extends Exception {
public final class SyntaxException extends RuntimeException {

/**
* Serialization marker.
Expand Down
43 changes: 30 additions & 13 deletions src/main/java/org/xembly/Verbs.java
Expand Up @@ -30,11 +30,12 @@
package org.xembly;

import java.util.Collection;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;

/**
* Directives in plain text.
Expand All @@ -59,16 +60,32 @@ final class Verbs {
/**
* Parse script.
* @return Collection of directives
* @throws SyntaxException If can't parse
*/
public Collection<Directive> directives() throws SyntaxException {
final CharStream input = new ANTLRStringStream(this.text);
final XemblyLexer lexer = new XemblyLexer(input);
final TokenStream tokens = new CommonTokenStream(lexer);
final XemblyParser parser = new XemblyParser(tokens);
public Collection<Directive> directives() {
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(Verbs.this.text, error);
}
};
final XemblyLexer lexer = new XemblyLexer(
CharStreams.fromString(this.text)
);
lexer.removeErrorListeners();
lexer.addErrorListener(errors);
final XemblyParser parser =
new XemblyParser(
new CommonTokenStream(lexer)
);
parser.removeErrorListeners();
parser.addErrorListener(errors);
try {
return parser.directives();
} catch (final RecognitionException | ParsingException ex) {
return parser.directives().ret;
} catch (final ParsingException ex) {
throw new SyntaxException(this.text, ex);
}
}
Expand Down

0 comments on commit 767fd4d

Please sign in to comment.