Populate parser/ hierarchy
[yangtools.git] / parser / 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
18 /**
19  * A single YANG statement in its raw string form. A statement is composed of:
20  * <ul>
21  *   <li>a mandatory keyword, modeled as {@link IRKeyword}</li>
22  *   <li>an optional argument, modeled as {@link IRArgument}</li>
23  *   <li>zero or more nested statements</li>
24  * </ul>
25  */
26 @Beta
27 public abstract class IRStatement extends AbstractIRObject {
28     private final @NonNull IRKeyword keyword;
29     private final IRArgument argument;
30
31     IRStatement(final IRKeyword keyword, final IRArgument argument) {
32         this.keyword = requireNonNull(keyword);
33         this.argument = argument;
34     }
35
36     /**
37      * Return this statement's keyword.
38      *
39      * @return This statement's keyword.
40      */
41     public final @NonNull IRKeyword keyword() {
42         return keyword;
43     }
44
45     /**
46      * Return this statement's argument, if it is present.
47      *
48      * @return This statement's argument, or null if this statement does not have an argument
49      */
50     public final @Nullable IRArgument argument() {
51         return argument;
52     }
53
54     /**
55      * Return this statement's substatements.
56      *
57      * @return This statement's substatements.
58      */
59     public @NonNull List<? extends IRStatement> statements() {
60         return ImmutableList.of();
61     }
62
63     /**
64      * Return the line number on which this statement's keyword has its first character, counting from <b>1</b>. This
65      * information is used only for diagnostic purposes.
66      *
67      * @return Line number where this statement started in the source code.
68      */
69     public abstract int startLine();
70
71     /**
72      * Return the column number on which this statement's keyword has its first character, counting from <b>0</b>. This
73      * information is used only for diagnostic purposes.
74      *
75      * @return Column number where this statement started in the source code.
76      */
77     public abstract int startColumn();
78
79     @Override
80     final StringBuilder toYangFragment(final StringBuilder sb) {
81         keyword.toYangFragment(sb);
82         if (argument != null) {
83             argument.toYangFragment(sb.append(' '));
84         }
85
86         final List<? extends IRStatement> statements = statements();
87         if (statements.isEmpty()) {
88             return sb.append(';');
89         }
90
91         sb.append(" {\n");
92         for (IRStatement stmt : statements) {
93             stmt.toYangFragment(sb).append('\n');
94         }
95         return sb.append('}');
96     }
97 }