Add a proper grouping statement policy
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / EffectiveStmtUtils.java
index 0d3ebb91558e6f92362ce86e0785a0140b59d74c..a780d970b6127756273baa4b1b1dd74dbbb0d494 100644 (file)
@@ -30,7 +30,8 @@ import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 @Beta
@@ -42,13 +43,11 @@ public final class EffectiveStmtUtils {
         // Hidden on purpose
     }
 
-    public static SourceException createNameCollisionSourceException(final StmtContext<?, ?, ?> ctx,
+    public static SourceException createNameCollisionSourceException(final EffectiveStmtCtx.Current<?, ?> stmt,
             final EffectiveStatement<?, ?> effectiveStatement) {
-        return new SourceException(ctx.getStatementSourceReference(),
+        return new SourceException(stmt,
             "Error in module '%s': cannot add '%s'. Node name collision: '%s' already declared.",
-            ctx.getRoot().getStatementArgument(),
-            effectiveStatement.argument(),
-            effectiveStatement.argument());
+            stmt.moduleName().getLocalName(), effectiveStatement.argument(), effectiveStatement.argument());
     }
 
     public static Optional<ElementCountConstraint> createElementCountConstraint(final EffectiveStatement<?, ?> stmt) {
@@ -60,8 +59,8 @@ public final class EffectiveStmtUtils {
     public static Optional<ElementCountConstraint> createElementCountConstraint(
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
         return createElementCountConstraint(
-            BaseQNameStatementSupport.findFirstArgument(substatements, MinElementsEffectiveStatement.class, null),
-            BaseQNameStatementSupport.findFirstArgument(substatements, MaxElementsEffectiveStatement.class, null));
+            AbstractStatementSupport.findFirstArgument(substatements, MinElementsEffectiveStatement.class, null),
+            AbstractStatementSupport.findFirstArgument(substatements, MaxElementsEffectiveStatement.class, null));
     }
 
     private static Optional<ElementCountConstraint> createElementCountConstraint(
@@ -168,33 +167,33 @@ public final class EffectiveStmtUtils {
         return false;
     }
 
-    public static void checkUniqueGroupings(final StmtContext<?, ?, ?> ctx,
+    public static void checkUniqueGroupings(final EffectiveStmtCtx.Current<?, ?> stmt,
             final Collection<? extends EffectiveStatement<?, ?>> statements) {
-        checkUniqueNodes(ctx, statements, GroupingDefinition.class);
+        checkUniqueNodes(stmt, statements, GroupingDefinition.class);
     }
 
-    public static void checkUniqueTypedefs(final StmtContext<?, ?, ?> ctx,
+    public static void checkUniqueTypedefs(final EffectiveStmtCtx.Current<?, ?> stmt,
             final Collection<? extends EffectiveStatement<?, ?>> statements) {
         final Set<Object> typedefs = new HashSet<>();
-        for (EffectiveStatement<?, ?> stmt : statements) {
-            if (stmt instanceof TypedefEffectiveStatement
-                    && !typedefs.add(((TypedefEffectiveStatement) stmt).getTypeDefinition())) {
-                throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
+        for (EffectiveStatement<?, ?> statement : statements) {
+            if (statement instanceof TypedefEffectiveStatement
+                    && !typedefs.add(((TypedefEffectiveStatement) statement).getTypeDefinition())) {
+                throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, statement);
             }
         }
     }
 
-    public static void checkUniqueUses(final StmtContext<?, ?, ?> ctx,
+    public static void checkUniqueUses(final EffectiveStmtCtx.Current<?, ?> stmt,
             final Collection<? extends EffectiveStatement<?, ?>> statements) {
-        checkUniqueNodes(ctx, statements, UsesNode.class);
+        checkUniqueNodes(stmt, statements, UsesNode.class);
     }
 
-    private static void checkUniqueNodes(final StmtContext<?, ?, ?> ctx,
+    private static void checkUniqueNodes(final EffectiveStmtCtx.Current<?, ?> stmt,
             final Collection<? extends EffectiveStatement<?, ?>> statements, final Class<?> type) {
         final Set<Object> nodes = new HashSet<>();
-        for (EffectiveStatement<?, ?> stmt : statements) {
-            if (type.isInstance(stmt) && !nodes.add(stmt)) {
-                throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
+        for (EffectiveStatement<?, ?> statement : statements) {
+            if (type.isInstance(statement) && !nodes.add(statement)) {
+                throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, statement);
             }
         }
     }