53c6631a6cc70166f1bf91551545c7fbba3b06ae
[yangtools.git] / parser / yang-parser-rfc7950 / 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 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.yang.model.api.stmt.IfFeatureExpr;
20 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionLexer;
21 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParser;
22 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParser.Identifier_ref_argContext;
23 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParser.If_feature_exprContext;
24 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParser.If_feature_factorContext;
25 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParser.If_feature_termContext;
26 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParserBaseVisitor;
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;
31
32 // FIXME: YANGTOOLS-1396: refactor on top of vanilla IfFeatureExpressionParser
33 @NonNullByDefault
34 final class IfFeaturePredicateVisitor extends IfFeatureExpressionParserBaseVisitor<IfFeatureExpr> {
35     private final StmtContext<?, ?, ?> stmtCtx;
36
37     private IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
38         this.stmtCtx = requireNonNull(ctx);
39     }
40
41     static IfFeatureExpr parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx, final String value) {
42         final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(CharStreams.fromString(value));
43         final IfFeatureExpressionParser parser = new IfFeatureExpressionParser(new CommonTokenStream(lexer));
44         final IfFeatureExpr ret = new IfFeaturePredicateVisitor(ctx).visit(SourceExceptionParser.parse(lexer, parser,
45             parser::if_feature_expr, ctx.sourceReference()));
46
47         return ret;
48     }
49
50     @Override
51     public IfFeatureExpr visitIf_feature_expr(final @Nullable If_feature_exprContext ctx) {
52         return IfFeatureExpr.or(ctx.if_feature_term().stream()
53             .map(this::visitIf_feature_term)
54             .collect(ImmutableSet.toImmutableSet()));
55     }
56
57     @Override
58     public IfFeatureExpr visitIf_feature_term(final @Nullable If_feature_termContext ctx) {
59         final IfFeatureExpr factor = visitIf_feature_factor(ctx.if_feature_factor());
60         final List<If_feature_termContext> terms = ctx.if_feature_term();
61         if (terms == null || terms.isEmpty()) {
62             return factor;
63         }
64         final List<IfFeatureExpr> factors = new ArrayList<>(terms.size() + 1);
65         factors.add(factor);
66         for (If_feature_termContext term : terms) {
67             factors.add(visitIf_feature_term(term));
68         }
69         return IfFeatureExpr.and(ImmutableSet.copyOf(factors));
70     }
71
72     @Override
73     public IfFeatureExpr visitIf_feature_factor(final @Nullable If_feature_factorContext ctx) {
74         if (ctx.if_feature_expr() != null) {
75             return visitIf_feature_expr(ctx.if_feature_expr());
76         } else if (ctx.if_feature_factor() != null) {
77             return visitIf_feature_factor(ctx.if_feature_factor()).negate();
78         } else if (ctx.identifier_ref_arg() != null) {
79             return visitIdentifier_ref_arg(ctx.identifier_ref_arg());
80         }
81
82         throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
83                 + "Most probably IfFeature grammar has been changed.", stmtCtx);
84     }
85
86     @Override
87     public IfFeatureExpr visitIdentifier_ref_arg(final @Nullable Identifier_ref_argContext ctx) {
88         return IfFeatureExpr.isPresent(StmtContextUtils.parseNodeIdentifier(stmtCtx, ctx.getText()));
89     }
90 }