本文整理匯總了Java中org.antlr.v4.runtime.Recognizer類的典型用法代碼示例。如果您正苦於以下問題:Java Recognizer類的具體用法?Java Recognizer怎麽用?Java Recognizer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Recognizer類屬於org.antlr.v4.runtime包,在下文中一共展示了Recognizer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: syntaxError
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine,
String msg, RecognitionException e) {
if (!reportSyntaxErrors) {
return;
}
// String sourceName = recognizer.getInputStream().getSourceName();
// if (!sourceName.isEmpty()) {
// sourceName = String.format("%s:%d:%d: ", sourceName, line, charPositionInLine);
// }
System.err.println("CUSTOM: " + "line " + line + ":" + charPositionInLine + " " + msg);
syntaxErrors = msg;
}
示例2: setupPicky
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
private void setupPicky(PainlessParser parser) {
// Diagnostic listener invokes syntaxError on other listeners for ambiguity issues,
parser.addErrorListener(new DiagnosticErrorListener(true));
// a second listener to fail the test when the above happens.
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(final Recognizer<?,?> recognizer, final Object offendingSymbol, final int line,
final int charPositionInLine, final String msg, final RecognitionException e) {
throw new AssertionError("line: " + line + ", offset: " + charPositionInLine +
", symbol:" + offendingSymbol + " " + msg);
}
});
// Enable exact ambiguity detection (costly). we enable exact since its the default for
// DiagnosticErrorListener, life is too short to think about what 'inexact ambiguity' might mean.
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
}
示例3: syntaxError
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine,
String msg, RecognitionException e)
{
if (!REPORT_SYNTAX_ERRORS) {
return;
}
String sourceName = recognizer.getInputStream().getSourceName();
if (!sourceName.isEmpty()) {
sourceName = String.format("%d:%d: ", line, charPositionInLine);
}
error_msg.add(sourceName+"line "+line+":"+charPositionInLine+" "+msg);
}
示例4: parseInputStream
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
private void parseInputStream(CharStream inputStream, OboParseResultListener listener) {
final OboLexer l = new OboLexer(inputStream);
final Antlr4OboParser p = new Antlr4OboParser(new CommonTokenStream(l));
p.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) {
throw new IllegalStateException("Failed to parse at line " + line + " due to " + msg, e);
}
});
if (debug) {
p.addErrorListener(new DiagnosticErrorListener());
}
p.addParseListener(new OboParserListener(listener));
p.oboFile();
}
示例5: syntaxError
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine,
String msg, RecognitionException e)
{
List<String> stack = ((Parser)recognizer).getRuleInvocationStack(); Collections.reverse(stack);
System.err.println("rule stack: "+stack);
System.err.println("linea "+line+":"+charPositionInLine+" at "+
offendingSymbol+": "+msg);
String rule = "rule stack: "+stack;
String mensaje = "linea "+line+":"+charPositionInLine+" at "+
offendingSymbol+": "+msg + "\n\r";
agregarLog("Un error inesperado ha ocurrido " +"\n" + mensaje, line, charPositionInLine,true);
}
示例6: registerErrorListener
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
public static void registerErrorListener(final CoqFTParser parser) {
parser.removeErrorListeners();
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e) {
throw new CoqSyntaxException(parser,
(Token)offendingSymbol, line, charPositionInLine, msg,
e);
}
});
}
示例7: syntaxError
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
/**
* Logs parser errors in Checkstyle manner. Parser can generate error
* messages. There is special error that parser can generate. It is
* missed close HTML tag. This case is special because parser prints
* error like {@code "no viable alternative at input 'b \n *\n'"} and it
* is not clear that error is about missed close HTML tag. Other error
* messages are not special and logged simply as "Parse Error...".
*
* <p>{@inheritDoc}
*/
@Override
public void syntaxError(
Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine,
String msg, RecognitionException ex) {
final int lineNumber = offset + line;
if (MSG_JAVADOC_WRONG_SINGLETON_TAG.equals(msg)) {
errorMessage = new ParseErrorMessage(lineNumber,
MSG_JAVADOC_WRONG_SINGLETON_TAG, charPositionInLine,
((Token) offendingSymbol).getText());
throw new IllegalArgumentException(msg);
}
else {
final int ruleIndex = ex.getCtx().getRuleIndex();
final String ruleName = recognizer.getRuleNames()[ruleIndex];
final String upperCaseRuleName = CaseFormat.UPPER_CAMEL.to(
CaseFormat.UPPER_UNDERSCORE, ruleName);
errorMessage = new ParseErrorMessage(lineNumber,
MSG_JAVADOC_PARSE_RULE_ERROR, charPositionInLine, msg, upperCaseRuleName);
}
}
示例8: testExampleFieldCondition
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
@Test
public void testExampleFieldCondition() throws Exception {
ScoreLexer l = new ScoreLexer(CharStreams.fromString(expression));
ScoreParser p = new ScoreParser(new CommonTokenStream(l));
p.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) {
throw new IllegalStateException(String.format(
"Failed to parse at line %d position %d due to %s", line, charPositionInLine, msg), e);
}
});
ScoreTranslator visitor = new ScoreTranslator();
AnyExpressionContext ctx = p.anyExpression();
String text = visitor.visitAnyExpression(ctx);
System.out.println(text);
}
示例9: testExampleFieldCondition
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
@Test
public void testExampleFieldCondition() throws Exception {
ScoreLexer l = new ScoreLexer(CharStreams.fromString(fieldCondition));
ScoreParser p = new ScoreParser(new CommonTokenStream(l));
p.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) {
throw new IllegalStateException(String.format(
"Failed to parse at line %d position %d due to %s", line, charPositionInLine, msg), e);
}
});
ScoreBaseVisitor<Object> visitor = new ScoreBaseVisitor<>();
AnyExpressionContext ctx = p.anyExpression();
Object expression = visitor.visitAnyExpression(ctx);
//System.out.println(expression.getClass().getSimpleName());
}
示例10: compileExpression
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
public Statement compileExpression(String expr, MethodDecl method, ModuleInstance module) throws CompileException {
VbaLexer lexer = new VbaLexer(new org.antlr.v4.runtime.ANTLRInputStream(expr));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
VbaParser parser = new VbaParser(tokenStream);
parser.setBuildParseTree(true);
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
String msg, RecognitionException e) {
// errors.add(new CompileException(new SourceLocation(file, line, charPositionInLine, 0),
// CompileException.SYNTAX_ERROR, msg, ((CommonToken) offendingSymbol).getText()));
System.err.println(msg);
}
});
EvalStmtContext eval = parser.evalStmt();
ParserRuleContext c = (ParserRuleContext) eval.getChild(0);
if (c instanceof ValueStmtContext) {
return this.compileValueStatement((ValueStmtContext) c, method).getStatement();
} else {
return new BlockCompiler(method, this).compileBlockStatement(c);
}
}
示例11: syntaxError
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
String msg, RecognitionException e)
{
if (parserException != null)
{
return;
}
String errorSymbol = getOffendingSymbol(recognizer, offendingSymbol, charPositionInLine);
parserException =
new ParseException(
ErrorCode.SEMANTICANALYZE_PARSE_ERROR,
"You have an error in your CQL syntax; check the manual that corresponds to your Streaming version for the right syntax to use near '"
+ errorSymbol + "' at line " + line + ":" + charPositionInLine);
LOG.error(parserException.getMessage(), parserException);
}
示例12: syntaxError
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
/**
* Syntax error occurred. Add the error to the list of parse issues.
*/
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
String msg, RecognitionException e) {
parseIssues.add(new ParseIssue(line, charPositionInLine, msg, currentFileName, ParseIssueType.SYNTAX_ERROR));
try {
setAtLeastOneError(true);
// Print error messages with file name
if (currentFileName == null)
log.error("line " + line + ":" + charPositionInLine + " " + msg);
else {
String fileName = currentFileName;
log.error(fileName + " line " + line + ":" + charPositionInLine + " " + msg);
}
} catch (Exception e1) {
log.error("ERROR: while customizing error message:" + e1);
}
}
示例13: syntaxError
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
/**
* Method, which overrides method from antlr package. This method, as an input, gets the offending
* mismatched symbol, location of the mismatch and the message returned by the antlr error listener.
* In our case the line is always 1.
* With this method we change the incoming error message to something more user friendly
*
* @param recognizer antlr parser
* @param offendingSymbol mismatched symbol
* @param line line of the mismatch
* @param charPositionInLine position of the character for the mismatch
* @param msg error message returned by antlr
* @param e antlr recognition exception
*/
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) {
errors = true;
charPositionInLine++;
if (msg.contains("extraneous input")) { //example: \declare{syntax={infix,7,"+",l}, meaning=artih1.sum, misc=&abc"}
String input = getInput(msg);
String expectedElements = getExpectedElements(msg);
msg = "Extraneous input: " + input + ", were expecting one of the following: " + expectedElements;
}
else if (msg.contains("no viable alternative at input")) { //example: \declare{syntax={infix,7,"+",l}, meaning=artih1.sum, misc={&abc"} (???)
msg = "Something missing from the declaration, possibly a brace or a key-value pair.";
}
else if (msg.contains("missing")) {
msg = msg.replaceAll("missing", "Missing character:");
}
msg = msg.replaceAll("WS", "whitespace");
msg = msg.replaceAll("<EOF>", "the end of the file");
Logger.log("Syntax error at character " + charPositionInLine + ": " + msg);
}
示例14: parse
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
/**
* Compile request to AST.
*
* @param path request
* @return AST parse tree
*/
public static ParseTree parse(String path) {
String normalizedPath = Paths.get(path).normalize().toString().replace(File.separatorChar, '/');
if (normalizedPath.startsWith("/")) {
normalizedPath = normalizedPath.substring(1);
}
ANTLRInputStream is = new ANTLRInputStream(normalizedPath);
CoreLexer lexer = new CoreLexer(is);
lexer.removeErrorListeners();
lexer.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) {
throw new ParseCancellationException(msg, e);
}
});
CoreParser parser = new CoreParser(new CommonTokenStream(lexer));
parser.setErrorHandler(new BailErrorStrategy());
return parser.start();
}
示例15: parse
import org.antlr.v4.runtime.Recognizer; //導入依賴的package包/類
/**
* Compile request to AST.
* @param path request
* @return AST
*/
public static ParseTree parse(String path) {
ANTLRInputStream is = new ANTLRInputStream(path);
CoreLexer lexer = new CoreLexer(is);
lexer.removeErrorListeners();
lexer.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) {
throw new ParseCancellationException(e);
}
});
CoreParser parser = new CoreParser(new CommonTokenStream(lexer));
parser.setErrorHandler(new BailErrorStrategy());
return parser.start();
}