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
index 04f54c624cb0bf587c9b1b817f4d1f542d8eefaf..526f9d1adcbdec90ddc4ee95d33e46510d999a2a 100644 (file)
@@ -9,26 +9,25 @@ package org.opendaylight.yangtools.yang.parser.impl;
 
 import java.util.ArrayList;
 import java.util.List;
-
-import javax.annotation.concurrent.Immutable;
-
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
+import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.antlr.v4.runtime.tree.ParseTree;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParserBaseListener;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.YangConstants;
-import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
-import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
+
+import javax.annotation.concurrent.Immutable;
 
 @Immutable
 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
@@ -43,9 +42,6 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList
 
     public YangStatementParserListenerImpl(String sourceName) {
         this.sourceName = sourceName;
-        // TODO
-        // this.sourceName = Preconditions.checkNotNull(sourceName, "YangStatementParserListenerImpl#sourceName cannot "
-        //        + "be null");
     }
 
     public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef) {
@@ -61,8 +57,8 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList
 
     @Override
     public void enterStatement(YangStatementParser.StatementContext ctx) {
-        final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getStart().getLine(),
-                ctx.getStart().getCharPositionInLine());
+        final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx
+                .getStart().getLine(), ctx.getStart().getCharPositionInLine());
         boolean action = true;
         QName identifier;
         for (int i = 0; i < ctx.getChildCount(); i++) {
@@ -71,8 +67,7 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList
                 try {
                     identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE,
                             ((YangStatementParser.KeywordContext) child).children.get(0).getText());
-                    if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier)
-                            && toBeSkipped.isEmpty()) {
+                    if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier) && toBeSkipped.isEmpty()) {
                         if (identifier.equals(Rfc6020Mapping.TYPE.getStatementName())) {
                             isType = true;
                         } else {
@@ -80,10 +75,9 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList
                         }
                     } else {
                         if (writer.getPhase().equals(ModelProcessingPhase.FULL_DECLARATION)) {
-                            throw new IllegalArgumentException(identifier.getLocalName() + " is not a YANG statement "
-                                    + "or use of extension. Source: " + ref);
-                        }
-                        else {
+                            throw new IllegalArgumentException(identifier.getLocalName() + " is not a YANG statement " +
+                                    "or use of extension. Source: " + ref);
+                        } else {
                             action = false;
                             toBeSkipped.add(((YangStatementParser.KeywordContext) child).children.get(0).getText());
                         }
@@ -95,17 +89,16 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList
                 try {
                     final String argument = Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child);
                     if (isType) {
-                        if (TypeUtils.isYangTypeBodyStmt(argument)) {
-                            writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, argument), ref);
-                        } else {
-                            writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, Rfc6020Mapping.TYPE
-                                    .getStatementName().getLocalName()), ref);
-                        }
+                            if (TypeUtils.isYangTypeBodyStmtString(argument)) {
+                                writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, argument), ref);
+                            } else {
+                                writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, Rfc6020Mapping
+                                        .TYPE.getStatementName().getLocalName()), ref);
+                            }
                         writer.argumentValue(argument, ref);
                         isType = false;
                     } else if (action) {
-                        writer.argumentValue(
-                                Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref);
+                        writer.argumentValue(Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref);
                     } else {
                         action = true;
                     }
@@ -119,16 +112,15 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList
 
     @Override
     public void exitStatement(YangStatementParser.StatementContext ctx) {
-        final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getStart().getLine(),
-                ctx.getStart().getCharPositionInLine());
+        final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getStart().getLine(), ctx
+                .getStart().getCharPositionInLine());
         for (int i = 0; i < ctx.getChildCount(); i++) {
             ParseTree child = ctx.getChild(i);
             if (child instanceof YangStatementParser.KeywordContext) {
                 try {
                     String statementName = ((YangStatementParser.KeywordContext) child).children.get(0).getText();
                     QName identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE, statementName);
-                    if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier)
-                            && toBeSkipped.isEmpty()) {
+                    if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier) && toBeSkipped.isEmpty()) {
                         writer.endStatement(ref);
                     }