forked from pmd/pmd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate AntlrTokenManager and decouple the lexer instance from the t…
…okenizer
- Loading branch information
Matias Fraga
committed
Sep 9, 2018
1 parent
1eed099
commit 15bd890
Showing
3 changed files
with
99 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
pmd-core/src/main/java/net/sourceforge/pmd/lang/AntlrTokenManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang; | ||
|
||
import org.antlr.v4.runtime.BaseErrorListener; | ||
import org.antlr.v4.runtime.Lexer; | ||
import org.antlr.v4.runtime.RecognitionException; | ||
import org.antlr.v4.runtime.Recognizer; | ||
|
||
/** | ||
* Generic token manager implementation for all Antlr lexers. | ||
*/ | ||
public class AntlrTokenManager implements TokenManager { | ||
private final Lexer lexer; | ||
private String fileName; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param lexer The lexer | ||
* @param fileName The file name | ||
*/ | ||
public AntlrTokenManager(final Lexer lexer, final String fileName) { | ||
this.lexer = lexer; | ||
this.fileName = fileName; | ||
} | ||
|
||
@Override | ||
public Object getNextToken() { | ||
return lexer.nextToken(); | ||
} | ||
|
||
@Override | ||
public void setFileName(String fileName) { | ||
this.fileName = fileName; | ||
} | ||
|
||
public String getFileName() { | ||
return fileName; | ||
} | ||
|
||
public void resetListeners() { | ||
lexer.removeErrorListeners(); | ||
lexer.addErrorListener(new ErrorHandler()); | ||
} | ||
|
||
|
||
private static class ErrorHandler extends BaseErrorListener { | ||
|
||
@Override | ||
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, | ||
final int charPositionInLine, final String msg, final RecognitionException ex) { | ||
throw new ANTLRSyntaxError(msg, line, charPositionInLine, ex); | ||
} | ||
} | ||
|
||
public static class ANTLRSyntaxError extends RuntimeException { | ||
private static final long serialVersionUID = 1L; | ||
private final int line; | ||
private final int column; | ||
|
||
/* default */ ANTLRSyntaxError(final String msg, final int line, final int column, | ||
final RecognitionException cause) { | ||
super(msg, cause); | ||
this.line = line; | ||
this.column = column; | ||
} | ||
|
||
public int getLine() { | ||
return line; | ||
} | ||
|
||
public int getColumn() { | ||
return column; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters