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.CharStreams;
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.meta.AbstractDeclaredStatement;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IfFeatureEffectiveStatementImpl;
35 public class IfFeatureStatementImpl extends AbstractDeclaredStatement<Predicate<Set<QName>>>
36 implements IfFeatureStatement {
37 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
41 protected IfFeatureStatementImpl(final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> context) {
45 public static class Definition extends AbstractStatementSupport<Predicate<Set<QName>>, IfFeatureStatement,
46 EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> {
49 super(YangStmtMapping.IF_FEATURE);
53 public Predicate<Set<QName>> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
54 if (YangVersion.VERSION_1_1.equals(ctx.getRootVersion())) {
55 return parseIfFeatureExpression(ctx, value);
58 final QName qname = StmtContextUtils.qnameFromArgument(ctx, value);
59 return setQNames -> setQNames.contains(qname);
63 public IfFeatureStatement createDeclared(final StmtContext<Predicate<Set<QName>>, IfFeatureStatement, ?> ctx) {
64 return new IfFeatureStatementImpl(ctx);
68 public EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement> createEffective(
69 final StmtContext<Predicate<Set<QName>>, IfFeatureStatement,
70 EffectiveStatement<Predicate<Set<QName>>, IfFeatureStatement>> ctx) {
71 return new IfFeatureEffectiveStatementImpl(ctx);
75 protected SubstatementValidator getSubstatementValidator() {
76 return SUBSTATEMENT_VALIDATOR;
79 private static Predicate<Set<QName>> parseIfFeatureExpression(final StmtContext<?, ?, ?> ctx,
81 final IfFeatureExpressionLexer lexer = new IfFeatureExpressionLexer(CharStreams.fromString(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
89 extends IfFeatureExpressionParserBaseVisitor<Predicate<Set<QName>>> {
90 private final StmtContext<?, ?, ?> stmtCtx;
92 IfFeaturePredicateVisitor(final StmtContext<?, ?, ?> ctx) {
93 this.stmtCtx = Preconditions.checkNotNull(ctx);
97 public Predicate<Set<QName>> visitIf_feature_expr(final If_feature_exprContext ctx) {
98 if (ctx.if_feature_expr() == null) {
99 return visitIf_feature_term(ctx.if_feature_term());
102 return visitIf_feature_term(ctx.if_feature_term()).or(visitIf_feature_expr(ctx.if_feature_expr()));
106 public Predicate<Set<QName>> visitIf_feature_term(final If_feature_termContext ctx) {
107 if (ctx.if_feature_term() == null) {
108 return visitIf_feature_factor(ctx.if_feature_factor());
111 return visitIf_feature_factor(ctx.if_feature_factor()).and(visitIf_feature_term(ctx.if_feature_term()));
115 public Predicate<Set<QName>> visitIf_feature_factor(final If_feature_factorContext ctx) {
116 if (ctx.if_feature_expr() != null) {
117 return visitIf_feature_expr(ctx.if_feature_expr());
118 } else if (ctx.if_feature_factor() != null) {
119 return visitIf_feature_factor(ctx.if_feature_factor()).negate();
120 } else if (ctx.identifier_ref_arg() != null) {
121 return visitIdentifier_ref_arg(ctx.identifier_ref_arg());
124 throw new SourceException("Unexpected grammar context during parsing of IfFeature expression. "
125 + "Most probably IfFeature grammar has been changed.", stmtCtx.getStatementSourceReference());
129 public Predicate<Set<QName>> visitIdentifier_ref_arg(final Identifier_ref_argContext ctx) {
130 final QName featureQName = StmtContextUtils.qnameFromArgument(stmtCtx, ctx.getText());
131 return setQNames -> setQNames.contains(featureQName);
137 public Predicate<Set<QName>> getIfFeaturePredicate() {