Update yang-parser-api
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / EnumSpecificationSupport.java
index 33998b07abceb989b2f5596fd568119e0310e4fe..f0cc35357dfefe6574b998ca7b55c1a45b77e301 100644 (file)
@@ -9,32 +9,30 @@ package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
 import com.google.common.collect.ImmutableList;
 import java.util.Optional;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
 import org.opendaylight.yangtools.yang.model.api.stmt.ValueEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
-import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
-import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
+import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.ri.type.EnumerationTypeBuilder;
+import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
+import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
-final class EnumSpecificationSupport
-        extends BaseStatementSupport<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> {
+final class EnumSpecificationSupport extends AbstractTypeSupport<EnumSpecification> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
             SubstatementValidator.builder(YangStmtMapping.TYPE).addMultiple(YangStmtMapping.ENUM).build();
 
-    EnumSpecificationSupport() {
-        super(YangStmtMapping.TYPE);
-    }
-
-    @Override
-    public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
-        return value;
+    EnumSpecificationSupport(final YangParserConfiguration config) {
+        super(config);
     }
 
     @Override
@@ -43,33 +41,41 @@ final class EnumSpecificationSupport
     }
 
     @Override
-    protected EnumSpecification createDeclared(final StmtContext<String, EnumSpecification, ?> ctx,
+    protected EnumSpecification createDeclared(final StmtContext<QName, EnumSpecification, ?> ctx,
             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
-        return new EnumSpecificationImpl(ctx, substatements);
+        if (substatements.isEmpty()) {
+            throw noEnum(ctx);
+        }
+        return new EnumSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
     }
 
     @Override
-    protected EnumSpecification createEmptyDeclared(final StmtContext<String, EnumSpecification, ?> ctx) {
-        throw noEnum(ctx);
+    protected EnumSpecification attachDeclarationReference(final EnumSpecification stmt,
+            final DeclarationReference reference) {
+        return new RefEnumSpecification(stmt, reference);
     }
 
     @Override
-    protected EffectiveStatement<String, EnumSpecification> createEffective(
-            final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx,
-            final EnumSpecification declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
-        final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
+    protected EffectiveStatement<QName, EnumSpecification> createEffective(
+            final Current<QName, EnumSpecification> stmt,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        if (substatements.isEmpty()) {
+            throw noEnum(stmt);
+        }
+
+        final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(stmt.argumentAsTypeQName());
         Integer highestValue = null;
-        for (final EffectiveStatement<?, ?> stmt : substatements) {
-            if (stmt instanceof EnumEffectiveStatement) {
-                final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) stmt;
+        for (final EffectiveStatement<?, ?> subStmt : substatements) {
+            if (subStmt instanceof EnumEffectiveStatement) {
+                final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) subStmt;
 
                 final Optional<Integer> declaredValue =
                         enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
                 final int effectiveValue;
                 if (declaredValue.isEmpty()) {
                     if (highestValue != null) {
-                        SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
-                                "Enum '%s' must have a value statement", enumSubStmt);
+                        SourceException.throwIf(highestValue == 2147483647, stmt,
+                            "Enum '%s' must have a value statement", enumSubStmt);
                         effectiveValue = highestValue + 1;
                     } else {
                         effectiveValue = 0;
@@ -87,24 +93,17 @@ final class EnumSpecificationSupport
             }
         }
 
-        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
-    }
-
-    @Override
-    protected EffectiveStatement<String, EnumSpecification> createEmptyEffective(
-            final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx,
-            final EnumSpecification declared) {
-        throw noEnum(ctx);
+        return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
     }
 
-    private static SourceException noEnum(final StmtContext<?, ?, ?> ctx) {
+    private static SourceException noEnum(final CommonStmtCtx stmt) {
         /*
          *  https://tools.ietf.org/html/rfc7950#section-9.6.4
          *
          *     The "enum" statement, which is a substatement to the "type"
          *     statement, MUST be present if the type is "enumeration".
          */
-        return new SourceException("At least one enum statement has to be present", ctx.getStatementSourceReference());
+        return new SourceException("At least one enum statement has to be present", stmt);
     }
 
-}
\ No newline at end of file
+}