Use switch with patterns in YangInstanceIdentifierWriter
[yangtools.git] / parser / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextUtils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.spi.meta;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Strings;
13 import com.google.common.base.VerifyException;
14 import com.google.common.collect.ImmutableList;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Optional;
18 import java.util.Set;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.Empty;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.common.YangVersion;
25 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
28 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
29 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureExpr;
30 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
39 import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement;
40 import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
45 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
46
47 public final class StmtContextUtils {
48     private StmtContextUtils() {
49         // Hidden on purpose
50     }
51
52     @SuppressWarnings("unchecked")
53     public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(
54             final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<D> declaredType) {
55         for (var ctx : contexts) {
56             if (ctx.producesDeclared(declaredType)) {
57                 return (A) ctx.argument();
58             }
59         }
60         return null;
61     }
62
63     @SuppressWarnings("unchecked")
64     public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(final StmtContext<?, ?, ?> ctx,
65             final Class<D> declaredType) {
66         return ctx.producesDeclared(declaredType) ? (A) ctx.argument() : null;
67     }
68
69     public static <A, D extends DeclaredStatement<A>> A firstSubstatementAttributeOf(
70             final StmtContext<?, ?, ?> ctx, final Class<D> declaredType) {
71         return firstAttributeOf(ctx.allSubstatements(), declaredType);
72     }
73
74     @SuppressWarnings("unchecked")
75     public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstDeclaredSubstatement(
76             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
77         for (var subStmtContext : stmtContext.declaredSubstatements()) {
78             if (subStmtContext.producesDeclared(declaredType)) {
79                 return (StmtContext<A, ?, ?>) subStmtContext;
80             }
81         }
82         return null;
83     }
84
85     @SafeVarargs
86     public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
87             int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
88         if (startIndex >= types.length) {
89             return null;
90         }
91
92         for (var subStmtContext : stmtContext.declaredSubstatements()) {
93             if (subStmtContext.producesDeclared(types[startIndex])) {
94                 return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext,
95                         ++startIndex, types);
96             }
97         }
98         return null;
99     }
100
101     @SuppressWarnings("unchecked")
102     public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllDeclaredSubstatements(
103             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
104         final var listBuilder = ImmutableList.<StmtContext<A, D, ?>>builder();
105         for (var subStmtContext : stmtContext.declaredSubstatements()) {
106             if (subStmtContext.producesDeclared(declaredType)) {
107                 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
108             }
109         }
110         return listBuilder.build();
111     }
112
113     @SuppressWarnings("unchecked")
114     public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllEffectiveSubstatements(
115             final StmtContext<?, ?, ?> stmtContext, final Class<D> type) {
116         final var listBuilder = ImmutableList.<StmtContext<A, D, ?>>builder();
117         for (var subStmtContext : stmtContext.effectiveSubstatements()) {
118             if (subStmtContext.producesDeclared(type)) {
119                 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
120             }
121         }
122         return listBuilder.build();
123     }
124
125     public static <A, D extends DeclaredStatement<A>> Collection<StmtContext<A, D, ?>> findAllSubstatements(
126             final StmtContext<?, ?, ?> stmtContext, final Class<D> type) {
127         return ImmutableList.<StmtContext<A, D, ?>>builder()
128             .addAll(findAllDeclaredSubstatements(stmtContext, type))
129             .addAll(findAllEffectiveSubstatements(stmtContext, type))
130             .build();
131     }
132
133     @SuppressWarnings("unchecked")
134     public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstEffectiveSubstatement(
135             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
136         for (var subStmtContext : stmtContext.effectiveSubstatements()) {
137             if (subStmtContext.producesDeclared(declaredType)) {
138                 return (StmtContext<A, ?, ?>) subStmtContext;
139             }
140         }
141         return null;
142     }
143
144     public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
145             final StmtContext<?, ?, ?> stmtContext, final Class<? super D> declaredType, int sublevel) {
146         for (var subStmtContext : stmtContext.declaredSubstatements()) {
147             if (sublevel == 1 && subStmtContext.producesDeclared(declaredType)) {
148                 return subStmtContext;
149             }
150             if (sublevel > 1) {
151                 final var result = findFirstDeclaredSubstatementOnSublevel(subStmtContext, declaredType, --sublevel);
152                 if (result != null) {
153                     return result;
154                 }
155             }
156         }
157
158         return null;
159     }
160
161     public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
162             final StmtContext<?, ?, ?> stmtContext, final Class<? super D> declaredType) {
163         for (final var subStmtContext : stmtContext.declaredSubstatements()) {
164             if (subStmtContext.producesDeclared(declaredType)) {
165                 return subStmtContext;
166             }
167
168             final var result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
169             if (result != null) {
170                 return result;
171             }
172         }
173
174         return null;
175     }
176
177     // FIXME: 8.0.0: This method goes back as far as YANGTOOLS-365, when we were build EffectiveStatements for
178     //               unsupported YANG extensions. We are not doing that anymore, do we still need this method? Also, it
179     //               is only used in augment support to disable mechanics on unknown nodes.
180     //
181     //               It would seem we can move this method to AbstractAugmentStatementSupport at the very least, but
182     //               also: augments are defined to operate on schema tree nodes, hence even if we have an
183     //               UnknownStatement, but its EffectiveStatement projection supports SchemaTreeAwareEffectiveStatement
184     //               we should operate normally -- the StatementSupport exposing such semantics is responsible for
185     //               arranging the backend details.
186     public static boolean isInExtensionBody(final StmtContext<?, ?, ?> stmtCtx) {
187         var current = stmtCtx;
188
189         while (true) {
190             final var parent = current.coerceParentContext();
191             if (parent.getParentContext() == null) {
192                 return false;
193             }
194             if (isUnknownStatement(parent)) {
195                 return true;
196             }
197             current = parent;
198         }
199     }
200
201     /**
202      * Returns true if supplied statement context represents unknown statement,
203      * otherwise returns false.
204      *
205      * @param stmtCtx
206      *            statement context to be checked
207      * @return true if supplied statement context represents unknown statement,
208      *         otherwise false
209      * @throws NullPointerException
210      *             if supplied statement context is null
211      */
212     public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
213         return UnknownStatement.class.isAssignableFrom(stmtCtx.publicDefinition().getDeclaredRepresentationClass());
214     }
215
216     /**
217      * Evaluate {@code if-feature} substatement of a statement and indicate whether they result in the statement being
218      * supported.
219      *
220      * @param stmt Parent statement
221      * @return {@code true} if the statement is indicated to be supported under currently-supported features
222      */
223     public static boolean evaluateIfFeatures(final @NonNull StmtContext<?, ?, ?> stmt) {
224         final var supportedFeatures = stmt.namespaceItem(ParserNamespaces.SUPPORTED_FEATURES, Empty.value());
225         return supportedFeatures == null || checkFeatureSupport(stmt, supportedFeatures);
226     }
227
228     public static boolean checkFeatureSupport(final StmtContext<?, ?, ?> stmtContext,
229             final FeatureSet supportedFeatures) {
230         boolean isSupported = false;
231         boolean containsIfFeature = false;
232         for (var stmt : stmtContext.declaredSubstatements()) {
233             if (YangStmtMapping.IF_FEATURE.equals(stmt.publicDefinition())) {
234                 containsIfFeature = true;
235                 final var argument = (IfFeatureExpr) stmt.getArgument();
236                 if (argument.test(supportedFeatures)) {
237                     isSupported = true;
238                 } else {
239                     isSupported = false;
240                     break;
241                 }
242             }
243         }
244
245         return !containsIfFeature || isSupported;
246     }
247
248     /**
249      * Checks whether statement context is a presence container or not.
250      *
251      * @param stmtCtx
252      *            statement context
253      * @return true if it is a presence container
254      */
255     public static boolean isPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
256         return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx);
257     }
258
259     /**
260      * Checks whether statement context is a non-presence container or not.
261      *
262      * @param stmtCtx
263      *            statement context
264      * @return true if it is a non-presence container
265      */
266     public static boolean isNonPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
267         return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx);
268     }
269
270     private static boolean containsPresenceSubStmt(final StmtContext<?, ?, ?> stmtCtx) {
271         return stmtCtx.hasSubstatement(PresenceEffectiveStatement.class);
272     }
273
274     /**
275      * Checks whether statement context is a mandatory leaf, choice, anyxml,
276      * list or leaf-list according to RFC6020 or not.
277      *
278      * @param stmtCtx
279      *            statement context
280      * @return true if it is a mandatory leaf, choice, anyxml, list or leaf-list
281      *         according to RFC6020.
282      */
283     public static boolean isMandatoryNode(final StmtContext<?, ?, ?> stmtCtx) {
284         if (stmtCtx.publicDefinition() instanceof YangStmtMapping mapping) {
285             return switch (mapping) {
286                 case LEAF, CHOICE, ANYXML -> Boolean.TRUE.equals(
287                     firstSubstatementAttributeOf(stmtCtx, MandatoryStatement.class));
288                 case LIST, LEAF_LIST -> {
289                     final Integer minElements = firstSubstatementAttributeOf(stmtCtx, MinElementsStatement.class);
290                     yield minElements != null && minElements > 0;
291                 }
292                 default -> false;
293             };
294         }
295         return false;
296     }
297
298     /**
299      * Checks whether a statement context is a statement of supplied statement
300      * definition and whether it is not mandatory leaf, choice, anyxml, list or
301      * leaf-list according to RFC6020.
302      *
303      * @param stmtCtx
304      *            statement context
305      * @param stmtDef
306      *            statement definition
307      * @return true if supplied statement context is a statement of supplied
308      *         statement definition and if it is not mandatory leaf, choice,
309      *         anyxml, list or leaf-list according to RFC6020
310      */
311     public static boolean isNotMandatoryNodeOfType(final StmtContext<?, ?, ?> stmtCtx,
312             final StatementDefinition stmtDef) {
313         return stmtCtx.publicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx);
314     }
315
316     /**
317      * Checks whether at least one ancestor of a StatementContext matches one from a collection of statement
318      * definitions.
319      *
320      * @param stmt Statement context to be checked
321      * @param ancestorTypes collection of statement definitions
322      * @return true if at least one ancestor of a StatementContext matches one
323      *         from collection of statement definitions, otherwise false.
324      */
325     public static boolean hasAncestorOfType(final StmtContext<?, ?, ?> stmt,
326             final Collection<StatementDefinition> ancestorTypes) {
327         requireNonNull(ancestorTypes);
328         var current = stmt.getParentContext();
329         while (current != null) {
330             if (ancestorTypes.contains(current.publicDefinition())) {
331                 return true;
332             }
333             current = current.getParentContext();
334         }
335         return false;
336     }
337
338     /**
339      * Check whether all of StmtContext's {@code list} ancestors have a {@code key}.
340      *
341      * @param stmt EffectiveStmtCtx to be checked
342      * @param name Human-friendly statement name
343      * @throws SourceException if there is any keyless list ancestor
344      */
345     public static void validateNoKeylessListAncestorOf(final Mutable<?, ?, ?> stmt, final String name) {
346         requireNonNull(stmt);
347
348         // We do not expect this to by typically populated
349         final var incomplete = new ArrayList<Mutable<?, ?, ?>>(0);
350
351         var current = stmt.coerceParentContext();
352         var parent = current.getParentContext();
353         while (parent != null) {
354             if (YangStmtMapping.LIST == current.publicDefinition()
355                     && !current.hasSubstatement(KeyEffectiveStatement.class)) {
356                 if (ModelProcessingPhase.FULL_DECLARATION.isCompletedBy(current.getCompletedPhase())) {
357                     throw new SourceException(stmt, "%s %s is defined within a list that has no key statement", name,
358                         stmt.argument());
359                 }
360
361                 // Ancestor has not completed full declaration yet missing 'key' statement may materialize later
362                 incomplete.add(current);
363             }
364
365             current = parent;
366             parent = current.getParentContext();
367         }
368
369         // Deal with whatever incomplete ancestors we encountered
370         for (var ancestor : incomplete) {
371             // This check must complete during the ancestor's FULL_DECLARATION phase, i.e. the ancestor must not reach
372             // EFFECTIVE_MODEL until it is done.
373             final var action = ancestor.newInferenceAction(ModelProcessingPhase.FULL_DECLARATION);
374             action.apply(new InferenceAction() {
375                 @Override
376                 public void apply(final InferenceContext ctx) {
377                     if (!ancestor.hasSubstatement(KeyEffectiveStatement.class)) {
378                         throw new SourceException(stmt, "%s %s is defined within a list that has no key statement",
379                             name, stmt.argument());
380                     }
381                 }
382
383                 @Override
384                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
385                     throw new VerifyException("Should never happen");
386                 }
387             });
388         }
389     }
390
391     /**
392      * Checks whether the parent of StmtContext is of specified type.
393      *
394      * @param ctx StmtContext to be checked
395      * @param parentType type of parent to check
396      * @return true if the parent of StmtContext is of specified type, otherwise false
397      */
398     public static boolean hasParentOfType(final StmtContext<?, ?, ?> ctx, final StatementDefinition parentType) {
399         requireNonNull(parentType);
400         final var parentContext = ctx.getParentContext();
401         return parentContext != null && parentType.equals(parentContext.publicDefinition());
402     }
403
404     /**
405      * Validates the specified statement context with regards to if-feature and when statement on list keys.
406      * The context can either be a leaf which is defined directly in the substatements of a keyed list or a uses
407      * statement defined in a keyed list (a uses statement may add leaves into the list).
408      *
409      * <p>
410      * If one of the list keys contains an if-feature or a when statement in YANG 1.1 model, an exception is thrown.
411      *
412      * @param ctx statement context to be validated
413      */
414     public static void validateIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> ctx) {
415         if (!isRelevantForIfFeatureAndWhenOnListKeysCheck(ctx)) {
416             return;
417         }
418
419         final var listStmtCtx = ctx.coerceParentContext();
420         final var keyStmtCtx = findFirstDeclaredSubstatement(listStmtCtx, KeyStatement.class);
421
422         if (YangStmtMapping.LEAF.equals(ctx.publicDefinition())) {
423             if (isListKey(ctx, keyStmtCtx)) {
424                 disallowIfFeatureAndWhenOnListKeys(ctx);
425             }
426         } else if (YangStmtMapping.USES.equals(ctx.publicDefinition())) {
427             findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> {
428                 if (isListKey(leafStmtCtx, keyStmtCtx)) {
429                     disallowIfFeatureAndWhenOnListKeys(leafStmtCtx);
430                 }
431             });
432         }
433     }
434
435     private static boolean isRelevantForIfFeatureAndWhenOnListKeysCheck(final StmtContext<?, ?, ?> ctx) {
436         return YangVersion.VERSION_1_1.equals(ctx.yangVersion()) && hasParentOfType(ctx, YangStmtMapping.LIST)
437                 && findFirstDeclaredSubstatement(ctx.coerceParentContext(), KeyStatement.class) != null;
438     }
439
440     private static boolean isListKey(final StmtContext<?, ?, ?> leafStmtCtx,
441             final StmtContext<Set<QName>, ?, ?> keyStmtCtx) {
442         return keyStmtCtx.getArgument().contains(leafStmtCtx.argument());
443     }
444
445     private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> leafStmtCtx) {
446         leafStmtCtx.allSubstatements().forEach(leafSubstmtCtx -> {
447             final var statementDef = leafSubstmtCtx.publicDefinition();
448             SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef)
449                     || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx,
450                     "%s statement is not allowed in %s leaf statement which is specified as a list key.",
451                     statementDef.getStatementName(), leafStmtCtx.argument());
452         });
453     }
454
455     public static @NonNull QName qnameFromArgument(StmtContext<?, ?, ?> ctx, final String value) {
456         if (Strings.isNullOrEmpty(value)) {
457             return ctx.publicDefinition().getStatementName();
458         }
459
460         QNameModule qnameModule;
461         String localName;
462         final var namesParts = value.split(":");
463         switch (namesParts.length) {
464             case 1:
465                 localName = namesParts[0];
466                 qnameModule = getModuleQName(ctx);
467                 break;
468             default:
469                 final var prefix = namesParts[0];
470                 localName = namesParts[1];
471                 qnameModule = getModuleQNameByPrefix(ctx, prefix);
472                 // in case of unknown statement argument, we're not going to parse it
473                 if (qnameModule == null && isUnknownStatement(ctx)) {
474                     localName = value;
475                     qnameModule = getModuleQName(ctx);
476                 }
477                 if (qnameModule == null && ctx.history().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
478                     ctx = ctx.getOriginalCtx().orElse(null);
479                     qnameModule = getModuleQNameByPrefix(ctx, prefix);
480                 }
481         }
482
483         return internedQName(ctx, InferenceException.throwIfNull(qnameModule, ctx,
484             "Cannot resolve QNameModule for '%s'", value), localName);
485     }
486
487     /**
488      * Parse a YANG identifier string in context of a statement.
489      *
490      * @param ctx Statement context
491      * @param str String to be parsed
492      * @return An interned QName
493      * @throws NullPointerException if any of the arguments are null
494      * @throws SourceException if the string is not a valid YANG identifier
495      */
496     public static @NonNull QName parseIdentifier(final @NonNull StmtContext<?, ?, ?> ctx, final String str) {
497         SourceException.throwIf(str.isEmpty(), ctx, "Identifier may not be an empty string");
498         return internedQName(ctx, str);
499     }
500
501     public static @NonNull QName parseNodeIdentifier(final @NonNull StmtContext<?, ?, ?> ctx, final String prefix,
502             final String localName) {
503         return internedQName(ctx,
504             InferenceException.throwIfNull(getModuleQNameByPrefix(ctx, prefix), ctx,
505                 "Cannot resolve QNameModule for '%s'", prefix),
506             localName);
507     }
508
509     /**
510      * Parse a YANG node identifier string in context of a statement.
511      *
512      * @param ctx Statement context
513      * @param str String to be parsed
514      * @return An interned QName
515      * @throws NullPointerException if any of the arguments are null
516      * @throws SourceException if the string is not a valid YANG node identifier
517      */
518     public static @NonNull QName parseNodeIdentifier(final @NonNull StmtContext<?, ?, ?> ctx, final String str) {
519         SourceException.throwIf(str.isEmpty(), ctx, "Node identifier may not be an empty string");
520
521         final int colon = str.indexOf(':');
522         if (colon == -1) {
523             return internedQName(ctx, str);
524         }
525
526         final var prefix = str.substring(0, colon);
527         SourceException.throwIf(prefix.isEmpty(), ctx, "String '%s' has an empty prefix", str);
528         final var localName = str.substring(colon + 1);
529         SourceException.throwIf(localName.isEmpty(), ctx, "String '%s' has an empty identifier", str);
530
531         return parseNodeIdentifier(ctx, prefix, localName);
532     }
533
534     private static @NonNull QName internedQName(final @NonNull StmtContext<?, ?, ?> ctx, final String localName) {
535         return internedQName(ctx, getModuleQName(ctx), localName);
536     }
537
538     private static @NonNull QName internedQName(final @NonNull CommonStmtCtx ctx, final QNameModule module,
539             final String localName) {
540         final QName template;
541         try {
542             template = QName.create(module, localName);
543         } catch (IllegalArgumentException e) {
544             throw new SourceException(ctx, e, "Invalid identifier '%s'", localName);
545         }
546         return template.intern();
547     }
548
549     public static @NonNull QNameModule getModuleQName(final @NonNull StmtContext<?, ?, ?> ctx) {
550         return getModuleQName(ctx.getRoot());
551     }
552
553     public static @NonNull QNameModule getModuleQName(final @NonNull RootStmtContext<?, ?, ?> ctx) {
554         if (ctx.producesDeclared(ModuleStatement.class)) {
555             return lookupModuleQName(ctx, ctx);
556         } else if (ctx.producesDeclared(SubmoduleStatement.class)) {
557             final var belongsTo = ctx.namespace(ParserNamespaces.BELONGSTO_PREFIX_TO_MODULECTX);
558             if (belongsTo == null || belongsTo.isEmpty()) {
559                 throw new IllegalArgumentException(ctx + " does not have belongs-to linkage resolved");
560             }
561             return lookupModuleQName(ctx, belongsTo.values().iterator().next());
562         } else {
563             throw new IllegalArgumentException("Unsupported root " + ctx);
564         }
565     }
566
567     private static @NonNull QNameModule lookupModuleQName(final NamespaceStmtCtx storage,
568             final StmtContext<?, ?, ?> module) {
569         final var ret = storage.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, module);
570         if (ret == null) {
571             throw new IllegalArgumentException("Failed to look up QNameModule for " + module + " in " + storage);
572         }
573         return ret;
574     }
575
576     public static QNameModule getModuleQNameByPrefix(final StmtContext<?, ?, ?> ctx, final String prefix) {
577         final var root = ctx.getRoot();
578         final var importedModule = root.namespaceItem(ParserNamespaces.IMPORT_PREFIX_TO_MODULECTX, prefix);
579         final var qnameModule = ctx.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, importedModule);
580         if (qnameModule != null) {
581             return qnameModule;
582         }
583
584         if (root.producesDeclared(SubmoduleStatement.class)) {
585             return ctx.namespaceItem(ParserNamespaces.MODULE_NAME_TO_QNAME,
586                 root.namespaceItem(ParserNamespaces.BELONGSTO_PREFIX_TO_MODULE_NAME, prefix));
587         }
588
589         return null;
590     }
591
592     public static Optional<Revision> getLatestRevision(final Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
593         Revision revision = null;
594         for (var subStmt : subStmts) {
595             if (subStmt.producesDeclared(RevisionStatement.class)) {
596                 if (revision == null && subStmt.argument() != null) {
597                     revision = (Revision) subStmt.argument();
598                 } else {
599                     final var subArg = (Revision) subStmt.argument();
600                     if (subArg != null && subArg.compareTo(revision) > 0) {
601                         revision = subArg;
602                     }
603                 }
604             }
605         }
606         return Optional.ofNullable(revision);
607     }
608 }