use of com.linkedin.data.grammar.PdlLexer in project rest.li by linkedin.
the class PdlSchemaParser method parse.
/**
* Parse a JSON representation of a schema from a {{java.io.Reader}}.
*
* The top level {{DataSchema}}'s parsed are in {{#topLevelDataSchemas}}.
* These are the types that are not defined within other types.
* Parse errors are in {{#errorMessageBuilder}} and indicated
* by {{#hasError()}}.
*
* @param reader with the JSON representation of the schema.
*/
public void parse(Reader reader) {
try {
ErrorRecorder errorRecorder = new ErrorRecorder();
PdlLexer lexer;
try {
lexer = new PdlLexer(new ANTLRInputStream(reader));
} catch (IOException e) {
ParseError error = new ParseError(new ParseErrorLocation(0, 0), e.getMessage());
startErrorMessage(error).append(error.message).append(NEWLINE);
return;
}
lexer.removeErrorListeners();
lexer.addErrorListener(errorRecorder);
PdlParser parser = new PdlParser(new CommonTokenStream(lexer));
parser.removeErrorListeners();
parser.addErrorListener(errorRecorder);
DocumentContext antlrDocument = parser.document();
parse(antlrDocument);
if (errorRecorder.errors.size() > 0) {
for (ParseError error : errorRecorder.errors) {
startErrorMessage(error).append(error.message).append(NEWLINE);
}
}
} catch (ParseException e) {
startErrorMessage(e.error).append(e.getMessage()).append(NEWLINE);
} catch (Throwable t) {
ParseError parseError = new ParseError(new ParseErrorLocation(0, 0), null);
startErrorMessage(parseError).append("Unexpected parser error: ").append(ExceptionUtils.getStackTrace(t)).append(NEWLINE);
}
}
Aggregations