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;
13 import org.antlr.v4.runtime.tree.ParseTree;
14 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
15 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParserBaseListener;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.YangConstants;
18 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
19 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
22 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
25 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
27 private StatementWriter writer;
28 private StatementSourceReference ref;
29 private QNameToStatementDefinition stmtDef;
30 private PrefixToModule prefixes;
31 private List<String> toBeSkipped = new ArrayList<>();
33 public YangStatementParserListenerImpl(StatementSourceReference ref) {
37 public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef) {
39 this.stmtDef = stmtDef;
42 public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) {
44 this.stmtDef = stmtDef;
45 this.prefixes = prefixes;
49 public void enterStatement(YangStatementParser.StatementContext ctx) {
50 boolean action = true;
51 for (int i = 0; i < ctx.getChildCount(); i++) {
52 ParseTree child = ctx.getChild(i);
53 if (child instanceof YangStatementParser.KeywordContext) {
55 QName identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE,
56 ((YangStatementParser.KeywordContext) child).children.get(0).getText());
57 if (stmtDef != null && stmtDef.get(identifier) != null && toBeSkipped.isEmpty()) {
58 writer.startStatement(identifier, ref);
61 toBeSkipped.add(((YangStatementParser.KeywordContext) child).children.get(0).getText());
63 } catch (SourceException e) {
66 } else if (child instanceof YangStatementParser.ArgumentContext) {
70 Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref);
73 } catch (SourceException e) {
82 public void exitStatement(YangStatementParser.StatementContext ctx) {
83 for (int i = 0; i < ctx.getChildCount(); i++) {
84 ParseTree child = ctx.getChild(i);
85 if (child instanceof YangStatementParser.KeywordContext) {
87 String statementName = ((YangStatementParser.KeywordContext) child).children.get(0).getText();
88 QName identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE, statementName);
89 if (stmtDef != null && stmtDef.get(identifier) != null && toBeSkipped.isEmpty()) {
90 writer.endStatement(ref);
93 if (toBeSkipped.contains(statementName)) {
94 toBeSkipped.remove(statementName);
96 } catch (SourceException e) {