05d8ed7c61a702f92ee8ddf5760a43a7f653fe3e
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / if_feature / IfFeaturePredicateVisitor.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.rfc7950.stmt.if_feature;
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.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27
28 final class IfFeaturePredicateVisitor extends IfFeatureExpressionParserBaseVisitor<Predicate<Set<QName>>> {
29     private final StmtContext<?, ?, ?> stmtCtx;
30
31     private IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
32         this.stmtCtx = requireNonNull(ctx);
33     }
34
35     static Predicate<Set<QName>> parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx, final String value) {
36         final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(CharStreams.fromString(value));
37         final CommonTokenStream tokens = new CommonTokenStream(lexer);
38         final IfFeatureExpressionParser parser = new IfFeatureExpressionParser(tokens);
39
40         return new IfFeaturePredicateVisitor(ctx).visit(parser.if_feature_expr());
41     }
42
43     @Override
44     public Predicate<Set<QName>> visitIf_feature_expr(final If_feature_exprContext ctx) {
45         if (ctx.if_feature_expr() == null) {
46             return visitIf_feature_term(ctx.if_feature_term());
47         }
48
49         return visitIf_feature_term(ctx.if_feature_term()).or(visitIf_feature_expr(ctx.if_feature_expr()));
50     }
51
52     @Override
53     public Predicate<Set<QName>> visitIf_feature_term(final If_feature_termContext ctx) {
54         if (ctx.if_feature_term() == null) {
55             return visitIf_feature_factor(ctx.if_feature_factor());
56         }
57
58         return visitIf_feature_factor(ctx.if_feature_factor()).and(visitIf_feature_term(ctx.if_feature_term()));
59     }
60
61     @Override
62     public Predicate<Set<QName>> visitIf_feature_factor(final If_feature_factorContext ctx) {
63         if (ctx.if_feature_expr() != null) {
64             return visitIf_feature_expr(ctx.if_feature_expr());
65         } else if (ctx.if_feature_factor() != null) {
66             return visitIf_feature_factor(ctx.if_feature_factor()).negate();
67         } else if (ctx.identifier_ref_arg() != null) {
68             return visitIdentifier_ref_arg(ctx.identifier_ref_arg());
69         }
70
71         throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
72                 + "Most probably IfFeature grammar has been changed.", stmtCtx.getStatementSourceReference());
73     }
74
75     @Override
76     public Predicate<Set<QName>> visitIdentifier_ref_arg(final Identifier_ref_argContext ctx) {
77         final QName featureQName = StmtContextUtils.qnameFromArgument(stmtCtx, ctx.getText());
78         return setQNames -> setQNames.contains(featureQName);
79     }
80 }