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.QName;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
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.getStatementArgument())) {
175 final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.getStatementArgument());
176 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
177 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
178 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
179 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
182 * If the type does not exist, throw new InferenceException.
183 * Otherwise perform no operation.
185 typeAction.apply(new InferenceAction() {
187 public void apply(final InferenceContext ctx) {
192 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
193 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
194 "Type [%s] was not found.", typeQName);
200 public final String internArgument(final String rawArgument) {
202 return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
206 public boolean hasArgumentSpecificSupports() {
207 return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
211 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
212 return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
216 protected final SubstatementValidator getSubstatementValidator() {
217 return SUBSTATEMENT_VALIDATOR;
221 protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
222 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
223 return new RegularTypeStatement(ctx.coerceRawStatementArgument(), substatements);
227 protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
228 final TypeStatement builtin;
229 return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin
230 : new EmptyTypeStatement(ctx.coerceRawStatementArgument());
234 protected EffectiveStatement<String, TypeStatement> createEffective(final Current<String, TypeStatement> stmt,
235 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
236 // First look up the proper base type
237 final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(stmt);
238 if (substatements.isEmpty()) {
242 // Now instantiate the proper effective statement for that type
243 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
244 final TypeStatement declared = stmt.declared();
245 if (baseType instanceof BinaryTypeDefinition) {
246 return createBinary(stmt, (BinaryTypeDefinition) baseType, declared, substatements);
247 } else if (baseType instanceof BitsTypeDefinition) {
248 return createBits(stmt, (BitsTypeDefinition) baseType, declared, substatements);
249 } else if (baseType instanceof BooleanTypeDefinition) {
250 return createBoolean(stmt, (BooleanTypeDefinition) baseType, declared, substatements);
251 } else if (baseType instanceof DecimalTypeDefinition) {
252 return createDecimal(stmt, (DecimalTypeDefinition) baseType, declared, substatements);
253 } else if (baseType instanceof EmptyTypeDefinition) {
254 return createEmpty(stmt, (EmptyTypeDefinition) baseType, declared, substatements);
255 } else if (baseType instanceof EnumTypeDefinition) {
256 return createEnum(stmt, (EnumTypeDefinition) baseType, declared, substatements);
257 } else if (baseType instanceof IdentityrefTypeDefinition) {
258 return createIdentityref(stmt, (IdentityrefTypeDefinition) baseType, declared, substatements);
259 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
260 return createInstanceIdentifier(stmt, (InstanceIdentifierTypeDefinition) baseType, declared, substatements);
261 } else if (baseType instanceof Int8TypeDefinition) {
262 return createIntegral(stmt, declared, substatements,
263 RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
264 } else if (baseType instanceof Int16TypeDefinition) {
265 return createIntegral(stmt, declared, substatements,
266 RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
267 } else if (baseType instanceof Int32TypeDefinition) {
268 return createIntegral(stmt, declared, substatements,
269 RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
270 } else if (baseType instanceof Int64TypeDefinition) {
271 return createIntegral(stmt, declared, substatements,
272 RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
273 } else if (baseType instanceof LeafrefTypeDefinition) {
274 return createLeafref(stmt, (LeafrefTypeDefinition) baseType, declared, substatements);
275 } else if (baseType instanceof StringTypeDefinition) {
276 return createString(stmt, (StringTypeDefinition) baseType, declared, substatements);
277 } else if (baseType instanceof Uint8TypeDefinition) {
278 return createIntegral(stmt, declared, substatements,
279 RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
280 } else if (baseType instanceof Uint16TypeDefinition) {
281 return createIntegral(stmt, declared, substatements,
282 RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
283 } else if (baseType instanceof Uint32TypeDefinition) {
284 return createIntegral(stmt, declared, substatements,
285 RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
286 } else if (baseType instanceof Uint64TypeDefinition) {
287 return createIntegral(stmt, declared, substatements,
288 RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
289 } else if (baseType instanceof UnionTypeDefinition) {
290 return createUnion(stmt, (UnionTypeDefinition) baseType, declared, substatements);
292 throw new IllegalStateException("Unhandled base type " + baseType);
296 static final SchemaPath typeEffectiveSchemaPath(final Current<?, ?> stmt) {
297 final SchemaPath path = stmt.getSchemaPath();
298 final SchemaPath parent = path.getParent();
299 final QName parentQName = parent.getLastComponent();
300 checkArgument(parentQName != null, "Path %s has an empty parent", path);
302 final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
303 return parent.createChild(qname);
307 * Resolve type reference, as pointed to by the context's argument.
309 * @param ctx Statement context
310 * @return Resolved type
311 * @throws SourceException if the target type cannot be found
313 private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final Current<String, ?> ctx) {
314 final String argument = ctx.getArgument();
317 return BuiltinEffectiveStatement.BINARY;
319 return BuiltinEffectiveStatement.BOOLEAN;
321 return BuiltinEffectiveStatement.EMPTY;
322 case INSTANCE_IDENTIFIER:
323 return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
325 return BuiltinEffectiveStatement.INT8;
327 return BuiltinEffectiveStatement.INT16;
329 return BuiltinEffectiveStatement.INT32;
331 return BuiltinEffectiveStatement.INT64;
333 return BuiltinEffectiveStatement.STRING;
335 return BuiltinEffectiveStatement.UINT8;
337 return BuiltinEffectiveStatement.UINT16;
339 return BuiltinEffectiveStatement.UINT32;
341 return BuiltinEffectiveStatement.UINT64;
343 final QName qname = StmtContextUtils.parseNodeIdentifier(ctx.caerbannog(), argument);
344 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
345 SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
346 ctx.sourceReference(), "Type '%s' not found", qname);
347 return typedef.buildEffective().asTypeEffectiveStatement();
351 private static @NonNull TypeEffectiveStatement<TypeStatement> createBinary(final Current<?, ?> ctx,
352 final BinaryTypeDefinition baseType, final TypeStatement declared,
353 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
354 final LengthRestrictedTypeBuilder<BinaryTypeDefinition> builder =
355 RestrictedTypes.newBinaryBuilder(baseType, typeEffectiveSchemaPath(ctx));
357 for (EffectiveStatement<?, ?> stmt : substatements) {
358 if (stmt instanceof LengthEffectiveStatement) {
359 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
362 builder.setLengthConstraint(length, length.argument());
363 } catch (IllegalStateException e) {
364 throw new SourceException(ctx.sourceReference(), e, "Multiple length constraints encountered");
365 } catch (InvalidLengthConstraintException e) {
366 throw new SourceException(ctx.sourceReference(), e, "Invalid length constraint %s",
372 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
375 private @NonNull TypeEffectiveStatement<TypeStatement> createBits(final Current<?, ?> ctx,
376 final BitsTypeDefinition baseType, final TypeStatement declared,
377 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
378 final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath());
380 for (final EffectiveStatement<?, ?> stmt : substatements) {
381 if (stmt instanceof BitEffectiveStatement) {
382 builder.addBit(addRestrictedBit(ctx, baseType, (BitEffectiveStatement) stmt));
386 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
389 abstract @NonNull Bit addRestrictedBit(@NonNull EffectiveStmtCtx stmt, @NonNull BitsTypeDefinition base,
390 @NonNull BitEffectiveStatement bit);
392 private static @NonNull TypeEffectiveStatement<TypeStatement> createBoolean(final Current<?, ?> ctx,
393 final BooleanTypeDefinition baseType, final TypeStatement declared,
394 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
395 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newBooleanBuilder(baseType,
396 typeEffectiveSchemaPath(ctx)));
399 private static @NonNull TypeEffectiveStatement<TypeStatement> createDecimal(final Current<?, ?> ctx,
400 final DecimalTypeDefinition baseType, final TypeStatement declared,
401 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
402 final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
403 RestrictedTypes.newDecima64Builder(baseType, typeEffectiveSchemaPath(ctx));
405 for (EffectiveStatement<?, ?> stmt : substatements) {
406 if (stmt instanceof RangeEffectiveStatement) {
407 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
408 builder.setRangeConstraint(range, range.argument());
410 if (stmt instanceof FractionDigitsEffectiveStatement) {
411 final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
412 SourceException.throwIf(baseType.getFractionDigits() != digits, ctx.sourceReference(),
413 "Cannot override fraction-digits from base type %s to %s", baseType, digits);
417 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
420 private static @NonNull TypeEffectiveStatement<TypeStatement> createEmpty(final Current<?, ?> ctx,
421 final EmptyTypeDefinition baseType, final TypeStatement declared,
422 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
423 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newEmptyBuilder(baseType,
424 typeEffectiveSchemaPath(ctx)));
427 private @NonNull TypeEffectiveStatement<TypeStatement> createEnum(final Current<?, ?> ctx,
428 final EnumTypeDefinition baseType, final TypeStatement declared,
429 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
430 final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType, ctx.getSchemaPath());
432 for (final EffectiveStatement<?, ?> stmt : substatements) {
433 if (stmt instanceof EnumEffectiveStatement) {
434 builder.addEnum(addRestrictedEnum(ctx, baseType, (EnumEffectiveStatement) stmt));
438 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
441 abstract @NonNull EnumPair addRestrictedEnum(@NonNull EffectiveStmtCtx stmt, @NonNull EnumTypeDefinition base,
442 @NonNull EnumEffectiveStatement enumStmt);
444 private static @NonNull TypeEffectiveStatement<TypeStatement> createIdentityref(final Current<?, ?> ctx,
445 final IdentityrefTypeDefinition baseType, final TypeStatement declared,
446 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
447 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newIdentityrefBuilder(baseType,
448 typeEffectiveSchemaPath(ctx)));
451 private static @NonNull TypeEffectiveStatement<TypeStatement> createInstanceIdentifier(
452 final Current<?, ?> ctx, final InstanceIdentifierTypeDefinition baseType, final TypeStatement declared,
453 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
454 final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(baseType,
455 typeEffectiveSchemaPath(ctx));
457 for (EffectiveStatement<?, ?> stmt : substatements) {
458 if (stmt instanceof RequireInstanceEffectiveStatement) {
459 builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
463 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
466 private static <T extends RangeRestrictedTypeDefinition<T, N>, N extends Number & Comparable<N>>
467 @NonNull TypeEffectiveStatement<TypeStatement> createIntegral(final Current<?, ?> ctx,
468 final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
469 final RangeRestrictedTypeBuilder<T, N> builder) {
470 for (EffectiveStatement<?, ?> stmt : substatements) {
471 if (stmt instanceof RangeEffectiveStatement) {
472 final RangeEffectiveStatement rangeStmt = (RangeEffectiveStatement)stmt;
473 builder.setRangeConstraint(rangeStmt, rangeStmt.argument());
478 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
479 } catch (InvalidRangeConstraintException e) {
480 throw new SourceException(ctx.sourceReference(), e, "Invalid range constraint: %s",
481 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.sourceReference(), e, "Multiple length constraints encountered");
513 } catch (InvalidLengthConstraintException e) {
514 throw new SourceException(ctx.sourceReference(), e, "Invalid length constraint %s",
518 if (stmt instanceof PatternEffectiveStatement) {
519 builder.addPatternConstraint((PatternEffectiveStatement) stmt);
523 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
526 private static @NonNull TypeEffectiveStatement<TypeStatement> createUnion(final Current<?, ?> ctx,
527 final UnionTypeDefinition baseType, final TypeStatement declared,
528 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
529 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newUnionBuilder(baseType,
530 typeEffectiveSchemaPath(ctx)));