2 * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
10 import com.google.common.base.Preconditions;
12 import java.util.function.Predicate;
13 import org.antlr.v4.runtime.ANTLRInputStream;
14 import org.antlr.v4.runtime.CommonTokenStream;
15 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionLexer;
16 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser;
17 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.Identifier_ref_argContext;
18 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_exprContext;
19 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_factorContext;
20 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParser.If_feature_termContext;
21 import org.opendaylight.yangtools.antlrv4.code.gen.IfFeatureExpressionParserBaseVisitor;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.YangVersion;
24 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
27 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IfFeatureEffectiveStatementImpl;
34 public class IfFeatureStatementImpl extends AbstractDeclaredStatement<Predicate<Set<QName>>>
35 implements IfFeatureStatement {
36 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
40 protected IfFeatureStatementImpl(
41 final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> context) {
45 public static class Definition
47 AbstractStatementSupport<Predicate<Set<QName>>, IfFeatureStatement, EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> {
50 super(YangStmtMapping.IF_FEATURE);
54 public Predicate<Set<QName>> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
55 if(YangVersion.VERSION_1_1.equals(ctx.getRootVersion())) {
56 return parseIfFeatureExpression(ctx, value);
58 final QName qName = Utils.qNameFromArgument(ctx, value);
59 return setQNames -> setQNames.contains(qName);
64 public IfFeatureStatement createDeclared(
65 final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> ctx) {
66 return new IfFeatureStatementImpl(ctx);
70 public EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement> createEffective(
71 final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> ctx) {
72 return new IfFeatureEffectiveStatementImpl(ctx);
76 protected SubstatementValidator getSubstatementValidator() {
77 return SUBSTATEMENT_VALIDATOR;
80 private static Predicate<Set<QName>> parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx, final String value) {
81 final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(new ANTLRInputStream(value));
82 final CommonTokenStream tokens = new CommonTokenStream(lexer);
83 final IfFeatureExpressionParser parser = new IfFeatureExpressionParser(tokens);
85 return new IfFeaturePredicateVisitor(ctx).visit(parser.if_feature_expr());
88 private static class IfFeaturePredicateVisitor extends IfFeatureExpressionParserBaseVisitor<Predicate<Set<QName>>> {
89 private final StmtContext<?, ?, ?> stmtCtx;
91 public IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
92 this.stmtCtx = Preconditions.checkNotNull(ctx);
96 public Predicate<Set<QName>> visitIf_feature_expr(final If_feature_exprContext ctx) {
97 if (ctx.if_feature_expr() != null) {
98 return visitIf_feature_term(ctx.if_feature_term()).or(visitIf_feature_expr(ctx.if_feature_expr()));
100 return visitIf_feature_term(ctx.if_feature_term());
105 public Predicate<Set<QName>> visitIf_feature_term(final If_feature_termContext ctx) {
106 if (ctx.if_feature_term() != null) {
107 return visitIf_feature_factor(ctx.if_feature_factor()).and(visitIf_feature_term(ctx.if_feature_term()));
109 return visitIf_feature_factor(ctx.if_feature_factor());
114 public Predicate<Set<QName>> visitIf_feature_factor(final If_feature_factorContext ctx) {
115 if (ctx.if_feature_expr() != null) {
116 return visitIf_feature_expr(ctx.if_feature_expr());
117 } else if (ctx.if_feature_factor() != null) {
118 return visitIf_feature_factor(ctx.if_feature_factor()).negate();
119 } else if (ctx.identifier_ref_arg() != null) {
120 return visitIdentifier_ref_arg(ctx.identifier_ref_arg());
123 throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
124 + "Most probably IfFeature grammar has been changed.", stmtCtx.getStatementSourceReference());
128 public Predicate<Set<QName>> visitIdentifier_ref_arg(final Identifier_ref_argContext ctx) {
129 final QName featureQName = Utils.qNameFromArgument(stmtCtx, ctx.getText());
130 return setQNames -> setQNames.contains(featureQName);
136 public Predicate<Set<QName>> getIfFeaturePredicate() {