Bug 6868: If-feature argument may be boolean expression
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / IfFeatureStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Set;
12 import java.util.function.Predicate;
13 import org.antlr.v4.runtime.ANTLRInputStream;
14 import org.antlr.v4.runtime.CommonTokenStream;
15 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionLexer;
16 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser;
17 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.Identifier_ref_argContext;
18 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_exprContext;
19 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_factorContext;
20 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_termContext;
21 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParserBaseVisitor;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.YangVersion;
24 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
27 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
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.source.SourceException;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IfFeatureEffectiveStatementImpl;
33
34 public class IfFeatureStatementImpl extends AbstractDeclaredStatement<Predicate<Set<QName>>>
35         implements IfFeatureStatement {
36     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
37             .IF_FEATURE)
38             .build();
39
40     protected IfFeatureStatementImpl(
41             final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> context) {
42         super(context);
43     }
44
45     public static class Definition
46             extends
47             AbstractStatementSupport<Predicate<Set<QName>>, IfFeatureStatement, EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> {
48
49         public Definition() {
50             super(YangStmtMapping.IF_FEATURE);
51         }
52
53         @Override
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);
57             } else {
58                 final QName qName = Utils.qNameFromArgument(ctx, value);
59                 return setQNames -> setQNames.contains(qName);
60             }
61         }
62
63         @Override
64         public IfFeatureStatement createDeclared(
65                 final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> ctx) {
66             return new IfFeatureStatementImpl(ctx);
67         }
68
69         @Override
70         public EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement> createEffective(
71                 final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> ctx) {
72             return new IfFeatureEffectiveStatementImpl(ctx);
73         }
74
75         @Override
76         protected SubstatementValidator getSubstatementValidator() {
77             return SUBSTATEMENT_VALIDATOR;
78         }
79
80         private static Predicate<Set<QName>> parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx, final String value) {
81             final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(new ANTLRInputStream(value));
82             final CommonTokenStream tokens = new CommonTokenStream(lexer);
83             final IfFeatureExpressionParser parser = new IfFeatureExpressionParser(tokens);
84
85             return new IfFeaturePredicateVisitor(ctx).visit(parser.if_feature_expr());
86         }
87
88         private static class IfFeaturePredicateVisitor extends IfFeatureExpressionParserBaseVisitor<Predicate<Set<QName>>> {
89             private final StmtContext<?, ?, ?> stmtCtx;
90
91             public IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
92                 this.stmtCtx = Preconditions.checkNotNull(ctx);
93             }
94
95             @Override
96             public Predicate<Set<QName>> visitIf_feature_expr(final If_feature_exprContext ctx) {
97                 if (ctx.if_feature_expr() != null) {
98                     return visitIf_feature_term(ctx.if_feature_term()).or(visitIf_feature_expr(ctx.if_feature_expr()));
99                 } else {
100                     return visitIf_feature_term(ctx.if_feature_term());
101                 }
102             }
103
104             @Override
105             public Predicate<Set<QName>> visitIf_feature_term(final If_feature_termContext ctx) {
106                 if (ctx.if_feature_term() != null) {
107                     return visitIf_feature_factor(ctx.if_feature_factor()).and(visitIf_feature_term(ctx.if_feature_term()));
108                 } else {
109                     return visitIf_feature_factor(ctx.if_feature_factor());
110                 }
111             }
112
113             @Override
114             public Predicate<Set<QName>> visitIf_feature_factor(final If_feature_factorContext ctx) {
115                 if (ctx.if_feature_expr() != null) {
116                     return visitIf_feature_expr(ctx.if_feature_expr());
117                 } else if (ctx.if_feature_factor() != null) {
118                     return visitIf_feature_factor(ctx.if_feature_factor()).negate();
119                 } else if (ctx.identifier_ref_arg() != null) {
120                     return visitIdentifier_ref_arg(ctx.identifier_ref_arg());
121                 }
122
123                 throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
124                         + "Most probably IfFeature grammar has been changed.", stmtCtx.getStatementSourceReference());
125             }
126
127             @Override
128             public Predicate<Set<QName>> visitIdentifier_ref_arg(final Identifier_ref_argContext ctx) {
129                 final QName featureQName = Utils.qNameFromArgument(stmtCtx, ctx.getText());
130                 return setQNames -> setQNames.contains(featureQName);
131             }
132         }
133     }
134
135     @Override
136     public Predicate<Set<QName>> getIfFeaturePredicate() {
137         return argument();
138     }
139 }