This is an automated email from the ASF dual-hosted git repository.

lkishalmi pushed a commit to branch use-snakeyaml-parser-improved
in repository https://gitbox.apache.org/repos/asf/netbeans.git

commit 2e9400a80231a4c16325a346d32bf7c3fba29c9c
Author: Laszlo Kishalmi <laszlo.kisha...@gmail.com>
AuthorDate: Wed Oct 13 15:50:06 2021 -0700

    Made the YAML parser recover from errors.
---
 .../modules/languages/yaml/YamlParser.java         | 55 ++++++++++++++--------
 .../modules/languages/yaml/YamlParserResult.java   | 11 ++++-
 .../modules/languages/yaml/YamlSection.java        | 36 +++++++++-----
 3 files changed, 70 insertions(+), 32 deletions(-)

diff --git 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlParser.java 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlParser.java
index ff230f4..9ce1ecf 100644
--- a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlParser.java
+++ b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlParser.java
@@ -18,7 +18,6 @@
  */
 package org.netbeans.modules.languages.yaml;
 
-import java.util.Deque;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.logging.Level;
@@ -103,7 +102,6 @@ public class YamlParser extends 
org.netbeans.modules.parsing.spi.Parser {
         if (startReplace == -1) {
             return;
         }
-
         while (startReplace > -1) {
             int endReplace = source.indexOf(endToken, startReplace) + 1;
             if (endReplace > startReplace) {
@@ -178,28 +176,45 @@ public class YamlParser extends 
org.netbeans.modules.parsing.spi.Parser {
 
         YamlParserResult result = new YamlParserResult(snapshot);
 
-        Deque<YamlSection> sources = new LinkedList<>();
+        LinkedList<YamlSection> sources = new LinkedList<>();
         sources.push(new YamlSection(sb.toString()));
+        int sourceLength = Integer.MAX_VALUE;
+        int stallCounter = 0;
         while (!sources.isEmpty()) {
+            int len = 0;
+            for (YamlSection source : sources) {
+                len += source.length();
+            }
             
-            YamlSection section = sources.pop();
-            try {
-                List<?  extends StructureItem> items = section.collectItems();
-                result.addStructure(items);
-            } catch (ScannerException se) {
-                result.addError(section.processScannerException(snapshot, se));
-//                YamlSection after = 
section.after(se.getProblemMark().get().getIndex());
-//                YamlSection before = 
section.before(se.getContextMark().get().getIndex());
-//                if (!after.isEmpty()) sources.push(after);
-//                if (!before.isEmpty()) sources.push(before);
-            } catch (ParserException pe ){
-                result.addError(section.processParserException(snapshot, pe));
-//                sources.addAll(section.splitOnParserException(pe));
-            } catch (Exception ex) {
-                String message = ex.getMessage();
-                if (message != null && message.length() > 0) {
-                    result.addError(processError(message, snapshot, 0));
+            if (len < sourceLength) {
+                sourceLength = len;
+                stallCounter = 0;
+            } else {
+                stallCounter++;
+            }
+            if (stallCounter < 2) {
+                YamlSection section = sources.pop();
+                try {
+                    List<?  extends StructureItem> items = 
section.collectItems();
+                    result.addStructure(items);
+                } catch (ScannerException se) {
+                    result.addError(section.processException(snapshot, se));
+                    for (YamlSection part : section.splitOnException(se)) {
+                        sources.push(part);
+                    }
+                } catch (ParserException pe ){
+                    result.addError(section.processException(snapshot, pe));
+                    for (YamlSection part : section.splitOnException(pe)) {
+                        sources.push(part);
+                    }
+                } catch (Exception ex) {
+                    String message = ex.getMessage();
+                    if (message != null && message.length() > 0) {
+                        result.addError(processError(message, snapshot, 0));
+                    }
                 }
+            } else {
+                sources.clear();
             }
         }
         return result;
diff --git 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlParserResult.java
 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlParserResult.java
index 4010e73..08f8b7e 100644
--- 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlParserResult.java
+++ 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlParserResult.java
@@ -41,7 +41,16 @@ public class YamlParserResult extends ParserResult {
     }
 
     public void addError(Error error) {
-        errors.add(error);
+        boolean alreadyReported = false;
+        for (Error e : errors) {
+            if ((e.getStartPosition() <= error.getStartPosition()) && 
(e.getEndPosition() >= error.getStartPosition())) {
+                alreadyReported = true;
+                break;
+            }
+        }
+        if (!alreadyReported) {
+            errors.add(error);
+        }
     }
 
     public void addStructure(List<? extends StructureItem> items) {
diff --git 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlSection.java 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlSection.java
index 80befe2..8ad1ed8 100644
--- 
a/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlSection.java
+++ 
b/ide/languages.yaml/src/org/netbeans/modules/languages/yaml/YamlSection.java
@@ -180,17 +180,21 @@ public class YamlSection {
         return item;
     }
 
-    DefaultError processScannerException(Snapshot snapshot, ScannerException 
se) {
-        int contextIndex = getIndex(se.getContextMark());
+    DefaultError processException(Snapshot snapshot, ScannerException se) {
         int problemIndex = getIndex(se.getProblemMark());
-        StringBuilder message = new StringBuilder(se.getContext());
-        message.append(", ").append(se.getProblem());
+        int contextIndex = problemIndex;
+        StringBuilder message = new StringBuilder();
+        if (se.getContext() != null) {
+            contextIndex = getIndex(se.getContextMark());
+            message.append(se.getContext()).append(", ");
+        }
+        message.append(se.getProblem());
         char upper = Character.toUpperCase(message.charAt(0));
         message.setCharAt(0, upper);
         return new DefaultError(null, message.toString(), null, 
snapshot.getSource().getFileObject(), contextIndex, problemIndex, 
Severity.ERROR);
     }
 
-    DefaultError processParserException(Snapshot snapshot, ParserException se) 
{
+    DefaultError processException(Snapshot snapshot, ParserException se) {
         int problemIndex = se.getProblemMark().isPresent() ? 
getIndex(se.getProblemMark()) : 0;
         int contextIndex = problemIndex;
         StringBuilder message = new StringBuilder();
@@ -204,13 +208,23 @@ public class YamlSection {
         return new DefaultError(null, message.toString(), null, 
snapshot.getSource().getFileObject(), contextIndex, problemIndex, 
Severity.ERROR);
     }
 
-    List<YamlSection> splitOnParserException(ParserException pe) {
+    List<YamlSection> splitOnException(ScannerException se) {
+        int problemIndex = se.getProblemMark().get().getIndex();
+        if (se.getContextMark().isPresent()) {
+            int contextIndex = se.getContextMark().get().getIndex();
+            return split(contextIndex, problemIndex);
+        } else {
+            return split(problemIndex, problemIndex);
+        }
+    }
+
+    List<YamlSection> splitOnException(ParserException pe) {
         if (pe.getContextMark().isPresent()) {
             int contextIndex = pe.getContextMark().get().getIndex();
-            return split(contextIndex, contextIndex + 1);
+            return split(contextIndex, contextIndex);
         } else {
             int problemIndex = pe.getProblemMark().get().getIndex();
-            return split(problemIndex, problemIndex + 1);
+            return split(problemIndex, problemIndex);
         }
     }
 
@@ -224,12 +238,12 @@ public class YamlSection {
         if (after.isEmpty()) {
             before = before.trimTail();
         }
-        if (!before.isEmpty()) {
-            ret.add(before);
-        }
         if (!after.isEmpty()) {
             ret.add(after);
         }
+        if (!before.isEmpty()) {
+            ret.add(before);
+        }
         return ret;
     }
 

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to