Add an explicit intermediate YANG representation
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / ir / IRStatement.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  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.parser.rfc7950.ir;
9
10 import static java.util.Objects.requireNonNull;
11
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;
18
19 /**
20  * A single YANG statement in its raw string form. A statement is composed of:
21  * <ul>
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>
25  * </ul>
26  */
27 @Beta
28 public abstract class IRStatement extends AbstractIRObject {
29     private final @NonNull IRKeyword keyword;
30     private final IRArgument argument;
31
32     IRStatement(final IRKeyword keyword, final IRArgument argument) {
33         this.keyword = requireNonNull(keyword);
34         this.argument = argument;
35     }
36
37     /**
38      * Create an {@link IRStatement} from a parsed {@link StatementContext}.
39      *
40      * @param context ANTLR statement context
41      * @return A new IRStatement
42      * @throws NullPointerException if {@code context} is null
43      */
44     public static @NonNull IRStatement forContext(final StatementContext context) {
45         return new StatementFactory().createStatement(context);
46     }
47
48     /**
49      * Return this statement's keyword.
50      *
51      * @return This statement's keyword.
52      */
53     public final @NonNull IRKeyword keyword() {
54         return keyword;
55     }
56
57     /**
58      * Return this statement's argument, if it is present.
59      *
60      * @return This statement's argument, or null if this statement does not have an argument
61      */
62     public final @Nullable IRArgument argument() {
63         return argument;
64     }
65
66     /**
67      * Return this statement's substatements.
68      *
69      * @return This statement's substatements.
70      */
71     public @NonNull List<? extends IRStatement> statements() {
72         return ImmutableList.of();
73     }
74
75     /**
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.
78      *
79      * @return Line number where this statement started in the source code.
80      */
81     public abstract int startLine();
82
83     /**
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.
86      *
87      * @return Column number where this statement started in the source code.
88      */
89     public abstract int startColumn();
90
91     @Override
92     final StringBuilder toYangFragment(final StringBuilder sb) {
93         sb.append(keyword);
94         if (argument != null) {
95             argument.toYangFragment(sb.append(' '));
96         }
97
98         final List<? extends IRStatement> statements = statements();
99         if (statements.isEmpty()) {
100             return sb.append(';');
101         }
102
103         sb.append(" {\n");
104         for (IRStatement stmt : statements) {
105             stmt.toYangFragment(sb).append('\n');
106         }
107         return sb.append('}');
108     }
109 }