cfc3546472b0e5e54ee4b27051c070407e3450ad
[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
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;
24
25 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
26
27     private StatementWriter writer;
28     private StatementSourceReference ref;
29     private QNameToStatementDefinition stmtDef;
30     private PrefixToModule prefixes;
31     private List<String> toBeSkipped = new ArrayList<>();
32
33     public YangStatementParserListenerImpl(StatementSourceReference ref) {
34         this.ref = ref;
35     }
36
37     public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef) {
38         this.writer = writer;
39         this.stmtDef = stmtDef;
40     }
41
42     public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) {
43         this.writer = writer;
44         this.stmtDef = stmtDef;
45         this.prefixes = prefixes;
46     }
47
48     @Override
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) {
54                 try {
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);
59                     } else {
60                         action = false;
61                         toBeSkipped.add(((YangStatementParser.KeywordContext) child).children.get(0).getText());
62                     }
63                 } catch (SourceException e) {
64                     e.printStackTrace();
65                 }
66             } else if (child instanceof YangStatementParser.ArgumentContext) {
67                 try {
68                     if (action)
69                         writer.argumentValue(
70                                 Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref);
71                     else
72                         action = true;
73                 } catch (SourceException e) {
74                     e.printStackTrace();
75                 }
76             }
77
78         }
79     }
80
81     @Override
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) {
86                 try {
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);
91                     }
92
93                     if (toBeSkipped.contains(statementName)) {
94                         toBeSkipped.remove(statementName);
95                     }
96                 } catch (SourceException e) {
97                     e.printStackTrace();
98                 }
99             }
100         }
101     }
102 }