Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-xpath-impl / src / main / java / org / opendaylight / yangtools / yang / xpath / impl / CapturingErrorListener.java
1 /*
2  * Copyright (c) 2019 Pantheon Technologies, s.r.o.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.xpath.impl;
9
10 import javax.xml.xpath.XPathExpressionException;
11 import org.antlr.v4.runtime.BaseErrorListener;
12 import org.antlr.v4.runtime.RecognitionException;
13 import org.antlr.v4.runtime.Recognizer;
14 import org.eclipse.jdt.annotation.Nullable;
15
16 final class CapturingErrorListener extends BaseErrorListener {
17     private @Nullable XPathExpressionException error;
18
19     @Override
20     public void syntaxError(final @Nullable Recognizer<?, ?> recognizer, final @Nullable Object offendingSymbol,
21             final int line, final int charPositionInLine, final @Nullable String msg,
22             final @Nullable RecognitionException cause) {
23         final XPathExpressionException ex = Utils.wrapException(cause, "%s", msg);
24         if (error == null) {
25             error = ex;
26         } else {
27             error.addSuppressed(ex);
28         }
29     }
30
31     void reportError() throws XPathExpressionException {
32         if (error != null) {
33             throw error;
34         }
35     }
36 }