2 * Copyright (c) 2013 Cisco Systems, Inc. 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
9 package org.opendaylight.yangtools.yang.parser.impl;
11 import com.google.common.base.Optional;
12 import java.util.List;
13 import org.antlr.v4.runtime.ParserRuleContext;
14 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
16 public final class ParserListenerUtils {
18 private ParserListenerUtils() {
23 * Check this base type.
28 * name of current module
31 * @throws YangParseException
32 * if this is one of YANG type which MUST contain additional
33 * informations in its body
35 public static void checkMissingBody(final String typeName, final String moduleName, final int line) {
38 throw new YangParseException(moduleName, line,
39 "The 'fraction-digits' statement MUST be present if the type is 'decimal64'.");
41 throw new YangParseException(moduleName, line,
42 "The 'base' statement MUST be present if the type is 'identityref'.");
44 throw new YangParseException(moduleName, line,
45 "The 'path' statement MUST be present if the type is 'leafref'.");
47 throw new YangParseException(moduleName, line, "The 'bit' statement MUST be present if the type is 'bits'.");
49 throw new YangParseException(moduleName, line,
50 "The 'enum' statement MUST be present if the type is 'enumeration'.");
54 public static <T extends ParserRuleContext> Optional<T> getFirstContext(final ParserRuleContext context,final Class<T> contextType) {
55 List<T> potential = context.getRuleContexts(contextType);
56 if (potential.isEmpty()) {
57 return Optional.absent();
59 return Optional.of(potential.get(0));