Merge "tests for arguments parsing parsing Augment in Uses (except extensions)"
[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                     }
77                 } catch (SourceException e) {
78                     LOG.warn(e.getMessage(), e);
79                 }
80             }
81
82         }
83     }
84
85     @Override
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) {
90                 try {
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);
95                     }
96
97                     if (toBeSkipped.contains(statementName)) {
98                         toBeSkipped.remove(statementName);
99                     }
100                 } catch (SourceException e) {
101                     LOG.warn(e.getMessage(), e);
102                 }
103             }
104         }
105     }
106 }