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 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
29 private StatementWriter writer;
30 private StatementSourceReference ref;
31 private QNameToStatementDefinition stmtDef;
32 private PrefixToModule prefixes;
33 private List<String> toBeSkipped = new ArrayList<>();
34 private static final Logger LOG = LoggerFactory.getLogger(YangStatementParserListenerImpl.class);
36 public YangStatementParserListenerImpl(StatementSourceReference ref) {
40 public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef) {
42 this.stmtDef = stmtDef;
45 public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) {
47 this.stmtDef = stmtDef;
48 this.prefixes = prefixes;
52 public void enterStatement(YangStatementParser.StatementContext ctx) {
53 boolean action = true;
54 for (int i = 0; i < ctx.getChildCount(); i++) {
55 ParseTree child = ctx.getChild(i);
56 if (child instanceof YangStatementParser.KeywordContext) {
58 QName identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE,
59 ((YangStatementParser.KeywordContext) child).children.get(0).getText());
60 if (stmtDef != null && stmtDef.get(identifier) != null && toBeSkipped.isEmpty()) {
61 writer.startStatement(identifier, ref);
64 toBeSkipped.add(((YangStatementParser.KeywordContext) child).children.get(0).getText());
66 } catch (SourceException e) {
67 LOG.warn(e.getMessage(), e);
69 } else if (child instanceof YangStatementParser.ArgumentContext) {
73 Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref);
77 } catch (SourceException e) {
78 LOG.warn(e.getMessage(), e);
86 public void exitStatement(YangStatementParser.StatementContext ctx) {
87 for (int i = 0; i < ctx.getChildCount(); i++) {
88 ParseTree child = ctx.getChild(i);
89 if (child instanceof YangStatementParser.KeywordContext) {
91 String statementName = ((YangStatementParser.KeywordContext) child).children.get(0).getText();
92 QName identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE, statementName);
93 if (stmtDef != null && stmtDef.get(identifier) != null && toBeSkipped.isEmpty()) {
94 writer.endStatement(ref);
97 if (toBeSkipped.contains(statementName)) {
98 toBeSkipped.remove(statementName);
100 } catch (SourceException e) {
101 LOG.warn(e.getMessage(), e);