2 * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
10 import static java.util.Objects.requireNonNull;
13 import java.util.function.Predicate;
14 import org.antlr.v4.runtime.CharStreams;
15 import org.antlr.v4.runtime.CommonTokenStream;
16 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionLexer;
17 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser;
18 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.Identifier_ref_argContext;
19 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_exprContext;
20 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_factorContext;
21 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_termContext;
22 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParserBaseVisitor;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.YangVersion;
25 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IfFeatureEffectiveStatementImpl;
36 public class IfFeatureStatementImpl extends AbstractDeclaredStatement<Predicate<Set<QName>>>
37 implements IfFeatureStatement {
38 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
42 protected IfFeatureStatementImpl(final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> context) {
46 public static class Definition extends AbstractStatementSupport<Predicate<Set<QName>>, IfFeatureStatement,
47 EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> {
50 super(YangStmtMapping.IF_FEATURE);
54 public Predicate<Set<QName>> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
55 if (YangVersion.VERSION_1_1.equals(ctx.getRootVersion())) {
56 return parseIfFeatureExpression(ctx, value);
59 final QName qname = StmtContextUtils.qnameFromArgument(ctx, value);
60 return setQNames -> setQNames.contains(qname);
64 public IfFeatureStatement createDeclared(final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> ctx) {
65 return new IfFeatureStatementImpl(ctx);
69 public EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement> createEffective(
70 final StmtContext<Predicate<Set<QName>>, IfFeatureStatement,
71 EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> ctx) {
72 return new IfFeatureEffectiveStatementImpl(ctx);
76 protected SubstatementValidator getSubstatementValidator() {
77 return SUBSTATEMENT_VALIDATOR;
80 private static Predicate<Set<QName>> parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx,
82 final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(CharStreams.fromString(value));
83 final CommonTokenStream tokens = new CommonTokenStream(lexer);
84 final IfFeatureExpressionParser parser = new IfFeatureExpressionParser(tokens);
86 return new IfFeaturePredicateVisitor(ctx).visit(parser.if_feature_expr());
89 private static class IfFeaturePredicateVisitor
90 extends IfFeatureExpressionParserBaseVisitor<Predicate<Set<QName>>> {
91 private final StmtContext<?, ?, ?> stmtCtx;
93 IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
94 this.stmtCtx = requireNonNull(ctx);
98 public Predicate<Set<QName>> visitIf_feature_expr(final If_feature_exprContext ctx) {
99 if (ctx.if_feature_expr() == null) {
100 return visitIf_feature_term(ctx.if_feature_term());
103 return visitIf_feature_term(ctx.if_feature_term()).or(visitIf_feature_expr(ctx.if_feature_expr()));
107 public Predicate<Set<QName>> visitIf_feature_term(final If_feature_termContext ctx) {
108 if (ctx.if_feature_term() == null) {
109 return visitIf_feature_factor(ctx.if_feature_factor());
112 return visitIf_feature_factor(ctx.if_feature_factor()).and(visitIf_feature_term(ctx.if_feature_term()));
116 public Predicate<Set<QName>> visitIf_feature_factor(final If_feature_factorContext ctx) {
117 if (ctx.if_feature_expr() != null) {
118 return visitIf_feature_expr(ctx.if_feature_expr());
119 } else if (ctx.if_feature_factor() != null) {
120 return visitIf_feature_factor(ctx.if_feature_factor()).negate();
121 } else if (ctx.identifier_ref_arg() != null) {
122 return visitIdentifier_ref_arg(ctx.identifier_ref_arg());
125 throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
126 + "Most probably IfFeature grammar has been changed.", stmtCtx.getStatementSourceReference());
130 public Predicate<Set<QName>> visitIdentifier_ref_arg(final Identifier_ref_argContext ctx) {
131 final QName featureQName = StmtContextUtils.qnameFromArgument(stmtCtx, ctx.getText());
132 return setQNames -> setQNames.contains(featureQName);
138 public Predicate<Set<QName>> getIfFeaturePredicate() {