/** Parse data or input file and initialize the parse tree. */
private void parseInput() {
if (data == null)
loadInputFile();
if (data == null)
throw new IllegalArgumentException("Unable to load input file or data is missing.");
ANTLRInputStream input = new ANTLRInputStream(data);
SaltLexer lexer = new SaltLexer(input);
lexer.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> arg0, Object arg1, int arg2,
int arg3, String arg4, RecognitionException arg5) {
throw new RuntimeException(arg5);
}
});
CommonTokenStream tokens = new CommonTokenStream(lexer);
SaltParser parser = new SaltParser(tokens);
tree = parser.document();
if (parser.getNumberOfSyntaxErrors() > 0) {
System.out.println("Syntax error in file " + inputFile);
return;
}
}
java类org.antlr.v4.runtime.Recognizer的实例源码
SaltProcessor.java 文件源码
项目:salt9000
阅读 16
收藏 0
点赞 0
评论 0
SaltTest.java 文件源码
项目:salt9000
阅读 19
收藏 0
点赞 0
评论 0
/**
* Parse the string passed as parameter returning the string representing the structure of the UI
* described by the parameter.
* @param document
* @return
* @throws Exception
*/
private String parse(String document) throws Exception {
ANTLRInputStream input = new ANTLRInputStream(document);
SaltLexer lexer = new SaltLexer(input);
// The lexer should not recover errors.
lexer.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> arg0, Object arg1, int arg2,
int arg3, String arg4, RecognitionException arg5) {
throw new RuntimeException(arg5);
}
});
CommonTokenStream tokens = new CommonTokenStream(lexer);
SaltParser parser = new SaltParser(tokens);
ParseTree tree = parser.document();
if (parser.getNumberOfSyntaxErrors() > 0) {
throw new SyntaxException("Syntax error into the document: " + document);
}
//
SaltTextVisitor visitor = new SaltTextVisitor(parser);
return visitor.visit(tree);
}
SimpleErrorListener.java 文件源码
项目:inflectible
阅读 17
收藏 0
点赞 0
评论 0
@Override
public void syntaxError(
final Recognizer<?, ?> recognizer,
final Object offending,
final int line,
final int position,
final String msg,
final RecognitionException exception
) {
super.syntaxError(
recognizer,
offending,
line,
position,
msg,
exception
);
throw new ParseCancellationException(exception);
}
JavadocDetailNodeParser.java 文件源码
项目:checkstyle
阅读 22
收藏 0
点赞 0
评论 0
/**
* 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);
}
}
DescriptiveErrorListener.java 文件源码
项目:visual-programming
阅读 23
收藏 0
点赞 0
评论 0
@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!=null) {
sourceName = String.format("%s:%d:%d: ", sourceName, line, charPositionInLine);
}
StringBuilder sb = new StringBuilder();
if(sourceName!=null)
sb.append(sourceName);
sb.append("line "+line+":"+charPositionInLine+" "+msg);
String errMsg = sb.toString();
//System.err.println(errMsg);
throw new RuntimeException(errMsg);
}
ParserErrorListener.java 文件源码
项目:freelib-edtf
阅读 42
收藏 0
点赞 0
评论 0
public void syntaxError(Recognizer<?, ?> aRecognizer,
Object aOffendingSymbol, int aLine, int aCharIndex,
String aMessage, RecognitionException aException) {
ANTLRErrorStrategy errorHandler = myParser.getErrorHandler();
if (LOGGER.isWarnEnabled()) {
LOGGER.warn(aMessage + " [" + aLine + ":" + aCharIndex + "]");
}
/*
* Setting the lexer exception in the parser since I don't see a
* getNumberOfSyntaxErrors() method in the lexer (like in antlr3) and
* the lexer's errors aren't being reported by parser's method
*
* I may just be missing the correct way this should be handled(?)
*/
if (aException instanceof LexerNoViableAltException) {
NoViableAltException exception = new NoViableAltException(myParser);
errorHandler.reportError(myParser, exception);
}
else {
errorHandler.reportError(myParser, aException);
}
}
WorkspacePresentation.java 文件源码
项目:Ultrastructure
阅读 18
收藏 0
点赞 0
评论 0
public static WorkspaceContext getWorkspaceContext(InputStream source) throws IOException {
WorkspaceLexer l = new WorkspaceLexer(CharStreams.fromStream(source));
WorkspaceParser p = new WorkspaceParser(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);
}
});
WorkspaceContext ctx = p.workspace();
return ctx;
}
TestParse.java 文件源码
项目:Ultrastructure
阅读 33
收藏 0
点赞 0
评论 0
@Test
public void testParse() throws Exception {
WorkspaceLexer l = new WorkspaceLexer(CharStreams.fromStream(getClass().getResourceAsStream("/thing.wsp")));
WorkspaceParser p = new WorkspaceParser(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);
}
});
p.workspace();
}
Spa.java 文件源码
项目:Ultrastructure
阅读 18
收藏 0
点赞 0
评论 0
public static Spa manifest(String resource) throws IOException {
SpaLexer l = new SpaLexer(CharStreams.fromStream(Utils.resolveResource(Spa.class,
resource)));
SpaParser p = new SpaParser(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);
}
});
SpaContext spa = p.spa();
SpaImporter importer = new SpaImporter();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(importer, spa);
return importer.getSpa();
}
SGFErrorListener.java 文件源码
项目:Suji
阅读 20
收藏 0
点赞 0
评论 0
@Override
public void syntaxError(Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e) {
output.write(msg);
output.write("\n");
if ( offendingSymbol instanceof org.antlr.v4.runtime.CommonToken )
symbol = ((CommonToken) offendingSymbol).getText();
}