6a0d38e2441384cd049bb8c4fe69855c8213879b
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangStatementParserListenerImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.impl;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
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;
26
27 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
28
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);
35
36     public YangStatementParserListenerImpl(StatementSourceReference ref) {
37         this.ref = ref;
38     }
39
40     public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef) {
41         this.writer = writer;
42         this.stmtDef = stmtDef;
43     }
44
45     public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) {
46         this.writer = writer;
47         this.stmtDef = stmtDef;
48         this.prefixes = prefixes;
49     }
50
51     @Override
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) {
57                 try {
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);
62                     } else {
63                         action = false;
64                         toBeSkipped.add(((YangStatementParser.KeywordContext) child).children.get(0).getText());
65                     }
66                 } catch (SourceException e) {
67                     LOG.warn(e.getMessage(), e);
68                 }
69             } else if (child instanceof YangStatementParser.ArgumentContext) {
70                 try {
71                     if (action)
72                         writer.argumentValue(
73                                 Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref);
74                     else
75                         action = true;
76                 } catch (SourceException e) {
77                     LOG.warn(e.getMessage(), e);
78                 }
79             }
80
81         }
82     }
83
84     @Override
85     public void exitStatement(YangStatementParser.StatementContext ctx) {
86         for (int i = 0; i < ctx.getChildCount(); i++) {
87             ParseTree child = ctx.getChild(i);
88             if (child instanceof YangStatementParser.KeywordContext) {
89                 try {
90                     String statementName = ((YangStatementParser.KeywordContext) child).children.get(0).getText();
91                     QName identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE, statementName);
92                     if (stmtDef != null && stmtDef.get(identifier) != null && toBeSkipped.isEmpty()) {
93                         writer.endStatement(ref);
94                     }
95
96                     if (toBeSkipped.contains(statementName)) {
97                         toBeSkipped.remove(statementName);
98                     }
99                 } catch (SourceException e) {
100                     LOG.warn(e.getMessage(), e);
101                 }
102             }
103         }
104     }
105 }