2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.antlr;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import java.util.function.Supplier;
14 import javax.annotation.concurrent.NotThreadSafe;
15 import org.antlr.v4.runtime.Lexer;
16 import org.antlr.v4.runtime.Parser;
17 import org.antlr.v4.runtime.RecognitionException;
18 import org.antlr.v4.runtime.Recognizer;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
25 * Utility class for converting ANTLRErrorListener errors to SourceExceptions.
27 * @author Robert Varga
32 public final class SourceExceptionParser {
33 private static final class Listener extends AbstractParserErrorListener<SourceException> {
34 private final StatementSourceReference ref;
36 private Listener(final StatementSourceReference ref) {
37 this.ref = requireNonNull(ref);
41 protected SourceException createException(final Recognizer<?, ?> recognizer,
42 final @Nullable Object offendingSymbol, final int line, final int charPositionInLine,
43 final String msg, final @Nullable RecognitionException cause) {
44 return new SourceException(ref, cause, "%s at %s:%s", msg, line, charPositionInLine);
48 private SourceExceptionParser() {
53 * Parse a Recognizer extracting its root item.
55 * @param recognizer Recognizer to use
56 * @param parseMethod Root item extractor method
57 * @param ref Source reference
59 * @throws NullPointerException if any argument is null
60 * @throws SourceException if a parser error occurs
62 public static <T> T parse(final Recognizer<?, ?> recognizer, final Supplier<T> parseMethod,
63 final StatementSourceReference ref) {
64 final Listener listener = new Listener(ref);
65 recognizer.removeErrorListeners();
66 recognizer.addErrorListener(listener);
68 final T ret = parseMethod.get();
74 * Use a Lexer/Parser pair extracting the parser's root item.
76 * @param lexer lexer to use
77 * @param parser parser to use
78 * @param parseMethod Root item extractor method
79 * @param ref Source reference
81 * @throws NullPointerException if any argument is null
82 * @throws SourceException if a parser error occurs
84 public static <T> T parse(final Lexer lexer, final Parser parser, final Supplier<T> parseMethod,
85 final StatementSourceReference ref) {
86 final Listener listener = new Listener(ref);
87 lexer.removeErrorListeners();
88 lexer.addErrorListener(listener);
89 parser.removeErrorListeners();
90 parser.addErrorListener(listener);
92 final T ret = parseMethod.get();