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 com.google.common.collect.ImmutableList;
14 import java.util.List;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.StatementContext;
20 * A single YANG statement in its raw string form. A statement is composed of:
22 * <li>a mandatory keyword, modeled as {@link IRKeyword}</li>
23 * <li>an optional argument, modeled as {@link IRArgument}</li>
24 * <li>zero or more nested statements</li>
28 public abstract class IRStatement extends AbstractIRObject {
29 private final @NonNull IRKeyword keyword;
30 private final IRArgument argument;
32 IRStatement(final IRKeyword keyword, final IRArgument argument) {
33 this.keyword = requireNonNull(keyword);
34 this.argument = argument;
38 * Create an {@link IRStatement} from a parsed {@link StatementContext}.
40 * @param context ANTLR statement context
41 * @return A new IRStatement
42 * @throws NullPointerException if {@code context} is null
44 public static @NonNull IRStatement forContext(final StatementContext context) {
45 return new StatementFactory().createStatement(context);
49 * Return this statement's keyword.
51 * @return This statement's keyword.
53 public final @NonNull IRKeyword keyword() {
58 * Return this statement's argument, if it is present.
60 * @return This statement's argument, or null if this statement does not have an argument
62 public final @Nullable IRArgument argument() {
67 * Return this statement's substatements.
69 * @return This statement's substatements.
71 public @NonNull List<? extends IRStatement> statements() {
72 return ImmutableList.of();
76 * Return the line number on which this statement's keyword has its first character, counting from <b>1</b>. This
77 * information is used only for diagnostic purposes.
79 * @return Line number where this statement started in the source code.
81 public abstract int startLine();
84 * Return the column number on which this statement's keyword has its first character, counting from <b>0</b>. This
85 * information is used only for diagnostic purposes.
87 * @return Column number where this statement started in the source code.
89 public abstract int startColumn();
92 final StringBuilder toYangFragment(final StringBuilder sb) {
94 if (argument != null) {
95 argument.toYangFragment(sb.append(' '));
98 final List<? extends IRStatement> statements = statements();
99 if (statements.isEmpty()) {
100 return sb.append(';');
104 for (IRStatement stmt : statements) {
105 stmt.toYangFragment(sb).append('\n');
107 return sb.append('}');