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.ArrayDeque;
12 import java.util.ArrayList;
13 import java.util.Deque;
14 import java.util.List;
15 import javax.annotation.concurrent.Immutable;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.ArgumentContext;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.KeywordContext;
18 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.StatementContext;
19 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParserBaseListener;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
23 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
24 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
25 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
32 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
33 private static final class Counter {
34 private int value = 0;
36 int getAndIncrement() {
41 private final List<String> toBeSkipped = new ArrayList<>();
42 private final Deque<Counter> counters = new ArrayDeque<>();
43 private final String sourceName;
44 private QNameToStatementDefinition stmtDef;
45 private PrefixToModule prefixes;
46 private StatementWriter writer;
48 public YangStatementParserListenerImpl(final String sourceName) {
49 this.sourceName = sourceName;
52 public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
54 this.stmtDef = stmtDef;
58 public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef,
59 final PrefixToModule prefixes) {
61 this.stmtDef = stmtDef;
62 this.prefixes = prefixes;
66 private void initCounters() {
68 counters.push(new Counter());
72 public void enterStatement(final StatementContext ctx) {
73 final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx
74 .getStart().getLine(), ctx.getStart().getCharPositionInLine());
75 final String keywordTxt = Verify.verifyNotNull(ctx.getChild(KeywordContext.class, 0)).getText();
76 final QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, keywordTxt);
77 final QName validStatementDefinition = Utils.getValidStatementDefinition(prefixes, stmtDef, identifier);
79 final int childId = counters.peek().getAndIncrement();
80 counters.push(new Counter());
81 if (stmtDef == null || validStatementDefinition == null || !toBeSkipped.isEmpty()) {
82 SourceException.throwIf(writer.getPhase() == ModelProcessingPhase.FULL_DECLARATION, ref,
83 "%s is not a YANG statement or use of extension.", keywordTxt);
84 toBeSkipped.add(keywordTxt);
88 final ArgumentContext argumentCtx = ctx.getChild(ArgumentContext.class, 0);
89 final String argument = argumentCtx != null ? Utils.stringFromStringContext(argumentCtx) : null;
90 writer.startStatement(childId, validStatementDefinition, argument, ref);
94 public void exitStatement(final StatementContext ctx) {
95 final StatementSourceReference ref = DeclarationInTextSource.atPosition(
96 sourceName, ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
98 KeywordContext keyword = ctx.getChild(KeywordContext.class, 0);
99 String statementName = keyword.getText();
100 QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, statementName);
101 if (stmtDef != null && Utils.getValidStatementDefinition(prefixes, stmtDef, identifier) != null
102 && toBeSkipped.isEmpty()) {
103 writer.endStatement(ref);
106 // No-op if the statement is not on the list
107 toBeSkipped.remove(statementName);