075269e1d2edd27b8514fe9a96b832e750e307cb
[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 com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.annotation.concurrent.Immutable;
15 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.ArgumentContext;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.KeywordContext;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.StatementContext;
18 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParserBaseListener;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.YangConstants;
21 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
23 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
24 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
25 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 @Immutable
35 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
36     private static final Logger LOG = LoggerFactory.getLogger(YangStatementParserListenerImpl.class);
37
38     private final List<String> toBeSkipped = new ArrayList<>();
39     private final String sourceName;
40     private QNameToStatementDefinition stmtDef;
41     private PrefixToModule prefixes;
42     private StatementWriter writer;
43
44     public YangStatementParserListenerImpl(final String sourceName) {
45         this.sourceName = sourceName;
46     }
47
48     public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
49         this.writer = writer;
50         this.stmtDef = stmtDef;
51     }
52
53     public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef, final PrefixToModule prefixes) {
54         this.writer = writer;
55         this.stmtDef = stmtDef;
56         this.prefixes = prefixes;
57     }
58
59     @Override
60     public void enterStatement(final StatementContext ctx) {
61         final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx
62                 .getStart().getLine(), ctx.getStart().getCharPositionInLine());
63         final KeywordContext keywordCtx = Verify.verifyNotNull(ctx.getChild(KeywordContext.class, 0));
64         final ArgumentContext argumentCtx = ctx.getChild(ArgumentContext.class, 0);
65         final String keywordTxt = keywordCtx.getText();
66
67         // FIXME: This is broken for extensions - not all statements are in RFC6020 ns.
68         final QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, keywordTxt);
69         if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier)
70                 && toBeSkipped.isEmpty()) {
71             final String argument = argumentCtx != null ? Utils.stringFromStringContext(argumentCtx) : null;
72             // FIXME: this looks like a fishy special case
73             if (identifier.equals(Rfc6020Mapping.TYPE.getStatementName())) {
74                 Preconditions.checkArgument(argument != null);
75                 if (TypeUtils.isYangTypeBodyStmtString(argument)) {
76                     writer.startStatement(QName.create(YangConstants.RFC6020_YIN_MODULE, argument), ref);
77                 } else {
78                     writer.startStatement(QName.create(YangConstants.RFC6020_YIN_MODULE, Rfc6020Mapping
79                         .TYPE.getStatementName().getLocalName()), ref);
80                 }
81                 writer.argumentValue(argument, ref);
82             } else {
83                 writer.startStatement(identifier, ref);
84                 if(argument != null) {
85                     writer.argumentValue(argument, ref);
86                 }
87             }
88         } else {
89             Preconditions.checkArgument(writer.getPhase() != ModelProcessingPhase.FULL_DECLARATION,
90                     "%s is not a YANG statement or use of extension. Source: %s", identifier.getLocalName(), ref);
91             toBeSkipped.add(keywordTxt);
92         }
93     }
94
95     @Override
96     public void exitStatement(final StatementContext ctx) {
97         final StatementSourceReference ref = DeclarationInTextSource.atPosition(
98             sourceName, ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
99
100         try {
101             KeywordContext keyword = ctx.getChild(KeywordContext.class, 0);
102             String statementName = keyword.getText();
103             QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, statementName);
104             if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier)
105                     && toBeSkipped.isEmpty()) {
106                 writer.endStatement(ref);
107             }
108
109             // No-op if the statement is not on the list
110             toBeSkipped.remove(statementName);
111         } catch (SourceException e) {
112             LOG.warn(e.getMessage(), e);
113         }
114     }
115 }