Refactor IfFeaturePredicateVisitor
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / if_feature / IfFeaturePredicateParser.java
1 /*
2  * Copyright (c) 2017, 2022 PANTHEON.tech, 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 org.antlr.v4.runtime.CharStreams;
14 import org.antlr.v4.runtime.CommonTokenStream;
15 import org.antlr.v4.runtime.ParserRuleContext;
16 import org.antlr.v4.runtime.tree.TerminalNode;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureExpr;
19 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionLexer;
20 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParser;
21 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParser.Identifier_ref_argContext;
22 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParser.If_feature_exprContext;
23 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParser.If_feature_factorContext;
24 import org.opendaylight.yangtools.yang.parser.antlr.IfFeatureExpressionParser.If_feature_termContext;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.antlr.SourceExceptionParser;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29
30 @NonNullByDefault
31 final class IfFeaturePredicateParser {
32     private final StmtContext<?, ?, ?> stmt;
33
34     private IfFeaturePredicateParser(final StmtContext<?, ?, ?> stmt) {
35         this.stmt = requireNonNull(stmt);
36     }
37
38     static IfFeatureExpr parseIfFeatureExpression(final StmtContext<?, ?, ?> stmt, final String value) {
39         final var lexer = new IfFeatureExpressionLexer(CharStreams.fromString(value));
40         final var parser = new IfFeatureExpressionParser(new CommonTokenStream(lexer));
41         final var ifFeatureExprContext =
42                 SourceExceptionParser.parse(lexer, parser, parser::if_feature_expr, stmt.sourceReference());
43         return new IfFeaturePredicateParser(stmt).parseIfFeatureExpr(ifFeatureExprContext);
44     }
45
46     private IfFeatureExpr parseIfFeatureExpr(final If_feature_exprContext expr) {
47         final int count = verifyExprOrTermChildren(expr);
48         final var expressions = ImmutableSet.<IfFeatureExpr>builderWithExpectedSize(count / 4 + 1);
49         for (int i = 0; i < count; i += 4) {
50             expressions.add(parseIfFeatureTerm(getChild(expr, i, If_feature_termContext.class)));
51         }
52         return IfFeatureExpr.or(expressions.build());
53     }
54
55     private IfFeatureExpr parseIfFeatureTerm(final If_feature_termContext term) {
56         final int count = verifyExprOrTermChildren(term);
57         final var expressions = ImmutableSet.<IfFeatureExpr>builderWithExpectedSize(count / 4 + 1);
58         expressions.add(parseIfFeatureFactor(getChild(term, 0, If_feature_factorContext.class)));
59
60         for (int i = 4; i < count; i += 4) {
61             expressions.add(parseIfFeatureTerm(getChild(term, i, If_feature_termContext.class)));
62         }
63         return IfFeatureExpr.and(expressions.build());
64     }
65
66     private IfFeatureExpr parseIfFeatureFactor(final If_feature_factorContext factor) {
67         final var first = factor.getChild(0);
68         if (first instanceof Identifier_ref_argContext refArg) {
69             return IfFeatureExpr.isPresent(StmtContextUtils.parseNodeIdentifier(stmt, refArg.getText()));
70         } else if (first instanceof TerminalNode terminal) {
71             return switch (terminal.getSymbol().getType()) {
72                 case IfFeatureExpressionParser.LP ->
73                     parseIfFeatureExpr(factor.getChild(If_feature_exprContext.class, 0));
74                 case IfFeatureExpressionParser.NOT ->
75                     parseIfFeatureFactor(getChild(factor, 2, If_feature_factorContext.class)).negate();
76                 default -> throw new SourceException(stmt, "Unexpected terminal %s in sub-expression at %s",
77                     terminal.getText(), factor.getSourceInterval());
78             };
79         } else {
80             throw new SourceException(stmt, "Unexpected error: sub-expression at %s has context %s. Please file a bug "
81                 + "report with the corresponding model attached.", factor.getSourceInterval(), first);
82         }
83     }
84
85     private int verifyExprOrTermChildren(ParserRuleContext context) {
86         final int count = context.getChildCount();
87         if (count % 4 != 1) {
88             throw new SourceException(stmt, "Unexpected error: sub-expression at %s has %s children. Please file a bug "
89                 + "report with the corresponding model attached.", context.getSourceInterval(), count);
90         }
91         return count;
92     }
93
94     private <T> T getChild(final ParserRuleContext parent, final int offset, final Class<T> clazz) {
95         final var child = parent.getChild(offset);
96         if (!clazz.isInstance(child)) {
97             throw new SourceException(stmt, "Unexpected error: sub-expression at %s has child %s at offset %s when "
98                 + "expecting %s. Please file a bug report with the corresponding model attached.",
99                 parent.getSourceInterval(), child, offset, clazz);
100         }
101         return clazz.cast(child);
102     }
103 }