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.impl;
10 import com.google.common.base.Verify;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.annotation.concurrent.Immutable;
14 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.ArgumentContext;
15 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.KeywordContext;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.StatementContext;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParserBaseListener;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.YangConstants;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
21 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
22 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
23 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
30 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
31 private final List<String> toBeSkipped = new ArrayList<>();
32 private final String sourceName;
33 private QNameToStatementDefinition stmtDef;
34 private PrefixToModule prefixes;
35 private StatementWriter writer;
37 public YangStatementParserListenerImpl(final String sourceName) {
38 this.sourceName = sourceName;
41 public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
43 this.stmtDef = stmtDef;
46 public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef, final PrefixToModule prefixes) {
48 this.stmtDef = stmtDef;
49 this.prefixes = prefixes;
53 public void enterStatement(final StatementContext ctx) {
54 final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx
55 .getStart().getLine(), ctx.getStart().getCharPositionInLine());
56 final String keywordTxt = Verify.verifyNotNull(ctx.getChild(KeywordContext.class, 0)).getText();
57 final QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, keywordTxt);
58 final QName validStatementDefinition = Utils.getValidStatementDefinition(prefixes, stmtDef, identifier);
60 if (stmtDef == null || validStatementDefinition == null || !toBeSkipped.isEmpty()) {
61 SourceException.throwIf(writer.getPhase() == ModelProcessingPhase.FULL_DECLARATION, ref,
62 "%s is not a YANG statement or use of extension.", keywordTxt);
63 toBeSkipped.add(keywordTxt);
67 final ArgumentContext argumentCtx = ctx.getChild(ArgumentContext.class, 0);
68 final String argument = argumentCtx != null ? Utils.stringFromStringContext(argumentCtx) : null;
69 writer.startStatement(validStatementDefinition, argument, ref);
73 public void exitStatement(final StatementContext ctx) {
74 final StatementSourceReference ref = DeclarationInTextSource.atPosition(
75 sourceName, ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
77 KeywordContext keyword = ctx.getChild(KeywordContext.class, 0);
78 String statementName = keyword.getText();
79 QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, statementName);
80 if (stmtDef != null && Utils.getValidStatementDefinition(prefixes, stmtDef, identifier) != null
81 && toBeSkipped.isEmpty()) {
82 writer.endStatement(ref);
85 // No-op if the statement is not on the list
86 toBeSkipped.remove(statementName);