Revert Merge "Bug 2366: new parser - Types & TypeDefs"
[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 import javax.annotation.concurrent.Immutable;
28
29 @Immutable
30 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
31
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);
38
39     public YangStatementParserListenerImpl(StatementSourceReference ref) {
40         this.ref = ref;
41     }
42
43     public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef) {
44         this.writer = writer;
45         this.stmtDef = stmtDef;
46     }
47
48     public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) {
49         this.writer = writer;
50         this.stmtDef = stmtDef;
51         this.prefixes = prefixes;
52     }
53
54     @Override
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) {
60                 try {
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);
65                     } else {
66                         action = false;
67                         toBeSkipped.add(((YangStatementParser.KeywordContext) child).children.get(0).getText());
68                     }
69                 } catch (SourceException e) {
70                     LOG.warn(e.getMessage(), e);
71                 }
72             } else if (child instanceof YangStatementParser.ArgumentContext) {
73                 try {
74                     if (action) {
75                         writer.argumentValue(
76                                 Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref);
77                     } else {
78                         action = true;
79                     }
80                 } catch (SourceException e) {
81                     LOG.warn(e.getMessage(), e);
82                 }
83             }
84
85         }
86     }
87
88     @Override
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) {
93                 try {
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);
98                     }
99
100                     if (toBeSkipped.contains(statementName)) {
101                         toBeSkipped.remove(statementName);
102                     }
103                 } catch (SourceException e) {
104                     LOG.warn(e.getMessage(), e);
105                 }
106             }
107         }
108     }
109 }