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.type;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import java.math.BigDecimal;
15 import java.util.Collection;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.common.Empty;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
37 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
42 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
50 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
51 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
52 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
53 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
54 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
55 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
56 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
57 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
58 import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder;
59 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
60 import org.opendaylight.yangtools.yang.model.util.type.InvalidRangeConstraintException;
61 import org.opendaylight.yangtools.yang.model.util.type.LengthRestrictedTypeBuilder;
62 import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
63 import org.opendaylight.yangtools.yang.model.util.type.RequireInstanceRestrictedTypeBuilder;
64 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
65 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
66 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
67 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
68 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx;
69 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
70 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
71 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
72 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
73 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
74 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
75 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
76 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
77 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
78 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
79 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
80 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
81 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
82 import org.slf4j.Logger;
83 import org.slf4j.LoggerFactory;
85 abstract class AbstractTypeStatementSupport
86 extends BaseStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
87 private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeStatementSupport.class);
89 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
91 .addOptional(YangStmtMapping.BASE)
92 .addAny(YangStmtMapping.BIT)
93 .addAny(YangStmtMapping.ENUM)
94 .addOptional(YangStmtMapping.FRACTION_DIGITS)
95 .addOptional(YangStmtMapping.LENGTH)
96 .addOptional(YangStmtMapping.PATH)
97 .addAny(YangStmtMapping.PATTERN)
98 .addOptional(YangStmtMapping.RANGE)
99 .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
100 .addAny(YangStmtMapping.TYPE)
103 static final String BINARY = "binary";
104 static final String BITS = "bits";
105 static final String BOOLEAN = "boolean";
106 static final String DECIMAL64 = "decimal64";
107 static final String EMPTY = "empty";
108 static final String ENUMERATION = "enumeration";
109 static final String IDENTITY_REF = "identityref";
110 static final String INSTANCE_IDENTIFIER = "instance-identifier";
111 static final String INT8 = "int8";
112 static final String INT16 = "int16";
113 static final String INT32 = "int32";
114 static final String INT64 = "int64";
115 static final String LEAF_REF = "leafref";
116 static final String STRING = "string";
117 static final String UINT8 = "uint8";
118 static final String UINT16 = "uint16";
119 static final String UINT32 = "uint32";
120 static final String UINT64 = "uint64";
121 static final String UNION = "union";
123 private static final ImmutableMap<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
126 .put(BOOLEAN, BOOLEAN)
127 .put(DECIMAL64, DECIMAL64)
129 .put(ENUMERATION, ENUMERATION)
130 .put(IDENTITY_REF,IDENTITY_REF)
131 .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
136 .put(LEAF_REF, LEAF_REF)
145 private static final ImmutableMap<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS =
146 ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
147 .put(BITS, new BitsSpecificationSupport())
148 .put(DECIMAL64, new Decimal64SpecificationSupport())
149 .put(ENUMERATION, new EnumSpecificationSupport())
150 .put(IDENTITY_REF, new IdentityRefSpecificationRFC6020Support())
151 .put(INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationSupport())
152 .put(LEAF_REF, new LeafrefSpecificationRFC6020Support())
153 .put(UNION, new UnionSpecificationSupport())
156 AbstractTypeStatementSupport() {
157 super(YangStmtMapping.TYPE);
161 public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
166 public final void onFullDefinitionDeclared(
167 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
168 super.onFullDefinitionDeclared(stmt);
170 // if it is yang built-in type, no prerequisite is needed, so simply return
171 if (BUILT_IN_TYPES.containsKey(stmt.argument())) {
172 // FIXME: consider populating BaseTypeNamespace here, which could be done quite efficiently, moving the
173 // logic from resolveType()
177 final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.argument());
178 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
179 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
180 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
181 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
184 * If the type does not exist, throw an InferenceException.
185 * If the type exists, store a reference to it in BaseTypeNamespace.
187 typeAction.apply(new InferenceAction() {
189 public void apply(final InferenceContext ctx) {
190 stmt.addToNs(BaseTypeNamespace.class, Empty.getInstance(), typePrereq.resolve(ctx));
194 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
195 InferenceException.throwIf(failed.contains(typePrereq), stmt, "Type [%s] was not found.", typeQName);
201 public final String internArgument(final String rawArgument) {
203 return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
207 public boolean hasArgumentSpecificSupports() {
208 return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
212 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
213 return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
217 protected final SubstatementValidator getSubstatementValidator() {
218 return SUBSTATEMENT_VALIDATOR;
222 protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
223 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
224 return new RegularTypeStatement(ctx.getRawArgument(), substatements);
228 protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
229 final TypeStatement builtin;
230 return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin
231 : new EmptyTypeStatement(ctx.getRawArgument());
235 protected EffectiveStatement<String, TypeStatement> createEffective(final Current<String, TypeStatement> stmt,
236 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
237 // First look up the proper base type
238 final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(stmt);
239 if (substatements.isEmpty()) {
243 // Now instantiate the proper effective statement for that type
244 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
245 final TypeStatement declared = stmt.declared();
246 if (baseType instanceof BinaryTypeDefinition) {
247 return createBinary(stmt, (BinaryTypeDefinition) baseType, declared, substatements);
248 } else if (baseType instanceof BitsTypeDefinition) {
249 return createBits(stmt, (BitsTypeDefinition) baseType, declared, substatements);
250 } else if (baseType instanceof BooleanTypeDefinition) {
251 return createBoolean(stmt, (BooleanTypeDefinition) baseType, declared, substatements);
252 } else if (baseType instanceof DecimalTypeDefinition) {
253 return createDecimal(stmt, (DecimalTypeDefinition) baseType, declared, substatements);
254 } else if (baseType instanceof EmptyTypeDefinition) {
255 return createEmpty(stmt, (EmptyTypeDefinition) baseType, declared, substatements);
256 } else if (baseType instanceof EnumTypeDefinition) {
257 return createEnum(stmt, (EnumTypeDefinition) baseType, declared, substatements);
258 } else if (baseType instanceof IdentityrefTypeDefinition) {
259 return createIdentityref(stmt, (IdentityrefTypeDefinition) baseType, declared, substatements);
260 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
261 return createInstanceIdentifier(stmt, (InstanceIdentifierTypeDefinition) baseType, declared, substatements);
262 } else if (baseType instanceof Int8TypeDefinition) {
263 return createIntegral(stmt, declared, substatements,
264 RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
265 } else if (baseType instanceof Int16TypeDefinition) {
266 return createIntegral(stmt, declared, substatements,
267 RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
268 } else if (baseType instanceof Int32TypeDefinition) {
269 return createIntegral(stmt, declared, substatements,
270 RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
271 } else if (baseType instanceof Int64TypeDefinition) {
272 return createIntegral(stmt, declared, substatements,
273 RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
274 } else if (baseType instanceof LeafrefTypeDefinition) {
275 return createLeafref(stmt, (LeafrefTypeDefinition) baseType, declared, substatements);
276 } else if (baseType instanceof StringTypeDefinition) {
277 return createString(stmt, (StringTypeDefinition) baseType, declared, substatements);
278 } else if (baseType instanceof Uint8TypeDefinition) {
279 return createIntegral(stmt, declared, substatements,
280 RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
281 } else if (baseType instanceof Uint16TypeDefinition) {
282 return createIntegral(stmt, declared, substatements,
283 RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
284 } else if (baseType instanceof Uint32TypeDefinition) {
285 return createIntegral(stmt, declared, substatements,
286 RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
287 } else if (baseType instanceof Uint64TypeDefinition) {
288 return createIntegral(stmt, declared, substatements,
289 RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
290 } else if (baseType instanceof UnionTypeDefinition) {
291 return createUnion(stmt, (UnionTypeDefinition) baseType, declared, substatements);
293 throw new IllegalStateException("Unhandled base type " + baseType);
297 static final SchemaPath typeEffectiveSchemaPath(final Current<?, ?> stmt) {
298 final SchemaPath path = stmt.getSchemaPath();
299 final SchemaPath parent = path.getParent();
300 final QName parentQName = parent.getLastComponent();
301 checkArgument(parentQName != null, "Path %s has an empty parent", path);
303 final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
304 return parent.createChild(qname);
308 * Resolve type reference, as pointed to by the context's argument.
310 * @param ctx Statement context
311 * @return Resolved type
312 * @throws SourceException if the target type cannot be found
314 private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final Current<String, ?> ctx) {
315 final StmtContext<?, ?, ?> baseType = ctx.namespaceItem(BaseTypeNamespace.class, Empty.getInstance());
316 if (baseType != null) {
317 return ((TypedefEffectiveStatement) baseType.buildEffective()).asTypeEffectiveStatement();
320 final String argument = ctx.getArgument();
323 return BuiltinEffectiveStatement.BINARY;
325 return BuiltinEffectiveStatement.BOOLEAN;
327 return BuiltinEffectiveStatement.EMPTY;
328 case INSTANCE_IDENTIFIER:
329 return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
331 return BuiltinEffectiveStatement.INT8;
333 return BuiltinEffectiveStatement.INT16;
335 return BuiltinEffectiveStatement.INT32;
337 return BuiltinEffectiveStatement.INT64;
339 return BuiltinEffectiveStatement.STRING;
341 return BuiltinEffectiveStatement.UINT8;
343 return BuiltinEffectiveStatement.UINT16;
345 return BuiltinEffectiveStatement.UINT32;
347 return BuiltinEffectiveStatement.UINT64;
349 throw new IllegalStateException("Unhandled type argument " + argument);
353 private static @NonNull TypeEffectiveStatement<TypeStatement> createBinary(final Current<?, ?> ctx,
354 final BinaryTypeDefinition baseType, final TypeStatement declared,
355 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
356 final LengthRestrictedTypeBuilder<BinaryTypeDefinition> builder =
357 RestrictedTypes.newBinaryBuilder(baseType, typeEffectiveSchemaPath(ctx));
359 for (EffectiveStatement<?, ?> stmt : substatements) {
360 if (stmt instanceof LengthEffectiveStatement) {
361 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
364 builder.setLengthConstraint(length, length.argument());
365 } catch (IllegalStateException e) {
366 throw new SourceException(ctx, e, "Multiple length constraints encountered");
367 } catch (InvalidLengthConstraintException e) {
368 throw new SourceException(ctx, e, "Invalid length constraint %s", length.argument());
373 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
376 private @NonNull TypeEffectiveStatement<TypeStatement> createBits(final Current<?, ?> ctx,
377 final BitsTypeDefinition baseType, final TypeStatement declared,
378 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
379 final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath());
381 for (final EffectiveStatement<?, ?> stmt : substatements) {
382 if (stmt instanceof BitEffectiveStatement) {
383 builder.addBit(addRestrictedBit(ctx, baseType, (BitEffectiveStatement) stmt));
387 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
390 abstract @NonNull Bit addRestrictedBit(@NonNull EffectiveStmtCtx stmt, @NonNull BitsTypeDefinition base,
391 @NonNull BitEffectiveStatement bit);
393 private static @NonNull TypeEffectiveStatement<TypeStatement> createBoolean(final Current<?, ?> ctx,
394 final BooleanTypeDefinition baseType, final TypeStatement declared,
395 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
396 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newBooleanBuilder(baseType,
397 typeEffectiveSchemaPath(ctx)));
400 private static @NonNull TypeEffectiveStatement<TypeStatement> createDecimal(final Current<?, ?> ctx,
401 final DecimalTypeDefinition baseType, final TypeStatement declared,
402 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
403 final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
404 RestrictedTypes.newDecima64Builder(baseType, typeEffectiveSchemaPath(ctx));
406 for (EffectiveStatement<?, ?> stmt : substatements) {
407 if (stmt instanceof RangeEffectiveStatement) {
408 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
409 builder.setRangeConstraint(range, range.argument());
411 if (stmt instanceof FractionDigitsEffectiveStatement) {
412 final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
413 SourceException.throwIf(baseType.getFractionDigits() != digits, ctx,
414 "Cannot override fraction-digits from base type %s to %s", baseType, digits);
418 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
421 private static @NonNull TypeEffectiveStatement<TypeStatement> createEmpty(final Current<?, ?> ctx,
422 final EmptyTypeDefinition baseType, final TypeStatement declared,
423 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
424 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newEmptyBuilder(baseType,
425 typeEffectiveSchemaPath(ctx)));
428 private @NonNull TypeEffectiveStatement<TypeStatement> createEnum(final Current<?, ?> ctx,
429 final EnumTypeDefinition baseType, final TypeStatement declared,
430 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
431 final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType, ctx.getSchemaPath());
433 for (final EffectiveStatement<?, ?> stmt : substatements) {
434 if (stmt instanceof EnumEffectiveStatement) {
435 builder.addEnum(addRestrictedEnum(ctx, baseType, (EnumEffectiveStatement) stmt));
439 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
442 abstract @NonNull EnumPair addRestrictedEnum(@NonNull EffectiveStmtCtx stmt, @NonNull EnumTypeDefinition base,
443 @NonNull EnumEffectiveStatement enumStmt);
445 private static @NonNull TypeEffectiveStatement<TypeStatement> createIdentityref(final Current<?, ?> ctx,
446 final IdentityrefTypeDefinition baseType, final TypeStatement declared,
447 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
448 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newIdentityrefBuilder(baseType,
449 typeEffectiveSchemaPath(ctx)));
452 private static @NonNull TypeEffectiveStatement<TypeStatement> createInstanceIdentifier(
453 final Current<?, ?> ctx, final InstanceIdentifierTypeDefinition baseType, final TypeStatement declared,
454 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
455 final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(baseType,
456 typeEffectiveSchemaPath(ctx));
458 for (EffectiveStatement<?, ?> stmt : substatements) {
459 if (stmt instanceof RequireInstanceEffectiveStatement) {
460 builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
464 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
467 private static <T extends RangeRestrictedTypeDefinition<T, N>, N extends Number & Comparable<N>>
468 @NonNull TypeEffectiveStatement<TypeStatement> createIntegral(final Current<?, ?> ctx,
469 final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
470 final RangeRestrictedTypeBuilder<T, N> builder) {
471 for (EffectiveStatement<?, ?> stmt : substatements) {
472 if (stmt instanceof RangeEffectiveStatement) {
473 final RangeEffectiveStatement rangeStmt = (RangeEffectiveStatement)stmt;
474 builder.setRangeConstraint(rangeStmt, rangeStmt.argument());
479 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
480 } catch (InvalidRangeConstraintException e) {
481 throw new SourceException(ctx, e, "Invalid range constraint: %s", e.getOffendingRanges());
485 private static @NonNull TypeEffectiveStatement<TypeStatement> createLeafref(final Current<?, ?> ctx,
486 final LeafrefTypeDefinition baseType, final TypeStatement declared,
487 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
488 final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> builder =
489 RestrictedTypes.newLeafrefBuilder(baseType, AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
491 for (final EffectiveStatement<?, ?> stmt : substatements) {
492 if (stmt instanceof RequireInstanceEffectiveStatement) {
493 builder.setRequireInstance(((RequireInstanceEffectiveStatement) stmt).argument());
496 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
499 private static @NonNull TypeEffectiveStatement<TypeStatement> createString(final Current<?, ?> ctx,
500 final StringTypeDefinition baseType, final TypeStatement declared,
501 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
502 final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
503 AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
505 for (EffectiveStatement<?, ?> stmt : substatements) {
506 if (stmt instanceof LengthEffectiveStatement) {
507 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
510 builder.setLengthConstraint(length, length.argument());
511 } catch (IllegalStateException e) {
512 throw new SourceException(ctx, e, "Multiple length constraints encountered");
513 } catch (InvalidLengthConstraintException e) {
514 throw new SourceException(ctx, e, "Invalid length constraint %s", length.argument());
517 if (stmt instanceof PatternEffectiveStatement) {
518 builder.addPatternConstraint((PatternEffectiveStatement) stmt);
522 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
525 private static @NonNull TypeEffectiveStatement<TypeStatement> createUnion(final Current<?, ?> ctx,
526 final UnionTypeDefinition baseType, final TypeStatement declared,
527 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
528 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newUnionBuilder(baseType,
529 typeEffectiveSchemaPath(ctx)));