Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.parser.impl.parserutils; |
| 18 | |
| 19 | import org.antlr.v4.runtime.BaseErrorListener; |
| 20 | import org.antlr.v4.runtime.RecognitionException; |
| 21 | import org.antlr.v4.runtime.Recognizer; |
| 22 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 23 | |
| 24 | /** |
| 25 | * By default, ANTLR sends all errors to standard error, this is changed by |
| 26 | * providing this new implementation of interface ANTLRErrorListener. The |
| 27 | * interface has a syntaxError() method that applies to both lexer and |
| 28 | * parser. |
| 29 | */ |
| 30 | public class ParseTreeErrorListener extends BaseErrorListener { |
| 31 | |
| 32 | // Exception of type parser exceptions are catched during parsing. |
| 33 | private ParserException parserException = new ParserException(); |
| 34 | |
| 35 | // Flag to indicate presence of exception. |
| 36 | private boolean exceptionFlag = false; |
| 37 | |
| 38 | /** |
| 39 | * Returns the status of exception flag. |
| 40 | * |
| 41 | * @return flag which contains the status of exception. |
| 42 | */ |
| 43 | public boolean isExceptionFlag() { |
| 44 | return exceptionFlag; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns the parser exception object populated with line, character |
| 49 | * position and message. |
| 50 | * |
| 51 | * @return object of parser exception. |
| 52 | */ |
| 53 | public ParserException getParserException() { |
| 54 | return parserException; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, |
| 59 | String msg, RecognitionException e) { |
| 60 | parserException.setLine(line); |
| 61 | parserException.setCharPosition(charPositionInLine); |
| 62 | parserException.setMsg(msg); |
| 63 | exceptionFlag = true; |
| 64 | } |
| 65 | } |