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;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.antlr.v4.runtime.CharStreams;
16 import org.antlr.v4.runtime.CommonTokenStream;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionLexer;
20 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser;
21 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.Identifier_ref_argContext;
22 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_exprContext;
23 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_factorContext;
24 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_termContext;
25 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParserBaseVisitor;
26 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureExpr;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.antlr.SourceExceptionParser;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 final class IfFeaturePredicateVisitor extends IfFeatureExpressionParserBaseVisitor<IfFeatureExpr> {
34 private final StmtContext<?, ?, ?> stmtCtx;
36 private IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
37 this.stmtCtx = requireNonNull(ctx);
40 static IfFeatureExpr parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx, final String value) {
41 final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(CharStreams.fromString(value));
42 final IfFeatureExpressionParser parser = new IfFeatureExpressionParser(new CommonTokenStream(lexer));
43 final IfFeatureExpr ret = new IfFeaturePredicateVisitor(ctx).visit(SourceExceptionParser.parse(lexer, parser,
44 parser::if_feature_expr, ctx.getStatementSourceReference()));
50 public IfFeatureExpr visitIf_feature_expr(final @Nullable If_feature_exprContext ctx) {
51 return IfFeatureExpr.or(ctx.if_feature_term().stream()
52 .map(this::visitIf_feature_term)
53 .collect(ImmutableSet.toImmutableSet()));
57 public IfFeatureExpr visitIf_feature_term(final @Nullable If_feature_termContext ctx) {
58 final IfFeatureExpr factor = visitIf_feature_factor(ctx.if_feature_factor());
59 final List<If_feature_termContext> terms = ctx.if_feature_term();
60 if (terms == null || terms.isEmpty()) {
63 final List<IfFeatureExpr> factors = new ArrayList<>(terms.size() + 1);
65 for (If_feature_termContext term : terms) {
66 factors.add(visitIf_feature_term(term));
68 return IfFeatureExpr.and(ImmutableSet.copyOf(factors));
72 public IfFeatureExpr visitIf_feature_factor(final @Nullable If_feature_factorContext ctx) {
73 if (ctx.if_feature_expr() != null) {
74 return visitIf_feature_expr(ctx.if_feature_expr());
75 } else if (ctx.if_feature_factor() != null) {
76 return visitIf_feature_factor(ctx.if_feature_factor()).negate();
77 } else if (ctx.identifier_ref_arg() != null) {
78 return visitIdentifier_ref_arg(ctx.identifier_ref_arg());
81 throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
82 + "Most probably IfFeature grammar has been changed.", stmtCtx.getStatementSourceReference());
86 public IfFeatureExpr visitIdentifier_ref_arg(final @Nullable Identifier_ref_argContext ctx) {
87 return IfFeatureExpr.isPresent(StmtContextUtils.parseNodeIdentifier(stmtCtx, ctx.getText()));