Introduce yang-xpath-antlr
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import javax.xml.xpath.XPathExpressionException;
12 import org.antlr.v4.runtime.BaseErrorListener;
13 import org.antlr.v4.runtime.RecognitionException;
14 import org.antlr.v4.runtime.Recognizer;
15 import org.eclipse.jdt.annotation.Nullable;
16
17 final class CapturingErrorListener extends BaseErrorListener {
18     @SuppressFBWarnings(value = "NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR",
19             justification = "SB does not grok CDT")
20     private @Nullable XPathExpressionException error;
21
22     @Override
23     public void syntaxError(final @Nullable Recognizer<?, ?> recognizer, final @Nullable Object offendingSymbol,
24             final int line, final int charPositionInLine, final @Nullable String msg,
25             final @Nullable RecognitionException cause) {
26         final XPathExpressionException ex = Utils.wrapException(cause, "%s", msg);
27         if (error == null) {
28             error = ex;
29         } else {
30             error.addSuppressed(ex);
31         }
32     }
33
34     void reportError() throws XPathExpressionException {
35         if (error != null) {
36             throw error;
37         }
38     }
39 }