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 java.util.ArrayList;
11 import java.util.List;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
15 import org.antlr.v4.runtime.tree.ParseTree;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
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.source.PrefixToModule;
21 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
24 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
27 import javax.annotation.concurrent.Immutable;
30 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
32 private StatementWriter writer;
33 private StatementSourceReference ref;
34 private QNameToStatementDefinition stmtDef;
35 private PrefixToModule prefixes;
36 private List<String> toBeSkipped = new ArrayList<>();
37 private static final Logger LOG = LoggerFactory.getLogger(YangStatementParserListenerImpl.class);
39 public YangStatementParserListenerImpl(StatementSourceReference ref) {
43 public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef) {
45 this.stmtDef = stmtDef;
48 public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) {
50 this.stmtDef = stmtDef;
51 this.prefixes = prefixes;
55 public void enterStatement(YangStatementParser.StatementContext ctx) {
56 boolean action = true;
57 for (int i = 0; i < ctx.getChildCount(); i++) {
58 ParseTree child = ctx.getChild(i);
59 if (child instanceof YangStatementParser.KeywordContext) {
61 QName identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE,
62 ((YangStatementParser.KeywordContext) child).children.get(0).getText());
63 if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier) && toBeSkipped.isEmpty()) {
64 writer.startStatement(identifier, ref);
67 toBeSkipped.add(((YangStatementParser.KeywordContext) child).children.get(0).getText());
69 } catch (SourceException e) {
70 LOG.warn(e.getMessage(), e);
72 } else if (child instanceof YangStatementParser.ArgumentContext) {
76 Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref);
80 } catch (SourceException e) {
81 LOG.warn(e.getMessage(), e);
89 public void exitStatement(YangStatementParser.StatementContext ctx) {
90 for (int i = 0; i < ctx.getChildCount(); i++) {
91 ParseTree child = ctx.getChild(i);
92 if (child instanceof YangStatementParser.KeywordContext) {
94 String statementName = ((YangStatementParser.KeywordContext) child).children.get(0).getText();
95 QName identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE, statementName);
96 if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier) && toBeSkipped.isEmpty()) {
97 writer.endStatement(ref);
100 if (toBeSkipped.contains(statementName)) {
101 toBeSkipped.remove(statementName);
103 } catch (SourceException e) {
104 LOG.warn(e.getMessage(), e);