Bug 3670 (part 1/5): Use of new statement parser in yang-maven-plugin
[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.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
13 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
14 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import org.antlr.v4.runtime.tree.ParseTree;
18 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
19 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParserBaseListener;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
22 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
23 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
28 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
29
30 import javax.annotation.concurrent.Immutable;
31
32 @Immutable
33 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
34
35     private StatementWriter writer;
36     private String sourceName;
37     private QNameToStatementDefinition stmtDef;
38     private PrefixToModule prefixes;
39     private List<String> toBeSkipped = new ArrayList<>();
40     private boolean isType = false;
41     private static final Logger LOG = LoggerFactory.getLogger(YangStatementParserListenerImpl.class);
42
43     public YangStatementParserListenerImpl(String sourceName) {
44         this.sourceName = sourceName;
45     }
46
47     public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef) {
48         this.writer = writer;
49         this.stmtDef = stmtDef;
50     }
51
52     public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) {
53         this.writer = writer;
54         this.stmtDef = stmtDef;
55         this.prefixes = prefixes;
56     }
57
58     @Override
59     public void enterStatement(YangStatementParser.StatementContext ctx) {
60         final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx
61                 .getStart().getLine(), ctx.getStart().getCharPositionInLine());
62         boolean action = true;
63         QName identifier;
64         for (int i = 0; i < ctx.getChildCount(); i++) {
65             ParseTree child = ctx.getChild(i);
66             if (child instanceof YangStatementParser.KeywordContext) {
67                 try {
68                     identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE,
69                             ((YangStatementParser.KeywordContext) child).children.get(0).getText());
70                     if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier) && toBeSkipped.isEmpty()) {
71                         if (identifier.equals(Rfc6020Mapping.TYPE.getStatementName())) {
72                             isType = true;
73                         } else {
74                             writer.startStatement(identifier, ref);
75                         }
76                     } else {
77                         if (writer.getPhase().equals(ModelProcessingPhase.FULL_DECLARATION)) {
78                             throw new IllegalArgumentException(identifier.getLocalName() + " is not a YANG statement " +
79                                     "or use of extension. Source: " + ref);
80                         } else {
81                             action = false;
82                             toBeSkipped.add(((YangStatementParser.KeywordContext) child).children.get(0).getText());
83                         }
84                     }
85                 } catch (SourceException e) {
86                     LOG.warn(e.getMessage(), e);
87                 }
88             } else if (child instanceof YangStatementParser.ArgumentContext) {
89                 try {
90                     final String argument = Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child);
91                     if (isType) {
92                             if (TypeUtils.isYangTypeBodyStmtString(argument)) {
93                                 writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, argument), ref);
94                             } else {
95                                 writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, Rfc6020Mapping
96                                         .TYPE.getStatementName().getLocalName()), ref);
97                             }
98                         writer.argumentValue(argument, ref);
99                         isType = false;
100                     } else if (action) {
101                         writer.argumentValue(Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref);
102                     } else {
103                         action = true;
104                     }
105                 } catch (SourceException e) {
106                     LOG.warn(e.getMessage(), e);
107                 }
108             }
109
110         }
111     }
112
113     @Override
114     public void exitStatement(YangStatementParser.StatementContext ctx) {
115         final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getStart().getLine(), ctx
116                 .getStart().getCharPositionInLine());
117         for (int i = 0; i < ctx.getChildCount(); i++) {
118             ParseTree child = ctx.getChild(i);
119             if (child instanceof YangStatementParser.KeywordContext) {
120                 try {
121                     String statementName = ((YangStatementParser.KeywordContext) child).children.get(0).getText();
122                     QName identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE, statementName);
123                     if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier) && toBeSkipped.isEmpty()) {
124                         writer.endStatement(ref);
125                     }
126
127                     if (toBeSkipped.contains(statementName)) {
128                         toBeSkipped.remove(statementName);
129                     }
130                 } catch (SourceException e) {
131                     LOG.warn(e.getMessage(), e);
132                 }
133             }
134         }
135     }
136 }