2 * Copyright (c) 2020 PANTHEON.tech, 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.ir;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.common.AbstractQName;
18 * A YANG keyword, as defined, as defined by section 6.1.2 of both
19 * <a href="https://tools.ietf.org/html/rfc6020#section-6.1.2">RFC6020</a> and
20 * <a href="https://tools.ietf.org/html/rfc7950#section-6.1.2">RFC7950</a>. The two options are discerned by nullability
21 * of {@link #prefix()} method's return, as hinted by the ABNF for {@code node-identifier} -- and while a keyword is a
22 * semantically different construct, it shares the same value space.
25 * Naming in this class prefers the formal ABNF specification and draws value-space and type-safety implications from
26 * that connection, rather than following the RFC-assigned names.
29 public abstract class IRKeyword extends AbstractIRObject {
31 public static final class Qualified extends IRKeyword {
32 private final @NonNull String prefix;
34 Qualified(final String prefix, final String localName) {
36 this.prefix = requireNonNull(prefix);
40 public @NonNull String prefix() {
45 public String asStringDeclaration() {
46 return prefix + ':' + identifier();
50 StringBuilder toYangFragment(final StringBuilder sb) {
51 return sb.append(prefix).append(':').append(identifier());
56 public static final class Unqualified extends IRKeyword {
57 Unqualified(final String localName) {
62 public String prefix() {
67 public String asStringDeclaration() {
72 StringBuilder toYangFragment(final StringBuilder sb) {
73 return sb.append(identifier());
77 private final @NonNull String identifier;
79 IRKeyword(final String localName) {
80 this.identifier = requireNonNull(localName);
84 * This keyword's 'identifier' part. This corresponds to what the RFCs refer to as {@code YANG keyword} or as
85 * {@code language extension keyword}.
88 * Note the returned string is guaranteed to conform to rules of {@code identifier} ABNF and therefore
89 * is directly usable as a {@code localName} in an {@link AbstractQName}.
91 * @return This keyword's identifier part.
93 public final @NonNull String identifier() {
98 * This keyword's 'prefix' part. This corresponds to {@code prefix identifier}. For {@code YANG keyword}s this is
99 * null. For language extension references this is the non-null prefix which references the YANG module defining
100 * the language extension.
103 * Note the returned string, if non-null, is guaranteed to conform to rules of {@code identifier} ABNF and therefore
104 * is directly usable as a {@code localName} in an {@link AbstractQName}.
106 * @return This keyword's prefix, or null if this keyword references a YANG keyword.
108 public abstract @Nullable String prefix();
111 * Helper method to re-create the string which was used to declared this keyword.
113 * @return Declaration string.
115 public abstract @NonNull String asStringDeclaration();