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.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionLexer;
19 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser;
20 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.Identifier_ref_argContext;
21 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_exprContext;
22 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_factorContext;
23 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_termContext;
24 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParserBaseVisitor;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.antlr.SourceExceptionParser;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 final class IfFeaturePredicateVisitor extends IfFeatureExpressionParserBaseVisitor<Predicate<Set<QName>>> {
33 private final StmtContext<?, ?, ?> stmtCtx;
35 private IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
36 this.stmtCtx = requireNonNull(ctx);
39 static Predicate<Set<QName>> parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx, final String value) {
40 final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(CharStreams.fromString(value));
41 final IfFeatureExpressionParser parser = new IfFeatureExpressionParser(new CommonTokenStream(lexer));
42 return new IfFeaturePredicateVisitor(ctx).visit(SourceExceptionParser.parse(lexer, parser,
43 parser::if_feature_expr, ctx.getStatementSourceReference()));
47 public Predicate<Set<QName>> visitIf_feature_expr(final @Nullable If_feature_exprContext ctx) {
48 final Predicate<Set<QName>> term = visitIf_feature_term(ctx.if_feature_term());
49 final If_feature_exprContext expr = ctx.if_feature_expr();
50 return expr == null ? term : term.or(visitIf_feature_expr(expr));
54 public Predicate<Set<QName>> visitIf_feature_term(final @Nullable If_feature_termContext ctx) {
55 final Predicate<Set<QName>> factor = visitIf_feature_factor(ctx.if_feature_factor());
56 final If_feature_termContext term = ctx.if_feature_term();
57 return term == null ? factor : factor.and(visitIf_feature_term(term));
61 public Predicate<Set<QName>> visitIf_feature_factor(final @Nullable If_feature_factorContext ctx) {
62 if (ctx.if_feature_expr() != null) {
63 return visitIf_feature_expr(ctx.if_feature_expr());
64 } else if (ctx.if_feature_factor() != null) {
65 return visitIf_feature_factor(ctx.if_feature_factor()).negate();
66 } else if (ctx.identifier_ref_arg() != null) {
67 return visitIdentifier_ref_arg(ctx.identifier_ref_arg());
70 throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
71 + "Most probably IfFeature grammar has been changed.", stmtCtx.getStatementSourceReference());
75 public Predicate<Set<QName>> visitIdentifier_ref_arg(final @Nullable Identifier_ref_argContext ctx) {
76 final QName featureQName = StmtContextUtils.parseNodeIdentifier(stmtCtx, ctx.getText());
77 return setQNames -> setQNames.contains(featureQName);