Integrate {Inference,Source}Exception with CommonStmtCtx
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / module / AbstractModuleStatementSupport.java
index f41c7b131c96e339d6435dc6a5b2ed0d84ce48db..b8792eb447fd3e0f5bc08a16e9ec7608b5746108 100644 (file)
@@ -35,9 +35,11 @@ import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.SubstatementIndexingException;
 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule;
 import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace;
+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.SemanticVersionModuleNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
@@ -55,7 +57,6 @@ import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelon
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleQNameToModuleName;
 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 
 abstract class AbstractModuleStatementSupport
         extends BaseStatementSupport<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> {
@@ -68,22 +69,22 @@ abstract class AbstractModuleStatementSupport
         try {
             return UnqualifiedQName.of(value);
         } catch (IllegalArgumentException e) {
-            throw new SourceException(e.getMessage(), ctx.getStatementSourceReference(), e);
+            throw new SourceException(e.getMessage(), ctx, e);
         }
     }
 
     @Override
     public final void onPreLinkageDeclared(
             final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
-        final String moduleName = stmt.coerceRawStatementArgument();
+        final String moduleName = stmt.getRawArgument();
 
-        final URI moduleNs = firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class);
-        SourceException.throwIfNull(moduleNs, stmt.getStatementSourceReference(),
+        final URI moduleNs = SourceException.throwIfNull(
+            firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class), stmt,
             "Namespace of the module [%s] is missing", moduleName);
         stmt.addToNs(ModuleNameToNamespace.class, moduleName, moduleNs);
 
-        final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
-        SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
+        final String modulePrefix = SourceException.throwIfNull(
+            firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class), stmt,
             "Prefix of the module [%s] is missing", moduleName);
         stmt.addToNs(ImpPrefixToNamespace.class, modulePrefix, moduleNs);
 
@@ -93,38 +94,36 @@ abstract class AbstractModuleStatementSupport
         final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
 
         stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
-        stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.coerceStatementArgument().getLocalName(),
-            revisionDate));
+        stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getArgument().getLocalName(), revisionDate));
     }
 
     @Override
     public final void onLinkageDeclared(
             final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt) {
 
-        final Optional<URI> moduleNs = Optional.ofNullable(firstAttributeOf(stmt.declaredSubstatements(),
-                NamespaceStatement.class));
-        SourceException.throwIf(!moduleNs.isPresent(), stmt.getStatementSourceReference(),
-            "Namespace of the module [%s] is missing", stmt.getStatementArgument());
+        final URI moduleNs = SourceException.throwIfNull(
+            firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class), stmt,
+            "Namespace of the module [%s] is missing", stmt.argument());
 
         final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
-        final QNameModule qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orElse(null)).intern();
+        final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
         final StmtContext<?, ModuleStatement, ModuleEffectiveStatement> possibleDuplicateModule =
                 stmt.getFromNamespace(NamespaceToModule.class, qNameModule);
         if (possibleDuplicateModule != null && possibleDuplicateModule != stmt) {
-            throw new SourceException(stmt.getStatementSourceReference(), "Module namespace collision: %s. At %s",
-                    qNameModule.getNamespace(), possibleDuplicateModule.getStatementSourceReference());
+            throw new SourceException(stmt, "Module namespace collision: %s. At %s", qNameModule.getNamespace(),
+                possibleDuplicateModule.sourceReference());
         }
 
-        final String moduleName = stmt.coerceRawStatementArgument();
+        final String moduleName = stmt.getRawArgument();
         final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(moduleName, revisionDate);
 
         stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
         stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
         stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
 
-        final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
-        SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(),
-            "Prefix of the module [%s] is missing", stmt.getStatementArgument());
+        final String modulePrefix = SourceException.throwIfNull(
+            firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class), stmt,
+            "Prefix of the module [%s] is missing", stmt.argument());
 
         stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
         stmt.addToNs(ModuleNameToModuleQName.class, moduleName, qNameModule);
@@ -168,19 +167,19 @@ abstract class AbstractModuleStatementSupport
     @Override
     protected final ModuleStatement createDeclared(final StmtContext<UnqualifiedQName, ModuleStatement, ?> ctx,
             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
-        return new ModuleStatementImpl(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(), substatements);
+        return new ModuleStatementImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
     }
 
     @Override
     protected final ModuleStatement createEmptyDeclared(final StmtContext<UnqualifiedQName, ModuleStatement, ?> ctx) {
-        throw noNamespace(ctx.getStatementSourceReference());
+        throw noNamespace(ctx);
     }
 
     @Override
     protected final ModuleEffectiveStatement createEffective(final Current<UnqualifiedQName, ModuleStatement> stmt,
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
         if (substatements.isEmpty()) {
-            throw noNamespace(stmt.sourceReference());
+            throw noNamespace(stmt);
         }
 
         final List<Submodule> submodules = new ArrayList<>();
@@ -190,7 +189,11 @@ abstract class AbstractModuleStatementSupport
             submodules.add((Submodule) submodule);
         }
 
-        return new ModuleEffectiveStatementImpl(stmt, substatements, submodules);
+        try {
+            return new ModuleEffectiveStatementImpl(stmt, substatements, submodules);
+        } catch (SubstatementIndexingException e) {
+            throw new SourceException(e.getMessage(), stmt, e);
+        }
     }
 
     private static Collection<StmtContext<?, ?, ?>> submoduleContexts(final Current<?, ?> stmt) {
@@ -199,14 +202,14 @@ abstract class AbstractModuleStatementSupport
         return submodules == null ? List.of() : submodules.values();
     }
 
-    private static SourceException noNamespace(final @NonNull StatementSourceReference ref) {
-        return new SourceException("No namespace declared in module", ref);
+    private static SourceException noNamespace(final @NonNull CommonStmtCtx stmt) {
+        return new SourceException("No namespace declared in module", stmt);
     }
 
     private static void addToSemVerModuleNamespace(
             final Mutable<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> stmt,
             final SourceIdentifier moduleIdentifier) {
-        final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(stmt.coerceRawStatementArgument(),
+        final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(stmt.getRawArgument(),
             stmt.getFromNamespace(SemanticVersionNamespace.class, stmt));
         stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt);
     }