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.stmt.reactor;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Collection;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.ImplicitSubstatement;
25 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter.ResumedStatement;
29 * Intermediate subclass of StatementContextBase facing the parser stream via implementation of ResumedStatement. This
30 * shields inference-type substatements from these details.
32 * @param <A> Argument type
33 * @param <D> Declared Statement representation
34 * @param <E> Effective Statement representation
36 abstract class AbstractResumedStatement<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
37 extends StatementContextBase<A, D, E> implements ResumedStatement {
38 private final @NonNull StatementSourceReference statementDeclSource;
39 private final StmtContext<?, ?, ?> originalCtx;
40 private final StmtContext<?, ?, ?> prevCopyCtx;
41 private final String rawArgument;
43 private StatementMap substatements = StatementMap.empty();
45 AbstractResumedStatement(final AbstractResumedStatement<A, D, E> original) {
47 this.statementDeclSource = original.statementDeclSource;
48 this.rawArgument = original.rawArgument;
49 this.originalCtx = original.getOriginalCtx().orElse(original);
50 this.prevCopyCtx = original;
51 this.substatements = original.substatements;
54 AbstractResumedStatement(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
55 final String rawArgument) {
57 this.statementDeclSource = requireNonNull(ref);
58 this.rawArgument = def.internArgument(rawArgument);
59 this.originalCtx = null;
60 this.prevCopyCtx = null;
63 AbstractResumedStatement(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
64 final String rawArgument, final CopyType copyType) {
65 super(def, CopyHistory.of(copyType, CopyHistory.original()));
66 this.statementDeclSource = requireNonNull(ref);
67 this.rawArgument = rawArgument;
68 this.originalCtx = null;
69 this.prevCopyCtx = null;
73 public final Optional<StmtContext<?, ?, ?>> getOriginalCtx() {
74 return Optional.ofNullable(originalCtx);
78 public final Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx() {
79 return Optional.ofNullable(prevCopyCtx);
83 public final StatementSourceReference getStatementSourceReference() {
84 return statementDeclSource;
88 public final String rawStatementArgument() {
93 public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
94 return substatements.values();
98 public @NonNull StatementDefinition getDefinition() {
99 return getPublicDefinition();
103 public @NonNull StatementSourceReference getSourceReference() {
104 return getStatementSourceReference();
108 public boolean isFullyDefined() {
109 return fullyDefined();
113 * Create a new substatement at the specified offset.
115 * @param offset Substatement offset
116 * @param def definition context
117 * @param ref source reference
118 * @param argument statement argument
119 * @param <X> new substatement argument type
120 * @param <Y> new substatement declared type
121 * @param <Z> new substatement effective type
122 * @return A new substatement
124 @SuppressWarnings("checkstyle:methodTypeParameterName")
125 final <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>>
126 AbstractResumedStatement<X, Y, Z> createSubstatement(final int offset,
127 final StatementDefinitionContext<X, Y, Z> def, final StatementSourceReference ref,
128 final String argument) {
129 final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase();
130 checkState(inProgressPhase != ModelProcessingPhase.EFFECTIVE_MODEL,
131 "Declared statement cannot be added in effective phase at: %s", getStatementSourceReference());
133 final Optional<StatementSupport<?, ?, ?>> implicitParent =
134 definition().getImplicitParentFor(def.getPublicView());
135 if (implicitParent.isPresent()) {
136 return createImplicitParent(offset, implicitParent.get(), ref, argument).createSubstatement(offset, def,
140 final AbstractResumedStatement<X, Y, Z> ret = new SubstatementContext<>(this, def, ref, argument);
141 substatements = substatements.put(offset, ret);
142 def.onStatementAdded(ret);
147 * Lookup substatement by its offset in this statement.
149 * @param offset Substatement offset
150 * @return Substatement, or null if substatement does not exist.
152 final AbstractResumedStatement<?, ?, ?> lookupSubstatement(final int offset) {
153 return substatements.get(offset);
156 final void resizeSubstatements(final int expectedSize) {
157 substatements = substatements.ensureCapacity(expectedSize);
160 final void walkChildren(final ModelProcessingPhase phase) {
161 checkState(isFullyDefined());
162 substatements.values().forEach(stmt -> {
163 stmt.walkChildren(phase);
164 stmt.endDeclared(phase);
168 private AbstractResumedStatement<?, ?, ?> createImplicitParent(final int offset,
169 final StatementSupport<?, ?, ?> implicitParent, final StatementSourceReference ref, final String argument) {
170 final StatementDefinitionContext<?, ?, ?> def = new StatementDefinitionContext<>(implicitParent);
171 return createSubstatement(offset, def, ImplicitSubstatement.of(ref), argument);