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;
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> {
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);
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);
@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<>();
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) {
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);
}