2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Throwables;
12 import com.google.common.base.Verify;
13 import java.util.ArrayList;
14 import java.util.List;
15 import javax.annotation.concurrent.Immutable;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.ArgumentContext;
18 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.KeywordContext;
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.model.api.Rfc6020Mapping;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
24 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
25 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
26 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
38 private StatementWriter writer;
39 private final String sourceName;
40 private QNameToStatementDefinition stmtDef;
41 private PrefixToModule prefixes;
42 private final List<String> toBeSkipped = new ArrayList<>();
43 private boolean isType = false;
44 private static final Logger LOG = LoggerFactory.getLogger(YangStatementParserListenerImpl.class);
46 public YangStatementParserListenerImpl(final String sourceName) {
47 this.sourceName = sourceName;
50 public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
52 this.stmtDef = stmtDef;
55 public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef, final PrefixToModule prefixes) {
57 this.stmtDef = stmtDef;
58 this.prefixes = prefixes;
62 public void enterStatement(final YangStatementParser.StatementContext ctx) {
63 final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx
64 .getStart().getLine(), ctx.getStart().getCharPositionInLine());
65 final KeywordContext keywordCtx = Verify.verifyNotNull(ctx.getChild(KeywordContext.class, 0));
66 final ArgumentContext argumentCtx = ctx.getChild(ArgumentContext.class, 0);
68 String keywordTxt = keywordCtx.getText();
70 // FIXME: This is broken for extensions - not all statements are in RFC6020 ns.
71 final QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, keywordTxt);
72 if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier)
73 && toBeSkipped.isEmpty()) {
74 final String argument = argumentCtx != null ? Utils.stringFromStringContext(argumentCtx) : null;
75 // See,s to be fishy specialcase
76 if (identifier.equals(Rfc6020Mapping.TYPE.getStatementName())) {
77 Preconditions.checkArgument(argument != null);
78 if (TypeUtils.isYangTypeBodyStmtString(argument)) {
79 writer.startStatement(QName.create(YangConstants.RFC6020_YIN_MODULE, argument), ref);
81 writer.startStatement(QName.create(YangConstants.RFC6020_YIN_MODULE, Rfc6020Mapping
82 .TYPE.getStatementName().getLocalName()), ref);
84 writer.argumentValue(argument, ref);
86 writer.startStatement(identifier, ref);
87 if(argument != null) {
88 writer.argumentValue(argument, ref);
91 } else if (writer.getPhase().equals(ModelProcessingPhase.FULL_DECLARATION)) {
92 throw new IllegalArgumentException(identifier.getLocalName() + " is not a YANG statement "
93 + "or use of extension. Source: " + ref);
95 toBeSkipped.add(keywordTxt);
97 } catch (SourceException e) {
98 throw Throwables.propagate(e);
103 public void exitStatement(final YangStatementParser.StatementContext ctx) {
104 final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getStart().getLine(), ctx
105 .getStart().getCharPositionInLine());
107 KeywordContext keyword = ctx.getChild(KeywordContext.class, 0);
108 String statementName = keyword.getText();
109 QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, statementName);
110 if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier)
111 && toBeSkipped.isEmpty()) {
112 writer.endStatement(ref);
114 if (toBeSkipped.contains(statementName)) {
115 toBeSkipped.remove(statementName);
117 } catch (SourceException e) {
118 LOG.warn(e.getMessage(), e);