Added module name to error log from parser. 92/2892/2
authorMartin Vitez <mvitez@cisco.com>
Wed, 20 Nov 2013 09:19:45 +0000 (10:19 +0100)
committerMartin Vitez <mvitez@cisco.com>
Thu, 21 Nov 2013 08:06:32 +0000 (09:06 +0100)
Change-Id: I18f7ad0e1af447e2786e623e40bd23701bea77f1
Signed-off-by: Martin Vitez <mvitez@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserListenerUtils.java

index 3f4166bd08331b059caef4946ddf2827dee73f1e..b77db6975ccbbef28aff203978326710f95ee5fb 100644 (file)
@@ -40,6 +40,7 @@ import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Max_elements_stmtC
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Max_value_argContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Min_elements_stmtContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Min_value_argContext;
+import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_stmtContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Must_stmtContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Numerical_restrictionsContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Ordered_by_argContext;
@@ -139,7 +140,8 @@ public final class ParserListenerUtils {
                 if (context != null) {
                     result = context.getChild(0).getText();
                     if (!(result.startsWith("\"")) && result.endsWith("\"")) {
-                        LOG.error("Syntax error at line " + context.getStart().getLine() + ": missing '\"'.");
+                        LOG.error("Syntax error in module {} at line {}: missing '\"'.", getParentModule(treeNode),
+                                context.getStart().getLine());
                     }
                     return result.replace("\"", "");
                 }
@@ -148,6 +150,23 @@ public final class ParserListenerUtils {
         return result;
     }
 
+    private static String getParentModule(final ParseTree ctx) {
+        ParseTree current = ctx;
+        while (current != null && !(current instanceof Module_stmtContext)) {
+            current = current.getParent();
+        }
+        if (current instanceof Module_stmtContext) {
+            Module_stmtContext module = (Module_stmtContext) current;
+            for (int i = 0; i < module.getChildCount(); i++) {
+                if (module.getChild(i) instanceof StringContext) {
+                    final StringContext str = (StringContext) module.getChild(i);
+                    return str.getChild(0).getText();
+                }
+            }
+        }
+        return "";
+    }
+
     /**
      * Parse 'description', 'reference' and 'status' statements and fill in
      * given builder.