93354e0371a24a56a25e4e6f1f06c5f6f1edca37
[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.meta.StmtContextUtils;
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(
42             final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> context) {
43         super(context);
44     }
45
46     public static class Definition
47             extends
48             AbstractStatementSupport<Predicate<Set<QName>>, IfFeatureStatement, EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> {
49
50         public Definition() {
51             super(YangStmtMapping.IF_FEATURE);
52         }
53
54         @Override
55         public Predicate<Set<QName>> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
56             if(YangVersion.VERSION_1_1.equals(ctx.getRootVersion())) {
57                 return parseIfFeatureExpression(ctx, value);
58             } else {
59                 final QName qName = StmtContextUtils.qnameFromArgument(ctx, value);
60                 return setQNames -> setQNames.contains(qName);
61             }
62         }
63
64         @Override
65         public IfFeatureStatement createDeclared(
66                 final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> ctx) {
67             return new IfFeatureStatementImpl(ctx);
68         }
69
70         @Override
71         public EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement> createEffective(
72                 final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> ctx) {
73             return new IfFeatureEffectiveStatementImpl(ctx);
74         }
75
76         @Override
77         protected SubstatementValidator getSubstatementValidator() {
78             return SUBSTATEMENT_VALIDATOR;
79         }
80
81         private static Predicate<Set<QName>> parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx, final String value) {
82             final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(new ANTLRInputStream(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 extends IfFeatureExpressionParserBaseVisitor<Predicate<Set<QName>>> {
90             private final StmtContext<?, ?, ?> stmtCtx;
91
92             public 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()).or(visitIf_feature_expr(ctx.if_feature_expr()));
100                 } else {
101                     return visitIf_feature_term(ctx.if_feature_term());
102                 }
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()).and(visitIf_feature_term(ctx.if_feature_term()));
109                 } else {
110                     return visitIf_feature_factor(ctx.if_feature_factor());
111                 }
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 }