Java源码示例:org.antlr.v4.runtime.LexerNoViableAltException
示例1
/**
* Ensures the ANTLR lexer will throw an exception after the first error
* @param lnvae the lexer exception
*/
@Override
public void recover(LexerNoViableAltException lnvae) {
CharStream charStream = lnvae.getInputStream();
int startIndex = lnvae.getStartIndex();
String text = charStream.getText(Interval.of(startIndex, charStream.index()));
ParseException parseException = new ParseException("unexpected character '" + getErrorDisplay(text) + "'" +
" on line (" + _tokenStartLine + ") position (" + _tokenStartCharPositionInLine + ")", _tokenStartCharIndex);
parseException.initCause(lnvae);
throw new RuntimeException(parseException);
}
示例2
@Override
public void recover(LexerNoViableAltException e) {
if (_recoveryStrategy != null) {
_recoveryStrategy.recover();
} else {
super.recover(e);
}
}
示例3
@Override
public Token nextToken()
{
if (_input == null) {
throw new IllegalStateException("nextToken requires a non-null input stream.");
}
// Mark start location in char stream so unbuffered streams are
// guaranteed at least have text of current token
int tokenStartMarker = _input.mark();
try {
outer:
while (true) {
if (_hitEOF) {
emitEOF();
return _token;
}
_token = null;
_channel = Token.DEFAULT_CHANNEL;
_tokenStartCharIndex = _input.index();
_tokenStartCharPositionInLine = getInterpreter().getCharPositionInLine();
_tokenStartLine = getInterpreter().getLine();
_text = null;
do {
_type = Token.INVALID_TYPE;
int ttype = -1;
// This entire method is copied from org.antlr.v4.runtime.Lexer, with the following bit
// added to match the delimiters before we attempt to match the token
boolean found = false;
for (String terminator : delimiters) {
if (match(terminator)) {
ttype = SqlBaseParser.DELIMITER;
found = true;
break;
}
}
if (!found) {
try {
ttype = getInterpreter().match(_input, _mode);
}
catch (LexerNoViableAltException e) {
notifyListeners(e); // report error
recover(e);
ttype = SKIP;
}
}
if (_input.LA(1) == IntStream.EOF) {
_hitEOF = true;
}
if (_type == Token.INVALID_TYPE) {
_type = ttype;
}
if (_type == SKIP) {
continue outer;
}
}
while (_type == MORE);
if (_token == null) {
emit();
}
return _token;
}
}
finally {
// make sure we release marker after match or
// unbuffered char stream will keep buffering
_input.release(tokenStartMarker);
}
}
示例4
@Override
public Token nextToken()
{
if (_input == null) {
throw new IllegalStateException("nextToken requires a non-null input stream.");
}
// Mark start location in char stream so unbuffered streams are
// guaranteed at least have text of current token
int tokenStartMarker = _input.mark();
try {
outer:
while (true) {
if (_hitEOF) {
emitEOF();
return _token;
}
_token = null;
_channel = Token.DEFAULT_CHANNEL;
_tokenStartCharIndex = _input.index();
_tokenStartCharPositionInLine = getInterpreter().getCharPositionInLine();
_tokenStartLine = getInterpreter().getLine();
_text = null;
do {
_type = Token.INVALID_TYPE;
int ttype = -1;
// This entire method is copied from org.antlr.v4.runtime.Lexer, with the following bit
// added to match the delimiters before we attempt to match the token
boolean found = false;
for (String terminator : delimiters) {
if (match(terminator)) {
ttype = SqlBaseParser.DELIMITER;
found = true;
break;
}
}
if (!found) {
try {
ttype = getInterpreter().match(_input, _mode);
}
catch (LexerNoViableAltException e) {
notifyListeners(e); // report error
recover(e);
ttype = SKIP;
}
}
if (_input.LA(1) == IntStream.EOF) {
_hitEOF = true;
}
if (_type == Token.INVALID_TYPE) {
_type = ttype;
}
if (_type == SKIP) {
continue outer;
}
}
while (_type == MORE);
if (_token == null) {
emit();
}
return _token;
}
}
finally {
// make sure we release marker after match or
// unbuffered char stream will keep buffering
_input.release(tokenStartMarker);
}
}
示例5
@Override
public void recover(LexerNoViableAltException e) {
throw new CapitulatingRuntimeException();
}
示例6
/**
* Returns the firewall configuration as set of policies.
*
* @return the set of policies - may be empty but not <code>null</code>
*/
public Set<FWPolicy> getFirewallPolicies() throws Exception {
String config = getFirewallConfiguration();
if (notNullNorEmpty(config)) {
logger.debug("Given firewall configuration: " + config);
ANTLRInputStream input = new ANTLRInputStream(config);
FWPolicyLexer lexer = null;
try {
lexer = new FWPolicyLexer(input) {
@Override
public void recover(LexerNoViableAltException e) {
Interval interval = new Interval(e.getStartIndex(), e
.getInputStream().size() - 1);
String policy = e.getInputStream().getText(interval);
String message = Messages.get(
Messages.DEFAULT_LOCALE,
"error_invalid_firewallconfig_character",
new Object[] {
Integer.valueOf(e.getStartIndex()),
policy });
throw new RuntimeException(message);
}
};
} catch (RuntimeException rex) {
throw new Exception(rex);
}
CommonTokenStream tokens = new CommonTokenStream(lexer);
FWPolicyParser parser = new FWPolicyParser(tokens);
parser.setBuildParseTree(true);
parser.setErrorHandler(new FWPolicyErrorStrategy());
ParseTree tree = parser.policies();
PoliciesContext context = (PoliciesContext) tree.getPayload();
return Collections.unmodifiableSet(new HashSet<>(context.pList));
}
// return unmodifiable to signal the the result is not backed by the
// handler
return Collections.unmodifiableSet(new HashSet<FWPolicy>());
}
示例7
@Override
public void recover(LexerNoViableAltException e) {
throw e; // if some lexical error occurred, stop parsing!
}
示例8
@Override
public void recover(final LexerNoViableAltException exception) {
throw new LexerException(exception.getMessage());
}
示例9
@Override
public void recover(final LexerNoViableAltException exception) {
throw new LexerException(exception.getMessage());
}
示例10
@Override
public Token nextToken() {
if (_input == null) {
throw new IllegalStateException("nextToken requires a non-null input stream.");
}
// Mark start location in char stream so unbuffered streams are
// guaranteed at least have text of current token
int tokenStartMarker = _input.mark();
try {
outer:
while (true) {
if (_hitEOF) {
emitEOF();
return _token;
}
_token = null;
_channel = Token.DEFAULT_CHANNEL;
_tokenStartCharIndex = _input.index();
_tokenStartCharPositionInLine = getInterpreter().getCharPositionInLine();
_tokenStartLine = getInterpreter().getLine();
_text = null;
do {
_type = Token.INVALID_TYPE;
int ttype = -1;
// This entire method is copied from org.antlr.v4.runtime.Lexer, with the following bit
// added to match the delimiters before we attempt to match the token
boolean found = false;
for (String terminator : delimiters) {
if (match(terminator)) {
ttype = SqlBaseParser.DELIMITER;
found = true;
break;
}
}
if (!found) {
try {
ttype = getInterpreter().match(_input, _mode);
} catch (LexerNoViableAltException e) {
notifyListeners(e); // report error
recover(e);
ttype = SKIP;
}
}
if (_input.LA(1) == IntStream.EOF) {
_hitEOF = true;
}
if (_type == Token.INVALID_TYPE) {
_type = ttype;
}
if (_type == SKIP) {
continue outer;
}
}
while (_type == MORE);
if (_token == null) {
emit();
}
return _token;
}
} finally {
// make sure we release marker after match or
// unbuffered char stream will keep buffering
_input.release(tokenStartMarker);
}
}
示例11
/**
* Create an AST from a program (using ANTLR lexer & parser)
* Returns null if error
* Use 'alreadyIncluded' to keep track of from 'include' statements
*/
ParseTree createAst(CharStream input, boolean debug, Set<String> alreadyIncluded) {
BigDataScriptLexer lexer = null;
BigDataScriptParser parser = null;
try {
//---
// Lexer: Create a lexer that feeds off of input CharStream
//---
lexer = new BigDataScriptLexer(input) {
@Override
public void recover(LexerNoViableAltException e) {
throw new RuntimeException(e); // Bail out
}
};
//---
// Parser
//---
CommonTokenStream tokens = new CommonTokenStream(lexer);
parser = new BigDataScriptParser(tokens);
// Parser error handling
parser.setErrorHandler(new CompileErrorStrategy()); // Bail out with pendingException if errors in parser
parser.addErrorListener(new CompilerErrorListener()); // Catch some other error messages that 'CompileErrorStrategy' fails to catch
// Begin parsing at main rule
ParseTree tree = parserNode(parser);
// Error loading file?
if (tree == null) {
System.err.println("Can't parse file '" + programFileName + "'");
return null;
}
// Show main nodes
if (debug) {
Timer.showStdErr("AST:");
for (int childNum = 0; childNum < tree.getChildCount(); childNum++) {
Tree child = tree.getChild(childNum);
System.err.println("\t\tChild " + childNum + ":\t" + child + "\tTree:'" + child.toStringTree() + "'");
}
}
// Included files
boolean resolveIncludePending = true;
while (resolveIncludePending)
resolveIncludePending = resolveIncludes(tree, debug, alreadyIncluded);
return tree;
} catch (Exception e) {
String msg = e.getMessage();
CompilerMessages.get().addError("Could not compile " + programFileName //
+ (msg != null ? " :" + e.getMessage() : "") //
);
return null;
}
}