2 * Copyright (c) 2017 Pantheon Technologies, 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.stmt.grouping;
10 import com.google.common.collect.ImmutableList;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingStatement;
17 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
18 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 abstract class AbstractGroupingStatementSupport
26 extends BaseQNameStatementSupport<GroupingStatement, GroupingEffectiveStatement> {
27 AbstractGroupingStatementSupport() {
28 super(YangStmtMapping.GROUPING);
32 public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
33 return StmtContextUtils.parseIdentifier(ctx, value);
37 public final void onFullDefinitionDeclared(
38 final Mutable<QName, GroupingStatement, GroupingEffectiveStatement> stmt) {
39 super.onFullDefinitionDeclared(stmt);
42 final Mutable<?, ?, ?> parent = stmt.getParentContext();
44 // Shadowing check: make sure we do not trample on pre-existing definitions. This catches sibling
45 // declarations and parent declarations which have already been declared.
46 checkConflict(parent, stmt);
47 parent.addContext(GroupingNamespace.class, stmt.coerceStatementArgument(), stmt);
53 protected final GroupingStatement createDeclared(final StmtContext<QName, GroupingStatement, ?> ctx,
54 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
55 checkDeclaredConflict(ctx);
56 return new RegularGroupingStatement(ctx.coerceStatementArgument(), substatements);
60 protected final GroupingStatement createEmptyDeclared(final StmtContext<QName, GroupingStatement, ?> ctx) {
61 checkDeclaredConflict(ctx);
62 return new EmptyGroupingStatement(ctx.coerceStatementArgument());
66 protected GroupingEffectiveStatement createEffective(final Current<QName, GroupingStatement> stmt,
67 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
68 return new GroupingEffectiveStatementImpl(stmt.declared(), substatements, stmt.sourceReference(),
69 historyAndStatusFlags(stmt.history(), substatements), stmt.getSchemaPath());
72 private static void checkDeclaredConflict(final StmtContext<QName, ?, ?> ctx) {
73 // Shadowing check: make sure grandparent does not see a conflicting definition. This is required to ensure
74 // that a grouping in child scope does not shadow a grouping in parent scope which occurs later in the text.
75 final StmtContext<?, ?, ?> parent = ctx.getParentContext();
77 final StmtContext<?, ?, ?> grandParent = parent.getParentContext();
78 if (grandParent != null) {
79 checkConflict(grandParent, ctx);
84 private static void checkConflict(final StmtContext<?, ?, ?> parent, final StmtContext<QName, ?, ?> stmt) {
85 final QName arg = stmt.coerceStatementArgument();
86 final StmtContext<?, ?, ?> existing = parent.getFromNamespace(GroupingNamespace.class, arg);
87 SourceException.throwIf(existing != null, stmt.sourceReference(), "Duplicate name for grouping %s", arg);