9ae892af3969d669cf3753ec579a6fd91fc6609f
[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.CharStreams;
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.meta.AbstractDeclaredStatement;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IfFeatureEffectiveStatementImpl;
34
35 public class IfFeatureStatementImpl extends AbstractDeclaredStatement<Predicate<Set<QName>>>
36         implements IfFeatureStatement {
37     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
38             .IF_FEATURE)
39             .build();
40
41     protected IfFeatureStatementImpl(final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> context) {
42         super(context);
43     }
44
45     public static class Definition extends AbstractStatementSupport<Predicate<Set<QName>>, IfFeatureStatement,
46             EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> {
47
48         public Definition() {
49             super(YangStmtMapping.IF_FEATURE);
50         }
51
52         @Override
53         public Predicate<Set<QName>> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
54             if (YangVersion.VERSION_1_1.equals(ctx.getRootVersion())) {
55                 return parseIfFeatureExpression(ctx, value);
56             }
57
58             final QName qname = StmtContextUtils.qnameFromArgument(ctx, value);
59             return setQNames -> setQNames.contains(qname);
60         }
61
62         @Override
63         public IfFeatureStatement createDeclared(final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> ctx) {
64             return new IfFeatureStatementImpl(ctx);
65         }
66
67         @Override
68         public EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement> createEffective(
69                 final StmtContext<Predicate<Set<QName>>, IfFeatureStatement,
70                 EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> ctx) {
71             return new IfFeatureEffectiveStatementImpl(ctx);
72         }
73
74         @Override
75         protected SubstatementValidator getSubstatementValidator() {
76             return SUBSTATEMENT_VALIDATOR;
77         }
78
79         private static Predicate<Set<QName>> parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx,
80                 final String value) {
81             final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(CharStreams.fromString(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
89                 extends IfFeatureExpressionParserBaseVisitor<Predicate<Set<QName>>> {
90             private final StmtContext<?, ?, ?> stmtCtx;
91
92             IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
93                 this.stmtCtx = Preconditions.checkNotNull(ctx);
94             }
95
96             @Override
97             public Predicate<Set<QName>> visitIf_feature_expr(final If_feature_exprContext ctx) {
98                 if (ctx.if_feature_expr() == null) {
99                     return visitIf_feature_term(ctx.if_feature_term());
100                 }
101
102                 return visitIf_feature_term(ctx.if_feature_term()).or(visitIf_feature_expr(ctx.if_feature_expr()));
103             }
104
105             @Override
106             public Predicate<Set<QName>> visitIf_feature_term(final If_feature_termContext ctx) {
107                 if (ctx.if_feature_term() == null) {
108                     return visitIf_feature_factor(ctx.if_feature_factor());
109                 }
110
111                 return visitIf_feature_factor(ctx.if_feature_factor()).and(visitIf_feature_term(ctx.if_feature_term()));
112             }
113
114             @Override
115             public Predicate<Set<QName>> visitIf_feature_factor(final If_feature_factorContext ctx) {
116                 if (ctx.if_feature_expr() != null) {
117                     return visitIf_feature_expr(ctx.if_feature_expr());
118                 } else if (ctx.if_feature_factor() != null) {
119                     return visitIf_feature_factor(ctx.if_feature_factor()).negate();
120                 } else if (ctx.identifier_ref_arg() != null) {
121                     return visitIdentifier_ref_arg(ctx.identifier_ref_arg());
122                 }
123
124                 throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
125                         + "Most probably IfFeature grammar has been changed.", stmtCtx.getStatementSourceReference());
126             }
127
128             @Override
129             public Predicate<Set<QName>> visitIdentifier_ref_arg(final Identifier_ref_argContext ctx) {
130                 final QName featureQName = StmtContextUtils.qnameFromArgument(stmtCtx, ctx.getText());
131                 return setQNames -> setQNames.contains(featureQName);
132             }
133         }
134     }
135
136     @Override
137     public Predicate<Set<QName>> getIfFeaturePredicate() {
138         return argument();
139     }
140 }