2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.if_feature;
10 import static java.util.Objects.requireNonNull;
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;
28 final class IfFeaturePredicateVisitor extends IfFeatureExpressionParserBaseVisitor<Predicate<Set<QName>>> {
29 private final StmtContext<?, ?, ?> stmtCtx;
31 private IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
32 this.stmtCtx = requireNonNull(ctx);
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);
40 return new IfFeaturePredicateVisitor(ctx).visit(parser.if_feature_expr());
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());
49 return visitIf_feature_term(ctx.if_feature_term()).or(visitIf_feature_expr(ctx.if_feature_expr()));
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());
58 return visitIf_feature_factor(ctx.if_feature_factor()).and(visitIf_feature_term(ctx.if_feature_term()));
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());
71 throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
72 + "Most probably IfFeature grammar has been changed.", stmtCtx.getStatementSourceReference());
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);