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 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;
30 import javax.annotation.concurrent.Immutable;
33 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
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);
43 public YangStatementParserListenerImpl(String sourceName) {
44 this.sourceName = sourceName;
47 public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef) {
49 this.stmtDef = stmtDef;
52 public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) {
54 this.stmtDef = stmtDef;
55 this.prefixes = prefixes;
59 public void enterStatement(YangStatementParser.StatementContext ctx) {
60 final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getText(), ctx
61 .getStart().getLine(), ctx.getStart().getCharPositionInLine());
62 boolean action = true;
64 for (int i = 0; i < ctx.getChildCount(); i++) {
65 ParseTree child = ctx.getChild(i);
66 if (child instanceof YangStatementParser.KeywordContext) {
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())) {
74 writer.startStatement(identifier, ref);
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);
82 toBeSkipped.add(((YangStatementParser.KeywordContext) child).children.get(0).getText());
85 } catch (SourceException e) {
86 LOG.warn(e.getMessage(), e);
88 } else if (child instanceof YangStatementParser.ArgumentContext) {
90 final String argument = Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child);
92 if (TypeUtils.isYangTypeBodyStmtString(argument)) {
93 writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, argument), ref);
95 writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, Rfc6020Mapping
96 .TYPE.getStatementName().getLocalName()), ref);
98 writer.argumentValue(argument, ref);
101 writer.argumentValue(Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref);
105 } catch (SourceException e) {
106 LOG.warn(e.getMessage(), e);
114 public void exitStatement(YangStatementParser.StatementContext ctx) {
115 final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getText(), 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) {
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);
127 if (toBeSkipped.contains(statementName)) {
128 toBeSkipped.remove(statementName);
130 } catch (SourceException e) {
131 LOG.warn(e.getMessage(), e);