Populate parser/ hierarchy
[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 @NonNullByDefault
33 final class IfFeaturePredicateVisitor extends IfFeatureExpressionParserBaseVisitor<IfFeatureExpr> {
34     private final StmtContext<?, ?, ?> stmtCtx;
35
36     private IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
37         this.stmtCtx = requireNonNull(ctx);
38     }
39
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.sourceReference()));
45
46         return ret;
47     }
48
49     @Override
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()));
54     }
55
56     @Override
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()) {
61             return factor;
62         }
63         final List<IfFeatureExpr> factors = new ArrayList<>(terms.size() + 1);
64         factors.add(factor);
65         for (If_feature_termContext term : terms) {
66             factors.add(visitIf_feature_term(term));
67         }
68         return IfFeatureExpr.and(ImmutableSet.copyOf(factors));
69     }
70
71     @Override
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());
79         }
80
81         throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
82                 + "Most probably IfFeature grammar has been changed.", stmtCtx);
83     }
84
85     @Override
86     public IfFeatureExpr visitIdentifier_ref_arg(final @Nullable Identifier_ref_argContext ctx) {
87         return IfFeatureExpr.isPresent(StmtContextUtils.parseNodeIdentifier(stmtCtx, ctx.getText()));
88     }
89 }