Promoted yang syntactic error handling.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangErrorListener.java
index 2cb1b7be624800856ea4729f1aa7414ce6119619..16f6274bbdddfd2d8bec3758719991897f5f9bc6 100644 (file)
@@ -8,18 +8,38 @@
 package org.opendaylight.yangtools.yang.parser.impl;
 
 import org.antlr.v4.runtime.BaseErrorListener;
+import org.antlr.v4.runtime.Parser;
 import org.antlr.v4.runtime.RecognitionException;
 import org.antlr.v4.runtime.Recognizer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
 final class YangErrorListener extends BaseErrorListener {
-    private static final Logger LOGGER = LoggerFactory.getLogger(YangErrorListener.class);
 
     @Override
     public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
             String msg, RecognitionException e) {
-        LOGGER.warn("line " + line + ":" + charPositionInLine + " " + msg);
+        String module = getModuleName(recognizer);
+        throw new YangParseException(module, line, msg);
+    }
+
+    private String getModuleName(Recognizer<?, ?> recognizer) {
+        String result;
+        if (recognizer instanceof Parser) {
+            try {
+                Parser parser = (Parser) recognizer;
+                String model = parser.getInputStream().getTokenSource().getInputStream().toString();
+                model = model.substring(0, model.indexOf("\n"));
+                model = model.substring(model.indexOf("module") + 6);
+                model = model.substring(0, model.indexOf("{"));
+                model = model.trim();
+                result = model;
+            } catch (Exception e) {
+                result = "";
+            }
+        } else {
+            result = "";
+        }
+        return result;
     }
 
 }