YANGTOOLS-706: Split up base utility classes into rfc6020.util
[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 static java.util.Objects.requireNonNull;
11
12 import java.util.Set;
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;
35
36 public class IfFeatureStatementImpl extends AbstractDeclaredStatement<Predicate<Set<QName>>>
37         implements IfFeatureStatement {
38     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
39             .IF_FEATURE)
40             .build();
41
42     protected IfFeatureStatementImpl(final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> context) {
43         super(context);
44     }
45
46     public static class Definition extends AbstractStatementSupport<Predicate<Set<QName>>, IfFeatureStatement,
47             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             }
58
59             final QName qname = StmtContextUtils.qnameFromArgument(ctx, value);
60             return setQNames -> setQNames.contains(qname);
61         }
62
63         @Override
64         public IfFeatureStatement createDeclared(final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> ctx) {
65             return new IfFeatureStatementImpl(ctx);
66         }
67
68         @Override
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);
73         }
74
75         @Override
76         protected SubstatementValidator getSubstatementValidator() {
77             return SUBSTATEMENT_VALIDATOR;
78         }
79
80         private static Predicate<Set<QName>> parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx,
81                 final String value) {
82             final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(CharStreams.fromString(value));
83             final CommonTokenStream tokens = new CommonTokenStream(lexer);
84             final IfFeatureExpressionParser parser = new IfFeatureExpressionParser(tokens);
85
86             return new IfFeaturePredicateVisitor(ctx).visit(parser.if_feature_expr());
87         }
88
89         private static class IfFeaturePredicateVisitor
90                 extends IfFeatureExpressionParserBaseVisitor<Predicate<Set<QName>>> {
91             private final StmtContext<?, ?, ?> stmtCtx;
92
93             IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
94                 this.stmtCtx = requireNonNull(ctx);
95             }
96
97             @Override
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());
101                 }
102
103                 return visitIf_feature_term(ctx.if_feature_term()).or(visitIf_feature_expr(ctx.if_feature_expr()));
104             }
105
106             @Override
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());
110                 }
111
112                 return visitIf_feature_factor(ctx.if_feature_factor()).and(visitIf_feature_term(ctx.if_feature_term()));
113             }
114
115             @Override
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());
123                 }
124
125                 throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
126                         + "Most probably IfFeature grammar has been changed.", stmtCtx.getStatementSourceReference());
127             }
128
129             @Override
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);
133             }
134         }
135     }
136
137     @Override
138     public Predicate<Set<QName>> getIfFeaturePredicate() {
139         return argument();
140     }
141 }